/*******************************************************************************
FILE NAME       :form.js
DESCRIPTION     :contains functions for client side form validation
API DEPENDENCIES:trim_text_fields(form) ***requires sting.js
*******************************************************************************/

//-- global variables begin ----------------------------------------------------

//-- global variables end ------------------------------------------------------

//-- functions begin -----------------------------------------------------------

//FUNCTION-- trims all fields of type text, textarea and password in a form ***requires string.js api
function trim_text_fields(form_obj)
{
 for(var i=0;i<form_obj.elements.length;i++)//if document has a form with elements
 {
  if(form_obj.elements[i].type=="text" || form_obj.elements[i].type=="textarea" || form_obj.elements[i].type=="password")//only trim text, textarea or password form elements
  form_obj.elements[i].value=trim_string(form_obj.elements[i].value);
 }
}

//FUNCTION-- checks if text, textarea or password field contains data
function has_data(text_obj, message)
{
 if(text_obj.value.length==0) return validate_alert(text_obj, message);
 return true;
}

//FUNCTION-- provides loose validation for email address
function validate_email_address(text_obj)
{
 if(text_obj.value.length > 0 && text_obj.value.length < 5) 
 return validate_alert(text_obj, "This is not a valid length for an email address.");
 		
 if(text_obj.value.length > 0 && text_obj.value.indexOf("@") == 0)
 return validate_alert(text_obj, "An email address cannot start with \"@\".");
 
 if(text_obj.value.length > 0 && text_obj.value.indexOf(".") == 0)
 return validate_alert(text_obj, "An email address cannot start with a period.");
 
 if(text_obj.value.length > 0 && text_obj.value.indexOf("@.") > 0)
 return validate_alert(text_obj, "The \"second-level domain\" is missing from your email address.");
 
 if(text_obj.value.length > 0 && text_obj.value.indexOf(".") == text_obj.value.length-1)
 return validate_alert(text_obj, "The \"top-level domain\" is missing from you email address.");
 
 if(text_obj.value.length > 0 && text_obj.value.indexOf("@") < 0)
 return validate_alert(text_obj, "You are missing \"@\" in your email address.");
 
 if(text_obj.value.length > 0 && text_obj.value.indexOf(".") < 0)
 return validate_alert(text_obj, "You are missing a \".\" (period) in your email address.");
 
 return true; //return true is we make it this far
}

//FUNCTION-- checks if an item was selected in a pull down menu
function has_selected(menu_obj, message, index_offset)
{
 //checks against value of element not its indexed number
 if(menu_obj.options.selectedIndex > index_offset && menu_obj.options[menu_obj.options.selectedIndex].value != " ") return true;
 return validate_alert(menu_obj, message);
}

//FUNCTION-- checks if value is numeric between 0 and 9
function is_numeric(text_obj)
{
 for(var i=0;i<text_obj.value.length;i++)
 {
  if(isNaN(text_obj.value.charAt(i))) return validate_alert(text_obj, "Sorry, this field only excepts numeric values\n\n" + "Illegal character: " + text_obj.value.charAt(i));
 }
 return true;
}

//FUNCTION-- displays alert message, focus then select form object and returns false
function validate_alert(form_obj, message) 
{
 alert(message);
 form_obj.focus( ); //must be set first before using select() below
 //select() is a method of password, text and textarea objects only
 if(form_obj.type=="text" || 
    form_obj.type=="textarea" || 
    form_obj.type=="password") form_obj.select( );
 return false;
}

//FUNCTION-- focuses on a form object
function object_focus(form_obj)
{
 form_obj.focus( );
}

//FUNCTION-- selects a form object, select() is a method of password, text and textarea objects only
function object_select(form_obj)
{
 //focus on the form object must be set first before calling select()
 if(form_obj.type=="text" || form_obj.type=="textarea" || form_obj.type=="password") form_obj.select( );
}
//-- functions end -------------------------------------------------------------

