var postcode_old = '';
var type_old = '';

// Ready

$(function() 
{
	// Events
	
	$("input[name='postcode']").live("keypress",function(e)
	{
		if(e.keyCode == 13)
		{
			process(true);
		}
	});
	$("input[name='type']").live("click",function()
	{		
		$(this).attr('selected','selected');
		process(false);
	});
	$("#anchor").live("click",function()
	{	
		process(true);
		return false;
	});
	$("div.requestbutton table.ajaxbutton a.submit").live("click",function()
	{
		$("#requestlead").submit();
		return false;
	});

	// Process
	
	var postcode = $("input[name='postcode']").attr('value');
	var type = $("input[name='type']:checked").attr('value');
	
	if(postcode != undefined && postcode.length >= 1)
	{
		if(type > 0)
		{
			process();
		}
	}
	
	// Document size
	
	if($.browser.version.substr(0,1) == 6)
	{
		$("div.container").css("height",$(document).height());
	}
	else
	{
		$("div.container").css("min-height",$(document).height());
	}
	
	// View more
	
	$(".offerdetails_link").live("click", function() {
		$(".offerdetails").toggle();
		return false;
	});
	
	// Form process
	
	$("#requestlead").live("submit",function() {
		if(typeof(_gaq) != "undefined") {
			$(this).formSubmit({
		        before: function_before,  // pre-submit callback 
		        error: function_error,  // error callback 
		        success: function_succes, // post-submit callback
		        analytics: _gaq
			});
		} else {
			$(this).formSubmit({
		        before: function_before,  // pre-submit callback 
		        error: function_error,  // error callback 
		        success: function_succes // post-submit callback
			});
		}
		
		return false;
	});
});

function function_before(form, options)
{
	$("table.ajaxbutton span.img").css("display","inline");
}

function function_error(data)
{
	$("table.ajaxbutton span.img").css("display","none");
	$("span.error").removeClass('error');
	
	if(data.result && data.result != 'validation_error') {
		alert(data.result);
	} else if(data.missing_fields) {
		$.each(data.missing_fields, function(i, item) {
			$("#" + item).addClass('error');
		});
		
		alert($("input[type='hidden'][name='errormessage']").attr("value"));
	} else if(data.debug) {
		alert(data.debug);
	} else if(data.error) {
		alert(data.error);
	} else {
		window.location = $("input[type='hidden'][name='redirect']").attr("value");
	}
}

function function_succes(data)
{
	window.location = $("input[type='hidden'][name='redirect']").attr("value");
}

function process(startform)
{
	var postcode = $("input[name='postcode']").attr('value');
	var type = $("input[name='type']:checked").attr('value');
		
	if((postcode_old != postcode || type_old != type) && postcode.length >= 1)
	{		
		var url = $("input[name='url'][type='hidden']").attr('value');
		var form = $("#requestlead").formToArray();
		
		if(postcode_old != postcode) {
			form.push({ name: "postcode", value: postcode });
		}
		
		postcode_old = postcode;
		type_old = type;
		
		$.post(url, form, function(value)
		{
			if($("#request").attr('class') == 'blurred')
			{
				$("#request").html(value);
				$("#request").slideDown('slow');
				$("#request").attr('class','active');
				
				if(startform == true) {
					if(typeof(_gaq) != "undefined") {
						try {
							_gaq.push(['_trackPageview', '/form']);
						} catch(err) {}
					}
				}
			}
			else
			{
				$("#request").html(value);
			}
		});
		
	}
	
	return false;
}