/*
 * Ext JS Library 3.0 RC1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 *
 * http://extjs.com/license
 */
var supportTicketWaitingMask;

var thisUserId = 0;
var thisClientId = 0;

var supportTicketWin;
var supportTicketFieldTicketType;
var supportTicketFieldTicketTypeValidate;
var supportTicketFieldSubject;
var supportTicketFieldMessage;
var supportTicketFieldOpenAsUser;
var supportTicketFieldNotifyUser;
var tickettypestore;

Ext.onReady(function(){
// Ticket Type
    tickettypestore = new Ext.data.JsonStore({
        id:             "tickettypestore",
        root:           'tickettypes',
        totalProperty:  'totalcount',
        idProperty:     'id',
        remoteSort:     true,
        baseParams:     {
                            filter: "",
                            customerid: "0"
                        },
        fields:         [
                            'ticketTypeId',
                            'ticketTypeName'
                        ],
        proxy:          new Ext.data.HttpProxy({
                            url:  'index.php?fuse=support&action=GetTicketTypeList&simple=1'
                        }),
        listeners: {
            load: function() {
                supportTicketFieldTicketType.setValue(0);
            }
        }
                
    });
    
    tickettypestore.setDefaultSort('id', 'asc');
    
    supportTicketFieldTicketType = new Ext.form.ComboBox({
        id:               'supportTicketFieldTicketType',
        name:             'supportTicketFieldTicketType',
        typeAhead:        true,
        triggerAction:    'all',
        lazyRender:       true,
        mode:             'local',
        validationEvent:  false,
        anchor:           '100%',
        fieldLabel:       lang("Ticket Type"),
        labelStyle:       'font-weight:bold;font-size:11px;',
        store:            tickettypestore,
        valueField:       'ticketTypeId',
        displayField:     'ticketTypeName',
        value:            0,
        hiddenId:         'supportTicketFieldTicketTypeValue',
        hiddenName:       'supportTicketFieldTicketTypeValue',
        allowBlank:       false,
        blankText:        lang("This field is required"),
        forceSelection:   true,
        editable:         false
    });
    
    supportTicketFieldTicketTypeValidate = function ()
    {
        if(supportTicketFieldTicketType != undefined){
            if(supportTicketFieldTicketType.getValue() != 0){
                supportTicketFieldTicketType.clearInvalid();
                return true;
            }else{
                supportTicketFieldTicketType.markInvalid('This field is required');
                return false;
            }
        }else{
            return false;
        }
    }
    
// Subject
    supportTicketFieldSubject = new Ext.form.TextField({
        id:               'supportTicketFieldSubject',
        name:             'supportTicketFieldSubject',
        fieldLabel:       lang("Subject"),
        labelStyle:       'font-weight:bold;font-size:11px;',
        border:           false,
        validationEvent:  false,
        anchor:           '100%',
        allowBlank:       false,
        blankText:        lang("This field is required")
    });
    
// Message
    supportTicketFieldMessage = new Ext.form.TextArea({
        id:               'supportTicketFieldMessage',
        name:             'supportTicketFieldMessage',
        fieldLabel:       lang("Message"),
        labelStyle:       'font-weight:bold;font-size:11px;',
        border:           false,
        validationEvent:  false,
        anchor:           '100%',
        allowBlank:       false,
        blankText:        lang("This field is required"),
        height:           200,
        value:            ''
    });

// Open ticket in the name of user
    supportTicketFieldOpenAsUser = new Ext.form.Checkbox({
        id:               'supportTicketFieldOpenAsUser',
        name:             'supportTicketFieldOpenAsUser',
        //fieldLabel:       lang("Open ticket in the name of user"),
        hideLabel:        true,
        value:            true,
        validationEvent:  false,
        boxLabel :        lang('Open ticket in the name of user')
    });

// Notify user
    supportTicketFieldNotifyUser = new Ext.form.Checkbox({
        id:               'supportTicketFieldNotifyUser',
        name:             'supportTicketFieldNotifyUser',
        //fieldLabel:       lang("Notify user"),
        hideLabel:        true,
        value:            true,
        validationEvent:  false,
        boxLabel :        lang('Notify user')
    });
    
// FORM
    var simpleForm = new Ext.form.FormPanel({
        frame:        false,
        hideBorders:  true,
        bodyBorder:   false,
        border:       false,
        shadow:       false,
        bodyStyle:    'padding:5px 5px 0',
        floating:     false,
        autoHeight:   true,
        title:        '',
        anchor:           '100%',
        items:        [
            supportTicketFieldTicketType,
            supportTicketFieldSubject,
            supportTicketFieldMessage,
            {
                layout: 'column',
                border: false,
                items:  [
                    {
                        columnWidth:  0.6,
                        layout:       'form',
                        border:       false,
                        items:        [
                            supportTicketFieldOpenAsUser
                        ]
                    },
                    {
                        columnWidth:  0.4,
                        layout:       'form',
                        border:       false,
                        items:        [
                            supportTicketFieldNotifyUser
                        ]
                    }
                ]
            }
        ]
    });
    
    supportTicketWin = new Ext.Window({
        id:           "supportTicketWindow",
        x:            400,
        y:            200,
        labelAlign:   'right',
        layout:       'form',
        width:        700,
        modal:        true,
        autoHeight:   true,
        closeAction:  'hide',
        title:        lang("Add Support Ticket:"),
        plain:        true,
        shadow:       false,
        closable:     true,
        buttonAlign:  'right',
        items:        [
            simpleForm
        ],
        buttons:      [
            {
                id:       'btnAddSupportTicket',
                text:     lang("Save"),
                disabled: false,
                hidden:   false,
                handler:  function(){
                    sendSupportTicket();
                }
            }
        ]
    });
});

function addSupportTicket(UserId, ClientId)
{
    thisUserId = UserId;
    thisClientId = ClientId;
    
    supportTicketFieldSubject.reset();
    supportTicketFieldMessage.reset();
    supportTicketFieldOpenAsUser.setValue(true);
    supportTicketFieldNotifyUser.setValue(true);
    
    supportTicketWin.show();
    
    supportTicketFieldTicketType.focus('',300);

    tickettypestore.load();
}

function sendSupportTicket()
{
    if(supportTicketFieldTicketTypeValidate() //if(supportTicketFieldTicketType.validate()
      && supportTicketFieldSubject.validate()
      && supportTicketFieldMessage.validate()){
        showSupportTicketWaitingMask();

        Ext.Ajax.request({
           url: 'index.php?fuse=support&action=SaveNewSupportTicket',
           success: function(t) {
                onCompleteSendSupportTicket(t);
           },
           params: {mode:"extjs",
                   userid:thisUserId,
                   clientid:thisClientId,
                   domain:0,
                   messagetype:supportTicketFieldTicketType.getValue(),
                   priority:3,
                   subject:supportTicketFieldSubject.getValue(),
                   message:supportTicketFieldMessage.getValue(),
                   inNameOfUser:supportTicketFieldOpenAsUser.getValue(),
                   notifyUser:supportTicketFieldNotifyUser.getValue()
                  }
        });


    }else{
        message = '';
        newline = "<br>";
        
        if(!supportTicketFieldTicketTypeValidate()){ //if(!supportTicketFieldTicketType.validate()){
            if(message != ''){
                message += newline;
            }
            
            message += lang("Ticket Type")
        }
        
        if(!supportTicketFieldSubject.validate()){
            if(message != ''){
                message += newline;
            }
            
            message += lang("Subject")
        }
        
        if(!supportTicketFieldMessage.validate()){
            if(message != ''){
                message += newline;
            }
            
            message += lang("Message")
        }
        
        if(message != ''){
            Ext.MessageBox.alert(lang("Errors"), lang("The following form field(s) were incomplete or incorrect") + ":<br><br>" + message + "<br><br>" + lang("Please complete or correct the form and submit again."));
            return;
        }
    }
}

function onCompleteSendSupportTicket(responseObj)
{
    respArr = responseObj.responseText.split("|");
    responseStr = respArr[0];
    
    if(responseStr == 'closeWindow'){
        window.location = "index.php?fuse=support&view=Admin_ViewTicketMessage&ticketID=" + respArr[1] + "&frmClientID=" + respArr[2];
        //supportTicketWin.hide();
    }else{
        hideSupportTicketWaitingMask();
        responseStr = responseObj.responseText;
        Ext.MessageBox.alert(lang("Errors"), responseStr);
    }
}

function showSupportTicketWaitingMask(){
    supportTicketWaitingMask = new Ext.LoadMask('supportTicketWindow', {msg:"Please wait..."});
    
    supportTicketWaitingMask.show();
}

function hideSupportTicketWaitingMask(){
    supportTicketWaitingMask.hide();
}