// JavaScript Document
$(document).ready(function(){
					
						  var contributionComment=$("#txtContributionComment");
						 
						  ///////////////////////////////////////////Validation////////////////////////////////////////////						

                          //$("#txtContributionComment").blur(validateComment);
						  //$("#txtContributionComment").keyup(validateComment);
						 
 
	function validateCommentLength()						   
	{
		
		var filter = /^\s*(\w\s*){1,500}\s*$/;
		//if it's valid email
		var commentText=getCommentTextValue();
		if(filter.test(commentText)){			
			//$("#contributionCommentError").hide();
			 //$("#contributionCommentError").text('');
			return true;
		}
		//if it's NOT valid
		else{
			
			//$("#contributionCommentError").show();
			 //$("#contributionCommentError").text('You can enter only 500 characters for comment.');
			alert('You cannot enter more then 500 characters for comment.');
			return false;
		}
		//alert(ask.val().length);
		return true;
	}
	
	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////Object Events////////////////////////////////////////////////////////////
$("#btnSaveComment").click(function(){		
										 if(validateComment())
										 {
											 //alert('fdf');
										saveComment();
										 }
										 });
$("#btnCancelComment").click(function(){
										  
										  location.href='../index.php';
	                                      event.returnValue=false;
										 
										 });
						   });
/////////////////////////////////Private Method////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						 function validateComment()
	{
		
		var commentText=getCommentTextValue();
		if(commentText.length>0&&commentText.length<=5000){	
	
			$("#contributionCommentError").hide();
			 $("#contributionCommentError").text('');
			return true;
		}
		else if(commentText.length>5000)
		{
			$("#contributionCommentError").show();
			 $("#contributionCommentError").text('You cannot enter more then 5000 characters for comment');
			return false;
		}
		//if it's NOT valid
		else{
			
			$("#contributionCommentError").show();
			 $("#contributionCommentError").text('Please enter your comment');
			return false;
		}
		//alert(ask.val().length);
		
	}
						 function saveComment()
{
	var dreamId=getDreamId('dreamId');
$.ajax({
   type: "POST",
   url: "../BusinessLogic/dreamDiscussion.php",
   data: "comment="+getCommentTextValue()+"&dreamId="+dreamId+"&rating=5"+"&option=saveComment",
   async: false,
   success: function(msg){
	
	   response=msg;
         
                         }
	  });
		  if(response=='1')
		 {
			location.href='../Client/allContribution.php';
	         event.returnValue=false;
			 return false;
         }
          else if(response=='0')
         {
			 $("#contributionError").show();
			$("#contributionError").text("Comment has not been added due to some error.");
	        event.returnValue=false;
			 return false;
          }
}


function getDreamId( dreamId )
{
	 dreamId = dreamId.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+dreamId+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function limitCommentChars(limit)
  {

  var textlength = $("#txtContributionComment").val().length;
 // alert('fdd');
  if(textlength > limit)
  {
 
 
  $("#commentLimitDiv").html('You cannot write more then '+limit+' characters!');
  $("#txtContributionComment").val($("#txtContributionComment").val().substr(0,limit));
  return false;
  }
  else
  {
 
  $("#commentLimitDiv").html('You have '+ (limit - textlength) +' characters left.');
  return true;
 }
 }
 function getCommentTextValue()
{
	var oEditorShort = FCKeditorAPI.GetInstance('txtContributionComment');
	var src=oEditorShort.GetHTML( true );
	src=src.replace(/&nbsp;/g,'')
    return src;
	
 }
 function updateComment(commentId)
 {
	
	if(validateComment())
	{
	$.ajax({
   type: "POST",
   url: "../BusinessLogic/dreamDiscussion.php",
   data: "comment="+getCommentTextValue()+"&commentId="+commentId+"&option=updateComment",
   async: false,
   success: function(msg){
	
	   response=msg;
         
                         }
	  });
		  if(response=='1')
		 {
			location.href='../Client/allContribution.php';
	         event.returnValue=false;
			 return false;
         }
          else if(response=='0')
         {
			 $("#contributionError").show();
			$("#contributionError").text("Comment has not been updated due to some error.");
	        event.returnValue=false;
			 return false;
          }
	}
 }
