Ext.BLANK_IMAGE_URL = 'myNewsletter/extjs/resources/images/default/s.gif';

Ext.namespace('INSC');

Ext.onReady(function(){
	
	Ext.QuickTips.init();

/* *************************************************************
	Function: Gestionnaire de message d'erreur
	************************************************************** */
	
	INSC.ErrorMessage = function(message){
		
		Ext.Msg.show({
		   title:'Erreur',
		   msg: message,
		   buttons: Ext.Msg.OK,
		   icon: Ext.MessageBox.ERROR,
		   width:280
		});		
	};
	
	INSC.WarningMessage = function(message){
		
		Ext.Msg.show({
		   title:'Attention',
		   msg: message,
		   buttons: Ext.Msg.OK,
		   icon: Ext.MessageBox.WARNING,
		   width:280
		});		
	};
	
	INSC.infoMessage = function(message){
		
		Ext.Msg.show({
		   title:'Information',
		   msg: message,
		   buttons: Ext.Msg.OK,
		   icon: Ext.MessageBox.INFO,
		   width:280
		});		
	};
	
/* *************************************************************
	Function: Submit
************************************************************** */

	INSC.function_submit = function(){

		login.getEl().mask();   
	
		if(login.getForm().isValid()){

			login.getForm().submit({
								   
				url:'myNewsletter/php_ajax/inscrinptionNL.php',
				method: 'POST',
				success: function(form, action){
				var objSuc = Ext.util.JSON.decode(action.response.responseText);
				
				if(objSuc.hasOwnProperty('success') && objSuc.success == 'false'){
						INSC.ErrorMessage(objSuc.errors[0].msg);
						return;
				}
				INSC.infoMessage(objSuc.sucMSG[0].msg);
				
				login.getForm().reset();
									
				},
				failure:function(form, action){
					var obj = Ext.util.JSON.decode(action.response.responseText);
					var message = obj.errors.msg;
					INSC.ErrorMessage(message);
				}			   						   
			});					  
		}
		else{	
			INSC.WarningMessage('Veuillez remplir chacune des cases.');
		}
		login.getEl().unmask();	
	};
	
	
	var login = new Ext.FormPanel({
		id: 'login',                
		frame: false, 
		width: 280,
		labelWidth: 70, 
		labelAlign: 'left',
		buttonAlign: 'center',
		bodyStyle : 'background:none;',
		border:false,
		items: [{ 
			xtype: 'textfield',
			fieldLabel: 'Nom', 
			id: 'username',
			width: 150,
			name: 'username',	
			allowBlank: false,  
			blankText:"Veuillez saisir votre prénom suivi du nom." 
		},{
		
			xtype: 'textfield',		
			fieldLabel: 'Courriel',		
			width: 150,
			id: 'usermail',
			name: 'usermail',
			allowBlank: false,
			blankText:'Votre adresse courriel est requise',
			vtype: 'email',    
			vtypeText: 'Votre adresse courriel doit être de la forme user@domain.com'
		
		},{
			xtype: 'button',
			style : 'margin-left: 120px;margin-top:5px;',
			id: 'buttonLoggin',
			text: 'Envoyer',	
			handler: INSC.function_submit
		
		}]
	});
	
	login.render('home_center_newsletter');
		
	new Ext.KeyNav("usermail", {
    "enter" : INSC.function_submit
    });

	
});//end onready
