/*************************************************************
	website:	www.bestschoolsinmichigan.com	
	type:		Validation / Functionality
	desc:		javascript validation and functionality for
				Newsletter sign-up form
	version:	1.0.0                      
-----------------------------------------------------------
	author:		Jeremy S. Ward
	date:		25 March 2009

*************************************************************/

$(document).ready(function () {
	$('#newsletter_sign_up').click(function (e) {
		e.preventDefault();
		// load the contact form using ajax
		$.get("/data/form_newsletter.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				close: false,
				position: ["15%",],
				overlayId: 'contact-overlay',
				containerId: 'contact-container',
				onOpen: contact.open,
				onShow: contact.show,
				onClose: contact.close
			});
		});
	});

	// preload images
	var img = ['form_bottom.gif', 'form_top.gif', 'loading.gif'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'imgages/modal/' + this;
	});
	
	// form validation
	$('input#myEmail').livequery ('blur', function()
	{
		var inputValue = $(this).val();
		var reg = new RegExp("^[0-9a-zA-Z\._]+@[0-9a-zA-Z]+[\.]{1}[0-9a-zA-Z]+[\.]?[0-9a-zA-Z]+$");
		var match = reg.exec(inputValue);
		//alert("match = "+match);
		if (match == null)
		{
			if (inputValue.length == 0)
			{
				var topMessage = "This field cannot be left blank";
				var mainMessage = "Please enter your email address";
			}
			else
			{
				var topMessage = "The email you entered is not valid";
				var mainMessage = "Please enter a valid email address";
			}
			$("#error_myEmail, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageTop: topMessage,
				messageMain: mainMessage
			});
			addToContainerHeight("#contact-container .contact-content", "#error_myEmail");
		}
		else
		{
			subtractFromContainerHeight("#contact-container .contact-content", "#error_myEmail");
			$("#error_myEmail, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	$('input#confirmMyEmail').livequery ('blur', function()
	{
		var email1 = $('input#myEmail').val().toLowerCase();
		var email2 = $(this).val().toLowerCase();
		
		if (email2 != email1)
		{
			var topMessage = "The email addresses you entered do NOT match!";
			var mainMessage = "Please re-enter your email address.";
			
			$("#error_confirmMyEmail, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageTop: topMessage,
				messageMain: mainMessage
			});
			addToContainerHeight("#contact-container .contact-content", "#error_confirmMyEmail");
		}
		else
		{
			subtractFromContainerHeight("#contact-container .contact-content", "#error_confirmMyEmail");
			$("#error_confirmMyEmail, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	$('input#fn').livequery ('blur', function()
	{
		var inputValue = $(this).val();
		var nameLength = inputValue.length;
		
		if (nameLength < "2")
		{
			$("#error_fn, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageMain: "Please enter your first name."
			});
			addToContainerHeight("#contact-container .contact-content", "#error_fn");
		} 
		else
		{
			subtractFromContainerHeight("#contact-container .contact-content", "#error_fn");
			$("#error_fn, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	$('input#ln').livequery ('blur', function()
	{
		var inputValue = $(this).val();
		var nameLength = inputValue.length;
		
		if (nameLength < "2")
		{
			$("#error_ln, div.errorClear").remove();
			$(this).errorMessage(
			{
				messageMain: "Please enter your last name"
			});
			addToContainerHeight("#contact-container .contact-content", "#error_ln");
		} 
		else
		{
			subtractFromContainerHeight("#contact-container .contact-content", "#error_ln");
			$("#error_ln, div.errorClear").remove();
			$(this).removeClass("errorFocus");
		}
	});
	
	
});

function addToContainerHeight(container, newElement) {
	var containerHeight = $(container).height();
	var newElementHeight = $(newElement).height();
	var newHeight = containerHeight + newElementHeight + 10;
	$(container).height(newHeight);
}

function subtractFromContainerHeight(container, oldElement) {
	var containerHeight = $(container).height();
	var newHeight = containerHeight - $(oldElement).height();
	$(container).height(newHeight);
}

var contact = {
	message: null,
	open: function (dialog) {

		var title = $('#contact-container .contact-title').html();
		$('#contact-container .contact-title').html('Loading...');
		dialog.overlay.fadeIn(200, function () {
			dialog.container.fadeIn(200, function () {
				dialog.data.fadeIn(200, function () {
					$('#contact-container .contact-content').animate({height: 330}, function () {
						$('#contact-container .contact-title').html(title);
						$('#contact-container form').fadeIn(200, function () {
							$('#contact-container input[name=email_address]').focus();
							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#contact-container .contact-button').each(function () {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#contact-container .contact-send').click(function (e) {
			e.preventDefault();
			
			
			$('#contact-container .contact-message').fadeOut(function () {
				$('#contact-container .contact-message').removeClass('contact-error').empty();
			});
			$('#contact-container .contact-title').html('Registering your email...');
			$('#contact-container form').fadeOut(200);
			$('#contact-container .contact-content').animate({height: '80px'}, function () {
				$('#contact-container .contact-loading').fadeIn(200, function () {
					$.ajax({
						url: '/data/form_newsletter.php',
						data: $('#contact-container form').serialize() + '&action=send',
						type: 'post',
						cache: false,
						dataType: 'html',
						complete: function (xhr) {
							var thisResponse = xhr.responseText;
							$('#contact-container .contact-loading').fadeOut(200, function () {
								$('#contact-container .contact-title').html(thisResponse).fadeIn(200);
//								$('#contact-container .contact-message').html(thisResponse).fadeIn(200);
							});
						}
					});
				});
			});
		});
	},
	close: function (dialog) {
		$('#contact-container .contact-message').fadeOut();
		$('#contact-container .contact-title').html('Goodbye...');
		$('#contact-container form').fadeOut(200);
		$('#contact-container .contact-content').animate({
			height: 40
		}, function () {
			dialog.data.fadeOut(200, function () {
				dialog.container.fadeOut(200, function () {
					dialog.overlay.fadeOut(200, function () {
						$.modal.close();
					});
				});
			});
		});
	}
};


