// JavaScript Document

function objetoAjax(){

//	 Función que crea y devuelve un objeto para gestionar peticiones remotas (Ajax)

	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){
	/* *******************************************************************************************
	 
	 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= '<Table align="center" border="0"><Tr><Td><img src="img/loadingAnimation.gif" align="center"></Td></Tr><Tr><Td>Cargando</Td></Tr></Table>';	  
	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 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 jsWindowIt(Url,Titulo,W,H,Modal,CallBack){
	/* *******************************************************************************************
	 * Importancia: ALTA
	 Función que hace uso de un la funcion tb_show() del thickbox.js para mostrar una ventana flotante
	 Autor: FJRL
	 Fecha: 28 Enero 2009
	******************************************************************************************* **/
	//alert(Url)
	tb_show(Titulo, Url + '&height='+H+'&width='+W+'&modal='+ Modal + '&callback=' + CallBack, null);
	//para cerrar
	//tb_remove();
}


function curTop(obj){
	//Regresa la posicion del cursor en y
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return toreturn;
}

function curLeft(obj){
	//Regresa la posicion del cursor en x
	toreturn = 0;
	while(obj){
		toreturn += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return toreturn;
}

function jsTextNumero(e) {
	/*Funcion: Solo permite escribir numero en una caja de texto.
	Auto Hector de Leon
	Ejemplo: onkeypress='return jsTextNumero(event)'
	*/
	var charCode;
	charCode = e.keyCode;
	status = charCode;
	if (charCode > 31 && (charCode < 48 || charCode > 63)) {
		return false;
	}
		return true;
}


function jsSelectInicio(cadena)
{
	//Funcion: recive en cadena el identificador a utilizar para seleccionar el objeto que se quiera dar el foco al iniciar.
	setTimeout(function(){document.getElementById(cadena).select()},500);
}
