$(function() {
	$("#cancel").click(function () { $("#send_page").animate({opacity: 0}, function() { $("#send_page").css({height:0, width:0}); }); });
	
	$("a.email_notes").click(function() {
		var mWidth = 280;
		
		if (document.notes_form.notes.value.trim() == '' || document.notes_form.notes.value.trim() == 'You can use this area to take notes.') {
			alert("You haven't entered any notes yet.");
			return false;
		}
		
		$("#send_form").css('display', 'inherit');
		$("#send_response").css("display", "none");
		$("#send_page").width(mWidth).height("auto");
		var mHeight = $("#send_page").height();

		//Get window center, accounting for scrolling
		var winHCenter = $(window).scrollLeft() + ($(window).width() / 2);
		var winVCenter = $(window).scrollTop() + ($(window).height() / 2);

		var startCSS = {
			left: (winHCenter - (mWidth / 2)) + "px",
			top: (winVCenter - (mHeight / 2)) + "px",
			width: mWidth,
			height: mHeight,
			opacity: 0
		};
		
		//Box ending point
		var endCSS = {
			left: (winHCenter - (mWidth / 2)) + "px",
			top: (winVCenter - (mHeight / 2)) + "px",
			width: mWidth,
			height: mHeight,
			opacity: 1.0
		}
		$("#send_page").css(startCSS).animate(endCSS, 300);
		$("#name").focus();
		return false;
	});
	
	$("#send_page_form").submit(function() {
		var name = $("#name").val();
		if (name == '') {
			alert("Please enter a name.");
			$("#name").focus();
			return false;
		}
		var email = $("#email").val();
		if (validate_email(email) == false) {
			$("#email").focus();
			return false;
		}

		$("#send_form").css('display', 'none');
		$("#send_response").css("display", "inherit").html('<p align="center" style="margin-top:10px;">Loading, please wait...</p>');	
		
		$.get("/messages/emailnotes/?name=" + name + "&email=" + email + "&subject=" + 'Your NHC Sermon Notes' + "&notes=" + escape(document.notes_form.notes.value) + "", function(data) {
			$("#send_response").html(data);
			$("#send_page").height("auto");
			
			setTimeout(function(){
			  $("#send_page").animate({opacity: 0}, function() { $("#send_page").css({height:0, width:0}); })
			}, 3000);
		});
		return false;
	});
});

function validate_email(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail Address")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail Address")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail Address")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail Address")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail Address")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail Address")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail Address")
		return false
	 }

	 return true					
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
