// shorthand to get element by ID
if (typeof $ == "undefined")
{
	var $ = YAHOO.util.Dom.get;
}

function XSite(url)
{
	this.url = url;

	if(this.url.indexOf('?') == -1)
	{
		this.noCacheIE = '?noCache=' + (new Date()).getTime();
	}
	else
	{
		this.noCacheIE = '&noCache=' + (new Date()).getTime();
	}

	this.head = document.getElementsByTagName('head').item(0);
	this.id = 'XSITE' + XSite.count++;

	this.create();
	this.run();
}

XSite.count = 1;

XSite.prototype =
{
	create: function ()
	{
		this.element = document.createElement('script');

		this.element.setAttribute('type', 'text/javascript');
		this.element.setAttribute('charset', 'utf-8');
		this.element.setAttribute('src', this.url + this.noCacheIE);
		this.element.setAttribute('id', this.id);
	},

	run: function ()
	{
		this.head.appendChild(this.element);
	},

	destroy: function ()
	{
		this.head.removeChild(this.element);
	}
};

function validateEMail(value)
{
	reg_exp = /^[a-z0-9][^\(\)\<\>\@\,\;\:\\\"\[\]]*\@[a-z0-9][a-z0-9\-\.]*\.[a-z]{2,4}$/i;
	return (reg_exp.test(value));
}

var HelloLeads = function(leadType)
{
	leadType = leadType || 0;

	var stepWidth = 500, currentStep = 0, anim = null;

/*
	function validateStep1()
	{
		if ((leadType || $("hello-lead-type").value > 0) && $("hello-mortgage-amount").value > 24999 && $("hello-mortgage-term").value >= 5 && $("hello-property-value").value > 24999)
		{
			$("hello-continue-1").disabled = false;
		}
		else
		{
			$("hello-continue-1").disabled = true;
		}
	}

	YAHOO.util.Event.addListener("hello-mortgage-amount", "keyup", validateStep1);
	YAHOO.util.Event.addListener("hello-mortgage-term", "keyup", validateStep1);
	YAHOO.util.Event.addListener("hello-property-value", "keyup", validateStep1);

	function validateStep2()
	{
		if ($("hello-forename").value && $("hello-surname").value && $("hello-dob-day").value && $("hello-dob-month").value && $("hello-dob-year").value && validateEMail($("hello-email").value))
		{
			$("hello-continue-2").disabled = false;
		}
		else
		{
			$("hello-continue-2").disabled = true;
		}
	}

	YAHOO.util.Event.addListener("hello-forename", "keyup", validateStep2);
	YAHOO.util.Event.addListener("hello-surname", "keyup", validateStep2);
	YAHOO.util.Event.addListener("hello-dob-day", "change", validateStep2);
	YAHOO.util.Event.addListener("hello-dob-month", "change", validateStep2);
	YAHOO.util.Event.addListener("hello-dob-year", "change", validateStep2);
	YAHOO.util.Event.addListener("hello-email", "keyup", validateStep2);

	function validateStep3()
	{
		var tels = 0;

		if ($("hello-tel-main").value && $("hello-tel-main").value.length >= 10) ++tels;
		if ($("hello-tel-alt").value && $("hello-tel-alt").value.length >= 10) ++tels;

		if ($("hello-address1").value && $("hello-town").value && $("hello-county").value && tels >= 2)
		{
			$("hello-submit").disabled = false;
		}
		else
		{
			$("hello-submit").disabled = true;
		}
	}

	YAHOO.util.Event.addListener("hello-address1", "keyup", validateStep3);
	YAHOO.util.Event.addListener("hello-town", "keyup", validateStep3);
	YAHOO.util.Event.addListener("hello-county", "keyup", validateStep3);
	YAHOO.util.Event.addListener("hello-postcode", "keyup", validateStep3);
	YAHOO.util.Event.addListener("hello-tel-main", "keyup", validateStep3);
	YAHOO.util.Event.addListener("hello-tel-alt", "keyup", validateStep3);

	validateStep1();
	validateStep2();
	validateStep3();
*/

	function gotoStep(step)
	{
		step = (step -= 1) < 0 ? 0 : (step > 2 ? 2 : step);

		var from = currentStep * stepWidth, to = step * stepWidth;

		if ((anim && anim.isAnimated()) || step == currentStep)
		{
			return;
		}

		anim = new YAHOO.util.Motion("hello-container",
		{
			left:
			{
				from: -from,
				to: -to
			}
		}, 0.5, YAHOO.util.Easing.easeBoth);

		anim.onComplete.subscribe(function()
		{
			YAHOO.util.Dom.removeClass("hello-header", "hello-wizard-" + (currentStep + 1));
			YAHOO.util.Dom.addClass("hello-header", "hello-wizard-" + (step + 1));

			$("hello-step-" + (currentStep + 1)).style.visibility = "hidden";

			currentStep = step;
		});

		$("hello-step-" + (step + 1)).style.visibility = "visible";

		anim.animate();
	}

	function filterInts(e)
	{
		var keyCode = window.event ? window.event.keyCode : e.which, allowed = [8, 9, 13, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105];

		for (var i = 0, j = allowed.length; i < j; ++i)
		{
			if (keyCode == allowed[i])
			{
				return;
			}
		}

		YAHOO.util.Event.stopEvent(e);
	}

	YAHOO.util.Event.addListener("hello-mortgage-amount", "keydown", filterInts);
	YAHOO.util.Event.addListener("hello-property-value", "keydown", filterInts);

	YAHOO.util.Event.addListener("hello-continue-1", "click", function()
	{
		if (!leadType && !$("hello-lead-type").value)
		{
			alert("Please select the type of mortgage that you are enquiring about");
			$("hello-lead-type").focus();
			return;
		}

		if (!$("hello-mortgage-term").value)
		{
			alert("Please enter a mortgage payment term in years");
			$("hello-mortgage-term").focus();
			return;
		}

		var mortAmt = parseInt($("hello-mortgage-amount").value, 10) || 0, propVal = parseInt($("hello-property-value").value, 10) || 0, ltv = (mortAmt > 0 && propVal > 0) ? (mortAmt / propVal) : 100, badCredit = $("hello-credit-1").checked || $("hello-credit-2").checked || $("hello-credit-3").checked, selfCert = $("hello-self-cert").value == 1 ? true : false;

		if (mortAmt < 25000)
		{
			alert("We are only able to help with mortgage amounts above " + String.fromCharCode(163) + "25,000");
			$("hello-mortgage-amount").focus();
			return;
		}

		var theType = leadType || $("hello-lead-type").value, minLTV = 0.90;

		if (badCredit)
		{
			minLTV = 0.80;
		}
		else if (selfCert)
		{
			if (theType == 2)
			{
				minLTV = 0.95;
			}
			else
			{
				minLTV = 0.85;
			}
		}
		else if (theType == 4)
		{
			minLTV = 0.85;
		}

		if (ltv > minLTV)
		{
			alert("The mortgage value entered must be less than " + (minLTV * 100) + "% of the property value to proceed with mortgage advice");
			$("hello-mortgage-amount").focus();
			return;
		}

		gotoStep(2);
	});

	YAHOO.util.Event.addListener("hello-back-2", "click", function()
	{
		gotoStep(1);
	});

	YAHOO.util.Event.addListener("hello-continue-2", "click", function()
	{
		if (!$("hello-forename").value)
		{
			alert("Please enter your forename(s)");
			$("hello-forename").focus();
			return;
		}

		if (!$("hello-surname").value)
		{
			alert("Please enter your surname");
			$("hello-surname").focus();
			return;
		}

		var day = $("hello-dob-day").value, month = $("hello-dob-month").value, year = $("hello-dob-year").value;

		if (!day || !month || !year)
		{
			alert("Please select your date of birth");
			$("hello-dob-day").focus();
			return;
		}

		var now = new Date(), ageCheck = new Date(now.getFullYear() - 65, now.getMonth(), now.getDate()), dob = new Date(year, month - 1, day);

		if (dob < ageCheck)
		{
			alert("We are only able to accept enquiries from customers under the age of 65");
			$("hello-dob-day").focus();
			return;
		}

		if (!validateEMail($("hello-email").value))
		{
			alert("Please enter a valid e-mail address to continue with your enquiry");
			$("hello-email").focus();
			return;
		}

		gotoStep(3);
	});

	YAHOO.util.Event.addListener("hello-back-3", "click", function()
	{
		gotoStep(2);
	});

	function validateTel(tel)
	{
		
	}

	YAHOO.util.Event.addListener("hello-submit", "click", function()
	{
		if (!$("hello-address1").value)
		{
			alert("Please enter the first line of your postal address");
			$("hello-address1").focus();
			return;
		}

		if (!$("hello-town").value)
		{
			alert("Please enter your postal town or city");
			$("hello-town").focus();
			return;
		}

		if (!$("hello-county").value)
		{
			alert("Please enter your postal county");
			$("hello-county").focus();
			return;
		}

		if (!$("hello-postcode").value)
		{
			alert("Please enter your postcode");
			$("hello-postcode").focus();
			return;
		}

		var telMain = $("hello-tel-main").value, telAlt = $("hello-tel-alt").value;

		// TELEPHONE REGULAR EXPRESSIONS:
		// 0{7,}|1{7,}|2{7,}|3{7,}|4{7,}|5{7,}|6{7,}|7{7,}|8{7,}|9{7,} - number repeated 7 or more times
		// ^0[123578] - starts with 01, 02, 03, 05, 07 or 08
		// ^[0-9 \(\)\+]+$ - only match digits, brackets, plus and spaces

		if (!telMain|| telMain.length < 10)
		{
			alert("Main telephone number must be 10 digits or longer");
			$("hello-tel-main").focus();
			return;
		}

		if ((/0{7,}|1{7,}|2{7,}|3{7,}|4{7,}|5{7,}|6{7,}|7{7,}|8{7,}|9{7,}/).test(telMain))
		{
			alert("Main telephone number must not contain 7 identical consecutive digits e.g. 00000000");
			$("hello-tel-main").focus();
			return;
		}

		if (!(/^0[123578]/).test(telMain))
		{
			alert("Main telephone number must start with 01, 02, 03, 05, 07 or 08");
			$("hello-tel-main").focus();
			return;
		}

		if (!(/^[0-9 \(\)\+]+$/).test(telMain))
		{
			alert("Main telephone number contains invalid characters - please use only digits, spaces, brackets and plus sign");
			$("hello-tel-main").focus();
			return;
		}

		for (var nc = 0, i = 0, j = telMain.length; i < j; ++i)
		{
			var c = telMain.charAt(i);
			if (c >= "0" && c <= "9")
			{
				++nc;
			}
		}

		if (nc < 10 || nc > 11)
		{
			alert("Main telephone number must contain 10 or 11 numbers");
			$("hello-tel-main").focus();
			return;
		}

		if (!telAlt || telAlt.length < 10)
		{
			alert("Alternative telephone number must be 10 digits or longer");
			$("hello-tel-alt").focus();
			return;
		}

		if ((/0{7,}|1{7,}|2{7,}|3{7,}|4{7,}|5{7,}|6{7,}|7{7,}|8{7,}|9{7,}/).test(telAlt))
		{
			alert("Alternative telephone number must not contain 7 identical consecutive digits e.g. 11111111");
			$("hello-tel-alt").focus();
			return;
		}

		if (!(/^0[123578]/).test(telAlt))
		{
			alert("Alternative telephone number must start with 01, 02, 03, 05, 07 or 08");
			$("hello-tel-alt").focus();
			return;
		}

		if (!(/^[0-9 \(\)\+]+$/).test(telAlt))
		{
			alert("Alternative telephone number contains invalid characters - please use only digits, spaces, brackets and plus sign");
			$("hello-tel-alt").focus();
			return;
		}

		for (var nc = 0, i = 0, j = telAlt.length; i < j; ++i)
		{
			var c = telAlt.charAt(i);
			if (c >= "0" && c <= "9")
			{
				++nc;
			}
		}

		if (nc < 10 || nc > 11)
		{
			alert("Alternative telephone number must contain 10 or 11 numbers");
			$("hello-tel-alt").focus();
			return;
		}

		var form = $("hello-form");
		form.action = "http://www.helloleads.co.uk/script/submit2.php?err=" + encodeURIComponent(HelloLeads.leadbayError);
		form.submit();
	});

	function expressConsent()
	{
		if ($("hello-postcode").value.length >= 3)
		{
			var typeRef = (leadType > 0) ? leadType : $("hello-lead-type").value;

			var request = "http://www.helloleads.co.uk/script/express2.php?expressID=" + encodeURIComponent("hello-express") + "&typeRef=" + encodeURIComponent(typeRef) + "&postcode=" + encodeURIComponent($("hello-postcode").value) + "&mortgageSize=" + encodeURIComponent($("hello-mortgage-amount").value) + "&propertyValue=" + encodeURIComponent($("hello-property-value").value) + "&selfCert=" + encodeURIComponent($("hello-self-cert").value) + "&badCredit=" + (($("hello-credit-1").checked || $("hello-credit-2").checked || $("hello-credit-3").checked) ? 1 : 0);

			$("hello-ajax").style.visibility = "visible";
			var xs = new XSite(request);
		}
	}

	YAHOO.util.Event.addListener("hello-postcode", "blur", expressConsent);
//	YAHOO.util.Event.addListener("hello-lead-type", "blur", expressConsent);

	if (leadType > 0)
	{
		try{$("hello-mortgage-amount").focus();}catch(e){}
	}
	else
	{
		try{$("hello-lead-type").focus();}catch(e){}
	}
};

HelloLeads.leadbayError = "";
