<!--

 // This is an include file for client-side forms validation functions
 // It includes the following client-side functions:
 //  - isEmpty
 //  - isChecked
 //  - isValidEmailAddress
 //  - isAlpha
 //  - isAlphaNumeric
 //  - isAlphaNumericUnderscores
 //  - ContainsValidCharacters
 //  - ContainsInvalidCharacters
 //  - getSelected
 //  - getQueryString
 //  - EraseInvalidCharacters
 //


 // This function determines if strInput is empty:
 //        Example usage: if (isEmpty(form.login.value)) { ...
 //
  function isEmpty(strInput) {
   if (strInput == "" || strInput == null) { return true; }
   return false;
  } //isEmpty


 // This function determines if a single radio button is checked or not:
 //        Example usage: if (!isChecked(form.question2)) { ...
 //
  function isChecked(btnRadio) {
   
   // length = undefined if we only have 1 radio button (why only 1?!):
   if (!btnRadio.length) {
       if (btnRadio.checked) {
           return true;
       }
   }

   for (var i = 0; i < btnRadio.length; i++) {
       if (btnRadio[i].checked) { return true; }
   } // for

   return false;

  } // isChecked

 // This function will attempt to validate an e-mail address, if it is given
 // It assumes that the email field on the form is called "email".
 // It will take 2 parameters: the email address object and true/false if
 //  the field is required.
 //        Example usage:  if (!isValidEmailAddress(form.email, true)) { ...
 //
  function isValidEmailAddress(email, blnRequired) {
   var allValid = true;
   var strInput = email.value;

   // Check if the email address exists and if it is required:
    if (isEmpty(strInput)) {
     if (!blnRequired) { return true; }
     else {
      alert("Please enter your e-mail address.");
      email.focus();
      return false;
     } // else
    } //if

   var strLen = strInput.length;

   // CHECK IF EMAIL ADDRESS HAS A '@' CHARACTER:
     if (strInput.indexOf("@") == -1) {
      alert("The e-mail address you entered was not valid because it does not have a '@'.  Please re-enter your \"e-mail address\".");
      email.focus();
      email.select();
      return false;
     } //if

   // CHECK IF EMAIL ADDRESS HAS A '.' CHARACTER:
     	if (strInput.indexOf(".") == -1) {
       		alert("The e-mail address you entered was not valid because it does not have a '.'.  Please re-enter your \"e-mail address\".");
          email.focus();
          email.select();
          return false;
     	} //if

   // CHECK IF EMAIL ADDRESS STARTS WITH A '.':
     	if (strInput.indexOf('.') == 0) {
       		alert("The e-mail address you entered was not valid because it starts with '.'  Please re-enter your \"e-mail address\".");
         email.focus();
         email.select();
       	 return false;
     	} //if
     	
   // CHECK IF EMAIL ADDRESS STARTS WITH A '@':
     	if (strInput.indexOf('@') == 0) {
       		alert("The e-mail address you entered was not valid because it starts with '@'  Please re-enter your \"e-mail address\".");
         email.focus();
         email.select();
       	 return false;
     	} //if     	

   // Check for any invalid characters in the address:
     	for (var i = 0; i < strLen; i++) {
         	ch = strInput.charAt(i)
         	if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                 	|| (ch == "@") || (ch == ".") || (ch == "_")
               	  	|| (ch == "-") || (ch >= "0" && ch <= "9")) { ; }
         	else { allValid = false; }
     	} //for

     	if (!allValid) {
       		alert("The e-mail address you entered was not valid because it contains an illegal character.  Valid characters are letters, numbers, dashes or underscores.  Please re-enter your \"e-mail address\".");
         email.focus();
         email.select();
       	 return false;
     	} //if

   //It has a . adjacent to the @:
         var atpos=strInput.indexOf('@')
     	if (strInput.charAt(atpos-1)=='.'){
       		alert("The e-mail address you entered was not valid because it contains a '.' adjacent to the '@'  Please re-enter your \"e-mail address\".");
         email.focus();
         email.select();
       	 return false;
     	} // if

         var copy=strInput.substring(atpos+1,strInput.length);
   //It contains more than one @:
  	if (copy.indexOf('@')!=-1){
       		alert("The e-mail address you entered was not valid because it contains more than one '@'.  Please re-enter your \"e-mail address\".");
         email.focus();
         email.select();
       	 return false;
     	}

   // It has a . adjacent to the @:
  	if (copy.indexOf(".")<1){
       		alert("The e-mail address you entered was not valid because it contains a '.' adjacent to the '@'.  Please re-enter your \"e-mail address\".");
         email.focus();
         email.select();
       	 return false;
     	}

   // It ends with a . or an @:
  	if (copy.lastIndexOf(".")+1==copy.length){
       		alert("The e-mail address you entered was not valid because it ends with '.' or an '@'.  Please re-enter your \"e-mail address\".");
         email.focus();
         email.select();
       	 return false;
     	}

   // Invalid top level domain length:
  	copy = strInput.substring(strInput.lastIndexOf('.')+1,strLen);
  	var len = copy.length;
   	if (len == 2 || len == 3 || len == 4) { ; }
   	else {
       	 alert("The e-mail address you entered was not valid since the top-level domain (after the last .) should only be 2 or 3 characters in length.  Please re-enter your \"e-mail address\".");
          email.focus();
          email.select();
       	 return false;
 	} //else

   // It contains two adjacent .:
  	copy = strInput;
  	while (copy.indexOf('.') != -1) {
         copy = copy.substring(copy.indexOf('.') + 1, copy.length);
         if (copy.indexOf('.') == 0) {
       	  alert("The e-mail address you entered was not valid because it contains two adjacent periods.  Please re-enter your \"e-mail address\".");
          email.focus();
          email.select();
       	  return false;
     	 } //if
  	} //while

     	return true;

  } //isValidEmailAddress

 // This function will determine if the date is in a valid format
 // as either "mm-dd-yy", "mm/dd/yy", "mm-dd-yyyy", "mm/dd/yyyy".
 //  Example usage:  if (!isValidDateFormat(form.dob, "Date of Birth", true)) {
 //
  function isValidDateFormat(strDate, strFieldName, blnRequired) {

   var strInput  = strDate.value;
   var blnValid  = true;
   var strTestChar;
   var intLength = strInput.length;

   // Check if the date exists and if it is required:
    if (isEmpty(strInput)) {
     if (!blnRequired) { return true; }
     else {
      alert('Please enter the ' + strFieldName + '.');
      strDate.focus();
      return false;
     } // else
    } //if

   if (intLength != 8 && intLength != 10) { blnValid = false; }
   else {
    if (strInput.charAt(2) != '-' && strInput.charAt(2) != '/') {
     blnValid = false; }
    if (strInput.charAt(5) != '-' && strInput.charAt(5) != '/') {
     blnValid = false; }
    for (var i = 0; i < intLength; i++) {
     strTestChar = strInput.charAt(i);
     if (strTestChar < "0" || strTestChar > "9")
     if (i != 2 && i != 5) { blnValid = false; }
    } //for
   } //else

    if (blnValid == false) {
     alert('The ' + strFieldName + ' field must contain only numbers and be of the format MM-DD-YYYY.');
     strDate.focus();
     strDate.select();
     return false;
    } //if

   return true;

  } //isValidDateFormat


// This will check if a given date is a valid date.  The date must be in the
// format mmddyyyy or mm/dd/yyyy.  It is recommended that isValidDateFormat()
// be applied to the date before using this function.
// Example usage:
//  if(!isValidDate(form.testDate.value)) { ...
//
 function isValidDate(dateToCheck) {
 
   var month = "";
   var date  = "";
   var year  = "";

  // Parse out the date components based on the length of the given date:
   if (dateToCheck.length == 10) {
	month = dateToCheck.substr(0,2);
	date  = dateToCheck.substr(3,2);
	year  = dateToCheck.substr(6,4);
   }
   else if (dateToCheck.length == 8) {
	month = dateToCheck.substr(0,2);
	date  = dateToCheck.substr(2,2);
	year  = dateToCheck.substr(4,4);
   }
   else
	return false;

  // Validate the month:
   if (isNaN(month))
	return false;
   if (month < 1 || month > 12)
	return false;

  // Validate the date:
   var monthMax = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
   var top = monthMax[(month - 1)];
   if (isNaN(date)) {
	return false;
   }
   if (date < 1 || date > top) {
	return false;
   }

  // Validate the year:
   if (isNaN(year)) {
	return false;
   }
   if (year < 1900 || year > 2100) {
	return false;
   }

   // Special case: if date is Feb 29, and this isn't a leap year, it's invalid:
   if (month == 2 && day == 29) {
       if ((year % 4 != 0) || ((year % 100 == 0) && (year % 400 != 0))) { 
           return false; 
       }
   }

  return true;

 } //isValidDate


 // This function will determine if the given input (strInput) contains
 // only letters.  It is useful for validating
 // first_name and last_name input where we don't typically allow reserved or
 // special characters.
 //        Example usage:  if (!isAlpha(form.first_name.value)) { ...
 //
  function isAlpha(strInput) {
   var strValidChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   return ContainsValidCharacters(strInput, strValidChars);
  } //isAlpha


 // This function will determine if the given input (strInput) contains
 // only numbers and letters.  It is useful for validating
 // first_name and last_name input where we don't typically allow reserved or
 // special characters.
 //        Example usage:  if (!isAlphaNumeric(form.first_name.value)) { ...
 //
  function isAlphaNumeric(strInput) {
   var strValidChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
   return ContainsValidCharacters(strInput, strValidChars);
  } //isAlphaNumeric


 // This function will determine if the given input (strInput) contains
 // only numbers, letters and underscores.  It is useful for validating
 // username and password input where we don't typically allow reserved or
 // special characters.
 //  Example usage:  if (!isAlphaNumericUnderscores(form.username.value)) { ...
 //
  function isAlphaNumericUnderscores(strInput) {
   var strValidChars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_';
   return ContainsValidCharacters(strInput, strValidChars);
  } //isAlphaNumericUnderscores


 // This function will determine if the given input (strInput) contains
 // only characters found in the strValidChars parameter passed to it.
  function ContainsValidCharacters(strInput, strValidChars) {
   for (var i=0; i< strInput.length; i++) {
    if (strValidChars.indexOf(strInput.charAt(i)) == -1) { return false; }
   } //for
   return true;
  } //ContainsValidCharacters


 // This function will determine if the given input (strInput) contains
 // only characters found in the strInvalidChars parameter passed to it.
  function ContainsInvalidCharacters(strInput, strInvalidChars) {
   for (var i=0; i< strInput.length; i++) {
    if (strInvalidChars.indexOf(strInput.charAt(i)) != -1) { return true; }
   } //for
   return false;
  } //ContainsInvalidCharacters


 // This function is used to find out which radio button is checked:
 //        Example usage: var choice = getSelected(form.Reference);
 //
  function getSelected(buttonGroup) {
   for (var i = 0; i < buttonGroup.length; i++) {
    if (buttonGroup[i].checked) { return buttonGroup[i].value; }
   } //for
   return false;
  } //getSelected


 // This function will return the QUERY_STRING passed to the page:
 //        Example usage:  var strQS = getQueryString();
 //
  function getQueryString() {
   var location = document.URL.indexOf("?");
   if (location != -1) return(document.URL.substring(location + 1));
   return false;
  } //getQueryString


// This function will delete any invalid characters from an input field
// as the user is typing them.
//  form       - The name of the form containing the field to format
//  fieldName  - The name of the form field to format
//  validChars - A text string containing all the characters that are acceptable
//
// Example usage:
//  onkeyup="EraseInvalidCharacters(this.form.elements['phone'], '0123456789')"
//
function EraseInvalidCharacters(fieldName, validChars) {

 // Only allow this for IE browsers:
  if (navigator.appVersion.indexOf('MSIE') == -1) return false;

 var fieldValue  = fieldName.value;
 var fieldLength = fieldValue.length;
 var newValue	 = "";			// The new field without invalid chars
 var blnInvalid	 = false;		// Track if it is bad or not

 for (var i = 0; i< fieldLength; i++) {
	if (validChars.indexOf(fieldValue.charAt(i)) >= 0)
		newValue += fieldValue.charAt(i);
	else
		blnInvalid = true;
 }

 if (blnInvalid == true)
  	fieldName.value = newValue;	// Set form field to the corrected value

} //EraseInvalidCharacters

function postToPopUp(form) {
 if (!validateComments(form)) {
   return false;
 }

 handle = window.open('','postWindow','width=420,height=150,scrollbars=1')
 //form.submit();

 return true;
}

// This function will validate the comments form.
function validateComments(form) {
 
 if (isEmpty(form.name.value)) {
  alert("Please enter your full name.");
  form.name.focus();
  return false;
 } 

 if (!isValidEmailAddress(form.emailaddr, true)) {
  form.emailaddr.focus();
  return false;
 } 

 if (isEmpty(form.comment.value)) {
  alert("Please enter your comments.");
  form.comment.focus();
  return false;
 } 

 return true;
}

//-->
