<!--
//==============================================
//	@File : lib_nodevo_txt.js
//	@Date : 08/08/2007  à partir du validate.js de Christhophe PETIT
//	@Auteur : D.CAMUS <dcamus@nodevo.com>
//	@Copyright : Nodevo 2007
//	@Version : 1.0
//	----------------------------
//	Librairie de gestion de chaines
//	----------------------------
//	Comprends	:
//		- txt_isInteger
//		- txt_isNumeric
//		- txt_isString
//		- txt_isStringStrict
//		- txt_isDate
//		- txt_isEmail
//		- txt_isEmails
//		- txt_isHttpSite
//		- txt_isFtpSite
//		- txt_y2k
//		- txt_countDaysBetween
//		- txt_splitString
//		- txt_formatString
//		- txt_isWellFormated
//		- txt_ltrim
//		- txt_rtrim
//		- txt_trim
//		- txt_replace
//*******************************************************************************



//-------------------------------------------------------------------------------
//	txt_isString	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string fait de min à max caractères, faux sinon
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isString(string, min, max) {

	var l = string.length;
	if (l < min || l > max)
		return false;
	return true;
}






//-------------------------------------------------------------------------------
//	txt_isSiret	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si le nic et le siren sont corrects
//	Création	:	13/08/2007, DCA (sur base source GREF-bretagne.com)
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isSiret(siren, nic) {

// Si nic <> chaine vide, renvoie VRAI si le numéro de SIRET est valide
// Si nic = chaine vide, renvoie VRAI si le numéro de SIREN est valide

	var TOT = 0;
	
	if (siren == "000000000") {
		//alert("Le numéro de SIRET saisi n'est pas valide !");
		return false;
	}
	
	for (var i = 0; i < siren.length; i++) {
		var carac = siren.charAt(i);
	
		if ((i+1) / 2 ==  Round((i+1) / 2)) {
			var valeur = 2 * parseInt(carac);
			if (valeur > 9) valeur = valeur - 9;
			TOT = TOT + valeur;
		}
		else
			TOT = TOT + parseInt(carac);
	}
	if (TOT / 10 != Round(TOT / 10)) {
		//alert("Le numéro de SIRET saisi n'est pas valide !");
		return false;
	}

	if (nic != "") {
		var siret = siren + nic;

		if (nic == "00000") {
			//alert("Le numéro de SIRET saisi n'est pas valide !");
			return false;
		}
			
		for (var i = 0; i < siret.length; i++) {
			var carac = siret.charAt(i);
			if ((i+1) / 2 !=  Round((i+1) / 2)) {
				var valeur = 2 * parseInt(carac);
				if (valeur > 9) valeur = valeur - 9;
				TOT = TOT + valeur;
			}
			else
				TOT = TOT + parseInt(carac);
		}
		if (TOT / 10 != Round(TOT / 10)) {
			//alert("Le numéro de SIRET saisi n'est pas valide !");
			return false;
		}
	}
	return true;
}



//-------------------------------------------------------------------------------
//	txt_isStringStrict	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si tous les caractères de string sont dans alphabet, faux sinon
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isRestrictString(string, alphabet, min, max) {
	var i = 0;
	var c = '';
	var l = string.length;
	
	if (l < min || l > max)
		return false;
		
	for (i = 0; i < l; i++)
	{
		c = string.charAt(i);
		if (alphabet.indexOf(c) == -1)
			return false;
	}
	return true;
}

//-------------------------------------------------------------------------------
//	txt_isTel	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si la chaine correspond à un numéro de tel
//	Création	:	14/08/2007, DCA
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isTel(string) {
	if (string.length == 0) return true;
	var regex = /^((0d\{9}|(0\d{1})|(\+\(\d{2}\)\d))\s?(\d{2})\s?)(\d{2})\s?(\d{2})\s?(\d{2})\s?$/;
	return (regex.exec(string)!=null)
}


//-------------------------------------------------------------------------------
//	txt_isEmail	(V.2.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string "ressemble à un e-mail ", faux sinon
//	Création	:	14/08/2007, DCA
//	Dern. modif	:	
//-------------------------------------------------------------------------------
function txt_isEmail(string) {
	if (string == "") return true;
	var reg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]{2,}[.][a-zA-Z]{2,3}$/
	 return (reg.exec(string)!=null)
}


//-------------------------------------------------------------------------------
//	txt_isDate	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string est une date au format jj-mm-aaaa (aaaa compris entre 2007 et 2009)
//	Création	:	15/08/2007, DCA
//	Dern. modif	:	
//-------------------------------------------------------------------------------
function txt_isDate(string) {
	if (string == "") return true;
	var reg = /^((([0]{1})[1-9]{1})|(([1-2]{1})[0-9]{1})|(([3]{1})([0-1]{1})))[-]((([0]{1})[1-9]{1})|(([1]{1})([0-2]{1})))[-]((20)([0-9]{2}))$/
	 return (reg.exec(string)!=null)
}



//-------------------------------------------------------------------------------
//	txt_isInteger	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string est uniquement composé de chiffres
//	Création	:	30/04/2008, DCA
//	Dern. modif	:	
//-------------------------------------------------------------------------------
function txt_isInteger(string) {
	if (string == "") return true;
	var reg = /^[0-9]+?$/
	 return (reg.exec(string)!=null)
}



//-------------------------------------------------------------------------------
//	txt_isNumeric	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string est uniquement composé de chiffres (+ séparateur virguke)
//	Création	:	14/08/2007, DCA
//	Dern. modif	:	
//-------------------------------------------------------------------------------
function txt_isNumeric(string) {
	if (string == "") return true;
	var reg = /^[0-9]+(,[0-9]+)?$/
	 return (reg.exec(string)!=null)
}


//-------------------------------------------------------------------------------
//	txt_isEmail	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string "ressemble à un e-mail ", faux sinon
//	Création	:	02/10/2001, DD
//	Dern. modif	:	14/08/2007, DCA > fonction remplacée par regexp
//-------------------------------------------------------------------------------
function txt_isEmailOLD(string) {
	if (string == "") return true;
	var alphabet = 
		"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_";

	// chercher @
	var pos1 = string.indexOf('@');
	if (pos1 < 0) return false; // pas de @
	var pos2 = string.lastIndexOf('@');
	if (pos1 != pos2) return false; // plusieurs @
	
	// à gauche, c'est la partie compte
	var account = string.substring(0, pos1-1);
	if (account=="") return false; // il faut un compte
	if (!txt_isRestrictString(account, alphabet)) return false; // caractères invalides ?
	
	// à droite, c'est la partie domaine
	var domain = string.substring(pos2+1, string.length-1);
	if (domain=="") return false; // il faut un domaine
	if (!txt_isRestrictString(domain, alphabet)) return false; // caractères invalides ?
	
	return true;
}

//-------------------------------------------------------------------------------
//	txt_isEmails	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string est une suiet d'e-mails séparés par delimiter
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isEmails(string, delimiter) {
	if (string == "") return true;
	
	var table = txt_splitString(string, delimiter);
	var i = 0;

	for (i = 0; i < table.length; i++)
		if (!isEmail(table[i]))
			return false;

	return true;
}



//-------------------------------------------------------------------------------
//	txt_isURL	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:		
//	Création	:	14/08/2007, DCA
//	Dern. modif	:	
//-------------------------------------------------------------------------------

function txt_isURL(string) {
	if (string == "") return true;
	var reg=/^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/;
	 return (reg.exec(string)!=null)
}	
	
	
//-------------------------------------------------------------------------------
//	txt_isHttpSite	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	UTILISER txt_isURL()	
//	Création	:	02/10/2001, DD
//	Dern. modif	:	
//-------------------------------------------------------------------------------
function txt_isHttpSite(string) {
	if (string == "") return true;
	else if (string.substring(0, 7).toUpperCase() == "HTTP://" || string.substring(0, 8).toUpperCase() == "HTTPS://")
		return true;
	
	return false;
}

//-------------------------------------------------------------------------------
//	txt_isFtpSite	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	UTILISER txt_isURL()	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isFtpSite(string) {
	if (string == "") return true;
	else if (string.substring(0, 6).toUpperCase() == "FTP://")
		return true;
	
	return false;
}

//-------------------------------------------------------------------------------
//	txt_y2k	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_y2k(number) {
	return (number < 1000) ? number + 1900 : number;
}

//-------------------------------------------------------------------------------
//	txt_countDaysBetween	(V.1.0.1)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_countDaysBetween(strDate1,strDate2) {
    //if (!isDate(strDate1) || !isDate(strDate2))
    //  return 0;
    //else {
        var date1 = new Date(strDate1.substring(6,10),strDate1.substring(3,5) - 1, strDate1.substring(0,2));
        var date2 = new Date(strDate2.substring(6,10),strDate2.substring(3,5) - 1, strDate2.substring(0,2));

        var difference =
            Date.UTC(txt_y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
          - Date.UTC(txt_y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
        
		//var difference = date1.valueOf() - date2.valueOf();

        return difference/1000/60/60/24;
    //}
}

//-------------------------------------------------------------------------------
//	txt_splitString	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	découpe la chaîne string en un tableau de n éléments équivalent à la fonction Split de VBScript et split de JavaScript 1.1
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_splitString(string, delimiter) {
	var table = new Array();
	var buffer = "";
	var index = 0;
	var i = 0;
	var c = '';
		
	for (i = 0; i < string.length; i++)
	{
		c = string.charAt(i);
		if (c == delimiter)
		{
			table[index] = buffer;
			index++;
			buffer = "";
		}
		else
			buffer += c;
	}
	if (buffer != "")
		table[index] = buffer;
	return table;
}

//-------------------------------------------------------------------------------
//	txt_formatString	(V.1.0.1)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:	27/09/2002, DD : remplacement des caractères non numériques	
//-------------------------------------------------------------------------------
function txt_formatString(strText, strFormat) {
	if (strText.length <= 0) return "";
	var strTextFormated = "";
	var i = 0;
	var j = 0;
	
	while (i<strText.length && j<strFormat.length){
		if (strFormat.charAt(j) == "#") {
			strTextFormated += strText.charAt(i);
			i++;
		}
		else {
			if (strFormat.charAt(j) == strText.charAt(i) || isNaN(strText.charAt(i))) {
				strTextFormated += strFormat.charAt(j);
				i++;
			}
			else
				strTextFormated += strFormat.charAt(j);
		}
		j++;

	}
	
	return strTextFormated;
}

//-------------------------------------------------------------------------------
//	txt_isWellFormated	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isWellFormated(strText, strFormat) {
	if (strText.length <= 0) return true;
	if (strText.length != strFormat.length) return false;
	
	for (i=0; i<strText.length; i++) {
		if (strFormat.charAt(i) != "#") {
			if (strText.charAt(i) != strFormat.charAt(i))
				return false;
		}
		else if (strText.charAt(i) < '0' || strText.charAt(i) > '9') {
			return false;
		}
	}
	return true;
}

//-------------------------------------------------------------------------------
//	txt_ltrim	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_ltrim(argvalue) {
  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }

  return argvalue;
}

//-------------------------------------------------------------------------------
//	txt_rtrim	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_rtrim(argvalue) {
  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}

//-------------------------------------------------------------------------------
//	txt_trim	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_trim(argvalue) {
  var tmpstr = txt_ltrim(argvalue);

  return rtrim(tmpstr);

}

//-------------------------------------------------------------------------------
//	txt_replace	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_replace(argvalue, x, y) {
  if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
    errmessage = "replace function error: \n";
    errmessage += "Second argument and third argument could be the same ";
    errmessage += "or third argument contains second argument.\n";
    errmessage += "This will create an infinite loop as it's replaced globally.";
    alert(errmessage);
    return false;
  }
    
  while (argvalue.indexOf(x) != -1) {
    var leading = argvalue.substring(0, argvalue.indexOf(x));
    var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, 
	argvalue.length);
    argvalue = leading + y + trailing;
  }

  return argvalue;
}

//-------------------------------------------------------------------------------
//	txt_replaceAll	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	20/05/2005, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_replaceAll(str, strRech, strRepl) {
	var strRegExp = new RegExp(strRech, "g");
	var strTemp = str;

	return str.replace(strRegExp, strRepl);
	
	//sql = sql.replace(/\+/g, "%2B");
}
//-------------------------------------------------------------------------------
//	txt_toUpperCase	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_toUpperCase(str)
{
  /*return s.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });*/
   return txt_removeAccents(str.toLowerCase()).toUpperCase();
}
