if(lang==undefined) lang = "ru";
Globalite.setLang(lang);
var RegisterUserForm;

function reloadImg(id) {
    var obj = document.getElementById(id);
    var src = obj.src;
    var pos = src.indexOf('?');
    if (pos >= 0) {
        src = src.substr(0, pos);
    }
    var date = new Date();
    obj.src = src + '?v=' + date.getTime();
    return false;
}
Ext.QuickTips.init();
Ext.form.Field.prototype.msgTarget = 'side';
Ext.form.TextField.prototype.blankText = "ln_field_is_required".l();
Ext.form.VTypes.emailText = "ln_restore_error".l();
Ext.form.VTypes.alphaText = "ln_vtypes_info_1".l();
Ext.form.VTypes.alphanumText  = "ln_vtypes_info_1".l();
Ext.form.Field.prototype.boxMaxWidth = 345;
Ext.override(Ext.form.TextField, {
    filterKeys: function(e) {
        if (e.ctrlKey) {
            return;
        }
        var k = e.getKey();
        if (Ext.isGecko && (e.isNavKeyPress() || k == e.BACKSPACE || (k == e.DELETE && e.button == -1))) {
            return;
        }
        var cc = String.fromCharCode(e.getCharCode());
        if (!Ext.isGecko && e.isSpecialKey() && (Ext.isOpera || !cc)) {
            return;
        }
        if (!this.maskRe.test(cc)) {
            e.stopEvent();
        }
    }
});

Ext.time_zone_set_ar = [
    ['-11','GMT -11:00'],
    ['-10','GMT -10:00'],
    ['-9','GMT -9:00'],
    ['-8','GMT -8:00'],
    ['-7','GMT -7:00'],
    ['-6','GMT -6:00'],
    ['-5','GMT -5:00'],
    ['-4','GMT -4:00'],
    ['-3','GMT -3:00'],
    ['-2','GMT -2:00'],
    ['-1','GMT -1:00'],
    ['0','GMT +0:00'],
    ['1','GMT +1:00'],
    ['2','GMT +2:00'],
    ['3','GMT +3:00'],
    ['4','GMT +4:00'],
    ['5','GMT +5:00'],
    ['6','GMT +6:00'],
    ['7','GMT +7:00'],
    ['8','GMT +8:00'],
    ['9','GMT +9:00'],
    ['10','GMT +10:00'],
    ['11','GMT +11:00'],
    ['12','GMT +12:00'],
    ['13','GMT +13:00'],
    ['14','GMT +14:00']
    ];
	
var time_zone_set_store = new Ext.data.SimpleStore({
    fields: ['value', 'string'],
    data : Ext.time_zone_set_ar
});
	
Ext.register_user_form_class = Ext
    .extend(
        Ext.Window,
        {
            xtype : "window",
            title : "ln_register_new_user".l(),
            id : 'register_user_form',
            width : 410,
            height : 660,
            modal : true,
            floating : true,
            resizable : false,
            closable : false,
            bodyBorder : true,
            border : true,
            shadow : 'frame',
            shadowOffset : 8,
            //layout: 'vbox',
            listeners:{
                'show':function(){
                    if (RegisterUserForm.isnew==false) fillForm();
                }
            },
            initComponent : function() {
                this.items = [
                {
                    xtype: 'tabpanel',
                    activeTab: 0,
                    id: 'tab_form',
                    items: [
                    {
                        xtype: 'panel',
                        title: "profile".l(),
                        layout: 'form',
                        height : 570,
                        frame:true,
                        items: [
                        {
                            xtype : "fieldset",
                            title : '<img src="./images/16x16_profile.png" style="vertical-align: middle;">'+"ln_personal_data".l(),
                            labelAlign : "top",
                            items : [ {
                                xtype : "textfield",
                                fieldLabel : "ln_prof_name".l(),
                                allowBlank : false,
                                vtype : 'alphanum',
                                blankText : "ln_field_is_required".l(),
                                id:"firstname",
                                anchor : "100%"
                            }, {
                                xtype : "textfield",
                                fieldLabel : "ln_prof_last_name".l(),
                                allowBlank : false,
                                vtype : 'alphanum',
                                id:"lastname",
                                anchor : "100%"
                            }, {
                                xtype : "textfield",
                                fieldLabel : "ln_prof_organization".l(),
                                allowBlank : false,
                                vtype : 'alphanum',
                                id:"organization",
                                anchor : "100%"
                            }, {
                                xtype : "numberfield",
                                fieldLabel : "ln_prof_tel".l(),
                                allowBlank : false,
                                id:"phone",
                                anchor : "100%"
                            } ]
                        },
                        {
                            xtype : "fieldset",
                            title : '<img src="./images/16x16lock.png" style="vertical-align: middle;">'+"ln_prof_credention".l(),
                            layout : "form",
                            labelAlign : "top",
                            items : [ {
                                xtype : "textfield",
                                fieldLabel : "ln_prof_user_name".l(),
                                allowBlank : false,
                                vtype : 'latalphanum',
                                id:"user",
                                anchor : "100%"
                            }, {
                                xtype : "textfield",
                                fieldLabel : "ln_prof_password".l(),
                                allowBlank : false,
                                vtype : 'alphanum',
                                inputType:"password",
                                id:"regpass",
                                anchor : "100%"
                            }, {
                                xtype : "textfield",
                                fieldLabel : "ln_prof_confirm_pass".l(),
                                allowBlank : false,
                                vtype : 'alphanum',
                                inputType:"password",
                                id:"cregpass",
                                anchor : "100%"
                            }, {
                                xtype : "textfield",
                                fieldLabel : "ln_prof_email".l(),
                                allowBlank : false,
                                vtype : 'email',
                                id:"email",
                                anchor : "100%"
                            } ]
                        },
                        {
                            xtype : "container",
                            layoutConfig : {
                                align : "stretch"
                            },
                            autoEl : "div",
                            id: 'form_capcha',
                            height : 65,
                            autoHeight : false,
                            autoWidth : false,
                            layout : "hbox",
                            items : [
                            {
                                xtype : "container",
                                autoEl : "div",
                                layout : "form",
                                labelAlign : "top",
                                flex : 1,
                                // width:100,
                                margins : "1",
                                autoHeight : false,

                                items : [ {
                                    xtype : "textfield",
                                    id : "capcha",
                                    allowBlank : false,
                                    vtype : 'latalphanum',
                                    boxMaxWidth:170,
                                    fieldLabel : "ln_enter_verification_code".l()+" <a href=\"#\" onclick=\"reloadImg('capchaimg');\" class=\"kcapcha\"> ("+"ln_prof_next_pic".l()+")</a>",
                                    anchor : "100%",
                                    style : ""
                                } ]
                            },
                            {
                                xtype : "container",
                                autoEl : "div",
                                layout : "form",
                                labelAlign : "top",
                                flex : 1,
                                margins : "1",
                                autoHeight : false,
                                layout : "absolute",
                                stateful:false,
                                items : [ {
                                    xtype : 'label',
                                    x : 5,
                                    html : '<right><img src="./capcha/capcha.php" id="capchaimg" onclick="reloadImg(\'capchaimg\');" style="border:1px #AAAAAA solid"></right>'
                                } ]
                            } ]
                        }
                        ]   
                    },
                         
                    {
                        xtype: 'panel',
                        title: "ln_settings".l(),
                        height : 550,
                        frame:true,
                        layout: 'form',
                        items: [
                        {
                            fieldLabel : "ln_time_zone".l()+" ",
                            xtype: 'combo',
                            forceSelection: true,
                            triggerAction: 'all',
                            editable : false,
                            //emptyText:'Выберите тип отчета',
                            store: time_zone_set_store,
                            id: 'time_zone_set',
                            mode: 'local',
                            displayField: 'string',
                            valueField: 'value'
                        },
                        {
                            xtype : 'combo',
                            fieldLabel : "language".l()+" ",
                            id : 'langTpyeSet',
                            stateful : true,
                            mode : 'local',
                            store : new Ext.data.ArrayStore(
                            {
                                id : 0,
                                fields : ['index','text', 'x_flag' ],
                                data : [
                                ['ru', 'Русский', 'ux-flag-ru'],
                                ['en', 'English',  'ux-flag-en']
                                ]
                            }),
                            width : 105,
                            triggerAction : 'all',
                            valueField : 'index',
                            displayField : 'text',
                            // iconClsField: 'x_flag',
                            editable : false,
                            listeners : {
                                'select' : function(me, rec,index) {
                                    setLang(this.getValue());
                                },
                                'afterrender' : function() {
                                    var curl = getCookie('lang');
                                    if (curl!=null&&curl!='') this.setValue(curl);
                                    else this.setValue('ru');
                                }
                            }
                        }
                        ]
                    }    
                    ]
                },
                                
                {
                    xtype : "container",
                    autoEl : "div",
                    layout : "absolute",
                    height : 30,
                    items : [ {
                        xtype : "button",
                        text : "ln_ok".l(),
                        id:'up_ok_button',
                        height : 25,
                        width : 190,
                        handler : function() {
                            commitForm();
                        }
                    }, {
                        xtype : "button",
                        text : "ln_cancel".l(),
                        height : 25,
                        width : 190,
                        x : 195,
                        handler : function(){
                            RegisterUserForm.close();
                        }
                    } ]
                } ]
                Ext.register_user_form_class.superclass.initComponent
                .call(this);
            }
        });
function fillForm(){
    var m = Ext.Msg.wait("ln_request_data".l(), "ln_please_wait".l());
    var prox = new Ext.data.HttpProxy(
        new Ext.data.Connection({
            method:'POST',
            url : './user_profile.php',
            extraParams:{
                'act':'load'
            }
        }));
    var genres = new Ext.data.Store( {
        reader : new Ext.data.JsonReader( {
            fields : ["firstname", "lastname", "organization", "telno", "login", "user_mail", "time_zone"],
            root : 'userinfo'
        }),
        proxy : prox
    });
    genres.on('load', function(a, records) {
        var rec = records[0];
        if (rec == undefined)
        {
            RegisterUserForm.close();
            delete RegisterUserForm;
            return;
        }
        Ext.getCmp("regpass").setValue("18a6b745a73763d4340524b262792801");
        Ext.getCmp("cregpass").setValue("18a6b745a73763d4340524b262792801");
        Ext.getCmp("firstname").setValue(rec.get("firstname"));
        Ext.getCmp("lastname").setValue(rec.get("lastname"));
        Ext.getCmp("organization").setValue(rec.get("organization"));
        Ext.getCmp("phone").setValue(rec.get("telno"));
        Ext.getCmp("user").setValue(rec.get("login"));
        Ext.getCmp("email").setValue(rec.get("user_mail"));
        Ext.getCmp("time_zone_set").setValue(rec.get("time_zone"));
        RegisterUserForm.enable();
        m.hide();
    });
    genres.load();
    if((tracker_permis == 3)||(tracker_permis == 2))
        Ext.getCmp('up_ok_button').disable(true);
}
function commitForm() {
    var pass = Ext.getCmp("regpass").getValue();
    var cpass = Ext.getCmp("cregpass").getValue();
    if (pass!=cpass)
    {
        Ext.Msg.alert("error".l(), "ln_prof_pass_no_match".l());
        return;
    }
    if(pass.length == 0)
    {
        Ext.Msg.alert("error".l(), "ln_prof_pass_not_empty".l());
        return;
    }
    var data = {};
    data.firstname = Ext.getCmp("firstname").getValue();
    data.lastname = Ext.getCmp("lastname").getValue();
    data.organization = Ext.getCmp("organization").getValue();
    data.phone = Ext.getCmp("phone").getValue();
    data.user = Ext.getCmp("user").getValue();
    data.password = pass;
    data.email = Ext.getCmp("email").getValue();
    data.time_zone = Ext.getCmp("time_zone_set").getValue();
    for(var key in data)
    {
        if (data[key].length == 0)
        {
            Ext.Msg.alert("error".l(), "ln_prof_filled_all_fields".l());
            return;
        }
    }
    var m = Ext.Msg.wait("ln_sending_data_to_server".l(), "ln_please_wait".l());
    var act;
    if(RegisterUserForm.isnew) act = "new"
    else act = "save";
    Ext.Ajax.request( {
        url : './user_profile.php',
        method : 'POST',
        success : function(result) {
            if (result.responseText != "OK") {
                m.hide();
                Ext.Msg.alert("error".l(), result.responseText);
                reloadImg('capchaimg');
                Ext.getCmp("capcha").setValue('');
                return;
            }
            else
            {
                m.hide();
                if (RegisterUserForm.isnew){
                    Ext.Msg.alert("ln_operation_completed_ok".l(), "ln_prof_msg_error_1".l());
                }
                else{
                    Ext.Msg.alert("ln_operation_completed_ok".l(), "ln_prof_inf_update".l());
                }
                RegisterUserForm.close();
            }
        },
        failure : function() {
            m.hide();
            Ext.Msg.alert("error".l(), "ln_service_unavailable".l());

        },
        params : {
            'code' : Ext.getCmp("capcha").getValue(),
            'data' : Ext.util.JSON.encode(data),
            'act' : act
        }
    });
}

function registerUser(isnew) {

    RegisterUserForm = new Ext.register_user_form_class();
    RegisterUserForm.isnew = isnew;
    if (isnew) {
        RegisterUserForm.show('register');
        Ext.getCmp("time_zone_set").setValue(2); 
    }
    else {
        var read_type = "";
        if(anonym_mode) read_type = "ln_prof_read_only".l();
        RegisterUserForm.title = "ln_prof_edit".l()+" "+read_type;
        Ext.getCmp("user").disable();
        Ext.getCmp("email").disable();  
        if(anonym_mode) Ext.getCmp('up_ok_button').disable();
        RegisterUserForm.disable();
        RegisterUserForm.show('editProfile');
        
        if(session_client_id != undefined){
            Ext.getCmp('form_capcha').hide();
            Ext.getCmp('tab_form').setHeight(510);
            RegisterUserForm.setHeight(570);
        }
    }

}
