<!--
//Developer unknown
//Customized by Manuel Chagoyan
//Lemoore, CA 93245
/* *************************************************************
* set some global variables that will be checked later         *
***************************************************************/
var msg = "";                 // an output message
var missing = "";             // for missing required fields
var invNum = "";              // for invalid numeric fields
var outOfRange = "";          // less than min or more than max
var invZIP = "";              // for invalid zip codes
var invPhone = "";            // for invalid phone numbers
var invState = ""; 			  // for invalid state fields
var invEmailAddress = "";     // for invalid emails
/* *************************************************************
* The main validation function, calls other sub-functions      *
***************************************************************/
function validate(frm)
  {

  for(i=0; i<frm.elements.length; i++)            	// loop through form fields (elements)
    {
    var el = frm.elements[i]; 						// set a variable equal to the first form field (element)
	
    if(el.required)                               	// if field (element) has required property, test it!
      {                                           	// test to see if field is empty
      if(isEmpty(el))								// if the function comes back true update the missing variable
        {
        missing += "\n   - " + el.id + " is a required field";
			//update form field
			displayError(el.id, "red");				//	call the display error function to update the form label to red
        }
		else
		{//update form field
			displayError(el.id, "white");			//	else turn the form label back to white 
		}
      }
	if(el.numeric)									//	if the field (element) must be a number test it!
      {
      if(notNumeric(el))							//	if it isn't a number
        {
        invNum += "\n   - " + el.id + " must be a number";	//	update the invNum variable 
			//update form field
			displayError(el.id, "red");				// call the display error function to update the form label to red
        }
      }
    if(el.minVal)
      {
      if(parseFloat(el.value) <= el.minVal)
        {
        outOfRange +=  "\n   - " + el.id + " must be larger than " + el.minVal + ", you entered " + el.value;
			//update form field
			displayError(el.id, "red");				
        }
      }
    if(el.maxVal)
      {
      if(parseFloat(el.value) >= el.maxVal)
        {
        outOfRange +=  "\n   - " + el.id + " must be smaller than " + el.maxVal + ", you entered " + el.value
			//update form field
			displayError(el.id, "red");				
        }
      }
    if(el.zip && el.value.length !=0)
      {
      if(invalidZIP(el))
        {
        invZIP += "\n  - " + el.value + " is not a valid zip code";
			//update form field
			displayError(el.id, "red");				
        }
      }
   if(el.phone && el.value.length !=0)
      {
      if(invalidPhone(el) || el.value.length > 14)
        {
        invPhone += "\n  - " + el.value + " is not a valid phone number";
			//update form field
			displayError(el.id, "red");				
        }
      }
	if(el.email)	
      {
   		 if(invalidEmailAddress(el))
        {
        invEmailAddress += "\n  - " + el.value + " is not a valid email address";
			//update form field
			displayError(el.id, "red");				
       	}
      }
  	if(el.state && el.value.length != 0)
      {
      if(invalidState(el))
        {
        invState += "\n  - " + el.value + " is not a valid two-letter state abbreviation";
			//update form field
			displayError(el.id, "red");				
        }
      }
    }
  
  // build output message
  if(missing.length !=0 || invNum.length != 0 || outOfRange.length != 0 || invZIP.length != 0 || invPhone.length != 0 || invEmailAddress.length !=0 || invState.length != 0)
    {
    if(missing.length !=0)
      {
      msg += "\n\nThe following required fields are missing:";
      msg += missing;
      }
    if(invNum.length !=0)
      {
      msg += "\n\nYou entered incorrect numeric data in these fields:";
      msg += invNum;
      }
    if(outOfRange.length !=0)
      {
      msg += "\n\nYou entered out-of-range data in these fields:";
      msg += outOfRange;
      }
    if(invZIP.length !=0)
      {
      msg += "\n\nYou entered an incorrect zip code";
      msg += invZIP;
      }
    if(invEmailAddress.length !=0)
      {
      msg += "\n\nYou entered an incorrect email address";
      msg += invEmailAddress;
      }	  
    if(invPhone.length !=0)
      {
      msg += "\n\nYou entered an incorrect phone number";
      msg += invPhone;
      }
    if(invState.length !=0)
      {
      msg += "\n\nYou entered an incorrect state abbreviation";
      msg += invState;
      }
    errMsg(msg);           // call the output function to send the message
    msg = ""; missing = ""; invNum = ""; invZIP = ""; invPhone = ""; invState = "" ; outOfRange = "" ; invEmailAddress = "";// reset all our variables
    return false;
    }
  else
    {
    return true;
    }
  }

/* *************************************************************
* Sub-functions follow from here to end of file                *
* All sub-functions return true if field is of invalid         *
* format and false if they are valid entries                   *
***************************************************************/
function isEmpty(field)
  {
  str = field.value;
  if(str == "") 
  // make sure not to put a space between those quotes
    {
    return true;
    }
  else
    {
    for(j=0; j<str.length; j++)
      {
      if(str.charAt(j) != " ")
      // make sure to put a space between those quotes!
        {
        return false;
        }
      }
    }

  return true;
  }

function notNumeric(field)
  {
  var errCount = 0;
  var numdecs = 0;                    // number of decimal points
  for(j=0;j<field.value.length;j++)
    {
    c = field.value.charAt(j);        // short hand notation for character at position j
    if((c >= 0 && c <= 9) || c=="." || (j==0 && c == "-"))
      {
      if(c==".") 
        {
        numdecs++;          // count the number of decimal points
        }
      }
    else
      {
      errCount++;                    // if it's none of those, increment error counter
      break;                         // no need to continue looping, it's not a number
      }
    }
  // error if count is non-zero or there are more than one decimal point
  if(errCount > 0 || numdecs > 1)
    {
    return true;
    }
  return false;
  }

function stripNonDigits(str)
  {
  newStr = "";
  for(j=0; j<str.length; j++)
    {
    c = str.charAt(j);
    if(c >= "0" && c <= "9")
      {
      newStr += c;
      }
    }
  return newStr;
  }

function invalidZIP(field)
  {
  var zipcode = field.value;
  if(zipcode.length == 5 || zipcode.length == 9)
    {
    var subZip = stripNonDigits(zipcode);
    if(subZip.length == zipcode.length)
      {
      return false;
      }
    else
      {
      return true;
      }
    }
  else if(zipcode.length == 10 && (zipcode.charAt(5) == "-" || zipcode.charAt(5) == " "))
    {
    subZip = zipcode.substring(0,5) + zipcode.substring(6,10);
    subZip = stripNonDigits(subZip);
    if(subZip.length == 9)
      {
      return false;
      }
    else
      {
      return true;
      }
    }
  return true;
  }

function invalidEmailAddress(field)
{
	var emailaddress = field.value  ; // create a new variable named emailaddress 
	AtPos = emailaddress.indexOf("@") ; //Determine position of @ symbol.  If not found AtPos = -1
	DotPos = emailaddress.lastIndexOf(".") ; //Determine position of . symbol.  If not found DotPos = -1

	if (AtPos == -1 || DotPos == -1) //There is not @ or dot
	{
		return true;
    }
	if (DotPos < AtPos) //The dot is before the @
	{
		return true;
	}
	if (DotPos - AtPos == 1) //the dot is right after the period 
	{
		return true;
	}	
	else
	{
		return  false;
	}
 }


function invalidPhone(field)
  {
  newStr = stripNonDigits(field.value);
  if(newStr.length == 10)
    {
    return false;
    }
  return true;
  }

function invalidState(field)
  {
  var STATES = "AL/AK/AZ/AR/CA/CO/CT/DE/DC/FL/GA/HI/ID/IL/IN/IA/KS/LA/ME/MD/MA/MI/MN/MS/MO/MT/NV/NH/NJ/NM/NY/NC/ND/OH/OK/OR/PA/PR/RI/SC/TN/TX/UT/VT/VA/WA/WV/WI/WY";
  var newStr = field.value.toUpperCase();
  if(STATES.indexOf(newStr) == -1 || newStr.indexOf("/") != -1 || newStr.length != 2)
    {
    return true;
    }
  return false;
  }

function errMsg(msg)
  {
var theMsg2 = "You entered some incorrect values into the form. ";
  theMsg2 += "Please correct your entries then re-submit the form.\n";
 theMsg2 += "";
 theMsg2 += msg;
 theMsg2 += "\n\n";
 //alert(theMsg);
(msg);
 
 
 var theMsg = "Please correct the red entries.<br /> <br />";
 
 
 //update form field
	errorMessage('message', theMsg);

	//errorMessage('rederrormessage', theMsg2);
  }


//-->
