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);
	}
};

var HelloLeads = function(typeID, postcodeID, expressID)
{
	this.typeID = typeID;
	this.postcodeID = postcodeID;
	this.expressID = expressID;

	YAHOO.util.Event.addListener(typeID, "change", this.express, this, true);
	YAHOO.util.Event.addListener(postcodeID, "change", this.express, this, true);
};

HelloLeads.prototype =
{
	express: function()
	{
		var el = YAHOO.util.Dom.get("theForm")[this.typeID], typeRef = 0;

		for (var i = 0, length = el.length; i < length; i++)
		{
			if (el[i].checked)
			{
				typeRef = el[i].value;
				break;
			}
		}

		if (typeRef == 0 || !YAHOO.util.Dom.get(this.postcodeID).value)
		{
			return;
		}

/*
		var callback =
		{
			success: function(o)
			{
				eval(o.responseText);
			},

			failure: function(o)
			{
				console.log(o);
			}
		};
*/

		var request = /*YAHOO.util.Connect.asyncRequest("GET", */"http://www.helloleads.co.uk/script/express.php?typeRef=" + escape(typeRef) + "&postcode=" + escape(YAHOO.util.Dom.get(this.postcodeID).value)/*, callback)*/;

		var xs = new XSite(request);
	}
}

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

	if (!reg_exp.test(value) && flag != false){
		alert('Please enter a valid email address');
		element.focus();
		return false;
	}
	else{
		return true;
	}
}

function validateRadio(element) {
	var count = -1;
	for (var i=element.length-1; i > -1; i--){
		if (element[i].checked) {
			count = i; i = -1;
		}
	}
	if (count > -1){
		//return element[count].value;
		return true;
	}
	else{
		return false;
	}
}

function getRadioValue(element)
{
	for (var i = 0, j = element.length; i < j; ++i)
	{
		if (element[i].checked)
		{
			return element[i].value;
		}
	}

	return null;
}

function step1to2()
{
	var element = YAHOO.util.Dom.get("theForm").typeRef, typeRef = 0;

	for (var i = 0, length = element.length; i < length; i++)
	{
		if (element[i].checked)
		{
			typeRef = element[i].value;
		}
	}

	if (typeRef == 0)
	{
		alert('Please select the appropriate product before you move to the next page');
		return false;
	}

	YAHOO.util.Dom.setStyle(['step1'], 'display', 'none');
	YAHOO.util.Dom.setStyle(['step2'], 'display', 'block');
}

function step2to1()
{
	YAHOO.util.Dom.setStyle(['step1'], 'display', 'block');
	YAHOO.util.Dom.setStyle(['step2'], 'display', 'none');
}

function step2to3(){
	var theForm = YAHOO.util.Dom.get("theForm");

	var ltv = 0.85;

	if (getRadioValue(theForm.badcredit))
	{
		// sub-prime
		ltv = 0.75;
	}
	else if (getRadioValue(theForm.typeRef) == 8)
	{
		// commercial prime
		ltv = 0.8;
	}

	if (theForm.mortgageSize.value == "")
	{
		alert('Please enter the Loan Amount required.');
		theForm.mortgageSize.focus();
		return false;
	}

	if (theForm.propertyValue.value == "")
	{
		alert('Please enter the Property Value.');
		theForm.propertyValue.focus();
		return false;
	}

	if ((theForm.mortgageSize.value / theForm.propertyValue.value) > ltv)
	{
/*
		alert("We are only able to help if your mortgage value is less than or equal to " + (ltv * 100) + "% of the property value.");
		theForm.mortgageSize.focus();
*/
		location.href = theForm.ltv || "/mortgageltv.htm";
		return false;
	}

	YAHOO.util.Dom.setStyle(['step2'], 'display', 'none');
	YAHOO.util.Dom.setStyle(['step3'], 'display', 'block');
}

function step3to2()
{
	YAHOO.util.Dom.setStyle(['step2'], 'display', 'block');
	YAHOO.util.Dom.setStyle(['step3'], 'display', 'none');
}

function submitForm(){
	var flag = true, theForm = YAHOO.util.Dom.get("theForm");
	if(theForm.forename.value == '' && flag != false){
		alert('Please enter your forename.');
		theForm.forename.focus();
		flag = false;
	}

	if(theForm.surname.value == '' && flag != false){
		alert('Please enter your surname.');
		theForm.surname.focus();
		flag = false;
	}

	if((theForm.telHome.value == '') && (theForm.telHome.value < 10 ) && flag != false){
		alert('Please enter a valid telephone number.');
		theForm.telHome.focus();
		flag = false;
	}

	emailflag = emailCheck(theForm.email.value, theForm.email, flag);
	if(emailflag == false){
		flag = false;
	}

	if(theForm.postcode.value == '' && flag != false){
		alert('Please enter your Post Code.');
		theForm.postcode.focus();
		flag = false;
	}

	if(flag == true){
		theForm.action = 'http://www.helloleads.co.uk/script/submit.php';
		theForm.submit();
	}
}
