function validateApplicationOnSubmit(theForm) {
var reason = "";

  reason += validateCompany(theForm.dealer_name);
	reason += validateAddress1(theForm.dealer_address1);
	reason += validateCity(theForm.dealer_address3);
	reason += validateCounty(theForm.dealer_address4);
	reason += validateCountry(theForm.dealer_address5);
	reason += validatePostcode(theForm.dealer_postcode);
	reason += validateWebsite(theForm.dealer_website);
	reason += validateEquipment(theForm.dealer_equipment);
	reason += validateFullname(theForm.dealer_contact);
	reason += validatePosition(theForm.dealer_contact_position);
	
	reason += validatePhone(theForm.dealer_contact_phone);
	reason += validateMobile(theForm.dealer_contact_mobile);
	
	reason += validateEmail(theForm.dealer_contact_email);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
	
	//calc_payment();
	
	popUp('http://www.f-i-f.co.uk/i-Max/quote-rates.php?email=' + theForm.email.value +
                      '&amount=' + theForm.amount.value +
                     '&deposit=' + theForm.deposit.value +
                    '&residual=' + theForm.residual.value +
                   '&frequency=' + theForm.frequency.value +
                '&adv_payments=' + theForm.adv_payments.value +
              '&total_payments=' + theForm.total_payments.value +
                   '&dealer_id=' + theForm.dealer_id.value);
	
}

function validateQuoteOnSubmit(theForm) {
var reason = "";

  reason += validateAmount(theForm.amount);
	reason += validateTotalPayments(theForm.total_payments);
	reason += validateAdvPayments(theForm.adv_payments);
	reason += validateEmail(theForm.email);
	
	if(theForm.postcode){
	reason += validatePostcodeNew(theForm.postcode);
	var postcode = theForm.postcode.value;
	} else {
	var postcode = "No Postcode";
	};
	
	if(theForm.reference){
	var reference = theForm.reference.value;
	} else {
	var reference = "No Reference";
	};
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
	
	//calc_payment();
	
	popUp('http://www.f-i-f.co.uk/i-Max/quote-rates.php?email=' + theForm.email.value +
                      '&amount=' + theForm.amount.value +
                     '&deposit=' + theForm.deposit.value +
                    '&residual=' + theForm.residual.value +
                   '&frequency=' + theForm.frequency.value +
                '&adv_payments=' + theForm.adv_payments.value +
              '&total_payments=' + theForm.total_payments.value +
					'&postcode=' + postcode +
			       '&reference=' + reference +
                   '&dealer_id=' + theForm.dealer_id.value);
	
}

function validatePostcodeOnSubmit(theForm) {
	var reason = "";
		
		if(theForm.postcode){
		reason += validatePostcodeNew(theForm.postcode);
		var postcode = theForm.postcode.value;
		} else {
		var postcode = "No Postcode";
		};
	      
	  if (reason != "") {
	    alert("Some fields need correction:\n\n" + reason);
	    return false;
	  }
		
		//calc_payment();
		
		popUp('http://www.f-i-f.co.uk/i-Max/local-contact.php?postcode=' + postcode +
	                   '&dealer_id=' + theForm.dealer_id.value);
		
	}

function validateProp1OnSubmit(theForm) {
var reason = "";

  reason += validateTitle(theForm.title);
  reason += validateForename(theForm.forename);
	reason += validateSurname(theForm.surname);
	reason += validateAddress1(theForm.address1);
	reason += validateCity(theForm.city);
	reason += validatePostcode(theForm.postcode);
	reason += validateEmail(theForm.email);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
}


function validateProp2OnSubmit(theForm) {
var reason = "";

  reason += validateTitle(theForm.title);
  reason += validateForename(theForm.forename);
	reason += validateSurname(theForm.surname);
	reason += validateDay(theForm.dob_day);
	reason += validateMonth(theForm.dob_month);
	reason += validateYear(theForm.dob_year);
	reason += validateAddress1(theForm.address1);
	reason += validateCity(theForm.city);
	reason += validatePostcode(theForm.postcode);
	reason += validateEmail(theForm.email);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
}


function validateProp4OnSubmit(theForm) {
var reason = "";

  reason += validateMake(theForm.make);
	reason += validateModel(theForm.model);
	reason += validateCondition(theForm.condition);
	reason += validateYOM(theForm.yom);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
}


function validateRegisterOnSubmit(theForm) {
var reason = "";

  reason += validatePassword(theForm.passwd);
	reason += validateConfirmPassword(theForm.confirmpasswd);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
	return true;
}

function validateLoginOnSubmit(theForm) {
var reason = "";

  reason += validatePassword(theForm.passwd);
      
  if (reason != "") {
    alert("Some fields need correction:\n\n" + reason);
    return false;
  }
	return true;
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}  

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\!\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Email address is required in order to continue.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}


// New Validations added by Ruairidh below

function validateAmount(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Amount cannot be 0.\n"
    } else {
		fld.style.background = 'White';
    }
    return error;  
}

function validateTotalPayments(fld) {
    var error = "";
 
    if (fld.value <= 0) {
        fld.style.background = 'Yellow'; 
        error = "Total Payments cannot be 0.\n"
    } else {
		fld.style.background = 'White';
    }
    return error;  
}

function validateAdvPayments(fld) {
    var error = "";
 
    if (fld.value > document.calc.total_payments.value) {
        fld.style.background = 'Yellow'; 
        error = "There cannot be more Advance payments than Total Payments.\n"
    } else {
		fld.style.background = 'White';
    }
    return error;  
}

function validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You must provide your full name.\n"
    } else {
		fld.style.background = 'White';
    }
    return error;  
}

function validatePhone(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "You must provide your phone number.\n"
    } else {
		fld.style.background = 'White';
    }
    return error;  
}

function validateConfirmPassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't confirm your password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password confirmation is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password confirmation contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password confirmation must contain at least one numeral.\n";
        fld.style.background = 'Yellow';			
		} else if (!((fld.value == document.register.passwd.value))) {
        error = "The passwords entered do not match.\n";
        fld.style.background = 'Yellow';				
    } else {
        fld.style.background = 'White';
    }
   return error;
} 

function validateTitle(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Title is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateForename(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Forename is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateSurname(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Surname is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateAddress1(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Address line 1 is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateCity(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "City is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validatePostcode(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Postcode is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateMake(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Equipment Make is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateModel(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Equipment Model is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateCondition(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Equipment Condition is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateYOM(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Equipment Year of Manufacture is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateDay(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a DOB day.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The Date of Birth contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 2)) {
        error = "The DOB day is the wrong length.\n";
        fld.style.background = 'Yellow';
    } else if ((fld.value < 1) || (fld.value > 31)) {
        error = "The DOB day is the wrong value. \n";
        fld.style.background = 'Yellow';
    } else {
		    fld.style.background = 'White';
		}
    return error;
}

function validateMonth(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a DOB month.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The Date of Birth contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 2)) {
        error = "The DOB month is the wrong length.\n";
        fld.style.background = 'Yellow';
    } else if ((fld.value < 1) || (fld.value > 12)) {
        error = "The DOB day is the wrong value. \n";
        fld.style.background = 'Yellow';
    } else {
		    fld.style.background = 'White';
		}
    return error;
}

function validateYear(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a DOB year.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The Date of Birth contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 4)) {
        error = "The DOB year is the wrong length.\n";
        fld.style.background = 'Yellow';
    } else {
		    fld.style.background = 'White';
		}
    return error;
}

function validateCompany(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Company Name is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateCompany(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Company Name is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateWebsite(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Website Address is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateCCL(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Comsumer Credit License is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateEquipment(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Type of Equipment is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateFullname(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Fullname is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validatePosition(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Position is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validatePhone(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Phone number is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateMobile(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Mobile number is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateCounty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "County is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validateCountry(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Country is required in order to continue.\n"
    } else {
        fld.style.background = 'White'; 
    }
    return error;  
}

function validatePostcodeNew(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var postcodeFilter = /[A-Za-z]{1,2}[0-9]{1,2}/;
    var illegalChars= /[\(\)\<\>\,\;\!\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Postcode is required in order to continue. e.g. KA3\n";
    } else if (!postcodeFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid postcode. e.g. KA3\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The postcode contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


//Postcode Regular Expression - /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;