$(document).ready(function () {
var inputUser = $("#name");
var inputEmail = $("#mail");
var inputMessage = $("#message");
var inputisRegis = $("#onForums");
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.indexOf('/') + 1);



   function editPrompt(comment, timestamp){
		$(".EditPrompt").show("slow");
		$(".EditPrompt").focus();
		$(".EditPrompt textarea").val(comment);
		$(".EditOK").val(timestamp);
   }
   
   function updateComments(){
     $.post("http://www.eyeofthevortexonline.com/baseFiles/postComment.php",{
       postAction: "update",
       ajaxURL: sPage
       }, function(data) {
	$("#responce").html(data);
     });
   }

  function deleteComment(timestamp,page){
    $.post("http://www.eyeofthevortexonline.com/baseFiles/postComment.php",{
      postAction: "delete",
      ajaxURL: page,
      ajaxTime: timestamp
    });
    updateComments();
  }

  function editComment(timestamp,newComment,page){
	$.post("http://www.eyeofthevortexonline.com/baseFiles/postComment.php",{
      postAction: "edit",
      ajaxURL: page,
      ajaxTime: timestamp,
      ajaxComt: newComment
    }, function () {
		updateComments();
	});
  }
  
  function checkForm(){
	if(inputUser.attr("value") && inputMessage.attr("value") && inputEmail.attr("value")){
	 emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
	 if( !emailpat.test( inputEmail.attr("value") ) ) {
	    alert( "Please enter a valid email address" );
	    return false;
	 }
	 else
	    return true;
	}
	else{
	  alert("Empty fields detected - please fill out all fields.");
	  return false;
	}
  }

  $("#send").click(function () {
     if(checkForm() == true){
      if(confirm("Please note by submitting your comment you agree to the following.\n - the logging of your IP address and E-mail for contacting purposes which will not be given out by any means \n - that you are only representing yourself and no other orginization \n - that the EOV is not responcible for your comments and that they may be removed or edited to confirm with our policies \n\n if you do not agree to this - the comment you have entered will not be sumbitted."))
      {
		var time =  new Date();
		//alert(sPage+" "+inputUser.attr("value")+" "+inputEmail.attr("value")+" "+time.getTime()+" "+inputisRegis.attr("value"));
		$.post("http://www.eyeofthevortexonline.com/baseFiles/postComment.php", {
			postAction: "insert",
			ajaxURL: sPage,
			ajaxName: inputUser.attr("value"),
			ajaxMail: inputEmail.attr("value"),
			ajaxComt: inputMessage.attr("value"),
			ajaxTime: time.getTime(),
			ajaxRegi: inputisRegis.attr("value"),
		  }, function (data) {
		  $("#responce").html(data);
		  updateComments();
		});
      }
     }
  });
  
  $(".DeleteButton").live("click", function () {
	if(confirm("Are you sure you want to delete this comment?")){
		var timestamp = $(this).attr("value");
		deleteComment(timestamp,sPage);
	}
  });
  
  $(".EditButton").live("click", function () {
	var timestamp = $(this).attr("value");
	$.post("http://www.eyeofthevortexonline.com/baseFiles/postComment.php", {
		postAction: "getComment",
		ajaxTime: timestamp,
		ajaxURL: sPage
	}, function (data) {
		editPrompt(data, timestamp);
	});
	updateComments();
  });
  
  $(".ReportButton").live("click", function () {
	var Reason = prompt("Why are you reporting this Post?","Spam");
	var timestamp = $(this).attr("value");
	$.post("http://www.eyeofthevortexonline.com/baseFiles/postComment.php", {
		postAction: "report",
		reason: Reason,
		ajaxTime: timestamp,
		ajaxURL: sPage
	});
  });
  
  $(".EditOK").click(function () {
	var newComment = $(".EditPrompt textarea").attr("value");
	var timestamp = $(".EditOK").attr("value");
	$(".EditPrompt").hide("slow");
	editComment(timestamp,newComment,sPage);
  });
  
  $(".EditCancel").click(function () {
	$(".EditPrompt").hide("fast");
  });
  
  updateComments();
});