
isc.defineClass("OPINIATOR_NumericRangeForm", isc.DynamicForm);
OPINIATOR_NumericRangeForm.addProperties({

    // Constructor Properties
    parentEditor:null,
    disableRange:null,

    // Abstract functions
    setRangeMin: function ( min ) { throw "setRangeMin"; },
    setRangeMax: function ( max ) { throw "setRangeMax"; },

    _minValue: 0,
    _maxValue: 10,

    // Superclass Properties
    cellPadding:2,
    numCols:2,
    //fixedColWidths:true,
    //titleWidths:80,
    cellBorder:0,
    layoutAlign:"left",
    //itemHoverDelay:3000,
    items:[
        { height:5, editorType:"RowSpacerItem" },
        //  Header
        {
        name:"header",
        defaultValue:"Numeric Range",
        align:"center",
        type:"header"
        },
        //  Max Value
        {
        name:"maxv",
        title:"Maximum Value",
        type:"integer",
        textAlign:"right",
        wrapTitle:false,
        showFocused:true,
        showFocusedIcons:true,
        width:30,
        height:24
        // Spinner's not working in SC 7.0RC2
        //editorType:"spinner"
        },
        //  Min Value
        {
        name:"minv",
        title:"Minimum Value",
        textAlign:"right",
        type:"integer",
        showFocused:true,
        showFocusedIcons:true,
        type:"integer",
        wrapTitle:false,
        width:30,
        height:24
        // Spinner's not working in SC 7.0RC2
        //editorType:"spinner"
        }
        ],

    initWidget: function ()
        { 
        this.Super("initWidget", arguments); 
        //this._adjustSpinnerIcons("minv", 14, 12);
        //this._adjustSpinnerIcons("maxv", 14, 12);
        if ( this.disableRange )
            {
            this.getItem("minv").disable();
            this.getItem("maxv").disable();
            }
        },

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

    _adjustSpinnerIcons: function ( itemname, width, height )
        {
        var item = this.getItem(itemname);
        for ( var i = 0; i < 2; i++ )
            {
            item.icons[i].width = width;
            item.icons[i].height = height;
            }
        },

    itemChange: function (item, newValue, oldValue)
        { 
        newValue = new Number(newValue);
        switch ( item.getFieldName() )
            {
            case "minv":
                if ( newValue < this._minValue || newValue > this._maxValue || newValue >= new Number(this.getValue("maxv")) )
                    { return false; }
                this.setRangeMin(new Number(newValue));
                return true;
                break;
            case "maxv":
                if ( newValue < this._minValue || newValue > this._maxValue || newValue <= new Number(this.getValue("minv")) )
                    { return false; }
                this.setRangeMax(new Number(newValue));
                return true;
                break;
            default:
                break;
            }
        return false;
        },

    editValues: function ( minv, maxv ) 
        {
        this.setValue("minv", minv);
        this.setValue("maxv", maxv);
        }

    });


isc.defineClass("OPINIATOR_ValueMapEditor", isc.ListGrid);
OPINIATOR_ValueMapEditor.addProperties({
    // Superclass Properties
    selectionType: "none",
    canPickFields:false,
    canResizeFields:false,
    canFreezeFields:false,
    canReorderFields:false,
    canGroupBy:false,
    canSort:false,
    canEdit:true,
    editOnFocus:true,
    leaveScrollbarGap:false,
    showAllRecords:true,
    showHeader:true,
    showHeaderContext:false,
    alternateRecordStyles:false,
    border:"1px solid #000000",
    baseStyle:"valuemapCell",
    styleName:"valuemapBody",
    bodyStyleName:"valuemapBody",
    headerBaseStyle:"valuemapHeader",
    bodyBackgroundColor:null,
    headerBackgroundColor:null,
    cellHeight:20,
    cellPadding:2,
    modalEditing:true,
    minValue: 0,
    maxValue: 10,

    fields: [
            {
            name:"value", 
            title:" ", 
            align:"center", 
            width:30,
            cellAlign:"left",
            canEdit:false
            },
            {
            name:"text", 
            title:"Value Text", 
            align:"center", 
            width:"*",
            editorProperties:{height:22},
            editorType:"OPINIATOR_TextItem",
            cellAlign:"left"
            }
        ],


    initWidget: function ()
        { this.Super("initWidget", arguments); },

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

    editorExit: function (editCompletionEvent, record, newValue, rowNum, colNum)
        {
        this.Super("editorExit", arguments);
        this.deselectAllRecords();
        this.redraw();
        this.parentEditor.enableSave();
        },

    editValues: function ( question )
        {
        this._question = question;
        this.setData(this._question.valueTextMap);
        },

    setRangeMax: function ( max )
        {
        max = new Number(max);

        this._question.maxValue = max;
        var list = this.getData();

        // Remove elements smaller greater then max
        while ( list.getLength() > 0 && (new Number(list[0].value) > max) )
            { list.removeAt(0); }

        // Add elements up to maxium
        while ( list.getLength() > 0 && (new Number(list[0].value) < max) )
            { 
            obj = new Object();
            obj.value =  new Number(list[0].value) + 1;
            obj.text =  "";
            list.addAt(obj, 0);
            }

        this.parentEditor.enableSave();
        return true;
        },

    setRangeMin: function ( min )
        {
        min = new Number(min);
        this._question.minValue = min;
        var list = this.getData();

        // Remove elements smaller than  min
        while ( list.getLength() > 0 && new Number(list[list.getLength()-1].value) < min )
            { list.removeAt(list.getLength()-1); }

        // Add elements up to minimum
        while ( list.getLength() > 0 && new Number(list[list.getLength()-1].value) > min )
            { 
            obj = new Object();
            obj.value =  new Number(list[list.getLength()-1].value) - 1;
            obj.text =  "";
            list.add(obj);
            }

        this.parentEditor.enableSave();
        }

    });


isc.defineClass("OPINIATOR_NumericResponseEditor", isc.HLayout);
OPINIATOR_NumericResponseEditor.addProperties({

    // Constructor Properties
    parentEditor: null,
    question: null,
    disableRange: false,

    // Private Properties
    _numericRangeForm: null,
    _valueMapEditor: null,

    initWidget : function () 
        {

        this._valueMapEditor = OPINIATOR_ValueMapEditor.create({ 
            parentEditor:this.parentEditor,
            width:"*"
            });

        this._numericRangeForm = OPINIATOR_NumericRangeForm.create({ 
            target:this._valueMapEditor,
            disableRange:this.disableRange,
            width:"200",
            setRangeMin: function( min )
                { this.target.setRangeMin(min); },
            setRangeMax: function( max )
                { this.target.setRangeMax(max); }
            });

        this.members = [this._numericRangeForm, this._valueMapEditor];
        this.layoutLeftMargin = 5;

        // call superclass implementation
        this.Super("initWidget", arguments);

        this._numericRangeForm.editValues(this.question.minValue, this.question.maxValue);
        this._valueMapEditor.editValues(this.question);
        },

    destroy: function()
        {
        delete this._numericRangeForm.target;
        delete this._numericRangeForm;
        delete this._valueMapEditor.parentEditor;
        delete this._valueMapEditor;
        delete this.question;
        delete this.parentEditor;
        this.Super("destroy", arguments);
        }

    });



isc.defineClass("OPINIATOR_ChoiceList", isc.ListGrid);
OPINIATOR_ChoiceList.addProperties({
    // Superclass Properties
    selectionType: "multi",
    canEdit:false,
    canReorderRecords:false,
    editEvent:"none",
    leaveScrollbarGap:false,
    showAllRecords:true,
    showHeader:true,
    showHeaderContext:false,
    alternateRecordStyles:false,
    border:"1px solid #000000",
    baseStyle:"choicelistCell",
    styleName:"choicelistBody",
    bodyStyleName:"choicelistBody",
    headerBaseStyle:"choicelistHeader",
    bodyBackgroundColor:null,
    headerBackgroundColor:null,
    cellHeight:20,
    cellPadding:2,
    modalEditing:true,

    fields: [
            {
            name:"text", 
            title:"Choices", 
            align:"center", 
            width:"*",
            editorProperties:{height:22},
            editorType:"OPINIATOR_TextItem",
            cellAlign:"left"
            }
        ],


    initWidget: function ()
        { this.Super("initWidget", arguments); },

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

    setValues: function ( question )
        {
        this._question = question;
        this._question.valueTextMap.sortByProperty("value", true);
        this.setData(this._question.valueTextMap);
        },

    canEditCell: function ( row, col )
        {
        if ( this._editableRow >= 0 )
            { return row == this._editableRow; }
        return false;
        },

    startEditing: function ( row, col )
        { 
        this._editableRow = row; 
        this.Super("startEditing", arguments); 
        },

    endEditing: function ()
        {
        delete this._editableRow; 
        this.Super("endEditing", arguments); 
        }

    });

isc.defineClass("OPINIATOR_ChoiceResponseEditor", isc.HLayout);
OPINIATOR_ChoiceResponseEditor.addProperties({

    // Constructor Properties
    parentEditor: null,
    question: null,

    // Private Properties
    _choicelist: null,
    _toolbar: null,

    initWidget : function () 
        {
        this._choicelist = OPINIATOR_ChoiceList.create({
            height:"*",

            target:this,
            cellClick: function ()
                { this.target._refreshButtonState(); return true;},
            editorExit: this.getID()+"._validateChoice(editCompletionEvent, record, newValue, rowNum, colNum)",
            editComplete: this.getID()+"._updateList()"
            });


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

            target:this,
            buttons: [
                {
                ID: "ChoiceAddID",
                title: "Add",
                visibility:"visible",
                tabIndex:1,
                icon: "icons/16/category.png",
                click: function ()
                    { this.target._addItem(); return true; }
                },
                {
                ID: "ChoiceRemoveID",
                title: "Remove",
                visibility:"hidden",
                tabIndex:2,
                icon: "icons/16/delete.png",
                width:110,
                click: function ()
                    { this.target._removeItems(); return true; }
                },
                {
                ID: "ChoiceUpID",
                title: "Up",
                visibility:"hidden",
                tabIndex:3,
                icon: "icons/16/up.png",
                click: function ()
                    { this.target._moveItem("up"); return true; }
                },
                {
                ID: "ChoiceDownID",
                title: "Down",
                visibility:"hidden",
                tabIndex:4,
                icon: "icons/16/down.png",
                click: function ()
                    { this.target._moveItem("down"); return true; }
                },
				{
                ID: "ChoiceEditID",
                title: "Edit",
                visibility:"hidden",
                tabIndex:4,               
				icon: "icons/16/pencil.png",				
                click: function ()
                    { this.target._editItem(); return true; }
                }
				]
            });

        this.members = [OPINIATOR.hSpacer("20%"), 
                        isc.VLayout.create({ members:[this._choicelist, OPINIATOR.vSpacer(5), this._toolbar]}),
                        OPINIATOR.hSpacer("20%")];

        this.layoutLeftMargin = 5;

        // call superclass implementation
        this.Super("initWidget", arguments);

        this._choicelist.setValues(this.question);
        },

    destroy: function()
        {
        delete this._choicelist;
        delete this._toolbar.target;
        delete this._toolbar;
        delete this.question;
        delete this.parentEditor;
        this.Super("destroy", arguments);
        },

    _refreshButtonState: function ()
        {
        var listgrid   = this._choicelist;
        var addbtn     = ChoiceAddID;
        var removebtn  = ChoiceRemoveID;
        var upbtn  = ChoiceUpID;
        var downbtn = ChoiceDownID;
		var editbn = ChoiceEditID;

        //listgrid.deselectAllRecords();
        addbtn.show();
        removebtn.show();
		editbn.show();

        var ssel = listgrid.getSelection();
        var slen = ssel.getLength();

        var sel = listgrid.getSelection();
        if ( sel.getLength() > 1 )
            {
            upbtn.hide();
            downbtn.hide();
			editbn.hide();
            this.parentElement.clearEditor();
            }
        else
            {
            // See if the actual category is cached see onCategoryLoaded
            var s = sel[0];
            if ( isc.isAn.Object(s) )
                { 
                var list = listgrid.getData();
                var rownum = list.indexOf(s);
                slen = list.getLength();

                if ( slen > 1 )
                    {
                    OPINIATOR.hideItem(upbtn, ( rownum === 0 ));
                    OPINIATOR.hideItem(downbtn, ( rownum === (slen - 1)));
                    }
                else
                    {
                    upbtn.hide();
                    downbtn.hide();
                    }
                }
            }
        },

    _addItem: function()
        {
        var listgrid = this._choicelist;
        var list = listgrid.getData();
        var ssel = listgrid.getSelection();
        var slen = ssel.getLength();
        var start = -1;

        listgrid.endEditing();

        if ( slen > 0 )
            { start = list.indexOf(ssel[slen-1]); }

        // The value item is zero and will get reset when the list is appropriately sorted
        var newchoice = {value:0, text:"New Choice"};

        // Force the item to added after the selected item
        start++;
        if ( (start > 0) && (start < list.getLength()) )
            { list.addAt(newchoice, start); }
        else
            { 
            list.push(newchoice); 
            start = list.getLength() - 1;
            if ( start === 0 )
                { listgrid.selectRecord(0); }
            }

        list.dataChanged();
        this._updateList();
        this._refreshButtonState();
        this.parentEditor.enableSave();
        listgrid.startEditing(start, 0);
        },

    _removeItems: function()
        {
        var listgrid = this._choicelist;
        var ssel = listgrid.getSelection();
        var slen = ssel.getLength();

        listgrid.endEditing();

        if ( slen > 0 )
            {
            listgrid.removeSelectedData();
            this.parentEditor.enableSave();
            this._updateList();
            }
        },

    _moveItem: function ( direction )
        {
        var listgrid = this._choicelist;
        listgrid.endEditing();

        if ( listgrid.getSelection().getLength() == 1 )
            {
            var sel = listgrid.getSelectedRecord();
            var list = listgrid.getData();
            var rownum = list.indexOf(sel);
            if ( direction == "up" )
                {
                if ( rownum > 0 )
                    { OPINIATOR.swap(list, rownum, rownum-1); }
                }
            else
                {
                if ( rownum < (list.getLength() -1) )
                    { OPINIATOR.swap(list, rownum, rownum+1); }
                }
            list.dataChanged();
            // Refresh the button state because of the change in positions
            this._refreshButtonState();
            this._updateList();
            this.parentEditor.enableSave();
            }
        },

	// edit item 	
	_editItem: function() {
		var listgrid = this._choicelist;
		if ( listgrid.getSelection().getLength() == 1 ) {
			var sel = listgrid.getSelectedRecord();			
			var data = listgrid.getData();
			var rowNum = data.indexOf(sel);			
			listgrid.startEditing(rowNum, 0);
		}				
	},

    _validateChoice: function (editCompletionEvent, record, newValue, rowNum, colNum)
        {
        return this._choicelist.getData().findIndex("text", newValue) === -1;
        },

    /**
     *  Update the valuemap values based upon the order they are in
     *  the list and then notify the parent.
     */
    _updateList: function ()
        {
        var listgrid = this._choicelist;
        var list = listgrid.getData();

        this.question.minValue = 1;
        this.question.maxValue = list.getLength();

        for ( var i = 0; i < list.getLength(); i++ )
            { list[i].value = i+1; }

        this.updateSMSText(this._listToText());
        },

    _listToText: function ()
        {
        var listgrid = this._choicelist;
        var list = listgrid.getData();

        var text = "(";
        for ( var i = 0; i < list.getLength(); i++ )
            { 
            if ( i > 0 )
                text = text + ", ";
            text = text + list[i].value + "-" + list[i].text; 
            }
        text = text + ")";
        return text;
        },

    // Parent should override to update SMS Field
    updateSMSText: function ( text )
        {
        isc.Log.logInfo("Updating SMS Text: " + text);
        },

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

    });

