$(document).ready(function(){
	$("#enquiry-form input.submit").click(function(){
		var txtNameVal = $("#txtName").val();
		var txtCompanyVal = $("#txtCompany").val();
		var txtPhoneVal = $("#txtPhone").val();
		var txtEmailVal = $("#txtEmail").val();
		var txtMessageVal = $("#txtMessage").val();
		$.post($("#enquiry-form").attr("action"),
			{txtName: txtNameVal, txtCompany: txtCompanyVal, txtPhone: txtPhoneVal, txtEmail: txtEmailVal, txtMessage: txtMessageVal, useAjax: "yes"},
			function(data,textStatus){
/*							alert(textStatus);
				alert(data);*/
			});
		return false;
	});

	$("#enquiry-form").ajaxStart(function(){
		if (!$("#form-response").length) {
			$("#enquiry-form").append("<div id=\"form-response\"><div class=\"loading\" style=\"display:none;\"><p class=\"sending\">sending, please wait...</p></div></div>");
		}
		$("#enquiry-form > p, #enquiry-form dl, #enquiry-form input.submit").fadeTo("slow",0.05,function(){
			if (this.tagName.toLowerCase() == "input") {
				$("#form-response .loading").fadeIn("fast");
			}
		});
	});
	$("#enquiry-form").ajaxComplete(function(e,xmlHttp){
		//$("#enquiry-form").append("<p>complete</p>");
	});
	$("#enquiry-form").ajaxError(function(e,xmlHttp){
		$("#form-response .loading").stop().fadeOut("fast",function(){
			$("#form-response .loading").remove();
			$("#form-response").append("<div class=\"error\" style=\"display:none;\"><h2>error</h2><p>There was an error sending your enquiry to accountserve. Please try again later. If the problem persists, please get in contact via the other methods listed on our <a href=\"contact.html\">Contact Us</a> page, and state you were having problems with this form.</p></div>");

			// Adds a link to return to the form
			$("#form-response .error").append("<p><a href=\"#enquiry-form\" class=\"return-to-form\">Return to the enquiry form</a></p>");
			// Adds an event for the users clicking on the link to return to the form
			$("#enquiry-form a.return-to-form").click(function(){
				$("#form-response").fadeOut("fast",function(){
					$("#enquiry-form > p, #enquiry-form dl, #enquiry-form input.submit").fadeTo("slow",1);
					$("#form-response").remove();
				});
				return false;
			});

			$("#form-response .error").fadeIn("fast");
		});
	});
	$("#enquiry-form").ajaxSuccess(function(e,xmlHttp){
		$("#form-response .loading").stop().fadeOut("fast",function(){
			$("#form-response .loading").remove();
			$("#form-response").html(xmlHttp.responseText);
			$("#form-response .thank-you").fadeIn("fast");
			if ($("#form-response .error").length) {
				// Adds a link to return to the form
				$("#form-response .error").append("<p><a href=\"#enquiry-form\" class=\"return-to-form\">Return to the enquiry form</a></p>");
				// Adds an event for the users clicking on the link to return to the form
				$("#enquiry-form a.return-to-form").click(function(){
					$("#form-response").fadeOut("fast",function(){
						$("#enquiry-form > p, #enquiry-form dl, #enquiry-form input.submit").fadeTo("slow",1);
						$("#form-response").remove();
					});
					return false;
				});
				$("#form-response .error").fadeIn("fast");
			}
		});
	});
});
