/**
 * Generalized form processing functions to be reused.
 */
 
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 dobPopup( str_event ){
  openWindow( 'index.php?event=' + str_event,
              '',
              500,
              300,
              ( screen.width - 535 ),
              ( screen.height - 380 ) );
}

function deleteIndexFromSelect( ref_select, int_index )
{
  if( ref_select.options[ int_index ] != null )
    ref_select.options[ int_index ] = null; 
}

function addValueToSelect( ref_select, str_value, str_text )
{
  var an_option = new Option();
  
  an_option.value = str_value;
  an_option.text  = str_text;
  
  ref_select[ ref_select.options.length ] = an_option;
} 

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 selectOptionById( str_id ){
  document.getElementById( str_id ).selected = true;
}

function enumerateOptionValues( ref_select, str_delim )
{
  var int_size   = ref_select.options.length;
  var i          = 0;
  var str_return = "";
  
  for( i = 0; i < int_size; i++ )
    str_return = str_return + ref_select.options[ i ].value + str_delim;

  return str_return.substring( 0, str_return.length - 1 );    
}

function enumerateOptionText( ref_select, str_delim )
{
  var int_size   = ref_select.options.length;
  var i          = 0;
  var str_return = "";
  
  for( i = 0; i < int_size; i++ )
    str_return = str_return + ref_select.options[ i ].text + str_delim;

  return str_return.substring( 0, str_return.length - 1 );  
}

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 valueIsRequired( ref_hasValue, str_message ){
// Test to make sure reference is not null.
  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 valueIsSentinel( ref_hasValue, str_sentinel ){
// Test to make sure reference is not null.
  if( !ref_hasValue ) return false;

  ref_hasValue.value = trim( ref_hasValue.value );
  if( ref_hasValue.value == str_sentinel )
     return true;

  return false;
}

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;
}

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.
function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire;
}
// Example:
// alert( readCookie("myCookie") );
function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}