
/* *************************************************************
* 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 invPC = "";              // for invalid Postal Codes
var invPhone = "";            // for invalid phone numbers
var invProv = "";            // for invalid province fields
  
/* *************************************************************
* The main validation function, calls other sub-functions      *
***************************************************************/
function validate(frm)
  {
  for(counter=0; counter<frm.elements.length; counter++)            // loop through form elements
    {
    var el = frm.elements[counter];
    if(el.required)                               // if element has required property
      {                                           // test to see if field is empty
      if(isEmpty(el))
        {
        missing += "\n   - " + el.name + " is a required field";
        }
      }
    if(el.numeric)
      {
      if(notNumeric(el))
        {
        invNum += "\n   - " + el.name + " must be a number";
        }
      }
    if(el.minVal)
      {
      if(parseFloat(el.value) <= el.minVal)
        {
        outOfRange +=  "\n   - " + el.name + " must be larger than " + el.minVal + ", you entered " + el.value;
        }
      }
    if(el.maxVal)
      {
      if(parseFloat(el.value) >= el.maxVal)
        {
        outOfRange +=  "\n   - " + el.name + " must be smaller than " + el.maxVal + ", you entered " + el.value
        }
      }
    if(el.phone && el.value.length !=0)
      {
      if(invalidPhone(el))
        {
        invPhone += "\n  - " + el.value + " is not a valid phone number";
        }
      }
    if(el.postal && el.value.length !=0)
      {
      if(invalidPostal(el))
        {
        invPC += "\n  - " + el.value + " is not a valid Postal Code";
        }
      }
    if(el.province && el.value.length != 0)
      {
      if(invalidProv(el))
        {
        invProv += "\n  - " + el.value + " is not a valid two-letter province abbreviation";
        }
      }
    }
  
  // build output message
  if(missing.length !=0 || invNum.length != 0 || outOfRange.length != 0 || invPC.length != 0 || invPhone.length != 0 || invProv.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(invPC.length !=0)
      {
      msg += "\n\nYou entered an incorrect Postal Code";
      msg += invPC;
      }
    if(invPhone.length !=0)
      {
      msg += "\n\nYou entered an incorrect phone number";
      msg += invPhone;
      }
    if(invProv.length !=0)
      {
      msg += "\n\nYou entered an incorrect province abbreviation";
      msg += invProv;
      }
    errMsg(msg);           // call the output function
    msg = ""; missing = ""; invNum = ""; invPC = ""; invPhone = ""; invProv = "" ; outOfRange = "" // 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 invalidPostal(field)
{
 var the_postalcode = field.value.toUpperCase(); 
 //check that the field is 6 or 7 chars long
 if (the_postalcode.length < 6) 
   {
   return true;
   } 
 else //continue checking
   {

	 //take uppercase stripped variable and loop through each char
   
     //1st, 3rd and 5th chars should be letters
    var oddAreLetters = true;
     for (i=0;i<6;i+=2) //loops through odd chars
     {
        if (the_postalcode.charAt(i)<"A" || the_postalcode.charAt(i)>"Z") //char at i is NOT a letter
          { 
			  oddAreLetters = false;
        	  continue;
     	  } // end if the_postalcode
	 } //end for i
  //check 2nd, 4th and 6th chars: these should be digits
  var evenAreDigits = true;
	    for (j=1;j<6;j+=2) //loops through even chars
	    	 {	if(the_postalcode.charAt(j)<"0" || the_postalcode.charAt(j)>"9") //char at j is not a number
		      {
		         evenAreDigits = false;
			    continue;
		      }
		     } //end for j
  //check that proper chars used
    if (oddAreLetters && evenAreDigits) return false;
  else return true;
    }//end the else continue checking
}  //end invalidPostal
function stripNonDigits(str)
  {
  newStr = "";
  for(p=0; p<str.length; p++)
    {
    c = str.charAt(p);
    if(c >= "0" && c <= "9")
      {
      newStr += c;
      }
    }
  return newStr;
  }
function invalidPhone(field)
  {
  newStr = stripNonDigits(field.value);
  if(newStr.length == 10)
    {
    return false;
    }
  return true;
  }

function errMsg(msg)
  { 
  var theMsg = "You entered some incorrect values into the form. ";
  theMsg += "Please correct your entries then re-submit the form.\n";
  theMsg += "____________________________________________________________________";
  theMsg += msg;
  theMsg += "\n____________________________________________________________________\n";
  alert(theMsg);
  }
  
