// Script for displaying and running subscribe to mailing list link at page header
$("a#subscribeMailingList").click(displaySubscribeBox);
var subscribeBoxActive = false;

function displaySubscribeBox(evt) { 
	if(subscribeNotCompatible) {
		location.href = "mailto:leonbaird@mac.com?subject=Subscribe%20to%20mailing%20list&body=Please%20add%20me%20to%20your%20mailing%20list.";
		return;
	}
	if(subscribeBoxActive) return;
	subscribeBoxActive = true;
	
	var htmlContent 
		= "<h2 style='color:#fff; font-size:1.8em; border-bottom:1px #fff solid; padding-bottom:5px;'>Subscribe to mailing list</h2>"
		+ "<p>Please complete your email details and name</p>"
		+ "<form name='subscribe'>"
		+ "<p><label>Name:<br /><input name='name' type='text' style='width:290px' /></label></p>"
		+ "<p><label>Email:<br /><input name='email' type='text' style='width:290px' /></label></p>"
		+ "<p><label>Confirm Email:<br /><input id='email' type='text' style='width:290px' /></label></p>"
		+ "<p style='margin-top:10px; text-align:right;'>"
		+ "<input type='button' onClick='cancelButton(); return false;' value='Cancel' class='subButton' />"
		+ "&nbsp;&nbsp;"
		+ "<input type='submit' value='Subscribe' class='subButton' />"
		+ "</p>"
		+ "</form>";
	var mouseX = evt.pageX-150;
	$("<div id='subscribeEmail' class='roundedCorners dropShadow' />")
		.html(htmlContent)
		.css({
			 position:"absolute",
			 top:"40px",
			 left:mouseX+"px",
			 border:"1px #444 solid",
			 backgroundColor:"#000",
			 opacity:"0.8",
			 padding:"10px",
			 width:"300px",
			 textAlign:"left",
			 color:"#fff",
			 fontSize:"1.4em"
			 })
		.appendTo(document.body)
		.hide()
		.fadeIn(500);
	$(window).scroll(function() {
		closeSubscribeBox();
		$(window).unbind("scroll");
  	});
	
	$(document.subscribe).submit(checkEmail);
};

function checkEmail() {
	if($("input[name='name']").val() == "") {alert("Please enter your name."); return false; }
	var em1 = $("input[name='email']").val();
	var em2 = $("#email").val();
	if(em1=="") {alert("Please enter an email address!"); return false; }
	if(em1 == em2) {
		if(validateEmail(em1)) { sendFormData(); } 
		else { alert("Please enter a valid email address"); return false;}
	} else {
		alert("Please check email addresses match!");	
	}
	
	return false;
}

function cancelButton() {
	closeSubscribeBox();
	return false;
}

function sendFormData() {
	var userName = $("input[name='name']").val();
	var email = $("input[name='email']").val();
	var data = "name="+encodeURI(userName)+"&email="+encodeURI(email);
	var url = "../../php/subscribe.php";
	$.post(url, data, checkSuccess, "text");
	$("<p />")
		.html("Sending data to server.<span style='text-decoration:blink;'>..</span>")
		.css({
			color:"#f00",
			textAlign:"center"
		})
		.appendTo($("#subscribeEmail"));
	$(document.subscribe).unbind("submit", checkEmail);
	$(document.subscribe).submit(function(){alert("Sending data, if no response, cancel and try again."); return false;});
}

function checkSuccess(data, textStatus) {
	if(textStatus == "success" && data == 1) {
		showSuccess();
	} else {
		showError();	
	}
}

function showSuccess() {
	var msg = "You have been sucessfully added to our mailing list. "
	        + "Thank you for subscribing. You will receive information from our site in due "
			+ "course when there is next a mailing.";

	updateMessageControls(msg)
}

function showError() {
	var msg = "Sorry, there has been an error. Please try again later. "
			+ "If you continue to experience this problem, please email me and let me know.";
			
	updateMessageControls(msg);
}

function updateMessageControls(msg) {
	$("#subscribeEmail > p:last").remove();
	$("#subscribeEmail input[class='subButton']").remove();
	$("<p />")
		.html(msg)
		.css({color:"#f00", padding:"0px 0px 5px 0px", marginBottom:"20px"})
		.appendTo("#subscribeEmail");	
	$("<p />")
		.html("<input type='button' value='Close' id='submitCloseButton' />")
		.css({textAlign:"right"})
		.appendTo("#subscribeEmail")
		.click(closeSubscribeBox);
}

function closeSubscribeBox() {
	$("#subscribeEmail").fadeOut(500, function() {$("#subscribeEmail").remove(); });
	subscribeBoxActive = false;
}
