// JavaScript Document   Plan3tari0
Ext.onReady(function(){
    Ext.QuickTips.init();
  // turn on validation errors beside the field globally
    Ext.form.Field.prototype.msgTarget = 'side';
	loc = location.href;
	if ( loc.indexOf("/it/") > 0 ) lang='it';
	if ( loc.indexOf("/en/") > 0 ) lang='en';
	
	ait = {'TitoloForm': 'Prenotazione Gruppi', 'SottoTitolo': 'Dati Generali','DatiVisita': 'Dati Visita',
			'NomeGruppo': 'Nome Gruppo','Indirizzo': 'Indirizzo','RecapitoTelefonico': 'Recapito telefonico',
			'FAX': 'FAX','Email':'e-mail','NomeReferente': 'Nome referente', 'Periodo': 'Periodo richiesta visita<br/><i>(indicare giorni e mese)</i>',
			'Orario': 'Orario<br/><i>(mattino,pomeriggio,sera)</i>','VisitaGuidata': 'Visita guidata','Spettacolo':'Spettacolo planetario',
			'Note': 'Note','Si':'Si','No':'No' ,'Partecipanti': 'Partecipanti','Adulti': 'adulti','Anni6':'sotto i 6 anni','Anni6-18': 'tra 6 e 18 anni','Over60':'oltre 60',
			'InviaRichiesta': ' Invia Richiesta',
			'MsgSuccess-T': 'Prenotazione effettuata','MsgSuccess': ' Prenotazione inviata correttamente.<br/> Verrete contattati da un nostro addetto appena possibile.',
			'Failure': ' Errore nel Server','ServerNotAv-T': 'Attenzione!', 'ServerNotAv': 'Server  non al momento raggiungibile. Ci scusiamo per l&#8217;inconveniente '
			};
			
	aen = {'TitoloForm': 'Groups Reservation', 'SottoTitolo': 'General data','DatiVisita': 'Visit Details',
			'NomeGruppo': 'Group&#8217;s name' ,'Indirizzo': 'address','RecapitoTelefonico': 'telephone number',
			'FAX': 'FAX','Email':'e-mail','NomeReferente': 'Representative&#8217;s name', 'Periodo': 'Period visit request<br/><i>(specify days and month)</i>,',
			'Orario': 'Visiting times<br/><i>(morning,afternoon,evening)</i>','VisitaGuidata': 'Guided tour','Spettacolo':'Planetarium projection',
			'Note':'Note','Si':'Yes','No':'No' , 'Partecipanti': 'Number of people committed','Adulti': 'adult','Anni6':'under 6','Anni6-18': '6-18 years old','Over60':'over 60',
			'InviaRichiesta': ' Send Request',
			'MsgSuccess-T': 'Reservation sent','MsgSuccess': ' As soon as possible a our rappresentative will contact you .',
			'Failure': ' Server Error','ServerNotAv-T': 'Attention!', 'ServerNotAv': 'Server unreachable . Sorry for the inconvenience! '
			
			};
			
	language = {'it': ait,'en':aen};
	hidelabel = (lang == 'it' ? false : true);
	hidefield = (lang == 'it' ? false : true);
//Ext.Msg.alert('NewGame',language[lang]['TitoloForm']);
	// Create a variable to hold our EXT Form Panel. 
	// Assign various config options as seen.	 
    var login = new Ext.FormPanel({ 
		url:'../services/jPrenotaGruppo.php', 
        labelWidth: 140, // label settings here cascade unless overridden
        frame:true,
        title: language[lang]['TitoloForm'],
        bodyStyle:'padding:5px 5px 0',
		monitorValid:true,
        width: 550,
        items: [{
			layout: 'form',
            xtype:'fieldset',
            //checkboxToggle:true,
            title: language[lang]['SottoTitolo'],
            autoHeight:true,
            defaults: {anchor:'-30',width: 70},
            defaultType: 'textfield',
            //collapsed: true,
            items :[{
                    fieldLabel: language[lang]['NomeGruppo'],
                    name: 'nomeGruppo',
                    allowBlank:false
                },{
                    fieldLabel: language[lang]['Indirizzo'],
                    name: 'indirizzo'
                },{
                    fieldLabel: language[lang]['RecapitoTelefonico'],
                    name: 'telefono',
					allowBlank:false

                },{
                    fieldLabel: language[lang]['FAX'],
                    name: 'fax'
                }, {
                    fieldLabel: language[lang]['Email'],
                    name: 'email',
                    vtype:'email',
					allowBlank:false
                }, {
                    fieldLabel: language[lang]['NomeReferente'],
                    name: 'nomeReferente',
					allowBlank:false
                }
            ]
        },
		
		{
            xtype:'fieldset',
            title: language[lang]['DatiVisita'],
            //collapsible: true,
            autoHeight:true,
            defaults: {anchor:'-30',width: 70},
            defaultType: 'textfield',
            items :[{
                    fieldLabel: language[lang]['Periodo'],
                    name: 'periodo',
					allowBlank:false

                },{
                    fieldLabel: language[lang]['Orario'] ,
					labelSeparator: '',
                    name: 'orario',
					allowBlank:false


                },{
                    fieldLabel: language[lang]['VisitaGuidata'],
					xtype:'radiogroup',
					columns: [40, 40],
					items: [
                {boxLabel: language[lang]['Si'], name: 'guidata', inputValue: 1},
                {boxLabel: language[lang]['No'], name: 'guidata', inputValue: 2, checked: true}]

                },{
                    fieldLabel: language[lang]['Spettacolo'],
					xtype:'radiogroup',
					columns: [40, 40],
					items: [
                {boxLabel: language[lang]['Si'], name: 'planetario', inputValue: 1},
                {boxLabel: language[lang]['No'], name: 'planetario', inputValue: 2, checked: true}]

                }]
		},			   			   
		{
            xtype:'fieldset',
            title: language[lang]['Partecipanti'],
            //collapsible: true,
            autoHeight:true,
            defaults: {anchor:'-30',width: 70},
            defaultType: 'textfield',
            items :[{
                    fieldLabel: language[lang]['Adulti'],
                    name: 'adulti'
                },{
                    fieldLabel: language[lang]['Anni6'],
                    name: 'bambini6'

                },{
                    fieldLabel: language[lang]['Anni6-18'],
                    name: 'bambini18'

                },{
                    fieldLabel: language[lang]['Over60'],
                    name: 'over60'

                },{
                    fieldLabel: 'Numero possessori di carta musei Torino/Piemonte',
                    name: 'numCarte',
					id: 'numCarte',
					hideLabel : hidelabel,
					hidden: hidefield //(lang == 'it' ? false : true)

                },{
                    fieldLabel: language[lang]['Note'],
                    name: 'note',
					xtype: 'textarea'

                }]
		}]
			   
			   ,


        buttons:[{ 
                text:language[lang]['InviaRichiesta'],
                formBind: true,	 
                // Function that fires when user clicks the button 
                handler:function(){ 
                    login.getForm().submit({ 
						//headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=ISO-8859-1'},
                        method:'POST', 
                        waitTitle:'Connecting', 
                        waitMsg:'Sending data...',
 
			// Functions that fire (success or failure) when the server responds. 
			// The one that executes is determined by the 
			// response that comes from login.asp as seen below. The server would 
			// actually respond with valid JSON, 
			// something like: response.write "{ success: true}" or 
			// response.write "{ success: false, errors: { reason: 'Login failed. Try again.' }}" 
			// depending on the logic contained within your server script.
			// If a success occurs, the user is notified with an alert messagebox, 
			// and when they click "OK", they are redirected to whatever page
			// you define as redirect. 
 
                        success:function(form,action){ 
						     //obj = Ext.util.JSON.decode(action.response.responseText); 
							 							//alert(action.response.responseText);

                            Ext.Msg.alert(language[lang]['MsgSuccess-T'], 
										  language[lang]['MsgSuccess'], 
										  function(btn, text){
				  			 					if (btn == 'ok'){
		                        					//	var redirect = '/admin/adminEventi.php'; 
		                        					//window.close() ;
													login.getForm().reset() ;
                                  				 }
										  });
			  
						   			//var redirect = '/test/admin/adminEventi.php'; 
		                        	//window.location = redirect;
/*
                        	Ext.Msg.alert('Status', 'Login Successful!', function(btn, text){
				  			 		//if (btn == 'ok'){
		                        		var redirect = '/admin/adminEventi.php'; 
		                        		window.location = redirect;
                                  // }
			       			});
							*/
                        },
 
			// Failure function, see comment above re: success and failure. 
			// You can see here, if login fails, it throws a messagebox
			// at the user telling him / her as much.  
 
                        failure:function(form, action){ 
							// non so come ma action.failureType ==>  action.failureType='server';							
                            if(action.failureType == 'server'){ 
                                obj = Ext.util.JSON.decode(action.response.responseText); 
                                Ext.Msg.alert(language[lang]['Failure'], obj.errors); 
                            }else{ 
															//Ext.Msg.alert(action.response.responseText);

                                Ext.Msg.alert(language[lang]['ServerNotAv-T'],language[lang]['ServerNotAv'] ); 
                            } 
                            //login.getForm().reset(); 
                        } 
                    }); 
                } 
            },{
				text:'Cancel',
				 handler:function(){ 
                    login.getForm().reset();
				 } 
			}
				] 
    });
 
 	login.render('formgruppi');
	/*
	// This just creates a window to wrap the login form. 
	// The login object is passed to the items collection.       
    var win = new Ext.Window({
        layout:'fit',
        width:300,
        height:400,
        closable: false,
        resizable: false,
        plain: true,
        border: false,
		renderTo: 'formgruppi',
        items: [login]
	});
	win.show();
*/	
});

