var videoEncodeIdArray = ["videoSwitch", "lightness", "contrast", "saturation", "chroma", "bitrate", "videoSelection", "resolution", "profile", "level", "aspectRatio", "gopStruct", "gopLen", "idrFrequency", "entropyCoding", "unlockCtrl", "testPattern"];
var audioEncode1IdArray = ["audioStatus1", "encodingType1", "lrSel1", "bitRate1", "volume1", "mode1", "delay1"];
var tsIdArray = ["programNo", "serviceName", "serviceProvider", "pmtPid", "pcrPid", "videoPid", "audioPid"];
var txtIdArray = ["txtPositionX", "txtPositionY", "textContent"];
var objIdArray = [videoEncodeIdArray, audioEncode1IdArray, tsIdArray, txtIdArray];
var SEC_NUM = 4;

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();
});

function funcUpdateDisplay() 
{
	DisableButton("applyBtn");
	DisableButton("refreshBtn");
	PageLoadStatus(true);
	
	displayParam();
	
	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);
				for(var i = 0; i < SEC_NUM; i++)
				{
					sectionData = data[i].split(SECOND_DELIMITER);
					length = sectionData.length;
					for(var j = 0; j < length; j++)
					{
						$("#" + objIdArray[i][j]).val(sectionData[j]);
					}
				}
				refreshFlag = true;
			}
		}
	}
	xmlHttp.send(null);
}

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;

	returnFlag = ValidateParam($("#lightness"), "INT_DEC", 0, 128, "<Video Bitrate(bps)>", "<亮度>", language);
	if(returnFlag == false)
	{
		return false;
	} 
	returnFlag = ValidateParam($("#contrast"), "INT_DEC", 0, 128, "<Video Bitrate(bps)>", "<对比度>", language);
	if(returnFlag == false)
	{
		return false;
	}  	
	returnFlag = ValidateParam($("#saturation"), "INT_DEC", 0, 128, "<Video Bitrate(bps)>", "<饱和度>", language);
	if(returnFlag == false)
	{
		return false;
	}  	
	returnFlag = ValidateParam($("#chroma"), "INT_DEC", 0, 128, "<Video Bitrate(bps)>", "<色度>", language);
	if(returnFlag == false)
	{
		return false;
	} 
	returnFlag = ValidateParam($("#bitrate"), "INT_DEC", 300000, 50000000, "<Video Bitrate(bps)>", "<视频码率(bps)>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	returnFlag = ValidateParam($("#gopLen"), "INT_DEC", 1, 30, "<GOP Size>", "<GOP长度>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	returnFlag = ValidateParam($("#idrFrequency"), "INT_DEC", 0, 15, "<IDR Frequency(GOPs)>", "<IDR频率(GOPs)>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	returnFlag = ValidateParam($("#delay1"), "INT_DEC", -500, 1400, "<Audio Delay(ms)>", "<音频延时(ms)>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	
	returnFlag = ValidateParam($("#programNo"), "INT_DEC_HEX", 1, 65535, "<Service ID>", "<节目号>", language);
	if(returnFlag == false)
	{
		return false;
	}
	returnFlag = ValidateParam($("#serviceName"), "LENGTH", 0, 31, "<Service Name>", "<业务名称>", language);
	if(returnFlag == false)
	{
		return false;
	}
	returnFlag = ValidateName($("#serviceName"), language);
	if(returnFlag == false)
	{
		return false;
	}
	returnFlag = ValidateParam($("#serviceProvider"), "LENGTH", 0, 31, "<Service Provider>", "<业务提供商>", language);
	if(returnFlag == false)
	{
		return false;
	}
	returnFlag = ValidateName($("#serviceProvider"), language);
	if(returnFlag == false)
	{
		return false;
	}
	returnFlag = ValidateParam($("#pmtPid"), "INT_DEC_HEX", 20, 8191, "<PMT PID>", "<PMT PID>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	returnFlag = ValidateParam($("#pcrPid"), "INT_DEC_HEX", 20, 8191, "<PCR PID>", "<PCR PID>", language);
	if(returnFlag == false)
	{
		return false;
	}
		
	returnFlag = ValidateParam($("#videoPid"), "INT_DEC_HEX", 16, 8190, "<Video PID>", "<视频PID>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	returnFlag = ValidateParam($("#audioPid"), "INT_DEC_HEX", 20, 8190, "<Audio PID>", "<音频PID>", language);
	if(returnFlag == false)
	{
		return false;
	}

	
	returnFlag = ValidateParam($("#txtPositionX"), "INT_DEC", 1, 1919, "<TXT Start PosX>", "<字幕起始点横坐标>", language);
	if(returnFlag == false)
	{
		return false;
	}
	
	returnFlag = ValidateParam($("#txtPositionY"), "INT_DEC", 1, 1079, "<TXT Start PosX>", "<字幕起始点纵坐标>", language);
	if(returnFlag == false)
	{
		return false;
	}
	returnFlag = ValidateParam($("#textContent"), "LENGTH", 0, 63, "<Text Content>", "<文字内容>", 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
			{
				data += SECOND_DELIMITER + $("#" + objIdArray[i][j]).val();
			}
		}
	}
	
	return data;
}