// 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;
}

// check the dropdown selection
function checkDropDown(control,input){
	if (control[control.selectedIndex].value==input){
		control.focus();
		return false;
	}
	return true;
}

function checkSubmit(theForm){
 var chkQ1 = theForm.q1;
 var chkQ2 = theForm.q2;
 var chkQ3 = theForm.q3;
 var chkQ4 = theForm.q4;
 var chkQ5 = theForm.q5;
 var chkQ6 = theForm.q6;
 var chkQ7 = theForm.q7;
 var chkQ8 = theForm.q8;
 var chkQ9 = theForm.q9;
 var chkQ10 = theForm.q10;
 var chkQ11 = theForm.q11;
 var chkQ12 = theForm.q12;
 var chkQ13 = theForm.q13;
 var chkQ14 = theForm.q14;
 var chkQ15 = theForm.q15;
 var chkQ16 = theForm.q16;
 var chkName = theForm.name;
 var chkEmail= theForm.email;
 var chkDate = theForm.dateofvisit;
 var chkTime = theForm.timeofvisit;
 var chkMessage = theForm.message;
 var chkCheck = theForm.check;

	if (!checkDropDown(chkQ1,"0")){
		alert("Please select an option for question 1");
		return false;
	}

	if (!checkDropDown(chkQ2,"0")){
		alert("Please select an option for question 2");
		return false;
	}

	if (!checkDropDown(chkQ3,"0")){
		alert("Please select an option for question 3");
		return false;
	}

	if (!checkDropDown(chkQ4,"0")){
		alert("Please select an option for question 4");
		return false;
	}

	if (!checkDropDown(chkQ5,"0")){
		alert("Please select an option for question 5");
		return false;
	}

	if (!checkDropDown(chkQ6,"0")){
		alert("Please select an option for question 6");
		return false;
	}

	if (!checkDropDown(chkQ7,"0")){
		alert("Please select an option for question 7");
		return false;
	}

	if (!checkDropDown(chkQ8,"0")){
		alert("Please select an option for question 8");
		return false;
	}

	if (!checkDropDown(chkQ9,"0")){
		alert("Please select an option for question 9");
		return false;
	}

	if (!checkDropDown(chkQ10,"0")){
		alert("Please select an option for question 10");
		return false;
	}

	if (!checkDropDown(chkQ11,"0")){
		alert("Please select an option for question 11");
		return false;
	}

	if (!checkDropDown(chkQ12,"0")){
		alert("Please select an option for question 12");
		return false;
	}

	if (!checkDropDown(chkQ13,"0")){
		alert("Please select an option for question 13");
		return false;
	}

	if (!checkDropDown(chkQ14,"0")){
		alert("Please select an option for question 14");
		return false;
	}

	if (!checkDropDown(chkQ15,"0")){
		alert("Please select an option for question 15");
		return false;
	}

	if (!checkDropDown(chkQ16,"0")){
		alert("Please select an option for question 16");
		return false;
	}

	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(chkDate.value) ==""){
		alert("Please enter your date of visit");
		chkPhone.focus();
		chkPhone.select();
		return false
	}

	if (trimSpaces(chkTime.value) ==""){
		alert("Please enter your time of visit");
		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) !="lounge" && trimSpaces(chkCheck.value) !="lounge"){
		alert("Please enter the text that appears in the image");
		chkCheck.focus();
		chkCheck.select();
		return false
	}
}