// NOTE : chaque fonction recoit les objets "champ" à traiter, il suffit ensuite dans utiliser les propriétés nécessaires pour le traitement (value, value.length, name ... etc.)

function isNumeric(tempChamp,libelle) {
	tempNum = new String(tempChamp.value);
	rExp = /,/gi;
	tempNum = tempNum.replace(rExp, ".");

	var pospoint = tempNum.lastIndexOf(".");

	if (isNaN(tempNum) && tempNum != "") {
		printMess(tempChamp,5,libelle);
		return false;
	} else {
		if (pospoint != -1) {
			var nbDec = (tempNum.length - 1 - pospoint);
			tempNum = new Number(tempNum);
			tempChamp.value = tempNum.toPrecision(pospoint+nbDec);
		} else {
			tempChamp.value = tempNum;
		}
	}
}
function isInteger(tempChamp,libelle) {
	var tempInt = tempChamp.value;
	var modele = /^[0-9]+$/;
	if (!modele.test(tempInt) && tempInt != "") {
		printMess(tempChamp,14,libelle);
		return false;
	}
}
function isAlphaNum(tempChamp,libelle){ 
	var field = tempChamp.value;
	var modele = /^[0-9A-Za-z]+$/;	
	if (!modele.test(field) && field != "") {
		printMess(tempChamp,8,libelle);
		return false;
	}
}
function maxLength(tempChamp,libelle) {
	if (tempChamp.value.length < 1) {
		printMess(tempChamp,6);
		return false;
	}
}
function textOblig(tempChamp,libelle) {
	if (tempChamp.value.length == 0 || tempChamp.value.charAt(0) == " ") {
		printMess(tempChamp,4,libelle);
		return false;
	}
}
function radioOblig(tempChamp,libelle) {
	var coche = 0;
	for (i=0;i<tempChamp.length;i++) {
		if (tempChamp[i].checked) {
			coche = 1;
		}
	}
	if (coche == 0) {
		printMess(tempChamp,10,libelle);
		return false;
	}
}
function selectOblig(tempChamp,libelle) {
	if (tempChamp.options[tempChamp.selectedIndex].value == 0) {
		printMess(tempChamp,9,libelle);
		return false;
	}
}
function zipCheck(tempChamp,libelle){
	var zip = tempChamp.value.toUpperCase();
	var modele = /^[A-Z]{1}[0-9]{1}[A-Z]{1}(-|| ){1}[0-9]{1}[A-Z]{1}[0-9]{1}$/;
	if (!modele.test(zip) && zip != "") {
		printMess(tempChamp,3,libelle);
		return false;
	}
}
function yearCheck(tempChamp,libelle){
	var tempAnnee = tempChamp.value;
	var modele = /^[0-9]{4}$/;
	if (!modele.test(tempAnnee) && tempAnnee != "") {
		printMess(tempChamp,13,libelle);
		return false;
	}
}
function emailCheck(tempChamp,libelle){
	var emailStr=tempChamp.value.toLowerCase();
	var checkTLD=1;
	var knownDomsPat=/^(com|ca|fr|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (emailStr == "")	{
		return true;
	}

	if (matchArray==null) {
		printMess(tempChamp,1,libelle);
		return false;
		}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			printMess(tempChamp,1,libelle);
			return false;
			}
		}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			printMess(tempChamp,1,libelle);
			return false;
			}
		}
	if (user.match(userPat)==null) {
		printMess(tempChamp,1,libelle);
		return false;
		}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				printMess(tempChamp,1,libelle);
				return false;
				}
			}
		return true;
		}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			printMess(tempChamp,1,libelle);
			return false;
			}
		}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		printMess(tempChamp,1,libelle);
		return false;
		}
	if (len<2) {
		printMess(tempChamp,1,libelle);
		return false;
		}
	return true;
}

function printMess(tempChamp,typeMess,libelle) {
	if (continuer) {
		switch(typeMess) {
			case 1 : // adresse de courriel non valide
				alert("L'adresse de courriel inscrite au champ \"" + libelle + "\" est invalide");
				tempChamp.focus();
				break;
			case 2 : // date non valide
				alert("La date inscrite au champ \"" + libelle + "\" n'est pas valide");
				tempChamp.focus();
				break;
			case 3 : // code postal non valide
				alert("Le code postal inscrit au champ \"" + libelle + "\" n'est pas valide");
				tempChamp.focus();
				break;
			case 4 : // champ obligatire
				alert("Le contenu du champ \"" + libelle + "\" est obligatoire");
				tempChamp.focus();
				break;
			case 5 : // format numérique obligatoire
				alert("Le contenu du champ \"" + libelle + "\" doit-être de type numérique.");
				tempChamp.focus();
				break;
			case 6 : // champs obligatoire
				alert("Le contenu du champ \"" + libelle + "\" ne peut contenir plus de X caractere");
				tempChamp.focus();
				break;
			case 7 : // telephone non valide
				alert("Le numéro de téléphone inscrit au champ \"" + libelle + "\" n'est pas valide");
				tempChamp.focus();
				break;
			case 8 : // contenu alphanumerique seulement
				alert("Le contenu du champ \"" + libelle + "\" doit-être de type alphanumériques (chiffres et lettres seulement)");
				tempChamp.focus();
				break;
			case 9 : // selection obligatoire
				alert("Vous devez sélectionner une option dans la liste \"" + libelle + "\"");
				tempChamp.focus();
				break;
			case 10 : // selection obligatoire
				alert("Vous devez choisir une option au champ \"" + libelle + "\"");
				break;
			case 11 : // selection obligatoire
				alert("L'adresse url inscrite au champ \"" + libelle + "\" est invalide.");
				tempChamp.focus();
				break;
			case 12 : // selection obligatoire
				alert("Le prix inscrit au champ \"" + libelle + "\" est invalide.");
				tempChamp.focus();
				break;
			case 13 : // selection obligatoire
				alert("L'année inscrite au champ \"" + libelle + "\" est invalide.");
				tempChamp.focus();
				break;
			case 14 : // selection obligatoire
				alert("Le chiffre inscrit au champ \"" + libelle + "\" doit-être un entier positif.");
				tempChamp.focus();
				break;
		}
		continuer=false;
	}
}

function telCheck(tempChamp,libelle){
	var tel = tempChamp.value;
	var modele = /^(\()?[0-9]{3}(\))?(-|.| ){1}[0-9]{3}(-|.| ){1}[0-9]{4}$/;
	if (!modele.test(tel) && tel != "") {
		printMess(tempChamp,7,libelle);
		return false;
	}
}

function urlCheck(tempChamp,libelle){
	var url = tempChamp.value;
	var modele = /^http|https|www/;
	if (!modele.test(url) && url != "") {
		printMess(tempChamp,11,libelle);
		return false;
	}
}


function dateCheck(tempChamp,libelle){
	var unedate = tempChamp.value;
	var modele = /^[1-2]{1}[0-9]{1}[0-9]{1}[0-9]{1}(-){1}[0-3]{1}[0-9]{1}(-){1}[0-3]{1}[0-9]{1}$/;
	if (!modele.test(unedate) && unedate != "") {
		printMess(tempChamp,2,libelle);
		return false;
	}
}
/*
function check_date(tempChamp,libelle) {
	var date_saisie = tempChamp.value;
	var checkstr = "0123456789";
	var DateValue = date_saisie;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "-";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;

	alert(date_saisie);

	for (i = 0; i < DateValue.length; i++) {
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
			DateTemp = DateTemp + DateValue.substr(i,1);
		}
	}
	DateValue = DateTemp;

	if (DateValue.length == 6) { DateValue = '20'; }
	if (DateValue.length != 8) { err = 19;}

	year = DateValue.substr(0,4);
	if (year == 0) { err = 20; }

	month = DateValue.substr(4,2);
	if ((month < 1) || (month > 12)) { err = 21; }

	day = DateValue.substr(6,2);
	if (day < 1) { err = 22; }

	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) { leap = 1; }
	if ((month == 2) && (leap == 1) && (day > 29)) { err = 23; }
	if ((month == 2) && (leap != 1) && (day > 28)) { err = 24; }

	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) { err = 25; }
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) { err = 26; }

	if ((day == 0) && (month == 0) && (year == 00)) { err = 0; day = ""; month = ""; year = ""; seperator = ""; }

	if (err == 0) { 
		date_saisie = (year + seperator + month + seperator + day); 
	} else { 
		date_saisie = "0000-00-00"; 
	}

	if (date_saisie == "0000-00-00") {
		printMess(tempChamp,2,libelle);
		return false;
	} else{
		tempChamp.value = date_saisie;
		return false;
	}
}
*/

function priceCheck(tempChamp,libelle) {
	// retour une chiffre valide... si c'est possible!
	prix = new String(tempChamp.value);
	var price="";
	var newprice="";
	var newdecimal="";
	var chiffre_valide = true;

	if (prix != "" && prix != 0) {
		rExp = /$/gi;
		prix = prix.replace(rExp, "");
		rExp = /,/gi;
		prix = prix.replace(rExp, ".");

		// s'il y a une virgule ou un point pour séparer les décimales
		var pospoint = prix.lastIndexOf(".");
		if ( pospoint != -1 && pospoint < prix.length){
			decimales=prix.substr(pospoint+1,2);
			for (i=0; i<decimales.length; i++){
				car=decimales.substr(i,1);
				if (!isNaN(car)){
					newdecimal+=car;
				}
			}
			price=prix.substr(0,pospoint);
			for (i=0; i<price.length; i++){
				car=price.substr(i,1);
				if (!isNaN(car)){
					newprice+=car;
				}
			}
			newdecimal=newdecimal*1;
			newprice=newprice*1;
			if (newdecimal >= 10) {
				price=newprice+(newdecimal/100);
			} else if (newdecimal >= 1) {
				price=newprice+(newdecimal/10);
			} else {
				price=newprice;
			}
		} else {
			for (i=0; i<prix.length; i++){
				car=prix.substr(i,1);
				if (!isNaN(car)){
					newprice+=car;
				}
				price=newprice*1;
			}
		}

		if (chiffre_valide && price > 0) {
			tempChamp.value = price;
		}else{
			printMess(tempChamp,12,libelle);
			return false;		
		}
	}
}