jQuery(document).ready(function(){
	// Check GET Variables to determine whether or not to show the newsletter form
	var $_GET = {};
	document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
		function decode(s) {
			return decodeURIComponent(s.split("+").join(" "));
		}
		$_GET[decode(arguments[1])] = decode(arguments[2]);
	});
	if($_GET["email_found"] == 'yes'){
		$(window).load(function () {
			centerPopup();  
			loadPopup();  
		});
	}
	if($_GET["subscribed"] == 'yes'){
		$(window).load(function () {
			centerPopup();  
			loadPopup();  
		});
	}
	
	var popupStatus = 0;  	
	
	function loadPopup(){  
		//loads popup only if it is disabled  
		if(popupStatus==0){  
			jQuery("#newsletter_background").css({  
			"opacity": "0.7"  
		});  
		jQuery("#newsletter_background").fadeIn("slow");  
		jQuery("#newsletter_popup").fadeIn("slow");  
		popupStatus = 1;
		jQuery("#newsletter_first_name").focus();
		}  
	}  
	function disablePopup(){  
		//disables popup only if it is enabled  
		if(popupStatus==1){  
			jQuery("#newsletter_background").fadeOut("slow");  
			jQuery("#newsletter_popup").fadeOut("slow");  
			popupStatus = 0;  
		}  
	}
	function centerPopup(){  
		//request data for centering  
		var windowWidth = document.documentElement.clientWidth;  
		var windowHeight = document.documentElement.clientHeight;  
		var popupHeight = jQuery("#newsletter_popup").height();  
		var popupWidth = jQuery("#newsletter_popup").width();  
		//centering  
		jQuery("#newsletter_popup").css({  
			"position": "absolute",  
			"top": windowHeight/2-popupHeight/2,  
			"left": windowWidth/2-popupWidth/2  
		});  
		//only need force for IE6  
		  
		jQuery("#newsletter_background").css({  
			"height": windowHeight  
		});   
	}
	jQuery("#newsletter_button").click(function(){  
		centerPopup();  
		loadPopup();  
	});
	//Click the x event
	jQuery("#newsletter_popup_close").click(function(){  
		disablePopup();  
	});  
	//Click out event
	jQuery("#newsletter_background").click(function(){  
		disablePopup();  
	});
	//Click cancel event  
	jQuery("#newsletter_cancel").click(function(){  
		disablePopup();  
	});
	//Press Escape event!  
	jQuery(document).keypress(function(e){  
		if(e.keyCode==27 && popupStatus==1){  
			disablePopup();  
		}  
	});
	jQuery("#newsletter_cancel").click(function(){  
		disablePopup();  
	});
	jQuery(".newsletter_submit").click(function(){
		var email = jQuery("#ctl00_newsletter_email_address").val();
		if(email.length > 0){
			var email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;           
			if(!email_regex.test(email)){
				alert('Please enter a valid email address');
				return false;
			} else {
				return true;
			}
			
		}
		return false;
	});
	
 });



