function objetoAjax()	{
	if (window.XMLHttpRequest)
		_ajax = new XMLHttpRequest();
	else
		if (window.ActiveXObject)
			_ajax = new ActiveXObject("Microsoft.XMLHTTP");
		else
			_ajax = false;
	return _ajax;
}

// algunos alias

function tag(id)	{return document.getElementById(id);}
function crear(tag)	{return document.createElement(tag);}
function texto(cual)	{return document.createTextNode(cual);}
function limpiar_tags(cuales)	{
	for (i = 0, tags = limpiar_tags.arguments, total = tags.length; i < total; i ++)
	while (tag(tags[i]).hasChildNodes())	tag(tags[i]).removeChild(tag(tags[i]).firstChild);
}
function insertAfter(dato, elemento) {
	with(elemento.parentNode)
		(lastChild == elemento) ? appendChild(dato) : insertBefore(dato, elemento.nextSibling);
}

function obtener_dato(objeto, i)	{
	var resultado, tipo;
	var acceso = true;

	try	{
		resultado = objeto[i];
	}
	catch(e)	{
		acceso = false;
	}
	return [resultado, typeof(resultado), acceso];
}

var Ajax;

function destripe_inicial()	{
	mensaje = "Objeto Ajax";
	Ajax = objetoAjax();
	for (i in Ajax)	{
		dato_y_tipo = obtener_dato(Ajax, i)
		mensaje += "\n" + i + " = " + dato_y_tipo[0] + " (" + dato_y_tipo[1] + ")";
	}
	limpiar_tags("tag_destripe_inicial");
	tag("tag_destripe_inicial").appendChild(texto(mensaje));

}

function objetando(objeto_base, subs)	{
	if (subs.indexOf("_") == -1)	return objeto_base[subs];
	else	return objetando(objeto_base[subs.substr(0, subs.indexOf("_"))], subs.substr(subs.indexOf("_") + 1));
}

var cuenta_estados;
var desglosados = new Object();
function plegar_desglosados(dato)	{
	for (i in desglosados)	if (desglosados[i])	//{
		if (i.substring(0, dato.length) == dato)	desglosados[i] = false;
}

function desglosePre(objeto)	{
	if (desglosados[objeto.id])	{
		objeto.parentNode.removeChild(objeto.nextSibling);
		desglosados[objeto.id] = false;
		plegar_desglosados(objeto.id);
	}	
	else	{
		desglosados[objeto.id] = true;
		nuevo_objeto = crear("pre");
		nuevo_objeto.appendChild(texto(objetando(window, objeto.id)));
		insertAfter(nuevo_objeto, objeto);
	}
}

function desglose(objeto)	{
	if (desglosados[objeto.id])	{
		objeto.parentNode.removeChild(objeto.nextSibling);
		desglosados[objeto.id] = false;
		plegar_desglosados(objeto.id);
	}	
	else	{
		desglosados[objeto.id] = true;
		__destripador = objetando(window, objeto.id);
		//alert(__destripador);
		mensaje = "<u>Objeto " + objeto.id.split("_").join(".") + "</u>";
		for (i in __destripador)	{
			datos = obtener_dato(__destripador, i)
			mensaje += "__" + i + " (" + datos[1] + ")";
			if (/(function|string|object)/.test(datos[1]))
				mensaje += maquillar(objeto.id, i, datos)
			else	mensaje += ": " + datos[0];
			if (!datos[2])	mensaje += " [acceso denegado]";
		}

		nuevo_objeto = crear("div");
		with(nuevo_objeto.style)	{
			border = "1px solid gray";
			backgroundColor = (objeto.id.split("_").length % 2 == 1) ? "#FFFFCC" : "#CCFFFF";
		}
		nuevo_objeto.innerHTML = mensaje.split("__").join("<br />");
		insertAfter(nuevo_objeto, objeto);
	}
}

function maquillar(base, objeto, datos)	{
	if(datos[2])
		switch(datos[1])	{
			case "function":
			case "string":
				return "<button id='" + base + "_" + objeto + "' onclick='desglosePre(this)'>mostrar</button>";
				break;
			default:
				return "<button id='" + base + "_" + objeto + "' onclick='desglose(this)'>mostrar</button>";
		}
	else	return datos[0];
}

var id_destino;
var arrayLimpieza = [];
function destripe_final(url, destino)	{

	function limpieza()	{
		for (i = 0, total = arrayLimpieza.length; i < total; i ++)
			tag(arrayLimpieza[i]).parentNode.removeChild(tag(arrayLimpieza[i]));
		arrayLimpieza = [];
	}

	limpiar_tags("tag_destripe_final", "tag_destripe_error");
	limpieza();
	id_destino = destino;
	cuenta_estados = 0;
	plegar_desglosados("Ajax");
	Ajax = objetoAjax();
	Ajax.onreadystatechange = function()	{
		mensaje = "<u>Objeto Ajax</u>";
		final = (Ajax.readyState == 4 && (Ajax.status == 200 || Ajax.status == 501));
		for (i in Ajax)	{
			datos = obtener_dato(Ajax, i)
			mensaje += "__" + i + " (" + datos[1] + ")";

			if (/(function|string|object)/.test(datos[1]))	{
				if (final)	mensaje += maquillar("Ajax", i, datos)
				else	mensaje += ": " + datos[0];
			}
			else	mensaje += ": " + datos[0];
			if (!datos[2])	mensaje += " [acceso denegado]";
		}
		insertar = texto(mensaje.split("__").join("\n"));
		if (final)	with(tag(id_destino))	{
			style.display = "block";
			innerHTML = mensaje.split("__").join("\n");
		}
		else	{
			nuevo_estado = tag(id_destino).cloneNode(true);
			while(nuevo_estado.hasChildNodes())	nuevo_estado.removeChild(nuevo_estado.firstChild);
			nuevo_id = "cambio_estado_" + (++cuenta_estados);
			nuevo_estado.id = nuevo_id;
			arrayLimpieza.push(nuevo_id);
			nuevo_estado.appendChild( texto(mensaje.split("__").join("\n")));
			tag(id_destino).parentNode.insertBefore(nuevo_estado, tag(id_destino));
		}
	}
	Ajax.open("get", url, true);
	Ajax.send(null);
}

function destripe_final(url, destino)	{

	function limpieza()	{
		for (i = 0, total = arrayLimpieza.length; i < total; i ++)
			tag(arrayLimpieza[i]).parentNode.removeChild(tag(arrayLimpieza[i]));
		arrayLimpieza = [];
	}

	limpiar_tags("tag_destripe_final", "tag_destripe_error");
	for (i in {tag_destripe_final: 0, tag_destripe_error: 0})
		tag(i).style.display = (i == destino) ? "block" : "none";
	limpieza();
	id_destino = destino;
	cuenta_estados = 0;
	plegar_desglosados("Ajax");
	_Ajax = objetoAjax();
	_Ajax.onreadystatechange = function()	{
		mensaje = "<u>Objeto Ajax</u>";
		final = (_Ajax.readyState == 4 && (_Ajax.status == 200 || _Ajax.status == 501));
		Ajax = _Ajax;
		for (i in Ajax)	{
			datos = obtener_dato(Ajax, i)
			mensaje += "__" + i + " (" + datos[1] + ")";

			if (/(function|string|object)/.test(datos[1]))	{
				if (final)	mensaje += maquillar("Ajax", i, datos)
				else	mensaje += ": " + datos[0];
			}
			else	mensaje += ": " + datos[0];
			if (!datos[2])	mensaje += " [acceso denegado]";
		}
		insertar = texto(mensaje.split("__").join("\n"));
		if (final)	with(tag(id_destino))	{
			style.display = "block";
			innerHTML = mensaje.split("__").join("\n");
		}
		else	{
			nuevo_estado = tag(id_destino).cloneNode(true);
			while(nuevo_estado.hasChildNodes())	nuevo_estado.removeChild(nuevo_estado.firstChild);
			nuevo_id = "cambio_estado_" + (++cuenta_estados);
			nuevo_estado.id = nuevo_id;
			arrayLimpieza.push(nuevo_id);
			nuevo_estado.appendChild( texto(mensaje.split("__").join("\n")));
			tag(id_destino).parentNode.insertBefore(nuevo_estado, tag(id_destino));
		}
	}
	_Ajax.open("get", url, true);
	_Ajax.send(null);
}

