 /*	
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 checkWholeForm(form) {

    var why = "";
	
	why += isEmpty(form.Home_Address.value, " Home Address");
	why += isEmpty(form.Home_Postcode.value, " Home Postcode");
	why += isEmpty(form.Mortgage_Required.value, " Mortgage Required");
	why += isEmpty(form.Property_Value.value, " Property Value");
	why += isEmpty(form.LTV_Amount.value, " Please Calculate Your Loan To Value Amount Using The 'Calculate' Button");
	
	why += isEmpty(form.Current_Balance.value, " Current Mortgage Balance");
	why += checkDropdown(form.Mortgage_Type.value, " Type Of Mortgage Required");
	why += checkDropdown(form.Repayment_Type.value, " Repayment Type");
	why += checkDropdown(form.Number_Applicants.value, " Number Of Applicants");

	why += checkDropdown(form.Title_1.value, " First Applicant - Title");
	why += isEmpty(form.Name_1.value, " First Applicant - Name");
	why += checkDOB1();
	why += isEmpty(form.email.value, " First Applicant - Email");
	why += isEmpty(form.Contact_Number_1.value, " First Applicant - Contact Number");
	why += checkDropdown(form.Employment_Type_1.value, " First Applicant - Employment Type");
	why += isEmpty(form.Occupation_1.value, " First Applicant - Occupation");
	why += isEmpty(form.Annual_Salary_1.value, " First Applicant - Annual Salary");
	
	
	if (document.getElementById("Number_Applicants").value == "2")
		{
			why += checkDropdown(form.Title_2.value, " Second Applicant - Title");
			why += isEmpty(form.Name_2.value, " Second Applicant - Name");
			why += checkDOB2();
			why += checkDropdown(form.Employment_Type_2.value, " Second Applicant - Employment Type");
			why += isEmpty(form.Occupation_2.value, " Second Applicant - Occupation");
			why += isEmpty(form.Annual_Salary_2.value, " Second Applicant - Annual Salary");			
		}
	
	

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

function checkDOB1(){
var error = "";
if ( (document.getElementById("DOB_day_1").value == "") || (document.getElementById("DOB_month_1").value == "0") || (document.getElementById('DOB_year_1').value == "") )
		{
			error = "- First Applicant - Enter A Valid DOB"+"\n";
		}
	return error;
}

function checkDOB2(){
var error = "";
if ( (document.getElementById("DOB_day_2").value == "") || (document.getElementById("DOB_month_2").value == "0") || (document.getElementById('DOB_year_2').value == "") )
		{
			error = "- Second Applicant - Enter A Valid DOB"+"\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;
}

