//
// Copyright (c) Pawtucketville Citizens Council.  All rights reserved.
//

var G_ErrorFields = new Array();
var G_ErrorStrings = new Array();
var G_Dirty = false;
var g_CurrentPage = location.href.substring(location.href.lastIndexOf('/') + 1);

function Loc(Key)
{
	var p = new Function("return(T_"+Key+"_Message)");
	var f = new Function("return(\"undefined\" == typeof(T_"+Key+"_Message))");

	if (true == f()) {
		alert("Undefined String: " + Key +"!")
		return "UNDEFINED STRING ["+Key+"]";
	}
	else
		return p();
}

function Err(Key)
{
	var TestLoc = false;

	if (TestLoc)
	{
		return "ABC";
	}else{
		var p = new Function("return(L_"+Key+"_Error_1_Message)");
		var f = new Function("return(\"undefined\" == typeof(L_"+Key+"_Error_1_Message))");

		if (true == f()) {
			alert("Undefined String: " + Key +"!")
			return "UNDEFINED STRING ["+Key+"]";
		}
		else
			return p();
	}
}

function Menu(Key)
{
	var p = new Function("return(T_"+Key+"_Message)");
	var f = new Function("return(\"undefined\" == typeof(T_"+Key+"_Message))");

	if (true == f()) {
		alert("Undefined String: " + Key +"!")
		return "UNDEFINED STRING ["+Key+"]";
	}
	else
		return p();
}

var g_CurrentToolBarItem = "";

document.write("<link rel=stylesheet type=\"text/css\" href=\"style.css\">");

//
//Prepare varibles as they would be generated by the ISAPI
var g_fPasswordConfigured = true;
var g_fNetworkConfigured = true;
var g_fNASConfigured = true;	


function GlobalSubmitHandler(SubOn)
{
	G_Dirty = false;
}

function ToggleMenu(Item)
{	
	var o = FindObject(Item);
	
	if(o.style.display == "block") {
		o.style.display = "none";
	} else {
		o.style.display = "block";
	}
}


var g_CurrentMenus = new Array;



var g_NavID = 0;
function MakeTab(Title)
{
	var Item = "NAV_ITEM_" +g_NavID ;

	document.write("<TBODY id=\"TABLE_" +g_NavID  + "\">");
	document.write("<TR><TD>");	
	document.write("<div class=hand_cursor onClick=ToggleMenu(\""+Item+"\")>");
	document.write("<img src=\"images/pcc_menu_up.gif\">");
	document.write("<font class=toolbarMenuHeader>"+Title+"</font>");
	document.write("</div>");


	g_CurrentMenus[Item] = "hidden";
	document.write("<span class=hidden_menu id="+Item+">");
	document.write("</span>");
	
	document.write("</TD></TR>");
	document.write("</TBODY>");


	g_NavID  ++;
	return (g_NavID -1);
}

function GoTo(URL)
{
	try
	{
		document.location = URL;
	}
		
	catch(e)
	{
		// canceling the onbeforeunload dialog can cause the browswer to throw an "unspecified" error in document.location	
		// this protects the user from seeing it and getting confused.
		return; 
	}
}

function AddSelection(Title, URL)
{	
	document.write("<TR><TD onMouseOver=HighlightCell(this,true) onMouseOut=HighlightCell(this,false) onclick=GoTo(\""+URL+"\")>");
	document.write("<font class=toolbarHeader>"+Title+"</font>");
	document.write("</TD></TR>");
}


function AddSelectionToTab(Tab, Item, URL)
{
	var Color;
	if(URL == g_CurrentPage) {
		Color = "bgcolor=#33CC33";
	} else {
		Color = "onMouseOver=HighlightCell(this,true) onMouseOut=HighlightCell(this,false)";
	}

	var MyTab = FindObject("NAV_ITEM_"+Tab);
	var String = "<TABLE width=100%><TR><TD " + Color + " onclick=GoTo(\""+URL+"\")><font class=toolbarItem>"+Item+"</font></TD></TR></TABLE>";
	MyTab.innerHTML += String;

	if(URL == g_CurrentPage) {
		ToggleMenu("NAV_ITEM_"+Tab);
	}
}

function HighlightCell(o, enabled) 
{
	if(true == enabled) {
		o.bgColor = "#33CC33";
	} else {
		o.bgColor = "#00FF00";
	}
}

function AddMenuTitle(Description)
{
	document.write("<TR><TD><font class=toolbarHeader>"+Description+"</font></TD></TR>");
}

function AddBlankLine()
{
	document.write("<TR><TD><BR></TD></TR>");
}

function AddMenuItem(Enabled, Title, URL, Description)
{
	fIsClicked = false;	
	if(-1 != new String(window.location).search(URL)) {
		//fIsClicked = true;
	}

	if(!Description) {
		Description = Title;
	}
	
	if(false == fIsClicked) {		
		document.write("<TR><TD onMouseOver=HighlightCell(this,true) onMouseOut=HighlightCell(this,false)>");
		
	} else {		
		document.write("<TR><TD>");
		g_CurrentToolBarItem = Description;
	}
	if(null != URL) {
		if(false == Enabled) {
			document.write("<a href=" + URL + " class=toolbarItemNotComplete>" + Title + "</font></a>");
		} else {
			document.write("<a href=" + URL + " class=toolbarItem>" + Title + "</a>");
		}
	} else {
		if(false == Enabled) {
			document.write("<font color='YELLOW'>" + Title + "</font>");
		} else {
			document.write(Title);
		}
	}
	document.write("</TD></TR>");
}

function DisplayDirtyMsg()
{
	// onbeforeunload is an odd duck.  If you return ANYTHING (null / true / false / a string) it will turn it into 
	// a string and display it on it's cancel dialog.  If you want to abort the dialog from appearing don't "return" at all.
	
	if (G_Dirty)
		return Loc("PageDirty");
}

function BuildPage(Title, Description, MainFunction)
{
	document.write("<head>");
	document.write("<meta http-equiv='Content-Language' content='en-us'>");
	document.write("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
	document.write("<meta name='Author' content='Joe Mendonca'>");
	document.write("<title class=pageHeadText>" + Loc("SiteTitle") + "</title>");
	document.write("</head>");

	document.write("<body onbeforeunload='return(DisplayDirtyMsg())'>");
		BuildMainTable(Title, Description, MainFunction);
		AddErrorDisplay();
	document.write("</body>");
	ClearError();
}


function BuildMainTable(Title, Description, MainFunction)
{

	document.write("<table class=TitleBar>");
		document.write("<TR>");
			document.write("<TD width=249 valign=center>" + "<img src=\"images/PCCLogo.gif\"></TD>");
			document.write("<TD valign=center class=TitleText>" + Loc("SiteTitle") + " <FONT class=TitleSubText>" + Loc("SiteSubTitle") + "</FONT></TD>");
		EndTR();
	document.write("</table>");	

	document.write("<table class=mainPage>");
		AddTR();
			document.write("<TD valign=top class=toolBar>");
				Make_NavBar();
				document.write("<td height=100% align=left valign=top>");
					document.write("<table class=componentContainer>");
						document.write("<TR><TD class=pageTextHeading>" + Title + "</TD></TR>");
						document.write("<TR><TD>" + Description + "</TD></TR>");
						document.write("<TR><TD>");
						if("" != MainFunction) {
								var fn = new Function(MainFunction);
								document.write("<table class=subFrame><TR><TD>");			
								fn();
								document.write("</TR></TD></table>");
   						} 
					document.write("</table>");	
				document.write("</TD>");
		    document.write("</TD>");
		document.write("</TR>");
    document.write("</table>");	

	document.write("<table width=100% class=FooterBar>");
		AddTR();
			document.write("<TD >Copyright 2009 Pawtucketville Citizens Council - All rights reserved.</TD>");
		document.write("</TR>");
	document.write("</table>");	
}	

function BuildMainTable_NoToolBar(Title, Description, MainFunction)
{
	document.write("<head>");
	document.write("<meta http-equiv='Content-Language' content='en-us'>");
	document.write("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
	document.write("<title class=pageHeadText>" + Title + "</title>");
	document.write("</head>");

	document.write("<body>");
	
		document.write("<table class=TitleBar>");
			document.write("<TR>");
				document.write("<TD width=214 valign=center>" + "<img src=\"../remoteadminimages/images_OuterLinkLogo.gif\"></TD>");
				document.write("<TD valign=center class=TitleText>" + Loc("WindowsCETitle") + "</TD>");
			EndTR();
		document.write("</table>");	

		document.write("<table class=mainPage>");
			document.write("<TR valign=top><TD><table class=subPage>");

				document.write("<TR><TD class=pageTextHeading>" + Title + "</TD></TR>");
				document.write("<TR><TD>" + Description + "</TD></TR>");
			   
				if("" != MainFunction) {
					var fn = new Function(MainFunction);
					document.write("<TR><TD width=760 valign=top><table class=subFrame>");			
					fn();
					document.write("</table></TD></TR>");
				}
			document.write("</table></TD></TR>");	
		document.write("</table>");	

	document.write("</body>");
}	

function BuildMainTable_Empty(Title, Description, MainFunction)
{
	document.write("<head>");
	document.write("<meta http-equiv='Content-Language' content='en-us'>");
	document.write("<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>");
	document.write("<title class=pageHeadText>" + Title + "</title>");
	document.write("</head>");

	document.write("<body class=body_noHeader>");	

	document.write("<table class=subFrame>");
    document.write("<TR><TD>" + Description + "</TD></TR>");
   
    if("" != MainFunction) {
 		var fn = new Function(MainFunction);
 		document.write("<TR><TD><table class=subFrame>");			
 		fn();
 		document.write("</TR></TD></table>");
 	}

    document.write("</table>");	
	document.write("</body>");
}	


function FindObject(Name)
{
	var Obj = null;
	if("Microsoft Internet Explorer" == navigator.appName) {
		Obj = document.getElementById(Name);
	} else {
		var Sels = document.getElementsByTagName("*"); 

		for(var i=0; i<Sels.length; i++) {
			//document.write(Sels[i].id + "<BR>");
			if(Sels[i].name && Name.toUpperCase() == Sels[i].name.toUpperCase()) {				
				Obj = Sels[i];
				//alert("Found by name");
				break;
			} else if(Sels[i].id && Name.toUpperCase() == Sels[i].id.toUpperCase()) {				
				Obj = Sels[i];
				//alert("Found by id");
				break;
			}
			
		}
	}
		
	if (!Obj)
	{		
		alert("Unable to find object [" + Name + "]");
	}
	
		
	return Obj;
}

function GetObject(Name)
{
	var Obj = null;
	if("Microsoft Internet Explorer" == navigator.appName) {
		Obj = document.getElementById(Name);
	} else {
		var Sels = document.getElementsByTagName("*"); 

		for(var i=0; i<Sels.length; i++) {
			//document.write(Sels[i].id + "<BR>");
			if(Sels[i].name && Name.toUpperCase() == Sels[i].name.toUpperCase()) {				
				Obj = Sels[i];
				//alert("Found by name");
				break;
			} else if(Sels[i].id && Name.toUpperCase() == Sels[i].id.toUpperCase()) {				
				Obj = Sels[i];
				//alert("Found by id");
				break;
			}
			
		}
	}
		
	return Obj;
}

function FindSelectedRadio(Name)
{
	if(Name[0]) {
		for(var i=0; i<Name.length; i++) {
			if(true == Name[i].checked) {
				return i;
			}
		}
	} else if(true == Name.checked) {
		return 0;
	} else {
		return -1;
	}
}

function FindRadio(Name, Value) {
	if(Name[0]) {
		for(var i=0; i<Name.length; i++) {
			if(Value == Name[i].value) {
				return Name[i];
			}
		}
	} else if(Value == Name.checked) {
		return Name;
	} else {		
		return null;
	}
}

function FindAllObjectsById(Name)
{
    var Sels = document.getElementsByTagName("*");
    var Ret = new Array();
    j = 0;

    for(var i=0; i<Sels.length; i++) {
        if(Sels[i].id && 0 == String(Sels[i].id).indexOf(Name)) {
            Ret[j] = Sels[i];
            j++;
        }
    }
    return Ret;
}

function BuildBoxHeader(TableID)
{ 
	var box = "";
	
	if ("undefined" != typeof(TableID))
		TID = "ID='"+TableID+"' ";
	else
		TID = "";
	
	box += "<SPAN " + TID + ">";
	box +=  "<TABLE width=100% cellspacing=0 cellpadding=0 class=boxMain>";
	box += "<TR>";

	box +=     "<TD class=boxOutsideCorner height=5 width=5><img src=images/pcc_corner_UL.gif></TD>";
	box +=     "<TD class=boxMiddleFiller></TD>";
	box +=     "<TD class=boxOutsideCorner height=5 width=5><img src=images/pcc_corner_UR.gif></TD>";
	box += "</TR>";

	box += "<TR>";
	box += "<TD width=5></TD>";
	box += "<TD>";
	document.write(box);
}
function BuildBoxFooter()
{
	box = "</TD>";
	box += "<TD width=5></TD>";
	box += "</TD></TR>";

	box += "<TR>";
	box +=     "<TD class=boxOutsideCorner height=5 width=5><img src=images/pcc_corner_LL.gif></TD>";
	box +=     "<TD class=boxMiddleFiller></TD>";
	box +=     "<TD class=boxOutsideCorner height=5 width=5><img src=images/pcc_corner_LR.gif></TD>";
	box += "</TR>";
	box += "</TABLE><BR>";
	box += "</SPAN>";
	document.write(box);
}

function BuildTermHeader(TableID)
{ 
	var box = "";
	
	if ("undefined" != typeof(TableID))
		TID = "ID='"+TableID+"' ";
	else
		TID = "";
	
	box += "<SPAN " + TID + ">";
	box +=  "<TABLE width=100% cellspacing=0 cellpadding=0 class=termMain>";
	box += "<TR>";

	box +=     "<TD class=termOutsideCorner height=5 width=5><img src=images/pcc_corner_UL.gif></TD>";
	box +=     "<TD class=termMiddleFiller></TD>";
	box +=     "<TD class=termOutsideCorner height=5 width=5><img src=images/pcc_corner_UR.gif></TD>";
	box += "</TR>";

	box += "<TR>";
	box += "<TD width=5></TD>";
	box += "<TD>";
	document.write(box);
}
function BuildTermFooter()
{
	box = "</TD>";
	box += "<TD width=5></TD>";
	box += "</TD></TR>";

	box += "<TR>";
	box +=     "<TD class=termOutsideCorner height=5 width=5><img src=images/pcc_corner_LL.gif></TD>";
	box +=     "<TD class=termMiddleFiller></TD>";
	box +=     "<TD class=termOutsideCorner height=5 width=5><img src=images/pcc_corner_LR.gif></TD>";
	box += "</TR>";
	box += "<TABLE><BR>";
	box += "</SPAN>";
	document.write(box);
}

// Returns true if the field contains only numeric characters.
function isNumeric(field)
{
    if("" == field.value) {
    }
    return (field.value.match(/^\d*$/));
} 

function ClearError()
{
	// empty the set of errors
	for (key in G_ErrorStrings)
	{
		delete G_ErrorStrings[key];
	}
	G_ErrorStrings.length = 0;

	// clears the error display field
	var ErrorVar = FindObject("ValidationErrorString");
	if (null != ErrorVar)
	{
		ErrorVar.innerHTML = "";
	}
	
	// clears error marker from all fields
	for (key in G_ErrorFields)
	{
		NoError(G_ErrorFields[key]);
	}
	
	// empty the list of errors
	G_ErrorFields.length = 0; 
}

function ClearErrorBackground(Field)
{
	Field.className = "FieldNormal";
}

function FlagError(Field, ErrMsgID)
{	
	G_ErrorFields[G_ErrorFields.length] = Field;	
	Field.className = "FieldError";

	if (null == Field.onfocus)
	{
		// Note this will only work if the field does not already have a onfocus handler.
		// But that is ok if they do they can call ClearErrorBackground in it.
		// Also once they have the handler they will keep it until the object is distroyed.
		// This is transparent if there is no error so we won't worry about clearing the handler	
	
		Field.onfocus = new Function("ClearErrorBackground(this);");
	}
	
	ShowError(Field, ErrMsgID);
}

function NoError(Field)
{
	Field.className = "FieldNormal";

	var FieldIdx = Field.name;
	if('undefined' == typeof(Field.name) || "" == Field.name) {
		FieldIdx = Field.id;
	}
	

	delete G_ErrorStrings[FieldIdx];
	
	DisplayErrorHelper();
}


function DisplayErrorHelper()
{
	var ErrorString = "";
	var DisplayErrors = new Array();

	// consolidate errors 
	for (err in G_ErrorStrings) {
		DisplayErrors[G_ErrorStrings[err]] ++;		
	}

	for(err in DisplayErrors) {
		ErrorString += err;
		ErrorString += "</BR>";
	}
	
	var ErrorVar = FindObject("ValidationErrorString");
	if (null != ErrorVar)
	{
		ErrorVar.innerHTML = ErrorString;
	}
}


function ShowError(Field, String)
{
	// First add the error to our list of errors
	var FieldIdx = Field.name;
	if('undefined' == typeof(Field.name) || "" == Field.name) {
		FieldIdx = Field.id;
	}
		
	G_ErrorStrings[FieldIdx] = Err(String);

	DisplayErrorHelper();
}

function AddErrorDisplay()
{	
	document.write("<BR><DIV class=ErrorText ID=ValidationErrorString></DIV>");
}

function FindParentForm(obj)
{
	if (null == obj)
		return null;
		
	if ("FORM" == obj.tagName)
		return obj;

	return FindParentForm(obj.parentElement);
}

function FindFormReset(btn)
{
	ClearError();
	G_Dirty	= false;

	ParentForm = FindParentForm(btn);
	if (null != ParentForm)
	{
		ParentForm.reset();
	
		var btn = FindObject("Apply");
		if (null != btn)
			btn.disabled = true;

		btn = FindObject("Cancel");
		if (null != btn)
			btn.disabled = true;
	}
}

function AddApplyCancel(FormName)
{
	if ("undefined" == typeof(FormName))
	{
		document.write("<input type=submit id=Apply value=" + Loc('BTNApply') + " disabled>  <input id=Cancel type=button disabled value=" + Loc('BTNCancel') + " onclick=FindFormReset(this)>");
	} else{
		document.write("<input type=submit id=Apply value=" + Loc('BTNApply') + " disabled>  <input id=Cancel type=button disabled value=" + Loc('BTNCancel') + " onclick=ResetForm('" + FormName + "')>");
	}
}


function AddSection(Heading, Blurb)
{
	document.write("<TR><TD COLSPAN=2 class=cellLtColor>" + Heading + "</TD></TR>");
	document.write("<TR><TD COLSPAN=4>" + Blurb + "</TD></TR>");
}	

function AddBar()
{
	document.write("<TR><TD COLSPAN=5><HR></TD></TR>");
}

function Show(ID)
{
	var Obj = FindObject(ID);
	
	if (Obj)
	{
		Obj.style.display="block";
	}else{
		// FindObject will have shown an alert
		//alert("ERR: Show() unable to find object ["+ID+"]");
	}
}

function Hide(ID)
{
	var Obj = FindObject(ID);
	
	if (Obj)
	{		
		Obj.style.display="none";
	}else{
		// FindObject will have shown an alert
		//alert("ERR: Hide() unable to find object ["+ID+"]");
	}
}

function setSelection(OptionControlID, SelValue)
{
    var OptionControl = FindObject(OptionControlID);
    var selIndex = -1;
	
	if (null == OptionControl)
	{
		alert("ERR: invalid OptionControlID '"+OptionControlID+"' passed to setSelection");
		return;
	}

    for(var i=0; i < OptionControl.options.length; i++)
    {
        if(SelValue == OptionControl.options[i].value)
        {
            selIndex = i;
        }
    }
    
    if (-1 == selIndex)
    {
		alert("ERR: Unable to find selection value [" + SelValue + "] in list [" + OptionControlID + "]");
		selIndex = 0;
	}
	
    OptionControl.selectedIndex = selIndex;
}

function AddNumericOptions(Start, End, Selected)
{
	for (var iOpt = Start; iOpt <= End; iOpt++)
	{
		Select = (Selected == iOpt) ? " SELECTED" : " ";

		document.write("<OPTION value=" + iOpt + Select + ">" + iOpt + "</OPTION>");
	}
}

function enableApply()
{
	// NOTE: If you are getting an error about "object doesn't support this property or method" then
	// you have probably got the AddApplyCancel call after the </FORM>.  This has to be inside the form
	// to work properly.
	var Apply = FindObject("Apply");
	
	if (Apply) {
		G_Dirty = true;
		
		Apply.disabled = false;
	} else{
		alert("ERR: enableApply unable to find apply button");
	}
	
	var Cancel = FindObject("Cancel");

	if (Cancel) {
		Cancel.disabled = false;
	} else{
		alert("ERR: enableApply unable to find Cancel button");
	}
}

function disableApply()
{
	// NOTE: If you are getting an error about "object doesn't support this property or method" then
	// you have probably got the AddApplyCancel call after the </FORM>.  This has to be inside the form
	// to work properly.
	var Apply = FindObject("Apply");
	
	if (Apply) {
		G_Dirty = false;
		
		Apply.disabled = true;
	} else{
		alert("ERR: enableApply unable to find apply button");
	}
	
	var Cancel = FindObject("Cancel");

	if (Cancel) {
		Cancel.disabled = true;
	} else{
		alert("ERR: enableApply unable to find Cancel button");
	}
}

function AddOptionsFromArray(Array, Selected)
{
	if (!Array)
	{
		alert("ERR: AddOptionsFromArray passed bad array");
		return;
	}
		
	for(var iOpt = 0; iOpt < Array.length; iOpt++)
	{
		var iParts = new String(Array[iOpt]).split(":");
		
		Select = (iParts[0] == Selected) ? "Selected" : " ";
		
		document.write("<OPTION value=" + iParts[0] + " " + Select + ">" + iParts[1] + "</OPTION>");
	}
}	

function AddTD(Data)
{
	document.write("<TD>" + Data + "</TD>");
}

function AddAttrTR(Data)
{
	document.write("<TR "+Data+">");
}

function AddTR()
{
	document.write("<TR>");
}

function EndTR()
{
	document.write("</TR>");
}	

function AddShadow(ID, Val)
{
	var a = "<input type=hidden id=" + ID + " value='" + Val + "'>";
	document.write(a);
}

function AddAttrTD(Attribs, String)
{
	document.write("<TD " + Attribs + ">" + String + "</TD>");
}
	
function Add2ColTRTD(String1, String2)
{
	AddTR();
	document.write("<TD>" + String1 + "</TD><TD>" + String2 + "</TD>");
	EndTR();
}

function Add2ColTD(String1, String2)
{
	document.write("<TD>" + String1 + "</TD><TD>" + String2 + "</TD>");
}

function AddTRTD(String)
{
	AddTR();
		AddTD(String);
	EndTR();
}

function ResetForm(FormName)
{
	ClearError();
	
	var Form = FindObject(FormName);
	Form.reset();

	var btn = FindObject("Apply");
	btn.disabled = true;

	btn = FindObject("Cancel");
	btn.disabled = true;
	
	G_Dirty = false;
	
}

function ValidateNoneEmpty(Field, ErrMsgID, SetFocus)
{
	var valid = true;
	
	if (Field.value.length < 1)
	{
		valid = false;
		FlagError(Field, ErrMsgID);
		if (SetFocus)
		{
			Field.select();
			Field.focus();
		}
	}else{
		NoError(Field);
	}
	
	return valid;
}

function ValidatePortEmptyOK(Field, SetFocus)
{
	var valid = true;
	
    if(!isNumeric(Field))
    {
		FlagError(Field, "PortNonNumeric");
		valid = false;
    }

	// Check the range (ports must be within 0 to 65535, and the range must be nondecreasing).
	if(parseInt(Field.value) < 0 || parseInt(Field.value) > 65535)
	{
		FlagError(Field, "PortBadRange");
		valid = false;
	}

	if (valid)
	{
		NoError(Field);
	}else{
		if (SetFocus)
		{
			Field.select();
			Field.focus();
		}
	}
	
	return valid;
}

function ValidatePort(Field, SetFocus)
{
	var valid = true;
	
    valid &= ValidatePortEmptyOK(Field, SetFocus);

	if (!Field.value.length)
	{
		FlagError(Field, "PortEmpty");
		valid = false;
    }
    
   	if (valid)
	{
		NoError(Field);
	}else{
		if (SetFocus)
		{
			Field.select();
			Field.focus();
		}
	}
	
    return valid;
}

function ValidatePW(PW, MinLen, MaxLen)
{
	var valid = true;
	
	if (PW.value.length < MinLen || PW.value.length > MaxLen)
	{
		FlagError(PW, "BadPW");
		valid = false;
	}else{
		NoError(PW);
	}
	
	return valid;
}

function FocusFirstError()
{
	if (G_ErrorFields.length > 0)
	{
		Field = G_ErrorFields[0];			
		Field.select();
		Field.focus();		
	}		
}

var _Tab		= 9;
var _BackSpace	= 8;
var _Del		= 46;
var _CR			= 13;
var _Left		= 37;
var _Right		= 39;
var _Home		= 36;
var _End		= 35;

function HandleKey(AllowedSet)
{
	if (navigator.appName == 'Netscape') {
	    return;
	}

	var KeyCode = event.keyCode;
	var Char = String.fromCharCode(KeyCode);
	
	event.returnValue = false;

	// special chars that are always allowed
	if (KeyCode == _BackSpace	||		
		KeyCode == _CR			||
		KeyCode == _Left		||
		KeyCode == _Right		||
		KeyCode == _Home		||
		KeyCode == _End			||
		KeyCode == _Tab)
	{
		event.returnValue = true;
		return;
	}
	
	var pos = AllowedSet.indexOf(Char);

	if (AllowedSet.indexOf(Char) != -1)
	{
		event.returnValue = true;
	}else{
		event.returnValue = false;
	}
}


function NumOnly()
{
	return HandleKey("0123456789");
}

function HexOnly()
{
	return HandleKey("0123456789abcdefABCDEF");
}

function NumRangesOnly()
{
	return HandleKey("0123456789,- ");
}
	
function AddSelectionRadio(RadioSetID, Text, Value, Checked, Callback)
{
	var selected = "";
	var CB = "";

	if (Checked == Value)
	{		
		selected = "checked";
	}
	
	if ("undefined" != typeof(Callback))
	{
		CB = "onclick='" + Callback + "(this);'";
	}
		
	document.write("<TR><TD><input type=radio value='"+Value+"' name='"+RadioSetID+"' + "+selected+" "+CB+" >" + Text + "</TD></TR>");
}



function AddDOWOptions(Selected)
{
	var DaysOfWeek = new Array(Loc("Sun"), Loc("Mon"), Loc("Tue"), Loc("Wed"), Loc("Thr"), Loc("Fri"), Loc("Sat"));

	for (var iDay = 0; iDay < DaysOfWeek.length; iDay++)
	{
		Select = (Selected == iDay) ? " SELECTED" : " ";

		document.write("<OPTION value=" + iDay + Select + ">" + DaysOfWeek[iDay] + "</OPTION>");
	}
}

function FormatHour(Hour, TwentyFourHourFormat)
{
	if (TwentyFourHourFormat)
	{
		// easy
		Format = Hour + ":00";
	}else{
		var pm = (Hour > 11);
		Hour = (Hour % 12);
		
		if (0 == Hour)
			Hour = 12;
		
		Format =  Hour + ":00 ";
		if (pm)
		{
			Format += Loc("PM");
		}else{
			Format += Loc("AM");
		}
	}
	
	return Format;
}


function AddAMPMHourOptions()
{
	var hour = 0;
	var i = 0;
	for (i=0; i < 24; i++)
	{
		hour = (i % 12);
		
		if (0 == hour)
			hour = 12;

		document.write("<OPTION value=" + i + ">" + hour + ":00 ");

		if (i < 12)
		{
			document.write(Loc('AM'));
		}else{
			document.write(Loc('PM'));
		}
		document.write("</OPTION>");
	}
}
	

function ExtractDayStart(MaskedDays)
{
	// Incoming bit mask Sunday == 0x1, Monday = 0x2, Tuesday = 0x4 etc.;
	// When returning the DOW Sun = 0; mon = 1; tue=2; Wed = 3, ...
	var Gaped = false;
	var iStart = 0;

	// Need to check and see if this mask is wraping the weekend
	if ((MaskedDays & 1) && MaskedDays & (1 << 6))
	{
		// Sun & Sat both set.  If there are any gaps then we are wrapping

		iStart = 6;	// (Sat)
		for (var iShift = 5; iShift > 0; iShift--)
		{
			if ((1 << iShift) & MaskedDays)
			{
				iStart = iShift;
			}else{
				Gaped = true;
				break;
			}
		}
	}
	
	if (!Gaped)
	{
		// Isn't crossing the weekend

		for (var iShift = 0; iShift < 7; iShift++)
		{
			if ((1 << iShift) & MaskedDays)
			{
				iStart = iShift;
				break;
			}
		}
	}
	return iStart;
}

function ExtractDayEnd(MaskedDays)
{
	// Incoming bit mask Sunday == 0x1, Monday = 0x2, Tuesday = 0x4 etc.;
	// When returning the DOW Sun = 0; mon = 1; tue=2; Wed = 3, ...
	var Gaped = false;
	var iEnd = 0;

	// Need to check and see if this mask is wraping the weekend
	if ((MaskedDays & 1) && MaskedDays & (1 << 6))
	{
		// Sun & Sat both set.  If there are any gaps then we are wrapping

		iEnd = 0;	// (Sun)
		for (var iShift = 1; iShift <6; iShift++)
		{
			if ((1 << iShift) & MaskedDays)
			{
				iEnd = iShift;
			}else{
				Gaped = true;
				break;
			}
		}
	}
	if (!Gaped)
	{
		// Isn't crossing the weekend
		for (var iShift=7; iShift > 0; iShift--)
		{
			if ((1 << iShift) & MaskedDays)
			{
				iEnd = iShift;
				break;
			}
		}
	}
	return iEnd;
}

function BuildDayMask(Start, End)
{
	var DayMask = 0;
	if (Start <= End)
	{
		// Straight range
		for (var i = Start; i <= End; i++)
			DayMask |= (1 << i);
	}else{
		// We cross the weekend, need to do this in two parts
		for (var i = 0; i <= End; i++)
			DayMask |= (1 << i);
			
		for (var i = Start; i <= 6; i++)
			DayMask |= (1 << i);
	}
	
	return DayMask;
}
			
			
		
		
		
	
	



	
function SetRadioSelect(RadioControl, SelValue)
{
    var selIndex = -1;
	
	if (null == RadioControl)
	{
		alert("ERR: invalid Radio object passed to SetRadioSelect");
		return;
	}
    for(var i=0; i < RadioControl.length; i++)
    {
        if(SelValue == RadioControl[i].value)
        {
            RadioControl[i].checked = true;
            selIndex = i;
            break;
        }
    }
    
    if (-1 == selIndex)
    {
		alert("ERR: Unable to find radio button value [" + SelValue + "] in set [" + RadioControl.name + "]");
		selIndex = 0;
	}
}

function BuildFilterString(Key)
{
	var DaysOfWeek = new Array(Loc("Sun"), Loc("Mon"), Loc("Tue"), Loc("Wed"), Loc("Thr"), Loc("Fri"), Loc("Sat"));

	var Filter = Loc("Always");

	if (l_ClientList[Key][_DayStart] || l_ClientList[Key][_DayEnd] || l_ClientList[Key][_HourStart] || l_ClientList[Key][_HourEnd])
	{
		Filter = DaysOfWeek[l_ClientList[Key][_DayStart]] + "-" +
				 DaysOfWeek[l_ClientList[Key][_DayEnd]] + " " +
				 FormatHour(l_ClientList[Key][_HourStart], false) + "-" +
				 FormatHour(l_ClientList[Key][_HourEnd], false);
	}

	return Filter;
}

function SetCookie(sName, sValue)
{
	// create a session based cookie.  Will last until they close IE
	document.cookie = sName + "=" + escape(sValue);
}

function GetCookie(sName)
{
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++)
	{
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
			return unescape(aCrumb[1]);
	}

  return null;
}

function AddSubmitItem(FormVar, ItemName, ItemValue)
{
	// Some browswers don't support a fully defined element in the CreateElement call.
	var NewElement = document.createElement("input");

	NewElement.setAttribute("type", "hidden");
	NewElement.setAttribute("name", ItemName);
	NewElement.setAttribute("value", ItemValue);

	FormVar.appendChild(NewElement);
}

function GetRadioValue(RS)
{
	for (var i = 0; "undefined" != typeof(RS[i]) ;i++)
	{
		if (RS[i].checked)
			return RS[i].value;
	}
	
	return "0";
}

