Ext.ns('Ext.app');
Ext.Direct.addProvider(Ext.app.REMOTING_API);

Ext.onReady(function() {

    var tbUsername = new Ext.form.TextField({
        id: 'tbUsername',
        fieldLabel: 'Username',
        allowBlank: false,
        minLength: '5',
        anchor: '-16'
    });
    
    var tbPassword = new Ext.form.TextField({
        id: 'tbPassword',
        fieldLabel: 'Password',
        allowBlank: false,
        inputType: 'password',
        vtype: 'password',
        minLength: '5',
        anchor: '-16'
    });
    
    var tbPasswordConfirm = new Ext.form.TextField({
        id: 'tbPassword2',
        fieldLabel: 'Conferma Password',
        allowBlank: false,
        inputType: 'password',
        vtype: 'password',
        initialPassField: 'tbPassword',
        anchor: '-16'
    });
    
    var tbNome = new Ext.form.TextField({
        id: 'tbNome',
        fieldLabel: 'Nome',
        allowBlank: false,
        anchor: '-16'
    });
    
    var tbCognome = new Ext.form.TextField({
        id: 'tbCognome',
        fieldLabel: 'Cognome',
        allowBlank: false,
        anchor: '-16'
    });
    
    var tbAzienda = new Ext.form.TextField({
        id: 'tbAzienda',
        fieldLabel: 'Azienda',
        allowBlank: true,
        anchor: '-16'
    });
    
    var tbIndirizzo = new Ext.form.TextField({
        id: 'tbIndirizzo',
        fieldLabel: 'Indirizzo',
        allowBlank: false,
        anchor: '-16'
    });
    
    var tbCap = new Ext.form.TextField({
        id: 'tbCap',
        fieldLabel: 'CAP',
        allowBlank: false,
        maskRe: /[\d\/]/,
        minLength: '5',
        maxLength: '5',
        anchor: '-16'
    });
    
    var tbComune = new Ext.form.TextField({
        id: 'tbComune',
        fieldLabel: 'Comune',
        allowBlank: false,
        anchor: '-16'
    });

    var tbProvincia = new Ext.form.TextField({
        id: 'tbProvincia',
        fieldLabel: 'Provincia',
        allowBlank: false,
        maskRe: /[A-Za-z]/,
        minLength: '2',
        maxLength: '2',
        anchor: '-16'
    });
    
    var tbCodiceFiscale = new Ext.form.TextField({
        id: 'tbCodiceFiscale',
        fieldLabel: 'Codice Fiscale',
        //vtype: 'taxCheck',
        alternateToCheck: 'tbPartitaIva',
        allowBlank: true,
        maskRe: /[A-Za-z0-9]/,
        validator:  function (val) {
            if (Ext.getCmp(this.alternateToCheck).getValue().length == 0 && val.length == 0)
                return false;
            else
            {
                if (val.length < 1)
                    return true;
                    
                re = new RegExp(/^[A-Za-z]{6}[A-Za-z0-9]{2}[A-Za-z][A-Za-z0-9]{2}[A-Za-z][A-Za-z0-9]{3}[A-Za-z]$/);
                re2 = new RegExp(/^[0-9]{11}$/);
                if (re.test(val) || re2.test(val)) {
                    Ext.getCmp(this.alternateToCheck).clearInvalid();
                    return true;
                }
                else
                    return false;
            }
        },
        anchor: '-16'
    });
    
    var tbPartitaIva = new Ext.form.TextField({
        id: 'tbPartitaIva',
        fieldLabel: 'Partita IVA',
        //vtype: 'taxCheck',
        alternateToCheck: 'tbCodiceFiscale',
        allowBlank: true,
        maskRe: /[\d\/]/,
        validator:  function (val) {
            if (Ext.getCmp(this.alternateToCheck).getValue().length == 0 && val.length == 0)
                return false;
            else
            {
                if (val.length < 1)
                    return true;
                    
                re = new RegExp(/[\d\/]{11}/);
                if (re.test(val)) {
                    Ext.getCmp(this.alternateToCheck).clearInvalid();
                    return true;
                }
                else
                    return false;
            }
        },
        anchor: '-16'
    });
    
    var tbEmail = new Ext.form.TextField({
        id: 'tbEmail',
        fieldLabel: 'EMail',
        allowBlank: false,
        anchor: '-16',
        vtype: 'email',
        //emptyText: 'esempio@email.com',
        invalidText: 'Indirizzo email non valido'
    });
    
    var tbPhoneNumber = new Ext.form.TextField({
        id: 'tbPhoneNumber',
        fieldLabel: 'Telefono fisso',
        allowBlank: false,
        maskRe: /[\+\d\/]/,
        maxLength: '20',
        anchor: '-16',
        value: '+39',
        vtype: 'phoneNumber'
    });
    
    var tbAgentCode = new Ext.form.TextField({
        id: 'tbAgentCode',
        fieldLabel: 'Codice Partner',
        //allowBlank: false,
        maskRe: /[A-Za-z0-9]/,
        /*validator:  function (val) {
            re = new RegExp(/[A-Za-z]{3}[\d\/]{4}/);
            if (re.test(val))
                return true;
            else
                return false;
        },*/
        anchor: '-16'
    });
    
    var cbPrivacy = new Ext.form.Checkbox({
        id: 'cbPrivacy',
        boxLabel: 'Accetto <a href=\'/Resources/Informativa-Privacy.pdf\' target=\'_blank\'>l\'informativa sulla privacy</a>',
        anchor: '-16'
    });

    var btnSignUp = new Ext.Button({
        text: 'Registrati',
        iconCls: 'icon-user-add'
    });
    
    var formLogin = new Ext.FormPanel({
        bodyStyle:'padding:5px 5px 0',
        width: 550,
        labelWidth: 130,
        border: false,
        renderTo: Ext.get('divSignUpForm'),
        buttonAlign: 'center',
        items: [
            tbUsername,
            tbPassword,
            tbPasswordConfirm,
            tbNome,
            tbCognome,
            tbAzienda,
            tbIndirizzo,
            tbCap,
            tbComune,
            tbProvincia,
            tbCodiceFiscale,
            tbPartitaIva,
            tbEmail,
            tbPhoneNumber,
            tbAgentCode,
            cbPrivacy
        ],
        buttons: [
            btnSignUp
        ]
    });
    
    btnSignUp.on('click', function(e, t) {
        var formIsValid = formLogin.form.isValid();
       
        if (formIsValid){
            if (!cbPrivacy.getValue()){
                formIsValid = false;
                           
                Ext.MessageBox.show({
                    title: 'Errore',
                    msg: 'E\' necessario accettare l\'informativa della privacy.',
                    buttons: Ext.MessageBox.OK,
                    animEl: 'mb9',
                    icon: 'ext-mb-error'
                });
            }
        }
        
        if (formIsValid){
            var msgMask = Ext.MessageBox.wait('Iscrizione in corso...');
            
            DirectUsers.SignUp(tbUsername.getValue(),
                tbPassword.getValue(),
                tbNome.getValue(),
                tbCognome.getValue(),
                tbAzienda.getValue(),
                tbIndirizzo.getValue(),
                tbComune.getValue(),
                tbCap.getValue(),
                tbProvincia.getValue(),
                tbCodiceFiscale.getValue(),
                tbPartitaIva.getValue(),
                tbEmail.getValue(),
                tbPhoneNumber.getValue(),
                tbAgentCode.getValue(),
                function (response, e){
                    if (response.success)
                    {
                        Ext.MessageBox.show({
                            title: 'Iscrizione',
                            msg: 'Iscrizione completata correttamente. E\' possibile effettuare il login a <a href=\'/Login.aspx\'>questa pagina.</a>',
                            buttons: false,
                            animEl: 'mb9',
                            icon: 'ext-mb-info'
                        });
                    }
                    else{
                        Ext.MessageBox.show({
                            title: 'Errore',
                            msg: response.error,
                            buttons: Ext.MessageBox.OK,
                            animEl: 'mb9',
                            icon: 'ext-mb-error'
                        });
                    }
                });
        }
    });
        
    if (Ext.get("tbAgentCodeDefault").dom.value != '')
    {
        tbAgentCode.setValue(Ext.get("tbAgentCodeDefault").dom.value);
        tbAgentCode.hide();
        tbAgentCode.hide();
    }
});

