/*
 * Ext JS Library 2.1
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
// Add the additional 'advanced' VTypes
var mindata = new Date();

var maxdata = mindata.add(Date.DAY, 15);

Ext.apply(Ext.form.VTypes, {
  daterange: function(val, field) {
    var date = field.parseDate(val);
	var start=Ext.getCmp('startdt').getValue()
	var end=Ext.getCmp('enddt').getValue()
	if(end){
	var datadiff=(end-start)/(24*60*60*1000);
	if(datadiff>15){
	//messaggio(Ext.MessageBox.WARNING, "Per un periodo superiore a 15 giorni bisogna essere clienti di Ideasalento.com", "Periodo");
	Ext.getCmp('startdt').invalidText= 'Non si possono superare 15 giorni per chi non è cliente di Ideasalento'
	return false
	}
	}

    if(!date){ 
            //return; // <-- existing code 
            return true; // <--- change to this 
        } 


    // We need to force the picker to update values to recaluate the disabled dates display
    var dispUpd = function(picker) {
      var ad = picker.activeDate;
      picker.activeDate = null;
      picker.update(ad);
    };
    
    if (field.startDateField) {
		
      var sd = Ext.getCmp(field.startDateField);
      //sd.maxValue = date;
      if (sd.menu && sd.menu.picker) {
        //sd.menu.picker.maxDate = date;
        dispUpd(sd.menu.picker);
      }
    } else if (field.endDateField) {
      var ed = Ext.getCmp(field.endDateField);
      ed.minValue = date;
	  ed.maxValue = date.add(Date.DAY, 15);//
	  
	   if (ed.menu && ed.menu.picker) {
        ed.menu.picker.minDate = date;
		ed.menu.picker.maxDate = date.add(Date.DAY, 15);//
        dispUpd(ed.menu.picker);
      }
    }
    /* Always return true since we're only using this vtype
     * to set the min/max allowed values (these are tested
     * for after the vtype test)
     */

    return true;
  }
  
 //--------

	 
});


Ext.onReady(function(){
var showToggle = function fnCallback(e){
if(e.value == '2') {
date_fieldset.show();
}
else {
alert("aslfjsadfklsjf");
//date_fieldset.hide();
}
}

var checkHandler = function(checkbox, checked){
     if (checked){
      date_fieldset.show();
     } else {
      date_fieldset.hide();
     }
}


    Ext.QuickTips.init();

    // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';

    var bd = Ext.getBody();
	
	

    /*
     * ================  Simple form  =======================
     */
	
        xstore = new Ext.data.SimpleStore({
        fields: ['abbr', 'state', 'nick'],
        data : Ext.exampledata.states // from states.js
        });

    var simple = new Ext.form.FormPanel({
	 monitorValid:true,
        labelWidth: 110, //  75 - label settings here cascade unless overridden
        url:'infosms-conferma.asp',
		//standardSubmit: true,
		id: 'formtest',
		method: 'POST',
        frame:true,
        title: 'REGISTRAZIONE A INFO SALENTO SMS',
        bodyStyle:'padding:5px 5px 0',
        width: 510,
        //defaults: {width: 270},
        defaultType: 'textfield',
		renderTo: 'formservizi',
		

		
		//name:'provaform',

        items: [
			 {
            xtype:'fieldset',
			id: 'fieldcheck1',
			name :'fieldcheckx',
            checkboxToggle:true,
            title: 'Residente nel Salento',
            autoHeight:true,
			//width: 450,
            //defaults: {width: 250},
            defaultType: 'textfield',
			
            collapsed: true,
			listeners:{'beforeexpand': function(){
						//alert("asjldfjasdjf");
						simple.form.reset();
						Ext.getCmp('fieldcheck2').disable();
						
						disabilita(this.id);
						
						},
						
						'beforecollapse': function(){
						
						Ext.getCmp('fieldcheck2').enable();
						}
			},
            items :[
			
{
xtype:'label',
//width: 380,
height: 35,
html: '<b>Se sei residente in una località del Salento, compila questa sezione.</b>'
},

			
			
			
                    new Ext.form.ComboBox({
            				id:'ComboBoxStato',
							name:'ComboBoxStato',
                        fieldLabel: 'Località residenza',
                        hiddenName:'localita1',
						 store: new Ext.data.SimpleStore({
                            fields: ['abbr', 'state'],
                            data : Ext.exampledata.states // from states.js
                        }),
                        valueField:'abbr',
                        displayField:'state',
                        typeAhead: true,
                        mode: 'local',
                        triggerAction: 'all',
                        emptyText:'Seleziona una località...',
                        selectOnFocus:true,
                        width:190,
						msgTarget: 'under',
						itemCls: 'required',
                        editable:false,
                        allowBlank:false, //aggiunto dopo
                        forceSelection:true //aggiunto dopo
						}),

                {
                    fieldLabel: 'Cognome',
					width:190,
					id:'cognome1',
					//maskRe:/^[a-zA-Z]{1,}$/,
					
                    name: 'cognome1'
                },{
                    fieldLabel: 'Nome',
					width:190,
					id:'nome1',
                    name: 'nome1'
                }, {
                    fieldLabel: 'Email',
					width:190,
					id:'email1',
					//value: 'info@mediameeting.it',
                    name: 'email1',
					allowBlank:false,
					//validationEvent: false,
					//validateOnBlur: false,
					emptyText:'tuaEmail@dominio.com',
					msgTarget: 'under',
					itemCls: 'required',
					
                    vtype:'email'
                },
	{
xtype:'label',
//width: 380,
//height: 40,
html: '<font color="#FF0000"><i>Attenzione: indicare una email valida per ricevere la conferma della registrazione al servizio.</i></font>'
},			
				
				{
xtype:'label',
//width: 380,
height: 40,
html: '<hr><b>Indicare  se si vuole ricevere sms riguardanti tutto il Salento o vicini alla località di residenza.</b>'
},
 {
                    xtype:'radio',
					boxLabel: 'Tutto il salento',
                    id: 'tuttosalento',
					hideLabel: true,
					inputValue: '1',
					name: 'tuttosalento',
					checkHandler : checkHandler,
					
					checked: true,
					listeners:{
					'check': function(cb, checked) {
					
					
if(checked){

Ext.getCmp('kmMaxResidenza').hide();
Ext.getCmp('km1').disable();
//Ext.getCmp('kmMaxResidenza').setDisabled(true);

}else{
Ext.getCmp('kmMaxResidenza').show();
Ext.getCmp('km1').enable();
//Ext.getCmp('kmMaxResidenza').setDisabled(false);
}
}
}
					
                    //vtype:'radio'
    },
	{
                    xtype:'radio',
					boxLabel: 'Vicino località di residenza',
                    id: 'tuttosalento2',
					hideLabel: true,
					inputValue: '2',
					name: 'tuttosalento',
					listeners:{
					'check': function(cb, checked) {
					
if(checked){

Ext.getCmp('kmMaxResidenza').show();
Ext.getCmp('km1').enable();
//Ext.getCmp('kmMaxResidenza').setDisabled(false);

}else{
Ext.getCmp('kmMaxResidenza').hide();
Ext.getCmp('km1').disable();
//Ext.getCmp('kmMaxResidenza').setDisabled(true);

}
}
}




            //vtype:'radio'
    },
	
	
	

 new Ext.Panel({
		//renderTo: 'tuttosalento2',
		//frame: true,
		//title: 'Attenzione',
		//width: 540,
		//height: 20,
		id:'kmMaxResidenza',
		region:'center',
		layout: 'form',
		items: [
		
			{

                    //xtype:'textfield',
					id: 'km1',
					xtype:'numberfield',
					//disabled: true,
					//vtype: 'km',
					allowDecimals: false,
					allowNegative: false,
					allowBlank:false,
					itemCls: 'required',
									
					fieldLabel: 'Km massimo',
					//maskRe:/^\d+$/,
					//minLength:0,
					msgTarget: 'under',
					maxValue:30,
					minValue:10,
					maxText: 'Inserire massimo 30 km',
					minText: 'Inserire minimo 10 km',
					
                    name: 'kmMaxdiResidenza'
	//}]
	}]
	}),
	
				{
xtype:'label',
//width: 380,
height: 35,
html: '<hr><b>Inserire il numero cellulare dove si desidera ricevere gli InfoSMS.</b>'
},
	 {
                    fieldLabel: 'Numero Cellulare',
					width:190,
					id:'cellulareN1',
					xtype:'numberfield',
					allowDecimals: false,
					allowNegative: false,
					allowBlank:false,
					itemCls: 'required',
					msgTarget: 'under',

					//maskRe:/^\d+$/,
					//maskRe:/^[+]?\d*$/,
					
					//allowBlank:false,
					minLength:7,
					minLengthText:'Il numero cell deve avere minimo 7 cifre',
					maxLength:15,
					maxLengthText:'Attenzione hai superato il numero max di cifre',
					//validationEvent: false,
					//validateOnBlur: false,
					
					//vtype: 'cellulare',
                    name: 'cellulareN1'
					
					
                    //vtype:'email'
                }
					
      
	



            ]
        },
		   {
            xtype:'fieldset',
			id: 'fieldcheck2',
			name :'fieldcheckx2',
            checkboxToggle:true,
            title: 'In vacanza nel Salento',
            autoHeight:true,
			//width: 450,
            //defaults: {width: 250},
            defaultType: 'textfield',
			
            collapsed: true,
			listeners:{'beforeexpand': function(){
						simple.form.reset();
						Ext.getCmp('fieldcheck1').disable();
						disabilita(this.id);
						
						},
						
						'beforecollapse': function(){
						Ext.getCmp('fieldcheck1').enable();
						}
			},
            items :[
			
{
xtype:'label',
//width: 380,
height: 35,
html: '<b>Se sei in vacanza in una località del Salento, compila questa sezione.</b>'
},

			
			
			
                    new Ext.form.ComboBox({
            				id:'ComboBoxStato2',
							name:'ComboBoxStato2',
                        fieldLabel: 'Località vacanza',
                        hiddenName:'localita',
						 store: new Ext.data.SimpleStore({
                            fields: ['abbr', 'state'],
                            data : Ext.exampledata.states // from states.js
                        }),
                        valueField:'abbr',
                        displayField:'state',
                        typeAhead: true,
                        mode: 'local',
                        triggerAction: 'all',
                        emptyText:'Seleziona una località...',
                        selectOnFocus:true,
                        width:190,
						itemCls: 'required',
					msgTarget: 'under',
                        editable:false,
                        allowBlank:false, //aggiunto dopo
                        forceSelection:true //aggiunto dopo
						}),

                {
                    fieldLabel: 'Cognome',
					id:'cognome',
					width:190,
                    name: 'cognome'
                },{
                    fieldLabel: 'Nome',
					id:'nome',
					width:190,
                    name: 'nome'
                }, {
                    fieldLabel: 'Email',
					width:190,
					id:'email',
                    name: 'email',
					allowBlank:false,
					itemCls: 'required',
					msgTarget: 'under',
					emptyText:'tuaEmail@dominio.com',
                    vtype:'email'
                },
{
xtype:'label',
//width: 380,
//height: 40,
html: '<font color="#FF0000"><i>Attenzione: indicare una email valida per ricevere la conferma della registrazione al servizio.</i></font>'
},

{
xtype:'label',
//width: 380,
height: 40,
html: '<hr><b>Indicare  se si vuole ricevere sms riguardanti tutto il Salento o vicini alla località dove si è in vacanza.</b>'
},
 {
                    xtype:'radio',
					boxLabel: 'Tutto il salento',
                    id: 'tuttosalento3',
					hideLabel: true,
					inputValue: '1',
					name: 'tuttosalento2',
					checkHandler : checkHandler,
					
					checked: true,
					listeners:{
					'check': function(cb, checked) {
if(checked){
Ext.getCmp('kmMaxVacanza').hide();
Ext.getCmp('km2').disable();
}else{
Ext.getCmp('kmMaxVacanza').show();
Ext.getCmp('km2').enable();
}
}
}
					
                    //vtype:'radio'
    },
	{
                    xtype:'radio',
					boxLabel: 'Vicino località vacanza',
                    id: 'tuttosalento4',
					hideLabel: true,
					inputValue: '2',
					name: 'tuttosalento2',
					listeners:{
					'check': function(cb, checked) {
if(checked){
Ext.getCmp('kmMaxVacanza').show();
Ext.getCmp('km2').enable();
}else{
Ext.getCmp('kmMaxVacanza').hide();
Ext.getCmp('km2').disable();
}
}
}




    },
	
	
	

 new Ext.Panel({
		//renderTo: 'tuttosalento2',
		//frame: true,
		//title: 'Attenzione',
		//width: 540,
		//height: 20,
		id:'kmMaxVacanza',
		region:'center',
		layout: 'form',
		items: [
		
			{

                    //xtype:"textfield",
					id: 'km2',				
					fieldLabel: 'Km massimo',
					xtype:'numberfield',
					//vtype: 'km',
					allowDecimals: false,
					allowNegative: false,
					allowBlank:false,
					
					itemCls: 'required',
					msgTarget: 'under',
					//maskRe:/^\d+$/,
					//maxLength:2,
					//width: 3,
					maxValue:30,
					minValue:10,
					maxText: 'Inserire massimo 30 km',
					minText: 'Inserire minimo 10 km',
					//validationEvent: false,
					//validateOnBlur: false,
					
					//disabled: true,
                    name: 'kmMaxInVacanza'
	//}]
	}]
	}),
	
{
xtype:'label',
//width: 380,
height: 35,
html: '<hr><b>Inserire qui il periodo della vacanza.</b>'
},
	
{
xtype:'label',
//width: 380,
height: 35,
html: '<br><i>Massimo 15 giorni per chi non è cliente di Ideasalento.com.</i>'
},
{
        xtype:'datefield',
		allowBlank:false,
		fieldLabel: 'Inizio vacanza',
        name: 'startdt',
        id: 'startdt',
		itemCls: 'required',
		msgTarget: 'under',
		blankText: 'Campo richiesto',
        vtype: 'daterange',
		//invalidText: 'Inserire un periodo valido',
		//validationEvent: false,
		
		//validateOnBlur: false,
		minValue: mindata,
		emptyText: 'dd/mm/aa',
		
		/*listeners:{'blur': function(field){
	var date=field.parseDate(this.value);
		
        Ext.getCmp('enddt').minValue = date.add(date.DAY, 1);
		Ext.getCmp('enddt').maxValue = date.add(date.DAY, 15);
        if (Ext.getCmp('enddt').menu) {
            Ext.getCmp('enddt').menu.picker.minDate = Ext.getCmp('startdt').getValue();
            delete Ext.getCmp('enddt').menu.picker.activeDate; // Clear active date
            Ext.getCmp('enddt').menu.picker.setValue(date.add(Ext.getCmp('enddt').getValue(), 15)); // So that this recalcs!
			
        }
    if (Ext.getCmp('startdt').menu) {
		
            Ext.getCmp('startdt').menu.picker.maxDate = Ext.getCmp('enddt').getValue();
            delete Ext.getCmp('startdt').menu.picker.activeDate;
            Ext.getCmp('startdt').menu.picker.setValue(Ext.getCmp('startdt').getValue());
        }
	
		}
		},*/


        endDateField: 'enddt' // id of the end date field
      },{
	  xtype:'datefield',
	  allowBlank:false,
        fieldLabel: 'Fine vacanza',
        name: 'enddt',
        id: 'enddt',
		itemCls: 'required',
		msgTarget: 'under',
        vtype: 'daterange',
		//validationEvent: false,
		//validateOnBlur: false,
		maxValue: maxdata,
		minValue: mindata,
		emptyText: 'dd/mm/aa',
		
		
		/*listeners:{'blur': function(field){
		if (Ext.getCmp('startdt').menu) {
		
            Ext.getCmp('startdt').menu.picker.maxDate = Ext.getCmp('enddt').getValue();
            delete Ext.getCmp('startdt').menu.picker.activeDate;
            Ext.getCmp('startdt').menu.picker.setValue(Ext.getCmp('startdt').getValue());
        }
    
	
		}
		},*/
        startDateField: 'startdt' // id of the start date field
      },

	
				{
xtype:'label',
//width: 380,
height: 35,
html: '<hr><b>Inserire il numero cellulare dove si desidera ricevere gli InfoSMS.</b>'
},
	 {
                    fieldLabel: 'Numero Cellulare',
					xtype:'numberfield',
					allowDecimals: false,
					allowNegative: false,
					width:190,
					id:'cellulareN',
					allowBlank:false,
					itemCls: 'required',
					msgTarget: 'under',
					//maskRe:/^[+]?\d*$/,
					minLength:7,
					minLengthText:'Il numero cell deve avere minimo 7 numeri',
					maxLength:15,
					maxLengthText:'Attenzione hai superato il numero max di caratteri',
					
					
					name: 'cellulareN'
                    //vtype:'email'
                }
					
      
	



            ]
        }
			       
		
        ],

        buttons: [{
		type:'submit',
		
		formBind: true,
            text: 'Registra',
			
handler: function() {

simple.form.submit({

waitMsg:'Saving Data...',
success: function(form, action) {


Ext.MessageBox.confirm('Conferma', 'Confermi i dati inseriti?', salva);

},
failure: function(form, action) {
var response = Ext.util.JSON.decode(action.response.responseText);
var messaggio=response.data.inserito;
Ext.MessageBox.show({
   title:'ATTENZIONE',
   msg: messaggio,
   buttons: Ext.MessageBox.OK,
   icon: Ext.MessageBox.WARNING
});
}

}); 
}
        },{
            text: 'Cancella',
			handler: function(){
			simple.form.reset();
			}
        }]
    });

	
function salva(btn) {
if(btn=="yes"){

simple.form.submit({
url : 'infosms-conferma2.asp',
//params: dati,
waitMsg:'Saving Data...',
success: function(form, action) {
var response = Ext.util.JSON.decode(action.response.responseText);
location.href ='infosms-confermato.asp';


},
failure: function(form, action) {
var response = Ext.util.JSON.decode(action.response.responseText);

var messaggio=response.data.inserito;
Ext.MessageBox.show({
   title:'ATTENZIONE',
   msg: messaggio,
   buttons: Ext.MessageBox.OK,
   //fn: processResult,
   //animEl: 'elId',
   icon: Ext.MessageBox.WARNING
});
}

});

}else{
Ext.MessageBox.alert('REGISTRAZIONE NON AVVENUTA', 'Modifica i dati e riprova Registra');
}// chiude if
}

	
});



function messaggio(icona, messag, titolo){
        Ext.MessageBox.show({
           title: titolo,
           msg: messag,
           buttons: Ext.MessageBox.OK,
           //animEl: 'mb9',
           //fn: showResult,
           icon: icona
       });


}
 function disabilita(campi){
var campidisabilita1="cognome1,nome1,email1,cellulareN1,ComboBoxStato";
var campidisabilita2="cognome,nome,email,cellulareN,ComboBoxStato2,startdt,enddt";
var Acampi1=campidisabilita1.split(",");
var Acampi2=campidisabilita2.split(",");


if(campi=="fieldcheck2"){ 

for(i=0;i<Acampi1.length;i++){
Ext.getCmp(Acampi1[i]).disable();
}
for(i=0;i<Acampi2.length;i++){

Ext.getCmp(Acampi2[i]).enable();
}
}else{
for(i=0;i<Acampi1.length;i++){
Ext.getCmp(Acampi1[i]).enable();
}
for(i=0;i<Acampi2.length;i++){

Ext.getCmp(Acampi2[i]).disable();
}
}//chiude if
}
