// Last modification : 06/03/2006.

/* Functions in this file :

	 f_isEmpty(s)
	 f_isInteger(s [,eok])
	 f_isDate(dd, mm, yyyy [,eok])
	 f_isTime(hh, mm, ss)
	 f_isSecond(ss)
	 f_isMinute(mm)
	 f_isHour(mm)
	 f_getStandardDate(dd, mm, yyyy [,strSplit])
	 f_getStandardTime(hh, mm, ss [,strSplit])
	 f_compareDate(date1, date2, sign)
	 f_compareDateTime(datetime1, datetime2, sign)
	 f_getSystemDate()
	 f_getSystemTime()
	 f_getCurrentYear()
	 f_getCurrentMonth()
	 f_getCurrentDay()
	 f_getCurrentHour()
	 f_getCurrentMinute()
	 f_getCurrentSecond()

	 f_trim(s)
	 f_lTrim(s)
	 f_rTrim(s)

	 f_len(s)
	 f_left(s, n)
	 f_right(s, n)
	 f_mid(s, start, len)
	 f_instr(strText, strFind)

	 f_countCharacter(strText, strFind)
	 f_normalizeSpace(s)
	 f_removeFromString(s)
	 f_replaceAccents(s)
	 f_replaceAll(strOld, strFind, strReplace [, boolRegExp])
*/

// --------------------------------------------------------------------------------
// f_isEmpty(s)
// --------------------------------------------------------------------------------
// Returns TRUE when s is empty.

function f_isEmpty(s) {
	return ((s == null) || (f_trim(s.toString()).length == 0));
}


// --------------------------------------------------------------------------------
// f_isInteger(s [,eok])
// --------------------------------------------------------------------------------
// Returns TRUE when all characters of s are integers.

function f_isInteger(s) {
	var i;

	if (f_isEmpty(s)) {
		if (f_isInteger.arguments.length == 1) {
			return (false);
		} else {
			return (f_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);
}


// --------------------------------------------------------------------------------
// f_isDate(dd, mm, yyyy [,eok])
// --------------------------------------------------------------------------------
// Returns TRUE when the date is valid.

function f_isDate(dd, mm, yyyy) {
	if (f_isEmpty(dd) && f_isEmpty(mm) && f_isEmpty(yyyy)) {
		if (f_isDate.arguments.length == 3) {
			return (false);
		} else {
			return (f_isDate.arguments[3] == true);
		}
	}

	if (!f_isInteger(dd) || !f_isInteger(mm) || !f_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);
}


// --------------------------------------------------------------------------------
// f_isTime (hh, mm, ss)
// --------------------------------------------------------------------------------
// Returns TRUE when the time is valid.

function f_isTime(hh, mm, ss) {
	if (!f_isHour(hh) || !f_isMinute(mm) || !f_isSecond(ss)) { return (false); }
	return (true);
}


// --------------------------------------------------------------------------------
// f_isSecond(ss)
// --------------------------------------------------------------------------------
// Returns TRUE when the number of seconds is correct (0-59).

function f_isSecond(ss) {
	if (!f_isInteger(ss)) {	return (false);	}
	if (ss > 59)				  { return (false); }
	return (true);
}


// --------------------------------------------------------------------------------
// f_isMinute(mm)
// --------------------------------------------------------------------------------
// Returns TRUE when the number of minutes is correct (0-59).

function f_isMinute(mm) {
	if (!f_isInteger(mm)) {	return (false);	}
	if (mm > 59)				  { return (false); }
	return (true);
}


// --------------------------------------------------------------------------------
// f_isHour(mm)
// --------------------------------------------------------------------------------
// Returns TRUE when the number of hours is correct (0-23).

function f_isHour(hh) {
	if (!f_isInteger(hh)) {	return (false);	}
	if (hh > 23)				  { return (false); }
	return (true);
}


// ------------------------------------------------------------------------------------------------------------------------------------------
// f_getStandardDate(dd, mm, yyyy [,strSplit])
// ------------------------------------------------------------------------------------------------------------------------------------------
// Returns the date in standard format (yyyymmdd). A separator is optional (default = none).

function f_getStandardDate(dd, mm, yyyy) {
	var sep = '';

	if (f_getStandardDate.arguments.length == 4) { sep = f_getStandardDate.arguments[3]; }

	if (!f_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() + sep + mm.toString() + sep + dd.toString());
	}
}


// ------------------------------------------------------------------------------------------------------------------------------
// f_getStandardTime(hh, mm, ss [,strSplit])
// ------------------------------------------------------------------------------------------------------------------------------
// Returns the time in standard format (hhmmss). A separator is optional (default = none).

function f_getStandardTime(hh, mm, ss) {
	var sep = '';

	if (f_getStandardTime.arguments.length == 4) { sep = f_getStandardTime.arguments[3]; }

	if (!f_isTime(hh, mm, ss)) {
		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() + sep + mm.toString() + sep + ss.toString());
	}
}


// --------------------------------------------------------------------------------
// f_compareDate(date1, date2, sign)
// --------------------------------------------------------------------------------
// Compare 2 dates in standard date format.
// The sign determines the type of comparison: "<", "<=", ">", ">=", "=".

function f_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);
}


// --------------------------------------------------------------------------------
// f_compareDateTime(datetime1, datetime2, sign)
// --------------------------------------------------------------------------------
// Compare 2 dates and times in standard date format.
// The sign determines the type of comparison: "<", "<=", ">", ">=", "=".

function f_compareDateTime(datetime1, datetime2, sign) {
	if (datetime1.length!=14) { return (false); }
	if (datetime2.length!=14) { return (false); }

	switch (sign) {
		case '<' :
			if (datetime1 >= datetime2) { return (false); }
			break;
		case '<=' :
			if (datetime1 > datetime2)  { return (false); }
			break;
		case '>' :
			if (datetime1 <= datetime2) { return (false); }
			break;
		case '>=' :
			if (datetime1 < datetime2)  { return (false); }
			break;
		case '=' :
			if (datetime1 != datetime2) { return (false); }
			break;
		default :
			return (false);
			break;
	}

	return (true);
}


// --------------------------------------------------------------------------------
// f_getSystemDate()
// --------------------------------------------------------------------------------
// Returns the system date in standard format (yyyymmdd).

function f_getSystemDate() {
	return (f_getStandardDate(f_getCurrentDay(), f_getCurrentMonth(), f_getCurrentYear()));
}


// --------------------------------------------------------------------------------
// f_getSystemTime()
// --------------------------------------------------------------------------------
// Returns the system time in standard format (hhmmss).

function f_getSystemTime() {
	var hh = f_getCurrentHour();
	var mm = f_getCurrentMinute();
	var ss = f_getCurrentSecond();
	return (hh.toString() + mm.toString() + ss.toString());
}


// --------------------------------------------------------------------------------
// f_getCurrentYear()
// --------------------------------------------------------------------------------
// Returns the current year.

function f_getCurrentYear() {
	var today = new Date();
	today = today.getYear();
	return (today < 1900 ? 1900 + today : today);
}


// --------------------------------------------------------------------------------
// f_getCurrentMonth()
// --------------------------------------------------------------------------------
// Returns the current month (01-12) of the current year.

function f_getCurrentMonth() {
	var today = new Date();
	today = today.getMonth() + 1;
	return (today < 10 ? '0' + today : today);
}


// --------------------------------------------------------------------------------
// f_getCurrentDay()
// --------------------------------------------------------------------------------
// Returns the current day (01-31) of the current month.

function f_getCurrentDay() {
	var today = new Date();
	today = today.getDate();
	return (today < 10 ? '0' + today : today);
}


// --------------------------------------------------------------------------------
// f_getCurrentHour()
// --------------------------------------------------------------------------------
// Returns the current hour (00-23) of the current day.

function f_getCurrentHour() {
	var today = new Date();
	today = today.getHours();
	return (today < 10 ? '0' + today : today);

}


// --------------------------------------------------------------------------------
// f_getCurrentMinute()
// --------------------------------------------------------------------------------
// Returns the current minutes (00-59) of the current hour.

function f_getCurrentMinute() {
	var today = new Date();
	today = today.getMinutes();
	return (today < 10 ? '0' + today : today);
}


// --------------------------------------------------------------------------------
// f_getCurrentSecond()
// --------------------------------------------------------------------------------
// Returns the current seconds (00-59) of the current minute.

function f_getCurrentSecond() {
	var today = new Date();
	today = today.getSeconds();
	return (today < 10 ? '0' + today : today);
}


// --------------------------------------------------------------------------------
// f_trim(s)
// --------------------------------------------------------------------------------
// Remove trailing and leading blanks from string s.

function f_trim(s) {
	return (f_rTrim(f_lTrim(s)));
}


// --------------------------------------------------------------------------------
// f_lTrim(s)
// --------------------------------------------------------------------------------
// Remove leading blanks from string s.

function f_lTrim(s) {
	// 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 str = new String(s);

	if (whitespace.indexOf(str.charAt(0)) != -1) {
		// We have a string with leading blank(s).
		var j = 0, i = str.length;

		// Iterate from the far left of string until we don't have any more whitespace.
		while (j < i && whitespace.indexOf(str.charAt(j)) != -1) { j++; }

		// Get the substring from the first non-whitespace character to the end of the string.
		str = str.substring(j, i);
	}

	return (str);
}


// --------------------------------------------------------------------------------
// f_rTrim(s)
// --------------------------------------------------------------------------------
// Remove trailing blanks from string s.

function f_rTrim(s) {
	// 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 str = new String(s);

	if (whitespace.indexOf(str.charAt(str.length-1)) != -1) {
		// We have a string with trailing blank(s).

		// Get length of string.
		var i = str.length - 1;

		// Iterate from the far right of string until we don't have any more whitespace.
		while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1) { i--; }

		// Get the substring from the front of the string to where the last non-whitespace character is.
		str = str.substring(0, i+1);
	}

	return (str);
}


// --------------------------------------------------------------------------------
// f_len(s)
// --------------------------------------------------------------------------------
// Returns the number of characters in string s.

function f_len(s) {
	return (String(s).length);
}


// --------------------------------------------------------------------------------
// f_left(s, n)
// --------------------------------------------------------------------------------
// Returns n number of characters from the left side of string s.

function f_left(s, n) {
	// Invalid bound, return blank string.
	if (n <= 0) {
   	return ("");
  }
  // Invalid bound, return entire string.
  else if (n > String(s).length) {
    return (s);
  }
  // Valid bound, return appropriate substring.
  else {
    return (String(s).substring(0, n));
  }
}


// --------------------------------------------------------------------------------
// f_right(s, n)
// --------------------------------------------------------------------------------
// Returns n number of characters from the right side of string s.

function f_right(s, n) {
	// Invalid bound, return blank string.
	if (n <= 0) {
  	return ("");
  }
  // Invalid bound, return entire string.
  else if (n > String(s).length) {
  	return (s);
  }
  // Valid bound, return appropriate substring.
  else {
  	var iLen = String(s).length;
 		return (String(s).substring(iLen, iLen - n));
  }
}


// --------------------------------------------------------------------------------
// f_mid(s, start, n)
// --------------------------------------------------------------------------------
// Returns n specified number of characters from string s starting from position
// start. First position is 0.

function f_mid(s, start, n) {
	var iEnd;
	var iLen = String(s).length;

	if (n == "end") {
   	n = iLen;
  }

	if (start < 0 || n < 0) {
		return ("");
	}
   else {
   	if ((start + n) > iLen) {
	  	iEnd = iLen;
	  }
	  else {
	  	iEnd = start + n;
		}
		return (String(s).substring(start, iEnd));
	}
}


// --------------------------------------------------------------------------------
// f_instr(strText, strFind)
// --------------------------------------------------------------------------------
// Returns the position of character strFind in string strText. If strFind is not
// found in strText, -1 is returned.

function f_instr(strText, strFind) {
	for (i=0; i < f_len(strText); i++) {
		if (strFind == f_mid(strText, i, 1)) {
			return (i);
		}
	}
	return (-1);
}


// --------------------------------------------------------------------------------
// f_countCharacter(strText, strFind)
// --------------------------------------------------------------------------------
// Returns the total times strFind is found in strText.

function f_countCharacter(strText, strFind) {
	var position = 0;
	var counter = 0;

	while (strText.indexOf(strFind, position) != -1) {
		position = (strText.indexOf(strFind, position) + strFind.length);
		counter++;
	}

	return (counter);
}


// --------------------------------------------------------------------------------
// f_normalizeSpace(s)
// --------------------------------------------------------------------------------
// Returns a string s without leading or trailing spaces, nor double spaces.

function f_normalizeSpace(s) {
	return (s.replace(/ +/g, " ").replace(/^\s+/g, "").replace(/\s+$/g, ""));
}


// --------------------------------------------------------------------------------
// f_removeFromString(r)
// --------------------------------------------------------------------------------
// Returns a string s with all occurences of string r removed.

function f_removeFromStringProtype(r) {
	var s = this;

	if (r + '' != 'undefined') {
		var i = 0;
		while (i < s.length) {
			if (r.indexOf(s.charAt(i)) == -1) {
				i++;
			} else {
				s = s.substr(0, i) + s.substr(i + 1);
			}
		}
	}

	return (s);
}
String.prototype.f_removeFromString = f_removeFromStringProtype;


// --------------------------------------------------------------------------------
// f_replaceAccents(s)
// --------------------------------------------------------------------------------
// Returns a string s where all characters with accents have been replaced by
// their equivalent without accents.

function f_replaceAccents(s) {
	return (s.replace(/[àáâãäå]/g, "a").replace(/[ÀÁÂÃÄÅ]/g, "A").replace(/[ç]/g, "c").replace(/[Ç]/g, "C").replace(/[èéêë]/g, "e").replace(/[ÈÉÊË]/g, "E").replace(/[ìíï]/g, "i").replace(/[ÌÍÏ]/g, "I").replace(/[ñ]/g, "n").replace(/[Ñ]/g, "N").replace(/[òóôõö]/g, "o").replace(/[ÒÓÔÕÖ]/g, "O").replace(/[ùúûü]/g, "u").replace(/[ÙÚÛÜ]/g, "U").replace(/[ý]/g, "y").replace(/[Ý]/g, "Y"));
}


// --------------------------------------------------------------------------------
// f_replaceAll(strOld, strFind, strReplace [, boolRegExp])
// --------------------------------------------------------------------------------
// Returns a string strNew where all occurences of strFind in strOld have been
// replaced by strReplace. boolRegExp defines if strFind is a Regular Expression or
// not (default = false).

function f_replaceAll(strOld, strFind, strReplace) {
	var strNew;
	var bRegExp;

	if (f_replaceAll.arguments.length == 3) {
		bRegExp = false;
	} else {
		bRegExp = f_replaceAll.arguments[3];
	}

	if (bRegExp) {
		var reg = new RegExp(strFind, 'g');
		strNew = strOld;
		while (reg.test(strNew)) { strNew = strNew.replace(reg, strReplace); }
	} else {
		var position = 0;
		strNew = '';

		while (strOld.indexOf(strFind, position) != -1) {
			strNew += strOld.substring(position, strOld.indexOf(strFind, position));
			strNew += strReplace;
			position = (strOld.indexOf(strFind, position) + strFind.length);
		}

		strNew += strOld.substring(position, strOld.length);
	}

	return (strNew);
}