// -----------------------------------------------------------------------------------------------------------------------
// FUNCION crearXMLHTTP
// Con esta funcion creamos un objeto XML-http
// No se le pasa nada
// Devuelve el objeto creado segun el navegador.
function crearXMLHTTP() {
	// Creamos una variable booleana para comprobar si se usa una instanacia valida de ActiveX Microsoft.
	var xmlhttp = false;
	// Comprobamos si se usa IE
	try {
		// Si la version es superior a la 5
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			// Si no, usamos el tradicional ActiveX Microsoft
			try {
			// Si usamos IE
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (E) {
			// Si no estamos usando IE (usamos otros Mozilla, etc.)
			xmlhttp = false;
		}
	}
	// Si no uso IE creo un objeto XML-http en javascript
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	// Devuelvo el objeto	
	return xmlhttp;
}
// -----------------------------------------------------------------------------------------------------------------------
// FUNCION procesarAjax
// La usamos para procesar una petición XMLHttpRequest
// Se le pasa la pagina, el div, POST|GET, cadena con parametros (formularios)
// Devuelve la peticion dentro del DIV que le hemos pasado
//Function to process an XMLHttpRequest.
function procesarAjax (serverPage, obj, getOrPost, str){
	// Obtener el objeto XMLHttpRequest a utilizar
	xmlhttp = crearXMLHTTP ();
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
	}
	xmlhttp.send(null);
	} 
	else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
					if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						obj.innerHTML = xmlhttp.responseText;
					}
				}
				xmlhttp.send(str);
	}
}
// -----------------------------------------------------------------------------------------------------------------------
// FUNCION muestraEstado
// La usamos para mostrar en una capa el estado de la accion
// Se le pasa el div y el texto a mostrar
// No devuelve nada
function muestraEstado (texto, miObj) {
	obj=document.getElementById(miObj);
	if (obj) {
		obj.innerHTML=texto;
	}
}

// -----------------------------------------------------------------------------------------------------------------------
// FUNCION muestraEstado CON IMAGEN
function ajaxImagen (img, miObj) {
	muestraEstado('<img src=\"' + img + '\" alt=\"Cargando...\" border=\"0\">', miObj);
}
function prepararCapaAjax(capa, objeto, ancho, alto) {
		miCapa=document.getElementById(capa);
		miCampo=document.getElementById(objeto);
		posx=findPosX(miCampo);
		posy=findPosY(miCampo);
		miCapa.style.left=posx + "px";
		miCapa.style.top=posy + "px";
		miCapa.style.width=ancho + "px";
		miCapa.style.width=alto + "px";
		miCapa.style.visibility="visible";
}
function centrarCapaAjax(capa){
		miCapa=document.getElementById(capa);
		miCapa.style.visibility="visible";
		//miCapa.style.position="fixed";
		miCapa.style.left="30" + "%";
		miCapa.style.top="20" + "%";
}


function xDef()
{
  for(var i=0; i<arguments.length; ++i){if(typeof(arguments[i])=='undefined') return false;}
  return true;
}

function xScrollLeft(e, bWin)
{
  var offset=0;
  if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && e) w = e;
    if(w.document.documentElement && w.document.documentElement.scrollLeft) offset=w.document.documentElement.scrollLeft;
    else if(w.document.body && xDef(w.document.body.scrollLeft)) offset=w.document.body.scrollLeft;
  }
  else {
    e = xGetElementById(e);
    if (e && xNum(e.scrollLeft)) offset = e.scrollLeft;
  }
  return offset;
}
function xScrollTop(e, bWin)
{
  var offset=0;
  if (!xDef(e) || bWin || e == document || e.tagName.toLowerCase() == 'html' || e.tagName.toLowerCase() == 'body') {
    var w = window;
    if (bWin && e) w = e;
    if(w.document.documentElement && w.document.documentElement.scrollTop) offset=w.document.documentElement.scrollTop;
    else if(w.document.body && xDef(w.document.body.scrollTop)) offset=w.document.body.scrollTop;
  }
  else {
    e = xGetElementById(e);
    if (e && xNum(e.scrollTop)) offset = e.scrollTop;
  }
  return offset;
}



function xClientWidth()
{
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientWidth)
    {v=d.documentElement.clientWidth;}
  else if(d.body && d.body.clientWidth)
    {v=d.body.clientWidth;}
  else if(xDef(w.innerWidth,w.innerHeight,d.height)) {
    v=w.innerWidth;
    if(d.height>w.innerHeight) v-=16;
  }
  return v;
}
function xClientHeight()
{
  var v=0,d=document,w=window;
  if((!d.compatMode || d.compatMode == 'CSS1Compat') && !w.opera && d.documentElement && d.documentElement.clientHeight)
    {v=d.documentElement.clientHeight;}
  else if(d.body && d.body.clientHeight)
    {v=d.body.clientHeight;}
  else if(xDef(w.innerWidth,w.innerHeight,d.width)) {
    v=w.innerHeight;
    if(d.width>w.innerWidth) v-=16;
  }
  return v;
}

function posicionaCapaCarga(capa){
  // OBTENGO EL OBJETO
  miCapa=document.getElementById(capa);
  // MEDIDAS DE LA CAPA
  ancho=300;
	alto=50;
  // OBTENGO LAS COORDENADAS  
  pos_left=200;
  pos_top = xScrollTop();
	// POSICIONO LA CAPA
  miCapa.style.left = pos_left;
	miCapa.style.top = pos_top;
}

function ajustarCapaObjeto(capa, objeto) {
    miCapa=document.getElementById(capa);
		miCampo=document.getElementById(objeto);
		posx=findPosX(miCampo);
		posx=posx + 12;
    posy=findPosY(miCampo);
		posy=posy + 12;
		miCapa.style.left=posx + "px";
		miCapa.style.top=posy + "px";
}