// ----------------------------------------------------------------------------------------------
// ----------------------------  Outsource Resource Form Javascript Validation --------------------------
// ------------------------------------------------------------------------------------------------


/* begin javascript form validation */	
function isAlphabet(elem, helperMsg){ 
	var alphaExp = /^[a-zA-Z0-9,-.#_!,\s]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAddress(elem, helperMsg){ 
	var alphaExp = /^[a-zA-Z\s!.,#]+$/;
	if((elem.value.match(alphaExp))&&(elem.value.length > 0)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isZip(elem, helperMsg){ 
	var alphaExp = /^[0-9-]+$/;
	if((elem.value.match(alphaExp))&&(elem.value.length > 0)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isPhoneOrEmailThere(elemPhone, elemEmail, helperMsg){ 
	if( (elemPhone.value.length > 0)||(elemEmail.value.length > 0)  ){
		return true;
	}else{
		alert(helperMsg);
		elemEmail.focus();
		return false;
	}
}

function isEmailAccurate(elemEmail, helperMsg){
	var alphaExp = /^[^<>:\s]+$/;
	if( (elemEmail.value.length == 0)|| elemEmail.value.match(alphaExp)  ){
		return true;
	}else{
		alert(helperMsg);
		elemEmail.focus();
		return false;
	}
}

function isEmailWellFormed(elemEmail, helperMsg){
	var alphaExp = /^.+@.+$/;
	if( (elemEmail.value.length == 0)|| elemEmail.value.match(alphaExp)  ){
		return true;
	}else{
		alert(helperMsg);
		elemEmail.focus();
		return false;
	}
}

function isPhoneAccurate(elemPhone, helperMsg){
	var alphaExp = /^[0-9-\s()._]+$/;
	if( (elemPhone.value.length == 0)|| elemPhone.value.match(alphaExp)  ){
		return true;
	}else{
		alert(helperMsg);
		elemPhone.focus();
		return false;
	}
}


// If the length of the element's string is 0 then display helper message. 
function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function messageValidator(elem, helperMsg){ 
	var messageExp = /^[^<>/\\]+$/; // modified version of code from http://blog.techsaints.com/2007/04/30/javascript-phone-number-validation/
	if(elem.value.match(messageExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function formValidator(){
	// Make quick references to our fields
	var userName = document.getElementById('userName');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	var message = document.getElementById('message');
	
	// Check each input in the order that it appears in the form!
	if(notEmpty(userName, "Please enter your name.")){
		if(notEmpty(email, "Please enter your email.")){
			if(notEmpty(phone, "Please enter your phone.")){
				if(notEmpty(message, "Please enter your message.")){
					if(isAlphabet(userName, "Only letters and spaces in the name field, please.")){
						if(isEmailAccurate(email, "No spaces or special characters in the email field, please.")){
							if(isEmailWellFormed(email, "Invalid email (must be in the form of name@location).")){
								if(isPhoneAccurate(phone, "Numbers, hypens, parentheses and spaces only in the phone field, please.")){
									if(messageValidator(message, "No special characters in the message, please.")){
										return true;
									}
								}
							}	
						}
					}
				}
			}
		}
	}
	return false;
}
/* end javascript form validation */


