/***********************************
 *
 * campaign-monitor.js
 * 
 * Turns mailing list subscription form into an AJAX form.
 * Authored and supplied by CampaignMonitor
 *
 *******************************/
 
//$(document).ready(function(){
$(function(){

	$('#email-confirm').jqm({modal:true,overlay:80});

	/****
	 * css fix for the email form!
	***********/
	if( $.browser.msie ){
	  $('.email input').css('padding', '8px');
	}else if ($.browser.mozilla){
	  $('.email input').css('padding-top', '7px');
	}else{
	  //	  alert('browser is ' + $.browser);
	}

		$("#subForm a.email_submit").click(function() {	

			
			// First, disable the form from submitting
			$('form#subForm').submit(function() { return false; });

			/*
			// so testing does not submit form!
			$('#email-confirm').jqmShow();
			return false;
						*/

			
			// Grab form action
			formAction = $("form#subForm").attr("action");
			
			// Hacking together id for email field
			// Replace the xxxxx below:
			// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
			emailId = "ikiklk";
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				alert("Please enter a valid email address");
				return;
			}
			
			// Serialize form values to be submitted with POST
			var str = $("form#subForm").serialize();
			
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "/front/proxy.php",
				type: "POST",
				data: final,
				success: function(html){
				  /*
					$("#theForm").hide(); // If successfully submitted hides the form
					$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
	 			  */
					$("#ikiklk-ikiklk").attr('value', '');
					//alert('Thanks for subscribing!');
					$('#email-confirm').jqmShow();
				}
			});
		});
	});

	function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
