﻿/* Validate.js, version 1.0.2
*  (c) 2006 achraf bouyakhsass <mutation[at]mutationevent.com>
* 
*  This software is licensed under the CC-GNU GPL
*  http://creativecommons.org/licenses/GPL/2.0/
*
*  For more details
*  http://www.mutationevent.com/project/validate.js
*
*  Package to validate various data :
*  hasValidChars
*  isSimpleIP
*  isAlphaLatin
*  isNotEmpty
*  isIntegerInRange
*  isNum
*  isEMailAddr
*  isZipCode
*  isDate
*  isMD5
*  isURL
*  isGuid
*  isISBN
*  isSSN
*  isDecimal
*  isplatform
*  addRules
*  Apply
/*--------------------------------------------------------------------------*/
var Class = {
	create: function() {
		return function() {
			this.initialize.apply(this, arguments);
		}
	}
}

function getValue(s){return document.getElementById(s).value}

var Validate = Class.create();
Validate.prototype = {
	/*--------------------------------------------------------------------------*/
	initialize:function(){
		this.error_array = []
		this.rules_array = [];
		this.e = true;
	},
	/*--------------------------------------------------------------------------*/
	hasValidChars:function(s, characters, caseSensitive){
		function escapeSpecials(s){
			return s.replace(new RegExp("([\\\\-])", "g"), "\\$1");
		}
		return new RegExp("^[" + escapeSpecials(characters) + "]+$",(!caseSensitive ? "i" : "")).test(s);
	},
	/*--------------------------------------------------------------------------*/
	/*--------------------------------------------------------------------------*/
	isAlphaLatin:function(string){
		alphaRegExp = /^[0-9a-z]+$/i
		return alphaRegExp.test(string);
	},
	/*--------------------------------------------------------------------------*/
	isNotEmpty:function (string){
		return /\S/.test(string);
	},
	/*--------------------------------------------------------------------------*/
	isEmpty:function(s){
		return !/\S/.test(s);
	},
	/*--------------------------------------------------------------------------*/
	/*--------------------------------------------------------------------------*/
	isNum:function(number){
		numRegExp = /^[0-9]+$/
		return numRegExp.test(number);
	},
	
	isIdCmd:function(number){
		numRegExp = /^[0-9]{10}$/
		return numRegExp.test(number);
	},
	/*--------------------------------------------------------------------------*/
	isEMailAddr:function(string){
		string = string.toLowerCase();
		//emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,4})$/
		emailRegExp = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,4}$/
		return emailRegExp.test(string);
	},
	/*--------------------------------------------------------------------------*/

	/*--------------------------------------------------------------------------*/
	isDate:function(date,format){
		if(!date) return false;
		if(!format) format = 'FR';
		
		switch(format){
			case'FR': RegExpformat = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/([2][0]|[1][9])\d{2})$/; break;
			case'US': RegExpformat = /^([2][0]|[1][9])\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/; break;
			case'SHORTFR': RegExpformat = /^([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/\d{2}$/; break;
			case'SHORTUS': RegExpformat = /^\d{2}\-([0]\d|[1][0-2])\-([0-2]\d|[3][0-1])$/; break;
			case'dd MMM yyyy':RegExpformat = /^([0-2]\d|[3][0-1])\s(Jan(vier)?|Fév(rier)?|Mars|Avr(il)?|Mai|Juin|Juil(let)?|Aout|Sep(tembre)?|Oct(obre)?|Nov(ember)?|Dec(embre)?)\s([2][0]|[1][19])\d{2}$/; break;
			case'MMM dd, yyyy':RegExpformat = /^(J(anuary|u(ne|ly))|February|Ma(rch|y)|A(pril|ugust)|(((Sept|Nov|Dec)em)|Octo)ber)\s([0-2]\d|[3][0-1])\,\s([2][0]|[1][9])\d{2}$/; break;
		}
		
		return RegExpformat.test(date);
	},
	/*--------------------------------------------------------------------------*/

	/*--------------------------------------------------------------------------*/

	/*--------------------------------------------------------------------------*/
	
	isDecimal:function(number){// positive or negative decimal
		if(!number) return false;
		decimalRegExp = /^-?(0|[1-9]{1}\d{0,})(\.(\d{1}\d{0,}))?$/
		return decimalRegExp.test(number);
	},
	
	/*--------------------------------------------------------------------------*/
	getValue:function(id){
		document.getElementById(id).value;
	},
	/*--------------------------------------------------------------------------*/
	addRules:function(rules){
		this.rules_array.push(rules);
	},
	/*--------------------------------------------------------------------------*/
	check:function(){
		this.error_array = [];
		this.e = true;
		for(var i=0;i<this.rules_array.length;i++){
			switch(this.rules_array[i].option){
				/*--------------------------------------------------------------------------*/
				
				/*--------------------------------------------------------------------------*/
				case'AlphaLatin':
					if (this.isAlphaLatin(getValue(this.rules_array[i].id))){
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'pink' } );
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else {
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'white' } );	
					}
				break;
				/*--------------------------------------------------------------------------*/
				case'required':
					if (this.isEmpty(getValue(this.rules_array[i].id))){
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'pink' } );
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else {
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'white' } );	
					}
				break;
				/*--------------------------------------------------------------------------*/
				
				/*--------------------------------------------------------------------------*/
				case'Number':
					if (!this.isNum(getValue(this.rules_array[i].id))){
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'pink' } );
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else {
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'white' } );	
					}
				break;

/*--------------------------------------------------------------------------*/

				case'IdCmd':
					if (!this.isIdCmd(getValue(this.rules_array[i].id))){
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'pink' } );
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else {
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'white' } );	
					}
				break;
				/*--------------------------------------------------------------------------*/
				case'email':
					if (!this.isEMailAddr(getValue(this.rules_array[i].id))){
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'pink' } );
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else {
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'white' } );	
					}
				break;
				/*--------------------------------------------------------------------------*/
				
				/*--------------------------------------------------------------------------*/
				case'date':
					if(!this.isDate(getValue(this.rules_array[i].id),this.rules_array[i].format)){
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'pink' } );
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else {
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'white' } );	
					}
				break;
				/*--------------------------------------------------------------------------*/
				
				/*--------------------------------------------------------------------------*/
				case'Decimal':
					if(!this.isDecimal(getValue(this.rules_array[i].id))){
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'pink' } );
						this.error_array.push(this.rules_array[i].error);
						this.e = false;
					}
					else {
						Element.setStyle( this.rules_array[i].id , { backgroundColor: 'white' } );	
					}
				break;
				/*--------------------------------------------------------------------------*/
			}
			
		}
	},
	/*--------------------------------------------------------------------------*/
	Apply:function(el){
		this.check();
		if(this.e){
			return true;
		}else{
			var endMsg = this.error_array;
			if(!el){
				alert( correction+ ' : \n\n\t' + this.error_array.toString().replace(/\,/gi,"\n\t"));
				document.location = "#errorclient";
			}else{
				Element.setStyle( el , { display: ''});
				document.getElementById(el).innerHTML = FormError + BaliseDeb + this.error_array.toString().replace(/\,/gi, jointure ) + BaliseFin;
				document.location = "#errorclient";
			}
			topelement = Element.getHeight('center') + 15;
			//Element.setStyle( 'footer' , { top: topelement } );
			Element.setStyle( 'footer' , { bottom: 0 });
			Element.setStyle( 'content' , { marginBottom: '5px'});
			return false;
		}
	}
	/*--------------------------------------------------------------------------*/
}
