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

// Ready

$(function() 
{
	// Voeg events to
		
	$("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);
	});
	$("input[name='type2']").live("click",function()
	{
		$(this).attr('selected','selected');
		process(false);
	});
	// submit on specific input elements
	$("input.process").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;
	});
	
	// select antecedent radio button on focus of textinput
	$("input[type='text'].focusradio").live("click", function()
	{	
		$(this).prev("input[type='radio']").attr("checked",'checked');
	});
	
	// select multiple checkboxes with shift-key
	var lastChecked = null;
	$("input[type='checkbox']").live("click", function(event) 
	{
		if(!lastChecked) {
			lastChecked = this;
			return;
		}

		if(event.shiftKey) {
			var start = $("input[type='checkbox']").index(this);
			var end = $("input[type='checkbox']").index(lastChecked);

			for(i=Math.min(start,end);i<=Math.max(start,end);i++) {
				$("input[type='checkbox']")[i].checked = lastChecked.checked;
			}
		}

		lastChecked = this;
	});
	
	// Process
	
	var postcode = $("input[name='postcode']").attr('value');
	var type = $("input[name='type']").attr('value');
	
	if(postcode != undefined && postcode.length >= 4)
	{
		if(type > 0)
		{
			process(false);
		}
	}
	
	// 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;
	});
});

// Form submit functions

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

// Process function

function process(startform)
{
	var postcode = $("input[name='postcode']").attr('value');
	var type = $("input[name='type']:checked").attr('value');
	var type2 = $("input[name='type2']:checked").attr('value');
	
	//if((postcode_old != postcode || type != type_old || type2 != type_old2) && postcode.length >= 1)
	if((postcode_old != postcode || type != type_old || type2 != type_old2 || 1 == 1) && 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;
		type_old2 = type2;
		
		$.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);
			}
			
			// Hack because incompatibility with ie6/7
			$("select.process").unbind("change");
			$("select.process").bind("change",function()
			{	
				$(this).attr('selected','selected');
				process(false);
			});
		});
	}
	
	return false;
}