﻿/// <reference path="../json2.js" />
/// <reference path="../enviro.js" />
/// <reference path="../jquery/jquery-1.3.2.js" />
/// <reference path="../jquery/jquery.pan.js" />
/// <reference path="../jquery/jquery.validate.js" />
/// <reference path="../swfobject-redux.js" />
/// <reference path="../template.js" />

window.$contact = function() {
	// Private Variables
	var _loaded = false;

	var _submitForm = function() {
		$("#ctl00_cphContent_Submit").attr("disabled", "disabled");
		$("#SubmittingForm").show();

		var sFirstname = $("#ctl00_cphContent_firstname").val();
		var sLastname = $("#ctl00_cphContent_lastname").val();
		var sEmail = $("#ctl00_cphContent_email").val();
		var sPhone = $("#ctl00_cphContent_phone").val();
		var sMobile = $("#ctl00_cphContent_mobile").val();
		var sAddress1 = $("#ctl00_cphContent_address1").val();
		var sAddress2 = $("#ctl00_cphContent_address2").val();
		var sAddress3 = $("#ctl00_cphContent_address3").val();
		var sCity = $("#ctl00_cphContent_city").val();
		var sPostcode = $("#ctl00_cphContent_postcode").val();
		var sContact1 = $("#ctl00_cphContent_chkContact")[0].checked;
		var sContact2 = $("#ctl00_cphContent_chkContactBallymore")[0].checked;
		var sComments = "";

		$.ajaxDotNet("/Service.asmx/SubmitForm", {
			data: {
				firstname: sFirstname,
				lastname: sLastname,
				email: sEmail,
				phone: sPhone,
				mobile: sMobile,
				address1: sAddress1,
				address2: sAddress2,
				address3: sAddress3,
				city: sCity,
				postcode: sPostcode,
				contact1: sContact1,
				contact2: sContact2,
				comments: sComments
			},
			success: function(obj) {
				if (obj.d !== undefined && obj.d.length > 0) {
					if (obj.d == "Success") {
						$("#ctl00_cphContent_contact_panel").hide();
						$("#ctl00_cphContent_contactLabel").text("Thank you " + sFirstname
							+ ", for your interest in The Point apartments. We will be in contact shortly.");
						$("#ctl00_cphContent_leadConversion")[0].contentWindow.document.location = "/ThankYou.aspx";
					}
					else {
						$("div.error span").addClass("redtext_bold");
						$("div.error span").html(obj.d);
						$("div.error").show();
					}
				}
			}
		});

		$("#SubmittingForm").hide();
		$("#ctl00_cphContent_Submit").attr("disabled", "");
	};

	var _buildPrivacy = function(obj) {
		if (!obj && !obj.d) {
			return;
		}

		var privacy = $("#ctl00_cphContent_privacyPolicy");
		privacy.children().remove();
		privacy.append(obj.d);
	};

	// Ajax Calls
	var _getPrivacy = function(error) {
		$.ajaxDotNet("/Service.asmx/GetPrivacy", {
			verb: "GET",
			data: {},
			success: _buildPrivacy,
			error: error
		});
	};

	// Builders
	var _createContact = function() {
		var contact = $("<div id=\"contact\"><div id=\"contact_back\"><div class=\"panel\"><div class=\"contact_left\">"
			+ "<div id=\"contactInnerDiv\" class=\"inner\"><h1>Contact Us</h1><div id=\"contactLeft_text\"><p>"
			+ "<span class=\"redtext_bold\">CALL US ON:</span> 0800 092 7070</p><br /><h2 class=\"enquiry\">Make an enquiry</h2>"
			+ "</div><div id=\"contactRight_text\"><p><span class=\"redtext_bold\">EMAIL US AT:</span> "
			+ "<a href=\"mailto:info@highpointvillage.co.uk\" title=\"Send an email to info@highpointvillage.co.uk\">"
			+ "info@highpointvillage.co.uk</a></p><br /></div><div id=\"ctl00_cphContent_contact_panel\">"
			+ "<div class=\"error\" style=\"display: none;\"><span>&nbsp;</span></div><div id=\"contactLeft\"><p>"
			+ "<label for=\"ctl00_cphContent_firstname\" id=\"ctl00_cphContent_lblfirstname\" title=\"Enter your first name\">"
			+ "First Name: <span class=\"redtext\">*</span></label><input type=\"text\" maxlength=\"50\""
			+ " id=\"ctl00_cphContent_firstname\" class=\"textbox_medium required\" /></p><p>"
			+ "<label for=\"ctl00_cphContent_lastname\" id=\"ctl00_cphContent_lblLastname\" title=\"Enter your last name\">"
			+ "Last Name: <span class=\"redtext\">*</span></label><input type=\"text\" maxlength=\"50\""
			+ " id=\"ctl00_cphContent_lastname\" class=\"textbox_medium required\" /></p><p>"
			+ "<label for=\"ctl00_cphContent_email\" id=\"ctl00_cphContent_lblEmail\" title=\"Enter your email address\">"
			+ "Email: <span class=\"redtext\">*</span></label><input type=\"text\" maxlength=\"100\" id=\"ctl00_cphContent_email\""
			+ " class=\"textbox_large required email\" /></p><p><label for=\"ctl00_cphContent_phone\""
			+ " id=\"ctl00_cphContent_lblPhone\" title=\"Enter your phone number\">Phone: <span class=\"redtext\">*</span>"
			+ "</label><input type=\"text\" maxlength=\"40\" id=\"ctl00_cphContent_phone\""
			+ " class=\"textbox_medium required numeric\" /></p><p><label for=\"ctl00_cphContent_mobile\""
			+ " id=\"ctl00_cphContent_lblMobile\" title=\"Enter your mobile phone number\">Mobile: </label>"
			+ "<input type=\"text\" maxlength=\"40\" id=\"ctl00_cphContent_mobile\" class=\"textbox_medium numeric\" /></p>"
			+ "</div><div id=\"contactRight\"><p id=\"houseno_row\"><label for=\"ctl00_cphContent_houseno\""
			+ " id=\"ctl00_cphContent_lblHouseNo\" title=\"Enter your house number\">House Number: <span class=\"redtext\">*"
			+ "</span></label><input type=\"text\" maxlength=\"5\" id=\"ctl00_cphContent_houseno\""
			+ " class=\"textbox_verysmall required\" /></p><div id=\"address_panel\" class=\"address_panel_start\"><p>"
			+ "<label for=\"ctl00_cphContent_address1\" id=\"ctl00_cphContent_lblAddress1\""
			+ " title=\"Enter the first line of your address\">Address1: <span class=\"redtext\">*</span></label>"
			+ "<input type=\"text\" maxlength=\"50\" id=\"ctl00_cphContent_address1\" class=\"textbox_medium required\" /></p>"
			+ "<p><label for=\"ctl00_cphContent_address2\" id=\"ctl00_cphContent_lblAddress2\""
			+ " title=\"Enter the second line of your address\">Address2: </label><input type=\"text\" maxlength=\"50\""
			+ " id=\"ctl00_cphContent_address2\" class=\"textbox_medium\" /></p><p><label for=\"ctl00_cphContent_address3\""
			+ " id=\"ctl00_cphContent_lblAddress3\" title=\"Enter the third line of your address\">Address3: </label>"
			+ "<input type=\"text\" maxlength=\"50\" id=\"ctl00_cphContent_address3\" class=\"textbox_medium\" /></p>"
			+ "<p><label for=\"ctl00_cphContent_city\" id=\"ctl00_cphContent_lblCity\" title=\"Enter your city\">City: "
			+ "<span class=\"redtext\">*</span></label><input type=\"text\" maxlength=\"50\" id=\"ctl00_cphContent_city\""
			+ " class=\"textbox_medium required\" /></p></div><p><label for=\"ctl00_cphContent_postcode\""
			+ " id=\"ctl00_cphContent_lblPostcode\" title=\"Enter your post code\">Post Code: <span class=\"redtext\">*</span>"
			+ "</label><input type=\"text\" maxlength=\"10\" id=\"ctl00_cphContent_postcode\" class=\"textbox_small required\" />"
			+ " <input type=\"image\" id=\"ctl00_cphContent_FindAddress\" title=\"Find my address\""
			+ " src=\"images/contact/find-address.png\" alt=\"Find Address Button\" style=\"border-width:0px;\" />"
			+ "<input type=\"image\" id=\"ctl00_cphContent_ReCheckAddress\" title=\"Re-check my address\""
			+ " src=\"images/contact/recheck.png\" alt=\"Re-Check Address Button\" style=\"border-width:0px;display: none;\" />"
			+ "</p></div><p class=\"indent\"><input id=\"ctl00_cphContent_chkContact\" type=\"checkbox\" checked=\"checked\" />"
			+ " <label for=\"ctl00_cphContent_chkContact\" id=\"ctl00_cphContent_lblContact1\""
			+ " title=\"If you would prefer NOT to receive regular updates on the progress of The Point,"
			+ " along with pre-release news on the new apartments, please un-tick the box.\" class=\"large\">"
			+ "I would like to receive regular updates on the progress of The Point.</label></p><p class=\"indent\">"
			+ "<input id=\"ctl00_cphContent_chkContactBallymore\" type=\"checkbox\" checked=\"checked\" />"
			+ " <label for=\"ctl00_cphContent_chkContactBallymore\" id=\"ctl00_cphContent_lblContact2\""
			+ " title=\"Sometimes Ballymore will send out general information on their developments,"
			+ " please un-tick this box if you would prefer NOT to receive this.\" class=\"large\">"
			+ "I would like to receive updates on other Ballymore developments.</label></p><p class=\"indent1\">"
			+ "<input type=\"image\" id=\"ctl00_cphContent_Submit\" title=\"Submit enquiry\" src=\"images/contact/submit.png\""
			+ " alt=\"Submit Enquiry Button\" style=\"border-width:0px;\" /><span id=\"SubmittingForm\" style=\"display: none;\">"
			+ "Submitting, please wait...</span></p><p>We will not send your details to any third parties without your permission."
			+ " Click here for our <a href=\"#\" id=\"privacyLink\" title=\"Open our privacy policy - opens in a new window\">"
			+ "privacy policy</a>.</p></div><p id=\"ctl00_cphContent_contactLabel\"></p></div></div><div id=\"ctl00_cphContent_privacy\">"
			+ "<div class=\"innerPrivacy\"><h1>Privacy Policy</h1>"
			+ "<div id=\"ctl00_cphContent_privacyPolicy\"></div></div><a href=\"#\" class=\"close\">close</a></div></div></div>"
			+ "</div>").insertBefore("#nav");

		if ($.browser.msie && $.browser.version < 7) {
			contact.find(".contact_left").before("<div class=\"contact_left_ie6\"></div>");
		}

		return contact;
	};

	// Callbacks
	var _updateSection = function(obj) {
		if (!obj || !obj.d) {
			return;
		}

		changeTitle(obj.d.MetaTitle);
	};

	// Ajax Calls
	var _getSection = function() {
		getSection("contact", _updateSection);
	};

	// Animations
	var _showPrivacy = function() {
		$("#ctl00_cphContent_privacy").animate({
			'left': '0px'
		});
	};

	var _hidePrivacy = function() {
		$("#ctl00_cphContent_privacy").animate({
			'left': '-730px'
		});
	};

	// Private Methods
	var _init = function() {
		if (!entry.contact) {
			_createContact();
			_getPrivacy();
		}

		$("#contact .numeric").numeric();
		$.validator.messages.required = "";

		$("form").bind("invalid-form.validate", function(e, validator) {
			e.preventDefault();
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
				? "You haven\'t completed 1 field. It has been highlighted below"
				: "You haven\'t completed some fields. They have been highlighted below";
				$("div.error span").html(message);
				$("div.error span").addClass("redtext_bold");
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		}).validate({
			onkeyup: false,
			submitHandler: _submitForm,
			messages: {
				email: {
					required: " ",
					email: "Please enter a valid email address, example: you@yourdomain.com"
				}
			},
			debug: false
		});

		$("#ctl00_cphContent_FindAddress").click(function(evt) {
			evt.preventDefault();
			checkAdd();
		});
		$("#ctl00_cphContent_ReCheckAddress").click(function(evt) {
			evt.preventDefault();
			recheckAddress();
		});
		$("#privacyLink").click(function(evt) {
			evt.preventDefault();
			_showPrivacy();
		});
		$("#ctl00_cphContent_privacy a.close").click(function(evt) {
			evt.preventDefault();
			_hidePrivacy();
		});
	};

	var _show = function() {
		$("#contact").css({
			"display": "block"
		});

		_getSection();

		$("#contact_back").css({ backgroundPosition: "-950px 0" })
			.animate({ backgroundPosition: "(0px 0px)" }, { duration: 1000 });

		$("#ctl00_cphContent_ReCheckAddress").css("display", "none");
	};

	var _hide = function() {
		$("#contact").css("display", "none");
		$("#ctl00_cphContent_contact_panel").css("display", "block");
		$("#ctl00_cphContent_contactLabel").text("");
	};

	return {
		// Public Properties
		isLoaded: function() {
			return _loaded;
		},
		type: "contact",
		// Public Methods
		load: function() {
			if (!_loaded) {
				_init();
				_loaded = true;
			}
			_show();
		},
		show: _show,
		hide: _hide
	};
} ();