window.onload = function()
{
	// MM_preloadImages('quote_old/images/proceed_hover.gif', '/images/get_quotes_on.jpg', '/images/navigation/life_insurance_on.jpg', '/images/navigation/navigation_on.jpg', '/images/navigation/financial_services_on.jpg');
	if (document.getElementById) 
	{	
		// Apply page javascript
		var aApplyForm = document.getElementById("lifequoteform");
		if (aApplyForm != null) 
		{
			var aSingle = document.getElementById("PolicyTypeSingle");
			var aJoint = document.getElementById("PolicyTypeJoint");
			if ((aSingle != null) && (aJoint != null))
			{
				aSingle.onclick = JSFnShowSecondApplicant;
				aJoint.onclick = JSFnShowSecondApplicant;
			}
			JSFnShowSecondApplicant();
			
			JSFnActivateToolTips();
		
			aApplyForm.onsubmit = JSFnCheckApplyForm;
			
			var aProceedButton = document.getElementById("proceedbutton");
			if (aProceedButton != null) 
			{
				aProceedButton.onmouseover = JsFnProceedOver;
				aProceedButton.onmouseout = JsFnProceedOut;
			}
			
			var aEmailAddress = document.getElementById("EmailAddress");
			if (aEmailAddress != null) aEmailAddress.onblur = JSFnValidateEmail;
			
			var aPostcode = document.getElementById("Postcode");
			if (aPostcode != null) aPostcode.onblur = JSFnValidatePostcode;
		}
		
		// Confirm page javascript
		var aConfirmForm = document.getElementById("lifequoteconfirmform");
		if (aConfirmForm != null) 
		{
			aConfirmForm.onsubmit = JSFnCheckConfirmForm;
			
			var aProceedButton = document.getElementById("proceedbutton");
			if (aProceedButton != null) 
			{
				aProceedButton.onmouseover = JsFnContinueOver;
				aProceedButton.onmouseout = JsFnContinueOut;
			}
			
			var aSecondApplicantInfo = document.getElementById("applicant2");			
			var aApp2Title = document.getElementById("App2Title");
			if ((aApp2Title != null) && (aSecondApplicantInfo != null))
			{
				if (aApp2Title.value == '') aSecondApplicantInfo.style.display = 'none';
			}
			
			var aEmailAddress = document.getElementById("EmailAddress");
			if (aEmailAddress != null) aEmailAddress.onblur = JSFnValidateEmail;
			
			var aPostcode = document.getElementById("Postcode");
			if (aPostcode != null) aPostcode.onblur = JSFnValidatePostcode;
		}
		
		// Results page javascript
		var aResultsTable = document.getElementById("weblineresults");
		if (aResultsTable != null) 
		{
			var aImgs = aResultsTable.getElementsByTagName("img");
			for (aIndex = 0; aIndex < aImgs.length; aIndex++)
			{
				if (aImgs[aIndex].src.indexOf('apply.gif') != -1)
				{
					aImgs[aIndex].onmouseover = JsFnApplyOver;
					aImgs[aIndex].onmouseout = JsFnApplyOut;
				}
			}
			
			var aLinks = aResultsTable.getElementsByTagName("a");
			for (aIndex = 0; aIndex < aLinks.length; aIndex++)
			{
				if (aLinks[aIndex].className == 'popup') aLinks[aIndex].target = '_blank';
			}
		}
	}
}

function JsFnApplyOver()
{
	this.src = FFileDirectory + "images/apply_hover.gif";
}

function JsFnApplyOut()
{
	this.src = FFileDirectory + "images/apply.gif";
}

function JsFnProceedOver()
{
	this.src = FFileDirectory + "images/proceed_hover.gif";
}

function JsFnProceedOut()
{
	this.src = FFileDirectory + "images/proceed.gif";
}

function JsFnContinueOver()
{
	this.src = FFileDirectory + "images/continue_hover.gif";
}

function JsFnContinueOut()
{
	this.src = FFileDirectory + "images/continue.gif";
}

function JSFnShowSecondApplicant()
{
	var aSecondApplicantInfo = document.getElementById("applicant2");			
	var aSingle = document.getElementById("PolicyTypeSingle");
	var aJoint = document.getElementById("PolicyTypeJoint");
	if ((aSingle != null) && (aJoint != null))
	{
		if (aSingle.checked != true) aSecondApplicantInfo.style.display = 'block';
		else aSecondApplicantInfo.style.display = 'none';
	}
}

function JSFnActivateToolTips()
{
	var aSpans = document.getElementsByTagName("span");
	for (aIndex = 0; aIndex < aSpans.length; aIndex++)
	{
		if (aSpans[aIndex].className == 'tooltip')
		{
			aSpans[aIndex].style.display = 'none';
			var aNewLink = document.createElement('a');
			var aText = document.createTextNode('[?]');
			aNewLink.href = 'javascript: void(0)';
			aNewLink.onmouseover = JSFnShowHideToolTip;
			aNewLink.onmouseout = JSFnShowHideToolTip;
			aNewLink.appendChild(aText);
			aSpans[aIndex].parentNode.insertBefore(aNewLink, aSpans[aIndex]);
			aSpans[aIndex].className = 'tooltipactive';
		}
	}
}

function JSFnShowHideToolTip()
{
	if (this.parentNode.childNodes[2].style.display == 'block') aDisplayMode = 'none';
	else aDisplayMode = 'block';		
	this.parentNode.childNodes[2].style.display = aDisplayMode;
	return false;
}

var aApplyFormApplicant1Fields = new Array ("App1Sex","Please indicate the sex of the first applicant",
								  			"App1DOBDay","Please select the date of birth of the first applicant",
								  			"App1DOBMonth","Please select the date of birth of the first applicant",
								  			"App1DOBYear","Please select the date of birth of the first applicant",
											"App1Smoker","Please indicate if the first applicant is a smoker",
											"App1Title","Please enter the title of the first applicant",
											"App1FirstName","Please enter the first name of the first applicant",
											"App1Surname","Please enter the surname of the first applicant");

var aApplyFormApplicant2Fields = new Array ("App2Sex","Please indicate the sex of the second applicant",
								  			"App2DOBDay","Please select the date of birth of the second applicant",
								  			"App2DOBMonth","Please select the date of birth of the second applicant",
								  			"App2DOBYear","Please select the date of birth of the second applicant",
											"App2Smoker","Please indicate if the second applicant is a smoker",
											"App2Title","Please enter the title of the second applicant",
											"App2FirstName","Please enter the first name of the second applicant",
											"App2Surname","Please enter the surname of the second applicant");

var aApplyFormCoverFields = new Array ("TypeOfCoverRequired","Please indicate the type of insurance you require",
								  	   "BenefitType","Please indicate the benefit type you require",
 								  	   "CoverAmount","Please enter the amount of cover you require",
								  	   "Term","Please select the term you require the cover for");

var aApplyFormContactFields = new Array ("EmailAddress","Please enter your email address",
								  	   	 "Telephone","Please enter your telephone number",
 								  	   	 "Address","Please enter the first line of your address",
								  	   	 "Postcode","Please enter your postcode");

function JSFnCheckApplyForm()
{
	aPassed = true;
	
	// Is this a joint application?
	aJointApplication = true;
	var aPolicySingle = document.getElementById("PolicyTypeSingle");
	if ((aPolicySingle != null) && (aPolicySingle.checked)) aJointApplication = false; 
	
	// Check applicant 1
	aPassed = JSFnValidateForm(aApplyFormApplicant1Fields);
	if (aPassed)
	{
		aDOB = document.getElementById('App1DOBDay').value + '/';
		aDOB += document.getElementById('App1DOBMonth').value + '/';
		aDOB += document.getElementById('App1DOBYear').value;

		if (!isDate(aDOB))
		{
			alert('Please enter a valid date of birth for the first applicant');
			aPassed = false;
		}
	}
	
	// Check applicant 2
	if (aPassed && aJointApplication) 
	{	
		aPassed = JSFnValidateForm(aApplyFormApplicant2Fields);
		if (aPassed)
		{
			aDOB = document.getElementById('App2DOBDay').value + '/';
			aDOB += document.getElementById('App2DOBMonth').value + '/';
			aDOB += document.getElementById('App2DOBYear').value;
	
			if (!isDate(aDOB))
			{
				alert('Please enter a valid date of birth for the second applicant');
				aPassed = false;
			}
		}
	}
	
	// Check cover selections
	if (aPassed) 
	{
		aPassed = JSFnValidateForm(aApplyFormCoverFields);
		if (aPassed)
		{
			var aCoverAmount = document.getElementById("CoverAmount");
			var aTerm = document.getElementById("Term");

			if ((isNaN(aCoverAmount.value)) || (isNaN(aTerm.value))) 
			{
				alert('The cover amount and term fields must only contain numbers');
				aPassed = false;	
			}
			else
			{
				if (aCoverAmount.value > 10000000)
				{
					alert('The cover amount can be a maximum of £10,000,000');
					aPassed = false;	
				}
				else if ((aTerm.value < 1) || (aTerm.value > 40))
				{
					alert('The term must be at least 1 year and at most 40 years');
					aPassed = false;	
				}
			}
		}
	}
	
	// Check contact details
	if (aPassed) aPassed = JSFnValidateForm(aApplyFormContactFields);
	
	return aPassed;
}

function JSFnCheckConfirmForm()
{
	aPassed = true;
	
	// Check contact details
	if (aPassed) aPassed = JSFnValidateForm(aApplyFormContactFields);
	
	return aPassed;
}


function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}

function isDate(sDate) 
{
	// Expects dd/mm/yyyy
	var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/
	if (re.test(sDate)) 
	{
		var dArr = sDate.split("/");
		var d = new Date(dArr[2], dArr[1] - 1, dArr[0]);
		return d.getMonth() + 1 == dArr[1] && d.getDate() == dArr[0] && d.getFullYear() == dArr[2];
	}
	else 
	{
		return false;
	}
}

function JSFnValidateEmail()
{
	var aStatus = 'none';
	
	if (!isValidEmail(this.value)) aStatus = 'inline';
	
	var aSpans = this.previousSibling.previousSibling.getElementsByTagName("span");
	for (aIndex = 0; aIndex < aSpans.length; aIndex++) aSpans[aIndex].style.display = aStatus;
}

function JSFnValidatePostcode()
{
	var aStatus = 'none';
	
	if (!isValidPostcode(this.value)) aStatus = 'inline';
	
	var aSpans = this.previousSibling.previousSibling.getElementsByTagName("span");
	for (aIndex = 0; aIndex < aSpans.length; aIndex++) aSpans[aIndex].style.display = aStatus;
}

function isValidPostcode(p) 
{
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}

function isValidEmail(email) 
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) return false;
   else return true;
}