
function saveComments( )
{
	if ($F('txtComments') == "")
	{
		alert("Please enter your Comments.");
		
		$('txtComments').focus( );
		
		return;
	}
	
	$('Processing').show( );
	$('BtnPost').disable( );
	
	var sUrl    = "ajax/today/save-comments.php"; 
	var sParams = $('frmComments').serialize( );
	
	new Ajax.Request(sUrl, { method:'post', parameters:sParams, onFailure:_showError, onSuccess:_saveComments });
}

function _saveComments(sResponse)
{
	if (sResponse.status == 200 && sResponse.statusText == "OK")
	{
		var sParams = sResponse.responseText.split('|-|');
		
		if (sParams[0] == "OK")
		{
			$('frmComments').reset( );

			$('Msg').innerHTML = sParams[1];			
			$('Msg').show( );
			
			setTimeout( 
			            function( )
			            { 
			               new Effect.SlideUp("Msg");
			               
			               setTimeout(
			               		   function ( )
			               		   {
							var objList = $$(".storyComments");

							if (objList.length == 0)
							{
								$('NoRecord').hide( );
								
								if (document.getElementById('NoComments'))
									$('NoComments').hide( );
							}

			               		   	new Insertion.Top('Comments', sParams[2]);
			               		   	new Effect.ScrollTo('Comments');
			               		   },
			               		   
			               		   2000
			               		 );
			            },
			            
			            2000
			          );
		}
			
		else
			_showError(sParams[1]);
			
		$('Processing').hide( );
		$('BtnPost').enable( );
	}
	
	else
		_showError( );
}

function deleteComments(iCommentId, iPageId, iPageCount, sPage)
{
	if (confirm("Are you SURE you want to DELETE these Story Comments?") == false)
		return;
		
	$('Processing').show( );	

	var sUrl    = "ajax/today/delete-comments.php"; 
	var sParams = ("CommentId=" + iCommentId + "&PageId=" + iPageId + "&PageCount=" + iPageCount + "&Page=" + sPage);
	
	new Ajax.Request(sUrl, { method:'post', parameters:sParams, onFailure:_showError, onSuccess:_deleteComments });
}

function _deleteComments(sResponse)
{
	if (sResponse.status == 200 && sResponse.statusText == "OK")
	{
		var sParams = sResponse.responseText.split('|-|');
		
		if (sParams[0] == "OK")
		{
			var iCommentId = sParams[1];
			var iPageId    = eval(sParams[2]);
			var iPageCount = eval(sParams[3]);
			var sPage      = sParams[4];
			var iStoryId   = $F('StoryId');
			
			$("Comments" + iCommentId).innerHTML = sParams[5];

			setTimeout( 
				    function( )
				    { 
				    	new Effect.SlideUp("Comments" + iCommentId); 
				    	
				    	setTimeout( 
				    		    function ( ) 
				    		    { 
				    		    	$('Comments' + iCommentId).remove( );
				    		    	
							var objList = $$(".storyComments");
							
							if (objList.length == 0)
							{
								if (iPageId >= 1 && iPageId < iPageCount)
									document.location = (sPage + "?StoryId=" + iStoryId + "&PageId=" + iPageId);
									
								else if (iPageId > 1 && iPageId == iPageCount)
									document.location = (sPage + "?StoryId=" + iStoryId + "&PageId=" + (iPageId - 1));
									
								else
									new Effect.Appear('NoRecord', { duration: 2.0 });
							}
				    		    },
				    		    
				    		    2000
				    		  );
				    },				    
				    
				    2000
				  );
		}
			
		else
			_showError(sParams[1]);
			
		$('Processing').hide( );
	}
	
	else
		_showError( );
}
