// JavaScript Document

//Para para obtener un objeto y ejecutar consulta
	function objetoAjax(){
		var xmlhttp=false;
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		return xmlhttp;
	}

function jsAjaxIt(Url,Obj,Func,Async){
	/* *******************************************************************************************
	 * Importancia: ALTA
	 Función que obtiene HTML de un archivo remoto (Ajax), si se desae que ejecute Javascript en
	 el contenido HTML agregue al final: --JAVASCRIPT-- seguido del codigo JS que requiera
	******************************************************************************************* **/
	var Tiempo = new Date();
	var Partes = "";
	var TieneJS = 0;
	thisdivResultado = document.getElementById(Obj);
	thisdivResultado.innerHTML= '<div align="center"><br><Table align="center" border="0"><Tr><Td><img src="img/loadingAnimation.gif" align="center"></Td></Tr><Tr><Td>Procesando, por favor espere...</Td></Tr></Table></div>';	  
	ajax = objetoAjax();
	if (Async === undefined) Async = true;
	ajax.open("GET",Url,Async);
	ajax.onreadystatechange=function(){
		if (ajax.readyState == 1 || ajax.readyState == 2 || ajax.readyState == 3 ){
			window.status = "Procesando..." + thisdivResultado.id;
		}
		if (ajax.readyState==4){
			var results = ajax.responseText;
			if (results.indexOf('--JAVASCRIPT--') != -1){
				Partes = results.split('--JAVASCRIPT--');
				thisdivResultado.innerHTML = Partes[0];
				TieneJS = 1;
			}
			else{
				Partes = results; 
				thisdivResultado.innerHTML = Partes;
			}
			if (TieneJS == 1) {
				eval(Partes[1]);
			}
			if (Func) eval(Func);
			var Fin= new Date();
			window.status = thisdivResultado.id + '...Procesado. Listo(' + jsKnowSpendTime(Tiempo.getTime(),Fin.getTime())+')';
		}
	}
	ajax.send(null);
	if (Async == false){
		var results = ajax.responseText;
		if (results.indexOf('--JAVASCRIPT--') != -1){
			Partes = results.split('--JAVASCRIPT--');
			thisdivResultado.innerHTML = Partes[0];
		}
		else{
			Partes = results; 
			thisdivResultado.innerHTML = Partes;
		}
		if (Partes.length > 1) {
			eval(Partes[1]);
		}
	}
	
}
/*
function jsAjaxIt(Url,Obj,Func){
		//alert(Url);		
		  var Tiempo = new Date();
		  divResultado = document.getElementById(Obj);
		  divResultado.innerHTML= '<Table align="center" border="1" bordercolor="#FF0000" height="95%"><Tr><Td bgcolor="#FFCC00">Descargando, por favor espere...</Td></Tr></Table>';	  
		  ajax=objetoAjax();	  
		  ajax.open("GET",Url,true);
		  ajax.onreadystatechange=function() 
		  	{		  
			if (ajax.readyState == 1 || ajax.readyState == 2 || ajax.readyState == 3 )
				{
				window.status = "Procesando..." + divResultado.id;
				}
			if (ajax.readyState==4) 
				{
			  		var results = ajax.responseText;
					//alert(ajax.responseText);
					if (results.indexOf('--JAVASCRIPT--') != -1)
						{
							Partes = results.split('--JAVASCRIPT--'); 
							divResultado.innerHTML = Partes[0];
						}
					else
						{
							Partes = results; 
							divResultado.innerHTML = Partes;
							//alert(Partes);
						}
					if (Partes.length >1) 
						{
							//alert('ejecutara ' + Partes[1]); 
							//jsLogger('evaluara >>>>=' + Partes[1]);
							//alert(Partes[1]);
							eval(Partes[1]);
						}
					
					if (Func) eval(Func);
					var Fin= new Date();
					window.status = 'Listo(' + jsKnowSpendTime(Tiempo.getTime(),Fin.getTime())+')';
					//alert('done ' + Url);
				 }
			}
	  //ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	  //enviando los valores
	  ajax.send(null);
	}
*/	
function jsKnowSpendTime(Inicio,Fin)
{
	var Resta = Fin - Inicio;
	var Ret = '';
		if (Resta < 1000) 
			Ret = Resta + 'ms';
		else
			Ret = parseFloat(Resta / 1000).toFixed(2) + 's';
	return Ret;
}

function jsJQueryIt(url,CallBackFunction){  
	/* *******************************************************************************************

	 Esta funcion te devuelve el resultado de una consulta a un php con ajax
	 url: trae la direccion del archivo php con sus respectivos parametros
	 CallBackFunction: Una cadena con codigo javascript que sera evaluada cuando la operacion ajax se complete

	******************************************************************************************* **/
	var WinSt;
	var resul;
	//if (!mode) mode=false;
	if (!CallBackFunction) 	CallBackFunction="";
	$.ajax({
		async:false,
		type: "GET",
		dataType: "html",
		contentType: "application/x-www-form-urlencoded",
		url: url,
		beforeSend:function(){
			WinSt=window.status;
			window.status = "Procesando...";
		},
		success: function(datos){
			resul=datos;
			//resul=resul.trim();
			//alert(resul);
		},  //  me contesta con una variable en la funcion mandada ejem. jsValidarUs(Resp) : te llenaria la variable Resp con el resultado por lo que en datos  llega lo que respondio el php
		error:function(XMLHttpRequest, textStatus){
			alert('['+textStatus+'] Ocurrio un Error al intentar ejecutar '+url)
		},
		complete: function(objeto, exito){
			window.status=WinSt;
			if(exito=="success"){
				//alert("Y con �xito");
				eval(CallBackFunction);
			}
		}
	});
	if(resul===undefined) resul=false;
	return resul;
}


function jsAjaxLoad(url,objeto)
{
//funcion: carga por metodo load de jquery una url a un objeto en su innerHTML

	document.getElementById(objeto).innerHTML='<div align="center"><Table align="center" border="0"><Tr><Td><img src="img/loadingAnimation.gif" align="center"></Td></Tr><Tr><Td>Cargando</Td></Tr></Table></div>';	  
	
	$("#"+objeto).load(url)

}