// 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= '<div alig="center"><Table align="center" border="0"><Tr><Td><br><img src="img/loadingAnimation.gif" align="center"></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 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){
	/* *******************************************************************************************

	 Funci�n que hace uso de un la funcion tb_show() del thickbox.js para mostrar una ventana flotante

	******************************************************************************************* **/
	//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(cadena, obj, e) {
	/*Funcion: Solo permite escribir numero en una caja de texto.
	Auto Hector de Leon
	Ejemplo: onkeypress='return jsTextNumero(event)'
	*/
	opc = false;
	tecla = (document.all) ? e.keyCode : e.which;

	if (cadena == "%d")
	if (tecla > 47 && tecla < 58)
	opc = true;
	if (cadena == "%f"){
	if ((tecla > 47 && tecla < 58)||tecla==8||tecla==9||tecla==0)
	opc = true;
	//para si se quiere punto---------------------------------------------
	//if (obj.value.search("[.*]") == -1 && obj.value.length != 0)
	//if (tecla == 46)
	//opc = true;
	//--------------------------------------------------------------------
	}

	return opc;

}

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);
}

function jsCheckBoxAll(limite)
{
	//funcion: selecciona todos los checkbox de una tabla
	for(i=0;i<limite;i++)	
	{
		if(document.getElementById("checkAll").checked)
			if(document.getElementById("tr"+i))
				document.getElementById("tr"+i).checked=true;
			else
				return false;
		else
			if(document.getElementById("tr"+i))
				document.getElementById("tr"+i).checked=false;
			else
				return false;
	}
}

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><br><img src="img/loadingAnimation.gif" align="center"></Td></Tr></Table></div>';	  
	
	$("#"+objeto).load(url)

}

function jsDialogIt(url, id, titulo, ancho, alto, bloquear, cerrar, arrastra, resize){
	// Abre un cuadro de dialogo con una carga din�mica (sustituto de jsWindowIt)
	//jsDialogIt('catalogos/activoFijo_tipode_modificar.php?id='+id, 'dlgModificaTipoDe', 'Modificar Tipo de Activo Fijo');

	var dlgId = id || 'ididid';
	ancho = ancho || 600;
	alto = alto || 130;
	titulo = titulo || '';
	bloquear = bloquear===undefined?true:bloquear;
	cerrar = cerrar===undefined?true:cerrar;
	arrastra = arrastra===undefined?true:arrastra;
	resize = resize===undefined?false:resize;
	if ($('#'+dlgId).length == 0) {
		var newDialog = document.createElement('DIV');
		newDialog.id = dlgId;
		$('body').prepend(newDialog);
		$('#'+newDialog.id).html('<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>');
		$('#'+newDialog.id).load(url);
		$('#'+newDialog.id).dialog({
			modal: bloquear,
			width: ancho,
			height: alto,
			closeOnEscape: cerrar,
			draggable: arrastra,
			resizable: resize,
			title: titulo,
			zIndex: 1000000,
			hide: 'fold',
			close: function(){
				$('#'+newDialog.id).remove();
			}
		});
	}
	else {
		$('#'+dlgId).effect('shake',{},50);
	}
}


function jsReturnCheckBox(limite)
{
	//funcion que regresa la cadena de lso checkbox seleccionados
	var cadena="";
	for(i=0;i<limite;i++)	
	{
			if (document.getElementById("tr" + i)) {
				if (document.getElementById("tr" + i).checked == true) 
					cadena += document.getElementById("tr" + i).value + ",";
			}
			else 
				i = limite;				
	}
	cadena=cadena.substr(0,cadena.length-1);
	return cadena;
}


function trim(cadena)
{
	//funcion que sirve para hacer trim
	for(i=0; i<cadena.length; )
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1)
	{
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena
	//forma.caja2.value=cadena;
}

function jsEvaluaEmail(valor){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(valor))
	{
		return (true)
	} 
	else 
	{
		alert("La direccion de email ("+valor+") es incorrecta.");
		return (false);
	}
}

function jsRedirigeIE(){
	//redirige para instalar internet explorer 7
	alert("Esta pagina esta diseñada para ser vista en Internet Explorer 7 o posteriores, porfavor actualiza tu navegador")
	if(confirm("Deseas actualizarlo ahora!"))
	{
		document.location.href=("http://www.microsoft.com/spain/windows/internet-explorer/download-ie.aspx");	
	}
}

function jsBounceLeft(objeto)
{
	//mueve un objeto a la izquierda lo tambalea
	$(objeto).effect("bounce", { times:3,direction:"left",distance:20 }, 300)
}


getDimensions = function(oElement)
{
	//obtener la dimension de un objeto y posicion
	var x, y, w, h;
	 x = y = w = h = 0;
	 if (document.getBoxObjectFor)
	 { // Mozilla
		 var oBox = document.getBoxObjectFor(oElement);
		 x = oBox.x-1;
		 w = oBox.width;
		 y = oBox.y-1;
		 h = oBox.height;
	 }
	 else if (oElement.getBoundingClientRect)
	 { // IE
		 var oRect = oElement.getBoundingClientRect();
		 x = oRect.left-2;
		 w = oElement.clientWidth;
		 y = oRect.top-2;
		 h = oElement.clientHeight;
	 }
	 return {x: x, y: y, w: w, h: h};
}

function jsMuestraDiv(div)
{
	d=document.getElementById(div)
	if(d.style.display=="none")
	{
		$("#"+div).show();
	}
	else
	{
		$("#"+div).hide();	
	}
}

function jsRefreshTxt(obj,cadena)
{
	//poine un texto en un text y lo quita
	if(obj.value==cadena)
	{
		obj.style.color="#000";
		obj.value="";
	}
	else
	return false;
}

function jsReloadTxt(obj,cadena)
{
		//poine un texto en un text y lo quita
	if(trim(obj.value)=="")
	{
		obj.style.color="#666";
		obj.value=cadena;
	}
	else
	return false;
}