/*****************/
/*	CLASS CART   */
/*****************/
var CLASS ={};
(function(){
	CLASS.cart = function(){
	};
	CLASS.cart.prototype = {
		qte:'',
		amount:'',
		productList:{},
		/**
		* add or delete product to CART
		* @param action
		* @param product
		* @param qte
		*/
		handleCart:function(product){
			switch(action){
				case'add':
					this.addtoCart(product, qte);
					break;
				case'delete':
					
					break;
			}
		},
		/**
		* add a product to CART
		* send request to actions.php ta add product to cart
		* retrieve product's cart's quantity and cart's amount  
		* @param string product -> product reference
		* @param qte
		*/
		addtoCart:function (product, qte) {
			if (qte <= 0){
				alert("Veuillez choisir une quantité supérieure à 0");
				return;
			}
			
			var cart_instance = this;
			var article = $('article.product_line input[value='+product+'], article.product_coffret input[value='+product+']').parents('article.product_line, article.product_coffret');
			$.post('includes/actions.php', { action:"addtoCart", product: product, qte: qte }, function(data) {
				var cart = data.split(";");
				var qte = cart[0];
				var amount = cart[1];
				cart_instance.updateCart(qte, amount);
				cart_instance.updateAddTocartHtmlElement(article);
			});
		},
		/**
		 * update product form order form
		 * @param string productRef -> product's reference
		 * @param integer qte -> quantity to update
		 */
		updateProduct:function(productRef, qte){
			if (qte <= 0){
				alert("Veuillez choisir une quantité supérieure à 0 ou supprimez le produit en utilisant la croix");
				return;
			}
			this.productList[productRef] = qte;
			//this.updateTotalProduct(productRef, qte);
			cart_instance = this;
			$.post("includes/actions.php", {action:"updateProduct", productRef:productRef, qte:qte}, function(data){
				$('div.commande').html(data);
			}, "text");
		},
		supprProduct:function(productRef){
			delete this.productList[productRef];
			cart_instance = this;
			$.post("includes/actions.php", {action:"supprProduct", productRef:productRef}, function(data){
				$('div.commande').html(data);
			}, "text");
		},
		updateTotalOrder:function(numArticles, TotalOrder){
			$("div#totalArticles span").text(numArticles);
			$("div#totalCommande span").text(TotalOrder);
		},
		calcAmount:function(){
		},
		/**
		 * empty cart
		 */
		empty:function(){
			this.productList = {};
			this.qte = 0;
			this.amount = 0;
			//cart_instance = this;
			$.post("includes/actions.php",{action:'emptyCart'},function(data){
				$('div.commande').html(data);
			});
		},
		/**
		 * update html Cart's box and value in classe instance 
		 * @param qte
		 * @param amount
		 */
		updateCart:function(qte, amount){
			this.qte = qte;
			this.amount = amount;
			$('#cart_articles').html(qte);
			$('#cart_amount').html(amount);
		},
		/**
		 * 
		 * @param json producList ->{productRef:qte, ....}
		 */
		updateProductList:function(producList){
			this.productList = producList;
		},
		/**
		 * Verify is a product is in cart
		 * @param refProduct
		 * @returns {Boolean}
		 */
		productInCart:function(refProduct){
			if(this.productList[refProduct] == undefined){
				return false;
			}
			return true;
		},
		/**
		 * add addToCart function to add to cart html element
		 */
		controlProductList:function(){
			var cart_instance = this; 
			$('article.product_line, article.product_coffret').each(function(){
				var refProduct =  $(this).find('input[name=ref]').val();
				var spanAddCart = $(this).find('span.addCart');
				if(cart_instance.productInCart(refProduct)){
					spanAddCart.html('<a href="panier.html">Dans mon panier</a>');
					$(this).find('label.quantite').css('display', 'none');
					/*spanAddCart.bind("click", function(e){
						window.location = 'http://www.fermedelataste.com/panier.html';
					});*/
				}else{
					spanAddCart.bind("click", {article_instance:$(this)}, function(e){
						var qte =  $(this).parents('article.product_line, article.product_coffret').find('input[name=qte]').val();
						cart_instance.addtoCart(refProduct, qte);
						
					});
				}
			});
		},
		/**
		 * 
		 * @param a -> article'html instance
		 */
		updateAddTocartHtmlElement:function(a){
			a.find('label.quantite').css('display', 'none');
			var spanAddCart = a.find('span.addCart');
			spanAddCart.html('Dans mon panier');
			spanAddCart.bind("click", function(e){
				window.location = 'http://www.fermedelataste.com/panier.html';
			});
		}
	};
	CLASS.customerInformation = function(){
	};
	CLASS.customerInformation.prototype = {
			/**
			 * instance of form that contains addresses informations
			 */
			$f:{},
			/**
			 * Form values
			 */
			CI_submitFlag:false,
			CI_formValues:{},
			CI_formCheck:true,
			CI_formMissingFields:{},
			/*
			 * instancie les bind sur les checkbox de forme de l'entité,
			 * le choix de l'adresse de livraison différente de celle de facturation
			 * les messages 
			 */
			CI_init:function(CIFromPhp){
				this.$f = $('.formAddress');
				this.CI_formValues = CIFromPhp;
				this.CI_setFormValues();
				/*this.CI_formValues['AF']={};
				this.CI_formValues['AL']={};
				this.CI_formValues['MB']={};
				this.CI_formValues['MD']={};
				this.CI_formValues['AF']['AFentity'] ="particular";
				this.CI_formValues['MD']['AMchooser'] ="noMessage";
				this.CI_formValues['MB']['AM1chooser'] ="noMessage";
				this.CI_formValues['AL']["ALChooser"]="shippingAddressAsInvoicing";*/
				var instance = this;
				var $af = $(".boxAddress");
				$af.find('input[name=AFentity], input[name=ALentity]').bind('click', {_I:instance}, function(e){
					//Entity handler
					var v =$(this).val();
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					var $elt =$(this).parents('.boxAddress'); 
					e.data._I.CI_setEntityType($elt, v);
					e.data._I.CI_formValues[f][n] = $(this).val();
				});
				$af.find('input[name=AFcivilite], input[name=ALcivilite]').bind('click', {_I:instance}, function(e){
					// civilite handler
					var v =$(this).val();
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					var $elt =$(this).parents('.boxAddress');
					e.data._I.CI_formValues[f][n] = $(this).val();
				});
				$af.find('input[name=AFcivilite], input[name=ALcivilite]').bind('click', {_I:instance}, function(e){
					// civilite handler
					var v =$(this).val();
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					var $elt =$(this).parents('.boxAddress');
					e.data._I.CI_formValues[f][n] = $(this).val();
					var s =e.data._I.$f.find('input[name='+n+']').prevAll('span');
					if(s.hasClass('formError')){
						s.removeClass('formError');	
					}
					
				});
				$af.find('input[name=ALchooser]').bind('click', {_I:instance}, function(e){
					// shipping address handler
					var v =$(this).attr('checked');
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					var $elt =$(this).parents('.boxAddress'); 
					e.data._I.CI_setShippingAddress($elt, v);
				});
				$af.find('input[name=AMchooser],input[name=AM1chooser]').bind('click', {_I:instance}, function(e){
					// messsages handler
					var v =$(this).attr('checked');
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					var b = $(this).attr('checked');
					var $elt =$(this).parents('.boxAddress'); 
					e.data._I.CI_setMessage($elt, v);
					if(b){
						e.data._I.CI_formValues[f][n] = $(this).val();
					}else{
						e.data._I.CI_formValues[f][n] = "noMessage";
						var $t = $elt.find('textarea');
						$t.val('');
						var n1 = $t.attr('name');
						e.data._I.CI_formValues[f][n1] = "";
					}
					var test="";
				});
				var $inputstext = this.$f.find('input[type=text]');
				var $inputsCheckbox = this.$f.find('input[type=checkbox]');
				var $inputsRadio = this.$f.find('input[type=radio]');
				var $textareas = this.$f.find('textarea');
				$inputstext.bind('focus', function(){
					var s = $(this).prevAll('span');
					if(s.hasClass('formError')){
						s.removeClass('formError');	
					}
				});
				$inputstext.bind('blur', {_I:instance}, function(e){
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					var cktype = $(this).attr('ck');
					e.data._I.CI_formValues[f][n] = $(this).val();
					e.data._I.CI_checkFieldType($(this), cktype, e.data._I.CI_formValues[f][n]);
				});
				$inputstext.bind('keyup', {_I:instance}, function(e) {
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					e.data._I.CI_formValues[f][n] = $(this).val();
				});
				$textareas.bind('keyup', {_I:instance}, function(e) {
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					e.data._I.CI_formValues[f][n] = $(this).val();
				});
				$textareas.bind('focus', function(){
					var s = $(this).prevAll('span');
					if(s.hasClass('formError')){
						s.removeClass('formError');	
					}
				});
				$textareas.bind('blur', {_I:instance}, function(e){
					var f = $(this).attr('family');
					var n = $(this).attr('name');
					var cktype = $(this).attr('ck');
					e.data._I.CI_formValues[f][n] = $(this).val();
					e.data._I.CI_checkFieldType($(this), cktype, e.data._I.CI_formValues[f][n]);
				});
				this.$f.bind('submit', {_I:instance}, function(e){
					return e.data._I.CI_submit();
				});
			},
			/**
			 * do stuff to hide/reveal div related to choosed value
			 * delete obsolete vars & remove errors classes
			 * @param $elt
			 * @param value
			 */
			CI_setEntityType:function($elt, value){
				var f = $elt.attr('family');
				switch(value){
					case'society':
						$elt.find('div.society').css('display','block');
						$elt.find('div.other input').val('');
						$elt.find('div.other span').removeClass('formError');
						$elt.find('div.other').css('display','none');
						delete this.CI_formValues[f]['OtherDenomination'];
						delete this.CI_formValues[f]['OtherInsee'];
						break;
					case'other':
						$elt.find('div.society').css('display','none');
						$elt.find('div.society input').val('');
						$elt.find('div.society span').removeClass('formError');
						$elt.find('div.other').css('display','block');
						delete this.CI_formValues[f]['SocietyName'];
						delete this.CI_formValues[f]['societyTvaIntra'];
						break;
					default:
						$elt.find('div.society').css('display','none');
						$elt.find('div.society input').val('');
						$elt.find('div.society span').removeClass('formError');
						$elt.find('div.other').css('display','none');
						$elt.find('div.other input').val('');
						$elt.find('div.other span').removeClass('formError');
						delete this.CI_formValues[f]['OtherDenomination'];
						delete this.CI_formValues[f]['OtherInsee'];
						delete this.CI_formValues[f]['SocietyName'];
						delete this.CI_formValues[f]['societyTvaIntra'];
						break;
				}
					
			},
			/**
			 * hide or reveal shipping address form's fields
			 * unset related vars if necessary
			 * @param object $elt -> jquery instance of div that contains shipping address informations
			 * @param bool value -> status of checkbox (0|1) that set different shipping address from invoicing address
			 */
			CI_setShippingAddress:function($elt,value){
				if(value){
					this.CI_formValues['AL'] = {};
					this.CI_formValues['AL']["ALChooser"]="shippingAddressAsInvoicing";
					$elt.find('.entity input').removeAttr('checked');
					$elt.find('.entity input:first').attr('checked','checked');
					$elt.find('input[type=text]').val('');
					$elt.find('input[name=ALcivilite]').removeAttr('checked');
					$elt.find('div:eq(2), div:gt(4)').addClass('nodisplay');
					$elt.find('div span').removeClass('formError');
					$elt.find('div.society').css('display','none');
					$elt.find('div.other').css('display','none');
					$elt.find('div.society').css('display','none');
					
				}else{
					$elt.find('div').removeClass('nodisplay');
					this.CI_formValues['AL'] = {};
					this.CI_formValues['AL']["ALChooser"]="distinctShippingAddress";
					this.CI_formValues['AL']['ALentity'] ="particular";
				}
			},
			/**
			 * hide or reveal shipping address form's fields
			 * unset related vars if necessary
			 * @param object $elt -> jquery instance of div that contains shipping address informations
			 * @param bool value -> status of checkbox (0|1) that set different shipping address from invoicing address
			 */
			CI_setMessage:function($elt,value){
				if(value){
					$elt.find('div').removeClass('nodisplay');
					$elt.find('').val('');
				}else{
					$elt.find('div:eq(2)').addClass('nodisplay');
					$elt.find('div:eq(2) span').removeClass('formError');
				}
			},
			/**
			 * handle form submit
			 * check requiered fields
			 * @param flag -> bool
			 * @returns {Boolean}
			 */
			CI_submit:function(){
				if(this.CI_submitFlag){return true;}
				var check = this.CI_checkReqfields();
				var instance = this;
				if (check){
					var action = "submitFormCI";
					$.post('includes/actions.php',{action:action, CI:this.CI_formValues},function(data){
						instance.CI_submitFlag = true;
						instance.$f.submit();
						//window.location = 'http://www.fermedelataste.com/validation-panier.html';
					});
					return false;
				}
				this.CI_printErrors();
				return false;
			},
			/**
			 * check for requiered values submitted by the form and stocked in this.CI_formValues
			 * @returns {Boolean}
			 */
			CI_checkReqfields:function(){
				this.CI_formCheck = true;
				this.CI_formMissingFields = {};
				// valid Invoice informations
				this.CI_checkReqField('AF', 'AFentity');
				if(this.CI_formValues['AF']['AFentity'] == "society"){
					this.CI_checkReqField('AF', 'SocietyName');
					this.CI_checkReqField('AF', 'societyTvaIntra');
				}
				if(this.CI_formValues['AF']['AFentity'] == "other"){
					this.CI_checkReqField('AF', 'OtherDenomination');
					this.CI_checkReqField('AF', 'OtherInsee');
				}
				this.CI_checkReqField('AF', 'AFcivilite');
				this.CI_checkReqField('AF', 'nom');
				this.CI_checkReqField('AF', 'prenom');
				this.CI_checkReqField('AF', 'adresse');
				this.CI_checkReqField('AF', 'codepostal');
				this.CI_checkReqField('AF', 'ville');
				this.CI_checkReqField('AF', 'pays');
				this.CI_checkReqField('AF', 'email');
				this.CI_checkReqField('AF', 'tel');
				// validate shipping address
				if(this.CI_formValues['AL']['ALChooser'] == 'distinctShippingAddress'){
					this.CI_checkReqField('AL', 'ALentity');
					if(this.CI_formValues['AL']['ALentity'] == "society"){
						this.CI_checkReqField('AL', 'SocietyName');
					}
					if(this.CI_formValues['AL']['ALentity'] == "other"){
						this.CI_checkReqField('AL', 'OtherDenomination');
					}
					this.CI_checkReqField('AL', 'ALcivilite');
					this.CI_checkReqField('AL', 'nom');
					this.CI_checkReqField('AL', 'prenom');
					this.CI_checkReqField('AL', 'adresse');
					this.CI_checkReqField('AL', 'codepostal');
					this.CI_checkReqField('AL', 'ville');
					this.CI_checkReqField('AL', 'pays');
					this.CI_checkReqField('AL', 'email');
					this.CI_checkReqField('AL', 'tel');
				}
				// validate messages
				if(this.CI_formValues['MB']['AM1chooser'] == 'messageBoutique'){
					this.CI_checkReqField('MB', 'messageBoutique');
				}
				if(this.CI_formValues['MD']['AMchooser'] == 'messageDestinataire'){
					this.CI_checkReqField('MD', 'messageDestinataire');
				}
				return this.CI_formCheck;
			},
			/** 
			 * Add formError class to span elements that preceed input or textarea fields
			 * @return void
			 */
			CI_printErrors:function(){
				for (var f in this.CI_formMissingFields){
					var le = this.CI_formMissingFields[f];
					for (var ie in le){
						var e =le[ie];
						var $elt = this.$f.find('input[family='+f+'][name='+e+'], textarea[family='+f+'][name='+e+']');
						$elt.prevAll('span').addClass('formError');	
					}
				}
			},
			/** verify if some requiered fields are missing
			* if missing fields : set formCheck var to false and add family(AF,AL,..) and fields name in formMissingFileds object
			* @return bool true|false
			*/ 
			CI_checkReqField:function(f, e){
				if(this.CI_formValues[f][e] == undefined || this.CI_formValues[f][e] == ""){
					if (this.CI_formMissingFields[f] == undefined){
						this.CI_formMissingFields[f] = [];
					}
					this.CI_formMissingFields[f].push(e);
					this.CI_formCheck = false;
					return false;
				}
				return true;
			},
			/**
			 * check content of field against its type
			 */
			CI_checkFieldType:function(elt, type, value){
				var testExp= new CLASS.regExp();
				var test = true;
				switch(type){
					case'alphanumWS':
						test = testExp.matchRegularExpression(value, testExp.regExpAlphanumericWithWhitespace);
						break;
					case'alphabetic':
						test = testExp.matchRegularExpression(value, testExp.regExpAlphabetic);
						break;
					case'alphanum':
						test = testExp.matchRegularExpression(value, testExp.regExpAlphanumeric);
						break;
					case'email':
						test = testExp.matchRegularExpression(value, testExp.regExpEmailAdress);
						break;
					case'codepostal':
						test = testExp.matchRegularExpression(value, testExp.regExpCodePostal);
						break;
					case'tel':
						test = true;
						var tfixe =testExp.isNotTelephone(value, 'fixe');
						var tip = testExp.isNotTelephone(value, 'IP');
						var tport = testExp.isNotTelephone(value, 'port');
						var tnati = testExp.isNotTelephone(value, 'nati');
						var tinte = testExp.isNotTelephone(value, 'inte');
						if(tfixe & tip & tport & tnati & tinte){
							test = false;
						}
						break;
					case'TVAIntra':
						test = testExp.matchRegularExpression(value, testExp.regExpTVAIntracommunautaire);
						break;
					case'none':
						break;
				}
				if (test === false){
					elt.prevAll('span').addClass('formError');
				}
			},
			CI_setFormValues:function(){
				/// Addresse de livraison
				this.$f.find('#adresseFacturation input[name=AFentity][value='+this.CI_formValues['AF']['AFentity']+']').attr('checked', 'checked');
				var $elt = this.$f.find("#adresseFacturation");
				this.CI_setEntityType($elt, this.CI_formValues['AF']['AFentity']);
				switch(this.CI_formValues['AF']['AFentity']){
					case'society':
						this.$f.find('#adresseFacturation input[name=SocietyName]').val(this.CI_formValues['AF']['SocietyName']);
						this.$f.find('#adresseFacturation input[name=societyTvaIntra]').val(this.CI_formValues['AF']['societyTvaIntra']);
						break;
					case'other':
						this.$f.find('#adresseFacturation input[name=OtherDenomination]').val(this.CI_formValues['AF']['OtherDenomination']);
						this.$f.find('#adresseFacturation input[name=OtherInsee]').val(this.CI_formValues['AF']['OtherInsee']);
						break;
				}
				// destinataire
				this.$f.find('#adresseFacturation input[name=AFcivilite][value='+this.CI_formValues['AF']['AFcivilite']+']').attr('checked', 'checked');
				this.$f.find('#adresseFacturation input[name=nom]').val(this.CI_formValues['AF']['nom']);
				this.$f.find('#adresseFacturation input[name=prenom]').val(this.CI_formValues['AF']['prenom']);
				//addresses
				this.$f.find('#adresseFacturation input[name=adresse]').val(this.CI_formValues['AF']['adresse']);
				if(this.CI_formValues['AF']['adresseCpl'] !== undefined){
					this.$f.find('#adresseFacturation input[name=adresseCpl]').val(this.CI_formValues['AF']['adresseCpl']);
				}
				this.$f.find('#adresseFacturation input[name=codepostal]').val(this.CI_formValues['AF']['codepostal']);
				this.$f.find('#adresseFacturation input[name=ville]').val(this.CI_formValues['AF']['ville']);
				this.$f.find('#adresseFacturation input[name=pays]').val(this.CI_formValues['AF']['pays']);
				//contacts
				this.$f.find('#adresseFacturation input[name=email]').val(this.CI_formValues['AF']['email']);
				this.$f.find('#adresseFacturation input[name=tel]').val(this.CI_formValues['AF']['tel']);
				//Adresse de facturation
				if(this.CI_formValues['AL']['ALChooser'] == "distinctShippingAddress" ){
					this.$f.find("#adresseLivraison input[name=ALchooser]").removeAttr('checked');
					value = false;
					var $elt = this.$f.find("#adresseLivraison");
					$elt.find('div').removeClass('nodisplay');
					this.$f.find('#adresseLivraison input[name=ALentity][value='+this.CI_formValues['AL']['ALentity']+']').attr('checked', 'checked');
					var $elt = this.$f.find("#adresseLivraison");
					this.CI_setEntityType($elt, this.CI_formValues['AL']['ALentity']);
					switch(this.CI_formValues['AL']['ALentity']){
						case'society':
							this.$f.find('#adresseLivraison input[name=SocietyName]').val(this.CI_formValues['AL']['SocietyName']);
							//var $sctva = this.$f.find('#adresseLivraison input[name=societyTvaIntra]');
							//$sctva.val(this.CI_formValues['AL']['societyTvaIntra']);
							break;
						case'other':
							this.$f.find('#adresseLivraison input[name=OtherDenomination]').val(this.CI_formValues['AL']['OtherDenomination']);
							//var $otherInsee = this.$f.find('#adresseLivraison input[name=OtherInsee]');
							//$otherInsee.val(this.CI_formValues['AL']['OtherInsee']);
							break;
					}
					// destinataire
					this.$f.find('#adresseLivraison input[name=ALcivilite][value='+this.CI_formValues['AL']['ALcivilite']+']').attr('checked', 'checked');
					this.$f.find('#adresseLivraison input[name=nom]').val(this.CI_formValues['AL']['nom']);
					this.$f.find('#adresseLivraison input[name=prenom]').val(this.CI_formValues['AL']['prenom']);
					//addresses
					this.$f.find('#adresseLivraison input[name=adresse]').val(this.CI_formValues['AL']['adresse']);
					if(this.CI_formValues['AL']['adresseCpl'] !== undefined){
						this.$f.find('#adresseLivraison input[name=adresseCpl]').val(this.CI_formValues['AL']['adresseCpl']);
					}
					this.$f.find('#adresseLivraison input[name=codepostal]').val(this.CI_formValues['AL']['codepostal']);
					this.$f.find('#adresseLivraison input[name=ville]').val(this.CI_formValues['AL']['ville']);
					this.$f.find('#adresseLivraison input[name=pays]').val(this.CI_formValues['AL']['pays']);
					//contacts
					this.$f.find('#adresseLivraison input[name=email]').val(this.CI_formValues['AL']['email']);
					this.$f.find('#adresseLivraison input[name=tel]').val(this.CI_formValues['AL']['tel']);
				}
				if(this.CI_formValues['MD']['AMchooser'] == "messageDestinataire" ){
					$elt = this.$f.find("#messageDestinataire")
					$elt.find('input[name=AMchooser]').attr('checked', 'checked');
					$elt.find('div').removeClass('nodisplay');
					$elt.find('textarea').val(this.CI_formValues['MD']['messageDestinataire']);
				}
				if(this.CI_formValues['MB']['AM1chooser'] == "messageBoutique" ){
					$elt = this.$f.find("#messageBoutique")
					$elt.find('input[name=AM1chooser]').attr('checked', 'checked');
					$elt.find('div').removeClass('nodisplay');
					$elt.find('textarea').val(this.CI_formValues['MB']['messageBoutique']);
				}
				
			}
	};
	CLASS.order = function(){
	};
	CLASS.order.prototype = {
	};
	CLASS.browser = function(){
	};
	CLASS.browser.prototype = {
	};
	CLASS.regExp = function(){
		this.regExpEmpty=/^$/g;// Accepte une chaine vide
		this.regExp8Chars=/^[0-9a-zA-Z]{8,}$/g;// Accepte une chaine d'au moins 8 carctères alphanumeriques (pour un mot de passe par exemple)
		// Expressions régulières de test de type de caractère
		this.regExpAlphanumeric=/[0-9a-zA-Z]+/g;// Accepte une chaine alphanumérique
		this.regExpAlphanumericWithWhitespace=/[0-9a-zA-Z áéíóäëiöúàèììù]+/g;	// Accepte une chaine alphanumérique + ' '
		this.regExpAlphabetic=/[a-zA-Z]+/g;// Accepte une chaine alphabétique
		this.regExpNumeric=/[0-9]+/g;// Accepte une chaine numérique
		// Expressions régulières de test de type
		this.regExpInt=/^[0-9]+$/g;	// Accepte une chaine de type 'int'
		this.regExpDouble=/^[-+]?[0-9]+(\.[0-9]+)?$/g;// Accepte une chaine de type 'double'
		this.regExpFloat=/^[-+]?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?$/g;	// Accepte une chaine de type 'float'
		this.regExpTime=/^([01][0-9]|2[0123])\:([012345][0-9])(\:([012345][0-9])(.([0-9]{3})+)?)?$/g;		 // Accepte une chaine de type 'time'. Ex : 12:51 ou 21:45:35.654
		this.regExpFrenchDate=/^(0[1-9]|[12][0-9]|3[01])[\- \/\.](0[1-9]|1[012])[\- \/\.](19|20)\d\d$/g;  // date au format jj/mm/aaaa ou jj-mm-aaaa ou jj mm aaaa ou jj.mm.aaaa avec aaaa compris entre 1900 et 2099.
		this.regExpEnglishDate=/^(19|20)\d\d[\- \/\.](0[1-9]|1[012])[\- \/\.](0[1-9]|[12][0-9]|3[01])$/g; // idem ci-dessus mais format anglais (Ex : aaaa/mm/jj)
		this.regExpBoolean=/^true|false$/g;// Accepte une chaine de type 'boolean'
		// Expressions régulières de test de types administratifs français
		this.regExpCodePostal=/^([A-Z]+[A-Z]?\-)?[0-9]{1,2} ?[0-9]{3}$/g;// Accepte une chaine de type 'code postal'. Ex : F-33370 ou 33 370 ou 33370 ou F-1 370
		this.regExpTelephoneFixe=/^(01|02|03|04|05)[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;	// Accepte un numero de téléphone de type 'fixe'. Ex : 01.34.12.52.30 ou 0134125230
		this.regExpTelephoneIP=/^(08|09)[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;	// Accepte un numero de téléphone de type 'fixeIP'. Ex : 09.34.12.52.30 ou 0934125230
		this.regExpTelephonePortable=/^(06|07)[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;// Accepte un numero de téléphone de type 'portable'.
		this.regExpTelephoneNational=/^(0[1234568])[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;	// Accepte un numero de téléphone de type 'national' y compris numéros en '08'.
		this.regExpTelephoneInternational=/^(\(\+[0-9]{2}\))[ \.\-]?[0-9][ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{2}$/g;// Accepte un numero de téléphone de type 'international'. Ex : (+33) 1 34 12 52 30

		this.regExpNumeroSecuriteSociale=/^[12][ \.\-]?[0-9]{2}[ \.\-]?(0[1-9]|[1][0-2])[ \.\-]?([0-9]{2}|2A|2B)[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{2}$/g; // Accepte un numero de sécurité sociale franÁais. Ex : 1 85 34 33 354 450 45

		this.regExpTVAIntracommunautaire=/^[A-Z]{2}[ \.\-]?[0-9]{2}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}$/g;		// Accepte un numero de TVA Intra-communautaire. Ex : FR 02 254 254 254
		this.regExpNumeroSiren=/^[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}$/g;// Accepte un numero SIREN. Ex : 254 254 254
		this.regExpNumeroSiret=/^[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{3}[ \.\-]?[0-9]{5}$/g;					// Accepte un numero SIRET. Ex : 254 254 254 12345
		this.regExpCodeApe=/^[0-9]{2}[ \.\-]?[0-9]{1} ?[a-zA-Z]$/g;							// Accepte un code APE. Ex : 25.4Z

		// Expressions réguliéres de test de types liés à internet

		this.regExpEmailAdress=/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/g;										// Accepte une adresse email. Ex : toto@toto.com
		this.regExpIpAdress=/\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/g;	// Accepte une adresse ip. Ex : 192.168.0.1
		this.regExpDomainName=/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/g;													// Accepte un nom de domaine. Ex : toto.com
		this.regExpUrl=/^(((ht|f)tp(s?))\:\/\/)?(([a-zA-Z0-9]+([@\-\.]?[a-zA-Z0-9]+)*)(\:[a-zA-Z0-9\-\.]+)?@)?(www.|ftp.|[a-zA-Z]+.)?[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,})(\:[0-9]+)?\/?/g;		// Accepte une url ftp, http ou https, avec ou sans login/mot de passe, avec ou sans numero de port. Ex : http://www.toto.com, ftp://toto:toto@ftp.toto.com:21/

		this.regExpHexColor=/^#[0-9A-Fa-f]{6}$/g; // Accepte une couleur hexadécimale
	};
	CLASS.regExp.prototype = {
			// Expressions régulières de test de longueur
			
				//Les deux fonction suivantes servent ‡ identifier si une chaine de caractËre est compatible ou non avec une expression rÈguliËre passÈe en paramËtre
			matchRegularExpression:function(valeur, regularExpression){
					var resultat = valeur.match(regularExpression);
					if(resultat!=null && resultat.length==1) return true;
					else return false;
				},
				doesntMatchRegularExpression:function(valeur, regularExpression){
					if(this.matchRegularExpression(valeur, regularExpression)) return false;
					else return true;
				},
				/*
				les fonctions de contrôle suivantes prennent toutes comme argument la valeur de l'attribut "value" d'un champ de formulaire de type "text" ou "password"
				*/
				isEmpty:function(valeur){
					return this.matchRegularExpression(valeur, this.regExpEmpty);
				},
				isNotEmpty:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpEmpty);
				},
				isNot8CharsLength:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExp8Chars);
				},
				isNotAlphanumeric:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpAlphanumeric);
				},
				isNotAlphanumericWithWhitespace:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpAlphanumericWithWhitespace);
				},
				isNotAlphabetic:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpAlphabetic);
				},
				isNotNumeric:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpNumeric);
				},
				isNotInt:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpInt);
				},
				isNotDouble:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpDouble);
				},
				isNotFloat:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpInt);
				},
				isNotBoolean:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpBoolean);
				},
				isNotTime:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpTime);
				},
				isNotDate:function(valeur, mode){
					switch (mode)
					{
						case "fr" : 
							return this.doesntMatchRegularExpression(valeur, this.regExpFrenchDate);
							break;
						case "en" :
							return this.doesntMatchRegularExpression(valeur, this.regExpEnglishDate);
							break;
						default : 
							return this.doesntMatchRegularExpression(valeur, this.regExpFrenchDate);
							break;
					}
				},
				isNotCodePostal:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpCodePostal);
				},
				isNotTelephone:function(valeur, mode){
					switch (mode)
					{
						case "fixe" : 
							return this.doesntMatchRegularExpression(valeur, this.regExpTelephoneFixe);
							break;
						case "port" :
							return this.doesntMatchRegularExpression(valeur, this.regExpTelephonePortable);
							break;
						case "IP" :
							return this.doesntMatchRegularExpression(valeur, this.regExpTelephoneIP);
							break;
						case "nati" :
							return this.doesntMatchRegularExpression(valeur, this.regExpTelephoneNational);
							break;
						case "inte" :
							return this.doesntMatchRegularExpression(valeur, this.regExpTelephoneInternational);
							break;
						default : 
							return this.doesntMatchRegularExpression(valeur, this.regExpTelephoneNational);
							break;
					}
				},
				isNotNumeroSecuriteSociale:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpNumeroSecuriteSociale);
				},
				isNotTVAIntracommunautaire:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpTVAIntracommunautaire);
				},
				isNotNumeroSiren:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpNumeroSiren);
				},
				isNotNumeroSiret:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpNumeroSiret);
				},
				isNotCodeApe:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpCodeApe);
				},
				isNotEmailAdress:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpEmailAdress);
				},
				isNotIpAdress:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpIpAdress);
				},
				isNotDomainName:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpDomainName);
				},
				isNotUrl:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpUrl);
				},
				isNotHexColor:function(valeur){
					return this.doesntMatchRegularExpression(valeur, this.regExpHexColor);
				}
	};
})();

