// JavaScript Document
$(document).ready(function(){
					
						  
						   var subject=$("#txtContributionSubject");
						   subject.blur(validateSubject);
						   subject.keyup(validateSubject);
						   //contribution.blur(validateContribution);
						   //contribution.keyup(validateContribution);
///////////////////////////////////////////Validation////////////////////////////////////////////						   
						   function validateControbutionLength()
	{
		
		
		var filter = /^\s*(\w\s*){1,5000}\s*$/;
		//if it's valid email
		if(filter.test(contribution.val())){			
			$("#contributionError").hide();
			 $("#contributionError").text('');
			return true;
		}
		//if it's NOT valid
		else{
			
			$("#contributionError").show();
			 $("#contributionError").text('You can enter only 5000 characters for contribution.');
			
			return false;
		}
		//alert(ask.val().length);
		return true;
	}
	function validateContribution()
	{
		
		if(getContributionTextValue().length>0&&getContributionTextValue().length<=5000){			
			$("#contributionError").hide();
			 $("#contributionError").text('');
			return true;
		}
		else if(getContributionTextValue().length>5000)
		{
			$("#contributionError").show();
			 $("#contributionError").text('You cannot enter more then 5000 characters for dream.');
			
			return false;
		}
		//if it's NOT valid
		else{
			
			$("#contributionError").show();
			 $("#contributionError").text('Please enter your contribution.');
			
			return false;
		}
		//alert(ask.val().length);
		
	}
	function validateSubject()
	{
		
		if(subject.val().length>0){			
			$("#subjtecError").hide();
			 $("#subjtecError").text('');
			return true;
		}
		//if it's NOT valid
		else{
			
			$("#subjtecError").show();
			 $("#subjtecError").text('Please enter contribution subject.');
			
			return false;
		}
		//alert(ask.val().length);
		
	}
///////////////////////////////////////////////Object Events////////////////////////////////////////////////////////////
$("#btnSaveContribution").click(function(){		
										 if(validateContribution()&validateSubject())
										 {
										 saveDream();
										 }
										 });
$("#btnCancelContribution").click(function(){
										  
										  location.href='../index.php';
	                                      event.returnValue=false;
										 
										 });
$("#btnUpdateContribution").click(function(){
										   if(validateContribution()&validateSubject())
										 {
					 updateDream();
										 }
					 });
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
						   });
function saveDream()
{
$.ajax({
   type: "POST",
   url: "../BusinessLogic/dreams.php",
   data: "subject="+$("#txtContributionSubject").val()+"&description="+getContributionTextValue()+"&option=saveContribution",
   async: false,
   success: function(msg){
	   response=msg;
         
                         }
	  });
		  if(response=='1')
		 {
			location.href='../Client/myContribution.php';
	         event.returnValue=false;
			 return false;
         }
          else if(response=='0')
         {
			 $("#contributionError").show();
			$("#contributionError").text("Contribution has not been added due to some error.");
	        event.returnValue=false;
			 return false;
          }
}

function updateDream()
{
	var dreamId=getDreamId('dreamId');
	
	if(dreamId!="")
	{
	$.ajax({
   type: "POST",
   url: "../BusinessLogic/dreams.php",
   data: "subject="+$("#txtContributionSubject").val()+"&description="+getContributionTextValue()+"&dreamId="+dreamId+"&option=updateContribution",
   async: false,
   success: function(msg){
	   response=msg;
         
                         }
	  });
		  if(response=='1')
		 {
			location.href='../Client/myContribution.php';
	         event.returnValue=false;
			 return false;
         }
          else if(response=='0')
         {
			 $("#contributionError").show();
			$("#contributionError").text("Contribution has not been added due to some error.");
	        event.returnValue=false;
			 return false;
          }
	}
}

function deleteDream(dreamId)
{
if(confirm('Do you want to delet this dream.'))
{
  $.ajax({
   type: "POST",
   url: "../BusinessLogic/dreams.php",
   data: "dreamId="+dreamId+"&option=deleteContribution",
   async: false,
   success: function(msg){
	   response=msg;
         
                         }
	  });
		  if(response=='1')
		 {
			$("#tbl_"+dreamId).hide();
			location.reload(true);
			 event.returnValue=false;
			 return false;
         }
          else if(response=='0')
         {
			 alert('Your dream has not been deleted due to some error');
          }
}
}
function getContributionTextValue()
{
	var oEditorShort = FCKeditorAPI.GetInstance('txtContribution');
	var src=oEditorShort.GetHTML( true );
	src=src.replace(/&nbsp;/g,'')
    return src;
	
 }
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 limitChars(limit)
  {

  var textlength = $("#txtContributionDescription").val().length;
 // alert('fdd');
  if(textlength > limit)
  {
  $("#characterLimitDiv").html('You cannot write more then '+limit+' characters!');
  $("#txtContributionDescription").val($("#txtContributionDescription").val().substr(0,limit));
  return false;
  }
  else
  {
 
  $("#characterLimitDiv").html('You have '+ (limit - textlength) +' characters left.');
  return true;
 }
 }
