/*************************************************
Function: 		GetArgs
Description: 	从urld地址获取参数
Input: 			无
Output:			无
Return: 		参数值,args[name]=value
Others: 		无
*************************************************/
function GetArgs() 
{
    var args = new Object();
    var query = location.search.substring(1);  //Get query string
    var pairs = query.split("&");  //Break at & ampersand
    for(var i = 0; i < pairs.length; i++) 
	{
        var pos = pairs[i].indexOf('=');  //Look for "name=value"
        if(pos == -1) 
		{
			continue;  //If not found, skip
		}
        var argname = pairs[i].substring(0, pos);  //Extract the name
        var value = pairs[i].substring(pos + 1);  //Extract the value
        value = decodeURIComponent(value);  //Decode it, if needed
        args[argname] = value;  //Store as a property
    }
    return args;  //Return the object
}

function ValidateAuthCode(obj, language)
{
	var patten = /^([A-F0-9]){32}$/;
	var licence;
	
	licence = obj.val();
	if((licence != DISPLAY_STRING) && (!patten.test(licence)))
	{
		if(language == ENGLISH)
		{
			alert("Please input valid licence!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的授权码！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false;
	}
	
	return true;
}

function ValidateName(obj, language)
{
	var nameValue = obj.val();
	var length = nameValue.length;
	
	for(var i = 0; i < length; i++)
	{
		char = nameValue.charAt(i);
		if(char == '"')
		{
			if(language == ENGLISH)
			{
				alert("Can't input the double quotes!");
			}
			else
			{
				alert("不能输入双引号！");
			}
			return false;
		}
	}
	
	return true;
}
/*************************************************
Function:  		ValidateParam
Description:  	非IP类参数校验，弹出校验结果，输入无效则聚焦
Input:  		obj: current param jquery object; 
				type: param type; 
				minValue: min value; 
				maxValue: max value; 
				str1: English param name; 
				str2: Chinese param name;
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		minValue and maxValue both 0, value is no range
				when type is "LENGTH", minValue and maxValue are the length value
*************************************************/
function ValidateParam(obj, type, minValue, maxValue, str1, str2, language)
{
	var patten;
	var strVal = obj.val();
	var numVal;
	switch(type) 
	{
		case "INT_DEC":  //十进制整数
			patten = /^-?\d+$/;
			if(!patten.test(strVal))
			{
				if(language == ENGLISH)
				{
					alert(str1 + " should be decimal integer!");
				}
				else if(language == CHINESE)
				{
					alert(str2 + "应该是十进制整数！");
				}
				setTimeout(function () {obj.focus()}, 0);
				setTimeout(function () {obj.select()}, 0);
				return false; 
			}
			break;
			
		case "INT_DEC_HEX":  //十进制或十六进制（以0x或0X开头），非负整数
			patten = /(^\d+$)|(^0[xX]([A-Fa-f0-9])+$)/;
			if(!patten.test(strVal))
			{
				if(language == ENGLISH)
				{
					alert(str1 + " should be non-negative integer(decimal or Hexadecimal which starts with 0x or 0X)!");
				}
				else if(language == CHINESE)
				{
					alert(str2 + "应该是非负整数（十进制或者十六进制，其中十六进制数以0x或0X起始）！");
				}
				setTimeout(function () {obj.focus()}, 0);
				setTimeout(function () {obj.select()}, 0);
				return false; 
			}
			break;
			
		case "INT_HEX":  //十六进制非负整数
			patten = /^([A-Fa-f0-9])+$/;
			if(!patten.test(strVal))
			{
				if(language == ENGLISH)
				{
					alert(str1 + " should be Hexadecimal non-negative integer!");
				}
				else if(language == CHINESE)
				{
					alert(str2 + "应该是十六进制非负整数！");
				}
				setTimeout(function () {obj.focus()}, 0);
				setTimeout(function () {obj.select()}, 0);
				return false; 
			}
			break;
			
		case "INT_HEX_NULL":  //十六进制非负整数，可以为空
			patten = /^([A-Fa-f0-9])*$/;
			if(!patten.test(strVal))
			{
				if(language == ENGLISH)
				{
					alert(str1 + " should be Hexadecimal non-negative integer!");
				}
				else if(language == CHINESE)
				{
					alert(str2 + "应该是十六进制非负整数！");
				}
				setTimeout(function () {obj.focus()}, 0);
				setTimeout(function () {obj.select()}, 0);
				return false; 
			}
			break;
			
		case "LENGTH":  //只对长度有要求
			if(minValue == maxValue)  //Length is fixed
			{
				if(strVal.length != minValue)
				{
					if(language == ENGLISH)
					{
						alert("The length of " + str1 + " should be " + minValue + "!");
					}
					else if(language == CHINESE)
					{
						alert(str2 + "长度应该为 " + minValue + "！");
					}
					setTimeout(function () {obj.focus()}, 0);
					setTimeout(function () {obj.select()}, 0);
					return false;
				}
			}
			else
			{
				if((strVal.length < minValue) || (strVal.length > maxValue))  //长度范围
				{
					if(language == ENGLISH)
					{
						alert("The length of " + str1 + " should be between " + minValue + " and " + maxValue + "!");
					}
					else if(language == CHINESE)
					{
						alert(str2 + "长度应该在 " + minValue + "和" + maxValue + "之间！");
					}			
					setTimeout(function () {obj.focus()}, 0);
					setTimeout(function () {obj.select()}, 0);
					return false;
				}
			} 
			break;
			
		case "FLOATING":  //非负浮点数
			var patten  = /^\d+(\.\d+)?$/;
			if(!patten.test(strVal))
			{
				if(language == ENGLISH)
				{
					alert(str1 + "Please input valid number!");
				}
				else if(language == CHINESE)
				{
					alert(str2 + "请输入合法的数字！");
				}
				setTimeout(function () {obj.focus()}, 0);
				setTimeout(function () {obj.select()}, 0);
				return false;
			}
			break;
		default:
			break;
	}
	if(type != "LENGTH")  //数值范围，非长度范围
	{
		if(type != "FLOATING")  //整数
		{
			var ch = strVal.slice(0, 2);
			if((ch == "0x") || (ch == "0X"))  //十六进制
			{
				numVal = parseInt(strVal, 16);
			}
			else  //十进制
			{
				numVal = parseInt(strVal, 10);
			}
		}
		else  //浮点数
		{
			numVal = parseFloat(strVal);
		}
		if((minValue < maxValue) || ((minValue > 0) && (minValue = maxValue))) //有数值范围,主千兆卡端口添加告警可能范围是[1,1]，输入0,0表示参数没有取值范围
		{
			if((numVal < minValue) || (numVal > maxValue))
			{
				if(language == ENGLISH)
				{
					alert(str1 + "should be between " + minValue + " and " + maxValue + "!");
				}
				else if(language == CHINESE)
				{
					alert(str2 + "应该在" + minValue + "和" + maxValue + "之间！");
				}
				setTimeout(function () {obj.focus()}, 0);
				setTimeout(function () {obj.select()}, 0);
				return false;
			}
		}
	}
	return true; 
}

/*************************************************
Function:  		ValidateIP
Description:  	校验IP参数，弹出校验结果，输入无效则聚焦
Input:  		obj: current IP param jquery object;
				type: param type; 
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateIP(obj, type, str1, str2, language)
{
	switch(type) 
	{
		case "UNICAST_IP_ADDR":  //first field value [1, 223] exclude 127, not including 223.255.255.255
			if(ValidateUnicastIP(obj, str1, str2, language) == false)
			{
				return false;
			}
			break;
		case "IP_ADDR":  //first field value [1, 255] exclude 127
			if(ValidateUniAndMultiIP(obj, str1, str2, language) == false)
			{
				return false;
			}
			break;
		case "SOURCE_IP":
			if(ValidateSourceIP(obj, str1, str2, language) == false)
			{
				return false;
			}
			break;
		case "SUBNET_MASK":  //子网掩码，固定的格式
			if(ValidateSubnetMask(obj, str1, str2, language) == false)
			{
				return false;
			}
			break;
		case "GATEWAY":  //all fields value [0, 255]
			if(ValidateGateway(obj, str1, str2, language) == false)
			{
				return false;
			}
			break;
		default:
			break;
	}
	return true; 
}

/*************************************************
Function:  		ValidateUnicastIP
Description:  	校验单播IP参数，弹出校验结果，输入无效则聚焦
				单播IP类的要求：
				(1) IP格式检验正确
				(2) 第一个字节必须在[1,223]内，不包含223.255.255.255
				(3) 不能为127.x.x.x
Input:  		obj: current unicast IP param jquery object;
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateUnicastIP(obj, str1, str2, language)
{
	var ipAddr = obj.val();
	var ipAddrFields = ipAddr.split(".");
	var length = ipAddrFields.length;
	var firstFieldValue = parseInt(ipAddrFields[0]);
	
	if(ValidateIPFormat(ipAddr) == false)
	{
		if(language == ENGLISH)
		{
			alert("Please input valid " + str1 + " address!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的" + str2 + "！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false;
	}
	
	if((firstFieldValue < 1) || (firstFieldValue > 223) || (firstFieldValue == 127))
	{
		if(language == ENGLISH)
		{
			alert(str1 + " should be a unicast address!(the first field value range should be [1, 223], not including 127)");
		}
		else if(language == CHINESE)
		{
			alert(str2 + "应该是单播地址！（单播地址第一个值的范围是[1, 223], 不包含127）");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false; 
	}
	
	if(ipAddr == "223.255.255.255")
	{
		if(language == ENGLISH)
		{
			alert("223.255.255.255(reserved) is not valid" + str1 + " address!");
		}
		else if(language == CHINESE)
		{
			alert("223.255.255.255（保留）不是合法的" + str2 + "！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false; 
	}
	return true;
}

/*************************************************
Function:  		ValidateUniAndMultiIP
Description:  	校验单播或组播IP参数，弹出校验结果，输入无效则聚焦
				单播或组播IP类的要求：
				(1) IP格式检验正确
				(2) 第一个字节不能为0
				(3) 不能为127.x.x.x
Input:  		obj: current unicast or multicast IP param jquery object;
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateUniAndMultiIP(obj, str1, str2, language)
{
	var ipAddr = obj.val();
	var ipAddrFields = ipAddr.split(".");
	var length = ipAddrFields.length;
	var firstFieldValue = parseInt(ipAddrFields[0]);
	
	if(ValidateIPFormat(ipAddr) == false)
	{
		if(language == ENGLISH)
		{
			alert("Please input valid " + str1 + " address!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的" + str2 + "！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false;
	}
	
	if((firstFieldValue == 0) || (firstFieldValue == 127))
	{
		if(language == ENGLISH)
		{
			alert("The first field value range of" + str1 + "should be [1, 255], not including 127!");
		}
		else if(language == CHINESE)
		{
			alert(str2 + "第一个值的范围是[1, 255], 不包含127！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false; 
	}
	return true;
}

/*************************************************
Function:  		ValidateSourceIP
Description:  	校验源IP参数，弹出校验结果，输入无效则聚焦
				源IP类的要求：
				(1) IP格式检验正确
				(2) 不能为127.x.x.x
Input:  		obj: current unicast or multicast IP param jquery object;
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateSourceIP(obj, str1, str2, language)
{
	var ipAddr = obj.val();
	var ipAddrFields = ipAddr.split(".");
	var length = ipAddrFields.length;
	var firstFieldValue = parseInt(ipAddrFields[0]);
	
	if(ValidateIPFormat(ipAddr) == false)
	{
		if(language == ENGLISH)
		{
			alert("Please input valid " + str1 + " address!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的" + str2 + "！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false;
	}
	
	if(firstFieldValue == 127)
	{
		if(language == ENGLISH)
		{
			alert("The first field value range of" + str1 + "should be [0, 255], not including 127!");
		}
		else if(language == CHINESE)
		{
			alert(str2 + "第一个值的范围是[0, 255], 不包含127！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false; 
	}
	return true;
}

/*************************************************
Function:  		ValidateSubnetMask
Description:  	校验子网掩码，弹出校验结果，输入无效则聚焦
				子网掩码类的要求：
				(1) IP格式检验正确
				(2) 子网掩码不能为全0和全1，其中的1必须是连续的且位于最高若干位
Input:  		obj: current subnet mask param jquery object;
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateSubnetMask(obj, str1, str2, language)
{
    var flag = -1;
	var mask = obj.val();
    var num = IP2Num(mask);
    var str = num.toString(2);
	
	if(ValidateIPFormat(mask) == false)
	{
		if(language == ENGLISH)
		{
			alert("Please input valid " + str1 + " address!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的" + str2 + "！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false;
	}
    for(var i = 0; i < str.length; i++) 
    {
        if(str.charAt(i) == "0")
		{
            flag = i;
		}
        if((str.charAt(i) == "1") && (flag > -1))
		{
			if(language == ENGLISH)
			{
				alert(str1 + " format is not correct!");
			}
			else if(language == CHINESE)
			{
				alert(str2 + "格式不正确！");
			}
			setTimeout(function () {obj.focus()}, 0);
			setTimeout(function () {obj.select()}, 0);
			return false;
		}
    }
    return true;
}

/*************************************************
Function:  		ValidateSubnetMaskRelation
Description:  	校验子网掩码和IP地址关系的函数，弹出校验结果，输入无效则聚焦
				子网掩码和IP地址关系的要求：
				(1) 设IP地址和子网掩码都转换为网络字节序的整数之后为ip，mask，则需满足：(ip&(~mask)!=0)&&(ip&(~mask)!=(~mask))
Input:  		ip: IP address
				maskObj: current subnet mask param jquery object;
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateSubnetMaskRelation(ip, maskObj, str1, str2, language)
{
	var mask = maskObj.val();
	var ipNum;
	var maskNum;
	var networkByte;
	var antiMask;
	
	ipNum = IP2Num(ip);
	maskNum = IP2Num(mask);
	networkByte = ipNum & (~maskNum);
	antiMask = ~maskNum;
	if((networkByte == 0) || (networkByte == antiMask))
	{
		if(language == ENGLISH)
		{
			alert("Please input valid " + str1 + ", Subnet Mask and IP Address do not match!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的" + str2 + "，子网掩码与IP地址不相符！");
		}
		setTimeout(function(){maskObj.focus()}, 0);
		setTimeout(function(){maskObj.select()}, 0);
		return false;
	}
	return true;
}

/*************************************************
Function:  		ValidateGateway
Description:  	校验网关，弹出校验结果，输入无效则聚焦
				网关类的要求：
				(1) IP格式检验正确
Input:  		obj: current gateway param jquery object;
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateGateway(obj, str1, str2, language)
{
	var gateway = obj.val();
	
	if(ValidateIPFormat(gateway) == false)
	{
		if(language == ENGLISH)
		{
			alert("Please input valid " + str1 + " address!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的" + str2 + "！");
		}
		setTimeout(function () {obj.focus()}, 0);
		setTimeout(function () {obj.select()}, 0);
		return false;
	}
	return true;
}

/*************************************************
Function:  		ValidateGatewayRelation
Description:  	校验网关和IP地址关系的函数，弹出校验结果，输入无效则聚焦
				网关和IP地址关系的要求：
				(1) 子网掩码、网关和IP必须落在同一网段
Input:  		ip: IP address
				mask: subnet mask
				gatewayObj: current gateway param jquery object;
				str1: English param name; 
				str2: Chinese param name; 
				language: current page language, 1-English; 2-Chinese
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateGatewayRelation(ip, mask, gatewayObj, str1, str2, language)
{
    var gateway = gatewayObj.val();
	var ipNum;
	var maskNum;
	var gatewayNum;
	var ipMask;
	var gatewayMask;
	
    ipNum = IP2Num(ip);
    maskNum = IP2Num(mask);
    gatewayNum = IP2Num(gateway);
    ipMask = (ipNum & maskNum)>>>0;
    gatewayMask = (gatewayNum & maskNum)>>>0;
    if(ipMask != gatewayMask)
	{
		if(language == ENGLISH)
		{
			alert("Please input valid " + str1 + ", Gateway and IP Address do not fall in the same network!");
		}
		else if(language == CHINESE)
		{
			alert("请输入合法的" + str2 + "，网关和IP地址没有落到同一网段！");
		}
		setTimeout(function(){gatewayObj.focus()}, 0);
		setTimeout(function(){gatewayObj.select()}, 0);
		return false;
	}
    return true;
}

/*************************************************
Function:  		ValidateIPFormat
Description:  	校验IP地址格式的函数，弹出校验结果，输入无效则聚焦
				IP地址格式的要求：
				(1) 4个数字用“.”连接起来
				(2) 每个数字是范围为[0, 255]的整数
Input:  		ipAddr: IP address
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateIPFormat(ipAddr)
{
	var ipAddrFields = ipAddr.split(".");
	var length = ipAddrFields.length;
	var fieldValue;
	
	// make sure there are 4 octets
	if(length != 4)
	{
		return false;
	}
	for(var i = 0; i < length; i++)
	{
		if(ValidateNumber(ipAddrFields[i]) == false)
		{
			return false;
		}
		fieldValue = parseInt(ipAddrFields[i]);
		if((fieldValue < 0) || (fieldValue > 255))
		{
			return false;
		}
	}
	return true;
}

/*************************************************
Function:  		ValidateNumber
Description:  	校验非负整数的函数，弹出校验结果，输入无效则聚焦
Input:  		input: 需要校验的值
Output:			无
Return:  		if validate, return ture; else, return false
Others: 		无
*************************************************/
function ValidateNumber(input)
{
	var len = input.length;

	if(len == 0)
	{
		return false;
	}
	for(var i = 0; i < len; i++)
	{
		if(isNaN(parseInt(input.charAt(i))))
		{
			return false;
		}
	}
	return true;
}

/*************************************************
Function:  		IP2Num
Description:  	IP地址转换成无符号整数的函数，弹出校验结果，输入无效则聚焦
Input:  		ip: IP address
Output:			无
Return:  		number
Others: 		无
*************************************************/
function IP2Num(ip) 
{
    var num = 0;
    ip = ip.split(".");
    num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]);
    num = num >>> 0;
    return num;
}

/*************************************************
Function:  		Num2IP
Description:  	无符号整数转换成IP地址的函数，弹出校验结果，输入无效则聚焦
Input:  		num: number
Output:			无
Return:  		IP address
Others: 		无
*************************************************/
function Num2IP(num) 
{
    var str;
    var ip = new Array();
    ip[0] = (num >>> 24) >>> 0;
    ip[1] = ((num << 8) >>> 24) >>> 0;
    ip[2] = (num << 16) >>> 24;
    ip[3] = (num << 24) >>> 24;
    str = String(ip[0]) + "." + String(ip[1]) + "." + String(ip[2]) + "." + String(ip[3]);
    return str;
}

function ipcompare(str1,str2)
{
	var arr1 = str1.split(".");
	var arr2 = str2.split(".");
	if((parseInt(arr1[0],10) == parseInt(arr2[0],10))&&
		(parseInt(arr1[1],10) == parseInt(arr2[1],10))&&
		(parseInt(arr1[2],10) == parseInt(arr2[2],10))&&
		(parseInt(arr1[3],10) == parseInt(arr2[3],10)))
	{
		return true;
	}
	else
	{
		return false;
	}	
}
/*************************************************
Function: 		GetHTTPObject
Description: 	创建用于ajax传输的交互对象
Input: 			无
Output: 		无
Return: 		返回创建的对象
Others: 		无
*************************************************/
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 xmlHttp != "undefined"))
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch(e)
		{
			xmlHttp = false;
		}
	}

	return xmlHttp;
}