$(document) .ready(function () {
	
	var regexMatchesHtmlChars = /[<>]/;
	
	$.validator.addMethod('noHTML', function (value, element) {
		return ! regexMatchesHtmlChars.test(value);
	},
	"Your input contains invalid characters; &lt; and &gt; are not allowed!");
	
	var validator = $('#contactForm') .validate({
		rules: {
			name: {
				minlength:4,
				maxlength:32,
				required: true,
				noHTML: true
			},
			email: {
				required: true,
				email: true
			},
			daytimePhone: {
				digits: true,
				maxlength:15,
				minlength:10
			},
			comments: {
				required: true,
				maxlength:1024,
				noHTML: true
			}
		},
		submitHandler: function (form) {
			makePOSTRequest('/contactUs/sendEmail', $('#contactForm') .serialize());
		}
	});
	
	$("#contactSubmit") .click(function () {
		$('#contactForm') .submit();
	});
	
	$("#contactReset") .click(function () {
		validator.resetForm();
		$('#contactForm') [0].reset();
	});
});

function thanku() {
	var getThanku = document.getElementById("thankuMssg");
	getThanku.innerHTML = "Thank you your message has been sent";
	
	var getContactName = document.getElementById("contactName");
	var getContactEmail = document.getElementById("contactEmail");
	var getContactComments = document.getElementById("contactComments");
	var getContactPhone = document.getElementById("contactPhone");
	var getContactForm = document.getElementById("contactForm");
	
	$(".error") .text("");
	getContactForm.reset();
	getContactName.style.color = "#000";
	getContactEmail.style.color = "#000";
	getContactComments.style.color = "#000";
	getContactPhone.style.color = "#000";
	getContactName.style.backgroundColor = "#fff";
	getContactEmail.style.backgroundColor = "#fff";
	getContactPhone.style.backgroundColor = "#fff";
	getContactComments.style.backgroundColor = "#fff";
	getContactName.value = "* Your Name";
	getContactEmail.value = "* Email Address";
	getContactPhone.value = "* Daytime Phone";
}

function sendError() {
	var getThanku = document.getElementById("thankuMssg");
	getThanku.innerHTML = "<span class='error'>There was an error sending your message.<br /> Please try again.</span>";
}
// ***********************************
// AJAX FUNCTIONS
// ***********************************


function makePOSTRequest(url, parameters) {
	
	$.ajax({
		type: "POST",
		url: url,
		data: parameters,
		success: function (msg) {
			//alert("Thank you");
			thanku();
			//window.location = thankyouPageUrl;
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			var result = eval('(' + XMLHttpRequest.responseText + ')');
			//alert(result.message);
			sendError();
			//window.location = errorPageUrl;
		}
	});
}
