﻿$(function() {
				
				var name = $("#name"),
					email = $("#email"),
					allFields = $([]).add(name).add(email),
					tips = $(".validateTips");

				function updateTips(t) {
					tips
						.append("<p class=\"tipsError\">"+t+"</p>")
						.addClass('ui-state-highlight');
					setTimeout(function() {
						tips.removeClass('ui-state-highlight', 1500)
						$('.tipsError').fadeOut('slow',function(){
							$(this).not('tipsError');
						});
					}, 1500);
				}

				function checkLength(o,n,min,max) {

					if ( o.val().length > max || o.val().length < min ) {
						o.addClass('ui-state-error');
						updateTips("La longueur requise du champs " + n + " est comprise entre "+min+" et "+max+".");
						return false;
					} else {
						return true;
					}

				}

				function checkRegexp(o,regexp,n) {

					if ( !( regexp.test( o.val() ) ) ) {
						o.addClass('ui-state-error');
						updateTips(n);
						return false;
					} else {
						return true;
					}

				}
				$("#dialog-form").dialog({
					autoOpen: false,
					height: 200,
					width: 958,
					modal: true,
					title:'Commande de brochures (1 / 2)',
					draggable: false,
					resizable: false,
					buttons: {
						'Envoyer': function() {
							if($('.tipsError')){
								$('.tipsError').remove();
							}
							var bValid = true;
							allFields.removeClass('ui-state-error');

							bValid = bValid && checkLength(name,"nom",1,80);
							bValid = bValid && checkLength(email,"email",6,80);
							bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"votre email doit être de la forme : adresse@fai.com");
							
							if (bValid) {
								$(this).dialog('close');
								$("#dialog-form2").dialog('open');
							}
						}
					},
					close: function() {
						allFields.removeClass('ui-state-error');
					}
				});
				
				var nom = $("#nom"),
					prenom = $("#prenom"),
					email2 = $("#email2"),
					organisme = $("#organisme"),
					adresse1 = $("#adresse1"),
					adresse2 = $("#adresse2"),
					cp = $("#cp"),
					ville = $("#ville"),
					pays = $("#pays"),
					centreInteret = $("#centreInteret"),
					nombreEnfant = $("#nombreEnfant"),
					trancheAge = $("#trancheAge");
					allFields.add(nom).add(prenom).add(email2).add(organisme).add(adresse1)
					.add(adresse2).add(cp).add(ville).add(pays).add(centreInteret).add(nombreEnfant).add(trancheAge);
					
					
				$("#dialog-form2").dialog({
					autoOpen: false,
					height: 809,
					width: 957,
					modal: true,
					title:'Commande de brochures (2 / 2)',
					draggable: false,
					resizable: false,
					buttons: {
						'Envoyer': function() {
							if($('.tipsError')){
								$('.tipsError').remove();
							}
							var bValid = true;
							allFields.removeClass('ui-state-error');

							bValid = bValid && checkLength(nom,"nom",3,16);
							bValid = bValid && checkLength(email2,"email",6,80);
							bValid = bValid && checkLength(adresse1,"adresse 1",6,180);
							bValid = bValid && checkLength(cp,"code postal",5,5);
							bValid = bValid && checkLength(ville,"ville",2,150);
							bValid = bValid && checkRegexp(email2,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
							
							if (bValid) {
								$(this).dialog('close');
								$("#dialog-form3").dialog('open');
							}
						}
					},
					close: function() {
						allFields.removeClass('ui-state-error');
					}
				});
				$("#dialog-form3").dialog({
					autoOpen: false,
					height: 190,
					width: 500,
					modal: true,
					title:'Commande de brochures',
					draggable: false,
					resizable: false,
					buttons: {
						'fermer': function() {
							if($('.tipsError')){
								$('.tipsError').remove();
							}
							var bValid = true;
							
							if (bValid) {
								//recuperer allFields[]
								$(this).dialog('close');
							}
						}
					},
					close: function() {
						allFields.removeClass('ui-state-error');
					}
				});
				$("#submitWrapBrochure").click(function(){
						$('#dialog-form').dialog('open');
						return false;
				});
			});
