var w = window;
w.output = "";

// Browser Detection
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;

var SubPrime = "false";
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('.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 smcellstyle = "style=\"font-family: Tahoma; font-size: 10px; border-bottom: solid 1px windowtext #FFFFFF; background-color: F0EEF6;\"";

var headerTableStart = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"507\">";
var nodeTableStart = "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
var tableEnd = "</table>";

var primaryInformation = ["Loan_Amount", "How_Soon_Do_You_Need_The_Money", "Purpose_Of_Loan", "UNSETTLED_Defaults_Or_CCJs_From_The_Last_3_Years"];
var contactDetails = ["ContactLine1", "ContactLine2", "ContactLine3", "ContactLine4", "ContactLine5", "ContactLine6", "ContactLine7", "ContactLine8"];

var loanOutgoings = ["Existing_Mortgage", "Total_Other_Loans", "Monthly_Endowment", "Held_Current_Mortgage_For", "Current_Mortgage_Lender", "Current_Mortgage_Interest_Rate", "Does_Current_Mortgage_Have_A_Redemption_Penalty", "Missed_Mortgage_Payments_In_Last_12_Months"];
var mortgageDetails = ["Move_In_Date", "Ownership_Status", "Date_Purchased", "Council_Purchase"];
//var creditHistory = ["Missed_Mortgage_Payments_In_Last_12_Months"];

var submitDetails = ["SubmitLine1", "SubmitLine2", "SubmitLine3", "SubmitLine4", "SubmitLine5"];

var App1_Details = ["Applicant1DataTags", "Applicant1Data"];
var App2_Details = ["Applicant2DataTags", "Applicant2Data"];


var firstTime = ["Council_Purchase", "Move_In_Date"];
var newHouse = ["Council_Purchase", "Move_In_Date"];
var remortgage = ["Ownership_Status", "Date_Purchased", "Council_Purchase"];



var mortVars = ["Existing_Mortgage", "Monthly_Endowment", "Existing_Mortgage_Type", "Held_Current_Mortgage_For", "Current_Mortgage_Lender", "Current_Mortgage_Interest_Rate", "Does_Current_Mortgage_Have_A_Redemption_Penalty", "Missed_Mortgage_Payments_In_Last_12_Months", "Ownership_Status", "Date_Purchased", "Council_Purchase", "Move_In_Date"];

var existMort = ["Existing_Mortgage", "Monthly_Endowment", "Held_Current_Mortgage_For", "Current_Mortgage_Lender", "Missed_Mortgage_Payments_In_Last_12_Months", "Current_Mortgage_Interest_Rate", "Does_Current_Mortgage_Have_A_Redemption_Penalty"];


function getNodeContent(id)
{
	switch(id)
	{
		case "Loan_Amount"					: display = [getCell(29, arrow, "align=\"right\""), getCell(227, "Mortgage Amount"), getCell(251, getInput("text", id, 16, '', "onchange=\"stringFilter(this)\""))]; 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, '', "onchange=\"stringFilter(this)\""))]; height = 25; break;
		case "Employment_Status"				: display = [getCell(29, arrow, "align=\"right\""), getCell(197, id.replace(/_/g, ' ')), getCell(281, getInput("select", id, 1, ''))]; height = 25; break;
		case "Employment_Status_2"			: display = [getCell(29, arrow, "align=\"right\""), getCell(197, id.replace(/_/g, ' ')), getCell(281, 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, '', "onchange=\"eventCheck(this)\""))]; height = 25; break;
		case "Applicant1IncomeProof"				: display = [getCell(29, arrow, "align=\"right\""), getCell(227, "Proof Of Income"), getCell(251, getInput("select", "Proof_Of_Income", 1, '', "onchange=\"eventCheck(this)\""))]; height = 25; break;

		case "Property_Value"					: display = [getCell(29, arrow, "align=\"right\""), getCell(170, "Property Value Or Purchase Price"), getCell(70, getInput("text", id, 8, '', "onchange=\"stringFilter(this)\"")), getCell(98, "Valuation Accuracy"), getCell(140, getInput("select", "Property_Valuation_Accuracy", 1))]; height = 25; break;
		case "Property_Construction"				: display = [getCell(29, arrow, "align=\"right\""), getCell(227, "Home Construction"), getCell(251, getInput("select", id, 1, '', "onchange=\"eventCheck(this)\""))]; 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 "Best_Time_To_Call"				: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("text", id, 16))]; height = 25; break;
		case "Type_Of_Mortgage_Required"			: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("select", id, 1))]; height = 25; break;
		case "Over_How_Many_Years"				: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("text", id, 16))]; height = 25; break;
		case "Can_You_Provide_15_Percent_Deposit"		: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("select", id, 1))]; height = 25; break;
		case "Job_Title"					: display = [getCell(29, arrow, "align=\"right\""), getCell(227, id.replace(/_/g, ' ')), getCell(251, getInput("text", id, 16))]; 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(89, "Title"), getCell(11, "&nbsp;"), getCell(96, "Firstname"), getCell(11, "&nbsp;"), getCell(96, "Surname"), getCell(11, "&nbsp;"), getCell(96, "Employment Status"), getCell(68, "&nbsp;")]; height = 25; break;
		case "Applicant1Data"					: display = [getCell(29, arrow, "align=\"right\""), getCell(89, getInput("select", "App1_Title", 1)), getCell(11, arrow), getCell(96, getInput("text", "App1_Firstname", 14)), getCell(11, arrow), getCell(96, getInput("text", "App1_Surname", 14)), getCell(5, arrow, "align=\"right\""), getCell(30, getInput("select", "Employment_Status", 1, '')), getCell(63, "&nbsp;")]; 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(89, getInput("select", "App2_Title", 1)), getCell(11, arrow), getCell(96, getInput("text", "App2_Firstname", 14)), getCell(11, arrow), getCell(271, getInput("text", "App2_Surname", 14))]; height = 25; break;
		case "Applicant2DataTags"				: display = [getCell(29, "&nbsp;"), getCell(89, "Title"), getCell(11, "&nbsp;"), getCell(96, "Firstname"), getCell(11, "&nbsp;"), getCell(96, "Surname"), getCell(11, "&nbsp;"), getCell(96, "Employment Status"), getCell(68, "&nbsp;")]; height = 25; break;
		case "Applicant2Data"					: display = [getCell(29, arrow, "align=\"right\""), getCell(89, getInput("select", "App2_Title", 1)), getCell(11, arrow), getCell(96, getInput("text", "App2_Firstname", 14)), getCell(11, arrow), getCell(96, getInput("text", "App2_Surname", 14)), getCell(5, arrow, "align=\"right\""), getCell(30, getInput("select", "Employment_Status_2", 1, '')), getCell(63, "&nbsp;")]; 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, "Existing Mortgage Type"), 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 "Current_Mortgage_Interest_Rate"			: display = [getCell(29, arrow, "align=\"right\""), getCell(189, id.replace(/_/g, ' ')), getCell(289, getInput("text", id, 14))]; height = 25; break;
		case "Does_Current_Mortgage_Have_A_Redemption_Penalty"	: display = [getCell(29, arrow, "align=\"right\""), getCell(302, id.replace(/_/g, ' ')), getCell(22, getInput("radio", id, 1, 2, "onclick=\"eventCheck(this)\"")), 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 "Move_In_Date"					: display = [getCell(29, arrow, "align=\"right\""), getCell(241, id.replace(/_/g, ' ')), getCell(187, getInput("select", id, 1), "align=\"right\""), getCell(50, "&nbsp;")]; height = 25; break;
		case "Ownership_Status"					: display = [getCell(29, arrow, "align=\"right\""), getCell(241, "What Is Your Ownership Status Of Your Property"), getCell(187, getInput("select", id, 1), "align=\"right\""), getCell(50, "&nbsp;")]; height = 25; break;
		case "Date_Purchased"					: display = [getCell(29, arrow, "align=\"right\""), getCell(287, "Date Of Purchase"), 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_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, "Property To Be Mortgaged Purchased From 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 "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, 2)), 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(88, "&nbsp;"), getCell(309, "<b>I Have Read And Understood The Terms &amp; Conditions</b>"), getCell(22, getInput("checkbox", "I_Agree", 1)), getCell(88, "&nbsp;")]; height = 25; break;
		case "SubmitLine5"					: display = [getCell(183, "&nbsp;"), getCell(141, getInput("submit", "Submit", '', "Submit Application")), 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", "First Time House Purchase", "Moving House", "Remortgage - Debt Consolidation", "Remortgage - Refinancing", "Remortgage - Home Improvement", "Remortgage - 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 "App2_Title"					: SelectOption = ["Select", "Mr", "Mrs", "Ms", "Sir", "Dr"]; break;
			case "Type_Of_Mortgage_Required"			: SelectOption = ["Select", "Variable Rate", "Fixed Rate", "Discounted Rate", "Tracker", "Any"]; break;
			case "Can_You_Provide_15_Percent_Deposit"		: SelectOption = ["Select", "Yes", "No - Only Have 10%", "No - Only Have 5%", "Have No Funds Available For Deposit"]; break;
			case "Move_In_Date"					: SelectOption = ["Select", "Next Month", "Within Next 3 Months", "Over 3 Months Away", "Have Not Got A Moving Date", "Have Not Got A Property"]; 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", "Actual Purchase Price"]; 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;
			case "UNSETTLED_Defaults_Or_CCJs_From_The_Last_3_Years"	: SelectOption = ["Select", "No", "Yes", "Uncertain"]; 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
		inputField = "<input type=\"" + type + "\" name=\"" + name + "\" value=\"" + value + "\" " + inputstyle + " " + event + ">";

	return inputField;
}

function eventCheck(field)
{
	eventcount = 0;
	switch(field.name)
	{
		/*case "Employment_Status" :
			checkSubPrime(); break;
		case "Proof_Of_Income" :
			checkSubPrime(); break;
		case "Property_Construction" :
			checkSubPrime(); break;
		case "UNSETTLED_Defaults_Or_CCJs_From_The_Last_3_Years" :
			checkSubPrime(); break;
		case "Purpose_Of_Loan" :
		if ( SubPrime == "true" )
		{
			if ( field.value <= 1 )
			{
				workNode(mortVars, true);
				workNode(firstTime, false);
			}
			else if ( field.value == 2 )
			{
				workNode(mortVars, true);
				workNode(newHouse, false);
				showNode('node_Existing_Mortgage_Type', 'Existing_Mortgage_Type');
			}
			else if ( field.value >= 3 )
			{
				workNode(mortVars, true);
				workNode(remortgage, false);
				showNode('node_Existing_Mortgage_Type', 'Existing_Mortgage_Type');
			}
		}
		break;*/
		
		case "App_Type" :
		if ( field.value == 1 )
		{		
			hideNode('node_App2Title');
			workNode(App2_Details, true);
		}
		else
		{
			if (IE4plus)
			{
				document.all('node_App2Title').innerHTML = buildSubHeading("Second Applicant Details", "node_App2Title");
			}
			else if (NS6)
			{
				document.getElementById('node_App2Title').innerHTML = buildSubHeading("Second Applicant Details", "node_App2Title");
			}
			workNode(App2_Details, false);
 		}
		break;
		/*case "Existing_Mortgage_Type" :
		if ( field.value == 10 || field.value == 0 )
			workNode(existMort, true);
		else
			workNode(existMort, false);
		break;*/
		default :
			//hideNode('node_App_Type');
			hideNode('node_App1SubTitle');
			//hideNode('node_App1Title');
			//hideNode('node_App2TitleEnd');
			//while ( App1_Details[eventcount] )
			//	hideNode( "node_" + App1_Details[eventcount++] );

			//hideNode('node_Best_Time_To_Call');
			//hideNode('node_Type_Of_Mortgage_Required');
			//hideNode('node_Over_How_Many_Years');
			//hideNode('node_Can_You_Provide_15_Percent_Deposit');
			//hideNode('node_Job_Title');

			//hideNode('node_Applicant1MoneyTags');
			//hideNode('node_Applicant1Money');
			eventcount = 0;			
			hideNode('node_App2Title');
			while ( App2_Details[eventcount] )
				hideNode( "node_" + App2_Details[eventcount++] );
			eventcount = 0;
			//hideNode('node_Mortgage_Information');
			//hideNode('node_Existing_Mortgage_Type');
			//hideNode('node_Outgoings');
			//while ( loanOutgoings[eventcount] )
				//hideNode( "node_" + loanOutgoings[eventcount++] );
			//hideNode('node_Property');
			//eventcount = 0;
			//while ( mortgageDetails[eventcount] )
				//hideNode( "node_" + mortgageDetails[eventcount++] );
			//eventcount = 0;
			//hideNode('node_Credit');
			//while ( creditHistory[eventcount] )
			//	hideNode( "node_" + creditHistory[eventcount++] );
			//hideNode('node_CreditEnd');		
	}
}

/*function checkSubPrime()
{
	var flag = "false";
	if ( document.mortgageForm.Employment_Status.value == 3 || document.mortgageForm.Employment_Status.value == 5 )
		flag = "true";
	if ( document.mortgageForm.Proof_Of_Income.value == 9 )
		flag = "true";
	if ( document.mortgageForm.Property_Construction.value > 1 )
		flag = "true";
	if ( document.mortgageForm.UNSETTLED_Defaults_Or_CCJs_From_The_Last_3_Years.value == 2 || document.mortgageForm.UNSETTLED_Defaults_Or_CCJs_From_The_Last_3_Years.value == 3 )
		flag = "true";


	if ( flag == "true" && SubPrime == "false" )
	{
		showSubPrimeFields();
		SubPrime = "true";
		eventCheck(document.mortgageForm.Purpose_Of_Loan);
	}
	else if ( flag == "false" )
	{
		hideSubPrimeFields();
		SubPrime = "false";
	}
}

function showSubPrimeFields()
{
	showNode('node_Best_Time_To_Call', 'Best_Time_To_Call');
	showNode('node_Type_Of_Mortgage_Required', 'Type_Of_Mortgage_Required');
	showNode('node_Over_How_Many_Years', 'Over_How_Many_Years');
	showNode('node_Can_You_Provide_15_Percent_Deposit', 'Can_You_Provide_15_Percent_Deposit');
	showNode('node_Job_Title', 'Job_Title');
	//document.all('node_App1Title').innerHTML = buildHeading("Applicant Details & Second Applicant (if required)");
	showNode('node_App_Type', 'App_Type');
	if (IE4plus)
	{
		document.all('node_App1SubTitle').innerHTML = buildSubHeading("Applicant Details", "node_App1SubTitle");
		document.all('node_App2TitleEnd').innerHTML = buildEnd();
		document.all('node_Mortgage_Information').innerHTML = buildHeading("Mortgage Information");
		document.all('node_Outgoings').innerHTML = buildSubHeading("Outgoings:", "node_Outgoings");
		document.all('node_Property').innerHTML = buildSubHeading("Property Details:", "node_Property");
		document.all('node_CreditEnd').innerHTML = buildEnd();

	}
	else if (NS6)
	{
		document.getElementById('node_App1SubTitle').innerHTML = buildSubHeading("Applicant Details", "node_App1SubTitle");
		document.getElementById('node_App2TitleEnd').innerHTML = buildEnd();
		document.getElementById('node_Mortgage_Information').innerHTML = buildHeading("Mortgage Information");
		document.getElementById('node_Outgoings').innerHTML = buildSubHeading("Outgoings:", "node_Outgoings");
		document.getElementById('node_Property').innerHTML = buildSubHeading("Property Details:", "node_Property");
		document.getElementById('node_CreditEnd').innerHTML = buildEnd();
	}

	//workNode(App1_Details, false);
	showNode('node_Applicant1MoneyTags', 'Applicant1MoneyTags');
	showNode('node_Applicant1Money', 'Applicant1Money');
	//showNode('node_App2TitleEnd', 'App2TitleEnd');
	workNode(loanOutgoings, false);	
	workNode(mortgageDetails, false);	
	//document.all('node_Credit').innerHTML = buildSubHeading("Credit History:", "node_Credit");
	//workNode(creditHistory, false);
	workNode(existMort, true);
}

function hideSubPrimeFields()
{
	eventcount = 0;
	hideNode('node_Best_Time_To_Call');
	hideNode('node_Type_Of_Mortgage_Required');
	hideNode('node_Over_How_Many_Years');
	hideNode('node_Can_You_Provide_15_Percent_Deposit');
	hideNode('node_Job_Title');
	//hideNode('node_App1Title');
	hideNode('node_App1SubTitle');
	hideNode('node_App_Type');
	//while ( App1_Details[eventcount] )
	//	hideNode( "node_" + App1_Details[eventcount++] );
	hideNode('node_Applicant1MoneyTags');
	hideNode('node_Applicant1Money');
	eventcount = 0;			
	hideNode('node_App2Title');
	while ( App2_Details[eventcount] )
		hideNode( "node_" + App2_Details[eventcount++] );
	//hideNode('node_App2TitleEnd');
	eventcount = 0;	
	hideNode('node_Mortgage_Information');
	hideNode('node_Outgoings');
	while ( loanOutgoings[eventcount] )
		hideNode( "node_" + loanOutgoings[eventcount++] );	
	eventcount = 0;
	hideNode('node_Property');
	while ( mortgageDetails[eventcount] )
		hideNode( "node_" + mortgageDetails[eventcount++] );		
	eventcount = 0;
	//hideNode('node_Credit');
	//while ( creditHistory[eventcount] )
	//	hideNode( "node_" + creditHistory[eventcount++] );
	hideNode('node_CreditEnd');				
}*/

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)
{
	if (IE4plus)
	{
		document.all(id).innerHTML = "<table></table>";
	}
	else if (NS6)
	{
		document.getElementById(id).innerHTML = "<table></table>";
	}	
}

function showNode(id, content)
{
	if (IE4plus)
	{
		document.all(id).innerHTML = getNodeContent(content)
	}
	else if (NS6)
	{
		document.getElementById(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=\"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 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_Salary" : 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 "Evening_Phone" : return false; break;
		default : return true; break;
	}
}

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 validateField(type, name, value, form, field)
{
	var notFound = false;
	var numbers=/[123456789]/;
	if ( type == "text" )
	{
		switch(name)
		{
			case "Loan_Amount"			: min = 3000; max = 2500000; message = "Mortgage Amount Must Be Between £3,000 & £2,500,000\n"; 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 (IE4plus)
				{
					if ( value == "" || stringFilter(form(name), true) == "" || value < min || value > max )
						return message;	
				}
				else if (NS6)
				{
					//alert(field.value);
					if ( value == "" || stringFilter(field, 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.1clickmortgages.co.uk/client-scripts/300039/clicksubmit.html\" onsubmit=\"return checkFields(this)\" name=\"mortgageForm\">";
w.output += "<form method=\"post\" action=\"http://www.loans.org/mortgagemail.asp\" onsubmit=\"return checkFields(this)\" name=\"mortgageForm\">";
w.output += "  <input type=\"hidden\" name=\"referrer\" value=\"" + referrer + "\">";
w.output += "  <input type=\"hidden\" name=\"URL\" value=\"" + URL + "?mailReferrer=300039\">";
w.output += "  <input type=\"hidden\" name=\"Website\" value=\"" + Website + "\">";
w.output += "  <input type=\"hidden\" name=\"Location\" value=\"UK Mainland\">";
w.output += "  <input type=\"hidden\" name=\"Product\" value=\"Mortgage\">";
w.output += "  <input type=\"hidden\" name=\"redirect\" value=\"http://www.cheap-mortgage.org/thank-you.php\">";

w.output += buildHeading("Your Mortgage Information");
w.output += displaySection(primaryInformation);
w.output += buildEnd();

w.output += "<div id=\"node_App1Title\">";
w.output += buildHeading("Applicant Details & Second Applicant (if required)");
w.output += "</div>";
w.output += buildNode("App_Type");
w.output += "<div id=\"node_App1SubTitle\">";
w.output += buildSubHeading("Applicant Details");
w.output += "</div>";
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 += "<div id=\"node_App2TitleEnd\">";
w.output += buildEnd();
w.output += "</div>";

// Mortgage Info
/*w.output += "<div id=\"node_Mortgage_Information\">";
w.output += buildHeading("Mortgage Information");
w.output += "</div>";
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 += "<div id=\"node_CreditEnd\">";
w.output += buildEnd();
w.output += "</div>";*/

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);