// Validation
$().ready(function() {
	$("#property-plan-form").validate({
		submitHandler: 
			function() { 
				propertyplanform();
		},								 
		rules: {
			FirstName: "required",
			Surname: "required",
			ContactNumber: "required",
			referral: "required",
			Other: "required",
			Email: {
				required: true,
				email: true
			},
			State: {
				required: true
			},
		messages: {
			FirstName: "Please enter your First Name",
			Surname: "Please enter your Surname",
			ContactNumber: "Please enter a Contact Number",
			Email: "Please enter a valid email address",
			referral: "Please select a referral",
			State: "Please enter a State"	
		}
		}
	});
});

$.metadata.setType("attr", "validate");



//contact form	
function propertyplanform() {

	var sendForm = function(element) {
		//alert("works");
		var xhr = new Hijax();
		xhr.setContainer(element);
		xhr.setUrl("/email-actions/plan.aspx");
		xhr.setCanvas(document.getElementById("property-plan-response"));
		xhr.setLoading(function() {
			displayLoading(document.getElementById("property-plan-response"));
		});
		xhr.setCallback(function() {
			success(document.getElementById("property-plan-response"));
			//alert("sent!");
	
		});
		xhr.setErrorhandler(function() {
			displayError();
		});	
		xhr.captureData();
	};
	
	var displayLoading = function(element) {
		$('#property-plan-response').html('<div id="loader"><p class="loading-text">Sending</p> <img src="/Content/images/shared/misc/loading.gif" alt="Loading" class="loading"></div>');		
	};
	
	var displayError = function() {
		$("#property-plan-response p").html('<p class="error">Unfortunately there was an error processing your registration. Please try again later.</p>');
	
	};
	
	var success = function(element) {
		$('#property-plan-response').html('<div id="message"><p class="success">Your e-mail has been successfully submitted and a staff member will be in contact with you shortly</p></div>')	
		$('#property-plan-form').slideUp();
	};
	
	
	
	var cform = document.getElementById("property-plan-form");
	sendForm(cform);
	cform = null;
	 
}

