// pop-up contact form for website enquiries

$(document).ready(function() {
	EnquiryContactForm.install();
});

var EnquiryContactForm = {
	form : null,
	email : null,
	subject : null,
//	ajaxURL : "/newcastlebride/enquiry_ajax.php",
	ajaxURL : "/enquiry_ajax.php",

	// create the pop-up form and add it to the page as a hidden element
	install : function() {
		this.hookContactLinks();

		$('<div/>')
			.attr({ className: "enquiryFormPopup", id: "EnquiryContactDiv" })
			.css({ display : "none" })
			.html("<p style='font-weight: bold'>Send an enquiry email</p>\
			<form method='post' id='EnquiryContactForm'>\
			<table>\
			<tr><th>Name</th><td><input type='text' class='inputText' name='Name' size='30' /></td></tr>\
			<tr><th>Email</th><td><input type='text' class='inputText' name='Email' size='30' /></td></tr>\
			<tr><th>Phone</th><td><input type='text' class='inputText' name='Phone' size='30' /></td></tr>\
			<tr><th>Wedding Date</th><td><input type='text' class='inputText' name='Wedding Date' size='30' /></td></tr>\
			<tr><th>Comment</th><td><textarea class='inputText' name='Comment' rows='6' cols='30'></textarea></td></tr>\
			<tr><td></td><td><input type='submit' class='inputButton' value='send' /><input type='button' class='inputButton' value='cancel' id='enquiryFormCancel'/></td></tr>\
			</table>\
			</form>")
			.appendTo("body");

		this.form = document.getElementById("EnquiryContactForm");
		$(this.form).submit(function(event) { EnquiryContactForm.onSubmit(event); });

		$("#enquiryFormCancel").click(function() { $("#EnquiryContactDiv").hide(); });
	},

	// request email address for enquiries, and hook into A: tags that reference it
	hookContactLinks : function() {
		$.ajax({
			type : "GET",
			url : this.ajaxURL,
			cache : true,
			dataType : "json",
			data : { action: "getEmailAddress" },
			success : function(data, status, xhr) {
				// attach click event to all A elements with mailto: links that specify cc={emailAddress}
				var re = new RegExp("^mailto:.*cc=" + data.emailAddress, "i");
				$("a").each(function() {
					if (re.test(this.href)) {

						$(this).click(function(event) {
							// extract address and subject from URI
							var matches = /^mailto:(.*)\?cc=.*subject=(.*)$/i.exec(this.href);
							if (matches && matches.length > 0) {
								event.preventDefault();
								EnquiryContactForm.showForm(decodeURIComponent(matches[1]), decodeURIComponent(matches[2]));
							}
						});

					}
				});
			}
		});
	},

	// show form
	showForm : function(email, subject) {
		this.email = email;
		this.subject = subject;

		var $popup = $("#EnquiryContactDiv");
		var $window = $(window);
		var top = Math.floor(($window.height() - $popup.height()) / 4) + $window.scrollTop();
		var left = Math.floor(($window.width() - $popup.width()) / 2) + $window.scrollLeft();

		$popup
			.css({ "top": top + "px", "left": left + "px" })
			.fadeIn(500);
	},

	// handle form submit
	onSubmit : function(event) {
		event.preventDefault();

		var v = new ValidateForm(this.form);

		v.isEmpty("Name");
		v.isInvalidEmail("Email", true);
		v.isEmpty("Phone");
		v.isEmpty("Wedding Date");

		if (v.hasErrors()) {
			v.showErrors();
		}
		else {
			var e = this.form.elements;
			$.ajax({
				type : "POST",
				url : this.ajaxURL,
				cache : false,
				dataType : "json",
				data : {
					action: "enquiry",
					Subject: this.subject,
					Target: this.email,
					Name: e['Name'].value,
					Email: e['Email'].value,
					Phone: e['Phone'].value,
					WeddingDate: e['Wedding Date'].value,
					Comment: e['Comment'].value
				},
				error : function(xhr, status, e) {
					alert("AJAX error: " + status + "\nPlease try again.");
				},
				success : function(data, status, xhr) {
					if (data.status == "ERROR")
						alert(data.error);
					else
						$("#EnquiryContactDiv").hide();
				}
			});
		}
	}
};
