/**
 * Library : lib_utils.js
 * version 2.0
 * Last modification date : 01 Aout 2002
 * Contents : 
 * Format/type methods
 * String methods
 * Number methods
 */

/**
 * BEGIN FORMATS METHODS
 */
function isTypeString(s) {
	/*  Returns true  if the type of the parameter is a string. False otherwise */
	if(s==null) return false;
	return typeof(s) == "string";
}
function isTypeNumber(n){
	/* Returns true  if the type of the parameter is a number. False otherwise*/
	if(n==null) return false;
	return typeof(n) == "number";
}
function isTypeBoolean(b) {
	/* Returns true  if the type of the parameter is boolean. False otherwise*/
	if(b==null) return false;
	return typeof(b) == "boolean";
}
function isNumber(n) {
	/* Returns true  if the value assigned to parameter is number. False otherwise*/
	if(!isTypeNumber(n)) return false;
	return !isNaN(n);
}
function isAlpha(s) {
	/* Returns true  if the value assigned to parameter is alphanumeric. False otherwise*/
	if(!isTypeString(s)) return false;
	return isNaN(s);
}
function isBoolean(b) {
	/* Returns true  if the value assigned to parameter is boolean. False otherwise*/
	if(!isTypeBoolean(b)) return false;
	return (b==true || b==false);
}
/**
 * END FORMATS METHODS
 */
/**
 * BEGIN STRING METHODS
 */
function isWithSpace(s){
	/* Returns true si la chaine contient un espace ou est une chaine vide */
	if(!isAlpha(s)) return false;
	for (var i=0; i<s.length; i++)
		if(s.charAt(i)==" ")
			return true;
	return false;
}
function isWithQuote(s){
	/* Returns true string contains a quote caractères (') and not if contains (")*/
	if(!isAlpha(s)) return false;
	return s.indexOf("'", 0) != -1;
}
function noSpaces(s){
	/* remove spaces, tabs and break line characters*/
	if(!isAlpha(s)) return s;
	return RTrim(LTrim(s));
	}
function LTrim(str){
	/* Remove spaces at the left of the string '    AAAA' become 'AAAA'*/
	if(!isAlpha(str)) return str;
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if(whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}
	return s;
}
function RTrim(str){
	/* Remove spaces at the right of the string 'AAAA    ' become 'AAAA'*/
	if(!isAlpha(str)) return str;
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if(whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}
function isEmpty(s){
	/* Return true if string is null or length equals 0 (empty string). False otherwise.*/
	if(!isAlpha(s)) return s;
	return s.length == 0;
}
function capitalizeFirstLetter(s) {
	/* Set upper case the first letter of each word that can be found in the string. "hello world" becomes "Hello World". "mr jones smith" becomes "Mr Jones Smith" */
	if(!isTypeString(s)) return s;
	s = s.substring(0,1).toUpperCase() + s.substring(1, s.length);
	for(var i = 0 ; i < s.length ; i++) {
		if(s.substring(i, i+1)==" ") {
			s = s.substring(0, i+1) + (s.substring(i+1, i+2)).toUpperCase()+s.substring(i+2,s.length);
		}
	}
	return s;
}
function isEmail(ps_src){
	/* Returns true or false if the string is email valid.*/
	if(!isTypeString(ps_src)) return false;
	var vt_txt = new Array(2);	// tableau qui va contenir les chaines avant et apres l'arobase
	var vi_arobase;				// va contenir l'index de l'arobase dans la chaine
	var vc_tmp;					// caractere temporaire
	vi_arobase = ps_src.indexOf("@");
	if(vi_arobase > 0 && vi_arobase != (ps_src.length - 1) && vi_arobase == ps_src.lastIndexOf("@") &&  ps_src.indexOf("..") < 0) {
		vt_txt[0] = ps_src.substring(0,vi_arobase);					// prend la chaine avant l'arobase <=> nom
		vt_txt[1] = ps_src.substring(vi_arobase + 1,ps_src.length);	// prend la chaine apres l'arobase <=> adresse
		if((vt_txt[0].length == 0) || (vt_txt[1].length < 6)) {   //Si la premiere chaine est vide ou si le nomde domaine est inferieur a 3 lettres
			return false;
		}
		if((vt_txt[0].indexOf(".") != 0) && (vt_txt[1].indexOf(".") > 0) && (vt_txt[0].lastIndexOf(".") < (vt_txt[0].length - 1)) &&  (vt_txt[1].lastIndexOf(".") < (vt_txt[1].length - 2))) {
			for (var vi_char_nbr = 0 ; vi_char_nbr < vt_txt[0].length ; vi_char_nbr++){				
				vc_tmp = vt_txt[0].charAt(vi_char_nbr);// si le caractere recupere
				if((vc_tmp < '0' || vc_tmp > '9') && (vc_tmp < 'a' || vc_tmp > 'z') && (vc_tmp < 'A' || vc_tmp > 'Z') && (vc_tmp != '.') && (vc_tmp != '_') && (vc_tmp != '-'))
					return false;						// alors l'adresse est invalide
			}
			//Pour la seconde partie de l'e-mail no ne doit aps avoir de '_'
			for (var vi_char_nbr = 0;vi_char_nbr < vt_txt[1].length;vi_char_nbr++){
				vc_tmp = vt_txt[1].charAt(vi_char_nbr);// si le caractere recupere
					if((vc_tmp < '0' || vc_tmp > '9') && (vc_tmp < 'a' || vc_tmp > 'z') && (vc_tmp < 'A' || vc_tmp > 'Z') && (vc_tmp != '.') && (vc_tmp != '-'))
						return false;
			}
			return true;
		}
	}
	return false;
}
function isUrlOLD(value) 
{	/*
	if(value.search(/^([http]+[/:/]+[\///])+(.+)?[/\./]+[a-z]{2,4}$/) == -1)
	{
        return false;

	}
	else {
		return true;
	}*/
}

function isUrl(value){
	var ret;
	var protocol=value.split("://")[0]; // return http, or ftp, or https 
	var domain=(value.split("://")[1]).split(".")[2]; // return com, fr, net ...
	var site=(value.split("://")[1]).split("."+domain)[0]; // return www.example, subdomain.domain etc ...

	var cond1=protocol=="http" || protocol == "https" || protocol == "ftp";
	var cond2=(domain.length>=2) && (domain.length<=3);
	var cond3=site.split(".").length==2 ; // il y a deux parties dans le site.
	ret=cond1 && cond2 && cond3;

	return ret;
}

function badChars(theStr, someBadChars){
	/* Returns true if the string contains some one of the bad chars */
  if(!isTypeString(theStr)) return false;
  var i,theChar,isBad=false;
  for (var i=0; i<theStr.length; i++) {
    theChar = theStr.charAt(i);
    if(someBadChars.indexOf(theChar) != -1) isBad = true;
  }
  return isBad;
}
function StripChars(theFilter,theString){
	/* Returns theString with all occurrences of every char in theFilter */
	if(!theString)
		theString = "";
	var strOut,i,curChar;
	strOut = "";
	for (i=0;i < theString.length; i++)	{		
		curChar = theString.charAt(i);
		if(theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar;		
	}	
	return strOut;
}
/**
 * END STRING METHODS
 */

/**
 * BEGIN NUMBER METHODS
 */
function isMultiple(nbToTest, multipleRecherche) {
	//Retourne true si nbToTest est un multiple de multipleRecherche, false sinon
	if(nbToTest%multipleRecherche==0)
		return true;
	else 
		return false;
}
function AllInRange(x,y,theString){
	/**
	 * Returns true if all characters in theString fall in the range x,y (inclusive)
	 * Example:AllInRange("0", "9", "848393874") is true, AllInRange("0", "9", "22Hello33") is false
	 */
	var i, curChar;
	for (i=0; i < theString.length; i++){
		curChar = theString.charAt(i);
		if(curChar < x || curChar > y) //the char is not in range
			return false;
	}
	return true;
}
function isPair(pi_number) {/* Returns true if the number if par. False if odd*/return getParite(pi_number)==0;}
function getParite(pi_number) {/* Returns  0 if  par and 1 if odd(Can use the isPair function also)*/return pi_number%2?0:1;}
/**
 * END NUMBER METHODS
 */
function chrono() {
	this.startedTime = 0;
	this.stoppedTime = 0;
  	this.start = chrono_start;
	this.stop = chrono_stop;
	this.getChrono = chrono_get;
}
function chrono_start() {
	this.startedTime = 0;
	this.stoppedTime = 0;
	this.startedTime = new Date();
}
function chrono_stop() {
	this.stoppedTime  = new Date();
}
function chrono_get() {
	var vi_milli = this.stoppedTime - this.startedTime;//time in milliseconds
	return vi_milli;
}