var easIdArray = ["easProgIndex","logoInfo", "logoClear", "switch", "osdX", "osdY", "logoType", "opacity", "osdWidth", "scrollMode", "scrollSpeed"];
var EAS_COL_NUM = 11;
var easRowNum;
var timeIdArray = ["timeProgIndex", "displayMode", "startTime1Hour", "startTime1Minute","startTime1Second", "duration1Time", "startTime2Hour", "startTime2Minute", "startTime2Second", "duration2Time", "startTime3Hour", "startTime3Minute", "startTime3Second", "duration3Time"];
var TIME_COL_NUM = 14;
var timeRowNum;
var curEditTable;

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

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

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

function checkEasAll()
{
	var flag = $("#easCheckAll").prop("checked");
	for(var i = 0; i < easRowNum; i++)
	{
		$("#easCheck" + (i + 1)).prop("checked", flag);
	}
	
	if(parent.groupAccess != USER)
	{
		EnableButton("modifyBtn");
	}
}
function checkTimeAll()
{
	var flag = $("#timeCheckAll").prop("checked");
	for(var i = 0; i < timeRowNum; i++)
	{
		$("#timeCheck" + (i + 1)).prop("checked", flag);
	}
	
	if(parent.groupAccess != USER)
	{
		EnableButton("modifyBtn");
	}
}

function changeRowChecked()
{
	if(parent.groupAccess != USER)
	{
		EnableButton("modifyBtn");
	}
		
}
function displayParam()
{
	var data;
	var easData;
	var timeData;
	var response;
	var length;
	var xmlHttp = GetHTTPObject();
	var infoData;

	xmlHttp.open("GET", "/goform/formEMR30?type=" + CARD_EAS + "&cmd=" + GET + "&language=" + language 
		+ "&slotNo=" + slotNo + "&ran=" + Math.random(), true);
	xmlHttp.onreadystatechange = function()
	{
		if((xmlHttp.readyState == 4) && (xmlHttp.status == 200))
		{
			response = AjaxStrDeal(xmlHttp.responseText);
			data = response.split(FIRST_DELIMITER);
			if(data[0] == "")
			{
				easRowNum = 0;
			}
			else
			{
				easData = data[0].split(SECOND_DELIMITER);
				easRowNum = easData.length;
			}
			DrawTableRow("easDataTable", "easDataRow", "EasCaseRow", easRowNum);
			if(easRowNum != 0)
			{
				updateEasTableData(easData);
			}
			if(data[1] == "")
			{
				timeRowNum = 0;
			}
			else
			{
				timeData = data[1].split(SECOND_DELIMITER);
				timeRowNum = timeData.length;
			}
			DrawTableRow("dataTable", "dataRow", "CaseRow", timeRowNum);
			if(timeRowNum != 0)
			{
				updateTimeTableData(timeData);
			}
			refreshFlag = true;
		}
	}
	xmlHttp.send(null);
}

function updateEasTableData(data)
{
	var rowData;
	var index;
	
	for(var i = 0; i < easRowNum; i++)
	{
		index = i + 1;
		rowData = data[i].split(THIRD_DELIMITER);
		for(var j = 0; j < EAS_COL_NUM; j++)
		{
			if((j == 0) || (j == 1))
			{
				$("#" + easIdArray[j] + index).html(rowData[j]);
			}
			else
			{
				$("#" + easIdArray[j] + index).val(rowData[j]);
			}
		}
	}
}

function updateTimeTableData(data)
{
	var rowData;
	var index;
	
	for(var i = 0; i < timeRowNum; i++)
	{
		index = i + 1;
		rowData = data[i].split(THIRD_DELIMITER);
		for(var j = 0; j < TIME_COL_NUM; j++)
		{
			if(j == 0)
			{
				$("#" + timeIdArray[j] + index).html(rowData[j]);
			}
			else
			{
				$("#" + timeIdArray[j] + index).val(rowData[j]);
			}			
		}
	}
}

function modifyEas()
{
	curEditTable = "eas";
	$("#modifyLogoClear").val("0");
	$("#modifyLogoClearCheck").prop("checked", false);
	
	$("#modifySwitch").val("0");
	$("#modifySwitchCheck").prop("checked", false);
	
	$("#modifyOsdX").val("0");
	$("#modifyOsdXCheck").prop("checked", false);
	
	$("#modifyOsdY").val("0");
	$("#modifyOsdYCheck").prop("checked", false);
	
	$("#modifyLogoType").val("0");
	$("#modifyLogoTypeCheck").prop("checked", false);
	
	$("#modifyOpacity").val("100");
	$("#modifyOpacityCheck").prop("checked", false);
	
	$("#modifyOsdWidth").val("100");
	$("#modifyOsdWidthCheck").prop("checked", false);
	
	$("#modifyScrollMode").val("0");
	$("#modifyScrollModeCheck").prop("checked", false);
	
	$("#modifyScrollSpeed").val("0");
	$("#modifyScrollSpeedCheck").prop("checked", false);
	
	$("#modifyEasDiv").modal();
}

function modifyTime()
{
	curEditTable = "time";
	$("#modifyDisplayMode").val("0");
	$("#modifyDisplayModeCheck").prop("checked", false);
	
	for(var i = 0; i < 3; i++)
	{
		$("#modifyStartTimeHour" + (i + 1)).val("");
		$("#modifyStartTimeMinute" + (i + 1)).val("");
		$("#modifyStartTimeSecond" + (i + 1)).val("");		
		$("#modifyStartTimeCheck").prop("checked", false);
		
		$("#modifyDurationTime" + (i + 1)).val("");		
		$("#modifyDurationTimeCheck").prop("checked", false);
	}
	$("#modifyTimeDiv").modal();
}
function modifyEasApply()
{
	if(validateBeforeModifyApply() == false)
	{
		return;
	}
	var modifyLogoClear = $("#modifyLogoClear").val();
	var modifyLogoClearCheck = $("#modifyLogoClearCheck").prop("checked");
	
	var modifySwitch = $("#modifySwitch").val();
	var modifySwitchCheck = $("#modifySwitchCheck").prop("checked");
	
	var modifyOsdX = $("#modifyOsdX").val();
	var modifyOsdXCheck = $("#modifyOsdXCheck").prop("checked");
	
	var modifyOsdY = $("#modifyOsdY").val();
	var modifyOsdYCheck = $("#modifyOsdYCheck").prop("checked");
	
	var modifyLogoType = $("#modifyLogoType").val();
	var modifyLogoTypeCheck = $("#modifyLogoTypeCheck").prop("checked");
	
	var modifyOpacity = $("#modifyOpacity").val();
	var modifyOpacityCheck = $("#modifyOpacityCheck").prop("checked");
	
	var modifyOsdWidth = $("#modifyOsdWidth").val();
	var modifyOsdWidthCheck = $("#modifyOsdWidthCheck").prop("checked");
	
	var modifyScrollMode = $("#modifyScrollMode").val();
	var modifyScrollModeCheck = $("#modifyScrollModeCheck").prop("checked");
	
	var modifyScrollSpeed = $("#modifyScrollSpeed").val();
	var modifyScrollSpeedCheck = $("#modifyScrollSpeedCheck").prop("checked");
	
	for(var i = 0; i < easRowNum; i++)
	{
		if($("#easCheck" + (i + 1)).prop("checked"))
		{
			if(modifyLogoClearCheck)
			{
				$("#logoClear" + (i + 1)).val(modifyLogoClear);
			}
			if(modifySwitchCheck)
			{
				$("#switch" + (i + 1)).val(modifySwitch);
			}
			if(modifyOsdXCheck)
			{
				$("#osdX" + (i + 1)).val(modifyOsdX);
			}
			if(modifyOsdYCheck)
			{
				$("#osdY" + (i + 1)).val(modifyOsdY);
			}
			if(modifyLogoTypeCheck)
			{
				$("#logoType" + (i + 1)).val(modifyLogoType);
			}
			if(modifyOsdWidthCheck)
			{
				$("#osdWidth" + (i + 1)).val(modifyOsdWidth);
			}
			if(modifyScrollModeCheck)
			{
				$("#scrollMode" + (i + 1)).val(modifyScrollMode);
			}
			if(modifyScrollSpeedCheck)
			{
				$("#scrollSpeed" + (i + 1)).val(modifyScrollSpeed);
			}
			if(modifyOpacityCheck)
			{
				$("#opacity" + (i + 1)).val(modifyOpacity);
			}
		}
	}
	$.modal.close();
	EnableButton("applyBtn");
}
function modifyTimeApply()
{
	if(validateBeforeModifyApply() == false)
	{
		return;
	}
	var modifyDisplayMode = $("#modifyDisplayMode").val();
	var modifyDisplayModeCheck = $("#modifyDisplayModeCheck").prop("checked");
	
	var modifyStartTimeCheck1 = $("#modifyStartTimeCheck1").prop("checked");
	var modifyStartTimeHour1 = $("#modifyStartTimeHour1").val();
	var modifyStartTimeMinute1 = $("#modifyStartTimeMinute1").val();
	var modifyStartTimeSecond1 = $("#modifyStartTimeSecond1").val();
	
	var modifyDurationTimeCheck1 = $("#modifyDurationTimeCheck1").prop("checked");
	var modifyDurationTime1 = $("#modifyDurationTime1").val();
		
	var modifyStartTimeCheck2 = $("#modifyStartTimeCheck2").prop("checked");
	var modifyStartTimeHour2 = $("#modifyStartTimeHour2").val();
	var modifyStartTimeMinute2 = $("#modifyStartTimeMinute2").val();
	var modifyStartTimeSecond2 = $("#modifyStartTimeSecond2").val();
	
	var modifyDurationTimeCheck2 = $("#modifyDurationTimeCheck2").prop("checked");
	var modifyDurationTime2 = $("#modifyDurationTime2").val();
	
	var modifyStartTimeCheck3 = $("#modifyStartTimeCheck3").prop("checked");
	var modifyStartTimeHour3 = $("#modifyStartTimeHour3").val();
	var modifyStartTimeMinute3 = $("#modifyStartTimeMinute3").val();
	var modifyStartTimeSecond3 = $("#modifyStartTimeSecond3").val();
	
	var modifyDurationTimeCheck3 = $("#modifyDurationTimeCheck3").prop("checked");
	var modifyDurationTime3 = $("#modifyDurationTime3").val();
	
	for(var i = 0; i < timeRowNum; i++)
	{
		if($("#timeCheck" + (i + 1)).prop("checked"))
		{
			if(modifyDisplayModeCheck)
			{
				$("#displayMode" + (i + 1)).val(modifyDisplayMode);
			}
			if(modifyStartTimeCheck1)
			{
				$("#startTime1Hour" + (i + 1)).val(modifyStartTimeHour1);
				$("#startTime1Minute" + (i + 1)).val(modifyStartTimeMinute1);
				$("#startTime1Second" + (i + 1)).val(modifyStartTimeSecond1);
			}
			if(modifyDurationTimeCheck1)
			{
				$("#duration1Time" + (i + 1)).val(modifyDurationTime1);
			}
			if(modifyStartTimeCheck2)
			{
				$("#startTime2Hour" + (i + 1)).val(modifyStartTimeHour2);
				$("#startTime2Minute" + (i + 1)).val(modifyStartTimeMinute2);
				$("#startTime2Second" + (i + 1)).val(modifyStartTimeSecond2);
			}
			if(modifyDurationTimeCheck2)
			{
				$("#duration2Time" + (i + 1)).val(modifyDurationTime2);
			}
			if(modifyStartTimeCheck3)
			{
				$("#startTime3Hour" + (i + 1)).val(modifyStartTimeHour3);
				$("#startTime3Minute" + (i + 1)).val(modifyStartTimeMinute3);
				$("#startTime3Second" + (i + 1)).val(modifyStartTimeSecond3);
			}
			if(modifyDurationTimeCheck3)
			{
				$("#duration3Time" + (i + 1)).val(modifyDurationTime3);
			}
		}
	}
	$.modal.close();
	EnableButton("applyBtn");
}
function validateBeforeModifyApply()
{
 	var returnFlag;
	if(curEditTable == "eas")
	{
		var modifyOsdXCheck = $("#modifyOsdXCheck").prop("checked");
		var modifyOsdYCheck = $("#modifyOsdYCheck").prop("checked");
		var modifyOsdWidthCheck = $("#modifyOsdWidthCheck").prop("checked");
		var modifyScrollSpeedCheck = $("#modifyScrollSpeedCheck").prop("checked");
		var modifyOpacityCheck = $("#modifyOpacityCheck").prop("checked");
		if(modifyOsdXCheck)
		{
			returnFlag = ValidateParam($("#modifyOsdX"), "INT_DEC", 0, 1920, "<Destination X>", "<横坐标>", language);
			if(returnFlag == false)
			{
				return false;
			}
		}
		if(modifyOsdYCheck)
		{
			returnFlag = ValidateParam($("#modifyOsdY"), "INT_DEC", 0, 1080, "<Destination Y>", "<纵坐标>", language);
			if(returnFlag == false)
			{
				return false;
			}
		}
		if(modifyOsdWidthCheck)
		{
			returnFlag = ValidateParam($("#modifyOsdWidth"), "INT_DEC", 1, 1920, "<Destination Width>", "<显示宽度>", language);
			if(returnFlag == false)
			{
				return false;
			}
		}
		if(modifyScrollSpeedCheck)
		{
			returnFlag = ValidateParam($("#modifyScrollSpeed"), "INT_DEC", 0, 20, "<Scroll Speed(pixel)>", "<滚动速度(像素)>", language);
			if(returnFlag == false)
			{
				return false;
			}
		}
		if(modifyOpacityCheck)
		{
			returnFlag = ValidateParam($("#modifyOpacity"), "INT_DEC", 0, 100, "<Opacity(%)>", "<透明度(%)>", language);
			if(returnFlag == false)
			{
				return false;
			}
		}
	}
	else if(curEditTable == "time")
	{
		var modifyStartTimeCheck1 = $("#modifyStartTimeCheck1").prop("checked");		
		var modifyDurationTimeCheck1 = $("#modifyDurationTimeCheck1").prop("checked");			
		var modifyStartTimeCheck2 = $("#modifyStartTimeCheck2").prop("checked");	
		var modifyDurationTimeCheck2 = $("#modifyDurationTimeCheck2").prop("checked");		
		var modifyStartTimeCheck3 = $("#modifyStartTimeCheck3").prop("checked");		
		var modifyDurationTimeCheck3 = $("#modifyDurationTimeCheck3").prop("checked");
		
		for(var i = 0; i < 3; i++)
		{
			if(eval("modifyStartTimeCheck" + (i + 1)))
			{
				returnFlag = ValidateParam($("#modifyStartTimeHour" + (i + 1)), "INT_DEC", 0, 23, "<Start Time_" + (i + 1) + " Hour>", "<起始时间_" + (i + 1) + " 时>", language);
				if(returnFlag == false)
				{
					return false;
				}
				returnFlag = ValidateParam($("#modifyStartTimeMinute" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_" + (i + 1) + " Minute>", "<起始时间_" + (i + 1) + " 分>", language);
				if(returnFlag == false)
				{
					return false;
				}
				returnFlag = ValidateParam($("#modifyStartTimeSecond" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_" + (i + 1) + " Second>", "<起始时间_" + (i + 1) + " 秒>", language);
				if(returnFlag == false)
				{
					return false;
				}
			}
			if(eval("modifyDurationTimeCheck" + (i + 1)))
			{
				returnFlag = ValidateParam($("#modifyDurationTime" + (i + 1)), "INT_DEC", 0, 1440, "<Duration_" + (i + 1) + "(min)>", "<持续时间_" + (i + 1) + "(分)>", language);
				if(returnFlag == false)
				{
					return false;
				}
			}
		}
	}
	return true;
}

function cancelModify()
{
	$.modal.close();
}
function pageApply()
{
	var data;
	var response;
	var xmlHttp = GetHTTPObject();
	
	if(paramCheck() == false)
	{
		return;
	}
	
	data = "type=" + CARD_EAS + "&cmd=" + SET + "&language=" + language + "&slotNo=" + slotNo + "&setString=";
	data += generateEasData() + FIRST_DELIMITER;
	data += generateTimeData();
	
	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 generateEasData()
{
	var data;
	var index;
	for(var i = 0; i < easRowNum; i++)
	{
		index = i + 1;
		for(var j = 0; j < EAS_COL_NUM; j++)
		{
			if((i == 0) && (j == 0))
			{
				data = $("#" + easIdArray[j] + index).html();
			}
			else if((i != 0) && (j == 0))
			{
				data += SECOND_DELIMITER + $("#" + easIdArray[j] + index).html();
			}
			else
			{
				data += THIRD_DELIMITER + $("#" + easIdArray[j] + index).val();
			}
		}		
	}
	return data;
}

function generateTimeData()
{
	var data;
	var index;
	for(var i = 0; i < timeRowNum; i++)
	{
		index = i + 1;
		for(var j = 0; j < TIME_COL_NUM; j++)
		{
			if((i == 0) && (j == 0))
			{
				data = $("#" + timeIdArray[j] + index).val();
			}
			else if((i != 0) && (j == 0))
			{
				data += SECOND_DELIMITER + $("#" + timeIdArray[j] + index).val();
			}
			else
			{
				data += THIRD_DELIMITER + $("#" + timeIdArray[j] + index).val();
			}
		}		
	}
	return data;
}

function paramCheck()
{
 	var returnFlag;
	
	for(var i = 0; i < easRowNum; i++)
	{
		returnFlag = ValidateParam($("#osdX" + (i + 1)), "INT_DEC", 0, 1920, "<Destination X>", "<横坐标>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#osdY" + (i + 1)), "INT_DEC", 0, 1080, "<Destination Y>", "<纵坐标>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#osdWidth" + (i + 1)), "INT_DEC", 1, 1920, "<Destination Width>", "<显示宽度>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#scrollSpeed" + (i + 1)), "INT_DEC", 0, 20, "<Scroll Speed(pixel)>", "<滚动速度(像素)>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#opacity" + (i + 1)), "INT_DEC", 0, 100, "<Opacity(%)>", "<透明度(%)>", language);
		if(returnFlag == false)
		{
			return false;
		}
	}
	for(var i = 0; i < timeRowNum; i++)
	{
		returnFlag = ValidateParam($("#startTime1Hour" + (i + 1)), "INT_DEC", 0, 23, "<Start Time_1 Hour>", "<起始时间_1 时>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime1Minute" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_1 Minute>", "<起始时间_1 分>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime1Second" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_1 Second>", "<起始时间_1 秒>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#duration1Time" + (i + 1)), "INT_DEC", 0, 1440, "<Duration_1(min)>", "<持续时间_1(分)>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime2Hour" + (i + 1)), "INT_DEC", 0, 23, "<Start Time_2 Hour>", "<起始时间_2 时>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime2Minute" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_2 Minute>", "<起始时间_2 分>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime2Second" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_2 Second>", "<起始时间_2 秒>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#duration2Time" + (i + 1)), "INT_DEC", 0, 1440, "<Duration_2(min)>", "<持续时间_2(分)>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime3Hour" + (i + 1)), "INT_DEC", 0, 23, "<Start Time_3 Hour>", "<起始时间_3 时>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime3Minute" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_3 Minute>", "<起始时间_3 分>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#startTime3Second" + (i + 1)), "INT_DEC", 0, 59, "<Start Time_3 Second>", "<起始时间_3 秒>", language);
		if(returnFlag == false)
		{
			return false;
		}
		returnFlag = ValidateParam($("#duration3Time" + (i + 1)), "INT_DEC", 0, 1440, "<Duration_1(min)>", "<持续时间_3(分)>", language);
		if(returnFlag == false)
		{
			return false;
		}
	}
	return true;
}
