// Send Email - Validate Form

// remove leading and tailing a string
function trimSpaces(inputStr){
	var len=inputStr.length;
	var i=0;
	while(i<len && inputStr.charAt(i)==" ") i++;
	var j=len-1;
	while(j>=0 && inputStr.charAt(j)==" ") j--;
	if (i<=j) // not empty
		return inputStr.substring(i,j+1);
	else
		return "";
}

// validate the email
function emailCheck (emailStr) {
 var emailPat=/^(.+)@(.+)$/
 var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
 var validChars="\[^\\s" + specialChars + "\]"
 var quotedUser="(\"[^\"]*\")"
 var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
 var atom=validChars + '+'
 var word="(" + atom + "|" + quotedUser + ")"
 var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
 var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
 var matchArray=emailStr.match(emailPat)

	if (emailStr=="") {
	 	alert("Please provide a contact Email Address")
		return false;
	}

	if (matchArray==null) {
	 	alert("Your Email Address seems incorrect.\nPlease check the formatting (@ and .)")
		return false;
	}
	var user=matchArray[1]
	if (user.match(userPat)==null) {
	    alert("The email username doesn't seem to be valid.")
	    return false;
	}

	return true;
}

function checkSubmit(theForm){
 var chkName = theForm.name;
 var chkEmail= theForm.email;
 var chkPhone = theForm.number;
 var chkMessage = theForm.message;
 var chkCheck = theForm.check;

	if (trimSpaces(chkName.value) ==""){
		alert("Please provide your name");
		chkName.focus();
		chkName.select();
		return false
	}

	if (trimSpaces(chkEmail.value) ==""){
		alert("Please enter your email address");
		chkEmail.focus();
		chkEmail.select();
		return false
	} else if (!emailCheck(trimSpaces(chkEmail.value))){
		chkEmail.focus();
		chkEmail.select();
		return false
	}

	if (trimSpaces(chkPhone.value) ==""){
		alert("Please enter your telephone number");
		chkPhone.focus();
		chkPhone.select();
		return false
	}

	if (trimSpaces(chkMessage.value) ==""){
		alert("Please enter your message");
		chkMessage.focus();
		chkMessage.select();
		return false
	}
	
	if (trimSpaces(chkCheck.value) !="LUNCH" && trimSpaces(chkCheck.value) !="lunch"){
		alert("Please enter the text that appears in the image");
		chkCheck.focus();
		chkCheck.select();
		return false
	}
}