function loadhelp(fname,anchname)
{
	if(!(top.helpframe))
		return;
	if (loadhelp.arguments.length == 1 || anchname == "")
		top.helpframe.location.href = "h" + fname + ".htm";
	else
		top.helpframe.location.href = "h" + fname + ".htm#" + anchname;
	return;
}

function select_getv(ctrl_list)
{
	for (var cnt = 0; cnt < ctrl_list.length; cnt++)
		if (ctrl_list.options[cnt].selected)
			return Number(ctrl_list.options[cnt].value);
	return -1;
}

function select_setv(ctrl_list, value)
{
	for (var cnt = 0; cnt < ctrl_list.length; cnt++)
		if (ctrl_list.options[cnt].value == value)
		{
			ctrl_list.options[cnt].selected = true;
			return 0;
		}
	ctrl_list.options[0].selected = true;
	return -1;
}

function radio_getv(ctrl_radio)
{
	for (var cnt = 0; cnt < ctrl_radio.length; cnt++)
		if (ctrl_radio[cnt].checked && !ctrl_radio[cnt].disabled)
			return Number(ctrl_radio[cnt].value);
	return -1;
}

function getIndex(e)
{
	var f = e.form.elements;
	for (var i = 0; i < f.length; i++)
		if (e == f[i])
			return i;
	return -1;
}

var posit = 0;
function calpos(e, len)
{
	e.select();
	if (posit > len)
		posit -= len;
	else
		posit = 0;
}

function GetKeyCode(event)
{
	if (event.keyCode != "undefined")
		return event.keyCode;
	else if (event.charCode != "undefined")
		return event.charCode;
	else
		alert("Can't get keyCode!");
	return 0;
}

function SetKeyCode(event, value)
{
	if (event.keyCode != "undefined")
		event.keyCode	= Number(value);
	else if (event.charCode != "undefined")
		event.charCode	= Number(value);
	else
		alert("Can't get keyCode!");
}

// Only allow certain inputs (those valid for MAC addresses)
// and automatically tab to the next field when current field is full.
function MacAddrEdit(event, obj)
{
	var KeyValue = GetKeyCode(event);
	if (KeyValue == 0x08)		// BackSpace
	{
		if (posit > 0)
			posit--;
		return;
	}
	if (KeyValue == 0x09 ||		// Tab
		KeyValue == 0x2e		// Delete
	)
		return;
		
	// Don't allow any keys outside of 0-9 and a-f or A-F     --dp
	if ((KeyValue < 0x30 || KeyValue > 0x39) &&
		(KeyValue < 0x60 || KeyValue > 0x69) &&	// numbers
		(KeyValue < 0x41 || KeyValue > 0x46)	// 'a' to 'f' or 'A' to 'F'
	)
	{
		event.returnValue = false;
		return;
	}
	
	
	// Before changing focus, we need to update the other synced fields.
	if (obj == document.formWDS.p2p_mac)
	{
		//document.formWDS.pxp_mac1.value = document.formWDS.p2p_mac.value;
	}
	
	// Posit is supposed to track your current position in the current field.
	// 0 = field blank,  1 = one char in,   2 = two chars in
	// If we are on the 2nd position, current text field is full, so it means we should jump to next field is possible... lets check
	if (posit == 2)
	{
		var myfield = obj.form.elements[getIndex(obj)+1];
		// If the next field can be tabbed to (can take focus()), then go there.
		if (!(myfield.style.visibility=="hidden" || myfield.style.display=="none" || myfield.disabled==true)) 
		{
			myfield.focus();		// Move to next field
			posit++;	// Only increment position if we can actually moved to next field
		}
	}
	else
		posit++;
		
	//alert ("MacAddrEdit:  Value is: " + obj.value);
}

function IpAddrEdit(event, obj)
{
	var KeyValue = GetKeyCode(event);
	if (KeyValue == 0x6e || KeyValue == 0xbe)	// "." key
	{
		if (obj.value.length > 0)
			obj.form.elements[getIndex(obj)+1].focus();
		SetKeyCode(event, 0x08);
		return;
	}
	if (KeyValue == 0x08)						// BackSpace
	{
		if (posit > 0)
			posit--;
		return;
	}
	if (KeyValue == 0x09 ||						// Tab
		KeyValue == 0x2e						// Delete
	)
		return;
	if ((KeyValue < 0x30 || KeyValue > 0x39) &&
		(KeyValue < 0x60 || KeyValue > 0x69))	// numbers
	{
		event.returnValue = false;
		return;
	}
	if (posit++ == 3)
		obj.form.elements[getIndex(obj)+1].focus();
}

function be_gray(v)
{
	if (typeof v.disabled != 'undefined')
	{
		v.style.color = "gray";
		v.disabled = 1;
	}
	else
	{
		v.oldonfocus = v.onfocus;
		v.onfocus = function()
		{
			this.blur();
		};
	}
}

function be_normal(v)
{
	if (typeof v.disabled != 'undefined')
	{
		v.style.color = document.fgColor;
		v.disabled = 0;
	}
	else
		v.onfocus = v.oldonfocus;
}

function goAround()
{
	location.reload();
}

function fill_options(options_array)
{
	//options_array[cnt]="Description|tri-string|value";
	var i = 0;
	var options = new Array();
	var css_optionstr = "<option value=$&val $&tri>$&des</option>";
	document.write (options_array);
	for (i = 0; i < options_array.length; i++)
	{
		options = options_array[i].split("|");
		if (options[2]!="TBD")
		{
			var output_optstr = css_optionstr.replace("$&des",options[0]);
			output_optstr = output_optstr.replace("$&tri",options[1]);
			output_optstr = output_optstr.replace("$&val",options[2]);
			document.write (output_optstr);
		}
	}
}

function GetElementIndex(theElement)
{
	var theElements = theElement.form.elements;
	var	i=0;
	for (i=0; i<theElements.length; i++)
	{
		if (theElements[i]==theElement)
		{
			return i;
		}
	}
	return -1;
}

function UpdateDisabledProperty(theElement, iCount, fIsDisabled)
{
	var theElements = theElement.form.elements;
	var	iIndex = GetElementIndex(theElement);
	while (iCount>0)
	{
		if (fIsDisabled)
		{
			be_gray(theElements[iIndex++]);
		}
		else
		{
			be_normal(theElements[iIndex++]);
		}
		iCount--;
	}
}

function Mac2Str(theInput)
{
	var	i=0;
	var	iInputIndex = GetElementIndex(theInput);
	var theElements = theInput.form.elements;	
	
	var szMacAddress = theInput.value;
	for (i=1; i<6; i++)
	{
		szMacAddress += ':' + theElements[i+iInputIndex].value;
	}
	if (':::::'==szMacAddress || '00:00:00:00:00:00'==szMacAddress)
	{
		szMacAddress = '';
	}
	return szMacAddress;
}

function MacFill(theInput, szMacAddress)
{
	var i = 0;
	var	iInputIndex = GetElementIndex(theInput);
	var theElements = theInput.form.elements;

	for (i=0;i<6;i++)
	{
		theElements[i+iInputIndex].value = szMacAddress.substr(i*3,2);
	}
}

var	szHexChars = '0123456789abcdefABCDEF';
function IsHexChar(c)
{
	return -1!=szHexChars.indexOf(c);
}

function IsValidMacAddress(szMacAddress)
{
	for (i=0;i<szMacAddress.length; i++)
	{
		if (0==((i+1)%3))
		{
			if (':'==szMacAddress.charAt(i))
			{
				continue;
			}
		}
		else if (IsHexChar(szMacAddress.charAt(i)))
		{
			continue;
		}
		return 'Part ' + (i+3-i%3)/3 +' of MAC address is invalid!';
	}

	if (17!=szMacAddress.length)
	{
		return 'Incomplete MAC address!';
	}

	if (0!=(szHexChars.indexOf(szMacAddress.charAt(1))%2))
	{
		return 'The first byte of MAC address must be even!';
	}
	
	return '';
}
