function change(formRef, id, newClass) {
	var el = formRef && formRef.elements[id];
	el.className = newClass;
}

function checkField(formRef, id) {
	var el = formRef && formRef.elements[id];
	return !el || Boolean(el.value);
}

function validateForm(formRef) {
		var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";

		if (!checkField(formRef,"postcode"))
			{
			alertMsg += "\nPlease enter your postcode details";
			change(formRef,'postcode','error') + formRef.postcode.focus();
			}
		else { change(formRef,'postcode','normal'); }
		
		if (document.form1.ImageSrc.value == "")
		{
		return false;
		}


		if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n")
			{
			alert(alertMsg);
			return false;
			}
		else {
			return true;
			}
}

function submitForm()
{
if (document.form1.onsubmit())
{document.form1.submit();}
}

function SetField(Source)
{
document.form1.ImageSrc.value=Source;
if (document.form1.onsubmit())
{document.form1.submit();}
}

function newwin(url, winname, parameters)
{
window.open(url, winname, parameters);
}

function GoBrokerbank()
{
		var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";


		if (document.form1.postcode.value !="")
			{
			SetField('secured')
			}

		else
			{
			alertMsg += "\nYou must enter your postcode details";
			}


		if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n")
			{
			alert(alertMsg);
			}
}

function ShowMessage(id)
{
	if(id=="Halifax")
	{
	alert('This Button will forward the customer to the Halifax Consumer MicroSite. \n \n Providing the customer has linked to this site using the link provided to you by Broker Support \n \n your specific id will be recorded against the application so that the commission can be paid through to yourselves. ')
	}
	else
	{
	alert('This functionality is currently under construction.')
	}
}

function IscheckedTenant()
{

	for (i=0;i<document.form1.Tenant.length;i++)
	{
		if (document.form1.Tenant[0].checked==true)
		{
		return true;
		}
	}
}

function IscheckedHomeOwner()
{

	for (i=0;i<document.form1.HomeOwner.length;i++)
	{
		if (document.form1.HomeOwner[0].checked==true)
		{
		return true;
		}
	}
}

function IscheckedOver25()
{

	for (i=0;i<document.form1.Over25.length;i++)
	{
		if (document.form1.Over25[0].checked==true)
		{
		return true;
		}
	}
}

function IscheckedUnsecured()
{

	for (i=0;i<document.form1.Unsecured.length;i++)
	{
		if (document.form1.Unsecured[0].checked==true)
		{
		return true;
		}
	}
}

function IscheckedSecured()
{

	for (i=0;i<document.form1.Secured.length;i++)
	{
		if (document.form1.Secured[0].checked==true)
		{
		return true;
		}
	}
}


function DecideRoute()
{

if ((document.form1.hidTenant.value =="") || (document.form1.hidHomeOwner.value =="") || (document.form1.hidOver25.value =="") || (document.form1.hidUnsecured.value =="") || (document.form1.hidSecured.value ==""))

	{
	alert ('Please complete all questions');
	}
	else

	{
		if (IscheckedTenant())
		{
		 if (checkTenHO())
			{
			SetField('Halifax');
			}
		}

		else

		if (IscheckedUnsecured())
		{
		SetField('Halifax');}

		else

		if (IscheckedHomeOwner())
		{
		GoBrokerbank();}

		else

		if (IscheckedOver25())
		{
		GoBrokerbank()}

		else

		if (IscheckedSecured())
		{
		GoBrokerbank()}

	}
}

function checkTenHO()
{
	if ((document.form1.hidTenant.value == "Yes") && (document.form1.hidHomeOwner.value == "Yes"))
	{
	alert ('Plese check your answers to the first 2 questions. \n \n \n You cannot be both a Tenant and a HomeOwner.');
	return false;
	}
	else
	{
	return true;
	}

}


function Sethid(btn, txt)
{
if (btn == "Tenant"){document.form1.hidTenant.value = txt;}
if (btn == "HomeOwner"){document.form1.hidHomeOwner.value = txt;}
if (btn == "Over25")
	{
		if ((txt == "Yes") && (document.form1.hidTenant.value == "Yes"))
			{
			alert('The maximum Loan Value available to a non-Homeowner through this facility is £25,000');
			}
	document.form1.hidOver25.value = txt;
	
	}
if (btn == "Secured"){document.form1.hidSecured.value = txt;}
if (btn == "Unsecured"){document.form1.hidUnsecured.value = txt;}
}
