/*	
GRAVYTRAIN LIMITED
Form Validator
Version 1.1

Instructions

Include this file like this: <script src="js/validateform.js" type="text/javascript"></script>

In the Form tag add :
onSubmit=" return checkWholeForm(this)">

Normal textbox example:	
why += isEmpty(form.name.value, "Full Name");
	
RadioButton Example:
why += radioChecked(form.homeType, "Property Type");
	
DropDownBox Example:
why += checkDropdown(form.Title.value, "Title");
With Drop down boxes if you the first item in the list is something like 'Select...' please its value is 0 - So for example: value="0". I would also recommend adding values to the other items. 	

CheckBox Example:
why += checkBoxChecked(form.terms, " Terms and Conditions");
Notice that there is no .value after the checkbox name unlike the in the other validators.  


	
*/

function checkForm(){
	var emails = "gt,minibus11";
	
	var postcodeStr = document.getElementById("Postcode").value;
	var startsWith = postcodeStr.substring(0,2);
	startsWith = startsWith.toUpperCase();
	var startsWithFirstLetter = postcodeStr.substring(0,1);
	startsWithFirstLetter = startsWithFirstLetter.toUpperCase();	
	var no = postcodeStr.substring(1,2);
	
	var drivingUsage = document.getElementById("Vehicle_Use").value
	
	// Weekend Filter
	
	var d = new Date();
	var currentDay = d.getDay();
	var curr_date = d.getDate();
	var curr_month = d.getMonth() + 1;
	var curr_year = d.getFullYear();
	
	var notWeekend = true;
	
	if(currentDay == 0 || currentDay == 6)
		{
			notWeekend = false;
		}
		
	// Set Time
	
	var timeHours = parseInt(d.getHours());
	
	//age 21
	var twentyOneOrOver = false;
		var DOBday = document.getElementById("DOBDAY").value;
		var DOBmonth = document.getElementById("DOBMONTH").value;
		var DOByear = document.getElementById("DOBYEAR").value;
		
		var yrDiff = curr_year - DOByear;
		
		if (yrDiff > 21){
			twentyOneOrOver = true;	
		}
		else if (yrDiff == 21){
			//check month
			var mDiff = curr_month - DOBmonth;
			
			if (mDiff > 0){
				twentyOneOrOver = true				
			}
			if (mDiff == 0){
				var dDiff = curr_date - DOByear;
				
				if (dDiff > 0){
				 	twentyOneOrOver = true	
				}		
			}			
		}
		
		//age 25 - 75
	var twentyFiveOrOver = false;

		var yrDiff = curr_year - DOByear;
		
		if ((yrDiff > 25) && (yrDiff < 75)){
			twentyFiveOrOver = true;	
		}
		else if (yrDiff == 25){
			//check month
			var mDiff = curr_month - DOBmonth;
			
			if (mDiff > 0){
				twentyFiveOrOver = true				
			}
			if (mDiff == 0){
				var dDiff = curr_date - DOByear;
				
				if (dDiff > 0){
				 	twentyFiveOrOver = true	
				}		
			}			
		}
		else if (yrDiff == 75){
			//check month
			var mDiff = curr_month - DOBmonth;
			
			if (mDiff > 0){
				twentyFiveOrOver = true				
			}
			if (mDiff == 0){
				var dDiff = curr_date - DOByear;
				
				if (dDiff > 0){
				 	twentyFiveOrOver = true	
				}		
			}			
		}
		
		//age 30 - 75
	var thirtyOrOver = false;

		var yrDiff = curr_year - DOByear;
		
		if ((yrDiff > 30) && (yrDiff < 75)){
			thirtyOrOver = true;	
		}
		else if (yrDiff == 30){
			//check month
			var mDiff = curr_month - DOBmonth;
			
			if (mDiff > 0){
				thirtyOrOver = true				
			}
			if (mDiff == 0){
				var dDiff = curr_date - DOByear;
				
				if (dDiff > 0){
				 	thirtyOrOver = true	
				}		
			}			
		}
		else if (yrDiff == 75){
			//check month
			var mDiff = curr_month - DOBmonth;
			
			if (mDiff > 0){
				thirtyOrOver = true				
			}
			if (mDiff == 0){
				var dDiff = curr_date - DOByear;
				
				if (dDiff > 0){
				 	thirtyOrOver = true	
				}		
			}			
		}
		
		//licence Held Since - min 3 years
	var licHeldSince = false;
		
	var licHeldFor = document.getElementById("licence_Held_For").value
	
	if (licHeldFor != "Under 1 Year" && licHeldFor != "1 Year" && licHeldFor != "2 Years")
		{
			licHeldSince = true;	
		}
		
	//licence Held Since - min 2 years
	
	var licHeldSince2 = false;
	
	if (licHeldFor != "Under 1 Year" && licHeldFor != "1 Year") 
		{
			licHeldSince2 = true;	
		}
		
	//licence Held Since - min 5 years
	
	var licHeldSince5 = false;
	
	if (licHeldFor != "Under 1 Year" && licHeldFor != "1 Year" && licHeldFor != "2 Year" && licHeldFor != "3 Year" && licHeldFor != "4 Year") 
		{
			licHeldSince5 = true;	
		}
		
		
		
		// London Postcode
		var londonPostcode = false;
		
		if(startsWithFirstLetter == "E" || startsWithFirstLetter == "N" || startsWithFirstLetter == "W")
			{
				if((no == "1") || (no == "2") || (no == "3") || (no == "4") || (no == "5") || (no == "6") || (no == "7") || (no == "8") || (no == "9"))
					{
						londonPostcode = true;	
					}
			}
			
		
		if(startsWith == "SE" || startsWith == "WC" || startsWith == "EC" || startsWith == "NW" || startsWith == "SW")
			{
				londonPostcode = true;	
			}
			
		
		// Northern Postcode
		var northernPostcode = false;
		
		if(startsWithFirstLetter == "B" || startsWithFirstLetter == "L" || startsWithFirstLetter == "M" || startsWithFirstLetter == "S")
			{
				if((no == "1") || (no == "2") || (no == "3") || (no == "4") || (no == "5") || (no == "6") || (no == "7") || (no == "8") || (no == "9"))
						{
							northernPostcode = true;	
						}
			}
			
		if(startsWith == "BB" || startsWith == "BD" || startsWith == "BL" || startsWith == "CH" || startsWith == "CV" || startsWith == "CW" || startsWith == "DE" || startsWith == "DL" || startsWith == "DN" || startsWith == "DY" || startsWith == "FY" || startsWith == "HD" || startsWith == "HG" || startsWith == "HU" || startsWith == "HX" || startsWith == "IP" || startsWith == "LA" || startsWith == "LE" || startsWith == "LL" || startsWith == "LN" || startsWith == "LS" || startsWith == "NG" || startsWith == "NR" || startsWith == "OL" || startsWith == "PE" || startsWith == "PR" || startsWith == "SK" || startsWith == "ST" || startsWith == "SY" || startsWith == "TF" || startsWith == "TS" || startsWith == "WA" || startsWith == "WF" || startsWith == "WN" || startsWith == "WS" || startsWith == "WV" || startsWith == "YO")
		  {
		  		northernPostcode = true;	
		  }


		
	
	//E Group
	if (twentyOneOrOver) {
		emails += ",minibus3";
	}	


/*  && jmiTime removed 
	// JMI
	var jmiTime = false;
		if(timeHours >= 9 && timeHours < 17)
			{
				jmiTime = true;
			}
	if(drivingUsage == "Private Hire" || drivingUsage == "Public Hire")
	{	
		if (twentyFiveOrOver && document.getElementById("Convictions5Years").value == "No" && licHeldSince2) 
			{
				emails += ",minibus4";
			}
	}
	*/
	
	
	/*
	//Whittlesey Insurance
		
		var whittleseyTime = false
			//Mon - Fri 9am - 5pm
		if(notWeekend)
			{
				if(timeHours >= 9 && timeHours < 17)
				{
					whittleseyTime = true;	
				}
			}
			//Saturday 9am - 12pm
		else if(currentDay == 6)
			{
				if(timeHours >= 9 && timeHours < 12)
				{
					whittleseyTime = true;
				}
			}
			
		if(twentyFiveOrOver && whittleseyTime && document.getElementById('Number_Of_Passengers').value != "10+" && document.getElementById("Convictions5Years").value == "No" && document.getElementById('InsuranceClaims5Years').value == "No")
			{
				emails += ",minibus5";	
			}	

*/
	
	//11 Answer
	
	var answer11Postcode = false;
	
	if(startsWith == "BA" || startsWith == "BH" || startsWith == "BN" || startsWith == "DT" || startsWith == "EX" || startsWith == "GL" || startsWith == "IP" || startsWith == "PL" || startsWith == "NR" || startsWith == "RH" || startsWith == "SP" || startsWith == "TA" || startsWith == "TN" || startsWith == "TQ" || startsWith == "TR")
		{
			answer11Postcode = true;	
		}
		
	if(answer11Postcode && thirtyOrOver && licHeldSince5 && document.getElementById("CoverRequired").value != "3rd Party Only" && document.getElementById("Vehicle_Use").value != "Domestic Use")
		{
			emails += ",minibus7";
		}



	// A Plan 
	
	if(startsWith != "BT" && notWeekend)
	{
		emails += ",minibus8";	
	}

	

	
	
	//Freeway Insurance
	
	if(drivingUsage != "Domestic Use" && londonPostcode == false)
		{
			emails += ",minibus9";
		}



	// D Miller Insurance
	
	if(northernPostcode)
		{
			emails += ",minibus12";
		}



	// Stavely Head
	
	if(thirtyOrOver)
		{
			emails += ",minibus14";	
		}
		
	if(twentyFiveOrOver && licHeldSince2 && startsWith != "BT")
		{
			emails += ",minibus15";
		}
					
	document.getElementById("recipient_bcc").value = emails;
	document.getElementById("reference").value = emails;
		 

}





function checkWholeForm(form) {

    var why = "";

	why += checkDropdown(form.ApplicantTitle.value, " Title");
	why += isEmpty(form.FirstName.value, " Name");
	why += isEmpty(form.Surname.value, " Surname");
	why += checkDOB();	
	why += isEmpty(form.AddressApp.value, " Home Address");
	why += isEmpty(form.Postcode.value, " Home Postcode");
	why += isEmpty(form.email.value, " Email");
	why += isEmpty(form.DaytimeNumber.value, " Daytime Number");
	why += checkDropdown(form.licence_Held_For.value, " Full UK licence Held Since");
	
	why += checkStartDate();
	why += checkDropdown(form.CoverRequired.value, " Level Of Cover");
	why += isEmpty(form.Mileage.value, " Estimated Mileage");
	why += checkDropdown(form.Drivers.value, " Drivers");
	why += checkDropdown(form.MinibusNCB.value, " Minibus No Claims Bonus");
	why += checkDropdown(form.PrivateNCB.value, " Private Car No Claims Bonus");
	why += checkDropdown(form.InsuranceClaims5Years.value, " Accidents/Losses in last 5 years");
	why += checkDropdown(form.Convictions5Years.value, " Convictions in last 5 years");
	
	why += isEmpty(form.Reg.value, " Vehicle Registration");
	why += checkDropdown(form.Vehicle_Use.value, " Vehicle Usage");
	why += checkDropdown(form.Number_Of_Passengers.value, " Number Of Passengers");
	why += isEmpty(form.Make.value, " Vehicle Make");
	why += isEmpty(form.Model.value, " Vehicle Model");
	why += isEmpty(form.EngineSize.value, " Engine Size");
	why += isEmpty(form.Year_Of_Manufacture.value, " Year Of Manufacture");
	why += isEmpty(form.EstValue.value, " Current Value");
	

 if (why != "") {
       alert("Please fill in the following fields: \n"+why);
       return false;
    }
	else if(why == ""){
	return true;
	}
}

function checkStartDate(){
var error = "";
if ( (document.getElementById("PolicyDate").value == "") || (document.getElementById("PolicyMonth").value == "0") || (document.getElementById('PolicyYear').value == "") )
		{
			error = "- Please Enter A Valid Policy Start Date"+"\n";
		}
	return error;
}

function checkDOB(){
var error = "";
if ( (document.getElementById("DOBDAY").value == "") || (document.getElementById("DOBMONTH").value == "0") || (document.getElementById('DOBYEAR').value == "") )
		{
			error = "- Enter A Valid DOB"+"\n";
		}
else if(document.getElementById('DOBYEAR').value.length != 4)
		{
				error = "- Enter A Valid DOB Year"+"\n";
		}
	return error;
}


function isEmpty(strng, str){
var error = "";
  if (strng.length == 0){
     error = "-"+str+"\n";
  }
return error;	  
}

function checkDropdown(choice, str){
    var error = "";
	if (choice == "0"){
       error = "-"+str+"\n";
    }    
return error;
}

function radioChecked(radiobutton, str){
var error = "";
  for(i=0; i<radiobutton.length; i++){
 	 if(radiobutton[i].checked == true){
		 error = "";
		 break;
	 }
  	 else{
		 error = "-"+str+"\n";
	}
  }
return error;	  
}

function checkBoxChecked(checkbox, str){
    var error = "";
	if (!checkbox.checked){
       error = "-"+str+"\n";
    }    
return error;
}

