/*---------------------------------------------------------
returns the value of a cookie. cName is the name of the cookie.
*/
function getCookie( name )
{
   var flag = document.cookie.indexOf( name+'=' );
   if( flag != -1 ) {
      flag += name.length + 1;
      var end = document.cookie.indexOf( "; ", flag );
      if( end == -1 ) end = document.cookie.length;
      return unescape(document.cookie.substring( flag, end ));
   }
}

/*---------------------------------------------------------
returns the value of a subcookie. cName is the name of the cookie,
scname is the sub-cookie name.
*/
function getSubCookie( cname, scname )
{
   var cbegin = document.cookie.indexOf(cname + '=');
   if (cbegin != -1) 
   {
	   var cend = document.cookie.indexOf("; ", cbegin);
	   if (cend == -1) cend = document.cookie.length;
	   var cstring = document.cookie.substring(cbegin, cend);
	   
	   var scbegin = cstring.indexOf(scname + '=');
	   if (scbegin != -1)
	   {
	     var scend = cstring.indexOf('&', scbegin);
	     if (scend == -1) scend = cstring.length;
	     var scstring = cstring.substring(scbegin, scend);
	     
	     var scequalspos = scstring.indexOf('=');
	     if (scstring.length > scequalspos)
	     {
	       scvalue = scstring.substring(scequalspos+1, scstring.length);
	       return scvalue;
	     }
	   }
   }
}

/*---------------------------------------------------------
Sets the cookie as specified by the parameters
*/
function setCookie(name,value,expires,path,domain,secure) {
  document.cookie = 
    name + '=' + escape(value) +
    ( (expires) ? ';expires=' + expires.toGMTString() : '') +
    ( (path) ? ';path=' + path : '') + 
    ( (domain) ? ';domain=' + domain : '') +
    ( (secure) ? ';secure' : '');
}

/*---------------------------------------------------------
Deletes the cookie specified by name, path, domain
*/
function deleteCookie(name,path,domain) {
  if (getCookie(name))
    document.cookie =
      name + '=' +
      ( (path) ? ';path=' + path : '') +
      ( (domain) ? ';domain=' + domain : '') +
      ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/*---------------------------------------------------------
Focuses the first entry of the first form on the page.
Call this in your body onLoad event with a tag such as:
<body onload="javascript: firstFocus();">
*/
function firstFocus() {
  if (document.forms.length > 0) {
    var TForm = document.forms[0];
    for (i=0;i<TForm.length;i++) {
      if ((TForm.elements[i].type=="text")||
          (TForm.elements[i].type=="textarea")||
          (TForm.elements[i].type.toString().charAt(0)=="s")) {
        if (!TForm.elements[i].disabled) {
          document.forms[0].elements[i].focus();
          break;
          }
        }
      }
    }
  }
function rightTrim( strValue ) {
/************************************************
DESCRIPTION: Trims trailing whitespace chars.

PARAMETERS:
   strValue - String to be trimmed.

RETURNS:
   Source string with right whitespaces removed.
*************************************************/
var objRegExp = /^([\w\W]*)(\b\s*)$/;

      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim( strValue ) {
/************************************************
DESCRIPTION: Trims leading whitespace chars.

PARAMETERS:
   strValue - String to be trimmed

RETURNS:
   Source string with left whitespaces removed.
*************************************************/
var objRegExp = /^(\s*)(\b[\w\W]*)$/;

      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trimAll( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading &amp; trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}


/************************************************
Enhances the unescape function slightly. Currently it replaces
+ symbols with a " " space character. Also, if a null
value is passed in, it returns that null value instead
of just bombing out.
*/
function enhancedUnescape(strValue)
{
	if (strValue)
		return unescape(strValue.replace("+", " "));
	else
		return strValue;
}

/************************************************
Displays a popup window with an error message in it.
*/
function criticalErrorWindow(intErrNum, strErrSource, strErrDesc)
{
  var newwin = window.open(null, '_blank', 'location=no, menubar=no, scrollbars=yes, status=no, titlebar=no, toolbar=no, ' + 
                           'height=300, width=400, resizable=yes, directories=no');
  newwin.document.write('<div align="left" style="font-size: 80%"><a href=null onClick="javascript: window.close();">Close</a></div>');
  newwin.document.write('<div align="center">');
  newwin.document.write('<h2>');
  newwin.document.write('Error');
  newwin.document.write('</h2>');
  newwin.document.write('</div>');
  newwin.document.write(strErrDesc + '<br />');
  if (intErrNum != 0)
  {
		newwin.document.write('Error Number: ' + intErrNum + '<br />');
  }
  if (strErrSource != "")
  {
		newwin.document.write('Error Source: ' + strErrSource + '<br />');
  }
  newwin.document.write('<br />');
  newwin.document.write('<div align="left" style="font-size: 80%"><a href=null onClick="javascript: window.close();">Close</a></div>');
}

/************************************************
Displays a popup window with a handy title and help message.
*/
function helpPopupWindow(strTitle, strMessage, strName)
{
  var newwin = window.open(null, strName, 'location=no, menubar=no, scrollbars=yes, status=no, titlebar=no, toolbar=no, ' + 
                           'height=275, width=375, resizable=yes, directories=no');
  newwin.document.write('<html>');
  newwin.document.write('<head>');
  newwin.document.write('<link href="../baseStyle.css" type="text/css" rel="stylesheet">');
  newwin.document.write('</head>');
  newwin.document.write('<body>');
  newwin.document.write('<div align="left" style="font-size: 80%"><a href=null onClick="javascript: window.close();">Close</a></div>');
  newwin.document.write('<br />');
  newwin.document.write('<div align="center">');
  newwin.document.write('<h3>');
  newwin.document.write(strTitle);
  newwin.document.write('</h3>');
  newwin.document.write('</div>');
  newwin.document.write('<br />');
  newwin.document.write(strMessage + '<br />');
  newwin.document.write('<br />');
  newwin.document.write('<div align="left" style="font-size: 80%"><a href=null onClick="javascript: window.close();">Close</a></div>');
  newwin.document.write('</body>');
  newwin.document.write('</html>');
}

/************************************************
Makes all Yes/No entries on the form visible
*/
function showAllYesNo()
{
  var elements = document.getElementsByTagName("select");
  var i;
  for (i=0; i<elements.length; i++)
  {
    //if (elements[i].id.toUpperCase().indexOf("YESNO") != -1)
    {
      elements[i].style.visibility="visible";
    }
  }
}

/************************************************
Returns a greeting message based on the time of day.
*/
function greetingString()
{
  var myDate = new Date();
  if (myDate.getHours() < 12)
    return "Good Morning ";
  else if (myDate.getHours() < 18)
    return "Good Afternoon ";
  else
    return "Good Evening ";
}

/************************************************
Given the array of image file paths imagePathArray,
this will pre-load all the images. In other words,
it makes sure the images are downloaded and cached
before the page is displayed. Useful for onmouseover
events.
*/
function preloadImages(imagePathArray)
{
  var index;
  for (index=0; index<imagePathArray.length;index++)
  {
    var tmpImg = new Image();
    tmpImg.src = imagePathArray[index];
  }
}

/************************************************
Given an element ID strElementId, this function is
supposed to make sure that the entire element is scrolled
into the current window view. The code doesn't work
though, so it is currently commented out. It is used
by the endorsement entry page. If you can get it working,
great.
*/
function scrollInView(strElementId)
{
  //var ind;
  //for(ind in document.getElementById(strElementId))
  //{
    //alert(ind + ": " + document.getElementById(strElementId)[ind]);
  //}
  //window.scrollTo(document.getElementById(strElementId).style.top + document.getElementById(strElementId).offsetHeight);
}

/************************************************
Given the dropdown entry thisSelect, and the value thisValue,
sets the selected index of the dropdown list to the
value specified by thisValue.
*/
function setSelectToValue(thisSelect, thisValue) {
  for (i = 0; thisSelect[i]; i++) 
  {
    if (thisSelect[i].value == thisValue) 
    {
        thisSelect.selectedIndex = i;
    }
  }
}

/* If you call this in the onkeypress event of a textbox (or textarea), 
   this ensures that the key pressed is either numeric or a period. If it
   is not, then the key is ignored. Works for IE and FireFox.
   event e - the window.event object.
   wholeNumOnly - boolean value that determines if the number can accept a ".".
                  True to not accept periods, false to accept them.
*/
function restrictEntryNum(e, allowDecimals)
{
	var key;
	var keychar;
	var reg;
	
  if (e != null)
  {
	  if(window.event) {
		  // for IE, e.keyCode or window.event.keyCode can be used
		  key = e.keyCode; 
	  }
	  else if(e.which) {
		  // netscape
		  key = e.which; 
	  }
	  else {
		  // no event, so pass through
		  return true;
	  }

	  if (key == 8)
	    return true;
	  else {
	    keychar = String.fromCharCode(key);
	    if (allowDecimals)
	      reg = /\d|\./;
	    else
	      reg = /\d/;
	    return reg.test(keychar);
	  }
	}
	else
	  return true;
}

/* If you call this in the onkeypress event of a textbox (or textarea), 
   this ensures that the key pressed is either numeric or a "/". If it
   is not, then the key is ignored. Works for IE and FireFox.
   event e - the window.event object.
*/
function restrictEntryDate(e)
{
	var key;
	var keychar;
	var reg;

  if (e != null)
  {
	  if(window.event) {
		  // for IE, e.keyCode or window.event.keyCode can be used
		  key = e.keyCode; 
	  }
	  else if(e.which) {
		  // netscape
		  key = e.which; 
	  }
	  else {
		  // no event, so pass through
		  return true;
	  }
	  if (key == 8)
	    return true;
	  else {
	    keychar = String.fromCharCode(key);
      reg = /\d|\//;
	    return reg.test(keychar);
	  }
	}
	else
	  return true;
}

/* If you call this in the onkeypress event of a textbox (or textarea), 
   this ensures that the key pressed is either numeric or a "-". If it
   is not, then the key is ignored. Works for IE and FireFox.
   event e - the window.event object.
*/
function restrictEntryZipCode(e)
{
	var key;
	var keychar;
	var reg;

  if (e != null)
  {
	  if(window.event) {
		  // for IE, e.keyCode or window.event.keyCode can be used
		  key = e.keyCode; 
	  }
	  else if(e.which) {
		  // netscape
		  key = e.which; 
	  }
	  else {
		  // no event, so pass through
		  return true;
	  }
	  if (key == 8)
	    return true;
	  else {
	    keychar = String.fromCharCode(key);
      reg = /\d|-/;
	    return reg.test(keychar);
	  }
	}
	else
	  return true;
}