$(document).ready(function()
{
	/* Fill in the 'characters remaining' text here, so it doesn't show if JS is disabled */
	limitChars('reminder', 120, 'counter_characters');
	$("#txt_characters").text('characters remaining');
	
	/* Show the 'Check' button for the same reason as above */
	$(".link_checkbtn").show();
	
	/* Checks to see if the user is following us onblur*/
	$("input#twitter_username").change(function()
	{
		if($("input#twitter_username").val() != "username" || $('.link_checkbtn').is(':hidden')){
			check_follow_status();
		}
	});	

	$('#reminder').keyup(function(){
		limitChars('reminder', 120, 'counter_characters');
	});

	$('#reminder').change(function(){
		$(this).keyup();
	});	
	
	/* Make anything pasted into the input box trigger the key up event to count the new length */
	$('#reminder').live
	(
		'input paste',
		function(e)
		{
			$('#reminder').keyup();
		}
	);
	
	/* Get the time zone from the user's browser */
	$('#tzoffset').val(new Date().getTimezoneOffset()/60);
	
	/* When the submit button is pressed */
	$("#tweetSubmit").click(function()
	{
		
		var check_error = false;

		if(limitChars('reminder', 120, 'counter_characters') === false || $('#reminder').val() === "What would you like us to remind you of...")
		{
			html = "You need to enter a reminder of 120 characters and under to continue";
			$("#reminder").addClass('input_warning');
			check_error = true;
		}
		else if($('input#valid_date').val() !== "true")
		{
			html = "You need to select a valid date in the future as Tweet Reminder has a strict no time travel policy.";
			$("#when_day").addClass('input_warning');
			$("#when_month").addClass('input_warning');
			$("#when_year").addClass('input_warning');
			check_error = true;
		}

		if(check_error === true){

			$("#msg_response").removeClass();
			$("#msg_response").addClass('msg_error');
			$("#msg_response").html(html);
			$("#msg_response").slideDown('slow');
			
		}else{
			
			$.ajax({
					type: "POST",
					url: "/includes/post.php",
					data: "reminder=" + $("#reminder").val() + 
					"&when_day=" + $("#when_day").val() + 
					"&when_month=" + $("#when_month").val() +
					"&when_year=" + $("#when_year").val() +
					"&twitter_username=" + $("#twitter_username").val(), 
					success: function(html)
					{
						$(".link_checkbtn").show();
						$("#check_container").css({'background' : ''});
						$("#msg_response").removeClass();
						$("#msg_response").addClass('msg_success');
						$("#reminder").val("What would you like us to remind you of...");
						$("#twitter_username").val("username");
						$("#twitter_username").removeClass('input_box_active')
						limitChars('reminder', 120, 'counter_characters');
						$("#msg_response").slideDown('slow');
						$("#msg_response").html(html);
					}
			});
		}
		return false;
		
	});
});
		
		
function check_follow_status()
{
	/* Resets the colour back */
	$(".link_checkbtn").hide();
	$("#twitter_username").removeClass('input_warning');
	
	$("div#msg_response").hide();
	
	$.ajax(
	{
		type: "GET",
		url: "/includes/check_follow.php",
		data: 	"username=" + $("input#twitter_username").val(),
		success: 
		function(html)
		{
			$("div#msg_response").html(html);
		}
	});
	
	return false;
	
}

function check_tweet_date()
{
	$.ajax(
	{
		type: "GET",
		url: "/includes/check_date.php",
		data: 	"when_day=" + $("#when_day").val() + 
				"&when_month=" + $("#when_month").val() +
				"&when_year=" + $("#when_year").val(),
		success: 
		function(html)
		{
			$("div#msg_response").html(html);
		}
	});	
}

function limitChars(textid, limit, infodiv)
{

	var text = $('#'+textid).val();
	text = text.replace("What would you like us to remind you of...","")
	var textlength = text.length;

	if(textlength > limit)
	{
		$('#' + infodiv).html(limit - textlength);
		/*$('#'+textid).val(text.substr(0,limit));*/
		
		$("#reminder").addClass('input_warning');
		$("#reminder_count").addClass('reminder_warning');
		
		return false;
		
	}else{
		
		$("#reminder").removeClass('input_warning');
		$("#reminder_count").removeClass('reminder_warning');
	
		$('#' + infodiv).html(limit - textlength);

		return true;
	}
}

