function checkForm(){
	
	var emails = "rest7,rest16"; // ,rest8, rest13
	
	var postcodeStr = document.getElementById("Postcode").value;
	var startsWith = postcodeStr.substring(0,2);
	var postcode3 = postcodeStr.substring(0,3);
	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;
		}
	
	// No ATM
	
	var atmMachine = false;
	
	if(document.getElementById("ATM_Terminal").value == "Yes")
		{
			atmMachine = true;
		}
		
	// Minimum Trading Years
	
	var oneTradingYear = false;
	
	if(document.getElementById("YearsEstablished").value != "Under 1 Year")
		{
			oneTradingYear = 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;	
			}


// SME Insurance
		
		if(startsWith != "BT" )
					{
						emails += ",rest2";
					}

//First4 & PS Roberts & Hensure Insurance
	
	if(startsWith != "BT")
		{
			emails += ",rest3"; //rest10, paused 5th July, please place back in here once live again.
		}
		

	//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;
				}
				
			}
			
	
	if(atmMachine == false && oneTradingYear && startsWith != "BT" && notWeekend && BGtime)
		{
			if (document.getElementById("TypeOfBusiness").value != "Public House" && document.getElementById("TypeOfBusiness").value != "Takeaway")
			{
				emails += ",rest5";
			}
		}



	// Your Insurance 
	if(notWeekend && (timeHours >= 9) && (timeHours < 17))
		{
			emails += ",rest6";
		}

/*

	//Crisis Insurance
	
	var crisisPostcode = false;
	
	if ((startsWith == "BL") || (startsWith == "OL") || (startsWith == "SK") || (startsWith == "WA")|| (startsWith == "WN")) 
			{
				crisisPostcode = true;
			}

		if (startsWithFirstLetter == "M") 
			{
				if ((no == "1") || (no == "2") || (no == "3") || (no == "4") || (no == "5") || (no == "6")|| (no == "7") || (no == "8") || (no == "9")) 
					{
						crisisPostcode = true;
					}
			}
			
		if (crisisPostcode)
			{
				emails += ",rest12";	
			}
*/
			
			
	// Cheers Insurance
	
	if(startsWith == "TW" || postcode3 == "KT1" || postcode3 == "KT2" || postcode3 == "KT7" || postcode3 == "KT8")
		{
			emails += ",rest14";	
		}
		
/*	
	//Oncover Insurance
	
	var oncoverPostcode = true
	
	if(londonPostcode || startsWith == "GL")
		{
			oncoverPostcode = false;
		}
	
	if(oncoverPostcode)
		{
			emails += ",rest15";	
		}
*/
		
	document.getElementById("recipient_bcc").value = emails;
	document.getElementById("reference").value = emails;
}




function checkWholeForm(form) {
	
    var why = "";
	why += checkDropdown(document.getElementById('Title').value, " Title");
    why += isEmpty(document.getElementById('FullName').value, "Full Name");
	why += isEmpty(document.getElementById('DaytimeNumber').value, " Contact Telephone Number");	
	why += isEmpty(document.getElementById('email').value, " Email");
	why += isEmpty(document.getElementById('AddressApp').value, " Restaurant Address");
	why += isEmpty(document.getElementById('Postcode').value, " Restaurant Postcode");
	why += isEmpty(document.getElementById('BusinessName').value, " Business/Trading Name");
	why += checkDropdown(document.getElementById('TypeOfBusiness').value, " Type Of Business");
	
	why += checkStartDate();
	why += checkDropdown(document.getElementById('InsuranceClaims5Years').value, " Number of claims made in the past 5 years?");
	why += isEmpty(document.getElementById('FixturesAndFittings').value, " Tenants Improvements/Fixtures and Fittings");
	
	why += isEmpty(document.getElementById('ComputersAndElectrical').value, " Computer Equipment/Electronic Business Machines");
	why += isEmpty(document.getElementById('GeneralStock').value, " General Stock");
	why += isEmpty(document.getElementById('Frozen_Stock').value, " Frozen Stock");
	why += isEmpty(document.getElementById('Tobacco').value, " Stock of Tobacco");
	why += isEmpty(document.getElementById('WinesAndSpirits').value, " Wines & Spirits");
	
	why += checkDropdown(document.getElementById('YearsEstablished').value, " How long have you been trading for?");
	why += checkDropdown(document.getElementById('ATM_Terminal').value, " Do you have an ATM on site?");
	
	why += checkDropdown(document.getElementById('Alarm').value, " Is There An Alarm System Fitted?");
	why += checkDropdown(document.getElementById('Signaling_Type').value, " Type Of Signalling?");
	 
    if (why != "") {
       alert("Please fill in the following fields: \n"+why);
       return false;
    }
}

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