function validate(form) {
	
	//Regex Validations
	var regIsSalary = /^[0-9]+\,?[0-9]+\.?\d?\d?$/;
	var regIsDay		= /^\d{2}$/;
	var regIsYear		= /^\d{4}$/;
	var regIsEmail	= /^[^\@]+@[^\.]+\.[a-z]{3}$|^[^\@]+@[^\.]+\.[a-z]+\.[a-z]+$/i
	var regIsPhone	= /^\(?[0-9\)\(\+\s]+\d$/
	

	
	if(form.app1Title.value == -1) {
		alert("Please select the title for the first applicant"); form.app1Title.focus(); return false;
	}
	if(form.app1FirstName.value == "") {
		alert("Please enter the first name of the First Applicant"); form.app1FirstName.focus(); return false;
	}	
	if(form.app1Surname.value == "") {
		alert("Please enter the surname of the First Applicant"); form.app1Surname.focus(); return false;
	}
	
	if(!regIsDay.test(form.app1day.value)) {
		alert("Please enter the date of birth of the first applicant in the form dd/mm/yyyy");
		form.app1day.focus();
		return false;	
	}
	
	if(!regIsDay.test(form.app1mth.value)) {
		alert("Please enter the date of birth of the first applicant in the form dd/mm/yyyy");
		form.app1mth.focus();
		return false;	
	} 
	if(!regIsYear.test(form.app1year.value)) {
		alert("Please enter the date of birth of the first applicant in the form dd/mm/yyyy");
		form.app1year.focus();
		return false;	
	}
		
	if(form.app1Employment.value == -1){
		alert("Please enter the employment staus of the first applicant"); form.app1Employment.focus(); return false;
	}
	
	
	if(form.app1Gross.value.length == 0 || regIsSalary.test(form.app1Gross.value) == false)
	{
		alert("Please enter the gross salary of the first applicant as a whole number eg 18000"); form.app1Gross.focus(); return false;
	}
	
	if(!regIsEmail.test(form.email.value))
	{
		alert("Please enter your email address"); form.email.focus(); return false; 
	}
	

	if(!regIsPhone.test(form.dayPhone.value)) {
		alert("Please enter a daytime telephone number we can contact you on"); form.dayPhone.focus(); return false;
	}	
	
	if(!regIsPhone.test(form.evePhone.value)) {
		alert("Please enter an evening telephone number we can contact you on"); form.evePhone.focus(); return false;
	}


	if(form.addr1.value.length == 0) {
		alert("Please enter the first line of your address"); form.addr1.focus(); return false;
	}	
	
	if(form.town.value.length == 0) {
		alert("Please enter your town"); form.town.focus(); return false;
	}	
	
	if(form.county.value.length == 0) {
		alert("Please enter your county"); form.county.focus(); return false;
	}	
	
	if(form.postcode.value.length == 0) {
		alert("Please enter your postcode"); form.postcode.focus(); return false;
	}
	
	if(form.homeowner.value == -1) {
		alert("Please indicate whether you are a homeowner or tenant"); form.homeowner.focus(); return false;
	}

	//This ones for loan amount but we can use the salary regex
	if(!regIsSalary.test(form.loan_amount.value)) {
		alert("Please indicate how much you wish to borrow"); form.loan_amount.focus(); return false;
	}	
	
	if(form.purpose.value == -1) {
		alert("Please indicate the reason you wish to take out a loan"); form.purpose.focus(); return false;
	}
	
	if(form.credit.value == -1) {
		alert("Please indicate whether you have had any CCJs in the last 12 months"); form.credit.focus(); return false;
	}
	
	return true;

}	

function newWindow(URL,name)
{
window.open(URL,name,'width=300,height=400,resizable=no,toolbar=no,menubar=no');
} 


function loanForm() {
//html form	
var win = this.win;

 
win.output =  "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"75%\">";
//win.output += "<form name=\"frmLoan\" method=\"POST\" action=\"http://coopershawk.ukfinancematters.co.uk/loan_ack.html\" onsubmit=\"return validate(this)\">";
win.output += "<form name=\"frmLoan\" method=\"POST\" action=\"cgi-bin/form2email.pl\" onsubmit=\"return validate(this)\">";
win.output +=  "<tr><td colspan=\"2\">Please fill in our fast track loan enquiry form to begin the application process</td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"10\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" class=\"introfaq\">Personal Details</td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"10\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"1\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"10\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" class=\"introfaq\">First Applicant</td>";
win.output +=  "<tr><td  width=\"50\">Title</td><td><select name=\"app1Title\" size=\"1\"><option value=\"-1\" selected>Please Select</option><option value=\"1\">Mr</option><option value=\"2\">Mrs</option><option value=\"3\">Miss</option><option value=\"4\">Dr</option><option value=\"5\">Other</option></select></td></tr>";
win.output +=  "<tr><td> First Name</td>";
win.output +=  "<td><input type=\"text\" name=\"app1FirstName\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td> Surname</td>";
win.output +=  "<td><input type=\"text\" name=\"app1Surname\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td> Date of Birth</td><td><input type=\"text\" name=\"app1day\" size=\"1\"> &nbsp;<input type=\"text\" name=\"app1mth\" size=\"1\"> &nbsp <input type=\"text\" name=\"app1year\" size=\"3\"> <em>dd/mm/yyyy</em></td></tr>";
win.output +=  "<tr><td> Employment Status</td><td><select name=\"app1Employment\" size=\"1\"><option value=\"-1\" selected>Please Select</option><option value=\"1\">Employed</option><option value=\"2\">Self Employed</option><option value=\"3\">Unemployed</option><option value=\"4\">Retired</option><option value=\"5\">Other</option></select></td></tr>";
win.output +=  "<tr><td> Gross Annual Salary(£)</td><td><input type=\"text\" name=\"app1Gross\" size=\"15\" maxlength=\"20\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"20\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" class=\"introfaq\">Second Applicant (If Applicable)</td>";
win.output +=  "<tr><td colspan=\"2\" height=\"1\"></td></tr>";
win.output +=  "<tr><td  width=\"50\"> Title</td><td><select name=\"app2Title\" size=\"1\"><option value=\"-1\" selected>Please Select</option><option value=\"1\">Mr</option><option value=\"2\">Mrs</option><option value=\"3\">Miss</option><option value=\"4\">Dr</option><option value=\"5\">Other</option></select></td></tr>";
win.output +=  "<tr><td> First Name</td>";
win.output +=  "<td><input type=\"text\" name=\"app2FirstName\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td> Surname</td>";
win.output +=  "<td><input type=\"text\" name=\"app2Surname\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td> Date of Birth</td><td><input type=\"text\" name=\"app2day\" size=\"1\"> &nbsp;<input type=\"text\" name=\"app2mth\" size=\"1\"> &nbsp <input type=\"text\" name=\"app2year\" size=\"3\"> <em>dd/mm/yyyy</em></td></tr>";
win.output +=  "<tr><td> Employment Status</td><td><select name=\"app2Employment\" size=\"1\"><option value=\"-1\" selected>Please Select</option><option value=\"1\">Employed</option><option value=\"2\">Self Employed</option><option value=\"3\">Unemployed</option><option value=\"4\">Retired</option><option value=\"5\">Other</option></select></td></tr>";
win.output +=  "<tr><td> Gross Annual Salary(£)</td><td><input type=\"text\" name=\"app2Gross\" size=\"15\" maxlength=\"20\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"20\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" class=\"introfaq\">Contact Details</td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"10\"></td></tr>";
win.output +=  "<tr><td> Email</td><td><input type=\"text\" name=\"email\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td> Daytime Phone </td><td><input type=\"text\" name=\"dayPhone\" size=\"30\" maxlength=\"30\"></td></tr>";
win.output +=  "<tr><td> Evening Phone</td><td><input type=\"text\" name=\"evePhone\" size=\"30\" maxlength=\"30\"></td></tr>";
win.output +=  "<tr><td> Preferred Call Time</td><td><select name=\"call_time\" size=\"1\"><option selected=\"0\">Daytime</option><option value=\"1\">Evening</option></select></td></tr>";
win.output +=  "<tr><td> Address </td><td><input type=\"text\" name=\"addr1\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td></td><td><input type=\"text\" name=\"addr2\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td> Town </td><td><input type=\"text\" name=\"town\" size=\"40\" maxlength=\"100\"></td></tr>";        
win.output +=  "<tr><td> County </td><td><input type=\"text\" name=\"county\" size=\"40\" maxlength=\"100\"></td></tr>";
win.output +=  "<tr><td> Postcode </td><td><input type=\"text\" name=\"postcode\" size=\"20\" maxlength=\"15\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"20\"></td></tr>";
win.output +=  "<tr><td colspan=\"2\" class=\"introfaq\">Loan Details</td></tr>";
win.output +=  "<tr><td> Do you own your own home </td><td align=\"right\"><select name=\"homeowner\" class=\"selectbox\" size=\"1\"><option value=\"-1\" selected>Please Select</option><option value=\"1\">Yes</option><option value=\"0\">No</option></select></td></tr>";
win.output +=  "<tr><td> How much do you wish to borrow </td><td align=\"right\"><input type=\"text\" name=\"loan_amount\" class=\"selectbox\" size=\"14\" maxlength=\"20\"></td></tr>";
win.output +=  "<tr><td> Do you have any CCJs registered in the last 12 months</td><td align=\"right\"><select name=\"credit\" class=\"selectbox\" size=\"1\"><option value=\"-1\" selected>Please Select</option><option value=\"1\">Yes</option><option value=\"0\">No</option></select></td></tr>";
win.output +=  "<tr><td colspan=\"2\" height=\"20\"></td></tr>";
win.output +=  "<tr><td></td><td align=\"right\"> <input class=\"button\" type=\"submit\" name=\"submit\" value=\"Submit Application\"></td></tr>";
//Hidden Elements
//Site Referrer
win.output +=  "<input type=\"hidden\" name=\"ref\" value=\"" + this.ref + "\">";
win.output +=  "<input type=\"hidden\" name=\"page\" value=\"" + this.page + "\">";
win.output +=  "<input type=\"hidden\" name=\"purpose\" value=\"3\">"; //Home Improvements
//NO TRACKING REQUIRED
//win.output +=  "<input type=\"hidden\" name=\"referer\" value=\"" + this.referer + "\">";
//win.output +=  "<input type=\"hidden\" name=\"campaign\" value=\"" + this.campaign + "\">";
//win.output +=  "<input type=\"hidden\" name=\"subcat\" value=\"" + this.subcat + "\">";
win.output += "</form>";
win.output += "</table>";

document.write(win.output);
}

function loanFooter() {
	
	var foot = this.win;
	foot.output  ="<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"75%\" valign=\"top\">";
	foot.output +="<tr><td height=\"20\" class=\"paddingall\"></td></tr>";
	foot.output +="<tr><td><img src=\"http://www.ukfinancematters.co.uk/images/ch_padlock.gif\"></td>";
	foot.output +="<td colspan=\"2\" class=\"introfaq\">Disclosure and use of your information</td></tr>";
	foot.output +="<tr><td colspan=\"2\" height=\"10\"></td></tr>";
	foot.output +="<tr><td colspan=\"2\">By submitting this application you agree for Coopers Hawk Ltd or another broker/lender to search the necessary credit reference agencies on yourself and any other named Person(s) on the application and that we may pass your details to other brokers or lenders, and that broker/lender may contact you for further information as required .<br><br>";
	foot.output +="The personal data relating to you, gathered by us in our enquiry, will be recorded electronically and only used in accordance with the Data Protection Act 1998.";
	foot.output +="Coopers Hawk Ltd, and any other broker or lender we refer this application to may, use the information you have provided from this website in connection with your application.<br> ";
	foot.output +="<tr><td colspan=\"2\" height=\"10\"></td></tr>";
	foot.output +="<tr><td colspan=\"2\" class=\"introfaq\">You should think carefully before securing other debts against your home. Your home may be reposessed if you do not keep up repayments on a mortgage or any other debt secured on it."; 
	foot.output +="<tr><td colspan=\"2\">Finance application processed on behalf of Fenestra by Coopers Hawk Ltd, 1 St. Georges Court, St. Georges Park, Kirkham, Preston, PR4 2EF.  Licensed credit broker 560809.";
  foot.output +="</td></tr></table></td>";
	document.write(foot.output);
}	

//function Loan(ref,page,referer,campaign,subcat)
function Loan(ref,page)
{
 this.ref        = ref;
 this.page       = page;
 //ppc 
 //this.referer    = referer;
 //this.campaign   = campaign;
 //this.subcat     = subcat;
 //general
 this.win        = window;
 this.loanForm   = loanForm;
 this.loanFooter = loanFooter;
}

//Enters Here
//parse out any ppc tracking
/* ################## NO NEED TO TRACK THIS ####################
var regIsPPC		= /^[a-z]+=([a-z]+)$/i;
var referer     = false;
var campaign		= false;
var subcat			= false;

ppc_tracking=location.href.split("?");
if(ppc_tracking[1]) { //if there are params after the ?
	params=ppc_tracking[1].split("&"); //Create an array of params
    //Referer
  isReferer = regIsPPC.test(params[0]); //Make sure they're the right format
	if(isReferer) {
		referer  = params[0].split(/=/);
		referer  = referer[1]; //We want the bit after the =
	}	
	//Campaign	
	isCampaign = regIsPPC.test(params[1]);	
	if(isCampaign) {
		campaign = params[1].split(/=/);
		campaign = campaign[1];
	}		
	//Subcat
	isSubcat = regIsPPC.test(params[2]);	
	if(isSubcat) {	
		subcat   = params[2].split(/=/);
		subcat   = subcat[1];
	}	
}
*/
//And finally build the form object
this_loan = new Loan("fenestra","http://www.fenestra-conservatories.co.uk/");
this_loan.loanForm();
this_loan.loanFooter();

      
 
        
