jQuery(document).ready(function($) {
	
	$("#confirmationDialog").replaceWith('<div id="confirmationDialog" title="Sending Contact"><div id="progressbar"></div></div>');
	
	$("#dialog").replaceWith('<div id="dialog" title="Contact Us"><p id="validateTips">Name and Email are required.</p><form action="#"><fieldset><label for="name">Name</label><input type="text" name="name" id="name" class="text ui-widget-content ui-corner-all" size="32" /><label for="phone">Phone Number</label><input type="text" name="phone" id="phone" value="" class="text ui-widget-content ui-corner-all" size="32" /><label for="email">Email</label><input type="text" name="email" id="email" value="" class="text ui-widget-content ui-corner-all" size="32" /><label for="subject">Subject</label> <select id="subject"><option value="question">General Question</option><option value="info">Trip Info</option><option value="request">Booking request</option><option value="other">Other</option></select><label for="comments">Comments</label><textarea id="comments" name="comments" class="text ui-widget-content ui-corner-all" rows="3" cols="32"></textarea></fieldset></form></div>');
	

	var name = $("#name"),
		email = $("#email"),
		password = $("#password"),
		phone = $("#phone"),
		allFields = $([]).add(name).add(email).add(password).add(phone),
		tips = $("#validateTips");

	function updateTips(t) {
		tips.text(t).effect("highlight",{},1500);
	}

	function checkLength(o,n,min,max) {

		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}

	}

	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}

	}

	$("#confirmationDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			modal: true,
			hide: 'drop',
			buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}
		});


	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 475,
		modal: true,
		show: 'drop',
		hide: 'drop',
		buttons: {
			Cancel: function() {
				$(this).dialog('close');
			},
			'Submit': function() {
						
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"username",3,16);
				bValid = bValid && checkLength(email,"email",6,80);
				
				bValid = bValid && checkRegexp(name,/^([a-zA-Z])+\s?([a-zA-Z])*\s?([a-zA-Z])*$/,"Invalid Name");
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. trip@ecoplanetadventure.com");

				//Action to complete after validation.
				if (bValid) {
				 	
					if(typeof(pageTracker) == "object")//Check that pageTracker object has been initialized.
						pageTracker._trackPageview('/contactsubmit');//Send pageview to Google Analytics.
				 
					$.ajax({
						type: "POST",	 
						url: "/wordpress/wp-admin/admin-ajax.php",
						data: "action=sendBasicContact&name=" + $("#name").val() + "&emailAddress=" + $("#email").val() + "&phoneNumber=" + $("#phone").val() + "&subject=" +$("#subject").val() + "&comments="+ $("#comments").val() +"&formType=contactInfo",
						success: AjaxSucceeded,  
						error: AjaxFailed	 
					});	


					//close dialog window.
					$("#confirmationDialog").html("<div id='progressbar'></div>");
					$("#progressbar").progressbar({value: 100});
					$('#confirmationDialog').dialog('open');
					$(this).dialog('close');
				}
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});

	$('#panelBtn').click(function() {
		$('#dialog').dialog('open');
		return false;
	});
	
	//sucessful ajax callback
	function AjaxSucceeded(result) {	
		$("#progressbar").progressbar('destroy');
		$("#confirmationDialog").html("<p><span class='ui-icon ui-icon-circle-check' style='float:left; margin:0 7px 50px 0;'></span>Your Inquiry was sent successfully, check your email for further details.</p>");
	}	 
	
	//unsuccessful ajax callback
	function AjaxFailed(result) {
		$("#progressbar").progressbar('destroy');
		$("#confirmationDialog").html("<p><span class='ui-icon ui-icon-circle-close' style='float:left; margin:0 7px 50px 0;'></span>Your Inquiry was unable to be sent. Please try again.</p>");
	}

});