String.prototype.strip = function() {
  return this.replace(/^\s+/, '').replace(/\s+$/, '');
};

// 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 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("premium", "keydown", filterInts);
	YAHOO.util.Event.addListener("fundSize", "keydown", filterInts);

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

		if (!$("currentPension").value)
		{
			alert("Do you currently have a Pension?");
			$("currentPension").focus();
			return;
		}
        
		if (!$("premium").value && $("currentPension").value != 10)
		{
			alert("Please enter a monthly premium payment.");
			$("premium").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 (!$("client1Forename").value)
		{
			alert("Please enter your forename(s)");
			$("client1Forename").focus();
			return;
		}

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

		var day = $("dobDay").value, month = $("dobMonth").value, year = $("dobYear").value;

		if (!day || !month || !year)
		{
			alert("Please select your date of birth");
			$("dobDay").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");
			$("dobDay").focus();
			return;
		}

		if (!validateEMail($("client1EmailAddress").value))
		{
			alert("Please enter a valid e-mail address to continue with your enquiry");
			$("client1EmailAddress").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 (!$("Adr1").value)
		{
			alert("Please enter the first line of your postal address");
			$("Adr1").focus();
			return;
		}

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

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

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

		var telMain = $("client1HomeTelephone").value, telAlt = $("client1WorkTelephone").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");
			$("client1HomeTelephone").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");
			$("client1HomeTelephone").focus();
			return;
		}

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

		if (!(/^[0-9 \(\)\+]+$/).test(telMain))
		{
			alert("Main telephone number contains invalid characters - please use only digits, spaces, brackets and plus sign");
			$("client1HomeTelephone").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");
			$("client1HomeTelephone").focus();
			return;
		}

		if (!telAlt || telAlt.length < 10)
		{
			alert("Alternative telephone number must be 10 digits or longer");
			$("client1WorkTelephone").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");
			$("client1WorkTelephone").focus();
			return;
		}

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

		if (!(/^[0-9 \(\)\+]+$/).test(telAlt))
		{
			alert("Alternative telephone number contains invalid characters - please use only digits, spaces, brackets and plus sign");
			$("client1WorkTelephone").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");
			$("client1WorkTelephone").focus();
			return;
		}

		var form = $("hello-form");
		form.action = "http://www.ileads.co.uk/service/lead/hello/post-pen";
		form.submit();
	});

	function expressConsent()
	{
		var bizTypeID = $('bizTypeID').value.strip();
		var leadTypeID = $('leadType').value.strip();
		var postcode1 = $('client1Postcode1').value.strip();
		var channelID = $('channelID').value.strip();
		var allocID = 'allocID'; //Do we need this?
		var feedbackID = 'hello-express';

		if (bizTypeID.length > 0 && leadTypeID.length > 0 && postcode1.length > 0)
		{
			var request = 'http://www.ileads.co.uk/service/lead/express-consent-pen?channelID=' + escape(channelID) + '&postcode1=' + escape(postcode1) + '&bizTypeID=' + escape(bizTypeID) + '&leadTypeID=' + escape(leadTypeID) + '&feedbackID=' + escape(feedbackID) + '&allocID=' + escape(allocID) + '&alloc=' + escape();

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

	YAHOO.util.Event.addListener("client1Postcode1", "blur", expressConsent);

	if (leadType > 0)
	{
		try{$("premium").focus();}catch(e){}
	}
	else
	{
		try{$("leadType").focus();}catch(e){}
	}
};

HelloLeads.leadbayError = "";