﻿function addComment(index)
{  // index is the area where you pressed

    var commentTextArea = document.getElementById(index);
	if(commentTextArea == null || commentTextArea.value == ''){
		return;
	}
	
	// check against html
	
	if ( (commentTextArea.value.indexOf("<") > -1) || (commentTextArea.value.indexOf(">") > -1) )
	{
	    return;
	}
	
	if ( (commentTextArea.value.indexOf("&lt;") > -1) || (commentTextArea.value.indexOf("&gt;") > -1) )
	{
	    return;
	}
	
	var ReplaceComment = document.getElementById('ReplaceComments');
	
	var url = appPathScript + "/Pages/Application/Comments/CommentService.aspx";
	var params = "command=2";
	
	params = params + "&type=" + CommentsUC.Type;
	params = params + "&source=" + CommentsUC.Source;		
	params = params + "&text=" + commentTextArea.value;
	params = params + "&sid=" + Math.random();
	params +="&IsHiddenDeleteOption=" + CommentsUC.IsHiddenDeleteOption;
	params +="&ShowBorder=" + CommentsUC.ShowBorder;
	params +="&CommentsPerPage=" + CommentsUC.CommentsPerPage;
	
	if (CommentsUC.IsLongCommentText != '0')
	{
	    params +="&isLong=1";
	}
	
	updateCommentsNumberBoomHeader(true);
	
	AJAX.replaceContent(ReplaceComment,url,params,false,false);
	
	commentTextArea.value = "";
}

// Add Replay -> after you press add in the enw replay window
// id of text box is the newly created
// id is NewReplay_CommentId
function addReplay(idOfTextBox)
{   


    try 
    {
        var commentTextArea = document.getElementById(idOfTextBox);      
        
	    if(commentTextArea == null || commentTextArea.value == '')
	    {
		    return;
	    }
	    if ( (commentTextArea.value.indexOf("<") > -1) || (commentTextArea.value.indexOf(">") > -1) )
	    {
	        return;
	    }
    	
	    if ( (commentTextArea.value.indexOf("&lt;") > -1) || (commentTextArea.value.indexOf("&gt;") > -1) )
	    {
	        return;
	    }
	}
	catch(e)
	{
	    return;
	}
	
	var commentId = CommentsUC.commentIdToDelete;
	var ReplaceComment = document.getElementById('ReplaceComments');
	
	var url = appPathScript + "/Pages/Application/Comments/CommentService.aspx";
	var params = "command=3";
	params = params + "&type=" + CommentsUC.Type;
	params = params + "&source=" + CommentsUC.Source;		
	params = params + "&text=" + commentTextArea.value;
	params = params + "&commentId=" + commentId;
	params = params + "&sid=" + Math.random();
	params +="&IsHiddenDeleteOption=" + CommentsUC.IsHiddenDeleteOption;
	params +="&ShowBorder=" + CommentsUC.ShowBorder;
	params +="&CommentsPerPage=" + CommentsUC.CommentsPerPage;
	if (CommentsUC.IsLongCommentText != '0')
	{
	    params +="&isLong=1";
	}
	updateCommentsNumberBoomHeader(true);
	
	AJAX.replaceContent(ReplaceComment,url,params,false,false);	
	commentTextArea.value = "";
	
}
function NextPage(pageId)
{
    var ReplaceComment = document.getElementById('ReplaceComments');
	
	var url = appPathScript + "/Pages/Application/Comments/CommentService.aspx";
	var params = "command=4";
	params = params + "&type=" + CommentsUC.Type;
	params = params + "&source=" + CommentsUC.Source;		
	params = params + "&sid=" + Math.random();
	params = params + "&page=" + pageId;
	params +="&IsHiddenDeleteOption=" + CommentsUC.IsHiddenDeleteOption;
	params +="&ShowBorder=" + CommentsUC.ShowBorder;
	params +="&CommentsPerPage=" + CommentsUC.CommentsPerPage;
	if (CommentsUC.IsLongCommentText != '0')
	{
	    params +="&isLong=1";
	}
	AJAX.replaceContent(ReplaceComment,url,params,false,false);
}
function ActualDeleteComment()
{
    var commentId = CommentsUC.commentIdToDelete;

	var url = appPathScript + "/Pages/Application/Comments/CommentService.aspx";
	var params = "command=0";
	params = params + "&commentId=" + commentId;
	params = params + "&type=" + CommentsUC.Type;
	params = params + "&source=" + CommentsUC.Source;
	params = params + "&sid=" + Math.random();
	params +="&ShowBorder=" + CommentsUC.ShowBorder;
	params +="&CommentsPerPage=" + CommentsUC.CommentsPerPage;
	if (CommentsUC.IsLongCommentText != '0')
	{
	    params +="&isLong=1";
	}
	var ReplaceComment = document.getElementById('ReplaceComments');
	
	updateCommentsNumberBoomHeader(false);
	
	AJAX.replaceContent(ReplaceComment,url,params,false,false);
}

function ActualDeleteReplay()
{
    var ReplayId = CommentsUC.commentIdToDelete;

	var url = appPathScript + "/Pages/Application/Comments/CommentService.aspx";
	var params = "command=1";
	params = params + "&commentId=" + ReplayId;
	params = params + "&type=" + CommentsUC.Type;
	params = params + "&source=" + CommentsUC.Source;
	params = params + "&sid=" + Math.random();
	params +="&ShowBorder=" + CommentsUC.ShowBorder;
	params +="&CommentsPerPage=" + CommentsUC.CommentsPerPage;
	if (CommentsUC.IsLongCommentText != '0')
	{
	    params +="&isLong=1";
	}
	var ReplaceComment = document.getElementById('ReplaceComments');
	
	updateCommentsNumberBoomHeader(false);
	
	AJAX.replaceContent(ReplaceComment,url,params,true,true);
}

//Just open pop up
function deleteComment(commentId)
{
	CommentsUC.commentIdToDelete = commentId;	
	showPopup('popup_confirm_deletion_comment');	
}

// just open pop up
function deleteReplay(replayid)
{
    CommentsUC.commentIdToDelete = replayid;
	showPopup('popup_confirm_deletion_replay');
}

function openReplyTextArea(commentId)
{    
    CommentsUC.commentIdToDelete = commentId;
    
	var divId = "replyTextboxDiv_" + commentId;
	
	divToDisplay = document.getElementById(divId);
	
	if(divToDisplay === null){ 
		//id's are installed from ajax, hance they dont have the prefix
		//id of the current control
		divId = "CommentsUserControl_replyTextboxDiv_" + commentId;
		divToDisplay = document.getElementById(divId);
	}
	
	
	if(divToDisplay != null){
		if(divToDisplay.style.display == 'none'){
			divToDisplay.style.display = '';
			divToDisplay.appendChild(buildTextArea(commentId));			
		}
		else{
			divToDisplay.innerHTML = "";
			divToDisplay.style.display = 'none';
		}
	}
}

function buildTextArea(commentId)
{
    var classLong = '';
    if (CommentsUC.IsLongCommentText != '0')
	{
	    classLong = "maximum_width_Long_with";
	}
	
 var htmlstr = "<div><table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">"
				+"<tr>"
				+"<td style=\"background-color:#fafafa;border:1px solid #efefef;padding:5px;\" align=\"left\">"
				+"<div>"
	            +"<textarea class=\"comments-replyTextBox\" id=\"replyTextArea_" + commentId + "\"></textarea>"
				+"</div>"
				+"</td>"
                +"</tr>"
                +"</table></div>"
                +"<div style=\"padding-top:10px;text-align:right;\">"
                +"\<div style=\"float:left;\">"
                +"<div style=\"cursor:pointer\" onclick=\"addReplay('replyTextArea_" + commentId + "');\">"
                +"<img style=\"float:left\" src=\"" + appPath + "/media/images/comments/comment.png\"/>&nbsp;"
                +"<span class=\"addComment\" onclick=\"addReplay('replyTextArea_" + commentId  + "');\">Post Reply</span>"
                +"</div></div>"
                            

	var span = document.createElement('span');	
	span.innerHTML = htmlstr; 
	return span;
}

function hidePopupsComments() {
    hidePopup('popup');
}	    
function revertCloseBtn(divObj) {
		divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('_over.gif', '.gif') : window.getComputedStyle(divObj, null).backgroundImage.replace('_over.gif', '.gif');
	}	
	

/*************************************************/
/*  set number of comment to header */
function updateCommentsNumberBoomHeader(IsPlus)
{
    var numberComments;
    var objSpanCommentsNumber =document.getElementById('Booms_BoomComments_spanCommentsCount');
    if (!isNullOrUndefined(objSpanCommentsNumber))
    {
        numberComments = parseInt(objSpanCommentsNumber.innerHTML);
        if (IsPlus)
        {
            objSpanCommentsNumber.innerHTML = numberComments +1;
        }
        else
        {
			objSpanCommentsNumber.innerHTML = numberComments - 1; 
        } 
    }
}

/*************************************************/	

function callHandleTabsComment(divObj, tabName, trId, isToDoCheckLeftBorderFunc)
{
	handleTabsComment(divObj, tabName, document.getElementById(trId).childNodes[0].id, isToDoCheckLeftBorderFunc);
}

//like function handleTabs2(divObj, tabName) {     
var lastDivObj = -1;
var lastTabObj = -1;
function handleTabsComment(divObj, tabName, tdId, isToDoCheckLeftBorderFunc) {
    var tabObj = document.getElementById(tabName);    
	if (tabObj.attributes["status"].value == 'open') {	    
	    if (!isNullOrUndefined(tdId) && !isNullOrUndefined(isToDoCheckLeftBorderFunc) && (isToDoCheckLeftBorderFunc=='True'))
        {
             checkLeftBorder(tdId,true);
        }
	    
	    new Effect.BlindUp(
	        tabObj,
	        {
	            duration: 0.5,
	            afterFinish : function(){
	                divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('minus', 'plus') : window.getComputedStyle(divObj, null).backgroundImage.replace('minus', 'plus');;
	                tabObj.attributes["status"].value = 'close';
	            }
	        }
	    );
	} else {
	    if (lastDivObj != -1 && lastDivObj != divObj && lastTabObj.attributes["status"].value == 'open') {
                        new Effect.BlindDown(
                            tabObj,
                            {   
                                duration: 0.5,
                                afterFinish: function(){
                                 //   new Effect.ScrollTo(tabObj, {offset:-30});
                                    divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('plus', 'minus') : window.getComputedStyle(divObj, null).backgroundImage.replace('plus', 'minus');
                                    tabObj.attributes["status"].value = 'open';
                                    lastDivObj = divObj;
                                    lastTabObj = tabObj;
                                    if (!isNullOrUndefined(tdId) && !isNullOrUndefined(isToDoCheckLeftBorderFunc) && (isToDoCheckLeftBorderFunc=='True'))
                                    {
                                         checkLeftBorder(tdId,false);
                                    }	
                                }
                            }
                         );
        } else{
            
            new Effect.BlindDown(
	          tabObj,
	            {   
	                duration: 0.5,
	                afterFinish: function(){
	                    divObj.style.backgroundImage = (document.all) ? divObj.currentStyle.backgroundImage.replace('plus', 'minus') : window.getComputedStyle(divObj, null).backgroundImage.replace('plus', 'minus');
	                    tabObj.attributes["status"].value = 'open';
	                    lastDivObj = divObj;
	                    lastTabObj = tabObj;
	                    if (!isNullOrUndefined(tdId) && !isNullOrUndefined(isToDoCheckLeftBorderFunc) && (isToDoCheckLeftBorderFunc=='True'))
                        {
                             checkLeftBorder(tdId,false);
                        }	                    	  	  
	                }
	            }
	         );	
        }
	}
}	