/* 	Script used by changeDetails.shtml to validate form fields before date is sent to 
	Formmail2.cgi
	written by Joseph Leniston joseph_leniston@jltgroup.com
*/
function validate (myForm) {
	var msg, show;
	
	msg = "<p style=\"font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 71%;\">The following required fields have not been completed:<br>\n";
	show = false;
	
	// Test that each mandatory field has been filled in
	
	if (myForm.FirstName.value == "") {
		msg = msg + "First Name<br>\n";
		show = true;
	}
	
	if (myForm.Surname.value == "") {
		msg = msg + "Surname<br>\n";
		show = true;
	}

	if (myForm.Address.value == "") {
		msg = msg + "Address<br>\n";
		show = true;
	}
	
	if (myForm.Postcode.value == "") {
		msg = msg + "Postcode<br>\n";
		show = true;
	}
	
	if (myForm.HomTel.value == "") {
		msg = msg + "Home Tel No<br>\n";
		show = true;
	}
	
	// Calls emailcheck function to ensure a valid email address has been entered
	
	if (emailcheck(myForm.email.value) == false) {
		msg = msg + "Email Address<br>\n";
		show = true;
	}
	
	if (myForm.Make[myForm.Make.selectedIndex].value == "Make") {
		msg = msg + "Make of your Caravan<br>\n";
		show = true;
	}
	
	if (myForm.Model.value == "") {
		msg = msg + "Model of your Caravan<br>\n";
		show = true;
	}
	
	if (myForm.Year[myForm.Year.selectedIndex].value == "Year") {
		msg = msg + "Year of your Caravan<br>\n";
		show = true;
	}
	
	if (myForm.CoverFromDay[myForm.CoverFromDay.selectedIndex].value == "Day") {
		msg = msg + "Cover Needed From Day<br>\n";
		show = true;
	}
	
	if (myForm.CoverFromMonth[myForm.CoverFromMonth.selectedIndex].value == "Month") {
		msg = msg + "Cover Needed From Month<br>\n";
		show = true;
	}
	
	if (myForm.CoverFromYear[myForm.CoverFromYear.selectedIndex].value == "Year") {
		msg = msg + "Cover Needed From Year<br>\n";
		show = true;
	}
	
	if (myForm.ClaimInLast5Years[myForm.ClaimInLast5Years.selectedIndex].value == "Yes") {
		if (myForm.DateOfClaimDay[myForm.DateOfClaimDay.selectedIndex].value == "Day") {
			msg = msg + "Date Of Claim Needed From Day<br>\n";
			show = true;
		}
	
		if (myForm.DateOfClaimMonth[myForm.DateOfClaimMonth.selectedIndex].value == "Month") {
			msg = msg + "Date Of Claim Needed From Month<br>\n";
			show = true;
		}
	
		if (myForm.DateOfClaimYear[myForm.DateOfClaimYear.selectedIndex].value == "Year") {
			msg = msg + "Date Of Claim Needed From Year<br>\n";
			show = true;
		}
	}
	if (myForm.CoverRequired[0].checked == false && myForm.CoverRequired[1].checked == false) 					
	{
		msg = msg + "Cover Required (Indemnity or Reinstatement)<br>\n";
		show = true;
	}
	
	if (myForm.Structure.value == "") {
		msg = msg + "Touring Caravan Value/Purchase Price - Structure<br>\n";
		show = true;
	}
	
	if (myForm.ReadFinePrint.checked == false) {
		msg = msg + "\nYou have not indicated (by ticking the box) that you have read and understood the fine print<br>\n";
		show = true;
	}
	
	if (myForm.ReadCP.checked == false) {
		msg = msg + "\nYou have not indicated (by ticking the box) that you have reviewed the Customer Prospectus<br>\n";
		show = true;
	}
	
	if (myForm.ReadCIL.checked == false) {
		msg = msg + "\nYou have not indicated (by ticking the box) that you have reviewed the Customer Information Leaflet<br>\n";
		show = true;
	}
	
	msg = msg + '</p>';
	
	/* 	if a field has been left blank then display a message to the user otherwise
		submit the form to formmail
	*/
	
	if (show) {
		msgbox(msg);
		return false;
	}
	else {
		return true;
	}
}

/*
Custom javascript error box
By JavaScript Kit (http://javascriptkit.com)
Over 200+ free scripts here!
*/

function msgbox(msg) {
	// Calculate screen size so the message box is centered
	var ah = (screen.availHeight - 30 - 300) / 2;
    var aw = (screen.availWidth - 10 - 350) / 2;
	
	msgwindow=window.open("","","width=350,height=320,left=" + aw + ",top=" + ah);
	msgwindow.document.write('<title>Missing Details</title>');
	msgwindow.document.write(msg);
	msgwindow.document.write('<br><center><form><input type="button" value="Return to Form" onClick="window.close()"></form></center>');
	msgwindow.document.close();
	msgwindow.document.bgColor="white";
	msgwindow.document.body.font="Verdana";
	return true;
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * http://www.smartwebby.com/DHTML/email_validation.asp
 */

function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if ((str==null)||(str=="")){
			return false
		}
	
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 
		 if (str.indexOf(dot)+1==lstr){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}