$(function() {
  $('.error').hide();
  $('input.text-input').css({backgroundColor:"#FFFFFF"});
  $('input.text-input').focus(function(){
    $(this).css({backgroundColor:"#FFDDAA"});
  });
  $('input.text-input').blur(function(){
    $(this).css({backgroundColor:"#FFFFFF"});
  });

  $(".button#Submit").click(function() {
		// validate and process form
		// first hide any error messages
$('.error').hide();

	// validate comments
	var comments = $("textarea#comments").val();
	if (comments == "") {
      $("label#comments_error").show();
      $("textarea#comments_error").focus();
      return false;
    }

	// validate Email
	var validemail = false
	var email = $("input#email").val();
	var filter = "/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/";
	
	var aindex = email.indexOf("@");
	var pindex = email.lastIndexOf(".");
	
	if (aindex>0){
		if ((pindex > aindex+1) && (email.length > pindex+1)){
			validemail = true;
		}
	}

	if (!validemail) {
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}

	
		var post_id = $("input#post_id").val();
		var nickname = $("input#nickname").val();
	


		var dataString = 'email=' + email + '&comments=' + comments + '&post_id=' + post_id + '&nickname=' + nickname;
		//alert (dataString);return false;
		
		$.ajax({
			type: "POST",
			url: "comment_post.php",
			data: dataString,			
			
			success: function(msg) {
				
				$('#message').html(msg)
				.hide()
				.fadeIn(1500);					
				document.getElementById("comments").value = "";
				document.getElementById("email").value = "";	
				document.getElementById("nickname").value = "";
				//location.reload();
			}


	     });

    return false;
	});
});


