var SNMP_TRAP_NUM = 3;
var NUM_DEV_CARD = 4;
var slotNum = 5;	//默认为主板页面
var CardIPChangeFlag = false;
var language;

var ctrlIpAddr = new Array(5);

function MgmtPortDisplay()
{
	var xmlhttp = getHTTPObject();
	var getParams = "type=18&cmd=1&language=" + language + "&getIpNetwork=1";
	var response;
	
	xmlhttp.open("POST", "/goform/formIPQAM", true);
	if((bd.browser != "IE") && (language == 1))
	{
		xmlhttp.overrideMimeType("text/html;charset=gb2312");
	}
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", getParams.length);

	xmlhttp.onreadystatechange = function()
    {
		if(xmlhttp.readyState == 4)
		{
			if((bd.browser == "IE") && (language == 1))
			{
				response = gb2utf8(xmlhttp.responseBody);
				var index = response.indexOf("</html>");
				var bias = response.length - index;
				response = response.slice(7, -bias);
			}
			else
			{
				response = xmlhttp.responseText.slice(7, -8);
			}
			var intData = 0;
			var cellCount = 0;
			var ifData = response.split("&");
			var i,id;
			var cnt = 0;
			
			document.getElementById("slot_name").innerHTML = ifData[cellCount++];
			
			/* Main Board - IP Address */	
			document.getElementById("ip_addr").value = ifData[cellCount];
			ctrlIpAddr[cnt++] = ifData[cellCount];
			
			/* Main Board - Subnet Mask */
			cellCount++;			
			document.getElementById("subnet_mask").value = ifData[cellCount];

			/* Main Board - MAC Address */
			cellCount++;
			document.getElementById("mac_addr").innerHTML = ifData[cellCount];
			
			/* Main Board - Default Gateway */
			cellCount++;
			document.getElementById("default_gateway").value = ifData[cellCount];
			
			for(i = 0; i < SNMP_TRAP_NUM; i++)
			{
				id = i + 1;
				/* Main Board - SNMP Trap Address */
				cellCount++;
				document.getElementById("snmp_addr" + id).value = ifData[cellCount];				
			}
			
			for(i = 0; i < (slotNum - 1); i++)
			{
				id = i + 1;
				
				cellCount++;
				document.getElementById("slot_name_" + id).innerHTML = ifData[cellCount];
				
				/* Card - IP Address */	
				cellCount++;
				document.getElementById("ip_addr_" + id).value = ifData[cellCount];
				ctrlIpAddr[cnt++] = ifData[cellCount];
				
				/* Card - Subnet Mask */
				cellCount++;			
				document.getElementById("subnet_mask_" + id).value = ifData[cellCount];
	
				/* Card - MAC Address */
				cellCount++;
				document.getElementById("mac_addr_" + id).innerHTML = ifData[cellCount];
				
				/* Card - Default Gateway */
				cellCount++;
				document.getElementById("default_gateway_" + id).value = ifData[cellCount];	
							
			}
			cellCount++;
			if(ifData[cellCount] == 1)
			{
				$("#vmCheck").prop("checked", true);
				$("#wanIp").attr("disabled", false);
				$("#wanPort1").attr("disabled", false);
				$("#wanPort2").attr("disabled", false);
				$("#wanPort3").attr("disabled", false);
				$("#wanPort4").attr("disabled", false);
				$("#vmTable").show();
				showAdvancedFlag = true;
				if(language == 0)
				{
					$("#advancedOption").val("Hide Advanced Options");
				}
				else
				{
					$("#advancedOption").val("隐藏高级选项");
				}
			}
			else
			{
				$("#vmCheck").prop("checked", false);
				$("#wanIp").attr("disabled", true);
				$("#wanPort1").attr("disabled", true);
				$("#wanPort2").attr("disabled", true);
				$("#wanPort3").attr("disabled", true);
				$("#wanPort4").attr("disabled", true);
				$("#vmTable").hide();
				showAdvancedFlag = false;
				if(language == 0)
				{
					$("#advancedOption").val("Show Advanced Options");
				}
				else
				{
					$("#advancedOption").val("显示高级选项");
				}
			}
			cellCount++;
			$("#wanIp").val(ifData[cellCount]);
			cellCount++;
			$("#wanPort1").val(ifData[cellCount]);
			cellCount++;
			$("#wanPort2").val(ifData[cellCount]);
			cellCount++;
			$("#wanPort3").val(ifData[cellCount]);
			cellCount++;
			$("#wanPort4").val(ifData[cellCount]);
			
			document.body.style.cursor="auto";
			window.status = "Done";
		}/* End of readyState */
	}/* End of onreadystatechange */
	xmlhttp.send(getParams);
	return true;
}

function DisableApply()
{
	disabledButton(document.getElementById("Apply"), true);
}

function EnableApply()
{
	disabledButton(document.getElementById("Apply"), false);
}


function adjustTable(slotNum)
{
	var ipTable = document.getElementById("ip_table");
	var tBody = ipTable.children[0];
	
	if(slotNum < 3)
	{
		document.getElementById("div_body").style.width = "500px";
		document.getElementById("div_tab").style.width = "480px";
		tBody.children[0].children[0].style.width = "30%";
		document.getElementById("trap_head").style.width = "30%";
	}
	else if(slotNum == 5)
	{
		return;
	}
	
	for(i=5,j=slotNum; i>j; i--)
	{
		for(k=0; k<5; k++)
		{
			tBody.children[k].children[i].style.display = "none";
		}
	}
	
	var per;
	switch(slotNum)
	{
		case 4:
			per = 20;
			break;
		case 3:
			per = 26;
			break;
		case 2:
			per = 35;
			break;
		case 1:
			per = 70;
			break;
		default:
			per = 16;
			break;
	}
	for(i=1; i<=slotNum; i++)
	{
		tBody.children[0].children[i].style.width = per + "%";
	}
	if(slotNum == 3)
	{
		tBody.children[0].children[3].style.width = "28%";
	}
}

function getSlotNum()
{
	var getParams = "type=18&cmd=1&getSlotNum=1";
	var xmlhttp = getHTTPObject();
	xmlhttp.open("POST", "/goform/formIPQAM", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", getParams.length);
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			slotNum = parseInt(xmlhttp.responseText.slice(7, -8));
			adjustTable(slotNum);
		}
	}
	
	xmlhttp.send(getParams);
	return true;
}

function PageLoadHandler()
{
	DisableApply();
	document.body.style.cursor = "wait";
	window.status = "Retrieving data, please wait...";
	getSlotNum();
	//adjustTable(slotNum);
	MgmtPortDisplay();
	CardIPChangeFlag = false;
}

function pageRefresh()
{
	PageLoadHandler();
}

/****************************************************************
	Populates the Channel table on page load
****************************************************************/
window.onload = function()
{
	var apply = document.getElementById("Apply");
	applyDispay(parent.group, apply);
	language = document.getElementById("language").value;
	PageLoadHandler();
	$("#vmTable").hide();
	$("#vmCheck").on("click", function()
	{
		EnableApply();
		if($(this).prop("checked") == true)
		{
			$("#wanIp").attr("disabled", false);
			$("#wanPort1").attr("disabled", false);
			$("#wanPort2").attr("disabled", false);
			$("#wanPort3").attr("disabled", false);
			$("#wanPort4").attr("disabled", false);
		}
		else
		{
			$("#wanIp").attr("disabled", true);
			$("#wanPort1").attr("disabled", true);
			$("#wanPort2").attr("disabled", true);
			$("#wanPort3").attr("disabled", true);
			$("#wanPort4").attr("disabled", true);
		}
	});
}
var showAdvancedFlag = false;
function displayAdvancedParam()
{
	if(showAdvancedFlag == false)
	{
		$("#vmTable").show();
		showAdvancedFlag = true;
		if(language == 0)
		{
			$("#advancedOption").val("Hide Advanced Options");
		}
		else
		{
			$("#advancedOption").val("隐藏高级选项");
		}
	}
	else
	{
		$("#vmTable").hide();
		showAdvancedFlag = false;
		if(language == 0)
		{
			$("#advancedOption").val("Show Advanced Options");
		}
		else
		{
			$("#advancedOption").val("显示高级选项");
		}
	}
}
function checkIpAddr(comp)
{
	if(Validate_IPAddress(comp.value) == false)
	{		
		if(language == 0)
		{
			alert("Invalid IP Address. Please input a valid IP Address.");
		}
		else
		{
			alert("无效的IP地址，请输入正确的IP地址。");
		}
		setTimeout(function() {comp.focus()}, 50);
		comp.select();
		return false;
	}
	return true;
}

function validateIP(obj, num)
{
	ctrlIpAddr[num] = obj.value;
	if(num != 0)
	{
		CardIPChange();
	}
	
	if(!checkIpAddr(obj))
	{		
		return false;
	}
	
	for(var i=0; i<slotNum; i++)
	{
		if(num == i)
		{
			continue;
		}
		if(obj.value == ctrlIpAddr[i])
		{
			if(language == 0)
			{
				alert("The ip address of mainboard and four cards should not be the same!");
			}
			else
			{
				alert("主板和子卡的IP地址不能一样！");
			}
			return false;
		}
	}
	return true;
}

function validateMask(obj)
{
	if(Valid_Mask(obj.value) == false)
	{
		if(language == 0)
		{
			alert("Invalid Subnet Mask. Please input a valid Subnet Mask.");
		}
		else
		{
			alert("无效的子网掩码，请输入正确的子网掩码。");
		}
		setTimeout(function () {obj.focus()}, 50);
		obj.select();
		return false;
	}
	return true;
}

function validateGateway(obj)
{
	if(Validate_IP_Format(obj.value) == false)
	{
		if(language == 0)
		{
			alert("Invalid IP Address. Please input a valid default Gateway IP Address.");
		}
		else
		{
			alert("无效的IP地址，请输入正确的默认网关IP地址。");
		}
		setTimeout(function () {obj.focus()}, 50);
		obj.select();
		return false;
	}
	return true;
}
function validatePort(obj)
{
	if(validateNumber(obj.value) == false)
	{
		if(language == 0)
		{
			alert("Invalid number. Please enter a valid numeric value for UDP Port.");
		}
		else
		{
			alert("无效的数字，请输入一个有效的UDP端口。");
		}
		setTimeout(function () {obj.focus()}, 50);
		obj.select();
		return false;
	}
	if((obj.value < 0) || (obj.value  > 65535))
	{
		if(language == 0)
		{
			alert("Valid range for Port is 0 - 65535.");
		}
		else
		{
			alert("端口号的范围是0 - 65535。");
		}
		setTimeout(function () {obj.focus()}, 50);
		obj.select();
		return false;
	}
	return true;  
}
function validateTrapAddr(obj)
{
	if(Validate_IP_Format(obj.value) == false)
	{		
		if(language == 0)
		{
			alert("Invalid SNMP Trap Address. Please input a valid SNMP Trap Address.");
		}
		else
		{
			alert("无效的SNMP Trap地址，请输入正确的SNMP Trap地址。");
		}
		setTimeout(function () {obj.focus()}, 50);
		obj.select();
		return false;
	}
	return true;
}
	
function ValidateAllIP()
{
	var obj;
	var i,j,id;
	var ip_value, mask_value, gw_value;
	
	ip_value = document.getElementById("ip_addr").value;
	mask_value = document.getElementById("subnet_mask").value;
	gw_value = document.getElementById("default_gateway").value;

	obj = document.getElementById("ip_addr");
	if(!checkIpAddr(obj))
	{
		return false;
	}
		
	obj = document.getElementById("subnet_mask");
	if(validateMask(obj) == false)
	{
		return false;
	}
	if(Validate_Subnet(ip_value, mask_value) == false)
	{
		if(language == 0)
		{
			alert("Subnet Mask and IP Address do not match .Please enter a valid Subnet Mask.")
		}
		else
		{
			alert("子网掩码和IP地址不相符，请输入一个有效的子网掩码。")
		}
		setTimeout(function () {obj.focus()}, 50);
		obj.select();
		return false;
	}
	
	obj = document.getElementById("default_gateway");
	if(validateGateway(obj) == false)
	{
		return false;
	}
	if(gw_value != "0.0.0.0")
	{
		if(Valid_Gateway(ip_value, mask_value, gw_value) == false)
		{
			if(language == 0)
			{
				alert("Gateway and IP Address do not fall in the same network .Please enter a valid Gateway.")
			}
			else
			{
				alert("网关和IP地址没有落到同一网段，请输入一个有效的网关。")
			}
			setTimeout(function () {obj.focus()}, 50);
			obj.select();
			return false;
		}
	}
	
	for(i = 0; i < (slotNum - 1); i++)
	{
		id = i + 1;
		ip_value = document.getElementById("ip_addr_" + id).value;
		mask_value = document.getElementById("subnet_mask_" + id).value;
		gw_value = document.getElementById("default_gateway_" + id).value;
		
		obj = document.getElementById("ip_addr_" + id);
		if(!checkIpAddr(obj))
		{
			return false;
		}
		
		obj = document.getElementById("subnet_mask_" + id);
		if(validateMask(obj) == false)
		{
			return false;
		}
		if(Validate_Subnet(ip_value, mask_value) == false)
		{
			if(language == 0)
			{
				alert("Subnet Mask and IP Address do not match .Please enter a valid Subnet Mask.")
			}
			else
			{
				alert("子网掩码和IP地址不相符，请输入一个有效的子网掩码。")
			}
			setTimeout(function () {obj.focus()}, 50);
			obj.select();
			return false;
		}
		
		obj = document.getElementById("default_gateway_" + id);
		if(validateGateway(obj) == false)
		{
			return false;
		}
		if(gw_value != "0.0.0.0")
		{
			if(Valid_Gateway(ip_value, mask_value, gw_value) == false)
			{
				if(language == 0)
				{
					alert("Gateway and IP Address do not fall in the same network .Please enter a valid Gateway.")
				}
				else
				{
					alert("网关和IP地址没有落到同一网段，请输入一个有效的网关。")
				}
				setTimeout(function () {obj.focus()}, 50);
				obj.select();
				return false;
			}
		}
	}
	
	for(i=0; i<slotNum; i++)
	{
		for(j=(i+1); j<slotNum;j++)
		{
			if(ctrlIpAddr[i] == ctrlIpAddr[j])
			{
				if(language == 0)
				{
					alert("The ip address of mainboard and four cards should not be the same!");
				}
				else
				{
					alert("主板和子卡的IP地址不能一样！");
				}
				return false;
			}
		}
	}
	
	for(i = 0; i < SNMP_TRAP_NUM; i++)
	{
		id = i + 1;
		obj = document.getElementById("snmp_addr" + id);
		if(validateTrapAddr(obj) == false)
		{
			return false;
		}
	}
	
	return true;	
}

function CardIPChange()
{
	CardIPChangeFlag = true;
}

function PortConfigThroDataBase()
{
	var i,id;
	/* Generate params to POST from the from controls*/
	var params = "type=18&cmd=2&";
	
	var ip_addr = document.getElementById("ip_addr").value;
	params = params + "ip_addr=" + ip_addr + "&";
	
	var subnet_mask = document.getElementById("subnet_mask").value;
	params = params + "subnet_mask=" + subnet_mask + "&";
	
	var default_gateway = document.getElementById("default_gateway").value;
	params = params + "default_gateway=" + default_gateway + "&";
	
	for(i = 0; i < SNMP_TRAP_NUM; i++)
	{
		id = i + 1;
		var snmp_addr = document.getElementById("snmp_addr" + id).value;
		params = params + "snmp_addr" + id + "=" + snmp_addr + "&";
	}
	
	for(var i = 0; i < (slotNum - 1); i++)
	{
		id = i + 1;
		var ip_addr = document.getElementById("ip_addr_" + id).value;
		params = params + "ip_addr_" + id + "=" + ip_addr + "&";
		
		var subnet_mask = document.getElementById("subnet_mask_" + id).value;
		params = params + "subnet_mask_" + id + "=" + subnet_mask + "&";
		
		var default_gateway = document.getElementById("default_gateway_" + id).value;
		params = params + "default_gateway_" + id + "=" + default_gateway + "&";
	}
	if($("#vmCheck").prop("checked") == true)
	{
		params = params + "vmCheck=1&wanIp=" + $("#wanIp").val() + "&wanPort1=" + $("#wanPort1").val() + "&wanPort2=" + $("#wanPort2").val() + "&wanPort3=" + $("#wanPort3").val() +"&wanPort4=" + $("#wanPort4").val();
	}
	else
	{
		params = params + "vmCheck=0";
	}

	var xmlhttp = getHTTPObject();
	xmlhttp.open("POST", "/goform/formIPQAM", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	
	xmlhttp.onreadystatechange = function()
	{
		if(xmlhttp.readyState == 4)
		{
			if(language == 0)
			{
				alert("System IP network configuration updated successfully.");
			}
			else
			{
				alert("系统IP参数已设置成功。");
			}
			if(CardIPChangeFlag == true)
			{
				var url;
				if(language == 0)
				{
					url = "../asp/main.asp?nodeId=" + parent.curTree.getSelectedNodes()[0].id;
				}
				else
				{
					url = "../asp/main_cn.asp?nodeId=" + parent.curTree.getSelectedNodes()[0].id;
				}
				window.parent.location.replace(url);
			}
			else
			{
				PageLoadHandler();
			}
		}
	}
	xmlhttp.send(params)
	return;
}

function SetPortConfiguration()
{
	DisableApply();
	document.body.style.cursor = "wait";
	window.status = "Applying settings, please wait...";
	
	if(ValidateAllIP() == true)
	{
		PortConfigThroDataBase();	
	}
	else
	{
		window.status = "Done";
		document.body.style.cursor = "auto";
		EnableApply();
		return false;
	}
}