var url = window.location.href;
var intPos = url.indexOf("="); 
var slotno= url.substr(intPos + 1);
var objIdArray = ["dstslotnum", "dstportnum", "tsid", "netid", "serviceid", "mode", "switch" ,"updateset", "filename", "workStatus"];
var COL_NUM = 10;
var WORK_STATUS = 9;
var modifyIdArray = ["modifyDstSlotCheck", "modifyTsIdCheck", "modifyServiceIdCheck", "modifyNetworkIdCheck", "modifyModeCheck", "modifySwitchCheck", "modifyUpdateSetCheck", "modifyFileNameCheck"];
var MODIFY_COL_NUM = 8;
var TotalNum = 0;
var CurPageRowNum = 0;
var MODAL_INDEX = 0;
var ADD = 0;
var EDIT = 1;
var MODIFY = 2;
var ENGLISH = 0;
var CHINESE = 1;

$(document).ready(function()
{
	GetEpgProgParam();
	updateOutput(MODAL_INDEX, ADD);
	updateOutput(MODAL_INDEX, MODIFY);
});

function getHTTPObject()
{
	var xmlhttp = false;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e2)
		{
			xmlhttp = false;
		}
	}
	if ((!xmlhttp) && (typeof XMLHttpRequest != "undefined"))
	{
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlhttp = false;
		}
	}
	return xmlhttp;
}
/*Create new form element*/
function CreateHiddenFormElem(form,name,value)
{
	var tempform = document.getElementById(form);
	var newInput = document.createElement("input");
	newInput.type = "hidden";	
	newInput.name = name;		
	newInput.value = value;			
	tempform.appendChild(newInput);	
	return;
}
/*Between the pages of the jump*/
function jump(id)
{
	if(id==1)
	{
		window.location.href="epgSys.asp?slotno="+slotno;
	}
	else
	{
		window.location.href="epgPro.asp?slotno="+slotno;
	}
}
/* Get EPG program information*/
function GetEpgProgParam()
{
	var CurrentRow = parent.CurrentRow;
	var xmlhttp = getHTTPObject();
	var PostParams = "RowIndex="+CurrentRow+"&CurSlot="+slotno;
	xmlhttp.open("POST", "/goform/EpgCardParamGet", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", PostParams.length);
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			if((xmlhttp.status>=200&&xmlhttp.status<300)||xmlhttp.status==304)
			{
				var response = xmlhttp.responseText.slice(7, -8);
				var response_array = response.split("||");
				Assignment(response_array);
			}
		}
	}
	xmlhttp.send(PostParams);
	return true;
}
function GetInformation(Operation)
{
	switch(Operation)
	{
		case "First":
			parent.CurrentRow = 0;
			$("#first").attr("disabled",true);
			$("#prev").attr("disabled",true);
			$("#next").attr("disabled",false);
			$("#last").attr("disabled",false);			
			GetEpgProgParam();
			break;
		case "Prev":
			parent.CurrentRow--;
			if(parent.CurrentRow == 0)
			{
				$("#next").attr("disabled",false);
				$("#last").attr("disabled",false);
				$("#first").attr("disabled",true);
				$("#prev").attr("disabled",true);
			}
			GetEpgProgParam();
			break;
		case "Next":
			parent.CurrentRow++;
			if((TotalNum <(parent.CurrentRow+1)*20)||(TotalNum <(parent.CurrentRow+1)*20))
			{
				$("#next").attr("disabled",true);
				$("#last").attr("disabled",true);
				$("#first").attr("disabled",false);
				$("#prev").attr("disabled",false);
			}
			GetEpgProgParam();
			break;
		case "Last":
			parent.CurrentRow = Math.floor(TotalNum/20);
			$("#next").attr("disabled",true);
			$("#last").attr("disabled",true);
			$("#first").attr("disabled",false);
			$("#prev").attr("disabled",false);
			GetEpgProgParam();
			break;
		default:
			break;
		}
	
}
function addproginfo()
{
	$("#container").modal();
}

function modifyParam()
{	
	$("#modifyTsId").val("");
	$("#modifyServiceId").val("");
	$("#modifyServiceIdStep").val("0");
	$("#modifyNetworkId").val("");
	$("#modifyFileName").val("");
	$("#modifyMode").val("0");
	$("#modifySwitch").val("0");
	$("#modifyUpdateSet").val("0");

	for(var i = 0; i < MODIFY_COL_NUM; i++)
	{
		$("#" + modifyIdArray[i]).attr("checked", false);
	}
	$("#modifyParam").modal();
}

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

function modifyApply()
{
	var reternFlag;
	var checkNum = 0;
	var modifyDstSlotNo = parseInt($("#modifyDstSlotNo").val(), 10);
	var modifyDstSlotCheck = $("#modifyDstSlotCheck").attr("checked");
	
	var modifyDstPort = parseInt($("#modifyDstPort").val(), 10);
	
	var modifyTsId = parseInt($("#modifyTsId").val(), 10);
	var modifyTsIdCheck = $("#modifyTsIdCheck").attr("checked");
	
	var modifyServiceId = parseInt($("#modifyServiceId").val(), 10);
	var modifyServiceIdCheck = $("#modifyServiceIdCheck").attr("checked");
	
	var modifyServiceIdStep = parseInt($("#modifyServiceIdStep").val(), 10);
	
	var modifyNetworkId = parseInt($("#modifyNetworkId").val(), 10);
	var modifyNetworkIdCheck = $("#modifyNetworkIdCheck").attr("checked");
	
	var modifyMode = parseInt($("#modifyMode").val(), 10);
	var modifyModeCheck = $("#modifyModeCheck").attr("checked");
	
	var modifySwitch = parseInt($("#modifySwitch").val(), 10);
	var modifySwitchCheck = $("#modifySwitchCheck").attr("checked");
	
	var modifyUpdateSet = parseInt($("#modifyUpdateSet").val(), 10);
	var modifyUpdateSetCheck = $("#modifyUpdateSetCheck").attr("checked");
	
	var modifyFileName = $("#modifyFileName").val();
	var modifyFileNameCheck = $("#modifyFileNameCheck").attr("checked");
	
	if(modifyTsIdCheck)
	{
		if(language == ENGLISH)
		{
			reternFlag = ValidateNum($("#modifyTsId"), "ID", "<TS ID>");
			if(reternFlag == false)
			{
				return;
			}
		}
		else
		{
			reternFlag = ValidateNum($("#modifyTsId"), "ID", "<传送流ID>");
			if(reternFlag == false)
			{
				return;
			}
		}		
	}
	if(modifyNetworkIdCheck)
	{
		if(language == ENGLISH)
		{
			reternFlag = ValidateNum($("#modifyNetworkId"), "ID", "<Net ID>");
			if(reternFlag == false)
			{
				return;
			}
		}
		else
		{
			reternFlag = ValidateNum($("#modifyNetworkId"), "ID", "<网络ID>");
			if(reternFlag == false)
			{
				return;
			}
		}
	}
	if(modifyFileNameCheck)
	{
		if(language == ENGLISH)
		{
			reternFlag = ValidateNum($("#modifyFileName"), "NAME", "<File Name>");
			if(reternFlag == false)
			{
				return;
			}
		}
		else
		{
			reternFlag = ValidateNum($("#modifyFileName"), "NAME", "<文件名>");
			if(reternFlag == false)
			{
				return;
			}
		}		
	}
	if(modifyServiceIdCheck)
	{
		if(language == ENGLISH)
		{
			reternFlag = ValidateNum($("#modifyServiceId"), "PROGIDENTIFIER", "<Service Id>");
			if(reternFlag == false)
			{
				return;
			}
		}
		else
		{
			reternFlag = ValidateNum($("#modifyServiceId"), "PROGIDENTIFIER", "<节目号>");
			if(reternFlag == false)
			{
				return;
			}
		}
	}	
	for(var i = 0; i < CurPageRowNum ; i++)
	{
		var index = i + 1;
		if($("#checkbox" + i).attr("checked"))
		{
			if(modifyDstSlotCheck)
			{
				$("#dstslotnum" + index).val(modifyDstSlotNo);
				changePortList(index, EDIT);
			}
			if(modifyDstSlotCheck)
			{
				$("#dstportnum" + index).val(modifyDstPort);
			}
			if(modifyTsIdCheck)
			{
				$("#tsid" + index).val(modifyTsId);
			}
			if(modifyServiceIdCheck)
			{
				$("#serviceid" + index).val(modifyServiceId + modifyServiceIdStep*checkNum);
			}
			if(modifyNetworkIdCheck)
			{
				$("#netid" + index).val(modifyNetworkId);
			}
			if(modifyModeCheck)
			{
				$("#mode" + index).val(modifyMode);
			}
			if(modifySwitchCheck)
			{
				$("#switch" + index).val(modifySwitch);
			}
			if(modifyUpdateSetCheck)
			{
				$("#updateset" + index).val(modifyUpdateSet);
			}
			if(modifyFileNameCheck)
			{
				$("#filename" + index).val(modifyFileName);
			}
			checkNum++;
		}
	}
	$.modal.close();
}

function DeleteALLInfo()
{
	CreateHiddenFormElem("paramform","Operation",4);
	CreateHiddenFormElem("paramform","CurSlot",slotno);
	parent.CurrentRow = 0;
	$("#paramform").submit(); 
	setTimeout("self.location.reload(true)",1000);
}
function DeleteSelectInfo()
{
	var tempcnt = 0;
	CreateHiddenFormElem("myform","Operation",5);
	CreateHiddenFormElem("myform","CurSlot",slotno);
	CreateHiddenFormElem("myform","CurRow",parent.CurrentRow);
	for(i=0;i<CurPageRowNum;i++)
	{
		if($("input[name='checkbox"+i+"']").attr("checked"))
		{
			tempcnt++;
		}
	}
	if(CurPageRowNum == tempcnt)
	{
		if(parent.CurrentRow != 0)
		{
			parent.CurrentRow--;
		}
	}
	$("#myform").submit(); 
	setTimeout("self.location.reload(true)",1000);
}
function SelectAll(checkbox)
{
	for(var i = 0; i < CurPageRowNum; i++)
	{
		$("input[name='checkbox"+i+"']").attr("checked", $(checkbox).attr("checked"));
	}
}
function Assignment(response_array)
{
	var CurrentRow = parent.CurrentRow;
	if(response_array.length == 0)//No EPG program parameters
	{
		TotalNum = 0;
		for(var i = 0; i < 20; i++)
		{
			$("#table_info3").find("tr").eq(i + 1).css("display", "none");
		}
		CurPageRowNum = 0;
	}
	else
	{
		TotalNum = response_array[0];
		CurPageRowNum =  response_array.length - 1;
		for(var i = CurPageRowNum ; i < 20; i++)
		{
			$("#table_info3").find("tr").eq(i + 1).css("display", "none");
		}
		for(var i = 0; i < CurPageRowNum; i++)
		{
			$("#table_info3").find("tr").eq(i + 1).css("display", "");
			updateOutput(i + 1, EDIT);
			var response_array_second = response_array[i+1].split("&");
			for(var j = 0; j < COL_NUM; j++)
			{
				if(j != WORK_STATUS)
				{
					$("#" + objIdArray[j] + (i + 1)).val(response_array_second[j]);
					if(j == 0)
					{
						changePortList(i+1, EDIT);	
					}
				}
				else
				{
					$("#" + objIdArray[j] + (i + 1)).html(response_array_second[j]);
					if(response_array_second[j] == "WORK-WELL")
					{
						$("#" + objIdArray[j] + (i + 1)).css("color","#006600");
					}
					else
					{
						$("#" + objIdArray[j] + (i + 1)).css("color","#000000");
					}
				}
			}
		}
	}
	for(var i = 0; i < CurPageRowNum; i++)
	{
		$("input[name='checkbox"+i+"']").attr("checked","");
	}
	if(TotalNum == 0)
	{
		$("#CurPageNum").html(CurrentRow);
		$("#Resetbtn").attr("disabled",true);
		$("#Subbtn").attr("disabled",true);
		$("#Addbtn").attr("disabled",false);
		$("#modifyBtn").attr("disabled",true);
		$("#Delbtn").attr("disabled",true);
		$("#Delallbtn").attr("disabled",true);
	}
	else
	{
		$("#CurPageNum").html(CurrentRow+1);
		$("#Resetbtn").attr("disabled",false);
		$("#Subbtn").attr("disabled",false);
		$("#Addbtn").attr("disabled",false);
		$("#modifyBtn").attr("disabled",false);
		$("#Delbtn").attr("disabled",false);
		$("#Delallbtn").attr("disabled",false);
	}
	$("#TotalPageNum").html(Math.ceil(TotalNum/20));
	if((TotalNum <(CurrentRow+1)*20)||(TotalNum == (CurrentRow+1)*20))
	{
		if(TotalNum > 20)
		{
			$("#next").attr("disabled",true);
			$("#last").attr("disabled",true);
			$("#first").attr("disabled",false);
			$("#prev").attr("disabled",false);
		}
		else
		{
			$("#first").attr("disabled",true);
			$("#prev").attr("disabled",true);
			$("#next").attr("disabled",true);
			$("#last").attr("disabled",true);
		}		
	}
	else if(CurrentRow == 0)
	{				
		$("#first").attr("disabled",true);
		$("#prev").attr("disabled",true);
		$("#next").attr("disabled",false);
		$("#last").attr("disabled",false);
	}
	else
	{
		$("#first").attr("disabled",false);
		$("#prev").attr("disabled",false);
		$("#next").attr("disabled",false);
		$("#last").attr("disabled",false);	
	}
}