

var qForm;
var currentStep = 1;
var rePostcode = /^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY])|[0-9][A-HJKSTUW]) [0-9][ABD-HJLNP-UW-Z]{2}))$/;
var reInteger = /^[0-9]+$/;
var mort_value = 0;

$(function () {
	if ($("form#propertyform").length > 0) {
		qForm = $("form#propertyform").get(0);
		$("#propertyform").append('<p><a href="#" id="prev_step">&laquo; Prev</a> <a href="#" id="next_step">Next &raquo;</a></p>');
		$("#propertyform a").click(function () { return show_step(this, true); });
		$("#mortgage").click(function () { 
			if (this.checked) { 
				qForm.mort_value.disabled = false;
				qForm.mort_value.value = mort_value;
				qForm.mort_value.focus(); 
				qForm.mort_value.select(); 
			} else { 
				qForm.mort_value.disabled = true; 
				mort_value = qForm.mort_value.value;
				qForm.mort_value.value = "0";
			} 
		});
		if (qForm.mortgage.checked) { 
			qForm.mort_value.disabled = false; 
		} else { 
			qForm.mort_value.disabled = true; 
			qForm.mort_value.value = 0;
		}
		upper_case(qForm.prop_postcode);
		upper_case(qForm.postcode);
		$("#prop_postcode, #postcode").blur(function () { upper_case(this); }).focus(function () { upper_case(this); });
		clean_phone(qForm.phone);
		$("#phone").blur(function () { clean_phone(this); }).focus(function () { clean_phone(this); });
		$("#propertyform h3").hide();
		qForm.email.value = "";
		$("#robottrap").remove();
		show_step($("#prev_step").get(0), false);
		$(qForm).submit(function () {
			if (currentStep < 5) {
				show_step($("#next_step").get(0), true);
				return false;
			} else {
				return true;
			}
		});
	}
});

function upper_case(field) {
	field.value = field.value.toUpperCase();
}
function clean_phone(field) {
	field.value = field.value.replace("-", " ").replace(/[A-Za-z\/\()\.]/g, "");
}

function validate_email(email) {
	var reEmail1 = /^[^@]{1,64}@[^@]{1,255}$/;
	var reEmail2 = /^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/;
	var reEmail3 = /^\[?[0-9\.]+\]?$/;
	var reEmail4 = /^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/;
	// First, we check that there's one @ symbol, and that the lengths are right
	if (!reEmail1.test(email)) {
		// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
		return false;
	}
	
	// Split it into sections to make life easier
	var email_array = email.split("@");
	var local_array = email_array[0].split(".");
	for (var i = 0; i < local_array.length; i++) {
		if (!reEmail2.test(local_array[i])) {
			return false;
		}
	}  
	if (!reEmail3.test(email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
		var domain_array = email_array[1].split(".");
		if (domain_array.length < 2) {
			return false; // Not enough parts to domain
		}
		for (var i = 0; i < domain_array.length; i++) {
			if (!reEmail4.test(domain_array[i])) {
				return false;
			}
		}
	}
	return true;
}



function show_step(anchor, validate) {
	var newStep = 0;
	if ($(anchor).id() == "prev_step") {
		newStep = currentStep - 1;
		validate = false;
	} else {
		newStep = currentStep + 1;
	}
	
	// Validate input of current step
	if (validate) {
		if (currentStep == 1) {
			// No Validation to do
		} else if (currentStep == 2) {
			if (qForm.prop_address.value.length < 1) {
				alert("You must enter the address of the property");
				qForm.prop_address.focus();
				return false;
			} else if (qForm.prop_postcode.value.length < 1) {
				alert("You must enter the postcode of the property");
				qForm.prop_postcode.focus();
				return false;
			} else if (!rePostcode.test(qForm.prop_postcode.value)) {
				alert("The postcode of the property must be a valid UK postcode");
				qForm.prop_postcode.focus();
				qForm.prop_postcode.select();
				return false;
			} else if (!reInteger.test(qForm.value.value) || Number(qForm.value.value) < 1) {
				alert("The value of the property must be a positive number - please do not use spaces, commas or full stops");
				qForm.value.focus();
				qForm.value.select();
				return false;
			}
		} else if (currentStep == 3) {
			if (qForm.transaction[0].checked) {
				// No validation to do
			} else {
				if (qForm.mortgage.checked && (!reInteger.test(qForm.mort_value.value) || Number(qForm.mort_value.value) < 1)) {
					alert("If the property is mortgaged, you must specify the mortgage value as a positive number - please do not use spaces, commas or full stops");
					qForm.mort_value.focus();
					qForm.mort_value.select();
					return false;
				}
			}
		} else if (currentStep == 4) {
			if (qForm.name.value.length < 1) {
				alert("You must specify your name");
				qForm.name.focus();
				return false;
			} else if (qForm.address.value.length < 1) {
				alert("You must specify your postal address");
				qForm.address.focus();
				return false;
			} else if (qForm.postcode.value.length < 1) {
				alert("You must specify your postcode");
				qForm.postcode.focus();
				return false;
			} else if (!rePostcode.test(qForm.postcode.value)) {
				alert("Your postcode must be a valid UK postcode");
				qForm.postcode.focus();
				qForm.postcode.select();
				return false;
			} else if (qForm.phone.value.length < 1) {
				alert("You must specify your telephone number");
				qForm.phone.focus();
				return false;
			} else if (qForm.ea.value.length < 1) {
				alert("You must specify your email address");
				qForm.ea.focus();
				return false;
			} else if (!validate_email(qForm.ea.value)) {
				alert("The email address specified does not appear to be valid");
				qForm.ea.focus();
				return false;
			}
		} else if (currentStep == 5) {
			// No validation to do
		}
	}
	
	// Display each step
	
	hide_steps();
	if (newStep <= 1) {
		$("#prev_step").hide();
		$("#next_step").show();
		$("p#explain").html('Step 1 of 5 &mdash; are you selling or purchasing the property?');
		$("#step1").show();
		qForm.transaction[0].focus();
		currentStep = 1;
	} else if (newStep == 2) {
		$("#prev_step").show();
		$("#next_step").show();
		if (qForm.transaction[0].checked) {
			$("p#explain").html('Step 2 of 5 &mdash; details of the property you are selling');
		} else {
			$("p#explain").html('Step 2 of 5 &mdash; details of the property you are buying');
		}
		$("#step2").show();
		qForm.prop_address.focus();
		currentStep = 2;
	} else if (newStep == 3) {
		$("#prev_step").show();
		$("#next_step").show();
		if (qForm.transaction[0].checked) {
			$("p#explain").html('Step 3 of 5 &mdash; mortgage details and Land Registry registration');
			$("#sale").show();
			qForm.mortgaged[0].focus();
		} else {
			$("p#explain").html('Step 3 of 5 &mdash; mortgage details');
			$("#purchase").show();
			qForm.mortgage.focus();
		}
		currentStep = 3;
	} else if (newStep == 4) {
		$("#prev_step").show();
		$("#next_step").show();
		$("p#explain").html('Step 4 of 5 &mdash; your contact information');
		$("#step4").show();
		qForm.name.focus();
		currentStep = 4;
	} else {
		$("#prev_step").show();
		$("#next_step").hide();
		$("p#explain").html('Step 5 &mdash; final step &mdash; select your contact preferences and submit your details');
		$("#step5").show();
		qForm.contact.focus();
		currentStep = 5;
	}
	//alert($(anchor).html() + ": " + currentStep + "->" + newStep);
	return false;
}

function hide_steps() {
	$("#step1").hide();
	$("#step2").hide();
	$("#sale").hide();
	$("#purchase").hide();
	$("#step4").hide();
	$("#step5").hide();
}