// JavaScript Document for contant form validation

function validateform(){
	sortir = 0;
	if (document.form1.name.value == '' && sortir == 0) 
	{
		window.alert('The field Name is required. Please, be sure that you fill itm up.');
		document.form1.name.focus();
		sortir = 1;
	}
	

		if (document.form1.email.value == '' && sortir == 0) {
		window.alert('The fields Email is required. Please, be sure that you fill it up.');
		document.form1.email.focus();
		sortir = 1;
	}
	else
	{
	   with (document.form1.email)
		{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
			{
			alert("Incorrect e-mail add1ess");
			return false;
			}
		}
	}

if (document.form1.phone.value == '' && sortir == 0) 
	{
		window.alert('The field Comment is required. Please, be sure that you fill itm up.');
		document.form1.name.focus();
		sortir = 1;
	}

 if (document.form1.subject.value == '' && sortir == 0) 
	{
		window.alert('The field Subject is required. Please, be sure that you fill itm up.');
		document.form1.subject.focus();
		sortir = 1;
	}
//return true;
	if (sortir == 0){
			document.form1.submit();
	}
	}
	
	//original source code

/*function validateform(){
	sortir = 0;
	if (document.form1.name.value == '' && sortir == 0) 
	{
		window.alert('The field Name is required. Please, be sure that you fill itm up.');
		document.form1.name.focus();
		sortir = 1;
	}
	

		if (document.form1.email.value == '' && sortir == 0) {
		window.alert('The fields Email is required. Please, be sure that you fill it up.');
		document.form1.email.focus();
		sortir = 1;
	}
	else
	{
	   with (document.form1.email)
		{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		lastpos=value.length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
			{
			alert("Incorrect e-mail add1ess");
			return false;
			}
		}
	}

	if (document.form1.comments.value == '' && sortir == 0) 
	{
		window.alert('The field Comment is required. Please, be sure that you fill itm up.');
		document.form1.name.focus();
		sortir = 1;
	}

	if (document.form1.subject.value == '' && sortir == 0) 
	{
		window.alert('The field Subject is required. Please, be sure that you fill itm up.');
		document.form1.subject.focus();
		sortir = 1;
	}

	if (sortir == 0){
			document.form1.submit();
	}
	}
*/


/* newly enterd one.
function processForm(){
		if (document.form1.name.value == 0) 
	{
		window.alert('The field Name is required. Please, be sure that you fill itm up.');
		return false;
	}
	
   else if (document.form1.email.value == 0) {
		window.alert('The fields Email is required. Please, be sure that you fill it up.');
		return false;

	}
	else if (document.form1.phone.value == 0) {
		window.alert('The fields Email is required. Please, be sure that you fill it up.');
		return false;

	}
	return true;

	}*/
	
function formValidator(){
	// Make quick references to our fields
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	var add1 = document.getElementById('add1');
	var captcha = document.getElementById('captcha');
	
	
	// Check each input in the order that it appears in the form!
	if(isAlphabet(name, "Please Enter Only Letters For Your Name")){
		if(emailValidator(email, "Please Enter a valid E-mail Address")){
		if(isNumeric(phone, "Please Enter a Valid Phone Number")){
		if(isAlphanumeric(add1, "Enter Numbers and Letters Only for additional information")){
		if(Captcha(captcha, "Enter The Correct Number")){
						
							return true;
						}
					}
				}
			}
		}
		
	return false;
	
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

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

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z' ']+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z' ']+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		alert("Please enter between " +min+ " and " +max+ " characters");
		elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

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 Captcha(elem, helperMsg){
	var numericExpression = 63;

	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}

}




