/*	
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 = "gravytrain"; 
	
	var postcodeStr = document.getElementById("Business_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);
	
	// Set Time
	
	var d = new Date();
	var timeHours = parseInt(d.getHours());
	
	// Weekend Filter
	
	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;
		}
	
	//Number of Vehicles
	
	var numberOfVehicles = document.getElementById("Number_Of_Vehicles").value;
	
	var vehicleUsage = document.getElementById("Vehicle_Usage").value;
	
	// 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
	//Time Filter (Receive leads 24hours a day except no leads after 12pm on Saturday and before 12 on Sunday)
	var EGroupTime = true;
                
    if(currentDay == 6)
		{
			if(timeHours >= 12)
			{
			EGroupTime =  false;
			}
		}
	
	if(currentDay == 0)
		{
			if(timeHours < 12)
			{
			EGroupTime =  false;
			}
		}

	if (EGroupTime && (numberOfVehicles >= "3" || numberOfVehicles == "10+"))
		{
			if (startsWith != "BT")
				{				
					emails += ",motorfleet13";
				}
		}
		
	
	//UK Special Risks
	/*	
	if(startsWith == "TN" || startsWith == "ME" || startsWith == "CT" || startsWith == "BN" || startsWith == "RH" || startsWith == "GU" || startsWith == "PO" || startsWith == "SO" || startsWith == "RG" || startsWith == "SL" || startsWith == "SS" || startsWith == "CM" || startsWith == "CO" || startsWith == "SG" || startsWith == "AL" || startsWith == "SL" || startsWith == "HP" || startsWith == "OX" || startsWith == "CB" || startsWith == "LU" || startsWith == "MK")
		{
		*/
			
		if (numberOfVehicles >= "4" && vehicleUsage != "Private Hire" && vehicleUsage != "Public Hire" && vehicleUsage !="SDP excluding commuting" && vehicleUsage !="SDP including commuting")
			{
				emails += ",motorfleet3";
			}
		
			

	// Balgrove
	/*
	if (numberOfVehicles >= "4"|| numberOfVehicles == "10+")
		{
			if (startsWith != "BT" && document.getElementById("FleetRated").value == "Yes")
				{
					emails += ",motorfleet4";
				}
		}
		*/
		
		/*
		//Horsham Insurance
		
		var horshamPostcode = false;
		
		if(startsWith == "RH" || startsWith == "GU" || startsWith == "BN" || startsWith == "PO" || startsWith == "KT" || startsWith == "TW" || startsWith == "SM" || startsWith == "CR" || startsWith == "SW" || startsWith == "SL" || startsWith == "CM" || startsWith == "TN")
			{
				horshamPostcode = true;
			}
			
		if(numberOfVehicles >= "5"|| numberOfVehicles == "10+")
			{
				if(horshamPostcode && document.getElementById("FleetRated").value == "Yes")
					{
						emails += ",motorfleet5";
					}
			}
		*/
		
		

		//Coversure (Ilkeston & Kidderminster Branch) motorfleet 6 & 12
		
			//time filters motorfleet6 (mon - fri 12am - 4pm)
			
			var coversureTime6 = false;
			
			if(timeHours < 16)
				{
					coversureTime6 = true;
				}
				
				
		
			//time filters motorfleet12 (mon - fri 9am - 5pm)
			
			var coversureTime12 = false;
			
			if(timeHours >= 9 && timeHours < 17)
				{
					coversureTime12 = true;
				}
			
			
		
		var coversureIlkeston = true;
		
		if(startsWith == "CB" || startsWith == "IP" || startsWith == "CM" || startsWith == "IG" || startsWith == "SS")
			{
				coversureIlkeston = false;
			}
			
		if(startsWithFirstLetter == "E")
			{
				if((no == "1") || (no == "2") || (no == "3") || (no == "4") || (no == "5") || (no == "6") || (no == "7") || (no == "8") || (no == "9"))
						{
							coversureIlkeston = false;	
						}
			}

	/*
		// Coversure Ilkeston
		if (startsWith != "BT" && coversureIlkeston) 
			{
				emails += ",motorfleet6"; 
			}
		*/	
		
		
		// Coversure Kidderminster
		if (startsWith != "BT" && coversureIlkeston && vehicleUsage != "SDP including commuting" && vehicleUsage != "SDP excluding commuting")  /* && notWeekend && coversureTime12 */
			{
				emails += ",motorfleet12"; 
			}
		// Coversure Huntingdon Branch
		
		
		if (vehicleUsage == "SDP including commuting" || vehicleUsage == "SDP excluding commuting")
			{
				emails += ",motorfleet14"; 
			}
	
		
		//Fleet Plan
		
		if (numberOfVehicles >= "5" || numberOfVehicles == "10+")
			{
				emails += ",motorfleet7";
			}
			
	/*		
		// Broadsure
		
		if (numberOfVehicles >= "4" || numberOfVehicles == "10+")
			{
			if (startsWith != "BT")
				{
					emails += ",motorfleet8";	<!-- motortrade9 goes here -->
				}
			}
	*/

			
			
		// Bridle Insurance
		
		if ((numberOfVehicles >= "4" || numberOfVehicles == "10+") && startsWith != "BT")
			{
				emails += ",motorfleet9";
			}
			
	/* Paused ***********************************
		// Coversure (Nuneaton Branch)
		
		var coversureNuneaton = true;
		
		if(startsWith == "CB" || startsWith == "IP" || startsWith == "CM" || startsWith == "IG" || startsWith == "SS")
			{
				coversureNuneaton = false;
			}
			
		if(startsWithFirstLetter == "E")
			{
				if((no == "1") || (no == "2") || (no == "3") || (no == "4") || (no == "5") || (no == "6") || (no == "7") || (no == "8") || (no == "9"))
						{
							coversureNuneaton = false;	
						}
			}
		
		if(vehicleUsage == "Private Car Fleet" || vehicleUsage == "Taxi Fleet")
			{
			if (coversureNuneaton)
				{
					if(numberOfVehicles >= "5" || numberOfVehicles == "10+")
					{
						emails += ",motorfleet10";	
					}
				}
			}
	*/				
		//Baronworth
		
		if (numberOfVehicles >= "4" || numberOfVehicles == "10+")
			{
				if(startsWith != "BT" && vehicleUsage != "Private Hire" && vehicleUsage != "Public Hire")
					{
						emails += ",motorfleet11";
					}
			}
			
		
		
		// Colliers Insurance
		/*
		var colliersPostcode = false;
		
		if (startsWith == "DA" || startsWith == "TW" || startsWith == "ME" || startsWith == "CR" || startsWith == "BR" || startsWith == "EN" || startsWith == "HA" || startsWith == "IG" || startsWith == "KT" || startsWith == "RM" || startsWith == "SM" || startsWith == "TW" || startsWith == "UB" || startsWith == "CT" || startsWith == "TN" || startsWith == "SE" || startsWith == "SW")
			{
				colliersPostcode = true;
			}
		
		if (colliersPostcode || londonPostcode)
			{
				if(document.getElementById("FleetRated").value == "Yes")
				{
					emails += ",motorfleet15";
				}
			}
		*/	
	
		//County Insurance
		
		if(vehicleUsage == "Private Hire" || vehicleUsage == "Public Hire" )
			{
				emails += ",motorfleet17";	
			}


			


		// Barry Grainger
	
		// Time Filter
			
				var BGtime = false;
			if(currentDay == 1 || currentDay == 2 || currentDay == 3 || currentDay == 4)
			{
			if(timeHours >= 9 && timeHours < 17)
				{
					BGtime = true;
				}
			}
			// Friday time - no leads after 5pm until 9am Monday
			if(currentDay == 5)
			{
				if(timeHours >= 9 && timeHours < 17)
				{
					BGtime = true;
				}
				
			}
	
	var grangerPostcodes = false;
	
	if ( startsWith == "AB" || startsWith == "AL" || startsWith == "BA" || startsWith == "BH" || startsWith == "BN" || startsWith == "BR" || startsWith == "BS" || startsWith == "CA" ||  startsWith == "CB" || startsWith == "CF" || startsWith == "CH" || startsWith == "CM" || startsWith == "CT" || startsWith == "CV" ||  startsWith == "CW" ||  startsWith == "DA" ||  startsWith == "DD" ||  startsWith == "DE" ||  startsWith == "DG" ||  startsWith == "DH" ||  startsWith == "DL" ||  startsWith == "DN" || startsWith == "DT" ||  startsWith == "DY" || startsWith == "EH" || startsWith == "EX" ||  startsWith == "FK" ||  startsWith == "FY" || startsWith == "GL" || startsWith == "GU" || startsWith == "HD" || startsWith == "HG" || startsWith == "HP" || startsWith == "HR" || startsWith == "HS" || startsWith == "HU" || startsWith == "IP" || startsWith == "IV" || startsWith == "KA" || startsWith == "KW" || startsWith == "LA" || startsWith == "LD" || startsWith == "LE" || startsWith == "LL" || startsWith == "LN" || startsWith == "LU" || startsWith == "ME" || startsWith == "MK" || startsWith == "ML" || startsWith == "NE" || startsWith == "NG" || startsWith == "NN" || startsWith == "NP" || startsWith == "OX" || startsWith == "PE" || startsWith == "PL" || startsWith == "PO" || startsWith == "PR" || startsWith == "RG" || startsWith == "RH" || startsWith == "RM" || startsWith == "SA" || startsWith == "SG" || startsWith == "SK" || startsWith == "SM" || startsWith == "SN" || startsWith == "SO" || startsWith == "SP" || startsWith == "SR" || startsWith == "ST" || startsWith == "SY" || startsWith == "TA" || startsWith == "TD" || startsWith == "TF" || startsWith == "TN" || startsWith == "TQ" || startsWith == "TR" || startsWith == "TS" || startsWith == "WA" || startsWith == "WD" || startsWith == "WF" || startsWith == "WN" || startsWith == "WR" || startsWith == "WV" || startsWith == "YO")
		{
			grangerPostcodes = true;
		}
		
	if (startsWithFirstLetter == "G" || startsWithFirstLetter == "S")
			{
					if ((no == "1") || (no == "2") || (no == "3") || (no == "4") || (no == "5") || (no == "6") || (no == "7") || (no == "8") || (no == "9"))
					{
						grangerPostcodes = true;
					}	
			}
	
	if (numberOfVehicles >= "4"|| numberOfVehicles == "10+")
		{
			if (grangerPostcodes && BGtime)
				{				
					emails += ",motorfleet20";
				}
		}
			
	
		// 1 Answer
		
		
		/* Time filters removed until further notice
		var OneAnswerTime = false;
		
		if(timeHours >= 9 && timeHours < 14 && notWeekend)
		{
			OneAnswerTime = true;
		}
		
		*/
		// 1 Answer's acceptable postcodes
		var oneAnsPostCode = false;
		if ( startsWith == "BN" || startsWith == "TN")
		{
			oneAnsPostCode = true;	
		}
		
		if(oneAnsPostCode)
			{
				emails += ",motorfleet21";
			}




	//emails = filterTimeEmail(emails);	
	
	document.getElementById("recipient_bcc").value = emails;
	document.getElementById("reference").value = emails;

}

function filterTimeEmail(emails){

	var currentTime = new Date();
	var hours = currentTime.getHours();
	var returnEmails= "gravytrain";
	
	var odds = "motorfleet3,motorfleet4,motorfleet5,motorfleet6,motorfleet7,motorfleet8,motorfleet10,motorfleet12,motorfleet13"; 
	var evens = "motorfleet2,motorfleet3,motorfleet5,motorfleet6,motorfleet7,motorfleet8,motorfleet9,motorfleet10";
	
	var tokens = emails.split( "," );

	for(var i=0; i<tokens.length; i++){
		if( hours % 2 == 0 )
		{
			if (evens.search(tokens[i])!=-1){
				returnEmails+=("," + tokens[i]);
			}
		
		}else{
			if (odds.search(tokens[i])!=-1){
				returnEmails+=("," + tokens[i]);
			}
		}
		
	}
	
	return 	returnEmails;	
}


function checkWholeForm(form) {

    var why = "";

	why += isEmpty(form.Name.value, " Name");
	why += isEmpty(form.Tel.value, " Contact Telephone");
	why += isEmpty(form.email.value, " Email");
	why += isEmpty(form.Business_Address.value, " Business Address");
	why += isEmpty(form.Business_Postcode.value, " Business Postcode");	
	why += checkStartDate();
	//why += checkDropdown(form.Cover_Required.value, " Cover Required"); Removed 18th August
	why += checkDropdown(form.FleetRated.value, " Are you currently Fleet Rated?");	
	why += checkDropdown(form.Vehicle_Usage.value, " Vehicle Usage");
	why += checkDropdown(form.Number_Of_Vehicles.value, " Number Of Vehicles In Your Fleet");
	//why += checkDropdown(form.Driving_Requirements.value, " Driving Requirements"); Removed 18th August
	why += checkDropdown(form.Drivers_Under25.value, " How many of your drivers are 25 years old and under?");
	//why += checkDropdown(form.Temporary_Drivers.value, " Do you use agency, temporary or casual drivers?"); Removed 18th August	
	//why += checkDropdown(form.DM_App_Form_Completed.value, " Have an application form completed?"); Removed 18th August
	//why += checkDropdown(form.DM_Copy_Of_licence.value, " Take a copy of their driving licence?"); Removed 18th August
	//why += checkDropdown(form.DM_Details_of_Convictions.value, " Obtain details of any previous motoring accidents or convictions?"); Removed 18th August
	

 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("Start_Day").value == "") || (document.getElementById("Start_Month").value == "0") || (document.getElementById('Start_Year').value == "") )
		{
			error = "- Please Enter A Valid Start Date Of Cover"+"\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;
}

