/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://extjs.com/license
 */

var opencommentid = 0;
var win;

function UpdateComment(){
    setStatus('Updating Ticket...');
    var comment = document.getElementById('commentfield').value;
    var email = document.getElementById('commentEmail').value;
    var internal = 0;
    if(document.getElementById('commentInternal').checked){
       internal =1;
    }

    var post;
    post = "commentid="+opencommentid;
    post += "&comment="+comment;
    post += "&email="+ email;

    post += "&internal="+ internal;

    Ext.Ajax.request({
        url: 'index.php?fuse=knowledgebase&action=KB_UpdateComment_Async',
        success: refreshGrid,
        params: post,
        method:'POST'
    });
}

function DeleteComment(){
    setStatus('Deleting Ticket...');
    
    Ext.Ajax.request({
        url: 'index.php?fuse=knowledgebase&action=KB_DeleteComment_Async',
        success: refreshGrid,
        params: 'commentid='+opencommentid,
        method:'POST'
    });
}

var commentds = new Ext.data.Store({
    proxy: new Ext.data.HttpProxy({
        url: 'index.php?fuse=knowledgebase&view=KB_ArticleComment_JSON'
    }),
    reader: new Ext.data.JsonReader({
        totalProperty: "results",
        root: 'data',
        id: 'id'
    }, [
        {name: 'commentfield', mapping: 'commenttext'},
        {name: 'commentid', mapping: 'id'}
    ])
});

commentds.on('load', function(){
    var articleId = this.reader.jsonData.data[0].articleid;
    var articleTitle = this.reader.jsonData.data[0].title;
    var articleLabel = "<span style='font-weight:bold;font-size:11px;'>Article:</span>&nbsp;&nbsp;";

    articleLabel += "<a href='../index.php?fuse=knowledgebase&view=KB_viewArticle&articleId="+articleId+"&public=1' target='_blank'>";
    articleLabel += articleTitle + "</a>";

    //since the SetCommentWindow id does not have commenttext or email of the
    //comment we have to wait until after the load function is called after the
    //datastore returns from its json call to populate the fields.
    document.getElementById('commentfield').value = this.reader.jsonData.data[0].commenttext;
    document.getElementById('commentEmail').value = this.reader.jsonData.data[0].email;
    document.getElementById('commentArticle').innerHTML = articleLabel;

    var cbInternal = Ext.getCmp('commentInternal');
    if(this.reader.jsonData.data[0].internal=="1"){
        cbInternal.setValue(true);
    }else{
        cbInternal.setValue(false);
    }
    
});


function SetCommentWindow(id){

    var newtop = getAbsoluteTop(id);
    var window = Ext.getCmp('commentWindow');
    var btnApprove = Ext.getCmp('btnApprove');
    var btnUpdate = Ext.getCmp('btnUpdate');
    window.y = eval(newtop-190);
    window.animateTarget = id;


    //set default to the category we are in
    var commentid=id;
    var commentDetails = commentid.split("/");

    //populate with information we have already but for comment and email
    //we will wait until the datastore load function to get the values
    //returned from the JSON
    document.getElementById('commentAddedOn').innerHTML = "<span style='font-weight:bold;font-size:11px;'>Added On:</span>&nbsp;&nbsp;"+commentDetails[2];
    document.getElementById('commentUser').innerHTML = "<span style='font-weight:bold;font-size:11px;'>User:</span>&nbsp;&nbsp;"+commentDetails[1];

    //disable button if it is already enabled
    if(commentDetails[3] == 1){
        btnApprove.hide();
        btnUpdate.show();
    }else{
        btnApprove.show();
        btnUpdate.hide();
    }

    opencommentid = commentid;
    commentds.removeAll();
    commentds.load({params:{start:0, limit:10, query:commentid}});

}

function editKBComment(link)
{
    if(!win){
        win = new Ext.Window({
            id:           "commentWindow",
            x:            200,
            applyTo:      'hello-win',
            labelAlign:   'right',
            layout:       'form',
            width:        400,
            height:       300,
            defaultType:  'textfield',
            store:        commentds,
            closeAction:  'hide',
            plain:        true,
            items:        [
                {
                    xtype:  "label",
                    id:     'commentArticle',
                    name:   'commentArticle',
                    style:  'display:block;text-align:left;margin:5px;padding-left:51px;',
                    width:  350
                },
                {
                    xtype:  "label",
                    id:     'commentUser',
                    name:   'commentUser',
                    style:  'display:block;text-align:left;margin:5px;padding-left:63px;',
                    width:  350
                },
                {
                    xtype:  "label",
                    id:     'commentAddedOn',
                    name:   'commentAddedOn',
                    style:  'display:block;text-align:left;margin:5px;padding-left:31px;',
                    width:  350
                },
                {
                    fieldLabel: 'Email',
                    name:       'commentEmail',
                    id:         'commentEmail',
                    width:      278,
                    labelStyle: 'font-weight:bold;font-size:11px;'
                },
                {
                    xtype:      "checkbox",
                    fieldLabel: 'Internal',
                    boxLabel:   '',
                    name:       'commentInternal',
                    id:         'commentInternal',
                    labelStyle: 'font-weight:bold;font-size:11px;'
                },
                new Ext.form.TextArea({
                    id:       "commentfield",
                    border:     false,
                    hideLabel:  true,
                    fieldLabel: 'Comment',
                    height:     110,
                    labelStyle: 'font-weight:bold;font-size:11px;',
                    anchor:     '100%',
                    name:       'commentfield'
                })
            ],
            buttons:      [
                {
                    id:       'btnUpdate',
                    text:     'Update',
                    disabled: false,
                    handler:  function(){
                        UpdateComment();
                        win.hide();
                    }
                },
                {
                    id:       'btnApprove',
                    text:     'Approve',
                    disabled: false,
                    handler:  function(){
                        UpdateComment();
                        win.hide();
                    }
                },
                {
                    id:       'btnDelete',
                    text:     'Delete',
                    handler:  function(){
                        Ext.MessageBox.confirm('Confirm', lang('Are you sure you want to do that?'), function(btn, text){
                            if(btn == 'yes'){
                                DeleteComment();
                                win.hide();
                            }
                        });
                    }
                },
                {
                    text:     'Close',
                    handler:  function(){
                        win.hide();
                    }
                }
            ]
        });
    }
    
    SetCommentWindow(link);
    win.show(link);
}

function refreshGrid()
{
    toggleButtons(0);
    
    grid.enable();
    
    gridstore.load({
        params: {
            start:  0,
            limit:  gridLimit
        }
    });
    
    msg(lang("The action has been performed"));
}