var w_sFunctionName = '';
var w_bProcessing = false;
//var w_xhttpLogin = new ActiveXObject("Microsoft.XMLHTTP");

/*
*/
function buildOptions(sNames, sValues, objSelect, bSingleEmpty)
{
  var myUndef;
  if(bSingleEmpty == myUndef)
  {
	bSingleEmpty = true;
  }

  var nOptionCount = objSelect.length;
  for(var i=0; i < nOptionCount; i++)
  {
 	objSelect.options.remove(0);
  }
		
  var aNames = sNames.split('~');
  var aValues = sValues.split('~');
  var nLength = aNames.length;
  if(nLength != aValues.length)
  {
	alert('Error: buildOptions - sNames and sValues length are not the same.');
	return;
  }
  nLength--;  // Hanging '~' makes length 1 too long
	
  var objElem;
  if(bSingleEmpty)
  {
	objElem = document.createElement('OPTION');
	objSelect.options.add(objElem);
  }
		
  for(var i=0; i < nLength; i++)
  {
	objElem = document.createElement('OPTION');
	objElem.text = aNames[i];
	objElem.value = aValues[i];
	objSelect.options.add(objElem);
  }
}

/* 
*  Allow a press of the Enter key to submit the form
*  Add onKeyPress="return checkForEnter(event);" to form inputs
*  Requires page to have submitForm() function
*/
function checkForEnter(e)
{
  var intKeyCode;
	
  if( window.event ) intKeyCode = window.event.keyCode;
  else if( e ) intKeyCode = e.which;
  else return true;

  if(intKeyCode == 13)
  {
	if( submitForm() )
	{
 		CheckLogin('document.forms[0].submit()');
	}
	return false;
  }
  else
  {
	return true;
  }
}

// Determine if the user is logged in
function CheckLogin(sFunctionName)
{
  w_sFunctionName = sFunctionName;
  w_bProcessing = false;
  w_xhttpLogin.Open ("POST", "wsIsLoggedIn.asp");
  w_xhttpLogin.onReadyStateChange = CheckLoginComplete;
  w_xhttpLogin.Send ("<ROOT/>");
}

// Complete the asynchronous CheckLogin
function CheckLoginComplete()
{
  if((w_xhttpLogin.readyState == 4) && !w_bProcessing)
  {
  	w_bProcessing = true;
	var sReturn = w_xhttpLogin.responseText;
	if(sReturn == 'TRUE')
 	{
		eval(w_sFunctionName);
	}
	else
	{
		StartLogin();
	}
  }
}

// Display the given error message
function displayError(sError)
{
  alert(sError);
}

// Format number as a string in $xxx,xxx format
function formatCurrency(anynum) 
{
   var dNum;		// Decimal number
   var strDec;		// Decimal string

   dNum=Math.round(anynum);
   strDec=dNum+'';

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=strDec.length;
      strDec=parseInt(""+(dNum/1000))+","+strDec.substring(dLen-3,dLen);
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=strDec.length;
      strDec=parseInt(""+(dNum/1000000))+","+strDec.substring(dLen-7,dLen);
   }
   retval = strDec;
   //-- Put numbers in parentheses if negative.
   if (anynum<0) {retval="("+retval+")"}
   return "$"+retval;
}

// Format number as a string in xxx,xxx.xx format
function formatDecimal(anynum) 
{
   var nWork;		// Work number
   var strDec;		// Decimal string
   var strPeriod;	// Period string
   var strWork;		// Work string
   anynum=eval(anynum);
   nWork=Math.abs((Math.round(anynum*100)/100));
   strWork = "" + nWork;
   if (strWork.indexOf(".")==-1)
   { 
	strWork+=".00" 
   }
   strDec=strWork.substr(0,strWork.indexOf("."));
   dNum=strDec-0;
   strPeriod=strWork.substr(strWork.indexOf("."));
   while (strPeriod.length<3)
   {
	strPeriod+="0"
   }

   //--- Adds comma in thousands place.
   if (dNum>=1000) {
      dLen=strDec.length
      strDec=parseInt(""+(dNum/1000))+","+strDec.substring(dLen-3,dLen)
   }

   //-- Adds comma in millions place.
   if (dNum>=1000000) {
      dLen=strDec.length
      strDec=parseInt(""+(dNum/1000000))+","+strDec.substring(dLen-7,dLen)
   }
   retval = strDec + strPeriod 

   return retval
}

// Return the value of the selected checkbox 
function getCheckboxValue(controlArray)
{
  var intLoop;	// Loop control

  for( intLoop = 0; intLoop < controlArray.length; intLoop++ )
  {
	if( controlArray[intLoop].checked == true )
	{
		return controlArray[intLoop].value;
	}
  }

  return "";
}

// Return trimmed value or NULL if empty
function getNullableTextValue(strValue)
{
  strValue = replaceDblQuotes(strValue);
  if( trim(strValue).length > 0 )
  {
	return trim(strValue);
  }
  else
  {
	return "NULL";
  }
}

// The the value of the selected radio button in the control array
function getRadioValue(controlArray)
{
  var intLoop;	// Loop control

  for( intLoop = 0; intLoop < controlArray.length; intLoop++ )
  {
	if( controlArray[intLoop].checked == true )
	{
		return controlArray[intLoop].value;
	}
  }

  return "";
}

// Get the selected option
function getSelectValue(dropDown)
{
  if(dropDown.selectedIndex == -1)
  {
	// Nothing Selected
	return '';
  }
  else
  {
	return dropDown.options[dropDown.selectedIndex].value;
  }
}

// Set the visibility property to hide the form contained
// in the given span or div reference
function hideSpan(strFormWrapper)
{
  if( document.layers && document.layers[strFormWrapper] != null )
  {	// NN4
	document.layers[strFormWrapper].visibility = 'hide';
  }
  else if( document.all && !document.getElementById )
  {	// IE4
	document.all[strFormWrapper].style.visibility = 'hidden';
  }
  else
  {	// IE5+, NN6
	document.getElementById(strFormWrapper).style.visibility = 'hidden';
  }
}

// Set the visibility property to hide the form contained
// in the given span or div reference
function clearSpan(strFormWrapper)
{
  if( document.layers && document.layers[strFormWrapper] != null )
  {	// NN4
	document.layers[strFormWrapper].innerHTML = '';
  }
  else if( document.all && !document.getElementById )
  {	// IE4
	document.all[strFormWrapper].innerHTML = '';
  }
  else
  {	// IE5+, NN6
	document.getElementById(strFormWrapper).innerHTML = '';
  }
}

// Log the user out
function Logout()
{
  w_bProcessing = false;
  w_xhttpLogin.Open ("POST", "wsLogout.asp");
  w_xhttpLogin.onReadyStateChange = LogoutComplete;
  w_xhttpLogin.Send ("<ROOT/>");	
}

function LogoutComplete()
{
  if((w_xhttpLogin.readyState == 4) && !w_bProcessing)
  {
	w_bProcessing = true;
	alert('Logged out');
  }
}

// Open a new window and display the given URL
function openNewWindow(url)
{
  window.open(url, "", "height=500, width=800, toolbar=no, resizable=no, status=no, titlebar=no, menubar=no, scrollbars=yes", true);
}

// Replace double quotes with a single quote
function replaceDblQuotes(strInput)
{
  return strInput.replace(/"/g,'\'');
}

// Replace XML special characters w/ their delimited value
function replaceSpecialChars(strInput)
{
  strInput = strInput.replace(/&/g,'&amp;');  // always replace first.
  strInput = strInput.replace(/"/g,'&quot;');
  strInput = strInput.replace(/</g,'&lt;');	
  strInput = strInput.replace(/>/g,'&gt;');
  strInput = strInput.replace(/'/g,'&apos;');
  strInput = trim(strInput);

  return strInput;
}

// Select the checkbox in the control array that
// equals the given value
function setCheckboxValue(controlArray, strValue)
{
  var intLoop;	// Loop control
  for( intLoop = 0; intLoop < controlArray.length; intLoop++ )
  {
	if( controlArray[intLoop].value == strValue )
	{
		controlArray[intLoop].checked = true;
		return;
	}
	else
	{
		controlArray[intLoop].checked = false;
	}
  }
}

// Set the desired radio button in the control array
function setRadioValue(controlArray, strValue)
{
  var intLoop;	// Loop control
  for( intLoop = 0; intLoop < controlArray.length; intLoop++ )
  {
	if( controlArray[intLoop].value == strValue)
	{
		controlArray[intLoop].checked = true;
		return;
	}
	else
	{
		controlArray[intLoop].checked = false;
	}
  }
}

// Set the selected option
function setSelectValue(dropDown, sValue)
{
  var intLoop;	// Loop control
  for( intLoop = 0; intLoop < dropDown.options.length; intLoop++ )
  {
	if( dropDown.options[intLoop].value == sValue )
	{
		dropDown.options[intLoop].selected = true;
		return;
	}
  }
}

// Set the visibility property to show the form contained
// in the given span or div reference
function showSpan(strFormWrapper)
{
  if( document.layers && document.layers[strFormWrapper] != null )
  {	// NN4
	document.layers[strFormWrapper].visibility = 'show';
  }
  else if( document.all && !document.getElementById )
  {	// IE4
	document.all[strFormWrapper].style.visibility = 'visible';
  }
  else
  {	// IE5+, NN6
	document.getElementById(strFormWrapper).style.visibility = 'visible';
  }
}

// Log the user in to the system
function StartLogin()
{
  var sMessage = 'You have been logged out due to inactivity, please login to continue.';
  var bReturn = showModalDialog("modalLogin.htm",sMessage,"help:no; dialogWidth:300px; dialogHeight:180px; status:no");
  if(bReturn)
  {
	eval(w_sFunctionName);
  }
}

// Are regular expressions supported?
function supportRegExp()
{
  if (window.RegExp) 
  {
	var tempStr = "a";
	var tempReg = new RegExp(tempStr);
	if (tempReg.test(tempStr)) { return true; }
  }
  return false;
}

// Trim leading and trailing spaces
function trim(strValue)
{
  return strValue.replace(/(^\s*)|(\s*$)/g, "");
}

// Remove dashes '-' from the given string
function unDash(sString)
{
  return sString.replace(/\D/g,'');
}

// Add the current page to user's Favorites
// This function only works with IE Explore.
function addPagetoFavorites(){
	var bookmarkurl = window.location.href;
	var bookmarktitle = document.title;
	
	if (document.all) window.external.AddFavorite(bookmarkurl,bookmarktitle);
}