/*	
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,caravan7,caravan9"; 
	
	var date = new Date();
	var currentYear = date.getFullYear();
	var caravanAge = currentYear - document.getElementById("Year_Of_Make").value;
	var caravanValue = document.getElementById("Value_Of_Caravan").value;
	var caravanMake = document.getElementById("Make").value;
	
	//Postcode Vars
	var postcodeStr = document.getElementById("Home_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);
	
	// Weekend Filter
	
	var d = new Date();
	var currentDay = d.getDay();
	
	var notWeekend = true;
	
	if(currentDay == 0 || currentDay == 6)
		{
			notWeekend = false;
		}

	
	//Set Time
	var timeHours = parseInt(d.getHours());
	
	// Caravan No Claims Bonus Min 6 Years
	var caravanNCB = document.getElementById('Caravan_No_Claims').value;
	var min6YearsNCB = false;
	
	if(caravanNCB == "6 Years" || caravanNCB == "7 Years" || caravanNCB == "8 Years" || caravanNCB == "9 Years" || caravanNCB == "10+ Years" )
		{
			min6YearsNCB = true;	
		}

	//lifesure
		//year of make
		if ( (document.getElementById("Storage_Type").value != "Working Farm") && (caravanAge <= 15) && document.getElementById("Make").value != "HOBBY" && startsWith != "BT"){
			emails += ",caravan10";
		}

		
/*
	// Caravan Guard
	
	if ( (document.getElementById("Storage_Type").value != "On The Road") && (document.getElementById("Make").value != "Other") && (caravanAge <= 25) && (caravanValue != 4500) )
		{
			emails += ",caravan3";
		}
*/
		
/*		
	// CoverSure
	
	var OneSureCaravan = true;
	
	if(document.getElementById("Make").value == "DETHLEFFS" || document.getElementById("Make").value == "HOBBY" || document.getElementById("Make").value == "FLEETWOOD" || document.getElementById("Make").value == "ROYALE" || document.getElementById("Make").value == "STERCKEMAN")
		{
			OneSureCaravan = false;
		}
		
	if(OneSureCaravan && document.getElementById("Storage_Type").value == "Driveway")
		{
			emails += ",caravan5";	
		{
*/
	
	// Discover Leisure
	
	if (document.getElementById("Best_Time").value != "Evening" && document.getElementById("Make").value != "HOBBY")
		{
			emails += ",caravan6";
		}


	//Stavely Head
	
	if (caravanAge <= 10)
		{
			emails += ",caravan8";
		}

		
	//A Plan
	
	if(notWeekend)
		{
			emails += ",caravan11";
		}
		
	
	//Coversure - Allow 3 leads per day
	
	
	var coversureTime = false;
		
	if(timeHours >= 9 && timeHours < 17 && notWeekend)
		{
			coversureTime = true;
		}
	
	if(coversureTime && document.getElementById("Make").value != "HOBBY")
		{
			emails += ",caravan12";	
		}
	
	
	//2gether insurance
	
	if(caravanAge <= 25 && document.getElementById("Make").value != "HOBBY" && document.getElementById("Make").value != "KNAUS")
	{
		emails += ",caravan13";		
	}

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

function checkWholeForm(form) {

    var why = "";
    var titleErr = checkDropdown(form.Title.value, " Title");
    var nameErr = isEmpty(form.Name.value, " Name");
    var homeAddressErr = isEmpty(form.Home_Address.value, " Home Address");
    var homePostCodeErr = isEmpty(form.Home_Postcode.value, " Home Postcode");
    var emailErr = isEmpty(form.email.value, " Email");
    var contactNumberErr = isEmpty(form.Contact_Number.value, " Contact Number");
    var dobErr = checkDOB();
    var occupationErr = isEmpty(form.Occupation.value, " Occupation");
    var bestTimeErr = checkDropdown(form.Best_Time.value, " When would you like to be contacted?");
    var startDOBErr = startDOB();
    var storageAddErr = isEmpty(form.storage_address.value, " Full Address & Postcode Of Where The Caravan Is Stored");
    var storageTypeErr = checkDropdown(form.Storage_Type.value, " Where Is Your Caravan Stored?");
    var makeErr = checkDropdown(form.Make.value, " Caravan Make");
    var modelErr = isEmpty(form.Model.value, " Caravan Model");
    var yearOfMakeErr = isEmpty(form.Year_Of_Make.value, " Year Of Make");
    var noClaimsErr = checkDropdown(form.Caravan_No_Claims.value, " No Claims Bonus");
    var noOfClaimsErr = checkDropdown(form.Number_Claims.value, " Number of Claims");
    var valOfCaravan = checkDropdown(form.Value_Of_Caravan.value, " Caravan Value");
    var valOfContents = checkDropdown(form.Value_Of_Contents.value, " Caravan Contents Value");
    
    var failedCount = 0;
    var failedField = "";
    
    if (titleErr != ""){
    	why += titleErr;	
    	failedCount = failedCount + 1;
    	failedField = "Title";
    	
    }
    if (nameErr != ""){
    	why += nameErr;
    	failedCount = failedCount + 1;
    	failedField = "Name";    	
    }
    if (homeAddressErr != ""){
    	why += homeAddressErr;	
    	failedCount = failedCount + 1;
    	failedField = "Home_Address";    	
    } 
    if (homePostCodeErr != ""){
    	why += homePostCodeErr;	
    	failedCount = failedCount + 1;
    	failedField = "Home_Postcode";    	
    }     
    if (emailErr != ""){
    	why += emailErr;	
    	failedCount = failedCount + 1;
    	failedField = "email";    	
    }  
    if (contactNumberErr != ""){
    	why += contactNumberErr;
    	failedCount = failedCount + 1;
    	failedField = "Contact_Number";    	
    }   
    if (dobErr != ""){
    	why += dobErr;	
    	failedCount = failedCount + 1;
    	failedField = "DOB";    	
    } 
    if (occupationErr != ""){
    	why += occupationErr;	
    	failedCount = failedCount + 1;
    	failedField = "Occupation";    	
    }     
    if (bestTimeErr != ""){
    	why += bestTimeErr;	
    	failedCount = failedCount + 1;
    	failedField = "Best_Time";    	
    }
    if (startDOBErr != ""){
    	why += startDOBErr;	
    	failedCount = failedCount + 1;
    	failedField = "Start";    	
    } 
    if (storageAddErr != ""){
    	why += storageAddErr;	
    	failedCount = failedCount + 1;
    	failedField = "storage_address";    	
    }  
    if (storageTypeErr != ""){
    	why += storageTypeErr;	
    	failedCount = failedCount + 1;
    	failedField = "Storage_Type";    	
    }      
    if (makeErr != ""){
    	why += makeErr;	
    	failedCount = failedCount + 1;
    	failedField = "Make";    	
    }  
    if (modelErr != ""){
    	why += modelErr;	
    	failedCount = failedCount + 1;
    	failedField = "Model";    	
    }  
    if (yearOfMakeErr != ""){
    	why += yearOfMakeErr;
    	failedCount = failedCount + 1;
    	failedField = "Year_Of_Make";    	
    }  
    if (noClaimsErr != ""){
    	why += noClaimsErr;	
    	failedCount = failedCount + 1;
    	failedField = "Caravan_No_Claims";    	
    }  
    if (noOfClaimsErr != ""){
    	why += noOfClaimsErr;
    	failedCount = failedCount + 1;
    	failedField = "Number_Claims";    	
    } 
    if (valOfCaravan != ""){
    	why += valOfCaravan;	
    	failedCount = failedCount + 1;
    	failedField = "Value_Of_Caravan";    	
    }  
    if (valOfContents != ""){
    	why += valOfContents;
    	failedCount = failedCount + 1;
    	failedField = "Value_Of_Contents";    	
    }      
	
	if (failedCount == 1){
		logEvent("validationfailed", failedField, null);
	} else {
		logEvent("validationfailed", "multipleFields", null);
	}

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

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

function startDOB(){
var error = "";
if ( (document.getElementById("Start_day").value == "") || (document.getElementById("Start_month").value == "0") || (document.getElementById('Start_year').value == "") )
		{
			error = "- Enter A Valid 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;
}

