var objIdArray = ["outPort", "outServiceId", "outServiceName", "backupMode", "workState", "mainCard", "mainPort", "mainDescType", "mainDesc", "mainServiceId", "mainServiceName", "backupCard", "backupPort", "backupDescType", "backupDesc", "backupServiceId", "backupServiceName"];
var rowNum = 0;
var initRow = true;
var curRespRowNum;
var rowIndex;

var COL_NUM = 17;
var portDataArray = [];
var outSlotNoArray = [];
var outSlotNo;

var OUT_PORT = 0;
var BACKUP_MODE = 3;
var WORK_STATE = 4;
var MAIN_PORT = 6;
var MAIN_DESC_TYPE = 7;
var MAIN_DESC = 8;
var BACKUP_PORT = 12;
var BACKUP_DESC_TYPE = 13;
var BACKUP_DESC = 14;
var SUPPORT_PORT_DESC = 0;

var INVALID = 0;
var MAIN = 1;
var BACKUP = 2;
var workStateEn = ["Invalid", "Main", "Backup"];
var workStateCn = ["无效", "主路", "备路"];
var workState = [workStateEn, workStateCn];

var backupModeEn = ["", "Assign Main", "Assign Backup", "Main Priority", "One Way Switch", "Floating"];
var backupModeCn = ["", "指定主路", "指定备路", "主路优先", "单向切换", "浮动"];
var backupMode = [backupModeEn, backupModeCn];
var OTHER = 7;

var mainMenu = ["Backup", "备份"];
var treeMenu = ["Service Backup", "节目备份"];
var detailMenu = ["Detail Edit", "详细编辑"];
var cardArray = ["Card", "卡"];
var gbeArray = ["GbE", "网口"];
var portArray = ["Port", "端口"];
var progArray = ["Service", "节目"];
var ONE_GBE_PORT_NUM = 256;
var GBE_ONE = 1;
var GBE_TWO = 2;

window.onresize = function()
{
	GetMainContentHeight(BUTTON_TOP_NO_MENU, NO_BUTTON_CHANGE);
}

$(document).ready(function()
{
	var args = GetArgs();
	outSlotNo = parseInt(args["slotNo"], 10);
	GetMainContentHeight(BUTTON_TOP_NO_MENU, NO_BUTTON_CHANGE);
	funcUpdateDisplay();
	
	if(parent.groupAccess != USER)
	{
		$("input[type=checkbox]").live("click", function()
		{
			EnableButton("deleteBtn");	
		});
	}
});

function funcUpdateDisplay() 
{		
	DisableButton("applyBtn");
	DisableButton("refreshBtn");
	DisableButton("deleteBtn");
	PageLoadStatus(true);
	
	displayParam();
	
	ResetColor();
	PageLoadStatus(false);
	$("input[type=checkbox]").prop("checked", false);
}

function checkAll()
{
	for(var i = 0; i < rowNum; i++)
	{
		$("#check" + (i + 1)).prop("checked", $("#checkAll").prop("checked"));
	}
}

function displayParam() 
{
	var data;
	var rowData;
	var response;
	var indexInfo;
	var curRowNum;
	var xmlHttp = GetHTTPObject();
	if(initRow)
	{
		rowIndex = 0;		
	}

	xmlHttp.open("GET", "/goform/formEMR30?type=" + PROGRAM_BACKUP + "&cmd=" + GET + "&rowIndex=" + rowIndex + 
		"&language=" + language + "&slotNo=" + slotNo + "&outSlotNo=" + outSlotNo + "&ran=" + Math.random(), true);
	xmlHttp.onreadystatechange = function()
	{
		if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
		{
			response = AjaxStrDeal(xmlHttp.responseText);
			if(response != "")
			{
				initRow = false;
				data = response.split(FIRST_DELIMITER);
				curRespRowNum = data.length - 1;
				indexInfo = data[curRespRowNum].split(SECOND_DELIMITER);
				rowIndex = parseInt(indexInfo[0], 10);
				rowNum = parseInt(indexInfo[1], 10);				
				curRowNum = $("#dataTable tr").filter(".mainCaseRow").length;
				if((curRowNum < rowNum) && (curRowNum < rowIndex))
				{
					DrawTableTwoRow("dataTable", "mainDataRow", "mainCaseRow", "backupDataRow", "backupCaseRow", rowIndex);
				}
				else if(curRowNum > rowNum)
				{
					DrawTableTwoRow("dataTable", "mainDataRow", "mainCaseRow", "backupDataRow", "backupCaseRow", rowNum);
				}
				if(curRespRowNum != 0)
				{
					updateTableData(data);
				}
				if(rowIndex < rowNum)
				{
					setTimeout("displayParam()", 5);
				}
				else if(rowIndex == rowNum)
				{
					EnableButton("refreshBtn");
					initRow = true;
					refreshFlag = true;
				}
			}
		}
	}
	xmlHttp.send(null);
}

function updateTableData(data)
{
	var rowData;
	var index;
	
	for(var i = (rowIndex - curRespRowNum); i < rowIndex; i++)
	{
		index = i + 1;
		rowData = data[i - (rowIndex - curRespRowNum)].split(SECOND_DELIMITER);
		outSlotNoArray[index] = rowData[COL_NUM]; //已经不存在的输出卡 的备份删除的时候用到
		for(var j = 0; j < COL_NUM; j++)
		{
			if(( j == MAIN_DESC_TYPE) || (j == BACKUP_DESC_TYPE))
			{				
				continue;
			}
			else if(j == OUT_PORT)
			{
				$("#" + objIdArray[j] + index).html(getPortDisplayStr(outSlotNo, parseInt(rowData[j], 10), language));
				portDataArray[index] = rowData[j];
			}
			else if((j == MAIN_DESC) || (j == BACKUP_DESC))
			{
				if(parseInt(rowData[j - 1], 10) == SUPPORT_PORT_DESC)
				{
					$("#" + objIdArray[j] + index).html(rowData[j]);
				}
				else
				{
					$("#" + objIdArray[j] + index).html($("#" + objIdArray[j - 2] + index).html());
				}
			}
			else if((j == MAIN_PORT)||(j == BACKUP_PORT))
			{
				$("#" + objIdArray[j] + index).html(getPortDisplayStr((parseInt(rowData[j - 1], 10) - 1), parseInt(rowData[j], 10), language));
			}
			else if(j == BACKUP_MODE)
			{
				$("#" + objIdArray[j] + index).html(backupMode[language][parseInt(rowData[j], 10)]);
			}
			else if(j == WORK_STATE)
			{
				if(parseInt(rowData[j], 10) == INVALID)
				{
					$("#modify" + index).css("display", "none");
					$("#mainDataRow" + index + " td").css("color", "#777e88");
					$("#backupDataRow" + index + " td").css("color", "#777e88");
				}
				else
				{
					$("#modify" + index).css("display", "");
					$("#mainDataRow" + index + " td").css("color", "#000");
					$("#backupDataRow" + index + " td").css("color", "#000");
				}
				$("#" + objIdArray[j] + index).html(workState[language][parseInt(rowData[j], 10)]);
			}
			else
			{
				$("#" + objIdArray[j] + index).html(rowData[j]);
			}			
		}
	}
}

/*************************************************
Function: 		deleteBackup
Description: 	用于删除备份
Input: 			无
Output:			无
Return: 		无
Others: 		无
*************************************************/
function deleteBackup()
{
	var data;
	var rowData;
	var index;
	var confirmPrompt;
	var xmlHttp = GetHTTPObject();
	var response;
	var deleteBackup = "";
	var deleteNum = 0;
	
	if(language == ENGLISH)
	{
		confirmPrompt = "Are you sure to delete the checked backup?";
	}
	else if(language == CHINESE)
	{
		confirmPrompt = "确定要删除选中的备份吗?";
	}
	if(window.confirm(confirmPrompt))
	{
	
		data = "type=" + PROGRAM_BACKUP + "&cmd=" + DELETE + "&language=" + language + "&slotNo=" + slotNo + "&outSlotNo=" + outSlotNo + "&setString=";	

		for(var i = 0; i < rowNum; i++)
		{
			index = i + 1;
			if($("#check" + index).prop("checked") == true)
			{
				rowData = outSlotNoArray[index] + SECOND_DELIMITER + portDataArray[index] + SECOND_DELIMITER + parseInt($("#outServiceId" + index).html());
				if(deleteNum == 0)
				{
					deleteBackup = rowData;
				}
				else
				{
					deleteBackup += FIRST_DELIMITER + rowData;
				}
				deleteNum++;
			}
		}
		if(deleteNum == 0)
		{
			if(language == ENGLISH)
			{
				alert("Please select one backup at least!");
			}
			else
			{
				alert("请至少选中一个备份！");
			}
			return;
		}
		data += deleteBackup;
		
		xmlHttp.open("POST", "/goform/formEMR30", true)
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", data.length);
	
		xmlHttp.onreadystatechange = function()
		{
			if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
			{
				response = parseInt(AjaxStrDeal(xmlHttp.responseText));
				if(response == RETURN_OK)
				{
					if(language == ENGLISH)
					{
						alert("Service backup delete successfully!");
					}
					else if(language == CHINESE)
					{
						alert("备份删除成功！");
					}
				}
				else
				{
					alert(returnError[language][response]);
				}
				funcUpdateDisplay();
			}
		}
		xmlHttp.send(data);
	}
}

function modifyBackup(obj)
{
	var curGbeNo;
	var curPortNo;
	var detailString;
	var index = GetIndexFromId(obj, 11);
	var outProgIdArray = [];
	outProgIdArray.push("O", outSlotNo + 1, portDataArray[index], parseInt($("#outServiceId" + index).html()));
	outProgId = outProgIdArray.join("_");
	if(parseInt(outSlotNo, 10) == MAIN_GBE_CARD)
	{
		if(parseInt(outProgIdArray[2], 10) > ONE_GBE_PORT_NUM)
		{
			curGbeNo = GBE_TWO;
			curPortNo = parseInt(outProgIdArray[2], 10) - ONE_GBE_PORT_NUM;
		}
		else
		{
			curGbeNo = GBE_ONE;
			curPortNo = outProgIdArray[2];
		}
		detailString = " > " + mainMenu[language] + " > " + treeMenu[language] + " > " + detailMenu[language] + " (" + cardArray[language] + outProgIdArray[1] + "-" + gbeArray[language] + curGbeNo + portArray[language] + curPortNo + progArray[language] + outProgIdArray[3] + ")";
	}
	else
	{
		detailString = " > " + mainMenu[language] + " > " + treeMenu[language] + " > " + detailMenu[language] + " (" + cardArray[language] + outProgIdArray[1] + "-" + portArray[language] + outProgIdArray[2] + progArray[language] + outProgIdArray[3] + ")";
	}
	if(language == ENGLISH)
	{
		self.location.replace("../mux/program_backup_en.asp?ID=" + outProgId);		
	}
	else
	{
		self.location.replace("../mux/program_backup_cn.asp?ID=" + outProgId);
	}
	parent.document.getElementById( "menuBar").innerHTML = detailString;
}
