var ENGLISH = 0;
var CHINESE = 1;

var url = window.location.href;
var intPos = url.indexOf("="); 
var slotno= url.substr(intPos + 1); 
var submitcnt =0;
var deletemarker = 0;

function jump(id)
{	
	if(id==1)
	{
		window.location.href="gbeMulti_inparam1.asp?slotno="+slotno;
	}
	else if(id==2)
	{
		window.location.href="gbeMulti_outparam1.asp?slotno="+slotno;
	}
	else if(id==3)
	{
		window.location.href="gbeMulti_sysparam.asp?slotno="+slotno;
	}
	else if(id==4)
	{
		window.location.href="gbeMulti_arppara.asp?slotno="+slotno;
	}
	else if(id==5)
	{
		window.location.href="gbeMulti_inbackup.asp?slotno="+slotno;
	}
	else if(id==6)
	{
		window.location.href="gbeMulti_state.asp?slotno="+slotno;
	}
}

function jumpphy(id)
{
	if(id==1)
	{
		window.location.href="gbeMulti_inparam1.asp?slotno="+slotno;
	}
	else if(id==2)
	{
		window.location.href="gbeMulti_inparam2.asp?slotno="+slotno;
	}
	else if(id==3)
	{
		window.location.href="gbeMulti_inparam3.asp?slotno="+slotno;
	}
	else if(id==4)
	{
		window.location.href="gbeMulti_inparam4.asp?slotno="+slotno;
	}
}
//删除选中端口
function DelPort()
{
	if(deletemarker == 1)
	{
		parent.portbegin[GBE_NO] = 0;
		parent.portend[GBE_NO]   = 256;
		parent.portselvalue[GBE_NO] = 256;
	}
	else
	{
		var othermarker = 0;
		for(var i=0; i<inportnum; i++)
		{
			var portvalue = parseInt(GBE_MULTI_VID_IN_PORT_NO[i],10);
			var portfrom = parent.portbegin[GBE_NO];
			var portto   = parent.portend[GBE_NO];
			if((i>=portfrom)&&(i<portto))
			{
				if(!$("input[name='check"+portvalue+"']").attr("checked"))
				{
					othermarker++;
				}
			}
		}
		if(othermarker == 0)
		{
			parent.portbegin[GBE_NO] = 0;
			parent.portend[GBE_NO]   = 256;
		}
	}
	if(language == ENGLISH)
	{
		var confirm_prompt = "Are you sure you want to delete the port?";
	}
	else
	{
		var confirm_prompt = "确定要删除端口吗？";
	}	
	if(window.confirm(confirm_prompt))
	{
		$("#OperationType").val("2");
		$("#paramform").submit();
		setTimeout("self.location.reload(true)",1000);
	}
}
//删除全部端口
function deleteall()
{
	$("#OperateType").val("3");
	if(language == ENGLISH)
	{
		var confirm_prompt = "Are you sure you want to delete all ports?";
	}
	else
	{
		var confirm_prompt = "确定要删除全部端口吗？";
	}	
	if(window.confirm(confirm_prompt))
	{
		$("#editportform").submit();
		setTimeout("self.location.reload(true)",1000)
	}
}
//add Port
function portadd()
{	
	$("#OperateType").val("1");
	$("#portedit").css("display", "");
	$("#batedit").css("display", "none");	
}
//edit Port
function editbat()
{	
	$("#portedit").css("display", "none");
	$("#batedit").css("display", "");	
}
//批量修改
function edit()
{	
	var selectnum = 0;
	var reternFlag;
	if($("#ip").attr("checked"))
	{
		reternFlag = ValidateIP($("#IP") , "IP_ADDR", "<Receive IP>", "<接收IP>", language);
		if(reternFlag == false)
		{
			return false;
		}
	}
	if($("#port").attr("checked"))
	{
		reternFlag = ValidateParam($("#Port"), "INT_DEC", 1, 65535, "<Receive Port>", "<接收端口>", language);
		if(reternFlag == false)
		{
			return false;
		}
	}
	if($("#srcip1").attr("checked"))
	{
		reternFlag = ValidateIP($("#SrcIP1"), "SOURCE_IP", "<Source IP1>", "<源IP1>", language);
		if(reternFlag == false)
		{
			return false;
		}
	}
	if($("#srcip2").attr("checked"))
	{
		reternFlag = ValidateIP($("#SrcIP2"), "SOURCE_IP", "<Source IP2>", "<源IP2>", language);
		if(reternFlag == false)
		{
			return false;
		}
	}		
	if($("#refrate").attr("checked"))
	{
		reternFlag = ValidateParam($("#RefRate"), "INT_DEC", 0, 200000000, "<Reference Bitrate>", "<端口基准码率>", language);
		if(reternFlag == false)
		{
			return false;
		}
	}
	var ipvalue=$("#IP").val();
	var ipstepvalue=$("#EditIpStep").val();		
	var temparr = ipvalue.split(".");
	if(temparr[0] < 224)
	{
		if(parseInt($("#EditIpStep").val(), 10) != 0)
		{
			if(language == ENGLISH)
			{
				alert("If receive ip address is a unicast address,the ip step value must be 0!");
				return;
			}
			else
			{
				alert("接收IP为单播地址，IP步进值只能为0！");
				return;
			}
		}
	}
	var inportvalue=$("#Port").val();
	var modevalue=$("#SrcMode").val();
	var srcip1value=$("#SrcIP1").val();
	var srcip2value=$("#SrcIP2").val();
	var rtpswitch=$("#RTP").val();
	var refswitch=$("#RefSwitch").val();
	var ratevalue=$("#RefRate").val();
	var alarmvalue=$("#AlarmSwitch").val();
	for(var i=0; i<inportnum; i++)
	{
		var portvalue = parseInt(GBE_MULTI_VID_IN_PORT_NO[i],10);
		if($("#"+(portvalue+1)+"").attr("checked"))
		{
			if($("#ip").attr("checked"))
			{	
				ipsection4 = parseInt(temparr[3],10)+selectnum*ipstepvalue;
				if((ipsection4<0)||(ipsection4>255))
				{
					if(language == ENGLISH)
					{
						alert("IP address must be valid!");
					}
					else
					{
						alert("IP地址必须为合法值!");
					}					
					return;
				}
				var newip = temparr[0].concat(".",temparr[1],".",temparr[2],".",ipsection4);	
				$("#InIPAddr"+portvalue+"").val(newip);
				selectnum++;
			}
			if($("#port").attr("checked"))
			{				
				$("#InPort"+portvalue+"").val(inportvalue);
			}
			if($("#srcmode").attr("checked"))
			{				
				$("#Srcmode"+portvalue+"").val(modevalue);
			}
			if($("#srcip1").attr("checked"))
			{				
				$("#SrcIPAddr1"+portvalue+"").val(srcip1value);
			}
			if($("#srcip2").attr("checked"))
			{				
				$("#SrcIPAddr2"+portvalue+"").val(srcip2value);
			}
			if($("#rtp").attr("checked"))
			{				
				$("#Rtp"+portvalue+"").val(rtpswitch);
			}
			if($("#refswitch").attr("checked"))
			{				
				$("#RefSwitch"+portvalue+"").val(refswitch);
			}
			if($("#refrate").attr("checked"))
			{				
				$("#RefBitrate"+portvalue+"").val(ratevalue);
			}
			if($("#alarmswitch").attr("checked"))
			{				
				$("#InAlarmSwitch"+portvalue+"").val(alarmvalue);
			}
		}
	}
	$("#batedit").css("display", "none");
}
//form 提交
function submitcheck()
{
	if(submitcnt!=0)
	{
		return;
	}
	var reternFlag;
    var i,j;
	for(var i=0; i<inportnum; i++)
	{
		var portvalue = GBE_MULTI_VID_IN_PORT_NO[i];
		reternFlag = ValidateIP($("#InIPAddr"+portvalue+""), "IP_ADDR", "<Receive IP>", "<接收IP>", language);
		if(reternFlag == false)
		{
			return false;
		}
		reternFlag = ValidateParam($("#InPort"+portvalue+""), "INT_DEC", 1, 65535, "<Receive Port>", "<接收端口>", language);
		if(reternFlag == false)
		{
			return false;
		}
		reternFlag = ValidateIP($("#SrcIPAddr1"+portvalue+""), "SOURCE_IP", "<Source IP1>", "<源IP1>", language);
		if(reternFlag == false)
		{
			return false;
		}
		reternFlag = ValidateIP($("#SrcIPAddr2"+portvalue+""), "SOURCE_IP", "<Source IP2>", "<源IP2>", language);
		if(reternFlag == false)
		{
			return false;
		}
		reternFlag = ValidateParam($("#RefBitrate"+portvalue+""), "INT_DEC", 0, 200000000, "<Reference Bitrate>", "<端口基准码率>", language);
		if(reternFlag == false)
		{
			return false;
		}
		if($("#Rtp"+portvalue).val() == 1)
		{
			if(parseInt($("#InPort"+portvalue).val())%2 != 0)
			{
				if(language == ENGLISH)
				{
					alert("If current NTP is RTP,UDP port must be even!");
				}
				else
				{
					alert("网络传输协议为RTP时，端口号必须为偶数!");
				}				
				return;
			}
		}		
		var thisip = $("#InIPAddr"+portvalue).val();
		var thisport = $("#InPort"+portvalue).val();
		if((ipcompare(thisip,GBE_MULTI_VID_RECV_IP[i]) == false)||(parseInt(thisport,10) !=parseInt(GBE_MULTI_VID_RECV_PORT[i],10)))		       //对IP或者端口之一作了修改才判断
		{
			for(j=0; j<inportnum; j++)
			{
				var tmp_port = GBE_MULTI_VID_IN_PORT_NO[j];
				if(i!=j)
				{
					if((ipcompare($("#InIPAddr"+portvalue).val(),$("#InIPAddr"+tmp_port).val()) == true))//和当前页面上存在相同的IP
					{
						if(parseInt($("#InPort"+portvalue).val(),10) == parseInt($("#InPort"+tmp_port).val(),10))//如果端口也是相同的
						{
							if(language == ENGLISH)
							{
								alert("Port"+(parseInt(portvalue)+1)+" and the other ports have the same IP address and receiving port!");
							}
							else
							{
								alert("端口"+(parseInt(portvalue)+1)+"存在和其他端口相同的IP地址和接收端口！");
							}							
							return;
						}
					}
				}
			}
		}
   }
	$("#paramform").submit();
	submitcnt++;
	setTimeout("self.location.reload(true)",1000);
}
function addsubmitcheck()
{
	if(submitcnt!=0)
	{
		return;
	}
	var reternFlag;
    var i,j;
	var addportnum=$("#addPortno").val();
	var addport=$("#addPort").val();
	var interval=$("#udpinterval").val();
	var str=$("#addIP").val();
	var arr = str.split(".");
	var addip =	arr[3]
	var ipinterval=$("#ipinterval").val();
	var sum=parseInt(addport)+(parseInt(addportnum)-1)*parseInt(interval);
	var sum1=parseInt(addip)+(parseInt(addportnum)-1)*parseInt(ipinterval);
	reternFlag = ValidateParam($("#addPortno"), "INT_DEC", 1, 256, "<Add Port Number>", "<添加端口数>", language);
	if(reternFlag == false)
	{
		return false;
	}
	if(addportnum > parent.maxPortNum - inportnum)
	{
		if(parent.maxPortNum - inportnum < 0)
		{
			if(language == ENGLISH)
			{
				alert("Sorry!You can only add 0 ports!");
			}
			else
			{
				alert("对不起！您只能添加0个端口！");
			}			
			return;
		}
		else
		{
			if(language == ENGLISH)
			{
				alert("Sorry!You can only add  "+ (parent.maxPortNum - inportnum) +"  ports!");
			}
			else
			{
				alert("对不起！您只能添加"+ (parent.maxPortNum - inportnum) +"个端口！");
			}
			return;
		}		
	}
	reternFlag = ValidateIP($("#addIP"), "IP_ADDR", "<Receive IP>", "<接收IP>", language);
	if(reternFlag == false)
	{
		return false;
	}
	if(arr[0] < 224)
	{
		if(addportnum != 1)
		{
			if(parseInt($("#ipinterval").val(), 10) != 0)
			{
				if(language == ENGLISH)
				{
					alert("If receive ip address is a unicast address,the ip step value must be 0!");
					return;
				}
				else
				{
					alert("接收IP为单播地址，IP步进值只能为0！");
					return;
				}
			}
		}
	}
	reternFlag = ValidateParam($("#ipinterval"), "INT_DEC", 0, 255, "<IP Step Value>", "<IP地址步进值>", language);
	if(reternFlag == false)
	{
		return false;
	}
	if((sum1<0)||(sum1>255))
	{
		if(language == ENGLISH)
		{
			alert("IP address must be valid!");
		}
		else
		{
			alert("添加端口后，必须保证IP地址为合法地址！");
		}
		return;
	}
	reternFlag = ValidateParam($("#addPort"), "INT_DEC", 1, 65535, "<Receive Port>", "<接收端口>", language);
	if(reternFlag == false)
	{
		return false;
	}
	reternFlag = ValidateParam($("#udpinterval"), "INT_DEC", 0, 65534, "<Port Step Value>", "<端口号步进值>", language);
	if(reternFlag == false)
	{
		return false;
	}
	if((sum < 0) || (sum > 65536))
	{
		if(language == ENGLISH)
		{
			alert("All Receive Ports must be between 1 and 65535!");
		}
		else
		{
			alert("添加端口后，端口号必须在1~65535之间!");
		}		
		return;
	}
	/*--网络传输协议为RTP时，端口号必须为偶数---*/
	var rtpvalue = $("#addRtp").val();
	if(rtpvalue == 1)
	{	
		if(addport%2 != 0)
		{
			if(language == ENGLISH)
			{
				alert("If current NTP is RTP,UDP port must be even!");
			}
			else
			{
				alert("网络传输协议为RTP时，端口号必须为偶数!");
			}
			return;
		}
		else
		{	
			if(addportnum!=1)
			{
				if(interval%2 != 0)
				{
					if(language == ENGLISH)
					{
						alert("If current NTP is RTP,UDP port must be even!");
					}
					else
					{
						alert("网络传输协议为RTP时，端口号必须为偶数!");
					}					
					return;
				}
			}
		}
	}
	if((addportnum!=1)&&(interval==0)&&(ipinterval==0))
	{
		if(language == ENGLISH)
		{
			alert("Cannot add the same destination IP and destination port to different ports!");
		}
		else
		{
			alert("不能添加目的IP和目的端口相同的端口！");
		}		
		return;
	}
	for(i=0; i<addportnum; i++)
	{
		var curip = arr[0]+"."+arr[1]+"."+arr[2]+"."+(parseInt(addip)+i*parseInt(ipinterval));
		for(j=0; j<inportnum; j++)
		{
			if(ipcompare(GBE_MULTI_VID_RECV_IP[j],curip)== true)//添加新端口时，如果和当前页面上已有的IP相同
			{
				var currentport = parseInt(addport,10)+i*parseInt(interval,10);
				if(currentport == parseInt(GBE_MULTI_VID_RECV_PORT[j],10))//判断如果端口也一样
				{
					if(language == ENGLISH)
					{
						alert(" Already exist in the same IP address and receiving port!");
					}
					else
					{
						alert("已存在相同的IP地址和接收端口！");
					}
					
					return;
				}
			}
		}
	}
	reternFlag = ValidateIP($("#addSrcIP1"), "SOURCE_IP", "<Source IP1>", "<源IP1>", language);
	if(reternFlag == false)
	{
		return false;
	}
	reternFlag = ValidateIP($("#addSrcIP2"), "SOURCE_IP", "<Source IP2>", "<源IP2>", language);
	if(reternFlag == false)
	{
		return false;
	}
	reternFlag = ValidateParam($("#addRefRate"), "INT_DEC", 0, 200000000, "<Reference Bitrate>", "<端口基准码率>", language);
	if(reternFlag == false)
	{
		return false;
	}
	$("#editportform").submit();
	submitcnt++;
	setTimeout("self.location.reload(true)",1000);
}
function updateportselect()
{
	var portUpdate;
	var selectdiv;
	portUpdate= document.getElementById("portsel");
	selectdiv = document.getElementById("portseldiv");
	if(inportnum <= 10)
	{
		parent.portbegin[GBE_NO] = 0;
	    parent.portend[GBE_NO]   = 256;
		parent.portselvalue[GBE_NO] = 256;
	}
	else
	{	
		selectdiv.style.display = "";
		if(language == ENGLISH)
		{
			var newitem = new Option("All", 256);
		}
		else
		{
			var newitem = new Option("显示全部", 256);
		}
		portUpdate.options.add(newitem);
	    var newitem = new Option("1~10",1);
		portUpdate.options.add(newitem);
		if(inportnum > 10)
		{
			var newitem = new Option("11~20",2);
		    portUpdate.options.add(newitem);
		}				
		if(inportnum > 20)
		{
			var newitem = new Option("21~30",3);
		    portUpdate.options.add(newitem);
			selectdiv.style.display = "";
		}
		if(inportnum > 30)
		{
			var newitem = new Option("31~40",4);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 40)
		{
			var newitem = new Option("41~50",5);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 50)
		{
			var newitem = new Option("51~60",6);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 60)
		{
			var newitem = new Option("61~70",7);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 70)
		{
			var newitem = new Option("71~80",8);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 80)
		{
			var newitem = new Option("81~90",9);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 90)
		{
			var newitem = new Option("91~100",10);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 100)
		{
			var newitem = new Option("101~110",11);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 110)
		{
			var newitem = new Option("111~120",12);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 120)
		{
			var newitem = new Option("121~130",13);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 130)
		{
			var newitem = new Option("131~140",14);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 140)
		{
			var newitem = new Option("141~150",15);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 150)
		{
			var newitem = new Option("151~160",16);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 160)
		{
			var newitem = new Option("161~170",17);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 170)
		{
			var newitem = new Option("171~180",18);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 180)
		{
			var newitem = new Option("181~190",19);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 190)
		{
			var newitem = new Option("191~200",20);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 200)
		{
			var newitem = new Option("201~210",21);
		    portUpdate.options.add(newitem);
		}	
		if(inportnum > 210)
		{
			var newitem = new Option("211~220",22);
		    portUpdate.options.add(newitem);
		}	
		if(inportnum > 220)
		{
			var newitem = new Option("221~230",23);
		    portUpdate.options.add(newitem);
		}	
		if(inportnum > 230)
		{
			var newitem = new Option("231~240",24);
		    portUpdate.options.add(newitem);
		}	
		if(inportnum > 240)
		{
			var newitem = new Option("241~250",25);
		    portUpdate.options.add(newitem);
		}
		if(inportnum > 250)
		{
			var newitem = new Option("251~256",26);
		    portUpdate.options.add(newitem);
		}				
	}
	var nowvalue = parent.portselvalue[GBE_NO];	
	$("#portsel").val(nowvalue);	
}
function portselect()
{
	var portUpdate;
	portUpdate = document.getElementById("portsel");
	var nowvalue = portUpdate.value;
	parent.portselvalue[GBE_NO] = nowvalue;
	
	if(256 == nowvalue)
	{
		parent.portbegin[GBE_NO] = 0;
		parent.portend[GBE_NO] = 256;
	}
	else
	{
		parent.portbegin[GBE_NO] = nowvalue*10 - 10;
		parent.portend[GBE_NO]   = nowvalue*10;
	}	
	self.location.reload(true);
}