
var fadeToColor = "#EEEEEE";
var fadeBackColor = "#77a5b5";
var fadeTime = 200;

function validateForm( form ) {
    var err_mess = "";
	

	// checks a text field (text,textarea) for: empty string
	// ( class name, form value , err message for empty string , min numbers of chars (0 for no minimum) , err message for too few chars)
	err_mess += isEmpty("", form.input_FullName.value , "Please enter your full name." , 2 , "Please enter your full name.");

	// checks an email address for: empty string , validity , illegal characters
	// ( class name, form value )
    err_mess += checkEmail("", form.input_Email.value );
	
	// checks a text field (text,textarea) for: empty string
	// ( class name, form value , err message for empty string , min numbers of chars (0 for no minimum) , err message for too few chars)
	err_mess += isEmpty("", form.input_Comment.value , "Please enter a comment." , 2 , "Please enter a comment.");

	// checks one or two password for: empty string , matching passwords , minimum length 
	// ( class name, number of passwords (1 or 2) ,  form password 1 , form password 2 , minimum length )
	err_mess += checkPhone("", form.input_Phone.value );
	



//	// checks an email address for: empty string , validity , illegal characters
//	// ( class name, form value )
//    err_mess += checkEmail("", form.email.value );

//	// checks a text field (text,textarea) for: empty string
//	// ( class name, form value , err message for empty string , min numbers of chars (0 for no minimum) , err message for too few chars)
//	err_mess += isEmpty("", form.username.value , "Please enter a username." , 0 , "");

//	// checks one or two password for: empty string , matching passwords , minimum length 
//	// ( class name, number of passwords (1 or 2) ,  form password 1 , form password 2 , minimum length )
//	err_mess += checkPasswords("", 2 , form.password1.value , form.password2.value , 5 );

//	// checks a phone number for: empty string , all numbers , correct length 10 digits
//	// ( class name, form value )
//	err_mess += checkPhone("", form.phone.value );

//	// checks a time for: empty string , validity , ":"
//    err_mess += checkTime( form.time1.value );

//	// checks a zipcode for: empty string , all numbers
//    err_mess += checkZipcode( form.zipcode.value );
	
//	// checks a drop down box for: null value
//	err_mess += checkDropdown( form.dropdown.selectedIndex , "Please select a choice from the drop down list.");
	
//	// checks a radio button for: null value
//	for (i=0, n=form.radio.length; i<n; i++) {
//        if (form.radio[i].checked) {
//            var radiovalue = form.radio[i].value;
//           	break;} 
//    }	
//	err_mess += checkRadioORCheckbox( radiovalue , "Please select a radio button.");

//	// checks a check box for: null value
//	for (i=0, n=form.checkbox.length; i<n; i++) {
//        if (form.checkbox[i].checked) {
//            var checkboxvalue = form.checkbox[i].value;
//           	break;} 
//    }	
//	err_mess += checkRadioORCheckbox( checkboxvalue , "Please select a check box.");






// ************************************ DO NOT DELETE ANYTHING BELOW THIS LINE ************************************

if (err_mess != "") {
       alert(err_mess);
       return false;
    }
	return true;
}



// ------------------------------------------------------------------------------------------------------ //
// --------------------------------------- check an email address --------------------------------------- //
// ------------------------------------------------------------------------------------------------------ //

	function checkEmail(strClassName, strEmail) {
		var error = "";
		// check the empty string
		if (strEmail == "") {
			error = "Please enter an email address.\n";
			if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
			return error;
		}
		// check the syntax "*@*.ab" or "*@*.abc"
		var filter=/^.+@.+\..{2,3}$/;
		if (!(filter.test(strEmail))) { 
		   error = "Please enter a valid email address.\n";
			if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
		   return error;
		}
		// check for invald characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strEmail.match(illegalChars)) {
		   error = "The email address contains illegal characters.\n";
			if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
		   return error;
		}
		//return error;  
		
		 if (strClassName != "") { $(strClassName).animate( { color: fadeBackColor }, fadeTime ); }
		else { return error; }
	}



// ------------------------------------------------------------------------------------------------------ //
// ----------------------------- check a text element for an empty string ------------------------------- //
// ------------------------------------------------------------------------------------------------------ //

	function isEmpty(strClassName, strText , strMessage , intMinChars , strMinMessage) {
		var error = "";
		// check for the empty string
		if (strText == "") {
			error = strMessage.concat("\n");
			if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
			return error;
		}
		// check for minimum number of characters
		if (intMinChars != 0) {
			if (strText.length < intMinChars) {
				error = strMinMessage.concat("\n");
				if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
				return error;
			}
		}
		 if (strClassName != "") { $(strClassName).animate( { color: fadeBackColor }, fadeTime ); }
		else { return error; }
	}
	
	

// ------------------------------------------------------------------------------------------------------ //
// --------------------------------- check a password and it's retype ----------------------------------- //
// ------------------------------------------------------------------------------------------------------ //

	function checkPasswords(strClassName, intPassNum , strPassword1 , strPassword2 , intMinChars) {
		var error = "";
		// if there are two occurrences of the password, check for the empty strings
		if (intPassNum == 2) {
			if ((strPassword1 == "") && (strPassword2 == "")) {
				error = "Please enter a password and then retype it.\n";
				if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
				return error;
			}
		}
		// check for the empty string
		if (strPassword1 == "") {
			error = "Please enter a password.\n";
			if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
			return error;
		}
		// check for the minimum number of characters
		if (strPassword1.length < intMinChars) {
		   error = "The password is too short.\n";
			if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
		   return error;
		}
		// if there are two occurrences of the password, check for the 2nd empty dtring
		if (intPassNum == 2) {
			if (strPassword2 == "") {
				error = "Please retype the password.\n";
				if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
				return error;
			}
		}
		// if there are two occurrences of the password,
		if (intPassNum == 2) {
			if (strPassword1 != strPassword2) {
				error = "The passwords don't match.\n";
				if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
				return error;
			}
		}
		 if (strClassName != "") { $(strClassName).animate( { color: fadeBackColor }, fadeTime ); }
		else { return error; }
	}   




// ------------------------------------------------------------------------------------------------------ //
// -------------------------------------- check an phone number ----------------------------------------- //
// ------------------------------------------------------------------------------------------------------ //

function checkPhone(strClassName, strPhone) {
	var error = "";
	// check for the empty string

	if (strPhone == "") {
   		error = "Please enter a phone number.\n";
		if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
		return error;
	}
	// check for all numbers after stripping string
	var stripped = strPhone.replace(/[\(\)\.\-\ ]/g, ''); 
    if (isNaN(parseInt(stripped))) {
       error = "Please enter a valid phone number.\n";
	   if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
	   return error;
    }
	// check for the proper length of 10 or 11 digits
    if (!(stripped.length == 10 )) {
		error = "The phone number is the wrong length. Please include the area code.\n";
		if (strClassName != "") { $(strClassName).animate( { color: fadeToColor }, fadeTime ); }
		return error;
    } 
	if (strClassName != "") { $(strClassName).animate( { color: fadeBackColor }, fadeTime ); }
	else { return error; }
}
// ----------------------------- check an phone number ----------------------------- //






// ----------------------------- check a time ----------------------------- //
function checkTime(strTime) {
	var error = "";
	//check for the empty string
	if (strTime == "") {
   		error = "Please enter a time.\n";
		return error;
	}
	// check for all numbers after stripping string
	var stripped = strTime.replace(/[\:\(\)\.\-\ ]/g, ''); 
    if (isNaN(parseInt(stripped))) {
       error = "Please enter a valid time.\n";
	   return error;
    }
	// check for a ":"
	if (strTime.search(/:/) == -1) {
		error = "Please enter a valid time.\n";
		return error;
	}
	// check for "1-12" : "00-59" and that the minute is in a double character format (00-59)
	var hour;
	var minute;
	hour = strTime.substr(0,strTime.search(/:/));
	minute = strTime.substr(strTime.search(/:/)+1,2);
	if ( parseInt(hour) > 12 || parseInt(hour) < 1 || parseInt(minute) > 59 || parseInt(minute) < 0 || minute.length != 2) {		
		error = "Please enter a valid time.\n";
		return error;
    } 
	return error;
}
// ----------------------------- check a time ----------------------------- //







// ----------------------------- check a zipcode ----------------------------- //
function checkZipcode(strZipcode) {
	var error = "";
	var valid = "0123456789-";
	var hyphencount = 0;
	//check for the empty string
	if (strZipcode == "") {
   		error = "Please enter a zip code.\n";
		return error;
	}
	if (strZipcode.length!=5 && strZipcode.length!=10) {
		error = "Please enter a 5 digit or 5 digit+4 zip code.\n";
		return error;
	}
	for (var i=0; i < strZipcode.length; i++) {
		temp = "" + strZipcode.substring(i, i+1);
		if (temp == "-") hyphencount++;
			if (valid.indexOf(temp) == "-1") {
				error = "The zip code contains illegal characters.\n";
				return error;
			}
			if ((hyphencount > 1) || ((strZipcode.length==10) && ""+strZipcode.charAt(5)!="-")) {
				error = "Please enter a 5 digit or 5 digit+4 zip code.\n";
				return error;
   			}
		}
	return error;
}
// ----------------------------- check a zipcode ----------------------------- //





// ----------------------------- check a radio button or group of ----------------------------- //
function checkRadioORCheckbox(intRadio , strMessage) {
	var error = "";
	// check for a null value
   	if (!(intRadio)) {
       error = strMessage.concat("\n");
    }
	return error;
}
// ----------------------------- check a radio button or group of ----------------------------- //




// ----------------------------- check a drop down list ----------------------------- //
function checkDropdown(intDropdown , strMessage) {
	var error = "";
	// check for a null value
    if (intDropdown == 0) {
    	error = strMessage.concat("\n");
    }    
	return error;
}
// ----------------------------- check a drop down list ----------------------------- //


