
isc.defineClass("OPINIATOR_SeqEditor", isc.VLayout);
OPINIATOR_SeqEditor.addProperties({

    // Constructor Properties
    /**
     * Parent editor for enable and disabling save
     */
    parentEditor: null,

    // Private Properties
    _overrides: null,
    _scats: null,
    _toolbar: null,
    _catpicker: null,
    _seqlist: null,

    initWidget: function ()
        {
        // Survey categories come in from the server
        this._seqlist = OPINIATOR_CategoryList.create({
                ID:"SESeqListID", 
                height:"65%",
                width:"100%",
                fields:
                    [ 
                    {
                    name:"title", 
                    title:"Survey", 
                    align:"Center", 
                    cellAlign:"left",
                    formatCellValue: function (value, record, rowNum, colNum, grid)
                        { return (rowNum+1) + ". " + record.catref.title; }
                    }
                    ],

                target:this,
                cellClick: function ()
                    { this.target._seqSelected(); return true;}
                });


        this._toolbar = OPINIATOR_ButtonBar.create({
            ID:"SECatToolbarID",
            align:"left",
            layoutAlign:"center",
            canFocus:false,
            defaultWidth:80,
            height:30,
            width:"100%",

            target:this,
            buttons: [
                {
                ID: "SECAddID",
                title: "Add",
                visibility:"hidden",
                tabIndex:1,
                icon: "icons/16/category.png",
                click: function ()
                    {
                    this.target._addSelectedCategories();
                    return true;
                    }
                },
                {
                ID: "SECRemoveID",
                title: "Remove",
                visibility:"hidden",
                tabIndex:2,
                icon: "icons/16/delete.png",
                width:110,
                click: function ()
                    { this.target._removeSelectedCategories(); return true; }
                },
                {
                ID: "SECUpID",
                title: "Up",
                visibility:"hidden",
                tabIndex:3,
                icon: "icons/16/up.png",
                click: function ()
                    { this.target._moveCategory("up"); return true; }
                },
                {
                ID: "SECDownID",
                title: "Down",
                visibility:"hidden",
                tabIndex:4,
                icon: "icons/16/down.png",
                click: function ()
                    { this.target._moveCategory("down"); return true; }
                }]
            });

        this._catpicker = OPINIATOR_CategoryPicker.create({
                ID:"SECatPickerID", 
                width:"100%", 
                hieght:"*", 
                fields: [ {name:"title", title:"Categories", align:"Center", cellAlign:"left"} ],

                target:this,
                cellClick: function ()
                    { this.target._catSelected(); return true; }
                });


        this.members = [ this._seqlist, OPINIATOR.vSpacer(3), this._toolbar, OPINIATOR.vSpacer(3), this._catpicker ]; 

        this.Super("initWidget", arguments);
        },

    destroy: function ()
        {
        this.parentEditor = null;

        this._overrides = null;
        this._scats = null;

        this._toolbar.target = null;
        this._toolbar = null;

        this._catpicker.target = null;
        this._catpicker = null;

        this._seqlist.target = null;
        this._seqlist = null;
        this.Super("destroy", arguments);
        },


    edit: function ( survey )
        {
        // Clear the category editor
        this._seqlist.deselectAllRecords();
        this._catpicker.deselectAllRecords();
        this.parentElement.clearEditor();
        if ( isc.isAn.Object(survey) )
            {
            this._overrides = survey.overrides;
            this._scats = survey.sequence;

            // Filter out all the Categories in the Sequence Categories
            this._seqlist.setData(this._scats);
            this._catpicker.setFilter(this._scats);

            SECUpID.hide();
            SECDownID.hide();
            }
        },

    save: function ( survey )
        {
        // Enures the sequence numnbers are up-to-date
        var len = this._scats.getLength();
        for ( var i = 0; i < len; i++ )
            { this._scats[i].seqno = i; }

        // Don't copy bogus overrides.  ISC sends nulls where Axis does not
        len = this._overrides.getLength();
        for ( i = len - 1; i >= 0; i-- )
            {
            var o = this._overrides[i];
            if ( !(isc.isA.String(o.mobiText) || isc.isA.String(o.smsText) || isc.isA.String(o.voiceText)) )
                { this._overrides.removeAt(i); }
            }

        survey.overrides = this._overrides;
        survey.sequence = this._scats;
        },

    _catSelected: function ()
        {
        this._seqlist.deselectAllRecords();
        this.parentElement.clearEditor();
        SECAddID.show();
        SECRemoveID.hide();
        SECUpID.hide();
        SECDownID.hide();
        },

    _seqSelected: function ()
        {
        this._catpicker.deselectAllRecords();
        SECAddID.hide();
        SECRemoveID.show();


        var ssel = this._seqlist.getSelection();
        var slen = ssel.getLength();
        // Don't enable remove if the category selected is not removable
        for ( var s = 0; s < slen; s++  )
            { 
            if ( ssel[s].catref.type == "WELCOME" || 
                 ssel[s].catref.type == "THANKYOU" )
                {
                SECRemoveID.hide();
                break;
                }
            }

        var sel = this._seqlist.getSelection();
        if ( sel.getLength() > 1 )
            {
            SECUpID.hide();
            SECDownID.hide();
            this.parentElement.clearEditor();
            }
        else
            {
            var scat = sel[0];
            // See if the actuall category is cached see onCategoryLoaded
            if ( isc.isAn.Object(scat) )
                {
                this._showMoveButtons(scat);
                this.parentElement.editCategory(scat.catref.id, this._overrides);
                }
            }
        },

    _showMoveButtons: function ( scat )
        {
        var rownum = this._scats.indexOf(scat);
        var slen = this._scats.getLength();
        if ( slen > 1 )
            {
            OPINIATOR.hideItem(SECUpID, ( rownum  <= 1 ));
            OPINIATOR.hideItem(SECDownID, ( rownum === 0 || rownum === (slen - 1)));
            }
        else
            {
            SECUpID.hide();
            SECDownID.hide();
            }
        },

    _addSelectedCategories: function ()
        {
        var ssel = this._catpicker.getSelection();
        var slen = ssel.getLength();

        if ( slen > 0 )
            {
            var scats = new Array(slen);
            for ( var s = 0; s < slen; s++  )
                {
                var catref = ssel[s];
                scats[s] = {catref:catref};
                }
            this._catpicker.removeSelectedData();
            var start = ( this._scats.getLength() > 0 ) ? 1 : 0;
            this._scats.addListAt(scats, start);
            this._seqlist.selectRecords(scats);
            this._seqSelected();
            this.parentEditor.enableSave();
            this.fireSequenceChange();
            }
        },

    /**
     * Remove selected survey categories and put the reference back 
     * into the _catpicker list. 
     */
    _removeSelectedCategories: function ()
        {
        var ssel = this._seqlist.getSelection();
        var slen = ssel.getLength();

        // Verify that 
        for ( var s = 0; s < slen; s++  )
            { 
            if ( ssel[s].catref.type == "WELCOME" )
                alert("You cannot Welcome Message");
            else if ( ssel[s].catref.type == "THANKYOU" )
                alert("You cannot remove Thank You Message");
            }

        if ( slen > 0 )
            {
            var dcatrefs = [];
            for ( s = 0; s < slen; s++  )
                { dcatrefs.add(ssel[s].catref); }
            this._seqlist.removeSelectedData();
            var pdata = this._catpicker.getData();
            pdata.addListAt(dcatrefs, 0);
            pdata.sortByProperty("title", true);
            this._catpicker.selectRecords(dcatrefs);
            this._catSelected();
            this.parentEditor.enableSave();

            /** 
             * Go through and remove the overrides for the removed the 
             * categories. 
             */
            var dlen = dcatrefs.getLength();
            for ( var d = 0; d < dcatrefs.getLength(); d++ )
                {
                var catref = dcatrefs[d];
                var del = this._overrides.findAll("catid", catref.id);
                if ( del !== null )
                    { this._overrides.removeAll(del); }
                }

            this.fireSequenceChange();
            }
        },

    _moveCategory: function ( direction )
        {
        if ( this._seqlist.getSelection().getLength() == 1 )
            {
            var scat = this._seqlist.getSelectedRecord();
            var rownum = this._scats.indexOf(scat);
            if ( direction == "up" )
                {
                if ( rownum > 1 )
                    { OPINIATOR.swap(this._scats, rownum, rownum-1); }
                }
            else
                {
                if ( rownum > 0 && rownum < (this._scats.getLength() -1) )
                    { OPINIATOR.swap(this._scats, rownum, rownum+1); }
                }
            this._scats.dataChanged();
            // Refresh the button state because of the change in positions
            this._seqSelected();
            this.parentEditor.enableSave();
            }
        },

    fireSequenceChange: function ( callback )
        { 
        if ( isc.isA.Object(this._sequenceChangeCallback) )
            { Class.fireCallback(this._sequenceChangeCallback, ["scats"], [this._scats]); }
        },

    onSequenceChange: function ( callback )
        { this._sequenceChangeCallback = callback; },

    callback: function (methodName)
        { return {target:this, methodName:methodName}; }

    });



isc.defineClass("OPINIATOR_QuestionnaireEditor", isc.HLayout);
OPINIATOR_QuestionnaireEditor.addProperties({
    // Consturctor Properties
    parentEditor: null,

    initWidget: function ()
        {
        OPINIATOR_QuestionEditor.create({ID:"SEQuestionEditorID", height:"100%", width:"70%", parentEditor:this.parentEditor});
        OPINIATOR_SeqEditor.create({ID:"SESeqEditorID", height:"100%", width:"*", parentEditor:this.parentEditor});
        
        this.members = [
            SESeqEditorID, 
            OPINIATOR.hSpacer(5), 
            SEQuestionEditorID
            ];

        this.Super("initWidget", arguments);
        },

    destroy: function ()
        {
        this.parentEditor = null;
        this.Super("destroy", arguments);
        },

    clearEditor: function ()
        {
        this.removeMember(SEQuestionEditorID);
        SEQuestionEditorID.destroy();
        OPINIATOR_QuestionEditor.create({ID:"SEQuestionEditorID", height:"100%", width:"70%", parentEditor:this.parentEditor});
        this.addMember(SEQuestionEditorID);
        },

    editCategory: function ( catid, overrides )
        { 
        this.clearEditor();
        SEQuestionEditorID.edit(catid, overrides); 
        },

    onSequenceChange: function ( callback )
        { SESeqEditorID.onSequenceChange(callback); },

    edit: function ( survey )
        { SESeqEditorID.edit(survey); },

    save: function (survey)
        { SESeqEditorID.save(survey); }

    });

