$(function() {
		
		var raison_sociale,
			nom,
			telephone ,
			email ,
			contenu ,
			allFields ,
			tips,
			id="0";
		
		function init(){
			raison_sociale = $("#raison_sociale"),
			nom = $("#nom"),
			telephone = $("#telephone"),
			email = $("#email"),
			contenu = $("#contenu"),
			allFields = $([]).add(raison_sociale).add(nom).add(telephone).add(contenu).add(email),
			tips = $("#message");
		}
		
		function updateTips(t) {
			tips.text(t).effect("highlight",{},5500);
		}

		function checkLength(o,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Le nombre de caractere doit etre compris entre "+min+" et "+max+".");
				return false;
			} else {
				return true;
			}

		}
		
		function trim (myString)
		{
			return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
		} 

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$('#envoi_message').click(function() {
					init();
					var bValid = true;
					allFields.removeClass('ui-state-error');
					bValid = bValid && checkLength(nom,6,20);
					bValid = bValid && checkLength(telephone,10,20);
					bValid = bValid && checkLength(contenu,10,1000);

					bValid = bValid && checkRegexp(nom,/^[a-z]([0-9 a-z_])+$/i,"le nom doit contenir les caracteres de a-z, 0-9, underscores et commencer par une lettre.");
					bValid = bValid && checkRegexp(email,/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/i,"l'email doit contenir les caracteres de a-z, 0-9, underscores, arobase et commencer par une lettre.");
					
					
					
					if (bValid) {
						$('#form_contact').submit();
					}
				
	
		});
	
	});
