
// Validation routines for IMCCSRA Herman van Beek 13 Aug 2001
// Updated:  3 Aug 2010 - added a few more stringent regular expression based tests.
// Updated:  8 Dec 2010 - concatenated all errors messages into a single alert message instead of individual ones

function validateContactInfo(theForm)
  {
   var msg = "";
   var ok =  true;
   if (typeof(theForm.realname_f) != 'undefined') {
      if (theForm.realname_f.value == "") {
	 msg = msg + "Please specify your first name\n";
	 ok = false;
        }
     }
   if (typeof(theForm.realname_l) != 'undefined') {
      if (theForm.realname_l.value == "") {
	 msg = msg + "Please specify your last name\n";
	 ok = false;
        }
     }
   if (typeof(theForm.street) != 'undefined') {
      if (theForm.street.value == "") {
	 msg = msg + "Please specify your street address\n";
	 ok = false;
        }
     }
   if (typeof(theForm.city) != 'undefined') {
      if (theForm.city.value == "") {
	 msg = msg + "Please specify the city where you live\n";
	 ok = false;
        }
     }
   if (typeof(theForm.state) != 'undefined') {
      if (theForm.state.value == "") {
	 msg = msg + "Please enter the state where you live\n";
	 ok = false;
        }
       else 
        {
         theForm.state.value = theForm.state.value.toUpperCase();
         if (!isUSState(theForm.state.value)) {
            ok = false;
           }
        }
     }
   if (typeof(theForm.zip) != 'undefined') {
      //  DESCRIPTION: Validates that a string is a United States zip code in 5 digit or zip+4 format. 99999 or 99999-9999
      //  rgxZIP_US = /(?:\d{5}(?:-\d{4})?)/                
      //  rgxZip_CA = /(?:[ABCEGHJ-NPRSTV-Z]\d[ABCEGHJ-NPRSTV-Z] \d[ABCEGHJ-NPRSTV-Z]\d)/       // (Canadian postal codes do not use the following letters: D, F, I, O, Q, and U)
      
      theForm.zip.value = theForm.zip.value.toUpperCase();
      var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)|(^[ABCEGHJ-NPRSTV-Z]\d[ABCEGHJ-NPRSTV-Z][- |]\d[ABCEGHJ-NPRSTV-Z]\d$)/;
      if (!objRegExp.test(theForm.zip.value)) {
	 msg = msg + "Please enter a valid Canadian postal code or US zip code; e.g., H6W 4R1 or 99999 or 99999-9999\n";
	 ok = false;
        }
     }
   if (typeof(theForm.phone) != 'undefined') {
      // DESCRIPTION: Validates that a string contains valid US phone pattern.  Ex. (999) 999-9999 or (999)999-9999
      // var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
      var objRegExp  = /^[1-9]\d{2}\-\d{3}\-\d{4}$/;
      if (!objRegExp.test(theForm.phone.value)) {
	 msg = msg + "Please specify your 10 digit telephone number using following format: 999-999-9999\n";
	 ok = false;
        }
     }
   if (typeof(theForm.office) != 'undefined') {
      // DESCRIPTION: Validates that a string contains valid US phone pattern.  Ex. (999) 999-9999 or (999)999-9999
      // var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
      if (!theForm.office.value == "") {
        var objRegExp  = /^[1-9]\d{2}\-\d{3}\-\d{4}$/;
        if (!objRegExp.test(theForm.office.value)) {
  	 msg = msg + "Please specify your 10 digit office telephone number using following format: 999-999-9999\n";
  	 ok = false;
          }
        }
     }
   if (typeof(theForm.fax) != 'undefined') {
      // DESCRIPTION: Validates that a string contains valid US phone pattern.  Ex. (999) 999-9999 or (999)999-9999
      // var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
      if (!theForm.fax.value == "") {
        var objRegExp  = /^[1-9]\d{2}\-\d{3}\-\d{4}$/;
        if (!objRegExp.test(theForm.fax.value)) {
  	 msg = msg + "Please specify your 10 digit fax number using following format: 999-999-9999\n";
  	 ok = false;
          }
        }
     }
   if (typeof(theForm.cell) != 'undefined') {
      // DESCRIPTION: Validates that a string contains valid US phone pattern.  Ex. (999) 999-9999 or (999)999-9999
      // var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
      if (!theForm.cell.value == "") {
        var objRegExp  = /^[1-9]\d{2}\-\d{3}\-\d{4}$/;
        if (!objRegExp.test(theForm.cell.value)) {
  	 msg = msg + "Please specify your 10 digit cell phone number using following format: 999-999-9999\n";
  	 ok = false;
          }
        }
     }
   if (typeof(theForm.email) != 'undefined') {
      if (!theForm.email.value == "") {
         if (!emailCheck(theForm.email.value)) {
           ok = false;
          } 
        }
     }
   if ( ! ok ) alert (msg);
   return (ok);  
  }
