var respuesta;
var reg_actual; 
var num_campos;		

	 
	function getDataServer(url, vars)
	{
     var xml = null;
     try{
         xml = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(expeption){
         xml = new XMLHttpRequest();
        }
     xml.open("GET",url + vars, false);
     xml.send(null);
     if(xml.status == 404) alert("Url no valida");
     return xml.responseText;
	}
	function eliminar_reg_combo(combo)
    {
	 var f=0,k=combo.length,opcion2;
	 while(f<k)
  	 {
  	  	 combo.remove(combo.childNodes[k]);
  	  	 f++;
  	 }
  	 opcion2 =new Option("...",0); 
	 combo.options[combo.length]=opcion2;
    }
    
  function consultar()
  { 
    var poliza=document.getElementById("co_poliza").value;
    
    if(isNaN(poliza))
    {
        alert("Solo se permiten numeros en el código de la poliza");
        exit;
    }
        		    
	 if (poliza!="")
	 { 
	     respuesta=getDataServer("sistema/funciones_deuda.php","?cons="+poliza);    
		 if (respuesta!=null)
		 {
		 	 var registros = eval('(' + respuesta + ')');		 	
			 if (registros.tam==0)
			 {  
			  alert(registros.mensaje);		 
			 }else
			 {					 	 			 			 
			   CargarRegistros(registros.datos, reg_actual);	      
			 }
		 }		   	  	 	      	 
	 }
	 document.getElementById("montop").value=0;
   }	
   	 		
   function CargarRegistros(registro, reg_actual)   
   {
      document.getElementById("nombrep").value = registro[0].tx_cliente;
	   document.getElementById("nb_cliente").value = registro[0].tx_cliente;	
	   document.getElementById("tx_direccion").value = registro[0].tx_dir;
	   document.getElementById("de_actual").value = registro[0].nu_deuda_act;
	   document.getElementById("mo_deuda_ant").value = registro[0].nu_deuda_ant;
	   document.getElementById("fecha").innerHTML = 'Fecha de Actualizaci&oacute;n: '+registro[0].fe_pase;
	   document.getElementById("div_reclamo").style.display = 'block';
	   document.getElementById("detalle").innerHTML=getDataServer("sistema/funciones_deuda.php","?co_poliza="+document.getElementById("co_poliza").value);
	   
      if(parseInt(registro[0].nu_deuda_act)>0) 
	     document.getElementById("boton_pago").style.display='';
	   else
	     document.getElementById("boton_pago").style.display='none';
   }  
	

function sumar_deuda()
{
    var checkbox = document.getElementById("detalle_deuda").getElementsByTagName("input");
    var i,sum=0,facturas='';
    
    for(i=1;i<checkbox.length;i++)
    {
	if(checkbox[i].checked==true)
	{
	    sum = parseFloat(sum) + parseFloat(checkbox[i].value);
      // facturas = facturas + checkbox[i].id + ':'+checkbox[i].value+'-';
	}
    }
    document.getElementById("montop").value = redondear(sum,2);
    var valor = redondear(sum,2);
    var cadena = valor.toString();
    var cadena2;
    if(parseFloat(document.getElementById("montop").value)>0)
    {
	
      cadena2 = cadena.split(".");
      if(cadena2[1].length<2)
      document.getElementById("montop").value = document.getElementById("montop").value+0;
    }
   // document.getElementById("total_facturas").value = facturas;
}


function redondear(cantidad, decimales) {
	var cantidad = parseFloat(cantidad);
	var decimales = parseFloat(decimales);
	decimales = (!decimales ? 2 : decimales);
	return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
} 


function uno(src,color_entrada) 
{
    src.bgColor=color_entrada;src.style.cursor="hand";
}

function dos(src) 
{
	
	if(src.rowIndex%2==0)
	{
		src.bgColor="#DDDDDD";   
	}
	else
	{
		src.bgColor="#FFFFFF";   
	}
    src.style.cursor="default";
}

function seleccionar_fila(src2,color_entrada)
{
   var fi=0;	 
   var tabla_elemento=document.getElementById('elemento_inventario');
   //coloca el color de cada una de las filas de la tabla elemento_inventario
   while(tabla_elemento.rows.length>fi)
   {
	  dos(tabla_elemento.rows[fi]);
	  fi++;
   } 
   document.getElementById('rubro').value=src2.cells[0].textContent;
   document.getElementById('f_inventario').value=src2.cells[2].textContent;
   src2.bgColor=color_entrada;
}

function cargar_input(str_ubicacion,val,input)
{
	var array_ubicacion = new Array();
	array_ubicacion=str_ubicacion.split("~");  
    for(var st=0;st<array_ubicacion.length;st++)
    {
       if(st%2==0)
       {
       	  if(array_ubicacion[st]==val)
       	  {
             document.getElementById(input).value = array_ubicacion[st+1];
             break;
       	  }
       }
   	}
}
function imprimir()
{
  window.open("reportes/reporte_reclamos.php?co_poliza="+document.getElementById("co_poliza").value,target="_blank"); 
}
function imprimir_recibo(co_transaccion)
{
  window.open("../reportes/recibo.php?co_correlativo="+co_transaccion,target="_blank"); 
}

function pagototal()
{
   var checkbox = document.getElementById("detalle_deuda").getElementsByTagName("input");
   var monto = document.getElementById("de_actual").value;
   if(confirm("Se dispone a cancelar el total de la deuda, cuyo monto es "+ monto +" bolivares \n ¿Está Seguro?"))
   {
	   for(i=1;i<checkbox.length;i++)
	     checkbox[i].checked=true;
	  
	   document.getElementById("montop").value=monto;
	   validar_pago();
	 
	}
	else
	{
	   for(i=1;i<checkbox.length;i++)
	     checkbox[i].checked=false;
	  
	   document.getElementById("montop").value=0;
	}
}

/*funtion marca_periodo(marcar,monto)
{
      for(i=1;i<checkbox.length;i++)
	     checkbox[i].checked=false;
	  
	   document.getElementById("montop").value=0;
}*/

function validar_pago()
{
   if(document.getElementById("montop").value>0)
       document.form1.submit();
   else
       alert("Para efectuar el pago el monto debe ser mayor a 0, Marque en las Casillas las facturas que desea cancelar");
}

//********************************************************
        addEvent(window,'load',inicializarEventos,false);


			function inicializarEventos()
			{
			// Aquie obtienes mediante DOM el control a traves de ID 
			  var ob1=document.getElementById('co_poliza');
			
			// Se le agrega al objeto el evento (keypress), y la funcion que se va a ejecutar al presionar cualquie tecla...('presionar')
			  addEvent(ob1,'keypress',presionar,false);
			}
			
			
			function presionar(e)
				{
				//Esta parrte es para IE
				if (window.event)
				  {
				           if (window.event.keyCode==13)
					{consultar()}// Aqui escribe el nombre tu funcion que hace la busqueda...
				  }
				  else
			                    //Esto es para Firefox y creo otros navegadores
					if (e)
					{
					  if(e.which==13)
					  	{consultar()}//Igual que arriba
					}
				}
				
			
			//Lo que hace la funcion addEvent es agregar la funcion para IE u otros navegadores, en IE es attachEvent y en los otros navegadores es addEventListener, fijense como se antepone el "on" para hacer referencia al evento para IE mientras que en los otros navegadores no es necesario...
			
			function addEvent(elemento,nomevento,funcion,captura)
			{
			  if (elemento.attachEvent)
			  {
			    elemento.attachEvent('on'+nomevento,funcion);
			    return true;
			  }
			  else  
			    if (elemento.addEventListener)
			    {
			      elemento.addEventListener(nomevento,funcion,captura);
			      return true;
			    }
			    else
			      return false;
			}
  
         function marcar()
         {
             var checkbox = document.getElementById("detalle_deuda").getElementsByTagName("input");

             for(i=1;i<checkbox.length;i++)
				 {
							if(document.getElementById("marca").checked==true)
							   checkbox[i].checked=true;
							else
							   checkbox[i].checked=false;
				 }  
				 sumar_deuda();    
				
         }
  //********************************************************

