//
// Written By Mike Pink
// Click Group 2004
//

var w = window;
w.output = "";

var referrer = document.referrer;
var URL = document.URL;
if ( URL.indexOf('.co.uk') != -1 )
	var Website = URL.substring(7, (URL.indexOf('.co.uk')+6));
else if ( URL.indexOf('.com') != -1 )
	var Website = URL.substring(7, (URL.indexOf('.com')+4));
else if ( URL.indexOf('.net') != -1 )
	var Website = URL.substring(7, (URL.indexOf('.net')+4));
else if ( URL.indexOf('.org') != -1 )
	var Website = URL.substring(8, (URL.indexOf('.org')+4));
else if ( URL.indexOf('.uk.com') != -1 )
	var Website = URL.substring(7, (URL.indexOf('.uk.com')+7));
else if ( URL.indexOf('.gb.com') != -1 )
	var Website = URL.substring(7, (URL.indexOf('.gb.com')+7));
else if ( URL.indexOf('.gb.net') != -1 )
	var Website = URL.substring(7, (URL.indexOf('.gb.net')+7));
else if ( URL.indexOf('.uk.net') != -1 )
	var Website = URL.substring(7, (URL.indexOf('.uk.net')+7));

var arrow = "<img src=\"arrow.gif\" width=\"11\" height=\"11\" border=\"0\">";
var inputstyle = "style=\"font-family: Tahoma; font-size: 11px;\"";
var cellstyle = "style=\"font-family: Tahoma; font-size: 11px; border-bottom: solid 1px windowtext #FFFFFF; background-color: F0EEF6;\"";
var headerstyle = "style=\"font-family: Tahoma; font-size: 11px; color: #FFFFFF; font-weight: bold;\"";
var subheaderstyle = "style=\"font-family: Tahoma; font-size: 11px; color: #6C5E97; font-weight: bold; background-color: FFFFFF;\"";

var headerTableStart = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"507\">";
var nodeTableStart = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
var tableEnd = "</table>";

var loanInformation = ["Are_You_A_Home_Owner", "Loan_Amount", "How_Soon_Do_You_Need_The_Money", "Purpose_Of_Loan", "UNSETTLED_Defaults_Or_CCJs_From_The_Last_3_Years"];
var App1_Details = ["Applicant1DataTags", "Applicant1Data", "Employment_Status"];
var App2_Details = ["Applicant2DataTags", "Applicant2Data", "Employment_Status_2"];
var loanOutgoings = ["Existing_Mortgage", "Total_Other_Loans", "Held_Current_Mortgage_For", "Current_Mortgage_Lender"];
var mortgageDetails = ["Ownership_Status", "Property_Construction", "Date_Purchased", "Property_Value", "Property_Valuation_Accuracy", "Council_Purchase"];
var creditHistory = ["Missed_Mortgage_Payments_In_Last_12_Months"];
var contactDetails = ["ContactLine1", "ContactLine2", "ContactLine3", "ContactLine4", "ContactLine5", "ContactLine6", "ContactLine7", "ContactLine8"];
var submitDetails = ["SubmitLine1", "SubmitLine2", "SubmitLine3", "SubmitLine4", "SubmitLine5"];
var smcellstyle = "style=\"font-family: Tahoma; font-size: 10px; border-bottom: solid 1px windowtext #FFFFFF; background-color: F0EEF6;\"";

var existMort = ['Existing_Mortgage', 'Held_Current_Mortgage_For', 'Current_Mortgage_Lender', 'Missed_Mortgage_Payments_In_Last_12_Months'];

var homeowner = ['Existing_Mortgage_Type', 'Ownership_Status', 'Property_Construction', 'Date_Purchased', 'Property_Value', 'Property_Valuation_Accuracy', 'Council_Purchase'];

function getNodeContent(id)
{
	switch(id)
	{
		case "Are_You_A_Home_Owner"			: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(22, getInput("radio", "Homeowner", 1, 1, "onclick=\"eventCheck(this)\"")), getCell(25, "Yes"), getCell(22, getInput("radio", "Homeowner", 1, 2, "onclick=\"eventCheck(this)\"")), getCell(182, "No")]; height = 25; break;
		case "Loan_Amount"				: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("text", id, 16, '', "onchange=\"stringFilter(this)\""))]; height = 25; break;
		case "Existing_Mortgage"			: display = [getCell(29, arrow, "align=\"right\""), getCell(227, "Outstanding Mortgage (approx.)"), getCell(240, getInput("text", "Existing_Mortgage", 14)),getCell(11, "&nbsp;")]; height = 25; break;
		case "How_Soon_Do_You_Need_The_Money"		: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("select", id, 1))]; height = 25; break;
		case "Purpose_Of_Loan"				: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("select", id, 1))]; height = 25; break;
		case "Employment_Status"			: display = [getCell(29, arrow, "align=\"right\""), getCell(128, id.replace(/_/g, ' ')), getCell(350, getInput("select", id, 1))]; height = 25; break;

		case "Employment_Status_2"			: display = [getCell(29, arrow, "align=\"right\""), getCell(128, id.replace(/_/g, ' ')), getCell(350, getInput("select", id, 1))]; height = 25; break;

		case "App_Type"					: display = [getCell(29, arrow, "align=\"right\""), getCell(90, "Single Application"), getCell(29, getInput("radio", id, 1, 1, "onclick=\"eventCheck(this)\"")), getCell(85, "Joint Application"), getCell(274, getInput("radio", id, 1, 2, "onclick=\"eventCheck(this)\""))]; height = 25; break;

		case "Applicant1DataTags"			: display = [getCell(29, "&nbsp;"), getCell(125, "Title"), getCell(11, "&nbsp;"), getCell(96, "Firstname"), getCell(11, "&nbsp;"), getCell(96, "Surname"), getCell(140, "&nbsp;")]; height = 25; break;
		case "Applicant1Data"				: display = [getCell(29, arrow, "align=\"right\""), getCell(125, getInput("select", "App1_Title", 1)), getCell(11, arrow), getCell(96, getInput("text", "App1_Firstname", 14)), getCell(11, arrow), getCell(235, getInput("text", "App1_Surname", 14))]; height = 25; break;
		case "Applicant1MoneyTags"			: display = [getCell(29, "&nbsp;"), getCell(125, "Monthly Salary"), getCell(11, "&nbsp;"), getCell(96, "Monthly Bonus"), getCell(11, "&nbsp;"), getCell(235, "Pensions Or Benefits")]; height = 25; break;
		case "Applicant1Money"				: display = [getCell(29, arrow, "align=\"right\""), getCell(125, getInput("text", "App1_Monthly_Salary", 14, '', "onchange=\"stringFilter(this)\"")), getCell(11, arrow), getCell(96, getInput("text", "App1_Monthly_Bonus", 14, '', "onchange=\"stringFilter(this)\"")), getCell(11, arrow), getCell(235, getInput("text", "App1_Pensions_Or_Benefits", 12, '', "onchange=\"stringFilter(this)\"") + "&nbsp;(enter 0 where not applicable)")]; height = 25; break;
		case "Applicant1IncomeProof"			: display = [getCell(29, arrow, "align=\"right\""), getCell(125, "Proof Of Income"), getCell(353, getInput("select", "Proof_Of_Income", 1))]; height = 25; break;

		case "Applicant2DataTags"			: display = [getCell(29, "&nbsp;"), getCell(125, "Title"), getCell(11, "&nbsp;"), getCell(96, "Firstname"), getCell(11, "&nbsp;"), getCell(96, "Surname"), getCell(140, "&nbsp;")]; height = 25; break;
		case "Applicant2Data"				: display = [getCell(29, arrow, "align=\"right\""), getCell(125, getInput("select", "App2_Title", 1)), getCell(11, arrow), getCell(96, getInput("text", "App2_Firstname", 14)), getCell(11, arrow), getCell(235, getInput("text", "App2_Surname", 14))]; height = 25; break;
		case "Applicant2MoneyTags"			: display = [getCell(29, "&nbsp;"), getCell(125, "Monthly Salary"), getCell(11, "&nbsp;"), getCell(96, "Monthly Bonus"), getCell(11, "&nbsp;"), getCell(235, "Pensions Or Benefits")]; height = 25; break;
		case "Applicant2Money"				: display = [getCell(29, arrow, "align=\"right\""), getCell(125, getInput("text", "App2_Monthly_Salary", 14, '', "onchange=\"stringFilter(this)\"")), getCell(11, arrow), getCell(96, getInput("text", "App2_Monthly_Bonus", 14, '', "onchange=\"stringFilter(this)\"")), getCell(11, arrow), getCell(235, getInput("text", "App2_Pensions_Or_Benefits", 12, '', "onchange=\"stringFilter(this)\"") + "&nbsp;(enter 0 where not applicable)")]; height = 25; break;

		//case "Existing_Mortgage_Type"			: display = [getCell(29, arrow, "align=\"right\""), getCell(189, "Current Mortgage Is"), getCell(289, getInput("select", id, 1, '', "onchange=\"eventCheck(this)\"") + "&nbsp;&nbsp;(if none select &quot;no mortgage&quot;)")]; height = 25; break;
		//case "Existing_Mortgage"			: display = [getCell(29, arrow, "align=\"right\""), getCell(189, "Current Mortgage"), getCell(124, getInput("text", id, 14, '', "onchange=\"stringFilter(this)\"")),getCell(11, "&nbsp;"), getCell(11, arrow), getCell(143, getInput("text", "Mortgage_Monthly_Repayment", 14, '', "onchange=\"stringFilter(this)\""))]; height = 25; break;
		case "Total_Other_Loans"			: display = [getCell(29, arrow, "align=\"right\""), getCell(189, id.replace(/_/g, ' ')), getCell(124, getInput("text", id, 14, '', "onchange=\"stringFilter(this)\"")),getCell(11, "&nbsp;"), getCell(11, arrow), getCell(143, getInput("text", "Other_Loans_Monthly_Repayments", 14, '', "onchange=\"stringFilter(this)\""))]; height = 25; break;
		case "Monthly_Endowment"			: display = [getCell(29, arrow, "align=\"right\""), getCell(189, id), getCell(146, arrow, "align=\"right\""), getCell(143, getInput("text", id, 14, '', "onchange=\"stringFilter(this)\""))]; height = 25; break;
		case "Held_Current_Mortgage_For"		: display = [getCell(29, arrow, "align=\"right\""), getCell(189, "Held A Mortgage For"), getCell(289, getInput("select", id, 1) + "&nbsp;&nbsp;(including previous mortgage)")]; height = 25; break;
		case "Current_Mortgage_Lender"			: display = [getCell(29, arrow, "align=\"right\""), getCell(189, id.replace(/_/g, ' ')), getCell(289, getInput("text", id, 14))]; height = 25; break;

		case "Ownership_Status"				: display = [getCell(29, arrow, "align=\"right\""), getCell(241, "Sole / Joint Ownership?"), getCell(187, getInput("select", id, 1), "align=\"right\""), getCell(50, "&nbsp;")]; height = 25; break;
		case "Property_Construction"			: display = [getCell(29, arrow, "align=\"right\""), getCell(225, "Home Construction"), getCell(203, getInput("select", id, 1), "align=\"right\""), getCell(50, "&nbsp;")]; height = 25; break;
		case "Date_Purchased"				: display = [getCell(29, arrow, "align=\"right\""), getCell(287, "Owned Home Since"), getCell(141, "&nbsp;" + getInput("text", "DOP_Month", 2, 'mm', "onblur=\"stringFilter(this)\"") + "&nbsp;" + getInput("text", "DOP_Year", 4, 'yyyy', "onblur=\"stringFilter(this)\""), "align=\"right\""), getCell(50, "&nbsp;")]; height = 25; break;
		case "Property_Value"				: display = [getCell(29, arrow, "align=\"right\""), getCell(306, "Current Home Value"), getCell(122, getInput("text", id, 16, '', "onchange=\"stringFilter(this)\""), "align=\"right\""), getCell(50, "&nbsp;")]; height = 25; break;
		case "Property_Valuation_Accuracy"		: display = [getCell(29, arrow, "align=\"right\""), getCell(306, "How Accurate Is This Valuation"), getCell(122, getInput("select", id, 1), "align=\"right\""), getCell(50, "&nbsp;")]; height = 25; break;
		case "Council_Purchase"				: display = [getCell(29, arrow, "align=\"right\""), getCell(340, "Did You Buy Your Property From The Council"), getCell(22, getInput("radio", id, 1, 1)), getCell(30, "Yes"), getCell(22, getInput("radio", id, 1, 2)), getCell(30, "No"), getCell(34, "&nbsp;")]; height = 25; break;

		case "Missed_Mortgage_Payments_In_Last_12_Months"	: display = [getCell(29, arrow, "align=\"right\""), getCell(286, id.replace(/_/g, ' ')), getCell(192, getInput("select", id, 1))]; height = 25; break;
		case "UNSETTLED_Defaults_Or_CCJs_From_The_Last_3_Years"	: display = [getCell(29, arrow, "align=\"right\""), getCell(302, "Any UNSETTLED Defaults Or CCJs From The Last 3 Years?"), getCell(22, getInput("radio", id, 1, 2)), getCell(25, "Yes"), getCell(22, getInput("radio", id, 1, 1)), getCell(20, "No"), getCell(22, getInput("radio", id, 1, 3)), getCell(65, "Uncertain")]; height = 25; break;

		case "Type_Of_Tenant"				: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("select", id, 1))]; height = 25; break;

		case "ContactLine1"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "Email Address"), getCell(378, getInput("text", "Email_Address", 43))]; height = 25; break;
		case "ContactLine2"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "Daytime Phone"), getCell(378, getInput("text", "Daytime_Phone", 25))]; height = 25; break;
		case "ContactLine3"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "Evening Phone"), getCell(378, getInput("text", "Evening_Phone", 25))]; height = 25; break;
		case "ContactLine4"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "Mobile Phone"), getCell(378, getInput("text", "Mobile_Phone", 25))]; height = 25; break;
		case "ContactLine5"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "Address"), getCell(378, getInput("text", "Address_1", 43))]; height = 25; break;
		case "ContactLine6"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "Town"), getCell(378, getInput("text", "Town", 43))]; height = 25; break;
		case "ContactLine7"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "County"), getCell(378, getInput("text", "County", 43))]; height = 25; break;
		case "ContactLine8"				: display = [getCell(29, arrow, "align=\"right\""), getCell(100, "Postcode"), getCell(378, getInput("text", "Postcode", 15))]; height = 25; break;

		case "SubmitLine1"				: display = [getCell(18, "&nbsp;"), getCell(471, "We are committed to reducing the cost of financial products on the web and would like to keep you informed of developments and offers with our quarterly newsletter."), getCell(18, "&nbsp;")]; height = 45; break;
		case "SubmitLine2"				: display = [getCell(119, "&nbsp;"), getCell(181, "Would you like to receive this?"), getCell(22, getInput("radio", "Newsletter", 1, 1, "checked")), getCell(25, "Yes"), getCell(22, getInput("radio", "Newsletter", 1, 3)), getCell(20, "No"), getCell(118, "&nbsp;")]; height = 25; break;
		case "SubmitLine3"				: display = [getCell(18, "&nbsp;"), getCell(471, "<textarea cols=\"110\" rows=\"2\" " + smcellstyle + ">By submitting this application I/We agree that:-\nThe information provided is true and accurate, and that you, as a broker, may provide details of this application, to other brokers or lending companies, you feel best suited to fulfil my/our requirements, and that broker/lender may contact me/us for further information as required. A lender may use the information held by credit reference agencies in support of our application, and may ask a third party to verify any information given in the application. You may hold the information given in this application on computer for as long as it takes to complete the loan, or until notified if opting for the email newsletter.</textarea>"), getCell(18, "&nbsp;")]; height=55; break;
		case "SubmitLine4"				: display = [getCell(217, "&nbsp;"), getCell(50, "<b>I Agree</b>"), getCell(22, getInput("checkbox", "I_Agree", 1)), getCell(218, "&nbsp;")]; height = 25; break;
		case "SubmitLine5"				: display = [getCell(183, "&nbsp;"), getCell(141, getInput("submit", "Submit", '', "Submit Enquiry")), getCell(183, "&nbsp;")]; height = 25; break;
	}

	count = 0;
	cellOutput = "";
	while ( display[count] )
		cellOutput += display[count++];
	nodeContent = nodeTableStart + "<tr height=\"" + height + "\">" + cellOutput + "</tr>" + tableEnd;
	return nodeContent;
}

function getCell(cellsize, content, align)
{
	cellOutput = "<td width=\"" + cellsize + "\" " + cellstyle + " " + align + ">" + content + "</td>";
	return cellOutput;
}

function getInput(type, name, fieldsize, value, event)
{
	if ( !event )
		event = '';
	if ( !value )
		value = '';
	if ( type == "text" )
		inputField = "<input type=\"text\" name=\"" + name + "\" size=\"" + fieldsize + "\" " + inputstyle + " " + event + " value=\"" + value + "\">"; 
	else if ( type == "select" )
	{
		switch(name)
		{
			case "How_Soon_Do_You_Need_The_Money"			: SelectOption = ["Select", "ASAP", "Within Next Month", "Within Next 3 Months", "Not Urgent"]; break;
			case "Purpose_Of_Loan"					: SelectOption = ["Select", "Debt Consolidation", "Refinancing", "House Purchase", "Home Improvement", "Remortgage - Home Improvement", "Wedding", "Car", "Holiday", "Business", "Other"]; break;
			case "Employment_Status"				: SelectOption = ["Select", "Employed", "Unemployed", "Self-Employed", "Director", "Director-Owner", "Retired", "Agency Worker"]; break;
			case "Employment_Status_2"				: SelectOption = ["Select", "Employed", "Unemployed", "Self-Employed", "Director", "Director-Owner", "Retired", "Agency Worker"]; break;
			case "App1_Title"					: SelectOption = ["Select", "Mr", "Mrs", "Ms", "Sir", "Dr"]; break;
			case "Type_Of_Tenant"					: SelectOption = ["Select", "Council Tenant", "MOD Tenant", "With Parents", "Housing Executive Tenant", "Private Landlord Tenant", "Agency Rental Tenant", "Living With Friend", "Housing Association Tenant"]; break;
			case "App2_Title"					: SelectOption = ["Select", "Mr", "Mrs", "Ms", "Sir", "Dr"]; break;
			case "Proof_Of_Income"					: SelectOption = ["Select", "Printed Payslips", "Handwritten Payslips", "Accountants Letter", "3 Years Accounts", "2 Years Accounts", "1 Years Accounts", "Employers Letter", "Just Bank Accounts Available", "None - Want To Self-Declare"]; break;
			//case "Existing_Mortgage_Type"				: SelectOption = ["Select", "Endowment", "Repayment", "Flexible", "Current A/C", "Open-Plan", "Lifestyle", "Part-Part", "Home Start", "Interest Only", "No Mortgage"]; break;
			case "Held_Current_Mortgage_For"			: SelectOption = ["Select", "Under 6 months", "6-12 Months", "1-2 Years", "2-3 Years", "Over 3 Years"]; break;
			case "Ownership_Status"					: SelectOption = ["Select", "Sole Owner", "Joint Owner (With Partner)", "Part Owner", "Other"]; break;
			case "Property_Construction"				: SelectOption = ["Select", "Standard - Brick/Tile", "Timber", "Concrete", "Concrete Block", "Timber Frame + Brick Overleaf", "Other"]; break;
			case "Property_Valuation_Accuracy"			: SelectOption = ["Select", "Very Accurate", "Good Estimate", "Rough Estimate", "Guess"]; break;
			case "Missed_Mortgage_Payments_In_Last_12_Months"	: SelectOption = ["Select", "None", "1 - Now Settled", "2 - Now Settled", "More Than 2 - Now Settled", "1 - Still Outstanding", "2 - Still Outstanding", "More Than 2 - Still Outstanding"]; break;
		}
		selectCount = 0;
		inputField = "<select name=\"" + name + "\" size=\"" + fieldsize + "\" " + inputstyle + " " + event + ">";
		while ( SelectOption[selectCount] )
		{
			if ( SelectOption[selectCount] == value )
				selected = " selected";
			else
				selected = "";
			inputField += "<option value=\"" + selectCount + "\"" + selected + ">" + SelectOption[selectCount++] + "</option>";
		}
		inputField += "</select>";
	}
	else if ( type == "checkbox" )
		inputField = "<input type=\"" + type + "\" name=\"" + name + "\" " + inputstyle + " " + event + ">";
	else
		inputField = "<input type=\"" + type + "\" name=\"" + name + "\" value=\"" + value + "\" " + inputstyle + " " + event + ">";

	return inputField;
}

function eventCheck(field)
{
	eventcount = 0;
	switch(field.name)
	{
		/*case "Existing_Mortgage_Type" :
		if ( field.value == 10 || field.value == 0 )
			workNode(existMort, true);
		else
			workNode(existMort, false);
		break;*/
		case "App_Type" :
		if ( field.value == 1 )
		{		
			hideNode('node_App2Title');
			workNode(App2_Details, true);
		}
		else
		{
			document.all('node_App2Title').innerHTML = buildSubHeading("Second Applicant Details", "node_App2Title");
			workNode(App2_Details, false);
 		}
		break;
		case "Homeowner" :
		if ( field.value == 2 )
		{
			hideNode("node_Existing_Mortgage");
		}
		else
		{
			showNode("node_Existing_Mortgage", "Existing_Mortgage");
		}
		break;
		default :
		//hideNode('node_Type_Of_Tenant');
		//workNode(existMort, true);
		hideNode('node_App2Title');
		//hideNode('node_Property');
		//hideNode('node_Credit');
		//workNode(homeowner, true);
		eventcount = 0;
		while ( App2_Details[eventcount] )
			hideNode( "node_" + App2_Details[eventcount++] );
		break;
	}
}

function workNode(node, hide, mort)
{
	eventcount = 0;
	while ( node[eventcount] )
	{
		if ( hide == true )
			hideNode( "node_" + node[eventcount++] );
		else
			showNode( "node_" + node[eventcount], node[eventcount++] );
	}
}

function hideNode(id)
{
	document.all(id).innerHTML = "<table></table>";
}

function showNode(id, content)
{
	document.all(id).innerHTML = getNodeContent(content)
}

function buildNode(id)
{
	node = "<div id=\"node_" + id + "\">";
	node += getNodeContent(id);
	node += "</div>";
	return node;
}

function displaySection(section)
{
	sectionOutput = "";
	nodecount = 0;
	while ( section[nodecount] )
		sectionOutput += buildNode(section[nodecount++]);
	return sectionOutput; 
}

function buildHeading(title)
{
	//headerOutput = headerTableStart + "<tr height=\"34\"><td width=\"29\"><img src=\"http://217.72.168.202/client-scripts/uk-mortgage-quote.co.uk/application/titleleft.gif\" width=\"29\" height=\"34\"></td><td " + headerstyle + " background=\"http://217.72.168.202/client-scripts/uk-mortgage-quote.co.uk/application/titleback.gif\">" + title + "</td></tr>";
	headerOutput = headerTableStart + "<tr height=\"34\"><td width=\"29\"><img src=\"titleleft.gif\" width=\"29\" height=\"34\"></td><td " + headerstyle + " background=\"titleback.gif\">" + title + "</td></tr>";
	headerOutput += "<tr height=\"1\"><td colspan=\"2\" background=\"topfill.gif\"></td></tr>" + tableEnd;
	return headerOutput;
}

function buildSubHeading(title)
{
	if ( title == "Outgoings:" )
		headerOutput = 	headerTableStart + "<tr height=\"25\"><td width=\"29\" " + subheaderstyle + ">&nbsp;</td><td " + subheaderstyle + " width=\"189\">" + title + "</td><td " + subheaderstyle + " width=\"148\">Balance</td><td " + subheaderstyle + ">Monthly Repayment</td></tr>" + tableEnd;
	else
		headerOutput = 	headerTableStart + "<tr height=\"25\"><td width=\"29\" " + subheaderstyle + ">&nbsp;</td><td " + subheaderstyle + ">" + title + "</td></tr>" + tableEnd;
	return headerOutput;
}

function buildEnd()
{
	endOutput = headerTableStart + "<tr height=\"1\"><td colspan=\"2\" background=\"botfill.gif\"></td></tr>" + tableEnd;
	return endOutput;
}

function stringFilter (input, onSub)
{
	if (input.value != "" && input.value != "unknown")
	{
		s = input.value;
		filteredValues = "1234567890";     // Characters to leave
		var i;
		var returnString = "";
		ignoreRest = false;

		for (i = 0; i < s.length; i++)
		{
			// Search through string and append to unfiltered values to returnString.
			var c = s.charAt(i);
			if (c == ".")
				ignoreRest = true;
			if (filteredValues.indexOf(c) != -1 && !ignoreRest)
			// numeric char, add it to the string
				returnString += c;
			if ((c == "k" || c ==  "K") && !ignoreRest)
			// convert k to 000
				returnString += "000";
		}
		if ( onSub )
			return returnString;
		else
			input.value = returnString;
	}
}

function requiredFields(inputName, formType)
{
	switch(inputName)
	{
		case "Total_Other_Loans" : return false; break;
		case "Other_Loans_Monthly_Repayments" : return false; break;
		case "Job_Title" : return false; break;
		case "App1_Monthly_Bonus" : return false; break;
		case "App1_Pensions_Or_Benefits" : return false; break;
		case "Monthly_Endowment" : return false; break;
		case "App2_Monthly_Bonus" : return false; break;
		case "App2_Pensions_Or_Benefits" : return false; break;
		case "Address_2": return false; break;
		case "Mobile_Phone" : return false; break;
		case "App1_Monthly_Salary" : return false; break;
		case "Evening_Phone" : return false; break;
		default : return true; break;
	}
}

function validateField(type, name, value, form)
{
	var notFound = false;
	var numbers=/[123456789]/;
	if ( type == "text" )
	{
		switch(name)
		{
			case "Loan_Amount"			: min = 0; max = 2500000; message = "Loan Amount"; break;
			case "Property_Value"			: min = 10000; max = 2000000; unknown = true; message = "Home Value Must Be Between £10,000 & £2,000,000 - enter 'unknown' if you do not know\n"; break;
			case "Existing_Mortgage"		: min = 0; max = 1000000; unknown = true; message = "Existing Mortgage Total Must Be Between £0 & £1,000,000 - enter 'unknown' if you do not know\n"; break;
			case "Mortgage_Monthly_Repayment"	: min = 0; max = 10000; unknown = true; message = "Mortgage Monthly Repayment Must Be Between £0 & £10,000 - enter 'unknown' if you do not know\n"; break;
			case "Monthly_Endowment"		: min = 0; max = 10000; message = "Monthly Endowment Payment Must Be Between £0 & £10,000\n"; break;
			case "Total_Other_Loans"		: min = 0; max = 200000; message = "Other Loans Total Balance Must Be Between £0 & £200,000\n"; break;
			case "Other_Loans_Monthly_Repayments"	: min = 0; max = 10000; message = "Other Loans Monthly Repayments Must Be Between £0 & £10,000\n"; break;
			case "App1_Monthly_Salary"		: min = 0; max = 10000; unknown = true; message = "Monthly Salary Must Be Between £0 & £10,000 - enter 'unknown' if you do not know\n"; break;
			case "App1_Monthly_Bonus"		: min = 0; max = 10000; unknown = true; message = "Monthly Bonus Must Be Between £0 & £10,000 - enter 'unknown' if you do not know\n"; break;
			case "App1_Pension_Or_Benefits"		: min = 0; max = 10000; unknown = true; message = "Pension/Benefits Must Be Between £0 & £10,000 - enter 'unknown' if you do not know\n"; break;
			case "App2_Monthly_Salary"		: min = 0; max = 10000; unknown = true; message = "Monthly Salary Must Be Between £0 & £10,000 - enter 'unknown' if you do not know\n"; break;
			case "App2_Monthly_Bonus"		: min = 0; max = 10000; unknown = true; message = "Monthly Bonus Must Be Between £0 & £10,000 - enter 'unknown' if you do not know\n"; break;
			case "App2_Pension_Or_Benefits"		: min = 0; max = 10000; unknown = true; message = "Pension/Benefits Must Be Between £0 & £10,000 - enter 'unknown' if you do not know\n"; break;
			case "Existing_Mortgage_Balance"	: min = 0; 
			if ( form.Home_Value ) {
				max = ( value > form.Current_Home_Value * 1.5 ); message = "Mortgage Balance Must Not Be Greater Than 1.5 x Your Home Value\n"; }
			else {
				max = 1000000; message = "Mortgage Balance Must Be Between £0 & £1,000,000\n"; }
			break;
			case "App1_DOB_Day"			: min = 1; max = 31; message = "Date Of Birth Day - Invalid Day (1-31)\n"; break;
			case "App1_DOB_Month"			: min = 1; max = 12; message = "Date Of Birth Month - Invalid Month (1-12)\n"; break;
			case "App1_DOB_Year"			: min = 1900; max = 2000; message = "Date Of Birth Year - Invalid Year (1900-2000)\n"; break;
			case "App2_DOB_Day"			: min = 1; max = 31; message = "Date Of Birth Day - Invalid Day (1-31)\n"; break;
			case "App2_DOB_Month"			: min = 1; max = 12; message = "Date Of Birth Month - Invalid Month (1-12)\n"; break;
			case "App2_DOB_Year"			: min = 1900; max = 2000; message = "Date Of Birth Year - Invalid Year (1900-2000)\n"; break;
			case "DOP_Month"			: min = 1; max = 12; message = "Date Of Purchase Month - Invalid Month (1-12)\n"; break;
			case "DOP_Year"				: min = 1900; max = 2050; message = "Date Of Purchase Year - Invalid Year (1900+)\n"; break;
			default : notFound = true; break;
		}
		if ( !notFound )
		{
			if ( value != 'unknown' ) {
				if ( value == "" || stringFilter(form(name), true) == "" || value < min || value > max )
					return message;	
			}
		}
	}
}

function checkFields(form)
{
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;
	var error=false;
	errorMessage = "We cannot accept your application until the following fields are completed\n----------------------------------------------------------------------------------------\n";
	myField = "";
	var firstError = null;
	//headerMessage = "We cannot accept your application until the following fields are completed\n----------------------------------------------------------------------------------------\n";

	for ( i = 0; i <= (form.elements.length - 2); i++ )
	{
		var field = form.elements[i];
		if ( requiredFields(field.name) )
		{
			if ( field.type == "select-one" && field.value == "0" )
			{
				errorMessage += field.name.replace(/_/g, ' ') + '\n';
				error = true;
				if(firstError == null)
					firstError = field;
				//alert(errorMessage);
				//document.all(field.name).focus();
				//return false;
			}
			else if ( field.type == "text" )
			{
				if ( validateField('text', field.name, field.value, form) )
				{
					errorMessage += message + '\n';
					error = true;
					if(firstError == null)
						firstError = field;
					//alert(errorMessage);
					//document.all(field.name).focus();
					//return false;
				}
				else if ( field.value == "" )
				{
					errorMessage += field.name.replace(/_/g, ' ') + '\n';
					error = true;
					if(firstError == null)
						firstError = field;
					//alert(errorMessage);
					//document.all(field.name).focus();
					//return false;
				}
				else if ( field.name == "Email_Address" )
				{
					if (((field.value.search(exclude) != -1)||(field.value.search(check)) == -1)||(field.value.search(checkend) == -1))
					{
						errorMessage += 'Please Enter A Valid Email Address\n';
						error = true;
						if(firstError == null)
							firstError = field;
						//alert(errorMessage);
						//document.all(field.name).focus();
						//return false;
					}
				}
			}
			else if ( field.type == "radio" && myField != field.name )
			{
				myField = field.name;
				radiochk = false;

				for ( n = 0; n < form(myField).length; n++ )
				{
					el = form(myField)[n];
					radiochk = radiochk || el.checked;
				}
				if ( !radiochk )
				{
					if ( field.name == "App_Type" )
						errorMessage += 'Please State Single Or Joint App\n';
					else if ( field.name == "Homeowner" )
						errorMessage += 'Please State Whether You Are A Homeowner\n';
					else
						errorMessage += field.name.replace(/_/g, ' ') + '\n';
					
					error = true;
					if(firstError == null)
						firstError = field;
					//alert(errorMessage);
					//return false;
				}
				else
					myField.checked = true;

			}
			else if ( field.type == "checkbox" && !field.checked )
			{
				errorMessage += field.name.replace(/_/g, ' ') + '\n';
				error = true;
				if(firstError == null)
					firstError = field;
				//alert(errorMessage);
				//return false;
			}
		}
	}
	if(error){
		alert(errorMessage);
		
		if( firstError.type == "text" || firstError.type == "select-one" )
			document.all(firstError.name).focus();
		else
			document.location = "#";
		return false;
	}
}

w.output += "<form method=\"post\" action=\"http://www.hostyourscripts.co.uk/pixelcode/300031/clicksubmit.html\" onsubmit=\"return checkFields(this)\">";
//w.output += "<form method=\"post\" action=\"thankyou.php\" onsubmit=\"return checkFields(this)\">";
w.output += "  <input type=\"hidden\" name=\"referrer\" value=\"300031\">";
w.output += "  <input type=\"hidden\" name=\"URL\" value=\"" + URL + "?mailReferrer=300031\">";
w.output += "  <input type=\"hidden\" name=\"Website\" value=\"" + Website + "\">";
w.output += "  <input type=\"hidden\" name=\"Location\" value=\"UK Mainland\">";

w.output += buildHeading("Your Loan Enquiry");
w.output += displaySection(loanInformation);
w.output += buildEnd();

w.output += "<div id=\"node_Existing_Mortgage\"></div>";

w.output += buildHeading("Applicant Details & Second Applicant (if required)");
w.output += buildNode("App_Type");
w.output += displaySection(App1_Details);

w.output += "<div id=\"node_App2Title\">";
w.output += buildSubHeading("Second Applicant Details");
w.output += "</div>";
w.output += displaySection(App2_Details);
w.output += buildEnd();

//w.output += "<div id=\"node_Existing_Mortgage\"></div>";

/*w.output += buildHeading("Financial Information");
w.output += buildNode("Existing_Mortgage_Type");
w.output += "<div id=\"node_Outgoings\">";
w.output += buildSubHeading("Outgoings:");
w.output += "</div>";
w.output += displaySection(loanOutgoings);

w.output += "<div id=\"node_Property\">";
w.output += buildSubHeading("Property Details:");
w.output += "</div>";
w.output += displaySection(mortgageDetails);

w.output += "<div id=\"node_Credit\">";
w.output += buildSubHeading("Credit History:");
w.output += "</div>";
w.output += displaySection(creditHistory);
w.output += buildEnd();*/

w.output += buildHeading("Contact Information");
w.output += displaySection(contactDetails);
w.output += buildEnd();

w.output += buildHeading("Submit Your Application");
w.output += displaySection(submitDetails);
w.output += buildEnd();
w.output += "</form>";

field = '';
field.name = '';
document.write(w.output);
eventCheck(field);