/*******************Funciones de cadena********************/
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
//Devuelve la cadena en minúsculas
function LCase(Value) {
  return Value.toString().toLowerCase();
}
//Devuelve la cadena en mayúsculas
function UCase(Value) {
  return Value.toString().toUpperCase();
}
//Devuelve la longitud de la cadena
function Len(Expression) {
  return Expression.toString().length;
}

function Mid(Str, Start, Length) {
  if (Length < 1) {
    return "";
  }
  if (Start < 0) {
    return "";
  }
  return Str.substring(Start, Start + Length);
}
function InStr(Start, String1, String2, Compare) {
  if (Start < 1) {
    return "";
  }
  if (Start > Len(String1)) return 0;
  if (Len(String2) == 0) return Start;
  if (Compare == 1) {String1 = LCase(String1); String2 = LCase(String2);}
  if (Start > 1) {
    var index = Right(String1, Len(String1) - Start + 1).indexOf(String2)
    if (index == -1) {return 0;} else {return index + Start;}
  } else {
    return String1.indexOf(String2) + 1
  }
}
function InStrRev(StringCheck, StringMatch, Start, Compare) {
  if (Start == 0 || Start < -1) {
	return "";
  }
  if (Len(StringMatch) == 0) return Start;
  if (Compare == 1) {
    StringCheck = LCase(StringCheck); StringMatch = LCase(StringMatch);
  }
  if (Start > 1) {
    return Left(StringCheck, Start).lastIndexOf(StringMatch) + 1;
    } else {
    return StringCheck.lastIndexOf(StringMatch) + 1;
  }
}
//Elimina de una cadena dada los espacios tanto de la derecha como de la izquierda
function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}
//Esta función funciona igual que el Replace de visual basic
function replaceSubstring(inputString, fromString, toString) {
	// Goes through the inputString and replaces every occurrence of fromString with toString
  		var temp = inputString;
	if (fromString == "") {
    	return inputString;
   	}
	if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
    	while (temp.indexOf(fromString) != -1) {
        	var toTheLeft = temp.substring(0, temp.indexOf(fromString));
        	var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
        	temp = toTheLeft + toString + toTheRight;
		}
	}else{ // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		// Find a string that doesn't exist in the inputString to be used
		// as an "inbetween" string
		while (midString == "") {
			for (var i=0; i < midStrings.length; i++) {
				var tempMidString = "";
				for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
					if (fromString.indexOf(tempMidString) == -1) {
						midString = tempMidString;
						i = midStrings.length + 1;
					}
			}
		} // Keep on going until we build an "inbetween" string that doesn't exist
		// Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		// Next, replace the "inbetween" string with the "toString"
		while (temp.indexOf(midString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} // Ends the check to see if the string being replaced is part of the replacement string or not
	return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function//-->
//Hace las mismas funciones que el StringBuilder de .NET
function StringBuilder(sString) {
	// public
	this.length = 0;
	this.append = function (sString) {
		// append argument
		this.length += (this._parts[this._current++] = String(sString)).length;
		// reset cache
		this._string = null;
		return this;
	};
	this.toString = function () {
		if (this._string != null)
			return this._string;
		
		var s = this._parts.join("");
		this._parts = [s];
		this._current = 1;
		this.length = s.length;
		
		return this._string = s;
	};
	// private
	this._current	= 0;
	this._parts		= [];
	this._string	= null;	// used to cache the string
	// init
	if (sString != null)
		this.append(sString);
}
/*******************Fin de funciones de cadena********************/