  // Generic Client-Side JavaScript Functions

  /********************************
             Change Log
  
  // Revised 11/08/2005
  // Modified alphaOnly, alphaNumericOnly(),
              numericOnly(), and formatAllowed()
  // All functions now support optional 'special_chars' 
  // parameter to extend the acceptable values.
			 
  // Revised 10/27/2005
  // Added clear_select_options()

  // Revised 10/11/2005
  // Modified alphaOnly(), alphaNumericOnly(),
  //          numericOnly(), and formatAllowed()
  // to use .charAt() instead of referencing 
  // characters as array. Referencing as array
  // does not appear to work in IE.

  // Revised 09/14/2005
  // Added validDate()

  // Revised 05/31/2005
  // Added get_radio_value(), 
  //       get_checkbox_value(),
  //       get_select_value(),
  //       get_select_mult_value()

  ********************************/
    
  
  /********************************
     Formatting Functions
  ********************************/
  
  // Format Date Function
  // Formats Date Into mm/dd/yyyy
  function formatDate(dateObj)
  {
     var dateStr = "";

	 var month, date, year;

	 month = dateObj.getMonth() + 1;
	 date  = dateObj.getDate();
	 year  = dateObj.getFullYear();
	 month = (month < 10) ? "0" + month : month;
	 date  = (date  < 10) ? "0" + date: date;

	 dateStr = month + "/" + date + "/" + year;
	 return dateStr;
  }

  /********************************
        Validation Functions
  ********************************/
  function alphaOnly(valuein, start, end, allowEmpty, allowWhitespaces, special_chars)
  {
     // Assign Default Values
     allowEmpty       = (allowEmpty == null) ? false : true;
     allowWhitespaces = (allowWhitespaces == null) ? false: true;
	 start            = (start < 0 || start == null) ? 0 : start;
	 end              = (end <= start || end == null) ? valuein.length : end;
	 special_chars    = (special_chars == null) ? "" : special_chars;

     var temp = valuein.toLowerCase();
	 temp     = temp.substring(start, end);

     var acceptedFlag = null;
     var accepted = "abcdefghijklmnopqrstuvxwyz";
	 accepted    += special_chars;

     // Allow Whitespaces?
	 if(allowWhitespaces)
	 {
		 accepted += " ";
	 }

     // Allow Empty?
	 if(temp.length < 1 && !allowEmpty)
	 {
	    // Empty Values Not Accepted
		return false;
	 }

     // Loop thru Each Character in valuein
     for(vIndex = 0; vIndex < temp.length; vIndex++)
	 {
	    acceptedFlag = false;   
	    for(aIndex = 0; aIndex < accepted.length; aIndex++)
		{
		   if(temp.charAt(vIndex) == accepted.charAt(aIndex))
		   {
		      acceptedFlag = true;
			  break;
		   }
		}

		if(!acceptedFlag)
		{
		   // Character is not accepted
		   return false;
		}

	 }
       
	 return true;
  }

  function alphaNumericOnly(valuein, start, end, allowEmpty, allowWhitespaces, special_chars)
  {
     // Assign Default Values
     allowEmpty        = (allowEmpty == null) ? false : true;
     allowWhitespaces  = (allowWhitespaces == null) ? false: true;	 
	 start             = (start < 0 || start == null) ? 0 : start;
	 end               = (end <= start || end == null) ? valuein.length : end;
	 special_chars     = (special_chars == null) ? "" : special_chars;

     var temp = valuein.toLowerCase();
	 temp     = temp.substring(start, end);

     var acceptedFlag = null;
     var accepted = "abcdefghijklmnopqrstuvxwyz0123456789";
	 accepted    += special_chars; 

     // Allow Whitespaces?

	 if(allowWhitespaces)
	 {
		 accepted += " ";
	 }

     // Allow Empty?
	 if(temp.length < 1 && !allowEmpty)
	 {
	    // Empty Values Not Accepted
		return false;
	 }

     for(vIndex = 0; vIndex < temp.length; vIndex++)
	 {
	    acceptedFlag = false;   
	    for(aIndex = 0; aIndex < accepted.length; aIndex++)
		{
		   if(temp.charAt(vIndex) == accepted.charAt(aIndex))
		   {
		      acceptedFlag = true;
			  break;
		   }
		}

		if(!acceptedFlag)
		{
		   // Character is not accepted
		   return false;
		}

	 }
       
	 return true;
  }

  function numericOnly(valuein, start, end, allowEmpty, allowWhitespaces, special_chars)
  {
     // Assign Default Value
     allowEmpty       = (allowEmpty == null) ? false : true;
	 allowWhitespaces = (allowWhitespaces == null) ? false: true;
	 start            = (start < 0 || start == null) ? 0 : start;
	 end              = (end <= start || end == null) ? valuein.length : end;
	 special_chars    = (special_chars == null) ? "" : special_chars;


	 var temp = valuein;
	 temp = temp.substring(start, end);
     var acceptedFlag = null;
     var accepted = "0123456789";
	 accepted    += special_chars;

     // Allow Whitespaces?
	 if(allowWhitespaces)
	 {
		 accepted += " ";
	 }

     // Allow Empty?
	 if(temp.length < 1 && !allowEmpty)
	 {
	    // Empty Values Not Accepted
		return false;
	 }

     for(vIndex = 0; vIndex < temp.length; vIndex++)
	 {
	    acceptedFlag = false;   
	    for(aIndex = 0; aIndex < accepted.length; aIndex++)
		{		   
		   if(temp.charAt(vIndex) == accepted.charAt(aIndex))
		   {
		      acceptedFlag = true;
			  break;
		   }
		}

		if(!acceptedFlag)
		{
		   // Character is not accepted
		   return false;
		}

	 }
       
	 return true;
  }

  function formatAllowed(valuein, format, special_chars)
  {
     // Wildcard Characters
	 // n - numeric
	 // a - alpha
	 // b - alphanumeric
	 // Any other character must match

	 var temp = valuein;
	 format = format.toLowerCase();
     var acceptedFlag = null;

     // First, See if value in is equal length to format
	 // otherwise it is invalid.
	 if(temp.length != format.length)
	 {
	    return false;
	 }
  
     for(dIndex = 0; dIndex < temp.length; dIndex++)
	 {

	   acceptedFlag = false;
	   switch(format.charAt(dIndex))
	   {
	 
	      case "n":			
			acceptedFlag = numericOnly(temp.charAt(dIndex), null, null, null, null, special_chars);
			break;
		  case "a":
		    acceptedFlag = alphaOnly(temp.charAt(dIndex), null, null, null, null, special_chars);
			break;
		  case "b":
		    acceptedFlag = alphaNumericOnly(temp.charAt(dIndex), null, null, null, null, special_chars);
			break;
		  default:
			// Format Character Must Match Character in valuein
			if(temp.charAt(dIndex) == format.charAt(dIndex))
			{
			   acceptedFlag = true;  
			}
			else
			{
			   acceptedFlag = false;
			}
		    break;
	   }

	   if(!acceptedFlag)
	   {
	     return false;
	   }

	 }

	 return true;
  }

  // Validate a Given Date String (mm/dd/yyyy) 
  function validDate(date_in, future_date_flag)
  {
	 // To Prevent Potential Problems, Make Sure date_in is a String Object
	 var date_in     = "" + date_in;

	 // Initialize Future Date Flag
	 var future_date_flag = (future_date_flag == true)?true:false;

	 var date_pieces = date_in.split("/");

	 var check_date_mm = parseInt(date_pieces[0], 10);
	 var check_date_dd = parseInt(date_pieces[1], 10);
	 var check_date_yy = "" + parseInt(date_pieces[2], 10);

	 // Add Leading Zeroes as Needed
	 check_date_mm = ((check_date_mm < 10)?"0":"") + check_date_mm;
	 check_date_dd = ((check_date_dd < 10)?"0":"") + check_date_dd;

     //alert("YEAR: " + check_date_yy.length);
	 // Make Sure Year is Four Digits
	 if(check_date_yy.length != 4)
	 {
	   return false;
	 }

	 // Check if Given Date is a Valid Date
	 var compare_date    = new Date(date_in);
	 var compare_date_mm = compare_date.getMonth() + 1;
	 var compare_date_dd = compare_date.getDate();
	 var compare_date_yy = compare_date.getFullYear();

	 // Add Leading Zeroes as Needed
	 compare_date_mm = ((compare_date_mm < 10)?"0":"") + compare_date_mm;
	 compare_date_dd = ((compare_date_dd < 10)?"0":"") + compare_date_dd;

     //alert("CHECK: " + check_date_mm + " " + check_date_dd + " " + check_date_yy + " COMP: " + compare_date_mm + " " + compare_date_dd + " " + compare_date_yy);
	 // Compare the Dates
	 if(check_date_mm != compare_date_mm || check_date_dd != compare_date_dd || check_date_yy != compare_date_yy)
	 {
       return false;
	 }	

	 // Allow Future Dates?
	 if(!future_date_flag)
	 {
	   // Future Dates Not Allowed
	   var check_date   = new Date(date_in);
	   check_date.setHours(0);
	   check_date.setMinutes(0);
	   check_date.setMilliseconds(0);

	   var current_date = new Date();
	   current_date.setHours(0);
	   current_date.setMinutes(0);
	   current_date.setMilliseconds(0);

       //alert("CHECK: " + check_date.getTime() + " COMP: " + current_date.getTime());
	   if(check_date.getTime() > current_date.getTime())
	   {
		 return false;
       }
     }

	 // Date Successfully Validated	 
	 return true;
  }

  // Validate Given Value as URL
  function validURL(valuein) 
  {

    var regEx = /^(file|http|https):\/\/\S+\.(com|net|edu|org|info|biz|ws|us|tv|cc|\d+)?(|\/.*)$/i
    if (!regEx.test(valuein))
    {
       return false;
    }
   
    // URL Validated
    return true;
  }

  /**************************
    Copied E-mail Validator
    From: http://geekswithblogs.net/rai.yawar.ijaz/archive/2004/11/12/14902.aspx
  ***************************/
  function validEmail(valuein)
  {
	var temp = valuein;
	var at="@";
	var dot=".";
	var lat=temp.indexOf(at);
	var lstr=temp.length;
	var ldot=temp.indexOf(dot);
	
	if (temp.indexOf(at)==-1)
	{
	   return false;
    }
	
	if (temp.indexOf(at)==-1 || temp.indexOf(at)==0 || temp.indexOf(at)==lstr)
    {
		return false;
	}
	
	if (temp.indexOf(dot)==-1 || temp.indexOf(dot)==0 || temp.indexOf(dot)==lstr)
	{
		return false;
	}
	
	if (temp.indexOf(at,(lat+1))!=-1)
	{
		return false;

    }
	
	if (temp.substring(lat-1,lat)==dot || temp.substring(lat+1,lat+2)==dot)
	{
		return false;
	}
	
	if (temp.indexOf(dot,(lat+2))==-1)
	{
		return false;
	}
	
	if (temp.indexOf(" ")!=-1)
	{
		return false;
	}
	
	return true;
  } 

  /******************************
       HTML Forms Functions
  ******************************/
  function get_radio_value(radio_obj)
  {
	if(radio_obj.type != "radio")
	{
      // return "";
	}

    var value = "";
    for (var x = 0; x < radio_obj.length; x++)
	{
      if (radio_obj[x].checked)
      {		
         value = radio_obj[x].value;
         break;
       }
	 }
     return value;  
  }

  function get_checkbox_value(check_obj)
  { 
	if(check_obj.type != "checkbox")
    {
      // return new Array();
	}

	// Return all Selected Values as an Array
    var values= new Array();
    for (var x = 0; x < check_obj.length; x++)
	{
      if (check_obj[x].checked)
      {
		 values[values.length] = check_obj[x].value;
       }
	 }
     return values; 
  }

  function get_select_value(select_obj)
  {
    if(select_obj.type != "select-one")
    {
      // return "";
	}

	var value = select_obj[select_obj.selectedIndex].value;

	return value;
  }

  function get_select_mult_value(select_mult_obj)
  {
	if(select_mult_obj.type != "select-multiple")
	{
      // return new Array();
	}

    // Return all Selected Values as an Array
    var values = new Array();
    for (var x = 0; x < select_mult_obj.length; x++)
	{
      if (select_mult_obj[x].selected)
      {
		 values[values.length] = select_mult_obj[x].value;
       }
	 }
     return values;
  }

  
  /******************************
    Preserve and Restore
    Form Object Values
  ******************************/
  // NOTE: These functions are very useful when
  //       dealing with dynamic <div> layers as
  //       manuiplating these layers will result
  //       in form values being lost.

  // Preserve Form Selections.
  function preserve_form_selections(form_name)
  {
    // NOTE: form_name is optional and restricts
    //       form selections saved to given form name.

    // Preserve Every Selection for all Forms
    var form_selections = new Array();

    // Build Array of Forms to Process
    var process_forms = new Array();

    // Specific Form Name Given as Param?
    if(form_name != "" && form_name != null)
    {
      process_forms[0] = form_name;
    }
    else
    {
      for(var f = 0; f < document.forms.length; f++)
      {
        process_forms[f] = f;
      }
    }

    // Loop Thru Forms
    for(var form_index = 0; form_index < process_forms.length; form_index++)
    {
      form_selections[form_index] = new Array();

      // Loop Thru This Form's Elements
	  for(var element_index = 0; element_index < document.forms[process_forms[form_index]].elements.length; element_index++)
	  {
	    form_selections[form_index][element_index] = new Array();

	    // Preserve Element Information

	    // Name
	    form_selections[form_index][element_index][0] = document.forms[process_forms[form_index]].elements[element_index].name;

	    // Type
	    form_selections[form_index][element_index][1] = document.forms[process_forms[form_index]].elements[element_index].type;

	    // Get Type Based on Type
	    element_type = "" + document.forms[process_forms[form_index]].elements[element_index].type;
	    element_type = element_type.toLowerCase();
	    //alert("Preserving: " + document.forms[process_forms[form_index]].elements[element_index].name + " + Type: " + document.forms[process_forms[form_index]].elements[element_index].type);
	    switch(element_type)
	    {
	      case "radio":
	   	  case "checkbox":
		    form_selections[form_index][element_index][2] = document.forms[process_forms[form_index]].elements[element_index].checked;
		  break;

		  case "select-one":
 		    form_selections[form_index][element_index][2] = document.forms[process_forms[form_index]].elements[element_index].selectedIndex;
		  break;

		  default:				     
		    form_selections[form_index][element_index][2] = document.forms[process_forms[form_index]].elements[element_index].value;
          break;

		  // Add case for select-multiple Later

		}// switch(element_type)			  
		
      }// Form Element Loop
    }// Form Loop

    return form_selections;

  }// function preserve_form_selections()

  function restore_form_selections(form_selections, form_name)
  {
    // NOTE: form_name param is optional and restricts
 	//       form selections restored to given form 
	//       name.

    // NOTE: Form Selections Array is Parallel
	//       To Form Objects on Page	
		
	// Build Array of Forms to Process
	var process_forms = new Array();

	// Specific Form Name Given as Param?
 	if(form_name != "" && form_name != null)
	{
	  process_forms[0] = form_name;
	}
	else
	{
	  for(var f = 0; f < form_selections.length; f++)
      {
	     process_forms[f] = f;
	  }
	}

	// Loop Thru Forms
	for(var form_index = 0; form_index < process_forms.length; form_index++)
	{
	  // Loop Thru This Form's Elements
	  for(var element_index = 0; element_index < form_selections[form_index].length; element_index++)
	  {
	    // Get Element Type
	    element_type = "" + document.forms[process_forms[form_index]].elements[element_index].type;
	    element_type = element_type.toLowerCase();

		switch(element_type)
		{  
		  case "radio":
		  case "checkbox":				      
		    document.forms[process_forms[form_index]].elements[element_index].checked = form_selections[form_index][element_index][2];
		  break;

		  case "select-one":	
		    document.forms[process_forms[form_index]].elements[element_index].selectedIndex = form_selections[form_index][element_index][2];
		  break;

		  // Add case for 'select-multiple' later

		  default:
		    document.forms[process_forms[form_index]].elements[element_index].text = form_selections[form_index][element_index][2];
		  break;
	    }
	  }
    }
  }

 function clear_select_options(select_obj)
	{	
	  for(var index = 0; index < select_obj.options.length; index++)
	  {
	    select_obj.options[index] = null;
	  }

	  select_obj.options.length = 0;
	}

  /*****************************
         Misc. Functions
  ******************************/

  // Redirect User to a Given URL
  function goBack(URL) 
  {
     location.replace(URL);
  }

  // Truncate Text by Given Characters (Adds a "." at the end
  // of truncated text.
  function text_truncate(textin, max_chars)
  {
 	var trunc_text = "";

	for(var index = 0; index < max_chars; index++)
	{
	   trunc_text += textin.charAt(index);
	}

	return trunc_text;
  }