
var FormatNombre=/^\d+$/;
var FormatDate=/^\d+[^\d]+\d+[^\d]+\d\d+$/;

/* Check validity of an Email address
==================================================================*/
function CheckEmail(email) {
	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=email.match(emailPat)
	if (matchArray==null) {	return false; }
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) { return false; }
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) { return false; }
	    }
	    return true;
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) { return false }
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) { return false; }
	if (len<2) { return false }
	return true;
}

/* Check validity of a number
==================================================================*/
function CheckNumber(numbertocheck) {
	if (numbertocheck.match(FormatNombre)) { return true; }
	return false;
}

/* Check validity of a date string
==================================================================*/
function CheckDate(datestring) {
	if (datestring.match(FormatDate)==null) { return false; }
	return true;
}

/* Check validity of a "code postal" string
==================================================================*/
function CheckCodePostal(codepostal,pays) {
	if (pays.match(/belgi/i)!=null) {
		if (codepostal.length != 4) { return false; }
		if (codepostal.match(FormatNombre)==null) { return false; }
	}
	if (pays.match(/canada/i)!=null || pays.match(/queb.c/i)!=null) {
		if (codepostal.length<6 || codepostal.length>7) { return false; }
	}
	if (pays.match(/france/i)!=null) {
		if (codepostal.length<5 || codepostal.length>6) { return false; }
		if (codepostal.match(FormatNombre)==null) { return false; }
	}
	if (codepostal == 0) { return false; }
	if (codepostal.length > 0 && codepostal.length < 2) { return false; }
	return true;
}

/* Check validity of an image file name
==================================================================*/
function CheckImageFileFormat(filename) {
	if (filename.match(/\.jpg$/i)==null && filename.match(/\.jpeg$/i)==null) {
		return false;
	}
	return true;
}
function CheckVideoFileFormat(filename) {
	if (filename.match(/\.avi$/i)==null && filename.match(/\.mpeg$/i)==null && filename.match(/\.mpg$/i)==null) {
		return false;
	}
	return true;
}

/* Check validity of a "town" string
==================================================================*/
function CheckVille(ville) {
	if (ville.length<2) { return false; }
	if (ville.match(/\d/)) { return false; }
	return true;
}

/* Check validity of a "country" string
==================================================================*/
function CheckCountry(pays) {
	if (pays.length<2) { return false; }
	if (pays.match(/\d/)) { return false; }
	return true;
}

/* Check validity of a RCS-SIRET-SIREN number
==================================================================*/
function CheckSiret(idprof,pays) {
	var noidprof=idprof;

	/* Remove non num chars */
    while (noidprof.search(/[^\d]+/) != -1) { noidprof = noidprof.replace(/[^\d]+/,""); }

	if (pays.match(/canada/i)!=null || pays.match(/queb.c/i)!=null) {
		if (noidprof.length < 6) { return false; }
		return true;
	}
	if (pays.match(/france/i)!=null) { /* Regle imposé par service repression des fraudes */
		if ((noidprof.length != 9) && (noidprof.length != 14) && (noidprof.length != 12)) { return false; }
		return true;
	}
	if (noidprof.length < 6) {	return false; }
	return true;
}

/* Check validity of a telephone number
==================================================================*/
function CheckTel(tel,pays) {

	/* Remove non num chars */
    while (tel.search(/[^\d]+/) != -1) { tel = tel.replace(/[^\d]+/,""); }

	if (tel == 0) { return false; }
	if (pays.match(/canada/i)!=null || pays.match(/queb.c/i)!=null) {
		return true;
	}
	if (pays.match(/france/i)!=null) {
		if (tel.length != 10) { return false; }
		if (tel.match(/^0/)==null) { return false; }
		return true;
	}
	if (tel.length < 6) { return false; }
	return true;
}

/* Check tatouage/puce
==================================================================*/
function CheckTatouage(tatouage,pays) {
	/* France   Tatouage = 4-6 alphanum, Puce = 15 chiffres */
	/* Belgique Tatouage = 4-6 alphanum (peut ne pas avoir de chiffres) */
	/* Canada   Tatouage = 4-6 alphanum */

	/* Remove unwanted string */
    while (tatouage.search(/\.+/) != -1) { tatouage = tatouage.replace(/\.+/,""); }
    while (tatouage.search(/:+/) != -1) { tatouage = tatouage.replace(/:+/,""); }
    tatouage = tatouage.replace(/^ +/i,"");
    tatouage = tatouage.replace(/^aucun/i,"");
	tatouage = tatouage.replace(/^non/i,"");
	tatouage = tatouage.replace(/^oui/i,"");
    tatouage = tatouage.replace(/^pas/i,"");
    tatouage = tatouage.replace(/^rien/i,"");
    tatouage = tatouage.replace(/^sans/i,"");
    tatouage = tatouage.replace(/de /i,"");
    tatouage = tatouage.replace(/no /i,"");
    tatouage = tatouage.replace(/n°/i,"");
    tatouage = tatouage.replace(/^ +/i,"");
    tatouage = tatouage.replace(/^lof/i,"");
    tatouage = tatouage.replace(/^loof/i,"");
    tatouage = tatouage.replace(/^loe/i,"");
    tatouage = tatouage.replace(/^losh/i,"");
    tatouage = tatouage.replace(/^nasds/i,"");
    tatouage = tatouage.replace(/^enregistr/i,"");
    tatouage = tatouage.replace(/^pedigr\w+/i,"");
    tatouage = tatouage.replace(/^tatou\w+/i,"");
	/* Enleve les espaces */
    while (tatouage.search(/\s+/) != -1) { tatouage = tatouage.replace(/\s+/,""); }

	if (tatouage.match(/,/)) {
		/* Si liste de num separe par , */
	    while (tatouage.search(/,/) != -1) { tatouage = tatouage.replace(/,/,""); }
	}
	else {
		/* Si num unique */

		/* Au moins un chiffre */
		if (tatouage.length > 6 && ! tatouage.match(/\d/)) { return false; }

		if (tatouage.length < 4) { return false; }
		if (tatouage.length > 15) { return false; }
	}
	return true;
}

/* Check pedigree
==================================================================*/
function CheckPedigree(pedigree,pays) {
	/* LOF    5-14 alphanum 123456/12345 */
	/* LOE                           */
	/* LOSH   5-7 alphanum  0874059  */
	/* Canada 2 char+6 num  AB123456 */

	/* Remove unwanted string */
    while (pedigree.search(/\.+/) != -1) { pedigree = pedigree.replace(/\.+/,""); }
    while (pedigree.search(/:+/) != -1) { pedigree = pedigree.replace(/:+/,""); }
    pedigree = pedigree.replace(/^ +/i,"");
    pedigree = pedigree.replace(/^aucun/i,"");
	pedigree = pedigree.replace(/^non/i,"");
	pedigree = pedigree.replace(/^oui/i,"");
    pedigree = pedigree.replace(/^pas/i,"");
    pedigree = pedigree.replace(/^rien/i,"");
    pedigree = pedigree.replace(/^sans/i,"");
    pedigree = pedigree.replace(/de /i,"");
    pedigree = pedigree.replace(/no /i,"");
    pedigree = pedigree.replace(/n°/i,"");
    pedigree = pedigree.replace(/^ +/i,"");
    pedigree = pedigree.replace(/^lof/i,"");
    pedigree = pedigree.replace(/^loof/i,"");
    pedigree = pedigree.replace(/^loe/i,"");
    pedigree = pedigree.replace(/^losh/i,"");
    pedigree = pedigree.replace(/^nasds/i,"");
    pedigree = pedigree.replace(/^enregistr/i,"");
    pedigree = pedigree.replace(/^pedigr\w+/i,"");
    pedigree = pedigree.replace(/^tatou\w+/i,"");
	/* Remove space */
    while (pedigree.search(/\s+/) != -1) { pedigree = pedigree.replace(/\s+/,""); }

	if (pays.match(/belgi/i)!=null) {
		if (pedigree.length < 5) { return false; }
		if (pedigree.length > 7) { return false; }
		return true;
	}
	if (pays.match(/canada/i)!=null || pays.match(/queb.c/i)!=null) {
		if (pedigree.length < 8) { return false; }
		if (pedigree.length > 8) { return false; }
		return true;
	}
	if (pays.match(/france/i)!=null) {
		if (pedigree.length < 4) { return false; }
		if (pedigree.length > 14) { return false; }
		return true;
	}
	return true;
}
