var webURL = 'http://www.casashare.it';

var dsRegioni = new Ext.data.Store({
        url: webURL + '/new_desk/ajax/utility/loadRegioni.php',
        reader: new Ext.data.JsonReader({
            root: 'regioni',
            fields: [
               {name: 'id'},
               {name: 'nome'}
            ]
        }),
        autoLoad: true
    });

var dsProvincie = new Ext.data.Store({
	      baseParams: {idRegione: 0},
        url: webURL + '/new_desk/ajax/utility/loadProvincie.php',
        reader: new Ext.data.JsonReader({
            root: 'provincie',
            fields: [
               {name: 'id'},
               {name: 'nome'}
            ]
        })
    });
    
var dsComuni = new Ext.data.Store({
	      baseParams: {idProvincia: 0},
        url: webURL + '/new_desk/ajax/utility/loadComuni.php',
        reader: new Ext.data.JsonReader({
            root: 'comuni',
            fields: [
               {name: 'id'},
               {name: 'nome'}
            ]
        })
    });

function fillProvincie(combo, record, index)
{
	 dsProvincie.setBaseParam('idRegione', combo.getValue());
	 dsProvincie.load();
	 
	 Ext.getCmp('provinciaLista').clearValue();
	 Ext.getCmp('comuneLista').clearValue();
}

function fillComuni(combo, record, index)
{
	 dsComuni.setBaseParam('idProvincia', combo.getValue());
	 dsComuni.load();

	 Ext.getCmp('comuneLista').clearValue();
}

function showRegisterBox()
{
	    win = new Ext.form.FormPanel({
                id: 'register-form',
                renderTo: 'boxIscrizione',
                border: false,
                bodyCssClass: 'background: transparent;',
                bodyStyle: 'background: transparent;',
                items:[
                        {
                   	         xtype: 'textfield',
                   	         name: 'ragsoc',
                   	         fieldLabel: '&nbsp;&nbsp;Ragione Sociale',
                             allowBlank: false,
                             blankText: 'Devi inserire una Ragione Sociale.',
                   	         width: 190,
                   	         tabIndex: 1
                   	    },
                   	    {
                   	         xtype: 'combo',
                   	         hiddenName: 'regione',
                   	         fieldLabel: '&nbsp;&nbsp;Regione',
                             mode: 'local',
                             emptyText: 'Seleziona una regione ...',
                             allowBlank: false,
                             blankText: 'Devi selezionare una Regione.',
                             valueField: 'id',
                             displayField: 'nome',
                             store: dsRegioni,
                             width: 190,
                   	         tabIndex: 2,
                             listeners: {
                                'select': fillProvincie
                             }
                        },
                        {
                   	         xtype: 'combo',
                   	         id: 'provinciaLista',
                   	         hiddenName: 'provincia',
                   	         fieldLabel: '&nbsp;&nbsp;Provincia',
                             mode: 'local',
                             emptyText: 'Seleziona una provincia ...',
                             allowBlank: false,
                             blankText: 'Devi selezionare una Provincia.',
                             valueField: 'id',
                             displayField: 'nome',
                             store: dsProvincie,
                             width: 190,
                   	         tabIndex: 3,
                             listeners: {
                                'select': fillComuni
                             }
                        },
                        {
                   	         xtype: 'combo',
                    	       id: 'comuneLista',
                    	       hiddenName: 'comune',
                   	         fieldLabel: '&nbsp;&nbsp;Comune',
                             mode: 'local',
                             emptyText: 'Seleziona un comune ...',
                             allowBlank: false,
                             blankText: 'Devi selezionare un Comune.',
                             valueField: 'id',
                             displayField: 'nome',
                             store: dsComuni,
                             width: 190,
                   	         tabIndex: 4
                        },
                        {
                   	         xtype: 'textfield',
                   	         fieldLabel: '&nbsp;&nbsp;Cognome',
                   	         name: 'cognome',
                             allowBlank: false,
                             blankText: 'Devi inserire il cognome di un referente.',
                   	         width: 190,
                   	         tabIndex: 5
                          },
                          {
                   	         xtype: 'textfield',
                   	         fieldLabel: '&nbsp;&nbsp;Nome',
                   	         name: 'nome',
                             allowBlank: false,
                             blankText: 'Devi inserire il nome di un referente.',
                   	         width: 190,
                   	         tabIndex: 6
                   	    },
                        {
                   	         xtype: 'textfield',
                   	         fieldLabel: '&nbsp;&nbsp;Telefono',
                   	         name: 'telefono',
                             allowBlank: false,
                             blankText: 'Devi inserire un numero di telefono.',
                             maskRe: /[0-9]/,
                             width: 190,
                   	         tabIndex: 7
                          },
                          {
                   	         xtype: 'textfield',
                   	         fieldLabel: '&nbsp;&nbsp;Cellulare',
                   	         name: 'cellulare',
                             allowBlank: false,
                             blankText: 'Devi inserire un numero di cellulare.',
                             maskRe: /[0-9]/,
                             width: 190,
                   	         tabIndex: 8
                        },
                        {
                   	         xtype: 'textfield',
                   	         fieldLabel: '&nbsp;&nbsp;E-Mail',
                   	         name: 'email',
                             allowBlank: false,
                             blankText: 'Devi inserire una mail valida.',
                             vtype: 'email',
                   	         width: 190,
                   	         tabIndex: 9
                          },
                          {
                   	         xtype: 'textfield',
                   	         fieldLabel: '&nbsp;&nbsp;Password',
                   	         name: 'password',
                             hidden: false,
                             allowBlank: false,
                             blankText: 'Devi inserire una password.',
                             inputType: 'password',
                   	         width: 190,
                   	         tabIndex: 10
                        }
                   	 ]
            });
}

function btnRegisterClick()
{
	 var url2call = webURL + '/ajax/mail/sendRegReqMail.php';
	 var msg2show = 'La sua richiesta &grave; stata inoltrata. Ricever&agrave; quanto prima User e Password. Grazie!';
   
   var frm = Ext.getCmp('register-form');
   if( frm.getForm().isValid() )
   {
   	  frm.getForm().submit({
         url: url2call,
         params: {idAge: 0},
         waitMsg: 'Sto inoltrando la richiesta ...',
         success: function(form, action){
         	           var result = action.result;
         	           if( result.errorTxt == 'OK' )
         	           {
       		   			      alert(msg2show);
       					        frm.getForm().reset();
       					     }
       					     else
       		   			      alert(result.errorTxt);
                  },
         failure: function(form, action){
        	           alert(action.response.errorTxt);
        	        }
      });
   }
   else
   	  alert('Ci sono dei campi OBBLIGATORI vuoti o errati!');
}

var tipoContratto = new Ext.data.ArrayStore({
    fields: ['id', 'codice', 'descrizione'],
    idIndex: 0
});

var tipoData = [ [1, 'A', 'Agenzia Immobiliare'],
                 [2, 'M', 'Mediatore Creditizio'] ];

tipoContratto.loadData(tipoData);

function showRegisterBoxPartner()
{
	    win = new Ext.form.FormPanel({
                id: 'register-form',
                width: 360,
                shim: false,
                animCollapse: false,
                border: false,
                frame: false,
                bodyCssClass: 'background: transparent;',
                bodyStyle: 'background: transparent;',
                constrainHeader: true,
                layout: 'form',
                title: '',
                items:[
                   	    {
                   	      xtype: 'combo',
                   	      name: 'tipo',
                   	      hiddenName: 'tipo_contratto',
                   	      store: tipoContratto,
                   	      mode: 'local',
                          valueField: 'codice',
                          displayField: 'descrizione',
                          forceSelection: true,
                          fieldLabel: '&nbsp;&nbsp;Tipologia',
                          allowBlank: false,
                          blankText: 'Devi selezionare una Tipologia.',
                   	      width: 250,
                   	      tabIndex: 1
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'ragsoc',
                          fieldLabel: '&nbsp;&nbsp;Ragione Sociale',
                          allowBlank: false,
                          blankText: 'Devi inserire una Ragione Sociale.',
                   	      width: 250,
                   	      tabIndex: 2
                   	    },
                   	    {
                   	      xtype: 'combo',
                   	      name: 'regioneLista',
                   	      hiddenName: 'regione',
                          mode: 'local',
                          focusClass: '',
                          fieldLabel: '&nbsp;&nbsp;Regione',
                          forceSelection: true,
                          emptyText: 'Seleziona una regione ...',
                          allowBlank: false,
                          blankText: 'Devi selezionare una Regione.',
                          valueField: 'id',
                          displayField: 'nome',
                          store: dsRegioni,
                   	      tabIndex: 3,
                          listeners: {
                             'select': fillProvincie
                          }
                        },
                        {
                   	      xtype: 'combo',
                   	      name: 'provinciaLista',
                   	      hiddenName: 'provincia',
                          mode: 'local',
                          focusClass: '',
                          fieldLabel: '&nbsp;&nbsp;Provincia',
                          forceSelection: true,
                          emptyText: 'Seleziona una provincia ...',
                          allowBlank: false,
                          blankText: 'Devi selezionare una Provincia.',
                          valueField: 'id',
                          displayField: 'nome',
                          store: dsProvincie,
                   	      tabIndex: 4,
                          listeners: {
                             'select': fillComuni
                          }
                        },
                        {
                   	      xtype: 'combo',
                    	    name: 'comuneLista',
                    	    hiddenName: 'comune',
                          mode: 'local',
                          focusClass: '',
                          fieldLabel: '&nbsp;&nbsp;Comune',
                          forceSelection: true,
                          emptyText: 'Seleziona un comune ...',
                          allowBlank: false,
                          blankText: 'Devi selezionare un Comune.',
                          valueField: 'id',
                          displayField: 'nome',
                          store: dsComuni,
                   	      tabIndex: 5
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'indirizzo',
                          fieldLabel: '&nbsp;&nbsp;Indirizzo',
                          allowBlank: false,
                          blankText: 'Devi inserire un indirizzo.',
                   	      width: 250,
                   	      tabIndex: 6
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'cap',
                          fieldLabel: '&nbsp;&nbsp;CAP',
                          allowBlank: false,
                          blankText: 'Devi inserire un CAP.',
                          width: 50,
                          maskRe: /[0-9]/,
                   	      tabIndex: 7
                   	    },
                        {
                   	      xtype: 'textfield',
                   	      name: 'piva',
                   	      width: 100,
                          fieldLabel: '&nbsp;&nbsp;P. IVA',
                          allowBlank: false,
                          blankText: 'Devi inserire una Partita Iva.',
                          maskRe: /[0-9]/,
                   	      tabIndex: 8
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'codfis',
                          fieldLabel: '&nbsp;&nbsp;Cod. Fis.',
                          allowBlank: false,
                          blankText: 'Devi inserire un Codice Fiscale<br />(se assente ripetere la P.IVA).',
                          vtype: 'alphanum',
                   	      tabIndex: 9
                   	    },
                        {
                   	      xtype: 'textfield',
                   	      name: 'cognome',
                          fieldLabel: '&nbsp;&nbsp;Cognome',
                          allowBlank: false,
                          blankText: 'Devi inserire il cognome di un referente.',
                   	      width: 250,
                   	      tabIndex: 10
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'nome',
                          fieldLabel: '&nbsp;&nbsp;Nome',
                          allowBlank: false,
                          blankText: 'Devi inserire il nome di un referente.',
                   	      width: 250,
                   	      tabIndex: 11
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'telefono',
                          fieldLabel: '&nbsp;&nbsp;Telefono',
                          allowBlank: false,
                          blankText: 'Devi inserire un numero di telefono.',
                          maskRe: /[0-9]/,
                   	      tabIndex: 12
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'cellulare',
                          fieldLabel: '&nbsp;&nbsp;Cellulare',
                          allowBlank: false,
                          blankText: 'Devi inserire un numero di cellulare.',
                          maskRe: /[0-9]/,
                   	      tabIndex: 13
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'fax',
                          fieldLabel: '&nbsp;&nbsp;Fax',
                          maskRe: /[0-9]/,
                   	      tabIndex: 14
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'email',
                          fieldLabel: '&nbsp;&nbsp;E-Mail',
                          allowBlank: false,
                          blankText: 'Devi inserire una mail valida.',
                          vtype: 'email',
                   	      width: 250,
                   	      tabIndex: 15
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'password',
                          fieldLabel: '&nbsp;&nbsp;Password',
                          hidden: true,
                          allowBlank: true,
                          blankText: 'Devi inserire una password.',
                          inputType: 'password',
                   	      width: 100,
                   	      tabIndex: 16
                        },
                        {
                   	      xtype: 'textfield',
                   	      name: 'emailInvito',
                          fieldLabel: '&nbsp;&nbsp;Invitato da',
                          hidden: true,
                          allowBlank: true,
                          blankText: 'Inserisci la mail di chi ti ha invitato.',
                          vtype: 'email',
                   	      width: 250,
                   	      tabIndex: 17
                        }
                   	 ]
            });
      
 	 return win;
}

function btnRegisterClickPartner(idAgeP)
{
	 var url2call = webURL + '/ajax/mail/sendRegReqMailPartner.php';
	 var msg2show = 'Iscrizione inoltrata. Quanto prima arriver&agrave; la User e Password alla mail di riferimento indicata. Grazie.';
   
   var frm = Ext.getCmp('register-form');
   if( frm.getForm().isValid() )
   {
   	  frm.getForm().submit({
         url: url2call,
         params: {idAge: idAgeP},
         waitMsg: 'Sto inoltrando la richiesta ...',
         success: function(form, action){
         	           var result = action.result;
         	           if( result.errorTxt == 'OK' )
         	           {
       		   			      alert(msg2show);
       					        frm.getForm().reset();
       					     }
       					     else
       		   			      alert(result.errorTxt);
                  },
         failure: function(form, action){
        	           alert(action.response.errorTxt);
        	        }
      });
   }
   else
   	  alert('Ci sono dei campi OBBLIGATORI vuoti o errati!');
}

