// Tools
//Remplazar todas la coincidencias en un texto.
function lTrim(sStr){
	 while (sStr.charAt(0) == " ")
	  sStr = sStr.substr(1, sStr.length - 1);
	 return sStr;
}
function rTrim(sStr){
	 while (sStr.charAt(sStr.length - 1) == " ")
	  sStr = sStr.substr(0, sStr.length - 1);
	 return sStr;
}
function allTrim(sStr){
	 return rTrim(lTrim(sStr));
}
function str_replace(busca, repla, orig)
{
	str 	= new String(orig);

	rExp	= "/"+busca+"/gi";
	rExp	= eval(rExp);
	newS	= String(repla);

	str = new String(str.replace(rExp, newS));

	return str;
}

function remplaceAll(keywords, texto, stringReplace){
	keywords = allTrim(keywords);
	texto = allTrim(texto);
	keys = keywords.split(" ");
	texto = texto.split(" ");
	for(var i=0; i< keys.length; i++){
		for(var j=0;j < texto.length; j++){
			 var first = texto[j].toLowerCase().indexOf(keys[i].toLowerCase());
			 newT = texto[j].substring(first,first+keys[i].length);

			 texto[j] = str_replace(keys[i],"<strong>"+newT+"</strong>", texto[j]);
		}
	}
	texto = texto.join(" ");
	return texto;
}
function loading(num){
	switch(num){
		case 1: msj = "cargando."; break;
		case 2: msj = "cargando.."; break;
		case 3: msj =  "cargando..."; break;
		default:msj =   "cargando..."
	}
	return msj;
}	

function mail(texto){

    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else
     mailres = false;
                
    return mailres;
}

var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
function getPage(url,dataSend,objectHTMLID){

		var xhttpRequest =  HTTPREQUEST();
	 	var title = document.title;
		xhttpRequest.onreadystatechange = function(){
			
			if(xhttpRequest.readyState == 1 || xhttpRequest.readyState == 2){
				document.title = "Cargando...";
			}else if(xhttpRequest.readyState == 3){
				document.title = "Cargando...";
			}else{
				
				if(xhttpRequest.status ==200 || xhttpRequest.status== 0){
					
					document.getElementById(objectHTMLID).innerHTML= xhttpRequest.responseText;
					document.title = title;
					
				}
				
			}
			
		}
		//alert(dataSend);
		xhttpRequest.open("POST",url,true);
		xhttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xhttpRequest.setRequestHeader("Accept-Charset","iso-8859-2")

		if(xhttpRequest.overrideMimeType){
			xhttpRequest.overrideMimeType('text/xml; charset=iso-8859-1');
		}
		xhttpRequest.send(dataSend);
}