﻿// JScript File

// use in aspx page to pass ClientID as parameter to JavaScript function
// $get('<%= TextBox1.ClientID %>')


function PrintCommentForm_New(pagePosition)
{
    var position = pagePosition; //either 'Head' or 'Foot'
    if(position != "Head" && position != "Foot"){
        position = "Head";
    }
    var CommentFormElementID = "newCommentForm" + position;
    //var rcIDElementID = elementPrefix + "rcID";
    //alert('rcIDElementID = ' + rcIDElementID);
    //var pcIDElementID = elementPrefix + "pcID";
    //alert('pcIDElementID = ' + pcIDElementID);
    
    var newCommentFormElement = document.getElementById( CommentFormElementID );
    var rcIDElement = document.getElementById( nc_rcID );
    var pcIDElement = document.getElementById( nc_pcID );
    rcIDElement.value = "0";
    pcIDElement.value = "0";
    
    if(newCommentFormElement.hasChildNodes())
    {
        CloseCommentForm();
    }
    else
    {
        //if another comment form is present on the page, remove it
        CloseCommentForm();
               
        var newCommentContainerElement = document.getElementById( "newCommentContainer" );
        AddClassToElement( newCommentContainerElement, "commentFormOpen" );
        RemoveClassFromElement( newCommentContainerElement, "commentFormClosed" );                  
        newCommentFormElement.innerHTML = GetCommentFormHTML();        
        var commentTextareaElement = document.getElementById( "commentTextarea" );
        commentTextareaElement.focus();
    }
}

function PrintCommentForm_Reply( rootID, parentID )
{
     
    var replyCommentFormElement = document.getElementById( "replyCommentForm_" + parentID );
    var rootIDElement = document.getElementById( nc_rcID );
    var parentIDElement = document.getElementById( nc_pcID );    
    rootIDElement.value = rootID;
    parentIDElement.value = parentID;
    
    
    if( replyCommentFormElement.hasChildNodes() )
    {
        CloseCommentForm();
    }
    else
    {
        //if another comment form is present on the page, remove it
        CloseCommentForm();
        
        var parentCommentElement = document.getElementById( "comment_" + parentID );
        //AddClassToElement( replyButtonElement, "hidden" );
        AddClassToElement( parentCommentElement, "replyingToComment" );
        replyCommentFormElement.innerHTML = GetCommentFormHTML();
        var commentTextareaElement = document.getElementById( "commentTextarea" );
        commentTextareaElement.focus();
    }
}

function GetCommentFormHTML()
{
    commentFormHTML = ""
        + "<div id=\"commentForm\">"
        + "<div id=\"commentTextAreaContainer\">"
        + "<span id=\"commentTextAreaLabel\">What say you?</span><br />"
        + "<textarea id=\"commentTextarea\" rows=\"3\" cols=\"40\" class=\"commentFormTextarea\"></textarea>"        
        + "</div>"
        + "<div class=\"commentFormButtons\">"
        + "<button id=\"postCommentButton\" onclick=\"JavaScript:PostComment(); return false;\">Post Comment</button>"
        + "<button id=\"cancelCommentButton\" onclick=\"JavaScript:CloseCommentForm(); return false;\">Cancel</button>"
        + "</div>"
        + "</div>";
        
    return commentFormHTML;        
}



function PostComment()
{
    var pIDElement = document.getElementById( nc_pID );
    var rcIDElement = document.getElementById( nc_rcID );
    var pcIDElement = document.getElementById( nc_pcID );
    var commentTextareaElement = document.getElementById( "commentTextarea" );
    var comCodeElement = document.getElementById( nc_comCode );
    
    
    var pID = pIDElement.value;
    var rcID = rcIDElement.value;
    var pcID = pcIDElement.value;
    var commentBody = commentTextareaElement.value;
    var comCode = comCodeElement.value;
    
    //check for any non-whitespace character
    var re = /\S/;
    if (commentBody.match(re)) {      
        UserPageService.ProcessComment( pID, rcID, pcID, commentBody, comCode, ProcessCommentCallback );        
    }
    else{
        alert('Please provide a comment.');
        commentTextareaElement.value = "";      
    }        
}

function ProcessCommentCallback( result )
{  
    if(result != ""){
        //update comments innerHTML

        var rcIDElement = document.getElementById( nc_rcID );
        var pcIDElement = document.getElementById( nc_pcID );
        var rcID = rcIDElement.value;
        var pcID = pcIDElement.value;
        if(rcID == "0" && pcID == "0")
        {
            var insertCommentAfter = document.getElementById( 'newCommentContainer' );           
        }
        else
        {
            var parentContainerName = 'commentContainer_' + pcID;            
            var insertCommentAfter = document.getElementById( parentContainerName );
        }
        var newDiv = document.createElement('div');
        newDiv.innerHTML = result;
        insertAfter(newDiv, insertCommentAfter);
        
    }
    else{
        //notify user of failure
        alert('An error occurred while processing your comment.');
    }
    CloseCommentForm();
}

function CloseCommentForm()
{
    var commentFormElement = document.getElementById( "commentForm" );
    if( commentFormElement )
    {
        //alert('commentForm found');
        var commentFormParentNode = commentFormElement.parentNode;
        //alert('commentFormParentNode = ' + commentFormParentNode.id);
        //alert('commentFormParentNode.firstChild = ' + commentFormParentNode.firstChild.id);
        //commentFormParentNode.removeChild( commentFormParentNode.firstChild );
        //removeElement(commentFormElement.id);
        
        
        commentFormParentNode.removeChild( commentFormElement );
        
        if(commentFormParentNode.parentNode.id == "newCommentContainer")
        {
            AddClassToElement( commentFormParentNode.parentNode, "commentFormClosed" );
            RemoveClassFromElement( commentFormParentNode.parentNode, "commentFormOpen" );
        }
        else
        {
            RemoveClassFromElement( commentFormParentNode.parentNode.parentNode, "replyingToComment" );
        }
                
        return true;
    }
    else{
        //alert('commentForm not found');
        return false;
    }
}

function insertAfter(newElement, targetElement) 
{   
    //target is what you want it to go after. Look for this elements parent.
    var parent = targetElement.parentNode;
    
    //if the parents lastchild is the targetElement...
    if(parent.lastchild == targetElement) 
    {
        //add the newElement after the target element.
        parent.appendChild(newElement);
    } 
    else 
    {
        // else the target has siblings, insert the new element between the target and it's next sibling.
        parent.insertBefore(newElement, targetElement.nextSibling);
    }
}

