/**
 * @author Tran Quoc Huy
 */
// ----- GeneralForm -----------
isc.defineClass("OPINIATOR_SEGeneralResForm", isc.DynamicForm);
OPINIATOR_SEGeneralResForm.addProperties({
    // Constructor Properties 
    parentEditor: null,	
	
    // Superclass Properties
    showBusy:OPINIATOR_ShowBusy,
    destroyBusy:OPINIATOR_DestroyBusy,

    // Superclass Properties
    width:"100%",    
    minColWidth:10,
    cellPadding:2,
    numCols:2,
    fixedColWidths:false,
    titleWidths:80,
    cellBorder:0,
    showResizeBar:false,
    layoutAlign:"left",
    itemHoverDelay:3000,
    selectOnFocus:true,
    items:[
        
		{ height:5, startRow:true, editorType:"rowSpacer" },
		//Organization Row
        {
	        name:"organization",
	        type:"text",
	        title:"Organization",
	        width:250,
	        height:25,		
	        _constructor:"OPINIATOR_TextItem"
        },
		
		{ height:5, startRow:true, editorType:"rowSpacer" },		
		//  Survey Name Row
        {
        name:"name",
        type:"text",
        title:"Survey Name",
        width:250,
        height:25,		
        _constructor:"OPINIATOR_TextItem"
        },
		
        { height:5, startRow:true, editorType:"rowSpacer" },
		//Number of Responses
         {
	        name:"numberOfResponses",
	        type:"text",
	        title:"Number of Responses",
	        width:250,
	        height:25,		
	        _constructor:"OPINIATOR_TextItem"
	     },
		 
		 {
	       ID:"ToolBarResponseID",
			name: "toolbar_button",
			title: "",
	        startRow:true,
	        height:30,			
	        type:"canvas",
	        showTitle:true,
	        showFocused:true,
	        showFocusedAsOver:false,
	        titleAlign:"right",
	        titleOrientation:"left",
	        align:"left",
	        textAlign:"left",
	        canvasConstructor:"OPINIATOR_ButtonBar",
	        canvasProperties: {
				ID: "ToolBarResponse",
				align: "left",
				layoutAlign: "center",
				canFocus: false,
				defaultWidth: 170,
				height: 30,				
				target: this,
				buttons: [
					{
		                ID: "deleteSurveyResponse",
		                title: "Remove All Responses",
						target: this,
		                visibility:"visible",
		                tabIndex:1,
		                icon: "icons/16/delete.png",
		                click: function () {
							this.parentElement.parentElement.deleteResponse();							
							return true;
						}
                	}
				]
			}
	     }		
		 		
        ],
	
    initWidget: function ()
	{
        this.Super("initWidget", arguments);		
    },	
	
	deleteResponse: function ()
    {
		surveyName=this.parentEditor.parentElement._editor._survey.name;
     	OPINIATOR.askOk("All responses for the "+surveyName+" Survey will be removed from the system.<br> Are you sure you wish to delete these responses?", 
     	this.callback("deleteResponseReply"));
	 },
    deleteResponseReply: function ( ok )
    { 
        if (ok){ 	
			WebClientSvc.deleteResponseOfSurvey(this.parentEditor.parentElement._editor._survey, this.callback("_edit"), this.callback("error") );
		}
    },		
    destroy: function ()
        {
        this.parentEditor = null;
        this.destroyBusy();
        this.Super("destroy", arguments);
        },

    _edit: function ( )
        {
       		this.parentEditor.parentElement._editor.edit(this.surveyid);  
        } ,
		
	edit: function ( survey )
        {
	        this.surveyid = survey.id;		
	        this.setValue("name", survey.name);
			this.setValue("organization", survey.orgUnitName);      
			this.setValue("numberOfResponses", survey.numberOfResponses);		  
        } ,	
	error: function (code, msg)
        {
        this.showBusy(false);
        switch ( msg )
            {
             case "DeletedState":
                OPINIATOR.warn("This survey has been deleted by another user.");                
                break;

            default:
                OPINIATOR.warn("Unhandled Exception: " + msg);
                break;
            }
        },	
	callback: function (methodName)
        { return {target:this, methodName:methodName}; }	  
	
});


// ----------------
isc.defineClass("OPINIATOR_SurveyResponseEditor", OPINIATOR_TabSet);
OPINIATOR_SurveyResponseEditor.addProperties({

    // Constructor Properties

    /**
     * The selector that contains the key node to this survey for 
     * refresh purposes 
     */
    selector: null,
	

    // The Survey we are editing
    _survey: null,


    initWidget: function ()
        { 
            

        this.tabs = new Array();

        this.tabs.add({
            ID:"SEGeneralResID",
            title:"General", 
            openPane: function ( parent, pane, survey ) 
                {
                if ( !isc.isAn.Object(pane) ) 
                    { pane = OPINIATOR_SEGeneralResForm.create({ID:"SEGeneralResFormID", parentEditor:parent}); }
                pane.edit(survey);
                return pane;
                }
            });       
        this.Super("initWidget", arguments);

        },

    destroy: function ()
        {
      
        this.selector = null;      
        this.Super("destroy", arguments);
        },

   
    edit: function ( survey )
        {
        if ( isc.isA.Number(survey) )
            {
            this.showBusy(true, OPINIATOR.BUSY_DELAY);
            WebClientSvc.getSurvey(survey, this.callback("edit"), this.callback("error"));			
            }
        else
            {
            // Update the survey node from a potential reload
            this._survey = survey;
            if ( isc.isA.Number(survey.id) )
                {
                this.selector.refreshSurveyNode(survey);                
                }          
            // Ensure that the all the survey's lists are indeed lists
            this.openPanes(survey);
            this.showBusy(false);
            }
        },

    openPanes : function (survey)
        {
        survey.kpis = OPINIATOR.toArray(survey.kpis);
        survey.sequence = OPINIATOR.toArray(survey.sequence);
        survey.overrides = OPINIATOR.toArray(survey.overrides);
        this.Super("openPanes", arguments);
        },  

    error: function (code, msg)
        {
        this.showBusy(false);
        switch ( msg )
            {
            case "DeletedState":
                OPINIATOR.warn("This survey has been deleted by another user.");
                this.selector.refresh();
                this.hide();
                break;

            default:
                OPINIATOR.warn("Unhandled Exception: " + msg);
                break;
            }
        },

    callback: function (methodName)
        { return {target:this, methodName:methodName}; }

    });

