$(document).ready(function() {
	
	var pathname = window.location.pathname;
	var depth = pathname.split("/").length - 2;
	var pre = "";
	for (i=0; i<depth; i++) {
		var pre = pre+"../";
	}
	
	$('#subscribe_form').submit(function(event){
		
    event.preventDefault();
		
		email = $(this).parent().find('input').val();
		
		if ( (email == "your_email@address.com") || ( email.indexOf("@") == -1) ) {
			$(this).parent().find('#subscribe_success').html('');
			$(this).parent().find('#subscribe_error').html('Please enter a valid email');
		} else {
			email = 'email='+email;
			$.ajax({
				type: "POST",
				url: pre+"php/subscribe_add.php",
				data: email,
				success: function(){
		    	$('#submit_subscribe').parent().find('#subscribe_error').html('');
		    	$('#submit_subscribe').parent().find('#subscribe_success').html('Subscription successful!');
				}
			});
		}
    });
	
	$('#submit_subscribe').click(function() {
		
		email = $(this).parent().find('input').val();
		
		if ( (email == "your_email@address.com") || ( email.indexOf("@") == -1) ) {
			$(this).parent().find('#subscribe_success').html('');
			$(this).parent().find('#subscribe_error').html('Please enter a valid email');
		} else {
			email = 'email='+email;
			$.ajax({
				type: "POST",
				url: pre+"php/subscribe_add.php",
				data: email,
				success: function(){
		    	$('#submit_subscribe').parent().find('#subscribe_error').html('');
		    	$('#submit_subscribe').parent().find('#subscribe_success').html('Subscription successful!');
				}
			});
		}
		
	});
	
});
