Wtf.namespace('Wtf');
Wtf.BLANK_IMAGE_URL = "web/lib/resources/images/default/s.gif";
Wtf.QuickTips.init();
function getCookie(c_name){
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) 
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

var loginid = getCookie('lid');
var username = getCookie('username');
var loginname = getCookie('loginname');
Wtf.realroles = getCookie('realroles');
Wtf.roleperms = getCookie('perms');
function HTMLStripper(val){
    var str = Wtf.util.Format.stripTags(val);
    return str.replace(/"/g, '');
};

function trim(str){
    if (!str || typeof str != 'string') 
        return null;
    return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
};

function msgBoxShow(choice,TITLE, MSG) {
    var msgIcon;
    switch(choice) {
        case 1: msgIcon = Wtf.MessageBox.INFO
                break;
        case 2: msgIcon = Wtf.MessageBox.ERROR
                break;
    }
    Wtf.MessageBox.show({
        title: TITLE,
        msg: MSG,
        buttons: Wtf.MessageBox.OK,
        animEl: 'upwin',
        icon : Wtf.MessageBox.INFO
    });
}

Wtf.ux.comboBoxRenderer = function(combo) {
    return function(value) {
        var idx = combo.store.find(combo.valueField, value);
        if(idx == -1)
            return "";
        var rec = combo.store.getAt(idx);
        return rec.get(combo.displayField);
    };
}


function getHeader(img,myTitle,description){
    var str =  "<div style = 'width:100%;height:100%;position:relative;float:left;'>"
                    +"<div style='float:left;height:100%;width:auto;position:relative;'>"
                    +"<img src = "+img+" style = 'width:40px;height:52px;margin:5px 5px 5px 5px;'></img>"
                    +"</div>"
                    +"<div style='float:left;height:100%;width:60%;position:relative;'>"
                    +"<div style='font-size:12px;font-style:bold;float:left;margin:15px 0px 0px 10px;width:100%;position:relative;'><b>"+myTitle+"</b></div>"
                    +"<div style='font-size:10px;float:left;margin:15px 0px 10px 10px;width:100%;position:relative;'>"+description+"</div>"
                    +"</div>"
                    +"</div>" ;
    return str;                
}

function changePassword(){
    var obj = new Wtf.changePassWin({});
    obj.show();
 }
 
 Wtf.AddComment = function(config) {
    Wtf.apply(this, config);

     Wtf.AddComment.superclass.constructor.call(this,{
            title :  "Add Comment" ,
            closable : true,
            modal : true,
            iconCls : 'win',
            width : 500,
            height: 250,
            resizable :false,
            buttonAlign : 'right',
            buttons :[{
                text : 'Add Comment',
                scope : this,
                handler : function() {
                    if(this.buttons[0].getText() == "Add Comment" ) {
                        this.Comment.validate();
                        if(!this.Comment.isValid())
                            return;         
                        this.buttons[0].setText("OK");
                        this.buttons[1].hide();
                        var activeFlag = 1;
                        var strg =escape(this.Comment.getValue());
                        Wtf.Ajax.requestEx({
                           url: 'web/phpfiles/tasks.php',
                           params : {
                                mode :  20 ,
                                taskid:this.taskid,
                                comment:strg
                           },
                           method:'POST'},
                           this,
                           function(request,response){
                                var suc = request;
                                if(suc.success == true) {
                                    this.coursebar.reset();
                                    this.coursebar.updateProgress(1.0, "Comment Added" );
                                    var unstrg = this.Comment.getValue();
                                    //var strg=this.Comment.getValue();
                                    //strg=strg.replace(/'/g, "\"");
                                    this.fireEvent('onSuccess',unstrg,Date.parseDate(suc.date,"Y-m-d H:i:s").format("Y-m-d g:i a"));
                                 }
                                else {
                                   this.coursebar.reset();
                                   this.coursebar.updateProgress(1.0,"Error : "+suc.ErrorCode); 
                                }
                                this.buttons[0].enable();
                                this.buttons[1].hide();
                            },                           
                           function(request,response){
                                this.coursebar.reset();
                                this.coursebar.updateText( "Error adding Comment" );
                                this.buttons[0].enable();
                                this.buttons[1].hide();
                        });
                        this.createCourseForm.hide();
                        this.setHeight(210);
                        this.coursebar.show();
                        this.coursebar.wait();
                   }
                   else {
                       this.close();
                   }
                }
            },{
                text : 'Cancel',
                scope : this,
                handler : function() {
                    this.close();
                }
            }],
            layout : 'border',
                        items :[{
                region : 'north',
                height : 75,
                border : false,
                bodyStyle : 'background:white;border-bottom:1px solid #bfbfbf;',
                html : getHeader('web/images/createuser.gif',"Comment","Add comment") 
            },{
                region : 'center',
                border : false,
                bodyStyle : 'background:#f1f1f1;font-size : 10px;padding:20px 20px 20px 20px;',
                layout : 'fit',
                items : [{
                    border : false,
                    bodyStyle : 'background:transparent;',
                    layout : "fit",
                    items : [this.createCourseForm = new Wtf.form.FormPanel({
                        baseCls: 'x-plain',
                        border : false,
                        bodyStyle : 'font-size:10px;',
                        //defaults : {width: 400},
                        lableWidth : 150,
                        defaultType: 'textfield',
                        items : [
                                this.Comment = new Wtf.form.TextArea({
                                    fieldLabel: 'Comment',
                                    name : 'Comment',
                                    maxlength :1024,
                                    invalidText : 'Invalid Comment',
                                    allowBlank:false,
                                    width : 300,
                                    height : 50
                                })]
                       }),this.coursebar = new Wtf.ProgressBar({
                            text:'Adding Comment...',
                            hidden : true
                   })]
                }]
             }]   
       });
   
    this.addEvents = ({
       'onSuccess':true
    });
}

Wtf.extend(Wtf.AddComment, Wtf.Window, {

});

TaskDetail = Wtf.extend(Wtf.Panel, {
	tplMarkup: [
  		'<b><lable style = "color : #15428B;">Task         : </b>  {taskname} &nbsp &nbsp&nbsp&nbsp&nbsp <b><lable style = "color : #15428B;"> Assigned By :</b> {assignbyuser} <br/><br/>',
		'<b>Description : </b>  {description}<br/><br/>',
		'<b>Due Date    : </b>  {enddate}<br/><br/>',
		'<b>Max Credits : </b> {maxcredits}<br/><br/>',
                '<b>Comments    : </b> {Comments}<br/><br/>'
	],
	startingMarkup: 'Please select a task to see details',
	initComponent: function() {
		this.tpl = new Wtf.Template(this.tplMarkup);
                this.bltpl = new Wtf.Template(this.startingMarkup);
		Wtf.apply(this, {
			bodyStyle: {
				background: '#ffffff',
				padding: '10px',
                                fontSize : '12px'
			},
			html: this.startingMarkup
		});
		TaskDetail.superclass.initComponent.call(this);
	},
	updateDetail: function(data) {
		this.tpl.overwrite(this.body, data);		
	},
        blankDetail : function() {
            this.bltpl.overwrite(this.body,"");	
	}
});

Wtf.reg('taskdetail', TaskDetail);

function viewAuditTrail() {
    var auditid = 'audittrail_'+loginid;
    if(!Wtf.getCmp(auditid)) {
        var auditTrail = new Wtf.WtfAuditTrail({
                layout : "fit",
                border : false,
                bodyStyle:"background:none;",
                id : auditid,
                closable : true
            });
        Wtf.getCmp("mainTabPanel").add(auditTrail);
    }    
    Wtf.getCmp("mainTabPanel").setActiveTab(auditid);
    Wtf.getCmp("mainTabPanel").doLayout();
};

function createStatusCombo()
{
     Wtf.statusStore = new Wtf.data.SimpleStore({
        fields: ['id', 'name'],
        data :[['1', '0'],['2', '20'],['3', '40'],['4', '60'],['5', '80'],['6', '100']]
     });

     var localcombo = new Wtf.form.ComboBox({
        fieldLabel: 'Status',
        name: 'status',
        store: Wtf.statusStore,
        emptyText: 'Select status----',
        displayField: 'name',
        valueField: 'id',
        typeAhead: true,
        mode: 'local',
        width: 110,
        forceSelection: true,
        editable: false,
        triggerAction: 'all',
        selectOnFocus: true
     });
     return localcombo;

}
