/***********************************************************************
 Form Validation Libarary includes the following:
   removeLeadingBlanks(textString)
      strips off leading white space
   removeTrailingBlanks(textString)
      strips off traling white space
   checkNonBlank(textString)
      returns false if a field is empty after removing whitespace
   checkRadio(radioList)
      verifies at least one button in a radio field is selected
   checkTextField (textString)
      accepts letters, numbers, the characters [-,./'] and spaces
   checkNameField(textString)
      accepts letters, numbers, the characters [-,.'] and spaces
   checkZipField(textString)
      accepts xxxxx and xxxxx-xxxx, where x is 0-9
   checkNumberField(textString)
      accepts digits and the characters [.,-]
   checkPhoneField(textString)
      accepts a 10-digit phone number compliant with the north
      american dialing plan in one of the following formats:
      npxxxxxxx, (npx)xxx-xxxx or npx-xxx-xxxx.
      where n is 2-9, p is 0-8 and x is 0-9.
   checkEMail(textString)
      Accepts elements (before the '@') that starts with a letter
         or number, followed by any number of alpha-numeric
         characters and the characters [-._]
      Accepts domains (after the '@') in one of the following formats:
      - String of alpha-numeric characters and [-.],
      - IP address of the form [<num>.<num>.<num>.<num>],
      - numeric domain of the form #<num>.
 ***********************************************************************/

/*==========================================================*/
/* Globals - including regular expression objects           */

var debug     = false; /* turns on extra alert text to aid debugging */

var nameRE    = /^[-A-Za-z ,.']+$/;
var textRE    = /^[-A-Za-z0-9 ,.&#\/']+$/;
var numberRE  = /^[-0-9.,]+$/;
var zipRE     = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
var phoneRE   = /(^[2-9][0-8][0-9][2-9]\d{6}$)|(^[(][2-9][0-8][0-9][)][2-9]\d{2}[-]\d{4}$)|(^[2-9][0-8][0-9][-][2-9]\d{2}[-]\d{4}$)/;
var domainRE  = /(^[-A-Za-z0-9.]+$)|(^\[[0-9]+.[0-9]+.[0-9]+.[0-9]\]$)|(^#[0-9]+$)/;
var elementRE = /^[A-Za-z0-9]+[-A-Za-z0-9._]*$/;

/*==========================================================*/
/* REMOVE LEADING BLANKS                                    */
/* Remove leading blanks from a string.                     */

function removeLeadingBlanks(textString) {
   if ( debug ) { alert ("removeLeadingBlanks for: " + textString.value); };

   var begIdx = 0;
   var endIdx = textString.value.length - 1;

   while ( textString.value.charAt(begIdx) == " ") { begIdx++; }

   if ( endIdx < begIdx ) {
      textString.value = "";
   }
   else {
      textString.value = textString.value.substring(begIdx, endIdx+1);
   }
}

/*==========================================================*/
/* REMOVE TRAILING BLANKS                                   */
/* Remove trailing blanks from a string                     */

function removeTrailingBlanks(textString) {
   var begIdx = 0;
   var endIdx = textString.value.length - 1;

   while ( textString.value.charAt(endIdx) == " ") { endIdx--; }

   if ( endIdx < begIdx ) {
      textString.value = "";
   }
   else {
      textString.value = textString.value.substring(begIdx, endIdx+1);
   }
}

/*==========================================================*/
/* CHECK NON BLANK                                          */
/* Verify a string is not empty.                            */

function checkNonBlank(textString) {

   if ( (textString.value != "") && (textString.value.length > 0) ) { removeLeadingBlanks (textString); }
   if ( (textString.value != "") && (textString.value.length > 0) ) { removeTrailingBlanks(textString); }

   if ( (textString==null) || (textString.value.length < 1) ) {
      return false;
   }
   else {
      return true;
   }
}

/*==========================================================*/
/* CHECK RADIO -                                            */
/* Verify at least one button is selected in a radio field. */

function checkRadio(radioTag, radioLength) {

   var radioID;
   var radioObj;

   if ( debug ) { alert("Inside checkRadio, radioTag = " + radioTag + ", radioLength = " + radioLength); };

   var buttonCount = 0;

   for (var i = 1;  i <= radioLength; i++) {
      radioID = radioTag + i;
      radioObj = document.getElementById(radioID);
/*    alert("checkRadio for loop, i = " + i + " radioName = " + radioObj.name); */
      if (radioObj.checked == true ) { buttonCount++ };
   }

   return ( buttonCount > 0);
}

/*==========================================================*/
/* CHECK FIELD -                                            */
/* Strip off leading and trailing blanks and verify the     */
/* passed-in string using the passed-in regular expression. */

function checkField(textString, regularExpression) {

   if ( debug ) { alert ("Check Any Field for string: " + textString.value + " and expression: " + regularExpression); }

   if ( !checkNonBlank(textString) ) {
      if ( debug ) { alert ("NON-BLANK validation FAILED for: " + textString.value); }
      return false;
   }

   if ( !regularExpression.test(textString.value) ) {
      return false;
   }

   return true;
}

/*==========================================================*/
/* CHECK TEXT FIELD - Verify field is a valid text string.  */

function checkTextField (textString) {

   if ( !checkField(textString, textRE) ) {
      if ( debug ) { alert ("TEXT validation FAILED for: " + textString.value); }
      return false;
   }
   return true;
}

/*==========================================================*/
/* CHECK NAME FIELD - Verify field is a valid name.         */

function checkNameField(textString) {

   if ( !checkField(textString, nameRE) ) {
      if ( debug ) { alert ("NAME validation FAILED for: " + textString.value); }
      return false;
   }

   return true;
}

/*==========================================================*/
/* CHECK NUMBER FIELD - Verify field is a valid number.     */

function checkNumberField (textString) {

   if ( !checkField(textString, numberRE) ) {
      if ( debug ) { alert ("NUMBER validation FAILED for: " + textString.value); }
      return false;
   }
   return true;
}

/*==========================================================*/
/* CHECK ZIP CODE - Verify field is a valid zip code.       */

function checkZipField(textString) {

   if ( !checkField(textString, zipRE) ) {
      if ( debug ) { alert ("ZIP validation FAILED for: " + textString.value); }
      return false;
   }

   return true;
}

/*==========================================================*/
/* CHECK PHONE - Verify field is a valid phone number.      */

function checkPhoneField(textString) {

   if ( debug ) { alert ("Phone validation for: " + textString.value); }

   if ( !checkNonBlank(textString) ) {
      if ( debug ) { alert ("NON-BLANK PHONE validation FAILED for: " + textString.value); }
      return false;
   }

   if ( !phoneRE.test(textString.value) ) {
      if ( debug ) { alert ("PHONE validation FAILED for: " + textString.value); }
      return false;
   }

   return true;
}

/*==========================================================*/
/* CHECK EMAIL                                              */
/* Verify the passed-in field is a valid e-mail address.    */

function checkEMail(textString) {

   /* Validation is very basic.  Anything before the first @ is valid, as long as
      it's one or more characters.  After the @ can only contain letters, numbers,
      hypens("-"), dots ("."), hash ("#") or square brackets ("[" and "]").
      For a full spec on valid e-mails see http://www.faqs.org/rfcs/rfc821.html.
   */

   var atIdx    = 0;
   var element  = new String("");
   var domain   = new String("");

   if ( !checkNonBlank(textString) ) {
      return false;
   }

   /* AT:: Verify there is an "@" in the string and save its position */
   atIdx = textString.value.indexOf("@");

   if ( atIdx <= 1 ) { return false; }

   /* ELEMENT:: Extract the string before the "@" and verify it's valid */
   element = textString.value.substring(0,atIdx);

   /* Verify the string length is at least one.  If the string is quoted anything   */
   /* is valid so don't check it.  Otherwise verify only valid characters are used. */

   if ( element.length < 1 ) {
      if ( debug ) { alert ("ELEMENT length FAILED for: " + element.value); }
      return false;
   }

   if ( !( element.charAt(0) == "\"" ) && ( element.charAt(element.length-1) == "\"" )) {
      if ( !elementRE.test(element) ) {
         return false;
      }
   }

   /* DOMAIN:: Extract the string after the "@" and verify it's valid */
   domain = textString.value.substring( (atIdx+1), (textString.value.length));

   if ( domain.length < 1 ) {
      return false;
   }

   if ( !domainRE.test(domain.value) ) {
      return false;
   }

   return true;
}