﻿function CommentAdd(sParentType, lParentID) {
   Ext.MessageBox.defaultTextHeight = 300;
   Ext.MessageBox.minWidth = 500;
    Ext.MessageBox.prompt(
        'Comment',
        'Please enter your comment:',
        function(btn, text) {
            if (btn == "ok") {
                Ext.Ajax.request({
                    url: "/bespoke/actions/",
                    params: {
                        action: "commentadd",
                        comment: text,
                        parenttype: sParentType,
                        parentid: lParentID
                    },
                    success: function (oResponse, oOptions) {
                        // Check for "logic" errors. ie the transport part of the request worked but server script disallowed
                        var oRespDecoded = Ext.decode(oResponse.responseText);
                        if (!oRespDecoded.success) {
                            Ext.MessageBox.show({
                                title: 'Error',
                                msg: oRespDecoded.error,
                                buttons: Ext.MessageBox.OK,
                                icon: Ext.MessageBox.ERROR,
                                fn: function(buttonId) {
                                    if (oRespDecoded.statuscode == 401) {
                                        refreshPage();
                                    }
                                }
                            });
                        } else {
                            refreshPage();
                        }
                    },
                    failure: function (oResponse, oOptions) {
                        //most often "transport" errors eg page not found, page crashed on server side, etc
                        Ext.MessageBox.show({
                            title: 'Error',
                            msg: oResponse.status + ' ' + oResponse.statusText,
                            buttons: Ext.MessageBox.OK,
                            icon: Ext.MessageBox.ERROR
                        });
                    }
                });
            }
        },
        '',
        true
    );
}

function refreshPage() {
    window.location = window.location.href;
}

