var objIdArray = ["index", "psi", "serviceId", "serviceName", "serviceProvider", "pmtPid", "pcrPid", "videoPid", "audio1Pid", "audio2Pid"];
var COL_NUM = 10;
var SERVICE_ID = 2;
var SERVICE_NAME = 3;
var SERVICE_PROVIDER = 4;
var FOLLOW_INPUT = 1;
var rowNum;

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

$(document).ready(function()
{
	var args = GetArgs();
	slotNo = parseInt(args["slotNo"], 10);
	GetMainContentHeight(BUTTON_TOP_MENU, NO_BUTTON_CHANGE);
	funcUpdateDisplay();
});

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

function checkAll()
{
	var flag = $("#checkAll").prop("checked");
	for(var i = 0; i < rowNum; i++)
	{
		$("#check" + (i + 1)).prop("checked", flag);
	}
	
	if(parent.groupAccess != USER)
	{
		EnableButton("modifyBtn");
	}
}

function changeRowChecked()
{
	if(parent.groupAccess != USER)
	{
		EnableButton("modifyBtn");
	}
		
}

function psiChange(obj)
{
	var psi = parseInt(obj.val(), 10);
	var index = GetIndexFromId(obj, 7);
	if(psi == FOLLOW_INPUT)
	{
		for(var i = SERVICE_ID; i < COL_NUM; i++)
		{
			$("#" + objIdArray[i] + index).attr("disabled", true);
		}
	}
	else
	{
		for(var i = SERVICE_ID; i < COL_NUM; i++)
		{
			$("#" + objIdArray[i] + index).attr("disabled", false);
		}
	}
}

function displayParam()
{
	var data;
	var response;
	var length;
	var xmlHttp = GetHTTPObject();
	var infoData;

	xmlHttp.open("GET", "/goform/formEMR30?type=" + CARD_TS + "&cmd=" + GET + "&language=" + language 
		+ "&slotNo=" + slotNo + "&ran=" + Math.random(), true);
	xmlHttp.onreadystatechange = function()
	{
		if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
		{
			response = AjaxStrDeal(xmlHttp.responseText);
			if(response == "")
			{
				rowNum = 0;
			}
			else
			{
				data = response.split(FIRST_DELIMITER);
				rowNum = data.length;
			}
			DrawTableRow("dataTable", "dataRow", "CaseRow", rowNum);
			if(rowNum != 0)
			{
				updateTableData(data);
			}
			refreshFlag = true;
		}
	}
	xmlHttp.send(null);
}

function updateTableData(data)
{
	var rowData;
	var index;
	
	for(var i = 0; i < rowNum; i++)
	{
		index = i + 1;
		rowData = data[i].split(SECOND_DELIMITER);
		for(var j = 0; j < COL_NUM; j++)
		{
			if(j == 0)
			{
				$("#" + objIdArray[j] + index).html(rowData[j]);
			}
			else
			{
				$("#" + objIdArray[j] + index).val(rowData[j]);
			}
		}
		psiChange($("#psi" + index));
	}
}

function pageApply()
{
	var data;
	var response;
	var xmlHttp = GetHTTPObject();
	
	if(paramCheck() == false)
	{
		return;
	}
	
	data = "type=" + CARD_TS + "&cmd=" + SET + "&language=" + language + "&slotNo=" + slotNo + "&setString=";
	data += generateData();
	
	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), 10);
			if(response == RETURN_OK)
			{
				applyFlag = true;
			}
			else
			{
				alert(returnError[language][response]);
			}
			GeneratePrompt(applyFlag, TOP, APPLY, language);  //设置后页面弹出提示
			applyFlag = false;  //设置标识符恢复为false
			funcUpdateDisplay();
		}
	}
	xmlHttp.send(data);
}

function generateData()
{
	var data;
	var index;
	for(var i = 0; i < rowNum; i++)
	{
		index = i + 1;
		for(var j = 0; j < COL_NUM; j++)
		{
			if((i == 0) && (j == 0))
			{
				data = $("#" + objIdArray[j] + index).html();
			}
			else if((i != 0) && (j == 0))
			{
				data += FIRST_DELIMITER + $("#" + objIdArray[j] + index).html();
			}
			else if((j == SERVICE_NAME) || (j == SERVICE_PROVIDER))
			{
				data +=  SECOND_DELIMITER + $("#" + objIdArray[j] + index).val().replace(/\+/g, "%2B").replace(/\&/g, "%26");
			}
			else
			{
				data += SECOND_DELIMITER + $("#" + objIdArray[j] + index).val();
			}
		}		
	}
	return data;
}

function paramCheck()
{
 	var returnFlag;

	for(var i = 0; i < rowNum; i++)
	{
		returnFlag = ValidateParam($("#serviceId" + (i + 1)), "INT_DEC_HEX", 1, 65535, "<Service ID>", "<节目号>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#serviceName" + (i + 1)), "LENGTH", 0, 31, "<Service Name>", "<业务名称>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateName($("#serviceName" + (i + 1)), language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#serviceProvider" + (i + 1)), "LENGTH", 0, 31, "<Service Provider>", "<业务提供商>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateName($("#serviceProvider" + (i + 1)), language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#pmtPid" + (i + 1)), "INT_DEC_HEX", 32, 8190, "<PMT PID>", "<PMT PID>", language);
		if(returnFlag == false)
		{
			return false;
		}	
		returnFlag = ValidateParam($("#pcrPid" + (i + 1)), "INT_DEC_HEX", 32, 8190, "<PCR PID>", "<PCR PID>", language);
		if(returnFlag == false)
		{
			return false;
		}		
		returnFlag = ValidateParam($("#videoPid" + (i + 1)), "INT_DEC_HEX", 32, 8190, "<Video PID>", "<视频PID>", language);
		if(returnFlag == false)
		{
			return false;
		}	
		returnFlag = ValidateParam($("#audio1Pid" + (i + 1)), "INT_DEC_HEX", 32, 8190, "<Audio 1 PID>", "<音频1 PID>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#audio2Pid" + (i + 1)), "INT_DEC_HEX", 32, 8190, "<Audio 2 PID>", "<音频2 PID>", language);
		if(returnFlag == false)
		{
			return false;
		}
		
	}
	return true;
}

function modifyBulk()
{
	var index;
	var firstCheckIndex;
	for(var i = 0; i < rowNum; i++)
	{
		index = i + 1;
		if($("#check" + index).prop("checked") == true)
		{
			firstCheckIndex = index;
			break;
		}
	}
	$("#modifyPsi").val($("#psi" + (firstCheckIndex)).val());
	$("#modifyPmtPid").val($("#pmtPid" + (firstCheckIndex)).val());
	$("#modifyPmtPidCheck").prop("checked", false);
	$("#modifyPmtPidStep").val("0X0020");
	$("#modifyPcrPid").val($("#pcrPid" + (firstCheckIndex)).val());
	$("#modifyPcrPidCheck").prop("checked", false);
	$("#modifyPcrPidStep").val("0X0020");
	$("#modifyVideoPid").val($("#videoPid" + (firstCheckIndex)).val());
	$("#modifyVideoPidCheck").prop("checked", false);
	$("#modifyVideoPidStep").val("0X0020");
	$("#modifyAudio1Pid").val($("#audio1Pid" + (firstCheckIndex)).val());
	$("#modifyAudio1PidCheck").prop("checked", false);
	$("#modifyAudio1PidStep").val("0X0020");
	$("#modifyAudio2Pid").val($("#audio2Pid" + (firstCheckIndex)).val());
	$("#modifyAudio2PidCheck").prop("checked", false);
	$("#modifyAudio2PidStep").val("0X0020");
	modifyPsiChange();
	$("#modifyDiv").modal();
}

var checkArray = ["modifyPmtPid", "modifyPmtPidCheck", "modifyPmtPidStep", "modifyPcrPid", "modifyPcrPidCheck", "modifyPcrPidStep", "modifyVideoPid", "modifyVideoPidCheck", "modifyVideoPidStep", "modifyAudio1Pid", "modifyAudio1PidCheck", "modifyAudio1PidStep", "modifyAudio2Pid", "modifyAudio2PidCheck", "modifyAudio2PidStep"];
var CHECK_NUM = 15;
function modifyPsiChange()
{
	var modifyPsi = parseInt($("#modifyPsi").val(), 10);
	if(modifyPsi == FOLLOW_INPUT)
	{
		for(var i = 0; i < CHECK_NUM; i++)
		{
			$("#" + checkArray[i]).attr("disabled", true);
		}
	}
	else
	{
		for(var i = 0; i < CHECK_NUM; i++)
		{
			$("#" + checkArray[i]).attr("disabled", false);
		}
	}
	
}

function modifyApply()
{
	var modifyPsi = $("#modifyPsi").val();
	var modifyPsiCheck = $("#modifyPsiCheck").prop("checked");
	var modifyPmtPid = unifyDataFormat($("#modifyPmtPid"));
	var modifyPmtPidCheck = $("#modifyPmtPidCheck").prop("checked");
	var modifyPmtPidStep = unifyDataFormat($("#modifyPmtPidStep"));
	var modifyPcrPid = unifyDataFormat($("#modifyPcrPid"));
	var modifyPcrPidCheck = $("#modifyPcrPidCheck").prop("checked");
	var modifyPcrPidStep = unifyDataFormat($("#modifyPcrPidStep"));
	var modifyVideoPid = unifyDataFormat($("#modifyVideoPid"));
	var modifyVideoPidCheck = $("#modifyVideoPidCheck").prop("checked");
	var modifyVideoPidStep = unifyDataFormat($("#modifyVideoPidStep"));
	var modifyAudio1Pid = unifyDataFormat($("#modifyAudio1Pid"));
	var modifyAudio1PidCheck = $("#modifyAudio1PidCheck").prop("checked");
	var modifyAudio1PidStep = unifyDataFormat($("#modifyAudio1PidStep"));
	var modifyAudio2Pid = unifyDataFormat($("#modifyAudio2Pid"));
	var modifyAudio2PidCheck = $("#modifyAudio2PidCheck").prop("checked");
	var modifyAudio2PidStep = unifyDataFormat($("#modifyAudio2PidStep"));
	
	var checkIndex = 0;
	var checkRowNum = 0;
	
	for(var i = 0; i < rowNum; i++)
	{
		if($("#check" + (i + 1)).prop("checked"))
		{
			checkRowNum++;
		}
	}
	var maxPmtPid = modifyPmtPid + modifyPmtPidStep * (checkRowNum - 1);
	var maxPcrPid = modifyPcrPid + modifyPcrPidStep * (checkRowNum - 1);
	var maxVideoPid = modifyVideoPid + modifyVideoPidStep * (checkRowNum - 1);
	var maxAudio1Pid = modifyAudio1Pid + modifyAudio1PidStep * (checkRowNum - 1);
	var maxAudio2Pid = modifyAudio2Pid + modifyAudio2PidStep * (checkRowNum - 1);
	if(modifyPmtPidCheck)
	{
		returnFlag = ValidateParam($("#modifyPmtPid"), "INT_DEC_HEX", 32, 8190, "<PMT PID>", "<PMT PID>", language);
		if(returnFlag == false)
		{
			return;
		}	
		returnFlag = ValidateParam($("#modifyPmtPidStep"), "INT_DEC_HEX", 20, 8190, "<PMT PID Step>", "<PMT PID 步进值>", language);
		if(returnFlag == false)
		{
			return;
		}
		if((maxPmtPid < 32) || (maxPmtPid > 8190))
		{
			if(language == ENGLISH)
			{
				alert("<PMT PID> should be between 32(0x20) and 8190(0x1ffe)!");
			}
			else if(language == CHINESE)
			{
				alert("<PMT PID>应该在32(0x20)和8190(0x1ffe)之间");
			}
			return;
		}
	}
	if(modifyPcrPidCheck)
	{
		returnFlag = ValidateParam($("#modifyPcrPid"), "INT_DEC_HEX", 32, 8190, "<PCR PID>", "<PCR PID>", language);
		if(returnFlag == false)
		{
			return;
		}	
		returnFlag = ValidateParam($("#modifyPcrPidStep"), "INT_DEC_HEX", 20, 8190, "<PCR PID Step>", "<PCR PID步进值>", language);
		if(returnFlag == false)
		{
			return;
		}	
		if((maxPcrPid < 32) || (maxPcrPid > 8190))
		{
			if(language == ENGLISH)
			{
				alert("<PCR PID> should be between 32(0x20) and 8190(0x1ffe)!");
			}
			else if(language == CHINESE)
			{
				alert("<PCR PID>应该在32(0x20)和8190(0x1ffe)之间");
			}
			return;
		}
	}
	if(modifyVideoPidCheck)
	{
		returnFlag = ValidateParam($("#modifyVideoPid"), "INT_DEC_HEX", 32, 8190, "<Video PID>", "<视频PID>", language);
		if(returnFlag == false)
		{
			return;
		}	
		returnFlag = ValidateParam($("#modifyVideoPidStep"), "INT_DEC_HEX", 20, 8190, "<Video PID Step>", "<视频PID步进值>", language);
		if(returnFlag == false)
		{
			return;
		}
		if((maxVideoPid < 32) || (maxVideoPid > 8190))
		{
			if(language == ENGLISH)
			{
				alert("<Video PID> should be between 32(0x20) and 8190(0x1ffe)!");
			}
			else if(language == CHINESE)
			{
				alert("<视频PID>应该在32(0x20)和8190(0x1ffe)之间");
			}
			return;
		}
	}
	if(modifyAudio1PidCheck)
	{
		returnFlag = ValidateParam($("#modifyAudio1Pid"), "INT_DEC_HEX", 32, 8190, "<Audio1 PID>", "<音频1PID>", language);
		if(returnFlag == false)
		{
			return;
		}	
		returnFlag = ValidateParam($("#modifyAudio1PidStep"), "INT_DEC_HEX", 20, 8190, "<Audio1 PID Step>", "<音频1PID步进值>", language);
		if(returnFlag == false)
		{
			return;
		}
		if((maxAudio1Pid < 32) || (maxAudio1Pid > 8190))
		{
			if(language == ENGLISH)
			{
				alert("<Audio1 PID> should be between 32(0x20) and 8190(0x1ffe)!");
			}
			else if(language == CHINESE)
			{
				alert("<音频1PID>应该在32(0x20)和8190(0x1ffe)之间");
			}
			return;
		}
	}
	if(modifyAudio2PidCheck)
	{
		returnFlag = ValidateParam($("#modifyAudio2Pid"), "INT_DEC_HEX", 32, 8190, "<Audio2 PID>", "<音频2PID>", language);
		if(returnFlag == false)
		{
			return;
		}	
		returnFlag = ValidateParam($("#modifyAudio2PidStep"), "INT_DEC_HEX", 20, 8190, "<Audio2 PID Step>", "<音频2PID步进值>", language);
		if(returnFlag == false)
		{
			return;
		}
		if((maxAudio2Pid < 32) || (maxAudio2Pid > 8190))
		{
			if(language == ENGLISH)
			{
				alert("<Audio2 PID> should be between 32(0x20) and 8190(0x1ffe)!");
			}
			else if(language == CHINESE)
			{
				alert("<音频2PID>应该在32(0x20)和8190(0x1ffe)之间");
			}
			return;
		}
	}
	
	for(var i = 0; i < rowNum; i++)
	{
		if($("#check" + (i + 1)).prop("checked"))
		{
			checkIndex++;
			if(modifyPsiCheck)
			{
				$("#psi" + (i + 1)).val(modifyPsi);
			}
			if(modifyPmtPidCheck)
			{
				$("#pmtPid" + (i + 1)).val("0X" + (modifyPmtPid + modifyPmtPidStep * (checkIndex - 1)).toString(16));
			}
			if(modifyPcrPidCheck)
			{
				$("#pcrPid" + (i + 1)).val("0X" + (modifyPcrPid + modifyPcrPidStep * (checkIndex - 1)).toString(16));
			}
			if(modifyVideoPidCheck)
			{
				$("#videoPid" + (i + 1)).val("0X" + (modifyVideoPid + modifyVideoPidStep * (checkIndex - 1)).toString(16));
			}
			if(modifyAudio1PidCheck)
			{
				$("#audio1Pid" + (i + 1)).val("0X" + (modifyAudio1Pid + modifyAudio1PidStep * (checkIndex - 1)).toString(16));
			}
			if(modifyAudio2PidCheck)
			{
				$("#audio2Pid" + (i + 1)).val("0X" + (modifyAudio2Pid + modifyAudio2PidStep * (checkIndex - 1)).toString(16));
			}
		}
		psiChange($("#psi" + (i + 1)));
	}
	$.modal.close();
	EnableButton("applyBtn");
}


function cancelModify()
{
	$.modal.close();
}