jQuery(document).ready(function($) {
	strRgxName = /^\s*$/;
	strRgxEmail = /^\s*(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))\s*$/;	//Matches an email address and ignores whitespace at either end (delete the '\s*' at either end to stop allowing whitespace)
	strInitNameTxt = $("#Input_MlstName").val();
	strInitEmailTxt = $("#Input_MlstEmail").val();
	
	$("#Div_Mlst div.Nrm").hover(
			function() {
				$(this).addClass("Hvr");
			},
			function() {
				$(this).removeClass("Hvr");
			}
	);

	$("#Input_MlstName, #Input_MlstEmail").focus(function() {
		var $this = $(this);
		if ((($this.attr("id") == "Input_MlstName") && ($this.val() == strInitNameTxt)) || (($this.attr("id") == "Input_MlstEmail") && ($this.val() == strInitEmailTxt))) { 
			$this.removeClass("Dft");
			$this.addClass("Nrm");
			$this.val("");
		} else {
			$this.select();
		}

		var $Inp_Name = $("#Input_MlstName");
		var $Inp_Email = $("#Input_MlstEmail");
		
		if ((!strRgxName.test($Inp_Name.val())) && (strRgxEmail.test($Inp_Email.val()))) {
			$('#Div_Mlst div.Nrm').show();
			$('#Div_Mlst div.Uns').hide();
			$('#Div_Mlst div.Wtg').hide();
		} else {
			$('#Div_Mlst div.Nrm').hide();
			$('#Div_Mlst div.Uns').show();
			$('#Div_Mlst div.Wtg').hide();
		}
	});

	$("#Input_MlstName, #Input_MlstEmail").blur(function() {
		var $this = $(this);
		if ($this.val().length == 0) {
			$this.addClass("Dft");
			$this.removeClass("Nrm");
			if ($this.attr("id") == "Input_MlstName") {
				$this.val(strInitNameTxt);
			} else {
				$this.val(strInitEmailTxt);				
			}
		}
	});
	
	$("#Input_MlstName, #Input_MlstEmail").keypress(function (e) {
		if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			e.preventDefault();
			var $Inp_Name = $("#Input_MlstName");
			var $Inp_Email = $("#Input_MlstEmail");

			if ((!strRgxName.test($Inp_Name.val())) && (strRgxEmail.test($Inp_Email.val()))) {
				Submit();
			}
		}
	});
	
    $("#Input_MlstName, #Input_MlstEmail").keyup(function(e) {
    	if (!(e.which && e.which == 13) && !(e.keyCode && e.keyCode == 13)) {
			var $Inp_Name = $("#Input_MlstName");
			var $Inp_Email = $("#Input_MlstEmail");

			if ((!strRgxName.test($Inp_Name.val())) && (strRgxEmail.test($Inp_Email.val()))) {
				$('#Div_Mlst div.Nrm').show();
				$('#Div_Mlst div.Uns').hide();
				$('#Div_Mlst div.Wtg').hide();
			} else {
				$('#Div_Mlst div.Nrm').hide();
				$('#Div_Mlst div.Uns').show();
				$('#Div_Mlst div.Wtg').hide();
			}
    	}
    });

    $("#Div_Mlst div.Nrm").click(function() {
		Submit();
	});
    
	function Submit() {
		$('#Div_Mlst div.Nrm').hide();
		$('#Div_Mlst div.Uns').hide();
		$('#Div_Mlst div.Wtg').show();

		var strName = $("#Input_MlstName").val();
		strName = strName.replace(/^\s+/, "");
		strName = strName.replace(/\s+$/, "");
		
		var strAddress = $("#Input_MlstEmail").val();
		strAddress = strAddress.replace(/^\s+/, "");
		strAddress = strAddress.replace(/\s+$/, "");
		
		$.ajax({
			type: "POST",
			url: "http://effm.org.au/addToMailingList",
			dataType: "text",
			data: {
				email: strAddress,
				firstname: strName
			},
			success: function(data, textStatus, XMLHttpRequest) {
				$("#Div_MlstIni").fadeOut(1000, function() {
					$("#Div_MlstSuc").show();
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#Div_MlstIni").fadeOut(1000, function() {
					$("#Div_MlstSuc").show();
				});
				//Uncomment the following to show the error message if an error occurs
				//$("#Div_MlstIni").fadeOut(1000, function() {
					//$("#Div_MlstErr").show();
				//});
			}
		});
	}
	
	$("#Div_Mlst span").click(function(){ 
		$("#Div_MlstErr").hide();
		$("#Input_MlstName").val(strInitNameTxt);
		$("#Input_MlstName").addClass("Dft");
		$("#Input_MlstName").removeClass("Nrm");
		$("#Input_MlstEmail").val(strInitEmailTxt);
		$("#Input_MlstEmail").addClass("Dft");
		$("#Input_MlstEmail").removeClass("Nrm");
		$('#Div_Mlst div.Nrm').removeClass("Hvr");
		$('#Div_Mlst div.Nrm').hide();
		$('#Div_Mlst div.Uns').show();
		$('#Div_Mlst div.Wtg').hide();
		$("#Div_MlstIni").fadeIn(1000);
	});
});
