﻿
var URL_GETPOSTCOMMENT = FSTPATH + "callbacks/comments/getaddcomment.aspx";
var URL_ADDCOMMENT = FSTPATH + 'callbacks/comments/addcomment.aspx';


/*--------------------------------------------------------------- */
function fst_CommentsInit()
{
    fst_HideLoader();
     fst_SetCommentFormEventHandler();
}

/*--------------------------------------------------------------- */
function fst_GetAddComment()
{
    if($(_addCommentPlaceholderID))
    {
        new Ajax.Request(URL_GETPOSTCOMMENT,
        {
            method: "post",
            parameters: { type: _currentItemType, itemID: _currentItemID },
	        onSuccess: function(transport)
            {
	            fst_GetAddCommentSuccess(transport.responseText);
            },
          onFailure: function()
            {
                alert("unable to retrieve the comment box");
            }
        });
    }
}

/*--------------------------------------------------------------- */
function fst_GetAddCommentSuccess(retVal)
{
    if($(_addCommentPlaceholderID) && retVal !== null && retVal.length > 0)
    {
        $(_addCommentPlaceholderID).innerHTML = retVal;
        
        _isCommentSignIn = true;
        
        if($(_registerLinkID)) { fst_HidePanel(_registerLinkID); }
        
        fst_SetCommentFormEventHandler();
        fst_SetRegisterFormEventHandler();
        fst_SetCommentSignInFormEventHandler();

        if(true === _focusOnCommentBox) { fst_NewCommentFocus(); }
    }
}

/*--------------------------------------------------------------- */
function fst_WaitThenGetAddComment(waitTime)
{
    _focusOnCommentBox = true;
    setTimeout("fst_GetAddComment();",waitTime);
}

/*--------------------------------------------------------------- */
function fst_NewCommentFocus()
{
    if($(_commentInputBoxID))
    {
        $(_commentInputBoxID).focus();
    }
}

/*--------------------------------------------------------------- */
function fst_AddComment(evt)
{
    if(DEBUG) { alert('fst_AddComment'); }
    
    Event.stop(evt);
    
    if(DEBUG) { alert("Comment in progress: " + _commentInProgress); }
    
    if(_commentInProgress === true)
    {
        alert('A comment is currently being added, please wait.');
        return;
    }
    
    if($(_commentInputBoxID).value.length < 2)
    {
        fst_DisplayCommentFeedbackMessage('Please enter your comment','fstSmallError');
        $(_commentInputBoxID).focus();
        return;
    }
    
    if($(_captchaInputBoxID))
    {
        if($(_captchaInputBoxID).value.length < 5)
        {
            fst_DisplayCommentFeedbackMessage('You must enter the capture code','fstSmallError');
            $(_captchaInputBoxID).focus();
            return;
        }
    }
        
    var allNodes = Form.serialize(_addCommentFormID);
    var nodeArray = allNodes.split('&');

    // extract the comment type        
    var commentType = nodeArray[1];
            
    if(DEBUG) { alert(allNodes); }
        
    fst_ShowLoader();
    _commentInProgress = 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()
        {
            _commentInProgress = true;
	        alert('Error posting the comment');
        }
    });
}

/*--------------------------------------------------------------- */
function fst_CheckAddCmtSuccess(type, retVal)
{	
    if(DEBUG) { alert('fst_CheckAddCmtSuccess: ' + retVal); }
        
    // check the length of the returned value
    if(retVal.substring(0,5) != 'error')
    {
        var dNode;
        
        if(type === 'type=discussion')
	    {
	        dNode = document.createElement('div');
	        dNode.setAttribute('class',"discussion");
	        dNode.innerHTML = retVal;
	        
	        $(_commentListID).appendChild(dNode);
	        $(_commentInputBoxID).value = '';
	        
	        //fst_ResetCommentInput();
	    }
	    else
	    {
	        var ccid = retVal.substring(retVal.indexOf('##CC:') + 5);
	        fst_DisplayCaptcha(ccid);
	       
	        dNode = document.createElement('div');
	        dNode.setAttribute('class','comment');
	        dNode.innerHTML = retVal.substring(0,retVal.indexOf('##CC:'));
	        
	        $(_commentListID).appendChild(dNode);
	        $(_commentInputBoxID).value = '';
	        
	        if($(_captchaInputBoxID))
	        {
	            $(_captchaInputBoxID).value = '';
	        }
        }
        
        fst_DisplayCommentFeedbackMessage('Your comment was added successfully','fstSmallInfo');
    }
    else
    {        
        // understand what the error is
        if(retVal.substring(0,11) === 'error - cc:')
        {
            // need to setup a new captcha code
            fst_DisplayCommentFeedbackMessage('Invalid Security Code. Please enter the Security code as displayed in the Image','fstSmallError');
            
            if($(_captchaInputBoxID))
            {
                fst_DisplayCaptcha(retVal.substring(11,retVal.length));
                $(_captchaInputBoxID).value = '';
                $(_captchaInputBoxID).focus();
            }
        }
    }
   
	_commentInProgress = false;
	fst_HideLoader();
}


/*--------------------------------------------------------------- */
function fst_DisplayCaptcha(ccid)
{
    if($(_captchaImageID)) { $(_captchaImageID).innerHTML = '<img src="' + FSTPATH + 'imageCaptcha.aspx?c=' + ccid + '"/>'; }
    if($(_captchaGuidID)) { $(_captchaGuidID).value = ccid; }
}

/*--------------------------------------------------------------- */
function fst_ResetCommentInput()
{
    tinyMCE.execCommand('mceRemoveControl', false, 'cmt');
    $('cmt').value = '';
    tinyMCE.execCommand('mceAddControl', false, 'cmt');
}

/*--------------------------------------------------------------- */
function fst_DisplayCommentFeedbackMessage(msg, className)
{
    if($(_commentMessageBoxID)) { $(_commentMessageBoxID).innerHTML = '<p class="' + className + '">' + msg + '</p>'; }
}

/*--------------------------------------------------------------- */
function fst_SetCommentFormEventHandler()
{
    if($(_addCommentFormID)) { Event.observe(_addCommentFormID, 'submit', fst_AddComment); }
}

/*--------------------------------------------------------------- */
function fst_SetRegisterFormEventHandler()
{
    if($(_commentRegisterFormID)) { Event.observe(_commentRegisterFormID, 'submit', fst_DoRegisterAccount); }
    //alert($(_commentRegisterFormID));
}

/*--------------------------------------------------------------- */
function fst_SetCommentSignInFormEventHandler()
{
    if($(_commentSignInFormID)) { Event.observe(_commentSignInFormID, 'submit', fst_DoCommentSignIn); }
}
