//Dernière modification le 03/09/2003.

/* Fonctions présentes dans ce fichier:
****************************************
   isEmpty(s)
   isInteger(s [,eok])
   isDate (dd, mm, yyyy [,eok])
   isSecond(ss)
   isMinute(mm)
   isHour(mm)
   getStandardDate(dd, mm, yyyy)
   getStandardHour(ss, mm, hh)
   compareDate(date1, date2, sign)
   getSystemDate()
   getSystemTime()
   getCurrentYear()
   getCurrentMonth()
   getCurrentDay()
   getCurrentHours()
   getCurrentMinutes()
   getCurrentSeconds()
   getDaysInMonth(date)
   checkURL(s)
   fillTime(time)
   lTrim(s) ** 03/09/2003
   rTrim(s) ** 03/09/2003
   trim(s)  ** 03/09/2003
   Len(str)
   Left(str, n)
   Right(str, n)
   Mid(str, start, len)
   InStr(strSearch, charSearchFor)
   --getSearchAsArray(stringToSearch)  Ne plus activer sur le site culture (public) : déjà presente dans tplgraphics.xsl
****************************************
*/


// ----------------------------------
// isEmpty(s)
// ----------------------------------
// Retourne true si le champ s est vide.

function isEmpty(s) {
	return ((s == null) || (trim(s).length == 0));
}


// ----------------------------------
// isInteger(s [,eok])
// ----------------------------------
// Retourne true si tous les caractères du champ s sont des chiffres.

function isInteger(s) {
  var i;

	if(isEmpty(s)) {
		if(isInteger.arguments.length == 1) {
			return (false);
		} else {
			return (isInteger.arguments[1] == true);
		}
	}

	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if((c < "0") || (c > "9")) return (false);
	}

	return (true);
}


// ----------------------------------
// isDate (dd, mm, yyyy [,eok])
// ----------------------------------
// Retourne true si la date est valide.

function isDate(dd, mm, yyyy) {
	if(isEmpty(dd) && isEmpty(mm) && isEmpty(yyyy)) {
		if(isDate.arguments.length == 3) {
			return (false);
		} else {
			return (isDate.arguments[3] == true);
		}
	}

	if (!isInteger(dd) || !isInteger(mm) || !isInteger(yyyy)) {	return (false);	}

	if (dd < 1 || dd > 31) { return (false); }
	if (mm < 1 || mm > 12) { return (false); }
	if ((yyyy.toString()).length != 4) { return (false); }
	if ((mm==4 || mm==6 || mm==9 || mm==11) && dd==31) { return (false); }

	if (mm == 2) {
		var isleap = (yyyy % 4 == 0 && (yyyy % 100 != 0 || yyyy % 400 == 0));
		if (dd > 29 || (dd==29 && !isleap)) {
			return false;
		}
	}

	return (true);
}


// ----------------------------------
// isSecond(ss)
// ----------------------------------
// Retourne true si le nombre de secondes est valide (0-59).

function isSecond(ss) {
	if (!isInteger(ss)) {	return (false);	}
	if (ss > 59)				{ return (false); }
	return (true);
}


// ----------------------------------
// isMinute(mm)
// ----------------------------------
// Retourne true si le nombre de minutes est valide (0-59).

function isMinute(mm) {
	if (!isInteger(mm)) {	return (false);	}
	if (mm > 59)				{ return (false); }
	return (true);
}


// ----------------------------------
// isHour(mm)
// ----------------------------------
// Retourne true si le nombre d'heures est valide (0-23).

function isHour(hh) {
	if (!isInteger(hh)) {	return (false);	}
	if (hh > 23)				{ return (false); }
	return (true);
}


// ----------------------------------
// getStandardDate(dd, mm, yyyy)
// ----------------------------------
// Retourne une date au format standard (yyyymmdd).

function getStandardDate(dd, mm, yyyy) {
	if (!isDate(dd, mm, yyyy)) {
		return (false);
	} else {
		if ((dd.toString()).length == 1) { dd = '0' + dd;	}
		if ((mm.toString()).length == 1) { mm = '0' + mm;	}

		return (yyyy.toString() + mm.toString() + dd.toString());
	}
}


// ----------------------------------
// getStandardHour(ss, mm, hh)
// ----------------------------------
// Retourne une heure au format standard (hhmmss).

function getStandardHour(ss, mm, hh) {
	if (!isSecond(ss) || !isMinute(mm) || !isHour(hh)) {
		return (false);
	} else {
		if ((ss.toString()).length == 1) { ss = '0' + ss;	}
		if ((mm.toString()).length == 1) { mm = '0' + mm;	}
		if ((hh.toString()).length == 1) { hh = '0' + hh;	}

		return (hh.toString() + mm.toString() + ss.toString());
	}
}


// ----------------------------------
// compareDate(date1, date2, sign)
// ----------------------------------
// Comparaison de deux dates au format standard.
// Le champ sign détermine le type de comparaison, et peut avoir
// les valeurs suivantes: "<", "<=", ">", ">=", "=".

function compareDate(date1, date2, sign) {
	if (date1.length!=8) { return (false); }
	if (date2.length!=8) { return (false); }

	switch (sign) {
		case '<' :
			if (date1 >= date2) { return (false); }
			break;
		case '<=' :
			if (date1 > date2)  { return (false); }
			break;
		case '>' :
			if (date1 <= date2) { return (false); }
			break;
		case '>=' :
			if (date1 < date2)  { return (false); }
			break;
		case '=' :
			if (date1 != date2) { return (false); }
			break;
		default :
			return (false);
			break;
	}

	return (true);
}


// ----------------------------------
// getSystemDate()
// ----------------------------------
// Retourne la date système au format standard (yyyymmdd).

function getSystemDate() {
	return (getStandardDate(getCurrentDay(), getCurrentMonth(), getCurrentYear()));
}


// ----------------------------------
// getSystemTime()
// ----------------------------------
// Retourne l'heure système au format standard (hhmm).

function getSystemTime() {
	var hh = getCurrentHours();
	var mm = getCurrentMinutes();
	var ss = getCurrentSeconds();
	return (hh.toString() + mm.toString() + ss.toString());
}


// ----------------------------------
// getCurrentYear()
// ----------------------------------
// Retourne l'année courante.

function getCurrentYear() {
	var today = new Date();
	today = today.getYear();
	return (today < 1900 ? 1900 + today : today);
}


// ----------------------------------
// getCurrentMonth()
// ----------------------------------
// Retourne le mois de l'année courante (01-12).

function getCurrentMonth() {
	var today = new Date();
	today = today.getMonth() + 1;
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// getCurrentDay()
// ----------------------------------
// Retourne le jour du mois courant (01-31).

function getCurrentDay() {
	var today = new Date();
	today = today.getDate()
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// getCurrentHours()
// ----------------------------------
// Retourne l'heure du jour courant (00-23).

function getCurrentHours() {
	var today = new Date();
	today = today.getHours();
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// getCurrentMinutes()
// ----------------------------------
// Retourne le nombre de minutes de l'heure courante (00-59).

function getCurrentMinutes() {
	var today = new Date();
	today = today.getMinutes();
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// getCurrentSeconds()
// ----------------------------------
// Retourne le nombre de secondes de la minute courante (00-59).

function getCurrentSeconds() {
	var today = new Date();
	today = today.getSeconds();
	return (today < 10 ? '0' + today : today);
}


// ----------------------------------
// checkURL(s)
// ----------------------------------

function checkURL(s) {
   var result = s;
   if(result.indexOf("http://") != 0 && result.indexOf("ftp://") != 0) {
      result = "http://" + result;
   }
   return result;
}


// ----------------------------------
// fillTime(time)
// ----------------------------------
// Verifie que le nombre de caractères encodés pour une heure, une minute ou une seconde est bien formatté sur 2 chiffres
// et rempli ces dernières au cas ou elles ne le seraient pas

function fillInteger(inti, longl) {
	if(!isInteger(inti.value, true)) { return false; }

	for(i = inti.value.length; i < longl; i++) {
		inti.value = "0" + inti.value;
	}

	return true;
}


// ============================================================================
// lTrim(string) : Returns a copy of a string without leading spaces.
//
// PURPOSE: Remove leading blanks from our string.
// IN: str - the string we want to LTrim
// ============================================================================
function lTrim(str) {
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \f\n\r\t");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
       // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

// ============================================================================
// rTrim(string) : Returns a copy of a string without trailing spaces.
//
// PURPOSE: Remove trailing blanks from our string.
// IN: str - the string we want to RTrim
// ============================================================================
function rTrim(str) {
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \f\n\r\t");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      // Get length of string
      var i = s.length - 1;

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;

      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}


// ============================================================================
// trim(string) : Returns a copy of a string without leading or trailing spaces
//
// PURPOSE: Remove trailing and leading blanks from our string.
// IN: str - the string we want to Trim
// RETVAL: a Trimmed string
// ============================================================================
function trim(str) {
   return rTrim(lTrim(str));
}

// ============================================================================
// Len(str) : Returns the number of characters in a string
//
// IN: str - the string whose length we are interested in
// RETVAL: The number of characters in the string
// ============================================================================
function Len(str) {
	return String(str).length;
}


// ============================================================================
// Left(str, n) : Returns a specified number of characters from the left side
//								of a string
//
// IN: str - the string we want to return the left part
//		 n - the number of characters we want to return
// RETVAL: n characters from the left side of the string
// ============================================================================

function Left(str, n) {
	if (n <= 0) { // Invalid bound, return blank string
   	return "";
  }
  else if (n > String(str).length) { // Invalid bound, return
    return str; // entire string
  }
  else { // Valid bound, return appropriate substring
    return String(str).substring(0,n);
  }
}


// ============================================================================
// Right(str, n) : Returns a specified number of characters from the right side
//								 of a string
//
// IN: str - the string we want to return the right part
//		 n - the number of characters we want to return
// RETVAL: n characters from the left side of the string
// ============================================================================

function Right(str, n) {
	if (n <= 0) { // Invalid bound, return blank string
  	return "";
  }
  else if (n > String(str).length) {// Invalid bound, return
  	return str; // entire string
  }
  else { // Valid bound, return appropriate substring
  	var iLen = String(str).length;
 		return String(str).substring(iLen, iLen - n);
  }
}


// ============================================================================
// Mid(str, start, len) : Returns a specified number of characters from a
//								        string
//
// IN: str - the string we are testing
//     start - our string's starting position (0 based!!)
//     len - how many characters from start we want to get

// RETVAL: The substring from start to start+len
// ============================================================================

function Mid(str, start, len) { // Make sure start and len are within proper bounds
	var iEnd;
	var iLen = String(str).length;

	if (len == "end") {
   	len = iLen;
  }

	if (start < 0 || len < 0) {
		return "";
	}
   else {
   	if (start + len > iLen) {
	  	iEnd = iLen;
	  }
	  else {
	  	iEnd = start + len;
		}
		return String(str).substring(start,iEnd);
	}
}


// ============================================================================
// InStr(strSearch, charSearchFor) : Returns the location a character
//								                   (charSearchFor) was found in the string
//																	 strSearch (If the character is not found,
//																	 -1 is returned.)
//
// REQUIRES USE OF Mid function - Len function
// ============================================================================

function InStr(strSearch, charSearchFor) {
	for (i=0; i < Len(strSearch); i++) {
		if (charSearchFor == Mid(strSearch, i, 1)) {
			return i;
		}
	}
	return -1;
}

// =================================================================================
// getDaysInMonth(date) : returns the number of days in the month specified as param

function getDaysInMonth(mydate) {
	for(cpt = 31; cpt > 27; cpt--) {
		if(isDate(cpt, mydate.getMonth() + 1, mydate.getFullYear() )) {
			return cpt;
		}
	}
}