/* 
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.
*/
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()
{	if (!confirm("Press OK to reset the device to the original factory settings. Press Cancel to do nothing."))
		return false;
	else
		return true;
}
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("?"));
}
function writeHeader(strTitle)
{	var strFilename = getPageFilename();
	document.write("<img id=\"idImgHeader\" src=\"banner.gif\">");
	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;");
		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) {
			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 USR5461 beta version is for evaluation purposes only. Normal support channels will not provide any support or information regarding this beta firmware version.</p>");
}
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>");
}
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;
}
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;
}
function normalizeMAC(mac)
{	mac = mac.toUpperCase();
	return mac.replace(/\W/g, ":");
}

function validateKeyForType(key, idType)
{	var eltType = document.getElementById(idType);
	if ((eltType == null) || !eltType.style || (eltType.style.display == null))
		return;
	var bits = 64;
	if ((eltType.selectedIndex == 0) || (eltType.selectedIndex == 1))
		bits = 128;
	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."); 
		return false;
	}
	return true;
}
function getKeyLength(bits, bHex)
{	var len = 0;
	if (bHex)
	{	switch (bits)
		{	case 64:	len = 10;	break;
			case 128:	len = 26;	break;
			case 256:	len = 58;	break;
			default:	len = 26;	break;
		}
	}
	else
	{	switch (bits)
		{	case 64:	len = 5;	break;
			case 128:	len = 13;	break;
			case 256:	len = 29;	break;
			default:	len = 13;	break;
		}
	}	
	return len;
}
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;
}
function copySSIDandMAC()
{	document.idForm.txtMACaddress.value = document.idForm.cbSurvey.options[document.idForm.cbSurvey.selectedIndex].value;
}
function is8021xselected(ix)
{	return ((ix >= 5) && (ix <= 7));
}
function isWPAselected(ix, change)
{	if (change == 0) {     //security.htm
		return ((ix >= 0) && (ix <= 2));
	}
	else {  //setup_sec.htm
		return ((ix == 0));
	}
}
function isWEPselected(ix, change)
{	if (change == 0) {     //security.htm
		return ((ix >= 3) && (ix <= 4));
	}
	else {  //setup_sec.htm
		return ((ix >= 1) && (ix <= 2));
	}
}
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));
	}
}
function setMaxLength(bClearValues, change)
{	var ixSecurity = document.idForm.cbAuthEncrypt.selectedIndex;
	if (isWEPselected(ixSecurity, change))
	{	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 = "";
			}
		}
	}
}
function onSecurityChange(bClearValues, change)
{	var ixSecurity = document.idForm.cbAuthEncrypt.selectedIndex;
	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);
	}
	var bWPA = isWPAselected(ixSecurity, change);
	setVisibility('idPassPhrase',	bWPA);
	if (change == 0)
	{	setVisibility('idKeyRotation',	bWPA);
		var b8021x = is8021xselected(ixSecurity);
		setVisibility('idRADIUSserver',	b8021x);
		setVisibility('idRADIUSport',	b8021x);
		setVisibility('idRADIUSkey',	b8021x);
		setVisibility('idRADIUSReauth',	b8021x);
		setVisibility('idRADIUSPreauth',b8021x && isWPA2selected(ixSecurity, change));
		setVisibility('idEncryption',	bWPA || b8021x);
	}
	setMaxLength(bClearValues, change);
}
function manageSupportedRates()
{  if (document.idForm.cbMode.selectedIndex == 2)
   {	document.idForm.cbSupportedRate.options.length = 7;
	document.idForm.cbMRate.options.length = 7;
   }
   else
   {  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");
      }
   }
}
function checksettings()
{	if (!document.idForm.chkWireless.checked)
		document.idForm.wl_radio.value="0";
	if (document.idForm.wlmode.checked)
		document.idForm.wl_mode.value="wds";
        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";
	} 
}
function checkDHCP()
{	if (!document.idForm.chkDHCP.checked)
		document.idForm.lan_proto.value="static";	
}
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;
	}
}
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);
}
function tableDisplay(objTable)
{	var inside = "";
	inside += "<input type=\"hidden\" name=\"" + objTable.strVarNameNum + "\" value=\"" + objTable.aRows.length + "\">";
	if (objTable.aRows.length > 0)
	{	inside += "<table class=\"clsRuled\">";
		inside += "<thead><tr valign=\"bottom\">";
		for (var col in objTable.aHeadings)
		{	inside += "<th align=\"left\">";
			inside += objTable.aHeadings[col];
			inside += "<\/th>";
		}
		inside += "<th><\/th>";	
		inside += "<\/tr><\/thead>";
		inside += "<tbody>";
		for (var row in objTable.aRows)
			inside += objTable.fctFormatRow(objTable, row);
		inside += "<\/tbody><\/table>";
	}
	objTable.eltTable.innerHTML = inside;
	objTable.eltBtnAdd.disabled = (objTable.aRows.length == objTable.iMaxNumRows);
}
function fctFormatRowText(objTable, row)
{	var strHTML = "<tr>";
	var aRow = objTable.aRows[row];
	for (var col in aRow)
	{	strHTML += "<td class=\"clsEntry\">";
		strHTML += aRow[col];
		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;
}
function tableAddRow(objTable, aRowNew)
{	objTable.aRows.push(aRowNew);
	tableDisplay(objTable);
	bDirty = true;
}
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;
}
function tableIsDuplicateRow(objTable, aRow)
{	for (var i in objTable.aRows)
	{	var bSame = true;
		for (var e in aRow)
		{	if (aRow[e] != objTable.aRows[i][e])
			{	bSame = false;
				break;
			}
		}
		if (bSame)
			return true;
	}
	return false;
}
function tableDeleteRow(objTable, ixRow)
{	for (var i = ixRow; i < objTable.aRows.length; ++i)
		objTable.aRows[i] = objTable.aRows[i + 1];
	--objTable.aRows.length;
	tableDisplay(objTable);
	bDirty = true;
}
function tableModifyValue(objTable, row, col)
{	objTable.aRows[row][col] = (objTable.aRows[row][col] == "on") ? "off" : "on";
	tableDisplay(objTable);
	bDirty = true;
}
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);
	}
}
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);
}
function isPrintable(strFieldName, s)
{	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];
	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;
}
function checklog()
{	if (!document.idForm.chkSystemLog.checked)
		document.idForm.log_ipaddr.value="";
}
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;
}
