LanDHCPURL = new Array("DHCPDisable.htm", "DHCPEnable.htm", "DHCPRelay.htm");

function SelLanDHCP(obj)
{
	n = obj.selectedIndex;
	parent.LanDHCP.location.href = LanDHCPURL[n];	
}

function FillDhcpReleaseTimeIn ()
{
	var	d = document.forms[0].DhcpReleaseTime.value;

	if ( d == '' ) { document.forms[0].DhcpReleaseTime.value = '0'; }
	else { document.forms[0].DhcpReleaseTime.value = ( parseInt ( d , 10 ) ); }
}

function ValidatingDhcpReleaseTime ()
{
	var	d = parseInt ( document.forms[0].DhcpReleaseTime.value , 10 );

	if (isNaN(d)) {
		//alert('Please enter a number!');
		alert(mavis_str(Alert_Msg_9));
		document.forms[0].DhcpReleaseTime.focus();		
		return 0;
	}
	if ( (d < 0) || (d > 86400) ) {
    		document.forms[0].DhcpReleaseTime.value = '0';
			alert(mavis_str(Alert_Msg_10));
		//alert('The Release Time is out of range [0 - 86400]');
		document.forms[0].DhcpReleaseTime.focus();
    		return false;
	}

//	document.forms[0].DhcpReleaseTime.value = d * 60;

	return true;
}

function SplitDhcpStartEnd ( IPStringStart, IPStringEnd)
{
	document.forms[0].StartIP.value = GetIPSegment ( IPStringStart , 4 );
	document.forms[0].EndIP.value = GetIPSegment ( IPStringEnd , 4 );

	return true;
}

var	LanIPAddress_1 = GetIPSegment ( '<%nvram_get("lan_ipaddr");%>' , 1 );
var	LanIPAddress_2 = GetIPSegment ( '<%nvram_get("lan_ipaddr");%>' , 2 );
var	LanIPAddress_3 = GetIPSegment ( '<%nvram_get("lan_ipaddr");%>' , 3 );

function AssembleDhcpStartEndAddress ()
{
	if ( !(IPCheck(document.forms[0].StartIP)) ) 
	{
		document.forms[0].StartIP.value = 0;
   		return false;
  	}
	
	if ( !(IPCheck(document.forms[0].EndIP)) ) 
	{
    		document.forms[0].EndIP.value = 0;
   		return false;
  	}  	

	document.forms[0].dhcp_start.value = LanIPAddress_1 + '.' +
				        LanIPAddress_2 + '.' +
				        LanIPAddress_3 + '.' +
				        document.forms[0].StartIP.value;
	document.forms[0].dhcp_end.value = LanIPAddress_1 + '.' +
				        LanIPAddress_2 + '.' +
				        LanIPAddress_3 + '.' +
				        document.forms[0].EndIP.value;				        
	return true;
}

function GettingNumOfUser ()
{
	var DhcpEnd =  parseInt(document.forms[0].dhcp_end.value,10);
	var DhcpStart = parseInt(document.forms[0].dhcp_start.value,10);
	document.forms[0].NumOfUsers.value = parseInt ( DhcpEnd , 10 ) - parseInt (DhcpStart , 10 ) + 1;
}

function AssembleDhcpStartAddress (passForm)
{
	lan_ipaddr = document.forms[0].lan_ipaddr_1.value + '.';
  	lan_ipaddr = lan_ipaddr + document.forms[0].lan_ipaddr_2.value + '.';
  	lan_ipaddr = lan_ipaddr + document.forms[0].lan_ipaddr_3.value + '.';
  	lan_ipaddr = lan_ipaddr + document.forms[0].lan_ipaddr_4.value;
  start_ipaddr = document.forms[0].lan_ipaddr_1.value + '.';
  	start_ipaddr = start_ipaddr + document.forms[0].lan_ipaddr_2.value + '.';
  	start_ipaddr = start_ipaddr + document.forms[0].lan_ipaddr_3.value + '.';
  	start_ipaddr = start_ipaddr + document.forms[0].StartIP.value;
  if ( lan_ipaddr == start_ipaddr) {
  	alert("Start IP of DHCP Server can't be set the same as local IP!");
  	return false;
  }
	if ( !(IPCheck(document.forms[0].StartIP)) ) {
    		document.forms[0].StartIP.value = 0;
    		return false;
  	}
	document.forms[0].lan_dhcps_start_ip.value = document.forms[0].StartIP.value;
	return true;
}

function AssembleDhcpEndAddress (passForm)
{
	var	d = parseInt ( document.forms[0].NumOfUsers.value , 10 );
	
	if (isNaN(d)) {
		alert(mavis_str(Alert_Msg_9));
		//alert('Please enter a number!');
		document.forms[0].NumOfUsers.focus();
		return 0;
	}
	
	if ( ( d <= 0 ) || ( d > 255 ) ) {
		document.forms[0].NumOfUsers.value = 0;
		alert(mavis_str(Alert_Msg_25));
		//alert('The number of users is out of range [1 - 255]');
		document.forms[0].NumOfUsers.focus();
    		return false;
	}

	
	d = parseInt ( document.forms[0].NumOfUsers.value , 10 ) + parseInt ( document.forms[0].StartIP.value , 10 ) - 1;
	//if (parseInt(document.forms[0].lan_ipaddr_4.value, 10 )<parseInt(document.forms[0].StartIP.value, 10) || parseInt(document.forms[0].lan_ipaddr_4.value, 10 )>d)
	//	d++;
	if (d > 254)
		d = 254;
	document.forms[0].lan_dhcps_end_ip.value = d;
	return true;
}


function ShowDHCPRange(passForm)
{
	document.write(document.forms[0].dhcp_start.value + " ~ ");
	document.write(GetIPSegment(document.forms[0].dhcp_end.value, 4));
}

function SplitDHCPDNS1Address ( IPString )
{
	document.forms[0].DHCP_DNS1_1.value = GetIPSegment ( IPString , 1 );
	document.forms[0].DHCP_DNS1_2.value = GetIPSegment ( IPString , 2 );
	document.forms[0].DHCP_DNS1_3.value = GetIPSegment ( IPString , 3 );
	document.forms[0].DHCP_DNS1_4.value = GetIPSegment ( IPString , 4 );
	return true;
}

function SplitDHCPDNS2Address ( IPString )
{
	document.forms[0].DHCP_DNS2_1.value = GetIPSegment ( IPString , 1 );
	document.forms[0].DHCP_DNS2_2.value = GetIPSegment ( IPString , 2 );
	document.forms[0].DHCP_DNS2_3.value = GetIPSegment ( IPString , 3 );
	document.forms[0].DHCP_DNS2_4.value = GetIPSegment ( IPString , 4 );
	return true;
}

function SplitDHCPDNS3Address ( IPString )
{
	document.forms[0].DHCP_DNS3_1.value = GetIPSegment ( IPString , 1 );
	document.forms[0].DHCP_DNS3_2.value = GetIPSegment ( IPString , 2 );
	document.forms[0].DHCP_DNS3_3.value = GetIPSegment ( IPString , 3 );
	document.forms[0].DHCP_DNS3_4.value = GetIPSegment ( IPString , 4 );
	return true;
}

function SplitDHCPWINSAddress ( IPString )
{
	document.forms[0].WINS_1.value = GetIPSegment ( IPString , 1 );
	document.forms[0].WINS_2.value = GetIPSegment ( IPString , 2 );
	document.forms[0].WINS_3.value = GetIPSegment ( IPString , 3 );
	document.forms[0].WINS_4.value = GetIPSegment ( IPString , 4 );
	return true;
}
function AssembleDHCPDNS1Address ()
{
	if (document.forms[0].DHCP_DNS1_1.value == 0 && document.forms[0].DHCP_DNS1_2.value == 0 && document.forms[0].DHCP_DNS1_3.value == 0 && document.forms[0].DHCP_DNS1_4.value == 0)
		d = "0.0.0.0";	
	else {
		if ( !(IPCheck(document.forms[0].DHCP_DNS1_1)) ) {
	    		document.forms[0].DHCP_DNS1_1.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS1_2)) ) {
	    		document.forms[0].DHCP_DNS1_2.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS1_3)) ) {
	    		document.forms[0].DHCP_DNS1_3.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS1_4)) ) {
	    		document.forms[0].DHCP_DNS1_4.value = 0;
	    		return false;
	  	}
		if ((document.forms[0].DHCP_DNS1_1.value==255) &&
		    (document.forms[0].DHCP_DNS1_2.value==255) &&
		    (document.forms[0].DHCP_DNS1_3.value==255) &&
		    (document.forms[0].DHCP_DNS1_4.value==255)
		   )
		{
			alert(mavis_str(Alert_Msg_46));
			//alert("All 255 is invalid!");
			document.forms[0].DHCP_DNS1_1.focus();
			return 0;
		}  	
		if ((document.forms[0].DHCP_DNS1_4.value==0) ||
			(document.forms[0].DHCP_DNS1_4.value==255)
			)
		{
			alert(mavis_str(Alert_Msg_26));
			//alert("0 or 255 is invalid!");
			document.forms[0].DHCP_DNS1_4.focus();
			return 0;
		}
		d = document.forms[0].DHCP_DNS1_1.value + '.';
	  	d = d + document.forms[0].DHCP_DNS1_2.value + '.';
	  	d = d + document.forms[0].DHCP_DNS1_3.value + '.';
	  	d = d + document.forms[0].DHCP_DNS1_4.value;
	
		//document.forms[0].DHCP_DNS.value = d;
	}
	document.forms[0].lan_dns1.value = d;
	return true;
}

function AssembleDHCPDNS2Address ()
{
	if (document.forms[0].DHCP_DNS2_1.value == 0 && document.forms[0].DHCP_DNS2_2.value == 0 && document.forms[0].DHCP_DNS2_3.value == 0 && document.forms[0].DHCP_DNS2_4.value == 0)
		d = "0.0.0.0";	
	else {
		if ( !(IPCheck(document.forms[0].DHCP_DNS2_1)) ) {
	    		document.forms[0].DHCP_DNS2_1.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS2_2)) ) {
	    		document.forms[0].DHCP_DNS2_2.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS2_3)) ) {
	    		document.forms[0].DHCP_DNS2_3.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS2_4)) ) {
	    		document.forms[0].DHCP_DNS2_4.value = 0;
	    		return false;
	  	}
		if ((document.forms[0].DHCP_DNS2_1.value==255) &&
		    (document.forms[0].DHCP_DNS2_2.value==255) &&
		    (document.forms[0].DHCP_DNS2_3.value==255) &&
		    (document.forms[0].DHCP_DNS2_4.value==255)
		   )
		{
			alert(mavis_str(Alert_Msg_46));
			//alert("All 255 is invalid!");
			document.forms[0].DHCP_DNS2_1.focus();
			return 0;
		}  	
		if ((document.forms[0].DHCP_DNS2_4.value==0) ||
			(document.forms[0].DHCP_DNS2_4.value==255)
			)
		{
			alert(mavis_str(Alert_Msg_26));
			//alert("0 or 255 is invalid!");
			document.forms[0].DHCP_DNS2_4.focus();
			return 0;
		}
		d = document.forms[0].DHCP_DNS2_1.value + '.';
	  	d = d + document.forms[0].DHCP_DNS2_2.value + '.';
	  	d = d + document.forms[0].DHCP_DNS2_3.value + '.';
	  	d = d + document.forms[0].DHCP_DNS2_4.value;
	
		//document.forms[0].DHCP_DNS.value = d;
	}
	document.forms[0].lan_dns2.value = d;
	return true;
}
function AssembleDHCPDNS3Address ()
{
	if (document.forms[0].DHCP_DNS3_1.value == 0 && document.forms[0].DHCP_DNS3_2.value == 0 && document.forms[0].DHCP_DNS3_3.value == 0 && document.forms[0].DHCP_DNS3_4.value == 0)
		d = "0.0.0.0";	
	else {
		if ( !(IPCheck(document.forms[0].DHCP_DNS3_1)) ) {
	    		document.forms[0].DHCP_DNS3_1.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS3_2)) ) {
	    		document.forms[0].DHCP_DNS3_2.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS3_3)) ) {
	    		document.forms[0].DHCP_DNS3_3.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].DHCP_DNS3_4)) ) {
	    		document.forms[0].DHCP_DNS3_4.value = 0;
	    		return false;
	  	}
		if ((document.forms[0].DHCP_DNS3_1.value==255) &&
		    (document.forms[0].DHCP_DNS3_2.value==255) &&
		    (document.forms[0].DHCP_DNS3_3.value==255) &&
		    (document.forms[0].DHCP_DNS3_4.value==255)
		   )
		{
			alert(mavis_str(Alert_Msg_46));
			//alert("All 255 is invalid!");
			document.forms[0].DHCP_DNS3_1.focus();
			return 0;
		}  	
		if ((document.forms[0].DHCP_DNS3_4.value==0) ||
			(document.forms[0].DHCP_DNS3_4.value==255)
			)
		{
			alert(mavis_str(Alert_Msg_26));
			//alert("0 or 255 is invalid!");
			document.forms[0].DHCP_DNS3_4.focus();
			return 0;
		}
		d = document.forms[0].DHCP_DNS3_1.value + '.';
	  	d = d + document.forms[0].DHCP_DNS3_2.value + '.';
	  	d = d + document.forms[0].DHCP_DNS3_3.value + '.';
	  	d = d + document.forms[0].DHCP_DNS3_4.value;
	
		//document.forms[0].DHCP_DNS.value = d;
	}
	document.forms[0].lan_dns3.value = d;
	return true;
}
function AssembleDHCPWINSAddress ()
{
	if (document.forms[0].WINS_1.value == 0 && document.forms[0].WINS_2.value == 0 && document.forms[0].WINS_3.value == 0 && document.forms[0].WINS_4.value == 0)
		d = "0.0.0.0";	
	else {
		if ( !(IPCheck(document.forms[0].WINS_1)) ) {
	    		document.forms[0].WINS_1.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].WINS_2)) ) {
	    		document.forms[0].WINS_2.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].WINS_3)) ) {
	    		document.forms[0].WINS_3.value = 0;
	    		return false;
	  	}
	
		if ( !(IPCheck(document.forms[0].WINS_4)) ) {
	    		document.forms[0].WINS_4.value = 0;
	    		return false;
	  	}
		if ((document.forms[0].WINS_1.value==255) &&
		    (document.forms[0].WINS_2.value==255) &&
		    (document.forms[0].WINS_3.value==255) &&
		    (document.forms[0].WINS_4.value==255)
		   )
		{
			alert(mavis_str(Alert_Msg_46));
			//alert("All 255 is invalid!");
			document.forms[0].WINS_1.focus();
			return 0;
		}  	
		if ((document.forms[0].WINS_4.value==0) ||
			(document.forms[0].WINS_4.value==255)
			)
		{
			alert(mavis_str(Alert_Msg_26));
			//alert("0 or 255 is invalid!");
			document.forms[0].WINS_4.focus();
			return 0;
		}
		d = document.forms[0].WINS_1.value + '.';
	  	d = d + document.forms[0].WINS_2.value + '.';
	  	d = d + document.forms[0].WINS_3.value + '.';
	  	d = d + document.forms[0].WINS_4.value;
	
		//document.forms[0].DHCP_DNS.value = d;
	}
	document.forms[0].lan_wins.value = d;
	return true;
}
function CheckRealDNS ()
{
	if (document.forms[0].real_dns.checked == true)
		return true;
	else
		return false;
}
function check_clientNumber(passForm)
{
	var netmask = parseInt(GetIPSegment (passForm.sel_netmask.value, 4));
	var net_number = 256 / (256 - netmask);
	var client = 255-netmask;
	var localip = parseInt(passForm.lan_ipaddr_4.value);
	var net_start = (netmask & localip) + 1;
	var net_end = (net_start + client) - 1;
	if (localip == (net_start-1)) {
		alert("Local IP can not apply with network IP " + localip);
		passForm.lan_ipaddr_4.focus();
		return false;
	}
	if (localip == (net_end)) {
		alert("Local IP can not apply with multicast IP " + localip);
		passForm.lan_ipaddr_4.focus();
		return false;
	}
	if (localip == (net_end+1)) {
		alert("Local IP can not apply with broadcast IP " + localip);
		passForm.lan_ipaddr_4.focus();
		return false;
	}
	if ((parseInt(passForm.StartIP.value) < net_start) || (parseInt(passForm.StartIP.value) > net_end)) {
		alert("Start IP not in the same network! " + net_start + "~" + (net_end-1));
		passForm.StartIP.focus();
		return false;
	}
	if ((parseInt(passForm.StartIP.value) + parseInt(passForm.NumOfUsers.value)) > net_end) {
		client = net_end - parseInt(passForm.StartIP.value);
		alert("Client IP Number can't large than " + client);
		passForm.NumOfUsers.focus();
		return false;
	}
	return true;
}
