var P6_validation = {
	invalid : new Array(),
	getElement:function(obj){
		if(!document.getElementById(obj.id)) return false;
		else return champ = document.getElementById(obj.id);		
	},
	email : function(e, champs){
		champs = (P6_validation.getElement(champs) != false) ? P6_validation.getElement(champs) : "";  
		var value = champs.value;		 
		if(value == "") return false;
//		var myReg = new RegExp ( "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)(\.[a-z]{2,4}){1}$", "gi" ) ;
		var myReg = new RegExp ( "^([a-z0-9_.-]+)@([a-z0-9_.]+)([a-z0-9_.]*)\.([a-z]){2,4}$", "gi" ) ;
		if (myReg.test(value) === false){
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
			var erreur = document.createElement("div");
			erreur.id = "error" + champs.name;
			erreur.style.display = "block";
			erreur.style.color = "red";
			erreur.style.fontSize = "11px";
			erreur.innerHTML = "Votre email n'est pas valide !";
			champs.parentNode.appendChild(erreur);
			champs.style.borderColor = "red";
			if(!P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid[P6_validation.invalid.length] = champs.name;
		}
		else{
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
			champs.style.borderColor = "";
			if(P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid = P6_validation.unset(P6_validation.invalid, champs.name);
		}
	},
	number : function(e, champs){
		champs = (P6_validation.getElement(champs) != false) ? P6_validation.getElement(champs) : "";
		alert(typeOf(P6_validation.getElement(champs)));
	},
	needed : function(e, champs){
		champs = (P6_validation.getElement(champs) != false) ? P6_validation.getElement(champs) : "";
		//alert(champs);
		if(champs.value == ""){
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
			var erreur = document.createElement("div");
			erreur.id = "error" + champs.name;
			erreur.style.display = "block";
			erreur.style.color = "red";
			erreur.style.fontSize = "11px";
			erreur.innerHTML = "Votre champs est vide !";
			champs.parentNode.appendChild(erreur);
			champs.style.borderColor = "red";
			if(!P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid[P6_validation.invalid.length] = champs.name;
		}
		else{
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
			if(P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid = P6_validation.unset(P6_validation.invalid, champs.name);
			champs.style.borderColor = "";
		}
	},
	neededCheckbox:function(e, champs){
		champs = (P6_validation.getElement(champs) != false) ? P6_validation.getElement(champs) : "";
		if(champs.checked){
			if(P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid = P6_validation.unset(P6_validation.invalid, champs.name);
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
		}
		else{
			if(!P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid[P6_validation.invalid.length] = champs.name;
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
			var erreur = document.createElement("div");
			erreur.id = "error" + champs.name;
			erreur.style.display = "block";
			erreur.style.color = "red";
			erreur.style.fontSize = "11px";
			erreur.innerHTML = "Ce champs est obligatoire !";
			champs.parentNode.appendChild(erreur);
			champs.style.borderColor = "red";
			if(!P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid[P6_validation.invalid.length] = champs.name;
		}
	},
	neededRadio:function(e, champs){
		champs = (P6_validation.getElement(champs) != false) ? P6_validation.getElement(champs) : "";
		var valid = false;
		for(i in champs){
			if(champs[i].checked) valid = true;
		}
		
		if(valid){
			if(P6_validation.inArray(P6_validation.invalid, champs[0].name)) P6_validation.invalid = P6_validation.unset(P6_validation.invalid, champs[0].name);
			if(document.getElementById("error" + champs[0].name)) P6_validation.removeElement(document.getElementById("error" + champs[0].name));
		}
		else{
			if(!P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid[P6_validation.invalid.length] = champs[0].name;
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs[0].name));
			var erreur = document.createElement("div");
			erreur.id = "error" + champs.name;
			erreur.style.display = "block";
			erreur.style.color = "red";
			erreur.style.fontSize = "11px";
			erreur.innerHTML = "Your input is empty !";
			champs[0].parentNode.appendChild(erreur);
			champs[0].style.borderColor = "red";
			if(!P6_validation.inArray(P6_validation.invalid, champs[0].name)) P6_validation.invalid[P6_validation.invalid.length] = champs[0].name;
		}
	},
	minlength:function(e, champs){
		var length = champs.size;
		champs = (P6_validation.getElement(champs) != false) ? P6_validation.getElement(champs) : "";
		var value = champs.value;
		
			if(value != ""){
			if((value.length < length) && (value.length > 0)){
				if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
				var erreur = document.createElement("div");
				erreur.id = "error" + champs.name;
				erreur.style.display = "block";
				erreur.style.color = "red";
				erreur.style.fontSize = "11px";
				erreur.innerHTML = length+" characters minimum";
				champs.parentNode.appendChild(erreur);
				champs.style.borderColor = "red";
				if(!P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid[P6_validation.invalid.length] = champs.name;
			}
		else{
			if(document.getElementById("error" + champs.name)) P6_validation.removeElement(document.getElementById("error" + champs.name));
			if(P6_validation.inArray(P6_validation.invalid, champs.name)) P6_validation.invalid = P6_validation.unset(P6_validation.invalid, champs.name);
			champs.style.borderColor = "";
		}
		}
	},
	maxlength:function(e, form){
		var length = form.size;
		form = (P6_validation.getElement(form) != false) ? P6_validation.getElement(form) : "";
		form.value=form.value.substr(0,length);
	},
	onSubmitForm : function(e, form){
		form = (P6_validation.getElement(form) != false) ? P6_validation.getElement(form) : "";
		if(P6_validation.invalid.length == 0) form.submit();
		else {
			for(i in P6_validation.invalid){
				if(!document.getElementById("error" + P6_validation.invalid[i])){
				eval("var champs = form."+P6_validation.invalid[i]); 
				var erreur = document.createElement("div");
				erreur.id = "error" + P6_validation.invalid[i];
				erreur.style.display = "block";
				erreur.style.color = "red";
				erreur.style.fontSize = "11px";
				erreur.innerHTML = "Votre champs est vide !";
				try{
					champs.parentNode.appendChild(erreur);
					champs.style.borderColor = "red";
					}
				catch(e){
					champs[champs.length-1].parentNode.appendChild(erreur);
					champs[champs.length-1].style.background = "red";
				}
				
				}
			}
		}
	},
	onSubmitFormModal : function(form){
		form = (P6_validation.getElement(form) != false) ? P6_validation.getElement(form) : "";
		if(P6_validation.invalid.length == 0) return true;
		
		else {
			for(i in P6_validation.invalid){
				if(!document.getElementById("error" + P6_validation.invalid[i])){
				eval("var champs = form."+P6_validation.invalid[i]); 
				var erreur = document.createElement("div");
				erreur.id = "error" + P6_validation.invalid[i];
				erreur.style.display = "block";
				erreur.style.color = "red";
				erreur.style.fontSize = "11px";
				erreur.innerHTML = "Your input is empty !";
				try{
					champs.parentNode.appendChild(erreur);
					champs.style.borderColor = "red";
					}
				catch(e){
					champs[champs.length-1].parentNode.appendChild(erreur);
					champs[champs.length-1].style.background = "red";
				}
				
				}
			}
		}
	},
	removeElement : function(div){
		var Node = div;
   		Node.parentNode.removeChild(Node);
	},
	inArray:function(array, valeur){
		for (var i in array) { if (array[i] == valeur) return true;}
		return false;
	},
	unset: function(array, value){
	var output=[];
	for(var i in array){
		if (array[i] != value)
			output[i] = array[i];
		}
		return output;
	}
}