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);
		}
	});
	
	$(document).delegate("input[name='type']", "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;
	});
	
	// Select antecedent radio button on focus of textinput
	
	$("input[type='text'].focusradio").live("click", function() {	
		$(this).prev("input[type='radio']").attr("checked",'checked');
	});
	
	// 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 && $.browser.msie) {
		$("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
	
	$("div.requestbutton table.ajaxbutton a.submit").live("click",function() {
		$("#requestlead").formSubmit({
	        before: function_before,  // pre-submit callback 
	        error: function_error,  // error callback 
	        success: function_succes // post-submit callback
		});
		
		return false;
	});
	
	// Google analytics
	
	if($("input[name='googleanalytics'][type='hidden']").val() != '') {
		try {
			var analytics = $.deparam($("input[name='googleanalytics'][type='hidden']").val());
		
			if(analytics.transaction) {
				var addTransaction = ['_addTrans'];
				
				for(var i=0; i < analytics.transaction.length; i++) {
					addTransaction[i+1] = analytics.transaction[i];
				}
				
				if(addTransaction.length > 1) {
					_gaq.push(addTransaction);
				}
			}
			
			if(analytics.items) {
				for (var key in analytics.items) {
					var addItem = ['_addItem'];
					
					for(var i=0; i < analytics.items[key].length; i++) {
						addItem[i+1] = analytics.items[key][i];
					}
					
					if(addItem.length > 1) {
						_gaq.push(addItem);
					}
				}
			}
			
			_gaq.push(['_trackTrans']);
		} catch(err) {}
	}
	
	// Privacy expand
	
	$(".sub_privacy").live("click", function() {	
		$(this).siblings(".msg_privacy").toggle(100);
		return false;
	});
	
	$(".sub_privacy").live("click", function() {
		$(this).parent(".msg_privacy").toggle(100);
		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 {
		$("#requestlead").submit();
	}
}

function function_succes(data)
{
	try {
		var analytics = $("<input>").attr('type','hidden').attr('name','analytics').attr('value',$.param(data.analytics));
		
		$("#requestlead").append(analytics);
	} catch(e) {}
	
	$("#requestlead").submit();
}

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