// ----------------------------------
// isEmpty (STRING s)
// ----------------------------------
// Retourne true si le champ s est vide.

function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}


// ----------------------------------
// LTrim (STRING str)
// ----------------------------------
// Returns a copy of a string without leading spaces.

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\v");
	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 str)
// ----------------------------------
// Returns a copy of a string without trailing spaces.

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\v");
	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 str)
// ----------------------------------
// Returns a copy of a string without leading or trailing spaces

function Trim(str) {
   return RTrim(LTrim(str));
}

// ----------------------------------------------------------
// replace (STRING str, STRING charToReplace, STRING newChar)
// ----------------------------------------------------------
function replace(str, charToReplace, newChar) {
	iStr = str.indexOf(charToReplace);
	if(iStr != -1) {
		return str.substring(0, iStr) + newChar + replace(str.substring(iStr+1, str.length));
	}
	return str;
}

// ----------------------------------
// isInteger (s [,eok])
// ----------------------------------
// Retourne true si tous les caractères du champ en entrée 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)
// ----------------------------------
// Retourne true si la date est valide.

function isDate(dd, mm, yyyy) {
	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;
}


// ----------------------------------
// fillInteger (int, long)
// ----------------------------------
// Vérifie que le nombre de caactères encodés pour une heure, une minute ou une seconde est
// bien formaté sur 2 chiffres et rempli ces dernieres 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;

}


// --------------------------------------
// isEmail (STRING s [, BOOLEAN emptyOK])
// --------------------------------------
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required

function isEmail (s) {
	if (isEmpty(s)) {
		if (isEmail.arguments.length == 1) {
     	return (false);
		} else {
			return (isEmail.arguments[1] == true);
		}
	}

	// there must be >= 1 character before @, so we start looking
	// at character position 1 (i.e. second character)
	var i = 1;
	var sLength = s.length;

	// look for @
	while ((i < sLength) && (s.charAt(i) != "@"))	{
		i++;
	}

	if ((i >= sLength) || (s.charAt(i) != "@")) {
		return (false);
	}	else {
		i += 2;
	}

	// look for .
	while ((i < sLength) && (s.charAt(i) != ".")) {
		i++;
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
		return (false);
	}	else {
		return (true);
	}
}

function checkImagePath(path) {
	if(path.length != 0) {
		if(path.indexOf("/") != 0) { path = "/" + path; }
		if(path.indexOf("/upload/") != 0) { path = "/upload" + path;	}
	}
	return (path);
}

function isCheckedExtension(image) {
	img = image;

	if(img.value.length != 0) {
		if(img.value.indexOf(".jpg") > 0 || img.value.indexOf(".jpeg") > 0 || img.value.indexOf(".gif") > 0 ||
			img.value.indexOf(".JPG") > 0 || img.value.indexOf(".JPEG") > 0 || img.value.indexOf(".GIF") > 0 ) {
			return true;
		} else {
			return false;
		}
	}
	return true;
}	
