var READY_STATE_COMPLETE=4;
var peticion_http = null;
 
function inicializa_xhr() {
  if(window.XMLHttpRequest) {
    return new XMLHttpRequest(); 
  }
  else if(window.ActiveXObject) {
    return new ActiveXObject("Microsoft.XMLHTTP");
  } 
}
function inicio(){
	var tpnorma = document.form1.tpnorma.value
	var numeronorma = document.form1.numeronorma.value
	var organismo = document.form1.organismo.value
	var texto = document.form1.texto.value
	var d = document.form1.d.value
	var m = document.form1.m.value
	var y = document.form1.y.value	
	var d2 = document.form1.d2.value
	var m2 = document.form1.m2.value
	var y2 = document.form1.y2.value
	document.form1.es.value = 0;		
	mm = document.getElementById("proceso");
	mm2 = document.getElementById("resultado");
	mm2.innerHTML = "";
	mm.innerHTML = '<p align="center"><img border="0" src="spinner.gif" width="20" height="20"></p>';
	peticion_http = inicializa_xhr();  
	if(peticion_http) {
		peticion_http.onreadystatechange = resultado;
		peticion_http.open("POST", "inicio.php", true);
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var buscar = "tpnorma="+tpnorma+"&numeronorma="+numeronorma+"&organismo="+organismo+"&texto="+texto+"&d="+d+"&m="+m+"&y="+y+"&d2="+d2+"&m2="+m2+"&y2="+y2+"&nocache=" + Math.random();
		peticion_http.send(buscar);
	}
}
function resultado() {
    if(peticion_http.readyState == READY_STATE_COMPLETE) {
  		if(peticion_http.status == 200) {
			mm = document.getElementById("proceso");
			var r = peticion_http.responseText;
			document.form1.reset();
			r = r.split("##");
			if(r[0]=="ok"){
				var tt = parseInt(r[1]);
				if(tt == 90){
					var max = " (L&iacute;mite m&aacute;ximo)";
				}else{
					var max = "";
				}
				mm.innerHTML = '<p align="center">Total coincidencias: '+r[1]+max+'<br>';			
				document.form1.es.value = String(r[1]);				
				pagina('0');
			}
			if(r[0]=="xx"){
				alert("NO SE ENCONTRARON COINCIDENCIAS")
				mm.innerHTML = "";
			}
			if(r[0]=="nr"){
				alert("DEBE INGRESAR UN CRITERIO DE BUSQUEDA")
				mm.innerHTML = "";				
			}
			if(r[0]=="ff"){
				alert("DEBE INDICAR UN RANGO DE FECHA VALIDO")
				mm.innerHTML = "";				
			}
		}
	}
}
function pagina(pag){
	var total = document.form1.es.value
	mm = document.getElementById("resultado");
	mm.innerHTML = '<p align="center"><img border="0" src="spinner.gif" width="20" height="20"></p>';
	peticion_http = inicializa_xhr();  
	if(peticion_http) {
		peticion_http.onreadystatechange = resultado2;
		peticion_http.open("POST", "index2.php", true);
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var buscar = "st="+pag+"&total="+total+"&nocache=" + Math.random();
		peticion_http.send(buscar);
	}
}
function resultado2() {
    if(peticion_http.readyState == READY_STATE_COMPLETE) {
  		if(peticion_http.status == 200) {
			mm = document.getElementById("resultado");
			var r = peticion_http.responseText;
			mm.innerHTML = r;
		}
	}
}




function verpdf2(libro, pagina){
	//alert(libro + " - " + pagina);
	// El alto max posible 
	var L=screen.availHeight; 
	// El ancho max posible 
	var A=screen.availWidth; 
	var ruta = 'listado2.php?libro='+libro+'&pagina='+pagina;
	window.open(ruta,"miventana","width="+A+",height="+L+",menubar=no,Scrollbars=YES")	
}
function verpdf(ff, dd, pr){
	// El alto max posible 
	var L=screen.availHeight; 
	// El ancho max posible 
	var A=screen.availWidth; 
	var ruta = 'listado.php?fecha='+ff+'&dia='+dd+'&pr='+pr;
	window.open(ruta,"miventana","width="+A+",height="+L+",menubar=no,Scrollbars=YES")	
}
function BuscaAutoLey(tipo)
{
	
	hoyDia = new Date() ;
	if(tipo != '')
	{
		document.form1.tpnorma.selectedIndex=tipo;
	}
	else
	{
		
		document.form1.d.value = '1';
		document.form1.m.value = '1';
		document.form1.y.value = '2010';
		
		document.form1.d2.value = hoyDia.getDate();
		document.form1.m2.value = hoyDia.getMonth()+1;//el mes comienza en 0
		document.form1.y2.value = hoyDia.getFullYear();
	}
	
   inicio();
}

function validar(){
	var cadena = document.form1.texto.value
	if(cadena != ""){
		if(cadena.length < 3){
			alert("Debe ingresar al menos tres caracteres de búsqueda")
			document.form1.texto.select()
			document.form1.texto.focus()
			return 0;
		}
	}
    var desde;
	var hasta;
	
	desde = document.form1.y.value+"-"+document.form1.m.value+"-"+document.form1.d.value;
    hasta =  document.form1.y2.value+"-"+document.form1.m2.value+"-"+document.form1.d2.value;
    document.form1.desde.value=desde;
	document.form1.hasta.value=hasta;
	inicio();
	//document.form1.submit();
}
function dia(){
	document.form1.d2.value = document.form1.d.value
}
function mes(){
	document.form1.m2.value = document.form1.m.value
}
function anio(){
	document.form1.y2.value = document.form1.y.value
}

function uno(src,color_entrada) {
    src.bgColor=color_entrada;
	src.style.cursor="pointer";
}
function dos(src,color_default) {
    src.bgColor=color_default;
	src.style.cursor="default";
}

//busca las normas madificadas 
var topDivFlotante = 0;
function modificacion(tipoModificacion,fecha,org,tipo,num,level,event)
{
	peticion_http = inicializa_xhr();  
	if(peticion_http) {
		peticion_http.onreadystatechange = resultadoModificacion;
		peticion_http.open("POST", "buscaBitacora.php", true);
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var buscar = "tipoModificacion="+tipoModificacion+"&fecha="+fecha+"&org="+org+"&tipo="+tipo+"&num="+num+"&nocache=" + Math.random();
		peticion_http.send(buscar);
	
	$('#datos_modificada').css("top", ( $(window).height() - 150 ) / 2+$(window).scrollTop() + "px");

	}
}
function resultadoModificacion() {
    if(peticion_http.readyState == READY_STATE_COMPLETE) {
  		if(peticion_http.status == 200) {
			capa = document.getElementById("datos_modificada");
			var r = peticion_http.responseText;
			capa.innerHTML = r;
			capa.style.display='block';
		}
	}
}


