/***************************************************************************************
 * Generic JavaScript functions used to manage strings
 * 
 * @author André Masson
 ***************************************************************************************/


/**
 * Indicates if the string is empty or not.
 */
function isEmpty(s) {
	return (trim(s) == "");
}

/**
 * Trim a string (left and right triming)
 */
function trim(val) {
	var s = new String("" + val);

	// Eliminates trivial cases...
	if (s == null || s == "") {
		return s;
	}

	// Remove left spaces
	var i = 0;
	while (s.charAt(i) == ' ') {
		i++;
	}
	var workString = s.substr(i);

	// Remove right spaces
	var cleanedString = "";
	for (i = workString.length-1 ; i >=0 ; i--) {
		if (workString.charAt(i) != ' ') {
			cleanedString = workString.charAt(i) + cleanedString;
		}
	}

	return cleanedString;
}

/**
 * Indicates if the 2 trimed string are equals
 */
function isTrimedEquals(s, s2) {
	var str1 = trim(s);
	var str2 = trim(s2);

	if (str1 == null && str2 == null) {
		return true;
	}

	if (str1 == "" && str2 == "") {
		return true;
	}

	return (str1 == str2);
}

/**
 * String replacement utility function
 */
function replaceString(oldS, newS, fullS) {
   for (var i=0; i<fullS.length; i++) {
      if (fullS.substring(i, i + oldS.length) == oldS) {
         fullS = fullS.substring(0, i) + newS + fullS.substring(i + oldS.length, fullS.length);
      }
   }

   return fullS;
}

