

//////////////////////////////////////////////////////////////////////////////
//
// 	Nombre:  				strstr.
// 	Descripción: 		Localiza un String dentro de otro String.
// 	Parámetros    	
//		donde, 				Strig donde buscar.
//		que,					string que se quiere localizar.
//	Retornos      	
//		-1,						String no localizado.
//		posición,			Posición en el string 'donde', donde se encuentra 'que'.
//
//////////////////////////////////////////////////////////////////////////////
	
function strstr(donde, que) {
	return strstrSpecial(donde, que, true, false);
	
} // De strstr

//////////////////////////////////////////////////////////////////////////////
//
// 	Nombre:  				findTextLike.
// 	Descripción: 		Localiza un String dentro de otro String. La búsqueda no
//									es exacta, por lo que no se distingue entre minúsculas y
//									mayúsculas ni se tienen en cuenta espacios o tabuladores.
// 	Parámetros    	
//		donde, 				Strig donde buscar.
//		que,					string que se quiere localizar.
//	Retornos      	
//		-1,						String no localizado.
//		posición,			Posición en el string 'donde', donde se encuentra 'que'.
//
//////////////////////////////////////////////////////////////////////////////
	
function findTextLike(donde, que) {
	return strstrSpecial(donde, que, false, true);
}

//////////////////////////////////////////////////////////////////////////////
//
// 	Nombre:  				strstrSpecial.
// 	Descripción: 		Localiza un String dentro de otro String, de forma especial
// 	Parámetros    	
//		donde, 				Strig donde buscar.
//		que,					string que se quiere localizar.
//		caseSensitive,True, es sensible a mayúsculas y minúsculas; False, no.
//		skipSpaces,		True, ignora espacios y tabuladores; False, no.
//		skipTags,			True, ignora los tags de HTML (en IExplorer ya están
//												ignorados, pero no en Mozilla.
//	Retornos
//		-1,						String no localizado.
//		posición,			Posición en el string 'donde', donde se encuentra 'que'.
//
//////////////////////////////////////////////////////////////////////////////
	
function strstrSpecial(donde, que, caseSensitive, skipSpaces, skipTags) {
	var encontrado = false;
	
	// Si la búsqueda no es sensible a la capitularidad de las letras,
	// convertivmos ambas cadenas a minúsculas, para hacer la comparación.
	if (!caseSensitive) {
		donde.toLowerCase();
		que.toLowerCase();
	}
	
	tagAbierto = false;
	caracterEspecialAbierto = false;
	iDonde = 0;
	iQue = 0;
	while ((!encontrado) && (iDonde < donde.length)) {
		ignorar = false;
		// Primeramente vemos si tenemos que saltarnos los espacios.
		if (skipSpaces && ((donde.charAt(iDonde) == ' ') || 
		                     (donde.charAt(iDonde) == '\t'))) {
			ignorar = true;
		}
		if (skipTags && (tagAbierto)) {
			ignorar = true;
		}		

		if (skipTags && (donde.charAt(iDonde) == '<')) {
			ignorar = true;
			tagAbierto = true;
		}			

		if (skipTags && (donde.charAt(iDonde) == '>')) {
			ignorar = true;
			tagAbierto = false;
		}			

		if (skipTags && (caracterEspecialAbierto)) {
			ignorar = true;
		}		

		if (skipTags && (donde.charAt(iDonde) == '&')) {
			ignorar = true;
			caracterEspecialAbierto = true;
		}			

		if (skipTags && (donde.charAt(iDonde) == ';')) {
			ignorar = true;
			caracterEspecialAbierto = false;
		}			

		if (!ignorar) {
			if (donde.charAt(iDonde) == que.charAt(iQue)) {
				if (iQue == (que.length - 1)) {
					encontrado = true;
					break;
				} else {
					// Nos colocamos en el siguiente caracter a comparar.
					iQue++;
				}
			} else {
				// Debemos empezar a comparar la cadena desde el principio.
				iQue = 0;
			}
		} // De if skipSpaces.
		
		// Nos colocamos en el siguiente carácter en la tira de caracteres donde buscamos.
		iDonde++;
	}	
	
	if (encontrado) {
		return iDonde;
	} else {
		return -1;
	}
	
} // De strstrSpecial.

function Trim(str) {
	var resultStr = "";
	
	resultStr = TrimLeft(str);
	resultStr = TrimRight(resultStr);
	
	return resultStr;
}

function TrimLeft(str) {
	var resultStr = "";
	var i = len = 0;
	
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
	// Make sure the argument is a string
	str += "";
	if (str.length == 0) 
		resultStr = "";
	else { 
		// Loop through string starting at the beginning as long as there
		// are spaces.
		// len = str.length - 1;
		len = str.length;
		
		while ((i <= len) && (str.charAt(i) == " "))
			i++;
		// When the loop is done, we're sitting at the first non-space char,
		// so return that char plus the remaining chars of the string.
		resultStr = str.substring(i, len);
	}
	return resultStr;
}

function TrimRight(str) {
	var resultStr = "";
	var i = 0;
	
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str == null) 
		return null;
	// Make sure the argument is a string
	str += "";
	
	if (str.length == 0) 
		resultStr = "";
	else {
		// Loop through string starting at the end as long as there
		// are spaces.
		i = str.length - 1;
		while ((i >= 0) && (str.charAt(i) == " "))
			i--;
		
		// When the loop is done, we're sitting at the last non-space char,
		// so return that char plus all previous chars of the string.
		resultStr = str.substring(0, i + 1);
	}
	
	return resultStr; 
}


