//Validation of Contact and Trial form personal details
//August 2010

//Validate Required fields have valid contents
function Validate(theForm)
{
	// Quick references to form fields
	var fname = theForm.first_name;
	var lname = theForm.last_name;
	var comp = theForm.company;
	var email = theForm.email;

    // Check each input in order that it appears on form
    if(notEmpty(fname, "Please enter your first name")){
        if(isAlphabet(fname, "Please enter only letters for your name")){
            if(notEmpty(lname, "Please enter your last name")){
                if(isAlphabet(lname, "Please enter only letters for your name")){
                    if(notEmpty(comp, "Please enter your company\'\s name")){
                        if(notEmpty(email, "Please enter your email address")){
                            if(emailValidator(email, "Please enter a valid email address")){
                                return true;
                            }
                        }
                    }
                }
            }
        }

    }
    return false;
}

function notEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus();
		return false;
	}
	return true;
}

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

function lengthRestriction(elem, max, helperMsg){
	var uInput = elem.value;
	if(uInput.length <= max){
		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;
	}
}

