﻿var commentInProcess = false;

/*---------------------------------------------------------------
name        : fst_CommentsInit
description : 
----------------------------------------------------------------- */
function fst_CommentsInit()
{
    if(DEBUG == true) { alert('fst_CommentsInit'); }
    fst_HideLoader();
    Event.observe('fstAddComment', 'submit', fst_AddComment);
}

/*---------------------------------------------------------------
name        : fst_NewCommentFocus
description : 
----------------------------------------------------------------- */
function fst_NewCommentFocus()
{
    if($('cmt'))
    {
        $('cmt').focus();
    }
}

/*---------------------------------------------------------------
name        : fst_AddComment
description : 
----------------------------------------------------------------- */
function fst_AddComment(evt)
{
    if(DEBUG) { alert('here'); }
    
    Event.stop(evt);
    
    if(commentInProcess == true)
    {
        alert('A comment is currently being added, please wait.');
        return;
    }
    
    if($('cmt').value.length < 2)
    {
        $('fstCommentMessage').innerHTML = "<p class=\"fstSmallError\">Please enter your comment.</p>";
        $('cmt').focus();
        return;
    }
    
    if($('cc'))
    {
        if($('cc').value.length < 5)
        {
            $('fstCommentMessage').innerHTML = "<p class=\"fstSmallError\">You must enter the capture code</p>";
            $('cc').focus();
            return;
        }
    }
        
    var allNodes = Form.serialize('fstAddComment');
    var nodeArray = allNodes.split('&');

    // extract the comment type        
    var commentType = nodeArray[1];
            
    if(DEBUG == true) { alert(allNodes); }
        
    fst_ShowLoader();
    commentInProcess = true;
    
    // fire off an event to set the rating
    new Ajax.Request(URL_ADDCOMMENT,
    {
      method: 'post',
      parameters: allNodes,
      onSuccess: function(transport)
        {
	        fst_CheckAddCmtSuccess(commentType, transport.responseText);
        },
      onFailure: function()
        {
            commentInProcess = true;
	        alert('Error posting the comment');
        }
    });
}

/*---------------------------------------------------------------
name        : fst_CheckAddCmtSuccess
description : 
----------------------------------------------------------------- */
function fst_CheckAddCmtSuccess(type, retVal)
{	
    if(DEBUG) { alert('fst_CheckAddCmtSuccess: ' + retVal); }
        
    // check the length of the returned value
    if(retVal.substring(0,5) != 'error')
    {
        if(type == 'type=discussion')
	    {
	        var dNode = document.createElement('div');
	        dNode.setAttribute('class',"discussion");
	        dNode.innerHTML = retVal;
	        $('fstCommentList').appendChild(dNode);
	        $('cmt').value = '';
	        fst_ResetCommentInput();
	    }
	    else
	    {
	        var ccid = retVal.substring(retVal.indexOf('##CC:') + 5);
	        fst_DisplayCaptcha(ccid);
	       
	        var dNode = document.createElement('div');
	        dNode.setAttribute('class','comment');
	        dNode.innerHTML = retVal.substring(0,retVal.indexOf('##CC:'));
	        $('fstCommentList').appendChild(dNode);
	        $('cmt').value = '';
	        if($('cc'))
	        {
	            $('cc').value = '';
	        }
        }
        
        $('fstCommentMessage').innerHTML = '<p class=\"fstSmallInfo\">Your comment was added successfully</p>';
    }
    else
    {        
        // understand what the error is
        if(retVal.substring(0,11) == 'error - cc:')
        {
            // need to setup a new captcha code
            $('fstCommentMessage').innerHTML = '<p class=\"fstSmallError\">Invalid Security Code. Please enter the Security code as displayed in the Image</p>';
            
            if($('cc'))
            {
                fst_DisplayCaptcha(retVal.substring(11,retVal.length));
                $('cc').value = '';
                $('cc').focus();
            }
        }
        else
        {
            $('fstCommentMessage').innerHTML = '<p>' + retVal + '</p>';
        }
    }
   
	commentInProcess = false;
	fst_HideLoader();
}

/*---------------------------------------------------------------
name        : fst_DisplayCaptcha
description : 
----------------------------------------------------------------- */
function fst_DisplayCaptcha(ccid)
{
    $('captchaimage').innerHTML = '<img src="/FireStarterApplication/imageCaptcha.aspx?c=' + ccid + '"/>';
    $('ccguid').value = ccid;
}

function fst_ResetCommentInput()
{
    tinyMCE.execCommand('mceRemoveControl', false, 'cmt');
    $('cmt').value = '';
    tinyMCE.execCommand('mceAddControl', false, 'cmt');
}