Ext.onReady(function() {
	Ext.QuickTips.init();
	Ext.QuickTips.enable();
	Ext.BLANK_IMAGE_URL = 'scripts/ext/resources/images/default/s.gif';
	// turn on validation errors beside the field globally
	Ext.form.Field.prototype.msgTarget = 'side';
	var cp = new Ext.state.CookieProvider();
	Ext.state.Manager.setProvider(cp);
	var cook = Ext.state.Manager.getProvider();
	// Add the additional 'advanced' VTypes
	Ext.apply(Ext.form.VTypes, {
		email2 : function(val, field) {
			this.email2Text = "e-mail non valide";
			if (val
					.match(/^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$/)) {
				if (field.initialPassField2) {
					var pwd = Ext.getCmp(field.initialPassField2);
					this.email2Text = "Les e-mails doivent être identiques";
					return (val == pwd.getValue());
				} else
					return true;
			} else
				return false;
		},
		email2Text : 'Les e-mails doivent être identiques'
	});

	var txt_pre = new Ext.form.TextField({
				fieldLabel : "Prénom",
				name : "prenom",
				labelStyle : " padding-left:10px; font-weight:bold; width:70px;",
				blankText : "Champ obligatoire",
				width : 160,
				validateOnBlur : false
			});
	txt_pre.on('change', saveField);
	var txt_nom = new Ext.form.TextField({
				fieldLabel : "Nom",
				name : "nom",
				labelStyle : " padding-left:10px; font-weight:bold; width:70px;",
				blankText : "Champ obligatoire",
				width : 160,
				validateOnBlur : false
			});
	txt_nom.on('change', saveField);
	var txt_mail = new Ext.form.TextField({
				fieldLabel : "E-mail",
				name : "mail",
				vtype : 'email2',
				labelStyle : " padding-left:10px; font-weight:bold; width:70px;",
				blankText : "Champ obligatoire",
				width : 160,
				id : 'pass2',
				validateOnBlur : false
			});
	txt_mail.on('change', saveField);
	var txt_confemail = new Ext.form.TextField({
				fieldLabel : "Confirmation E-mail",
				name : "confemail",
				vtype : 'email2',
				labelStyle : " padding-left:10px; font-weight:bold; width:70px;",
				blankText : "Champ obligatoire",
				width : 160,
				initialPassField2 : 'pass2',
				validateOnBlur : false
			});
	txt_confemail.on('change', saveField);
	var form_inscr = new Ext.FormPanel({
				url : 'servlet/ProcessInstantStep',
				itemCls : 'coucou',
				defaults : {
					allowBlank : false,
					validationEvent : false
				},
				items : [txt_pre, txt_nom, txt_mail, txt_confemail]
			});
	form_inscr.render(Ext.get('id_form_inscr'));
	if (cook.get('prenom') != null) {
		txt_pre.setRawValue(cook.get('prenom'));
	}
	if (cook.get('nom') != null) {
		txt_nom.setRawValue(cook.get('nom'));
	}
	if (cook.get('mail') != null) {
		txt_mail.setRawValue(cook.get('mail'));
	}
	if (cook.get('confemail') != null) {
		txt_confemail.setRawValue(cook.get('confemail'));
	}

	function saveField(ev, target) {
		// console.log('name='+this.name+' | valeur='+target)
		cook.set(this.name, target);
	}
	var bt_subscr = Ext.get('AffiliBtn');
	bt_subscr.on('click', function() {
		form_inscr.getForm().submit({
			success : function(form, action) {
				form.findField("nom").getValue();
				form.findField("prenom").getValue();
				form.findField("mail").getValue();
				idDB = action.result.id;
				document.location = "affiliationExpress.jsp?id="+idDB;
			},
			failure : function(form, action) {
				switch (action.failureType) {
					case Ext.form.Action.CLIENT_INVALID :
						// Ext.Msg.alert("Failure",
						// "Form fields may not be submitted with invalid
						// values");
						break;
					case Ext.form.Action.CONNECT_FAILURE :
						Ext.Msg.alert("Failure", "Ajax communication failed");
						break;
					case Ext.form.Action.SERVER_INVALID :
						Ext.Msg.alert("Failure", action.result.msg);
				}
			}
		});
	});

});