// General form validation functions

var std_bg = 'White';
var err_bg = '#FF6';

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = err_bg; 

        // Gate this instruction. 
        if (reg_form_empty_error == 0) {
          error = "Please supply the required information as indicated " +
                  "by the yellow background on the form.\n";
          reg_form_empty_error = 1;
        }
    } else {
        fld.style.background = std_bg;
    }
    return error;  
}

function validateNewPassword(fld1, fld2) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld1.value == "") {
        fld1.style.background = err_bg;
        fld2.style.background = err_bg;
        error = "Please enter a password.\n";
    } else if ((fld1.value.length < 6) || (fld1.value.length > 16)) {
        error = "Password must be between 6 and 16 characters.\n";
        fld1.style.background = err_bg;
        fld2.style.background = err_bg;
    } else if (illegalChars.test(fld1.value)) {
        error = "Password may only contain letters and numbers.\n";
        fld1.style.background = err_bg;
        fld2.style.background = err_bg;
    } else if (!((fld1.value.search(/(a-z)+/)) && (fld1.value.search(/(0-9)+/)))) {
        error = "Password must contain at least one numeral.\n";
        fld1.style.background = err_bg;
        fld2.style.background = err_bg;
    } else if (fld1.value != fld2.value) {
    	error = "Passwords to not match!\n"
        fld1.style.background = err_bg;
        fld2.style.background = err_bg;
    } else {
        fld1.style.background = std_bg;
        fld2.style.background = std_bg;
    }
   return error;
}   


function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var	tfld = trim(fld.value); // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

	if (fld.value == "") {
  	  fld.style.background = err_bg;
	  error = "Please enter an email address.\n";
	} 
	else if (!emailFilter.test(tfld)) { //test email for illegal characters
	  fld.style.background = err_bg;
	  error = "Please enter a valid email address.\n";
	} 
	else if (fld.value.match(illegalChars)) {
	  fld.style.background = err_bg;
	  error = "Email address contains illegal characters.  Please try again.\n";
	} 
	else {
	  fld.style.background = std_bg;
	}
	return error;
}

function validateEmail2(fld1, fld2) {
	var error="";
	var tfld1 = trim(fld1.value);
	var tfld2 = trim(fld2.value);
	
	// Validate the first e-mail field first.  If that is good then
	// check to see that the second is a match.
	
	error = validateEmail(fld1);
	
	if (error == "") {
	  if (tfld1 != tfld2) {
		fld1.style.background = err_bg;
	    fld2.style.background = err_bg;
	    error = "Email addresses do not match!  Please try again.\n";
	  }
	  else {
		fld1.style.background = std_bg;
		fld2.style.background = std_bg;
	  }
	}
	else {
		error += validateEmail(fld2);
	}
	
	return error;
}


function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Please enter a phone number.\n";
        fld.style.background = err_bg;
    } else if (isNaN(parseInt(stripped))) {
        error = "Phone number contains illegal characters.  Please try again.\n";
        fld.style.background = err_bg;
    } else if (!(stripped.length > 9)) {
        error = "Phone number must contain at least 10 digits.  Please try again.\n";
        fld.style.background = err_bg;
    }
    else {
    	fld.style.background = std_bg;
    }
   
    return error;
}

function validateZip(fld) {
    var error = "";

   if (fld.value == "") {
        error = "Please enter a zip (postal) code.\n";
        fld.style.background = err_bg;
    } else if (fld.value.length < 5) {
        error = "Zip code must contain at least 5 digits.  Please try again.\n";
        fld.style.background = err_bg;
    }
    else {
    	fld.style.background = std_bg;
    }
   
    return error;
}

/* See http://www.codewalkers.com/c/a/Miscellaneous-Code/JavaScriptPHP-US-phone-number-validation */
////////////////////////////////////////
//JavaScript function to validate US
//phone number:
//
//(c) 2003
//No restrictions have been placed on the use
//of this code
//
//Updated Friday Jan 9 2004 to optionally ignore
//the area code:
//
//Input: a single string parameter and an
//optional boolean variable (default=true)
//Output: boolean true(1) or false(0)
//
//The function will return true for any
//alphanumeric string with the following
//sequence of characters:
//any number of spaces [optional], a single
//open parentheses [optional], any number of
//spaces [optional], 3 digits (area
//code), any number of spaces [optional], a
//single close parentheses [optional], a single
//dash [optional], any number of spaces
//[optional], 3 digits, any number of spaces
//[optional], a single dash [optional], any
//number of spaces [optional], 4 digits, any
//number of spaces [optional].
//
////////////////////////////////////////
function check_usphone(phonenumber,useareacode)
{
if(!useareacode)useareacode=1;
if((phonenumber.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) && ((useareacode!=1) && (phonenumber.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))) return false;
return true;
} 

