// 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': 'Iscrizione NewsLetter', 
			'Cognome': 'Cognome','Nome': 'Nome','Email':'e-mail',
			'Si':'Si','No':'No' ,
			'InviaRichiesta': ' Invia Richiesta',
			'MsgSuccess-T': 'Dati registrati','MsgSuccess': ' Dati inviati correttamente. Riceverai presto una email per confermarne l\'esattezza .',
			'Failure': ' Registrazione  Fallita!','ServerNotAv-T': 'Attenzione!', 'ServerNotAv': 'Server  non al momento raggiungibile. Ci scusiamo per l&#8217;inconveniente '
			};
			
	aen = {'TitoloForm': 'Newsletter Registration ', 
			'Cognome': 'Last Name','Nome': 'First Name','Email':'e-mail',
			'Si':'Yes','No':'No' , 
			'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);

// Create a variable to hold our EXT Form Panel. 
	// Assign various config options as seen.	 
    var login = new Ext.FormPanel({ 
        labelWidth:80,
        url:'../services/jIscrizioneNewsLetter.php', 
        frame:true, 
		 width: 300,
        title:language[lang]['TitoloForm'], 
        defaultType:'textfield',
	    monitorValid:true,
	// Specific attributes for the text fields for username / password. 
	// The "name" attribute defines the name of variables sent to the server.
        items:[{ 
                fieldLabel:language[lang]['Nome'], 
                name:'nome', 
                allowBlank:false 
            },{ 
                fieldLabel:language[lang]['Cognome'], 
                name:'cognome', 
                allowBlank:false 
            },{ 
                fieldLabel:language[lang]['Email'], 
                name:'email', 
                vtype:'email',
                allowBlank:false 
            }],
 
	// All the magic happens after the user clicks the button     
        buttons:[{ 
                text:language[lang]['InviaRichiesta'],
                formBind: true,	 
                // Function that fires when user clicks the button 
                handler:function(){ 
				
                    login.getForm().submit({ 
                        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(action.response.responseText); 
							Ext.Msg.alert(language[lang]['MsgSuccess-T'], 
										  language[lang]['MsgSuccess'], 
										  //Ext.Msg.alert('Dati registrati', 
										  //"Iscizione inviata correttamente. Riceverai presto una email per confermare l'esattezza dei tuoi dati", 
						  function(btn, text){
								if (btn == 'ok'){
									//	var redirect = '/admin/adminEventi.php'; 
									login.getForm().reset() ;
								 }
						  });
/*
                        	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); 
								
                               // Ext.Msg.alert('Registrazione  Fallita!', obj.errors); 
                            }else{ 
                                Ext.Msg.alert(language[lang]['ServerNotAv-T'],language[lang]['ServerNotAv'] ); 

                               // Ext.Msg.alert('Attenzione!', 'Il server non è raggiungibile: ' ); 
                            } 
                            login.getForm().reset(); 
                        } 
                    }); 
                } 
            }] 
    });
 
 	login.render('formnewsletter');
	/*
	// 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();
*/	
});
