// JavaScript Document

		function validateFormOnSubmit(theForm) {
			var reason = "";
		  	reason += validateEmpty(theForm.first_name, "your");
		  	reason += validateEmpty(theForm.last_name, "your");
//		  	reason += validateEmpty(theForm.email);
//		  	reason += validateEmpty(theForm.phone_number);
//			reason += validateUsername(theForm.username);
//		  	reason += validatePassword(theForm.pwd);
		  	reason += validateEmail(theForm.email);
//		  	reason += validatePhone(theForm.phone_number);
			reason += validatePhoneTimeAndZone(theForm.phone_number, theForm.time_to_call, theForm.time_zone);
		  	if (reason != "") {
				alert("Some fields need correction:\n\n" + reason);
				return false;
				}
		  return true;
		}
		
		function validatePhoneTimeAndZone(phone, ttc, tz) {
			var error = "";
			if ((phone.value.length != 0) || (ttc.value.length != 0) || (tz.value.length != 0)) {
				error += validatePhone(phone);
				error += validateEmpty(ttc, "the best");
				error += validateEmpty(tz, "your");
			}
			return error;
		}
		
		function validateEmpty(fld, word) {
			var error = "";
				if (fld.value.length == 0) {
						fld.style.backgroundColor = '#f5f179'; 
						fld.style.border='red';
						var field_name = "";
						var char_match = /_/g;
						var char_replace = " ";
						field_name = fld.name.replace(char_match, char_replace);
						error = "\t" + "You didn't enter  " + word + " " + field_name.toUpperCase() +".\n\n"
//						error = "You didn't enter your " + fld.name +".\n"
						} else {
						fld.style.background = 'White';
				}
				return error;   
			}
		function validateUsername(fld) {
			var error = "";
			var illegalChars = /\W/; // allow letters, numbers, and underscores
		 
			if (fld.value == "") {
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
				error = "\t" + "You didn't enter a USERNAME.\n\n";
			} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
				error ="\t" +  "Your USERNAME is the wrong length.\n\n";
			} else if (illegalChars.test(fld.value)) {
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
				error ="\t" +  "Your USERNAME contains illegal characters.\n\n";
			} else {
				fld.style.background = 'white';
			} 
			return error;
		}
		function validatePassword(fld) {
			var error = "";
			var illegalChars = /[\W_]/; // allow only letters and numbers 
		 
			if (fld.value == "") {
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
				error = "\t" + "You didn't enter a PASSWORD.\n\n";
			} else if ((fld.value.length < 7) || (fld.value.length > 15)) {
				error = "\t" + "Your PASSWORD is the wrong length. \n\n";
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
			} else if (illegalChars.test(fld.value)) {
				error = "\t" + "Your PASSWORD contains illegal characters.\n\n";
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
			} else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
				error = "\t" + "Your PASSWORD must contain at least one numeral.\n\n";
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
			} else {
				fld.style.background = 'White';
			}
		   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 = '#f5f179'; 
					fld.style.border='red';
				error ="\t" + "You didn't enter your EMAIL.\n\n";
			} else if (!emailFilter.test(tfld)) {              //test email for illegal characters
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
				error ="\t" +  "Please enter a valid EMAIL.\n\n";
			} else if (fld.value.match(illegalChars)) {
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
				error = "\t" + "Your EMAIL contains illegal characters.\n\n";
			} else {
				fld.style.background = 'White';
			}
			return error;
		}
		function validatePhone(fld) {
			var error = "";
			var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     
		
		   if (fld.value == "") {
				error = "\t" + "Your didn't enter a PHONE NUMBER.\n\n";
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
			} else if (isNaN(parseInt(stripped))) {
				error = "\t" + "Your PHONE NUMBER contains illegal characters.\n\n";
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
			} else if (!(stripped.length == 10)) {
				error = "\t" + "Your PHONE NUMBER is the wrong length. Make sure you included an area code.\n\n";
					fld.style.background = '#f5f179'; 
					fld.style.border='red';
			} 
			return error;
		}
