
// Declare Styles
inputstyle = "style=\"font-family: Arial; font-size: 11px; color: #525252;\"";
formtext = "style=\"font-family: Arial; font-size: 12px; color: #525252;\"";
formtextsm = "style=\"font-family: Arial; font-size: 11px; color: #525252;\"";
formtextun = "style=\"font-family: Arial; font-size: 12px; color: #525252; border-bottom: solid 1px windowtext #E5E5E5;\"";

// Display Form
buildform();

// Build Rows
function createrow(input, field, merge, height, count)
{
	if ( count == "8" || count == "27" )
		row = "</table><br><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"465\"><tr height=\"" + height + "\">";
	else
		row = "<tr height=\"" + height + "\">";

	if ( count <= 7 )
	{	
		if ( count < 4 )
			row += "<td width=\"465\" " + formtext + ">" + input + "</td>";
		else if ( count == 4 )
			row += "<td width=\"465\" " + formtextsm + ">" + input + "</td>";
		else
			row += "<td width=\"465\" " + formtextun + ">" + input + "</td>";
	}
	else
	{
		if ( !merge )
		{
			row += "<td width=\"130\" " + formtextun + ">" + field + "</td>";
			row += "<td width=\"336\" " + formtextun + ">" + input + "</td>";
		}
		else
		{
			if ( count == 29 ) 
				row += "<td colspan=\"2\" " + formtext + " align=\"center\">" + input + "</td>";
			else
				row += "<td colspan=\"2\" " + formtextun + ">" + input + "</td>";
		}
	}
	row += "</tr>";
	return row;
}

// Construct the output string
function buildform()
{
	totalfields = 28;
	var w = window;
	w.output = "<form action=\"http://www.1clicklifeinsurance.co.uk/pixelcode/unitedlife.co.uk/control.php\" method=\"post\" name=\"LifeApplication\" onsubmit=\"return checkFields(this)\">";
	w.output += "<input type=\"hidden\" name=\"sitename\" value=\"www.unitedlife.co.uk\">";
	w.output += "<input type=\"hidden\" name=\"referrer\" value=\"200077\">";
	w.output += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"465\">";

	for ( i = 1; i <= totalfields; i++ )
	{
		merge = false;
		height = 25;

		switch(i)
		{
			//
			// Policy Details - Construction Code
			//
			case 1: input = "<b>All fields must be completed for us to give you a valid Life Insurance quote</b>"; field = "maintitle"; merge = true; break;
			case 2: input = "<b><font color=\"#00578A\">Select The Policy Type You Require</font></b>"; field = "title"; merge = true; break;
			case 3: input = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
					+ "<tr><td width=\"22\"><input type=\"radio\" name=\"select_cover_type\" value=\"Life Insurance Only\" checked></td><td width=\"419\" " + formtext + "><b>Life Insurance Only</b> (includes Terminal Illness Cover at no extra cost)</td></tr>"
					+ "<tr><td width=\"22\"><input type=\"radio\" name=\"select_cover_type\" value=\"Critical Illness Only\"></td><td " + formtext + "><b>Critical Illness Cover Only</b></td></tr>"
					+ "<tr><td width=\"22\"><input type=\"radio\" name=\"select_cover_type\" value=\"Life Insurance + CI\"></td><td " + formtext + "><b>One Policy Covering Both Life Insurance & Critical Illness Cover</b></td></tr>"
					+ "</table><br>"; merge = true; break;
//			case 4: input = "If you select \'One Policy Covering Both Life Insurance & Critical Illness Cover\' please note that this will give joint Life/Critical Illness cover on a \'first event\' basis. i.e. Life cover will NOT continue after payment for Critical Illness. If continuing cover is required then two separate policies must be purchased."; merge = true; height = 78; break;
			case 4: input = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>"
					+ "<td " + formtext + " width=\"117\">Amount Of Cover £ *</td>"
					+ "<td width=\"124\"><input type=\"text\" name=\"Life_Cover_Amount\" size=\"16\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td " + formtext + " width=\"106\">Over What Term? *</td>"
					+ "<td width=\"31\"><input type=\"text\" name=\"Life_Cover_Term_Standard\" size=\"2\" maxlength=\"2\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td " + formtext + " width=\"39\" align=\"right\">Years</td>"
					+ "</table>"; merge = true; break;
			case 5: input = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>"
					+ "<td " + formtext + " width=\"347\">Do you require the amount of cover to remain level or decrease over the term of the policy? *</td>"
					+ "<td><select name=\"lifecover_Liability_repayment_mortgage\" " + inputstyle + ">"
					+ "<option value=\"Select\">Select</option>"
					+ "<option value=\"No\">Level</option>"
					+ "<option value=\"Yes\">Decrease</option>"
					+ "</select></td></tr></table>"; merge = true; break;
			case 6: input = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>"
					+ "<td " + formtext + " width=\"347\">Do you require Guaranteed or Reviewable premiums? *</td>"
					+ "<td><select name=\"Type_of_Lifecover_Premiums\" " + inputstyle + ">"
					+ "<option value=\"Select\">Select</option>"
					+ "<option value=\"Guaranteed\">Guaranteed</option>"
					+ "<option value=\"Reviewable\">Reviewable</option>"
					+ "</select></td></tr></table>"; merge = true; break;
			//
			// Contact Details - Construction Code
			//
			case 7: input = "<b><font color=\"#00578A\">First Person Covered:</font></b>"; field = "title"; merge = true; break;
			case 8: input = "<input type=\"text\" name=\"FullName_First\" size=\"19\" " + inputstyle + ">"; field = "Name *"; break;
			case 9: input = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
					+ "<td width=\"46\"><input type=\"text\" name=\"DOB_Day_First\" size=\"2\" maxlength=\"2\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td width=\"46\"><input type=\"text\" name=\"DOB_Month_First\" size=\"2\" maxlength=\"2\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td width=\"42\"><input type=\"text\" name=\"DOB_Year_First\" size=\"2\" maxlength=\"4\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td width=\"65\" " + formtextsm + " align=\"right\">dd/mm/yyyy</td>"
					+ "</tr></table>"; field = "Date Of Birth *"; break;
			case 10: input = "<select name=\"Sex_First\" size=\"1\" " + inputstyle + ">"
					+ "<option value=\"Male\">Male</option>"
					+ "<option value=\"Female\">Female</option>"
				 	+ "</select>"; field = "Gender *"; break;

			case 11: input = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
					+ "<td " + formtext + " width=\"392\">Has this person smoked any tobacco products in the last 12 months?</td>"
					+ "<td><select name=\"Smoker_First\" size=\"1\" " + inputstyle + ">"
					+ "<option value=\"Select\">Select</option>"
					+ "<option value=\"No\">No</option>"
					+ "<option value=\"Yes\">Yes</option>"
					+ "<select></td></tr></table>"; merge = true; break;


			case 12: input = "<input type=\"text\" name=\"Email\" size=\"19\" " + inputstyle + ">"; field = "Email *"; break;
			case 13: input = "<input type=\"text\" name=\"WorkPhone\" size=\"19\" " + inputstyle + ">"; field = "Daytime Phone *"; break;
			case 14: input = "<input type=\"text\" name=\"HomePhone\" size=\"19\" " + inputstyle + ">"; field = "Evening Phone *"; break;
			//case 14: input = "<select name=\"Preferred_Call_Time\" size=\"1\" " + inputstyle + ">"
			//		+ "<option value=\"Please Select\">Please Select</option>"
			//		+ "<option value=\"08.00 - 12.00\">08.00 - 12.00</option>"
			//		+ "<option value=\"12.00 - 17.00\">12.00 - 17.00</option>"
			//		+ "<option value=\"17.00 - 20.00\">17.00 - 20.00</option>"
			//		+ "<option value=\"Anytime\" selected>Anytime</option>"
			//		+ "</select>"; field = "Preferred Call Time *"; break;
			case 15: input = "<input type=\"text\" name=\"Address1\" size=\"19\" " + inputstyle + ">"; field = "Address *"; break;
			case 16: input = "<input type=\"text\" name=\"Address2\" size=\"19\" " + inputstyle + ">"; field = ""; break;
			case 17: input = "<input type=\"text\" name=\"Town\" size=\"19\" " + inputstyle + ">"; field = "Town *"; break;
			case 18: input = "<input type=\"text\" name=\"County\" size=\"19\" " + inputstyle + ">"; field = "County *"; break;
			case 19: input = "<input type=\"text\" name=\"Postcode\" size=\"10\" " + inputstyle + ">"; field = "Postcode *"; break;


			case 20: input = "<b><font color=\"#00578A\">Second Person Covered: (if required)</font></b>"; field = "title"; merge = true; break;
			case 21: input = "<input type=\"text\" name=\"FullName_Second\" size=\"19\">"; field = "Name"; break;
			case 22: input = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
					+ "<td width=\"46\"><input type=\"text\" name=\"DOB_Day_Second\" size=\"2\" maxlength=\"2\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td width=\"46\"><input type=\"text\" name=\"DOB_Month_Second\" size=\"2\" maxlength=\"2\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td width=\"42\"><input type=\"text\" name=\"DOB_Year_Second\" size=\"2\" maxlength=\"4\" " + inputstyle + " onchange=\"stringFilter(this)\"></td>"
					+ "<td width=\"65\" " + formtextsm + " align=\"right\">dd/mm/yyyy</td>"
					+ "</tr></table>"; field = "Date Of Birth"; break;
			case 23: input = "<select name=\"Sex_Second\" size=\"1\" " + inputstyle + ">"
					+ "<option value=\"Male\">Male</option>"
					+ "<option value=\"Female\">Female</option>"
				 	+ "</select>"; field = "Gender"; break;
			case 24: input = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
					+ "<td " + formtext + " width=\"392\">Has this person smoked any tobacco products in the last 12 months?</td>"
					+ "<td><select name=\"Smoker_Second\" size=\"1\" " + inputstyle + ">"
					+ "<option value=\"Select\">Select</option>"
					+ "<option value=\"No\">No</option>"
					+ "<option value=\"Yes\">Yes</option>"
					+ "<select></td></tr></table>"; merge = true; break;
			case 25: input = "<b><font color=\"#00578A\">Submit Your Enquiry</font></b>"; field = "title"; merge = true; break;
			//case 26: input = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
			//		+ "<td " + formtext + " width=\"392\">Do you wish us to quote you the cheapest monthly premium? *</td>"
			//		+ "<td><select name=\"further_advice\" size=\"1\" " + inputstyle + ">"
			//		+ "<option value=\"No\">No</option>"
			//		+ "<option value=\"Yes\" selected>Yes</option>"
			//		+ "<select></td></tr></table>"; merge = true; break;
			case 26: input = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
					+ ""
					+ "<td " + formtext + " width=\"392\">&nbsp;I have read and understood the key facts document below and wish to submit my enquiry.</td>"
					+ "</td><td><input type=\"checkbox\" name=\"DDI_Agree\" value=\"Yes\"></td></tr></table>"; merge = true; break;
			case 27: input = "<input type=\"submit\" name=\"submit\" value=\"Submit Enquiry\" style=\"width:150px; height: 30px;\">"; merge = true; height = 35; break;
			case 28: input = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr>"
					+ "<td " + formtext + " width=\"452\"><IFRAME width=\"100%\" src=\"http://www.lifebrokers.co.uk/db_submit_files/keyfacts.html\"></IFRAME></td>"
					+ "</tr></table>"; merge = true; height = 55; break;




		} // end switch

		w.output += createrow(input, field, merge, height, i);		

	} // end for
	
	w.output += "</table>";
	w.output += "</form>";
	document.write(w.output);
}

function checkFields(form)
{
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var check=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;

	DOB_Day_First = form.DOB_Day_First.value
	DOB_Month_First = form.DOB_Month_First.value
	DOB_Year_First = form.DOB_Year_First.value
	DOB_Day_Second = form.DOB_Day_Second.value
	DOB_Month_Second = form.DOB_Month_Second.value
	DOB_Year_Second = form.DOB_Year_Second.value

	if ( form.Life_Cover_Amount.value == "" )
	{
		alert("Please enter a valid amount of Life Cover");	
		form.Life_Cover_Amount.focus();
		return false;
	}
	else if ( form.Life_Cover_Term_Standard.value == "" )
	{
		alert("Please select the term length you require");
		form.Life_Cover_Term_Standard.focus();
		return false;
	}
	else if ( form.lifecover_Liability_repayment_mortgage.value == "Select" )
	{
		alert("Please select whether you wish to protect your mortgage");
		form.lifecover_Liability_repayment_mortgage.focus();
		return false;
	}
	else if ( form.Type_of_Lifecover_Premiums.value == "Select" )
	{
		alert("Please state whether you require guaranteed or reviewable premiums");
		form.Type_of_Lifecover_Premiums.focus();
		return false;
	}
	else if ( form.FullName_First.value == "" )
	{
		alert("Please enter your full name");
		form.FullName_First.focus();
		return false;
	}
	else if ( form.DOB_Day_First.value == "" || DOB_Day_First.length <= 1 || form.DOB_Day_First.value > 31 )
	{
		alert("Please enter a correctly formatted day (dd)");
		form.DOB_Day_First.focus();
		return false;
	}
	else if ( form.DOB_Month_First.value == "" || DOB_Month_First.length <= 1 || form.DOB_Month_First.value > 12 )
	{
		alert("Please enter a correctly formatted month (mm)");
		form.DOB_Month_First.focus();
		return false;
	}
	else if ( form.DOB_Year_First.value == "" || DOB_Year_First.length <= 3 || form.DOB_Year_First.value > 2000 || form.DOB_Year_First.value < 1900 )
	{
		alert("Please enter a correctly formatted year (yyyy)");
		form.DOB_Year_First.focus();
		return false;
	}
	else if (((form.Email.value.search(exclude) != -1)
		||(form.Email.value.search(check)) == -1)
		||(form.Email.value.search(checkend) == -1))
	{
		alert("Please enter a valid email address");
		form.Email.focus(); 
		return false;
	} 
	else if ( form.WorkPhone.value == "" )
	{
		alert("Please enter a valid workplace telephone number.");
		form.WorkPhone.focus();
		return false;	
	}
	else if ( form.HomePhone.value == "" )
	{
		alert("Please enter a valid home telephone number.");
		form.HomePhone.focus();
		return false;	
	}
	
	else if ( form.Address1.value == "" )
	{
		alert("Please click your street address");
		form.Address1.focus();
		return false;
	}
	else if ( form.Town.value == "" )
	{
		alert("Please click your town");
		form.Town.focus();
		return false;
	}
	else if ( form.County.value == "" )
	{
		alert("Please click your county");
		form.County.focus();
		return false;
	}
	else if ( form.Smoker_First.value == "Select" )
	{
		alert("Please select whether you have smoked or not");
		form.Smoker_First.focus();
		return false;
	}
	else if ( form.DDI_Agree.checked != true )
	{
		alert("Please tick that you have read and understood the Key Facts Document");
		return false;
	}
	else return true;
}

function stringFilter (input)
{
	if (input.value != "")
	{
		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";
		}
		input.value = returnString;
	}
}
