/* 
Copyright 2004, U.S. Robotics Corporation
All Rights Reserved.

THIS SOFTWARE IS OFFERED "AS IS", AND U.S. Robotics GRANTS NO WARRANTIES OF ANY
KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. U.S. Robotics
SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
*/

/* --------------------------------------------------------------------------

	Device management functions

*/

/*
	This global variable and function prevent an IE user from accidentally
	leaving a page without saving their changes.
*/
var bDirty = false;

function verifyDeparture()
{
	if (!bDirty)
		return;

	event.returnValue = "You have made changes to this page. If you leave the page without saving them, your changes will be lost.";
}

function reloadDevice()
{
	/*
		Unfortunately, JavaScript doesn't offer a Yes/No message box.
	*/
	if (!confirm("Press OK to reset the device to the original factory settings. Press Cancel to do nothing."))
		return false;
	else
		return true;
}
/* --------------------------------------------------------------------------

   Page header and footer functions

*/

/*
   This function returns the filename of the current page.
   It looks for both slash and backward slash.
*/
function getPageFilename()
{
   var strFilename = document.URL;
   var ixSeparator = strFilename.lastIndexOf("/");
   var ixSeparator2 = strFilename.lastIndexOf("\\");
   if (ixSeparator2 > ixSeparator)
      ixSeparator = ixSeparator2;
   if (ixSeparator != -1)
      strFilename = strFilename.substr(ixSeparator + 1);
   return (strFilename.indexOf("?") == -1) ? strFilename : strFilename.substr(0, strFilename.indexOf("?"));
}

/*
	This writes out a page's header, including the optional menu bar.
	The title parameter is optional. If passed, it's written out
	as the header text.
*/
function writeHeader(strTitle)
{
	var strFilename = getPageFilename();		  // get filename of current page

	document.write("<img id=\"idImgHeader\" src=\"banner.gif\">");

	/*
		If the current page isn't in the list of pages, it doesn't get a menu.
	*/
	var bFound = false;
	var ix = -1;
	for (i = 0; i < menuMain.length; ++i)
	{
		if (menuMain[i].strFilename == strFilename)
		{
			bFound = true;
			break;
		}
	}
	if (!bFound)
	{
		document.write("<div id=\"idHeader\">");
		if (strTitle == null)
			document.write("&nbsp;");	// to make sure header is tall enough
		else
			document.write(strTitle);
		document.write("<\/div>");
	}
	else
	{
		document.write("<div id=\"idHeader\">" + menuMain[i].strTitle + "<\/div>");

		document.write("<div id=\"idMenu\">");
		for (var i = 0; i < menuMain.length; ++i)
		{
			// We don't output a newline after each because we use CSS to configure the gutter between tabs.
			// If we output a newline (or space), the rendering engine would add gutter.
			if (menuMain[i].strFilename == strFilename)
				document.write("<span class=\"clsMenuActive\">" + menuMain[i].strTitle + "<\/span>");
			else
				document.write("<a class=\"clsMenuInactive\" href=\"" + menuMain[i].strFilename + "\" title=\"" + menuMain[i].strDescription + "\">" + menuMain[i].strTitle + "<\/a>");
		}
		document.write("<\/div>");
	}

	document.write("<div id=\"idContent\">");
	//beta build only
	//document.write("<p class=\"clsNotice\">U.S. Robotics USR5451 beta version is for evaluation purposes only. Normal support channels will not provide any support or information regarding this beta firmware version.</p>");
}


/*
	This outputs the header for a Setup page.
	The passed number indicates which step this is and how many steps there are.

	<img id="idImgHeader" src="banner.gif">

	<h2 class="clsSteps">Setup
		<span class="clsStepCurrent">1</span>
		<span class="clsStepNotDone">&hellip; 2 &hellip; 3</span>
	</h2>

	<h2 class="clsSteps">Setup
		<span class="clsStepDone">1 &hellip;</span>
		<span class="clsStepCurrent">2</span>
		<span class="clsStepNotDone">&hellip; 3</span>
	</h2>


	<h2 class="clsSteps">Setup
		<span class="clsStepDone">1 &hellip; 2 &hellip;</span>
		<span class="clsStepCurrent">3</span>
	</h2>
*/
function writeHeaderSetup(iStep, iSteps)
{
	document.write("<img id=\"idImgHeader\" src=\"banner.gif\">");

	document.write("<div id=\"idSteps\">Setup ");

	for (var i = 1; i <= iSteps; ++i)
	{
		if (i == iStep)
			document.write("<span class=\"clsStepCurrent\">" + i + "<\/span> ");
		else
			document.write("<span class=\"clsStepNotDone\">" + i + "<\/span> ");

		if (i < iSteps)
			document.write("<span class=\"clsStepNotDone\">&hellip;<\/span> ");
	}
	document.write("<\/div>");

	document.write("<div id=\"idContent\">");
}

function writeCopyright()
{
	document.write("<\/div>");
	document.write("<p class=\"clsCopyright\">&copy; 2004-2006 <a href=\"http://www.usr.com\">U.S. Robotics Corporation<\/a>.<\/p>");
}

/* --------------------------------------------------------------------------

   General helper functions

*/

/*
	This function prompts the user to add a bookmark if the browser's not IE.
	Example:
		<script type="text/javascript">
			document.write('<a href="' + document.location.href + '" title="U.S. Robotics Router" rel="sidebar" onclick="bookmark(this); return false;">Bookmark the router</a>');
		</script>
*/
function bookmark(anchor)
{
	if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0))
       window.external.AddFavorite(anchor.getAttribute('href'), anchor.getAttribute('title'));
}

function toggleVisibility(id)
{
	var elt = document.getElementById(id);
	if ((elt == null) || !elt.style || (elt.style.display == null))
		return;

	if (elt.style.display == "none")
		elt.style.display = "";
	else
		elt.style.display = "none";
}


function setVisibility(id, state)
{
	var elt = document.getElementById(id);

	if ((elt == null) || !elt.style || (elt.style.display == null))
		return;

	if (state)
		elt.style.display = "";
	else
		elt.style.display = "none";
}


function setEnabled(id, state)
{
	var elt = document.getElementById(id);
	if ((elt == null) || !elt.style || (elt.style.display == null))
		return;

	elt.disabled = !state;
}

/*
	This function validates that the passed string is a valid format for a
	MAC address. The format is six hexadecimal bytes delimited by
	punctuation marks. The hex bytes can be lower or upper case.

	Valid examples:
		00-AA,BB;CC/DD:EE
		00.aa.bb.cc.dd.ee
*/
function validateMAC(mac)
{
	var re = new RegExp("^[\\dA-F]{2}([:\\.\\-,;/][\\dA-F]{2}){5}$", "i");
	var bOK = (mac.match(re) != null);
	if (!bOK)
		alert("Please enter a valid MAC address.\r\rA MAC address is a set of six bytes separated by colons, periods, dashes, commas, semicolons, or slashes. (A byte consists of two hexadecimal digits0-9 and A-F.) For example: \"00:C0:49:AA:22:43.\"");

	return bOK;
}


/*
	This function converts the MAC address byte delimiters to colons.
*/
function normalizeMAC(mac)
{
	mac = mac.toUpperCase();
	return mac.replace(/\W/g, ":");
}


/*
	This function determines if the passed key is the correct length
	for the passed key type (# of bits and ASCII/hex).
*/
function validateKeyForType(key, idType)
{
	var eltType = document.getElementById(idType);
	if ((eltType == null) || !eltType.style || (eltType.style.display == null))
		return;

	// determine # of bits
	var bits = 64;
	if ((eltType.selectedIndex == 0) || (eltType.selectedIndex == 1))
		bits = 128;

	// ASCII or hex key
	var bHex = true;
	if ((eltType.selectedIndex == 0) || (eltType.selectedIndex == 2))
		bHex = false;

	if (key.length != getKeyLength(bits, bHex))
		return alert("The length of the key is incorrect. A 128-bit key must be 13 characters long in ASCII or 26 characters long in hexadecimal. A 64-bit key must be 5 characters long in ASCII or 10 characters long in hexadecimal."); 
}

/*
				ASCII		Hex
	64-bits		 	 5		10
	128-bits		13		26
	256-bits		29		58
*/
function getKeyLength(bits, bHex)
{
	var len = 0;

	if (bHex)
	{
		// hex
		switch (bits)
		{
			case 64:	len = 10;	break;
			case 128:	len = 26;	break;
			case 256:	len = 58;	break;
			default:	len = 26;	break;
		}
	}
	else
	{
		// ASCII
		switch (bits)
		{
			case 64:	len = 5;	break;
			case 128:	len = 13;	break;
			case 256:	len = 29;	break;
			default:	len = 13;	break;
		}
	}	

	return len;
}

/*
	Functions to determine key type from the key length
*/

function isKeyASCII(strKey)
{
	var len = strKey.length;
	return ((len == 5) || (len == 13) || (len == 29));
}
function isKeyHex(strKey)
{
	var len = strKey.length;
	return ((len == 10) || (len == 26) || (len == 58));
}

function getKeySize(strKey)
{
	var len = strKey.length;
	if ((len == 5) || (len == 10))
		return 64;
	if ((len == 13) || (len == 26))
		return 128;
	if ((len == 29) || (len == 58))
		return 256;

	return 0;
}

/* --------------------------------------------------------------------------

	These functions are a quick way to implement the same things
	on more than one Web page (typically setup*.htm and something else).

*/

function copySSIDandMAC()
{
	// The selected AP's MAC address should be retrieved from the site survey data.
	document.idForm.txtMACaddress.value = document.idForm.cbSurvey.options[document.idForm.cbSurvey.selectedIndex].value;
}


/* --------------------------------------------------------------------------

	These functions are a quick way to implement the same things
	on more than one Web page (typically setup*.htm and something else).

*/

/*
	This is the mapping of the Method combobox to the 802.1x choices.
*/
function is8021xselected(ix)
{
	return ((ix >= 5) && (ix <= 7));
}

/*
	This is the mapping of the Method combobox to the WPA choices.
*/
function isWPAselected(ix, change)
{
	if (change == 0) {     //security.htm
		return ((ix >= 0) && (ix <= 2));
	}
	else {  //setup_sec.htm
		return ((ix == 0));
	}
}

/*
	This is the mapping of the Method combobox to the WEP choices.
*/
function isWEPselected(ix, change)
{
	if (change == 0) {     //security.htm
		return ((ix >= 3) && (ix <= 4));
	}
	else {  //setup_sec.htm
		return ((ix >= 1) && (ix <= 2));
	}
}

/*
	This is the mapping of the Method combobox to the WPA2 choices.
*/
function isWPA2selected(ix, change)
{
	if (change == 0)  {   //security.htm
		return (((ix >= 0) && (ix <= 1)) || ((ix >= 5) && (ix <= 6)));
	}
	else {   //setup_sec.htm
		return ((ix == 0));
	}
}

/*
	This is called when the user changes a sub-type of
	encryption (WEP ASCII 128-bit, WEP hex 64-bit, ...).
*/
function setMaxLength(bClearValues, change)
{
	var ixSecurity = document.idForm.cbAuthEncrypt.selectedIndex;
	
	// WEP
	if (isWEPselected(ixSecurity, change))
	{
		/*
			128-bit ASCII
			128-bit hex
			64-bit ASCII
			64-bit hex
		*/
		var bHex = (document.idForm.cbWEPkeytype[1].selected || document.idForm.cbWEPkeytype[3].selected);

		var iBits = 128;
		if (document.idForm.cbWEPkeytype[2].selected || document.idForm.cbWEPkeytype[3].selected)
			iBits = 64;

		var iMaxLength = getKeyLength(iBits, bHex);
		document.getElementById("idKeyNumChars").innerHTML = iMaxLength;
		
		document.idForm.wl_key1.maxLength = iMaxLength;
		if (document.idForm.wl_key2 != null)
		{
			document.idForm.wl_key2.maxLength = iMaxLength;
			document.idForm.wl_key3.maxLength = iMaxLength;
			document.idForm.wl_key4.maxLength = iMaxLength;
		}
		
		if (bClearValues)
		{
			document.idForm.wl_key1.value = "";
			if (document.idForm.wl_key2 != null)
			{
				document.idForm.wl_key2.value = "";
				document.idForm.wl_key3.value = "";
				document.idForm.wl_key4.value = "";
			}
		}
	}
}

/*
	This is called when the user changes the main type of
	encryption (none, WEP, WPA).
*/
function onSecurityChange(bClearValues, change)
{
	var ixSecurity = document.idForm.cbAuthEncrypt.selectedIndex;
	
	// if it's WEP, show the WEP properties
	var bWEP = isWEPselected(ixSecurity, change);
	setVisibility('idWEPkeytype',	bWEP);
	setVisibility('idKey1',		bWEP);
	if (change == 0)
	{
		setVisibility('idKey2',		bWEP);
		setVisibility('idKey3',		bWEP);
		setVisibility('idKey4',		bWEP);
		setVisibility('idKeyCurrent',	bWEP);
	}
	// if it's WPA, show WPA properties
	var bWPA = isWPAselected(ixSecurity, change);
	setVisibility('idPassPhrase',	bWPA);
	
	if (change == 0)
	{	setVisibility('idKeyRotation',	bWPA);
		// if it's 802.1x, show 802.1x properties
		var b8021x = is8021xselected(ixSecurity);
		setVisibility('idRADIUSserver',	b8021x);
		setVisibility('idRADIUSport',	b8021x);
		setVisibility('idRADIUSkey',	b8021x);
		setVisibility('idRADIUSReauth',	b8021x);
		// if it's WPA2 and 802.1x, show Pre-authentication
		setVisibility('idRADIUSPreauth',b8021x && isWPA2selected(ixSecurity, change));
		// if it's WPA-PSK or WPA/802.1x, show Encryption
		setVisibility('idEncryption',	bWPA || b8021x);
	}
	
	setMaxLength(bClearValues, change);
}

/*
   If the user selects "802.11b only," remove any supported rate
   over 11Mbps. If the user selects another setting, restore those
   rates.
*/
function manageSupportedRates()
{
   // if the user selected "802.11b only"
   if (document.idForm.cbMode.selectedIndex == 2)
   {
		document.idForm.cbSupportedRate.options.length = 7;
		document.idForm.cbMRate.options.length = 7;
   }
   else
   {
      // if there aren't enough rates, add them
      if (document.idForm.cbSupportedRate.options.length < 13)
      {
         document.idForm.cbSupportedRate.options[document.idForm.cbSupportedRate.options.length] = new Option("12 Mbps", "rate12");
         document.idForm.cbSupportedRate.options[document.idForm.cbSupportedRate.options.length] = new Option("18 Mbps", "rate18");
         document.idForm.cbSupportedRate.options[document.idForm.cbSupportedRate.options.length] = new Option("24 Mbps", "rate24");
         document.idForm.cbSupportedRate.options[document.idForm.cbSupportedRate.options.length] = new Option("36 Mbps", "rate36");
         document.idForm.cbSupportedRate.options[document.idForm.cbSupportedRate.options.length] = new Option("48 Mbps", "rate48");
         document.idForm.cbSupportedRate.options[document.idForm.cbSupportedRate.options.length] = new Option("54 Mbps", "rate54");
      }
      if (document.idForm.cbMRate.options.length < 13)
      {
         document.idForm.cbMRate.options[document.idForm.cbMRate.options.length] = new Option("12 Mbps", "rate12");
         document.idForm.cbMRate.options[document.idForm.cbMRate.options.length] = new Option("18 Mbps", "rate18");
         document.idForm.cbMRate.options[document.idForm.cbMRate.options.length] = new Option("24 Mbps", "rate24");
         document.idForm.cbMRate.options[document.idForm.cbMRate.options.length] = new Option("36 Mbps", "rate36");
         document.idForm.cbMRate.options[document.idForm.cbMRate.options.length] = new Option("48 Mbps", "rate48");
         document.idForm.cbMRate.options[document.idForm.cbMRate.options.length] = new Option("54 Mbps", "rate54");
      }
   }
}


/* check settings for the wireless page */
function checksettings()
{
	//if (document.idForm.chkWDSrestrict != null) {
		 if (!document.idForm.chkWDSrestrict.checked) 
			document.idForm.wl_lazywds.value="1";
	//}
		
	if (document.idForm.chkWME != null) {
		if (document.idForm.chkWME.checked) 
			document.idForm.wl_wme.value="on";
			
	/*	if (document.idForm.wl_wme_ap_bk5.checked)
			document.idForm.wl_wme_ap_bk5.value="on";
		if (document.idForm.wl_wme_ap_be5.checked)
			document.idForm.wl_wme_ap_be5.value="on";
		if (document.idForm.wl_wme_ap_vi5.checked)
			document.idForm.wl_wme_ap_vi5.value="on";
		if (document.idForm.wl_wme_ap_vo5.checked)
			document.idForm.wl_wme_ap_vo5.value="on";
		
		if (document.idForm.wl_wme_sta_bk5.checked)
			document.idForm.wl_wme_sta_bk5.value="on";	
		if (document.idForm.wl_wme_sta_be5.checked)
			document.idForm.wl_wme_sta_be5.value="on";
		if (document.idForm.wl_wme_sta_vi5.checked)
			document.idForm.wl_wme_sta_vi5.value="on";
		if (document.idForm.wl_wme_sta_vo5.checked)
			document.idForm.wl_wme_sta_vo5.value="on";
	*/
	}
	if (document.idForm.chkMcst != null) {
		if (document.idForm.chkMcst.checked) 
			document.idForm.wl_multicast.value="on";
	}
}

/* check settings for the LAN page */
function checkDHCP()
{
	if (!document.idForm.chkDHCP.checked)
		document.idForm.lan_proto.value="static";	
}

/* compute new lan_lease value for lan.htm*/
function new_lease(s)
{
	var day = document.idForm.txtLeaseDays.value;
	var hour = document.idForm.txtLeaseHours.value;
	var minutes = document.idForm.txtLeaseMinutes.value;
		
	var lan_lease = day*86400 + hour*3600 + minutes*60;
	if ((lan_lease == 0) || (lan_lease > 604800)) {
		alert("Please enter a lease time greater than zero and less than seven days.");
		if (s)
			return false;
	}
	else {
		document.idForm.lan_lease.value = lan_lease;
		return true;
	}
}

/* compute new wl_net_reauth value for security.htm*/
function new_reauth()
{
	var minutes = document.idForm.wl_net_reauth.value;
	var seconds = minutes*60;
	
	document.idForm.wl_net_reauth.value = seconds;
}

function inet_aton(a)
{
	var n;

	n = a.split(/\./);
	if (n.length != 4)
		return 0;

	return ((n[0] << 24) | (n[1] << 16) | (n[2] << 8) | n[3]);
}

function inet_ntoa(n)
{
	var a;

	a = (n >> 24) & 255;
	a += "."
	a += (n >> 16) & 255;
	a += "."
	a += (n >> 8) & 255;
	a += "."
	a += n & 255;

	return a;
}

function lan_ipaddr_change()
{
	var lan_netaddr, lan_netmask, dhcp_start, dhcp_end;

	lan_netaddr = inet_aton(document.idForm.lan_ipaddr.value);
	lan_netmask = inet_aton(document.idForm.lan_netmask.value);
	lan_netaddr &= lan_netmask;

	dhcp_start = inet_aton(document.idForm.dhcp_start.value);
	dhcp_start &= ~lan_netmask;
	dhcp_start |= lan_netaddr;
	dhcp_end = inet_aton(document.idForm.dhcp_end.value);
	dhcp_end &= ~lan_netmask;
	dhcp_end |= lan_netaddr;

	document.idForm.dhcp_start.value = inet_ntoa(dhcp_start);
	document.idForm.dhcp_end.value = inet_ntoa(dhcp_end);
}

/* --------------------------------------------------------------------------

	"List of entries" functions

	These functions use the passed "table object" to manipulate the entries
	in a device table.

*/

/*
	This function writes the table of entries represented by the passed object.
*/
function tableDisplay(objTable)
{
	/*
		Construct the table code
	*/
	var inside = "";

	// create hidden field with required form variable name for # of entries
	inside += "<input type=\"hidden\" name=\"" + objTable.strVarNameNum + "\" value=\"" + objTable.aRows.length + "\">";


	// if there are no rows, don't display the table at all
	if (objTable.aRows.length > 0)
	{
		inside += "<table class=\"clsRuled\">";

		/*
			Output the headings
		*/
		inside += "<thead><tr valign=\"bottom\">";
		// Add each column heading
		for (var col in objTable.aHeadings)
		{
			inside += "<th align=\"left\">";
			inside += objTable.aHeadings[col];
			inside += "<\/th>";
		}
		inside += "<th><\/th>";			// empty header cell for Delete button
		inside += "<\/tr><\/thead>";

		inside += "<tbody>";

		/*
			For each row of values, build that row.
		*/
		for (var row in objTable.aRows)
			inside += objTable.fctFormatRow(objTable, row);

		inside += "<\/tbody><\/table>";
	}

	objTable.eltTable.innerHTML = inside;

	// if we have the max # of rows in the table, disable Add button
	objTable.eltBtnAdd.disabled = (objTable.aRows.length == objTable.iMaxNumRows);
}


/*
	This function returns the HTML for the passed row of the table.
	It is the default "custom" formatting function, assuming that every
	column merely has text. Other, more complicated, tables can use
	their own custom formatting function to do more complicated things.

	Arguments:
			objTable 	table object
			row		index of row being formatted

	Returns:	HTML markup for this row
			<tr>...</tr>
*/
function fctFormatRowText(objTable, row)
{
	var strHTML = "<tr>";

	/*
		For each column in each of the column arrays, add to this row.
	*/
	var aRow = objTable.aRows[row];
	for (var col in aRow)
	{
		strHTML += "<td class=\"clsEntry\">";
		strHTML += aRow[col];

		// create hidden field with required form variable name
		strHTML += "<input type=\"hidden\" name=\"" + objTable.aVarNamePrefixes[col] + row + "\" value=\"" + aRow[col] + "\">";

		strHTML += "<\/td>";
	}
	strHTML += "<td>";
	strHTML += "<input type=\"button\" value=\"Delete\" class=\"clsSmall\" onclick=\"tableDeleteRow(" + objTable.strThis + ", " + row + ");\">";
	strHTML += "<\/td>";

	strHTML += "<\/tr>";

	return strHTML;
}


/*
	This function adds the passed row (array) to the table.
*/
function tableAddRow(objTable, aRowNew)
{
	objTable.aRows.push(aRowNew);

	tableDisplay(objTable);
	bDirty = true;
}


/*
	This function returns true if all values are not blank.
	It returns false if any value is blank.
	If a value is blank, it displays an error message specific to that column.

	This can be called by a table whose data are all text and require no
	special handling. It should be called before calling tableAddRow().
*/
function validateDataText(objTable, aRow)
{
	for (var col in aRow)
	{
		if (aRow[col] == "")
		{
			alert("Please enter a value for " + objTable.aHeadings[col] + ".");
			return false;
		}
	}

	return true;
}

/*
	This function returns true if the passed row is already in the passed
	table.
*/
function tableIsDuplicateRow(objTable, aRow)
{
	// compare passed row with each row in the table
	for (var i in objTable.aRows)
	{
		// compare each element in the passed row with each element in the current table row
		var bSame = true;
		for (var e in aRow)
		{
			if (aRow[e] != objTable.aRows[i][e])
			{
				bSame = false;
				break;
			}
		}
		if (bSame)
			return true;
	}
	return false;
}


/*
	This function deletes the specified row.
*/
function tableDeleteRow(objTable, ixRow)
{
	// BUG: This is a workaround for the above-mentioned IE bug.
	for (var i = ixRow; i < objTable.aRows.length; ++i)
		objTable.aRows[i] = objTable.aRows[i + 1];
	--objTable.aRows.length;

	tableDisplay(objTable);
	bDirty = true;
}


/*
	This function modifies a value in the specified column of the specified
	row. Right now, it only works for a checkbox, but it could be extended
	to work with any type of field.
*/
function tableModifyValue(objTable, row, col)
{
	objTable.aRows[row][col] = (objTable.aRows[row][col] == "on") ? "off" : "on";
	tableDisplay(objTable);
	bDirty = true;
}

/* if Acceleration is Afterburner - fragmentation field can't be changed */
function changeAcceleration()
{
	var ixAccel = document.idForm.cbAcceleration.selectedIndex;

	if (ixAccel == 2)
	{
		setEnabled("lblFrag", false);
		setEnabled("wl_frag", false);
		document.idForm.wl_frag.value = "2346";
	}
	else
	{
		setEnabled("lblFrag", true);
		setEnabled("wl_frag", true);
	}
}

/* setup encryption on the setup_sec.htm page */
function set_encryption()
{
	var ix = document.idForm.cbAuthEncrypt.selectedIndex;
	
	if (ix == 0)
		document.idForm.cbEncryption.value="tkip+aes";
	else	
		document.idForm.cbEncryption.value="none";
}

function validateUsername(s)
{
	if (s.length == 0)
	{
		alert("Please enter a user name.");
		return false;
	}

	return isPrintable("User name", s);
}

function validatePassword(s)
{
	if (s.length == 0)
	{
		alert("Please enter a password.");
		return false;
	}

	return isPrintable("Password", s);
}


/*
	This function determines if the passed string contains only printable characters.
	It returns true if the string is okay. It displays an error and returns false
	if the string contains an invalid character.

	95 legal characters:

	<space>!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
*/
function isPrintable(strFieldName, s)
{
	/*
		Note: We have to escape characters both for Javascript and for RegExp.
				Thus, " must be escaped ONLY for Javascript: \"
						\ and [ and ] must be escaped for RegExp and then the
						escape characters have to be escaped for Javascript:
						\\\\ and \\[ and \\]
	*/
//	var re = new RegExp("[^a-zA-Z0-9 !\"#$%&'()*+,\\-./:;<=>?\\\\@\\[\\]^_`{|}~]", "g");
	var re = new RegExp("[^vwxyz\\u0020-\\u007E\\u0080-\\u00FE]", "g");

	var arMatch = s.match(re);
	if (arMatch == null)
		return true;

	var strIllegalChars = "";
	for (var i = 0; i < arMatch.length; ++i)
		strIllegalChars += arMatch[i];

	// handle Kevin Willoughby Memorial Bug
//	var strMsg2 = "\n\nPlease use only a-z, A-Z, 0-9, space, or one of these: !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
	var strMsg2 = "\n\nPlease use only a-z, A-Z, 0-9, space, punctuation, or characters in the extended ASCII set, such as accented characters.";
	if (arMatch.length == 1)
		alert("The " + strFieldName + " entry contains an invalid character: " + strIllegalChars + strMsg2);
	else
		alert("The " + strFieldName + " entry contains invalid characters: " + strIllegalChars + strMsg2);

	return false;
}

/* check settings for the firewall settings in firewall.htm */
function checklog()
{
	if (!document.idForm.chkSystemLog.checked)
		document.idForm.log_ipaddr.value="";
}

/*
	This function validates that the passed string is a valid format for
	rotation key interval. The format is an integer number, up to 5 digits
*/
function validateKey(key)
{	
	var bOK = false;
	if (/^\d{1,5}$/.test(key))
		bOK = true;
	if (!bOK)
		alert("Please enter a valid Key rotation.\r\ The Key rotation value has to be a number between 0 and 99999");

	return bOK;
}
