// -----------------------------------------------------------------------------
//  Funcion AJAX - Interface para la conexión XMLHTTP - 10-03-2006      
//  Code licensed under Creative Commons Attribution-ShareAlike License
//  http://creativecommons.org/licenses/by-sa/2.0/
// -----------------------------------------------------------------------------
function ajax(sFile, sMethod, Params) {
// -----------------------------------------------------------------------------
// Separamos el php de la id de la capa donde se introducira el texto devuelto
// sFile <=> "script.php#id_capa"
// -----------------------------------------------------------------------------
	var sFile = sFile.split('#');
	var container = sFile[1];	// Nombre de la capa
	var myConn = new XHConn();	// Objeto de la conexión
	if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
	// -----------------------------------------------------------------------------
	// Funcion que se ejecutará cuando XHConn finalize la conexión.
	// Puede añadir delate('^') del codigo de la capa, detras('+') o sustituirlo.
	// -----------------------------------------------------------------------------
	var fnDone = function (oXML) {
		if(container) {
			switch(container[0]) {
			case '^': 
				container = container.replace(/^\^/,'');
				if(!document.getElementById(container)) return false;
				var update = document.getElementById(container).innerHTML;
				document.getElementById(container).innerHTML = oXML.responseText + update;
				break;
			case '+': 
				container = container.replace(/^\+/,'');
				if(!document.getElementById(container)) return false;
				document.getElementById(container).innerHTML += oXML.responseText;
				break;
			default:
				if(!document.getElementById(container)) return false;
				document.getElementById(container).innerHTML = oXML.responseText;
			break;
			}
		}
	};
	myConn.connect(sFile[0], sMethod, Params, fnDone);
}
// -----------------------------------------------------------------------------
//  XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08      
//  Code licensed under Creative Commons Attribution-ShareAlike License
//  http://creativecommons.org/licenses/by-sa/2.0/
// -----------------------------------------------------------------------------
function XHConn() {

  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone) {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();
    try {
      if (sMethod == "GET") {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      } else {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
		//xmlhttp.setRequestHeader("Content-Type",
		//						 "text/html; charset=iso-8859-1");
        xmlhttp.setRequestHeader("Content-Type",
								 "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && !bComplete) {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
// Convert all applicable characters to HTML entities
// example:
// test = 'äöü'
// test.htmlEntities() //returns '&auml;&ouml;&uuml;'
// Si do_escape es true se ejecuta la funcion escape sobre el string que se devuelve.
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
function htmlEntities(cadena,do_escape) {
	
	var chars = new Array ('&','à','á','â','ã','ä','å','æ','ç','è','é',
						 'ê','ë','ì','í','î','ï','ð','ñ','ò','ó','ô',
						 'õ','ö','ø','ù','ú','û','ü','ý','þ','ÿ','À',
						 'Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë',
						 'Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö',
						 'Ø','Ù','Ú','Û','Ü','Ý','Þ','€','\"','ß','<',
						 '>','¢','£','¤','¥','¦','§','¨','©','ª','«',
						 '¬','­','®','¯','°','±','²','³','´','µ','¶',
						 '·','¸','¹','º','»','¼','½','¾');
	
	var entities = new Array ('amp','agrave','aacute','acirc','atilde','auml','aring',
							'aelig','ccedil','egrave','eacute','ecirc','euml','igrave',
							'iacute','icirc','iuml','eth','ntilde','ograve','oacute',
							'ocirc','otilde','ouml','oslash','ugrave','uacute','ucirc',
							'uuml','yacute','thorn','yuml','Agrave','Aacute','Acirc',
							'Atilde','Auml','Aring','AElig','Ccedil','Egrave','Eacute',
							'Ecirc','Euml','Igrave','Iacute','Icirc','Iuml','ETH','Ntilde',
							'Ograve','Oacute','Ocirc','Otilde','Ouml','Oslash','Ugrave',
							'Uacute','Ucirc','Uuml','Yacute','THORN','euro','quot','szlig',
							'lt','gt','cent','pound','curren','yen','brvbar','sect','uml',
							'copy','ordf','laquo','not','shy','reg','macr','deg','plusmn',
							'sup2','sup3','acute','micro','para','middot','cedil','sup1',
							'ordm','raquo','frac14','frac12','frac34');
	
	newString = cadena;
	for (var i = 0; i < chars.length; i++) {
		
		myRegExp = new RegExp();
		myRegExp.compile(chars[i],'g');
		newString = newString.replace (myRegExp, '&' + entities[i] + ';');
	}
	if(do_escape != undefined && do_escape == true) {
		newString = escape(newString);
	}
	return newString;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 