//*******************************************************************************
//	lib_txt.js	(V.2.2)
//*******************************************************************************
//	Description	:	gestion de chaine
//	Création	:	02/10/2001, DD à partir du validate.js de Christhophe PETIT
//	Dern. modif	:	
//	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
//*******************************************************************************

var lib_txt = true;


//-------------------------------------------------------------------------------
//	txt_isInteger	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string est un entier compris entre min et max, faux sinon
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isInteger(string, min, max) {
	// ne prend pas en compte les espaces
	var i = 0;

	//if (string == '') return false;

	for (i = 0; i < string.length; i++)
	{
		var c = string.charAt(i);
		
		if ((string.charAt(0) != '-') || (c != '-')){
			if (c < '0' || c > '9')
				return false;
		}
	}
	
	i = parseInt(string, 10);

	if (i < min)
		return false;
	if (i > max)
		return false;
	return true;
}

//-------------------------------------------------------------------------------
//	txt_isNumeric	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isNumeric(string, min, max) {
	var l = string.length;
	if (l < min || l > max) return false;

	for (i=0; i<l; i++)
		if (string.charAt(i) < '0' || string.charAt(i) > '9')
			return false;
	
	return true;
}

//-------------------------------------------------------------------------------
//	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_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_isDate	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	renvoie vrai si string est une date valide dont l'année est comprise entre date1 et date2
//	Création	:	02/10/2001, DD
//	Dern. modif	:		
//-------------------------------------------------------------------------------
function txt_isDate(string, date1, date2) {
	var dateDelimiter = '/';
	var dateYear      = 2;
	var dateMonth     = 1;
	var dateDay       = 0;
	var dateYearMin   = 1900;
	var dateYearMax   = 2100;

	// vérif du format
	if (string == "") return true;

	var table = txt_splitString(string, "/");
	var d = 0, m = 0, y = 0;
	var ndm = new Array(12);
			
	if (table.length != 3)
		return false;
	y = parseInt(table[dateYear ], 10);
	m = parseInt(table[dateMonth], 10);
	d = parseInt(table[dateDay  ], 10);
	if (""+y=="NaN") return false;
	if (""+m=="NaN") return false;
	if (""+d=="NaN") return false;
	if (!txt_isInteger(y, dateYearMin, dateYearMax))
		return false;
	if (!txt_isInteger(m,           1,          12))
		return false;
	ndm[ 1] = 31; ndm[ 2] =  0; ndm[ 3] = 31; ndm[ 4] = 30;
	ndm[ 5] = 31; ndm[ 6] = 30; ndm[ 7] = 31; ndm[ 8] = 31;
	ndm[ 9] = 30; ndm[10] = 31; ndm[11] = 30; ndm[12] = 31;
	if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
		ndm[2] = 29;
	else
		ndm[2] = 28;
	if (!txt_isInteger(d, 1, ndm[m]))
		return false;

	// vérif si dépassement limite
	if (date1 != "")
		if (txt_countDaysBetween(string, date1) < 0)
			return false;
	if (date2 != "")
		if (txt_countDaysBetween(string, date2) > 0)
			return false;
	
	return true;	
}

//-------------------------------------------------------------------------------
//	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	:		
//-------------------------------------------------------------------------------
function txt_isEmail(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_isHttpSite	(V.1.0.0)
//-------------------------------------------------------------------------------
//	Description	:	
//	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	:	
//	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");
}