$(document).ready(function() {	
	
	//Put in the DIV id you want to display
	if(showSignup){
		launchWindow('#signup');
	}
	
	//if close button is clicked
	$('.window #close').click(function () {
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
	
});

function launchWindow(id) {
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		//$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.6);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height());
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 	

}

//if close button is clicked
//$('#signup .close').click(function (e) {
function closeWindow(){
	//Cancel the link behavior
	//e.preventDefault();
	$('#mask, .window').hide();
};    
 
//if mask is clicked
$('#mask').click(function () {
	$(this).hide();
	$('.window').hide();
});

function isEmailValid(email){
	var EmailOk = true;
	var AtSym = email.indexOf('@');
	var Period = email.lastIndexOf('.');
	var Space = email.indexOf(' ');
	var Length = email.length - 1;
	
	if ((AtSym < 1) || (Period <= AtSym+1) || (Period == Length ) || (Space != -1)){
		EmailOk = false;
	}
	return EmailOk;
}

function sendForm(){
	if(isEmailValid(jQuery("#Email").val())){		
		jQuery("#imessageOK, #imessageERROR").hide();
		jQuery(".required").removeClass("required");
		if(jQuery("#Name").val()==""){jQuery("#Name").addClass("required");window.scroll(0,0);return false;}
		if(jQuery("#Email").val()==""){jQuery("#Email").addClass("required");window.scroll(0,0);return false;}
		jQuery("#Signmeup").val("Please Wait...");		
		var AddDetails="php/mailer.php"; 
		AddDetails+="?Name="+encodeURIComponent(jQuery("#Name").val());
		AddDetails+="&Email="+encodeURIComponent(jQuery("#Email").val());
		AddDetails+="&Crop="+encodeURIComponent(jQuery("input[name=Crop]:checked").val());
		$.get(decodeURIComponent(AddDetails), function(data){
			alert(data);
			//document.theForm.reset();
			jQuery("#Signmeup").val("Sign me up!");	
			closeWindow();
		});
	}else{
		alert("Please enter a valid email address");
	}
} 
