/**
 * Generalized form processing functions to be reused.
    openWindow()
    setSelectBoxByValue()
    getSelectedOptionValue()
    getSelectedOptionText()
    getRadioValue()
    trim()
    validateEmailFormat()
    clientAcceptsCookies()
    valueIsRequired()
    valueHasRequiredLength()
    valueIsNumber()
    valueIsInteger()
 */
 
function openWindow( url, name, width, height, left, top ){
/*
SAMPLE CALL:
openWindow('handler.cfm?userID=22&userName=david&event=Customer,newCustomer','c',500,300,(screen.width - 535),( screen.height - 380 ));"
*/
// Use this to test window dimensions on various resolutions:
// alert( "screen.width = " + screen.width + ", screen.height =" + screen.height + "\nwidth = "+width+", height = " + height + "\nleft = " + left + " , top = " + top );
  newWin = window.open( url, name,"width=" + width + ",height=" + height + ",top=" + top + ",left=" + left);
  if( newWin.opener == null ) newWin.opener = window;
}

function refreshOpener()
{
  if( window.opener )
    window.opener.location.reload();
}

function setSelectBoxByValue( ref_form, str_select, str_value ){
/*
PURPOSE:
Selects the OPTION in REF_FORM[ STR_SELECT ] whose value matches STR_VALUE.
REQUIRES:
ref_form - reference to a form.
str_select - string name of SELECT box
str_value - value of OPTION to select
RETURNS:
true if found, false if not found
*/
	var int_num = ref_form[ str_select ].length;
 var i = 0;

  // Find and select OPTION with value = STR_VALUE
  for( i = 0; i < int_num; i++ )
    if( ref_form[ str_select ].options[ i ].value == str_value ){
      ref_form[ str_select ].options[ i ].selected = true;
      return true;
    }

    return false;
}


function getSelectedOptionValue( ref_form, str_select )
// Returns value of selected OPTION within the SELECT named str_select
{
	return ref_form[ str_select ].options[ ref_form[ str_select ].selectedIndex ].value;
}

function getSelectedOptionText( ref_form, str_select )
// Returns value of selected OPTION within the SELECT named str_select
{
	return ref_form[ str_select ].options[ ref_form[ str_select ].selectedIndex ].text;
}

function getRadioValue( ref_radio ){
// Returns value of selected RADIO box or null if none selected.
  var int_len = ref_radio.length;
  var i       = 0;

  for( i = 0; i < int_len; i++ )
    if( ref_radio[ i ].checked )
      return ref_radio[ i ].value;

  return null;
}

function trim( str_toTrim ){
// Trims spaces off of strings
  var chr_char  = " ";
  var i         = 0;
  var int_start = 0;
  var int_end   = str_toTrim.length;

  while( str_toTrim.charAt( int_start ) == " " && int_start < int_end )
    int_start++;

  while( str_toTrim.charAt( int_end - 1 ) == " " && int_end > 0 )
    int_end--;

  if( int_start >= int_end ) return '';
  return str_toTrim.substring( int_start, int_end );
}

function validateEmailFormat( str_eddress ){
// Checks whether a string has @ and a valid American Top Level Domain.
  if(  str_eddress.indexOf("@") != -1 &&    // Must have @
     ( str_eddress.indexOf(".com") != -1 || // Must have valid domain
       str_eddress.indexOf(".edu") != -1 ||
       str_eddress.indexOf(".gov") != -1 ||
       str_eddress.indexOf(".mil") != -1 ||
       str_eddress.indexOf(".net") != -1 ||
       str_eddress.indexOf(".org") != -1 ) )
    return true;

  return false;
}

function clientAcceptsCookies(){
// Returns true if can set cookies, false otherwise.
  var WM_acceptsCookies = false;

  if(document.cookie == '')
  {
    document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
    if(document.cookie.indexOf('WM_acceptsCookies=yes') != -1) {
	    WM_acceptsCookies = true; 
    }// If it succeeds, set variable
  }else { // there was already a cookie
    WM_acceptsCookies = true;
  }
  return WM_acceptsCookies;
}

function valueIsRequired( ref_hasValue, str_message ){
  if( !ref_hasValue )
    return false;

  if( !ref_hasValue.value ){
    alert( str_message );
    ref_hasValue.focus();
    return false;
  }

  ref_hasValue.value = trim( ref_hasValue.value );
  if( ref_hasValue.value.length == 0 ){
     alert( str_message );
     ref_hasValue.focus();
     return false;
  }
  return true;
}

function valueHasRequiredLength( ref_hasValue, int_length, str_message ){
  if( !ref_hasValue )
    return false;

  if( !ref_hasValue.value ){
    alert( str_message );
    ref_hasValue.focus();
    return false;
  }

  ref_hasValue.value = trim( ref_hasValue.value );
  if( ref_hasValue.value.length != int_length ){
     alert( str_message );
     ref_hasValue.focus();
     return false;
  }
  return true;
}

function valueIsNumber( field, message )
/*
PURPOSE:
  * checks whether field.value is a numeric. If yes, fine. If no,
    prints message and returns focus to field.
PARAMETERS:
  * field   - reference to a field having a .value attribute.
  * message - message to print in an alert box.
*/
{
  if( isNaN( field.value.valueOf() ) )
  {
    alert( message );
    field.focus();
    return false;
  }
  return true;
}

function valueIsInteger( field, message ){
  var value = parseInt( field.value );
  
  if( isNaN( value ) ){
    alert( message );
    field.focus();
    return false;
  }
  return true;
}