var generalIdArray = ["decodeSwitch", "syncMode", "errHandle", "pcrAdjust"];
var progDecodeIdArray = ["service", "pcrPid", "videoPid", "", "ttPid"];
var videoDecodeIdArray = ["videoPattern", "aspectRatio", "imageScaling"];
var audioDecodeIdArray = ["outMute", "vol", "audioMode", "embMode"];
var vbiIdArray = ["ccSwitch", "ccDelay", "ccLine", "CEA608Transport"];
var objIdArray = [generalIdArray, progDecodeIdArray, videoDecodeIdArray, audioDecodeIdArray, vbiIdArray];
var SEC_NUM = 5;
var PROGRAM_DECODE = 1;
var SERVICE = 0;
var AUDIO_PID = 3;
var FORTH_DELIMITER = "<*4*>";
var allProgPidList;
var PCR_TYPE = 0;
var VIDEO_TYPE = 1;
var AUDIO_TYPE = 2;
var TT_TYPE = 3;

var serviceList;
var serviceNum;

var NO_SERVICE = -1;
var CUSTOM = -2;
var noServiceList = ["None", "无"];
var customList = ["Custom", "自定义"];
var pidDisplayFlag = false;
var buttonShowText = ["Show PID List >>", "显示PID列表>>"];
var buttonHideText = ["Hide PID List >>", "隐藏PID列表>>"];

window.onresize = function()
{
	GetMainContentHeight(BUTTON_BOTTOM_MENU, BUTTON_CHANGE);
}

$(document).ready(function()
{
	var args = GetArgs();
	slotNo = parseInt(args["slotNo"], 10);
	portNo = parseInt(args["portNo"], 10);
	GetMainContentHeight(BUTTON_BOTTOM_MENU, BUTTON_CHANGE);
	funcUpdateDisplay();
	$("#pidListBtn").click(function()
	{
		if(pidDisplayFlag == false)
		{
			$("#pidListDiv").show();
			$(this).val(buttonHideText[language]);
			pidDisplayFlag = true;
		}
		else
		{
			$("#pidListDiv").hide();
			$(this).val(buttonShowText[language]);
			pidDisplayFlag = false;
		}
	});
});

function funcUpdateDisplay() 
{
	DisableButton("applyBtn");
	DisableButton("refreshBtn");
	PageLoadStatus(true);
	
	getProgramPid();
	
	
	ResetColor();
	EnableButton("refreshBtn");
	PageLoadStatus(false);
}

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

	xmlHttp.open("GET", "/goform/formEMR30?type=" + PORT_CONFIG + "&cmd=" + GET + 
		"&language=" + language + "&slotNo=" + slotNo + "&portNo=" + portNo + "&ran=" + Math.random(), true);
	xmlHttp.onreadystatechange = function()
	{
		if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
		{
			response = AjaxStrDeal(xmlHttp.responseText);						
			if(response != "")
			{
				data = response.split(FIRST_DELIMITER);
				if(data[SEC_NUM] == "")
				{
					serviceNum = 0;
				}
				else
				{
					serviceList = data[SEC_NUM].split(SECOND_DELIMITER);
					serviceNum = serviceList.length;
				}
				generateService();
				for(var i = 0; i < SEC_NUM; i++)
				{
					sectionData = data[i].split(SECOND_DELIMITER);
					length = sectionData.length;
					for(var j = 0; j < length; j++)
					{
						if((i == PROGRAM_DECODE) && (j == AUDIO_PID))
						{
							if($("#service").val() == CUSTOM)
							{
								$("#audioPidInput").show();
								$("#audioPidInput").val(sectionData[j]);
								$("#audioPidSelect").hide();
							}
							else
							{
								$("#audioPidSelect").show();
								$("#audioPidSelect").val(sectionData[j]);
								$("#audioPidInput").hide();
							}
						}
						else
						{
							$("#" + objIdArray[i][j]).val(sectionData[j]);
							if((i == PROGRAM_DECODE) && (j == SERVICE))
							{
								updateAudioPid();
							}
						}						
					}
				}
				var decodeMode = parseInt($("#service").val(), 10);	
				if(decodeMode == CUSTOM)
				{		
					$("#pcrPid").attr("disabled", false);
					$("#videoPid").attr("disabled", false);
					$("#ttPid").attr("disabled", false);
					
				} 
				else
				{			
					$("#pcrPid").attr("disabled", true);
					$("#videoPid").attr("disabled", true);
					$("#ttPid").attr("disabled", true);
				}
				refreshFlag = true;
			}
		}
	}
	xmlHttp.send(null);
}

function updateAudioPid()
{
	var $audioPidObj = $("#audioPidSelect");
	$audioPidObj.empty();
	var curServiceId = parseInt($("#service").val(), 10);
	if(curServiceId == NO_SERVICE)
	{
		option = $("<option>").text(noServiceList[language]).val(NO_SERVICE);
		$audioPidObj.append(option);
	}
	else
	{
		var progNum = allProgPidList.length;
		for(var i = 0; i < progNum; i++)
		{
			var progData = allProgPidList[i].split(SECOND_DELIMITER);
			if(curServiceId == progData[0])
			{
				var curPidList = progData[2].split(THIRD_DELIMITER);
				var curPidNum = curPidList.length;
				for(var j = 0; j < curPidNum; j++)
				{
					curPidData = curPidList[j].split(FORTH_DELIMITER);
					if(curPidData[0] == AUDIO_TYPE)
					{
						option = $("<option>").text(curPidData[1]).val(curPidData[1]);
						$audioPidObj.append(option);
					}
				}
				if(($audioPidObj).find("option").length == 0)
				{
					option = $("<option>").text(noServiceList[language]).val(NO_SERVICE);
					$audioPidObj.append(option);
				}
			}
		}
	}
}

function getProgramPid() 
{
	var progNum;
	var progData;
	var pidList;
	var sectionData;
	var response;
	var length;
	var xmlHttp = GetHTTPObject();

	xmlHttp.open("GET", "/goform/formEMR30?type=" + PROGRAM_PID + "&cmd=" + GET + 
		"&language=" + language + "&slotNo=" + slotNo + "&portNo=" + portNo + "&ran=" + Math.random(), true);
	xmlHttp.onreadystatechange = function()
	{
		if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
		{
			response = AjaxStrDeal(xmlHttp.responseText);
			if(response == "")
			{
				progNum = 0;
			}
			else
			{
				allProgPidList = response.split(FIRST_DELIMITER);
				progNum = allProgPidList.length;
			}
			DrawTableRow("dataTable", "dataRow", "CaseRow", progNum);
			for(var i = 0; i < progNum; i++)
			{
				progData = allProgPidList[i].split(SECOND_DELIMITER);
				$("#serviceId" + (i + 1)).html(progData[0]);
				$("#serviceName" + (i + 1)).html(progData[1]);
				if(progData[2] == "")
				{
					pidNum = 0;
				}
				else
				{
					pidList = progData[2].split(THIRD_DELIMITER);
					pidNum = pidList.length;
				}
				DrawTableRow("pidDataTable" + (i + 1), "pidDataRow", "pidCaseRow", pidNum);
				for(var j = 0; j < pidNum; j++)
				{
					pidData = pidList[j].split(FORTH_DELIMITER);
					$("#pidDataTable" + (i + 1) + " #pid" + (j + 1)).html(pidData[1]);
					$("#pidDataTable" + (i + 1) + " #elementName" + (j + 1)).html(pidData[2]);
				}
			}
			displayParam();
		}
	}
	xmlHttp.send(null);
}

function generateService()
{
	var service = $("#service");
	var option;
	
	service.empty();	
	if(serviceNum == 0)
	{
		option = $("<option>").text(noServiceList[language]).val(NO_SERVICE);
		service.append(option);
	}
	else
	{
		for(var i = 0; i < serviceNum; i++)
		{
			var serviceName = serviceList[i].split(THIRD_DELIMITER)[1];
			var serviceId = serviceList[i].split(THIRD_DELIMITER)[0];
			option = $("<option>").text(serviceName).val(serviceId);
			service.append(option);
		}
		option = $("<option>").text(customList[language]).val(CUSTOM);
		service.append(option);
	}
}

function changeDecMode()
{
	var decodeMode = parseInt($("#service").val(), 10);
	
	if(decodeMode == CUSTOM)
	{
		$("#audioPidInput").show();
		$("#audioPidInput").val("0x1FFF");
		$("#audioPidSelect").hide();
		$("#pcrPid").val("0x1FFF");
		$("#videoPid").val("0x1FFF");
		$("#ttPid").val("0x1FFF");
		$("#pcrPid").attr("disabled", false);
		$("#videoPid").attr("disabled", false);
		$("#ttPid").attr("disabled", false);
		
	} 
	else
	{			
		$("#audioPidSelect").show();
		$("#audioPidInput").hide();
		updateAudioPid();
		updateOtherPidValue();
		$("#pcrPid").attr("disabled", true);
		$("#videoPid").attr("disabled", true);
		$("#ttPid").attr("disabled", true);
	}
}

function updateOtherPidValue()
{
	var curServiceId = parseInt($("#service").val(), 10);
	if(curServiceId == NO_SERVICE)
	{
		return;
	}
	else
	{
		var progNum = allProgPidList.length;
		for(var i = 0; i < progNum; i++)
		{
			var progData = allProgPidList[i].split(SECOND_DELIMITER);
			if(curServiceId == progData[0])
			{
				var curPidList = progData[2].split(THIRD_DELIMITER);
				var curPidNum = curPidList.length;
				for(var j = 0; j < curPidNum; j++)
				{
					curPidData = curPidList[j].split(FORTH_DELIMITER);
					if(curPidData[0] == PCR_TYPE)
					{
						$("#pcrPid").val(curPidData[1]);
					}
					else if(curPidData[0] == VIDEO_TYPE)
					{
						$("#videoPid").val(curPidData[1]);
					}
					else if(curPidData[0] == TT_TYPE)
					{
						$("#ttPid").val(curPidData[1]);
					}
				}
			}
		}
	}
}
function pageApply()
{
	var data;
	var response;
	var xmlHttp = GetHTTPObject();
	
	if(paramCheck() == false)
	{
		return;
	}
	
	data = "type=" + PORT_CONFIG + "&cmd=" + SET + "&language=" + language + "&slotNo=" + slotNo + "&portNo=" + portNo + "&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, BOTTOM, APPLY, language);  //设置后页面弹出提示
			applyFlag = false;  //设置标识符恢复为false
			funcUpdateDisplay();
		}
	}
	xmlHttp.send(data);
}

function paramCheck()
{
 	var returnFlag;
	var decodeMode = parseInt($("#service").val(), 10);
	
	returnFlag = ValidateParam($("#ccDelay"), "INT_DEC", 1, 25, "<CC Delay>", "<CC延时输出>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	returnFlag = ValidateParam($("#ccLine"), "INT_DEC", 4, 19, "<CC Line Num>", "<CC行设置>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	if(decodeMode == CUSTOM)
	{
		returnFlag = ValidateParam($("#pcrPid"), "INT_DEC_HEX", 0, 8191, "<Decode PCR PID>", "<解码PCR PID>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#videoPid"), "INT_DEC_HEX", 0, 8191, "<Decode Video PID>", "<解码视频PID>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#audioPidInput"), "INT_DEC_HEX", 0, 8191, "<Decode Audio PID>", "<解码音频PID>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#ttPid"), "INT_DEC_HEX", 0, 8191, "<Teletext PID>", "<解码TT PID>", language);
		if(returnFlag == false)
		{
			return false;
		}
	}
	
	returnFlag = ValidateParam($("#vol"), "INT_DEC", -30, 12, "<Vol Attenuation>", "<输出音量衰减>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	return true;
}

function generateData()
{
	var data;
	var length;
	
	for(var i = 0; i < SEC_NUM; i++)
	{
		length = objIdArray[i].length;
		for(var j = 0; j < length; j++)
		{
			if((i == 0) && (j == 0))
			{
				data = $("#" + objIdArray[i][j]).val();
			}
			else if((i != 0) && (j == 0))
			{
				data += FIRST_DELIMITER + $("#" + objIdArray[i][j]).val();
			}
			else if((i == PROGRAM_DECODE) && (j == AUDIO_PID))
			{
				if($("#service").val() == CUSTOM)
				{
					data += SECOND_DELIMITER + $("#audioPidInput").val();
				}
				else
				{
					data += SECOND_DELIMITER + $("#audioPidSelect").val();
				}				
			}
			else
			{
				data += SECOND_DELIMITER + $("#" + objIdArray[i][j]).val();
			}
		}
	}
	
	return data;
}