// ----------------------------------------------------------------------------------------------
// ----------------------------  Training Form Javascript Validation --------------------------
// ------------------------------------------------------------------------------------------------


/* begin javascript form validation */	
function isAlphabet(elem, helperMsg){ 
	var alphaExp = /^[a-zA-Z0-9,-.#_!,\s]+$/;
	if((elem.value.match(alphaExp))&&(elem.value.length > 0)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isCompany(elem, helperMsg){ 
	var alphaExp = /^[a-zA-Z0-9,-.!_#,\s]+$/;
	if((elem.value.match(alphaExp))||(elem.value.length == 0)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function emailValidator(elem, helperMsg){ 
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		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 = /^[^<>:]+$/;
	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))&&(elem.value.length > 0)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function formValidator(){
	// Make quick references to our fields
	var name = document.getElementById('name');
	var company = document.getElementById('company');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	var address = document.getElementById('address');
	var message = document.getElementById('message');
	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(name, "Please enter your name (letters and spaces only)")){
		if(isCompany(company, "Letters and spaces only in your organization/company")){
			if(isPhoneOrEmailThere(phone, email, "Either phone or email is required")){
				if(isEmailAccurate(email, "No special characters in email")){
					if(isPhoneAccurate(phone, "No special characters in phone")){
						if(isCompany(address, "Please no special characters in address")){
							if(messageValidator(message, "Please enter your message (no special characters, please)")){
								return true;
							}
						}
					}
				}
			}
		}
	}
	return false;
}
/* end javascript form validation */

