var objIdArray = ["index", "switch", "encodingType", "bitrate", "resolution", "gopLen", "codingMode", "preProcessing", "eteDelay"];
var COL_NUM = 9;
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 displayParam()
{
	var data;
	var response;
	var length;
	var xmlHttp = GetHTTPObject();
	var infoData;

	xmlHttp.open("GET", "/goform/formEMR30?type=" + CARD_VIDEO + "&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]);
			}
		}
	}
}

function pageApply()
{
	var data;
	var response;
	var xmlHttp = GetHTTPObject();
	
	if(paramCheck() == false)
	{
		return;
	}
	
	data = "type=" + CARD_VIDEO + "&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
			{
				data += SECOND_DELIMITER + $("#" + objIdArray[j] + index).val();
			}
		}		
	}
	return data;
}

function paramCheck()
{
 	var returnFlag;

	for(var i = 0; i < rowNum; i++)
	{
		returnFlag = ValidateParam($("#bitrate" + (i + 1)), "INT_DEC", 300000, 15000000, "<Video Bitrate(bps)>", "<视频码率(bps)>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#gopLen" + (i + 1)), "INT_DEC", 1, 300, "<GOP Size>", "<GOP长度>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#eteDelay" + (i + 1)), "INT_DEC", 3500, 4500, "<ETE Delay(ms)>", "<ETE延时(ms)>", language);
		if(returnFlag == false)
		{
			return false;
		}
	}
	return true;
}

function modifyBulk()
{
	$("#modifySwitch").val("1");
	$("#modifySwitchCheck").prop("checked", false);
	$("#modifyEncodingType").val("0");
	$("#modifyEncodingTypeCheck").prop("checked", false);
	$("#modifyBitrate").val("8000000");
	$("#modifyBitrateCheck").prop("checked", false);
	$("#modifyResolution").val("0");
	$("#modifyResolutionCheck").prop("checked", false);
	$("#modifyGopLen").val("12");
	$("#modifyGopLenCheck").prop("checked", false);
	$("#modifyCodingMode").val("2");
	$("#modifyCodingModeCheck").prop("checked", false);
	$("#modifyPreProcessing").val("1");
	$("#modifyPreProcessingCheck").prop("checked", false);
	$("#modifyEteDelay").val("4500");
	$("#modifyEteDelayCheck").prop("checked", false);
	$("#modifyDiv").modal();
}

function modifyApply()
{
	if(validateBeforeModifyApply() == false)
	{
		return;
	}
	
	var modifySwitch = $("#modifySwitch").val();
	var modifySwitchCheck = $("#modifySwitchCheck").prop("checked");
	var modifyEncodingType = $("#modifyEncodingType").val();
	var modifyEncodingTypeCheck = $("#modifyEncodingTypeCheck").prop("checked");
	var modifyBitrate = $("#modifyBitrate").val();
	var modifyBitrateCheck = $("#modifyBitrateCheck").prop("checked");
	var modifyResolution = $("#modifyResolution").val();
	var modifyResolutionCheck = $("#modifyResolutionCheck").prop("checked");
	var modifyGopLen = $("#modifyGopLen").val();
	var modifyGopLenCheck = $("#modifyGopLenCheck").prop("checked");
	var modifyCodingMode = $("#modifyCodingMode").val();
	var modifyCodingModeCheck = $("#modifyCodingModeCheck").prop("checked");
	var modifyPreProcessing = $("#modifyPreProcessing").val();
	var modifyPreProcessingCheck = $("#modifyPreProcessingCheck").prop("checked");
	var modifyEteDelay = $("#modifyEteDelay").val();
	var modifyEteDelayCheck = $("#modifyEteDelayCheck").prop("checked");
	
	for(var i = 0; i < rowNum; i++)
	{
		if($("#check" + (i + 1)).prop("checked"))
		{
			if(modifySwitchCheck)
			{
				$("#switch" + (i + 1)).val(modifySwitch);
			}
			if(modifyEncodingTypeCheck)
			{
				$("#encodingType" + (i + 1)).val(modifyEncodingType);
			}
			if(modifyBitrateCheck)
			{
				$("#bitrate" + (i + 1)).val(modifyBitrate);
			}
			if(modifyResolutionCheck)
			{
				$("#resolution" + (i + 1)).val(modifyResolution);
			}
			if(modifyGopLenCheck)
			{
				$("#gopLen" + (i + 1)).val(modifyGopLen);
			}
			if(modifyCodingModeCheck)
			{
				$("#codingMode" + (i + 1)).val(modifyCodingMode);
			}
			if(modifyPreProcessingCheck)
			{
				$("#preProcessing" + (i + 1)).val(modifyPreProcessing);
			}
			if(modifyEteDelayCheck)
			{
				$("#eteDelay" + (i + 1)).val(modifyEteDelay);
			}
		}
	}
	$.modal.close();
	EnableButton("applyBtn");
}

function validateBeforeModifyApply()
{
 	var returnFlag;
	var modifyBitrateCheck = $("#modifyBitrateCheck").prop("checked");
	var modifyGopLenCheck = $("#modifyGopLenCheck").prop("checked");
	var modifyEteDelayCheck = $("#modifyEteDelayCheck").prop("checked");
	
	if(modifyBitrateCheck)
	{
		returnFlag = ValidateParam($("#modifyBitrate"), "INT_DEC", 300000, 15000000, "<Video Bitrate(bps)>", "<视频码率(bps)>", language);
		if(returnFlag == false)
		{
			return false;
		}
	}
	if(modifyGopLenCheck)
	{
		returnFlag = ValidateParam($("#modifyGopLen"), "INT_DEC", 1, 300, "<GOP Size>", "<GOP长度>", language);
		if(returnFlag == false)
		{
			return false;
		}
	}
	if(modifyEteDelayCheck)
	{
		returnFlag = ValidateParam($("#modifyEteDelay"), "INT_DEC", 3500, 4500, "<ETE Delay(ms)>", "<ETE延时(ms)>", language);
		if(returnFlag == false)
		{
			return false;
		}
	}
	
	return true;
}

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