function js_redirecccionar(strURL){
   top.document.location.href=strURL;
}
function js_mensaje(mensaje){
   alert(mensaje);
}
function js_isNum(q) {

  for ( i = 0; i < q.length; i++ ) {
    //con el for y la sentencia if( q.charAt(i) = " " ){... ..ya me queda lista para validar los espacios en blanco, de lo contrario:
    valor = parseInt(q.charAt(i)); // me permite convertir letra por letra en numero y si no es un numero entonces no regresa nada
    if (isNaN(valor)) {
      return true;
    }
  }
  return false
}

function js_formatNumber(num,decimales,prefix){
  prefix = prefix || '';
  decimales = decimales || 0;
  num=num.toFixed(decimales);
  num += '';
  var splitStr = num.split('.');
  var splitLeft = splitStr[0];
  var splitRight = splitStr.length > 1 ? '.' + splitStr[1] : '';
  var regx = /(\d+)(\d{3})/;
  while (regx.test(splitLeft)) {
    splitLeft = splitLeft.replace(regx, '$1' + ',' + '$2');
  }
  return prefix + splitLeft + splitRight;


 //alert("formateado");
}

function unformatNumber(num) {
return num.replace(/([^0-9\.\-])/g,'')*1;
} 

function js_solo_numeros(cadena, obj, e){
   opc = false;
   tecla = (document.all) ? e.keyCode : e.which;
   if ((tecla >= 48 && tecla <= 57) || tecla==8 || tecla==0)
   opc = true;   
   return opc;
}
function js_solo_num_cuenta(cadena, obj, e){
   opc = false;
   tecla = (document.all) ? e.keyCode : e.which;
   if(tecla==8 || tecla==0 || tecla==45) return true;
   if (tecla >= 48 && tecla <= 57) opc = true;
   return opc;
}
function js_solo_fecha(cadena, obj, e){
   opc = false;
   tecla = (document.all) ? e.keyCode : e.which;
   if(tecla==8 || tecla==0 || tecla==47) return true;
   if (tecla >= 48 && tecla <= 57) opc = true;
   return opc;
}
function formateafecha(fecha){
          var long = fecha.length;
          var dia;
          var mes;
          var ano;
          var primerslap=false;
          var segundoslap=false;

          if ((long>=2) && (primerslap==false)) { dia=fecha.substr(0,2);
          if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); primerslap=true; }
          else { fecha=""; primerslap=false;}
          }
          else
          { dia=fecha.substr(0,1);
          if (IsNumeric(dia)==false)
          {fecha="";}
          if ((long<=2) && (primerslap=true)) {fecha=fecha.substr(0,1); primerslap=false; }
          }
          if ((long>=5) && (segundoslap==false))
          { mes=fecha.substr(3,2);
          if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); segundoslap=true; }
          else { fecha=fecha.substr(0,3);; segundoslap=false;}
          }
          else { if ((long<=5) && (segundoslap=true)) { fecha=fecha.substr(0,4); segundoslap=false; } }
          if (long>=7)
          { ano=fecha.substr(6,4);
          if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); }
          else { if (long==10){ if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } } }
          }

          if (long>=10)
          {
            fecha=fecha.substr(0,10);
            dia=fecha.substr(0,2);
            mes=fecha.substr(3,2);
            ano=fecha.substr(6,4);
            // Año no viciesto y es febrero y el dia es mayor a 28
            if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; }
          }
          return (fecha);
}
function IsNumeric(valor) 
                               { 
                                               var log=valor.length; var sw="S"; 
                                               for (x=0; x<log; x++) 
                                               { v1=valor.substr(x,1); 
                                               v2 = parseInt(v1); 
                                               //Compruebo si es un valor numérico 
                                               if (isNaN(v2)) { sw= "N";} 
                                               } 
                                               if (sw=="S") {return true;} else {return false; } 
                               }
function js_solo_documento(cadena, obj, e){
   opc = false;
   tecla = (document.all) ? e.keyCode : e.which;
   if(tecla==8 || tecla==0) return true;
   if ((tecla >= 65 && tecla <= 90) || (tecla >= 97 && tecla <= 122) || (tecla >= 48 && tecla <= 57)) opc = true;
   return opc;
}
function js_solo_telefono(cadena, obj, e){
   opc = false;
   tecla = (document.all) ? e.keyCode : e.which;
   if ((tecla >= 48 && tecla <= 57) || tecla==8 || tecla==0  || tecla==40 || tecla==41 || tecla==43 || tecla==45)
   opc = true;   
   return opc;
}
function js_solo_letras_persona(cadena, obj, e){
   opc = false;
   tecla = (document.all) ? e.keyCode : e.which;
   if(tecla==8 || tecla==0 || tecla==32 || tecla==241 || tecla==209) return true;
   if ((tecla >= 65 && tecla <= 90) || (tecla >= 97 && tecla <= 122)) opc = true;
   return opc;
}
function js_trim(cadena){
   cadena = js_rTrim(cadena);
	cadena = js_lTrim(cadena);	
	return cadena;
}
function js_rTrim(cadena){
  
    for (ic = 0; ic <= cadena.length -1 ; ic++){
		if (cadena.charAt(ic) != " "){
			return cadena.substring(ic,cadena.length);
		}
	}
	return "";
}
function js_lTrim(cadena){
    for (ic = cadena.length -1 ; ic >= 0; ic--){
		if (cadena.charAt(ic) != " "){
			return cadena.substring(0,ic + 1);
		}
	}
	return cadena;
}
function js_valida_fecha(cadena){   
   var Fecha= new String(cadena)   
   var RealFecha= new Date()
   var Ano= new String(Fecha.substring(Fecha.lastIndexOf("/")+1,Fecha.length))
   var Mes= new String(Fecha.substring(Fecha.indexOf("/")+1,Fecha.lastIndexOf("/")))
   var Dia= new String(Fecha.substring(0,Fecha.indexOf("/")))
   
   if(Ano == "" || Mes == "" || Dia == "") return false;
   
   if(isNaN(Ano) || Ano.length<4 || parseFloat(Ano)<1900) return false;
   if(isNaN(Mes) || parseFloat(Mes)<1 || parseFloat(Mes)>12) return false;
   if(isNaN(Dia) || parseInt(Dia, 10)<1 || parseInt(Dia, 10)>31) return false;
   if(Mes==2 && (Ano % 4) == 0 && Dia <= 29)return true;    
   if (Mes==4 || Mes==6 || Mes==9 || Mes==11 || Mes==2){      
      if (Mes==2 && Dia > 28 || Dia>30) return false;
   }
   return true;
}
function js_comparar_fechas(fmin, fmax){
  //compara 2 fechas en formado dd/mm/aaaa
  sepfechamin=fmin.split("/");
  sepfechamax=fmax.split("/");
  minimo=new Date(sepfechamin[2],(sepfechamin[1]-1),sepfechamin[0]);
  maximo=new Date(sepfechamax[2],(sepfechamax[1]-1),sepfechamax[0]);
  
  //alert("fmin="+minimo+" fmax="+maximo);
  if(maximo>=minimo) return true;
  else  return false;
}
function js_validar_rango_edad(fnac,min, max){
     var fecha_sep=fnac.split("/");
      var edad=js_calcular_edad(fecha_sep[0],fecha_sep[1],fecha_sep[2]);
      if(edad>max){
        //alert("edad("+edad+")>"+max);
        return false;
      }
      if(edad<min){
        //alert("edad("+edad+")<"+min);
        return false;
      }
      return true;
}
function js_validar_rango_edad_a_fecha(fnac,fecha,min, max){
     var fecha_sep=fnac.split("/");
      var edad=js_calcular_edad_a_fecha(fecha_sep[0],fecha_sep[1],fecha_sep[2],fecha);
      //alert("edad="+edad);
      if(edad>max){
        //alert("edad("+edad+")>"+max);
        return false;
      }
      if(edad<min){
        //alert("edad("+edad+")<"+min);
        return false;
      }
      return true;
}
function js_valida_mail(email){
	var re  = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+$/;
	if (!re.test(email)) {
		return false;
	}
	return true;
}
function js_login(){
   if(js_trim(document.getElementById("l_documento_numero").value) == ""){
      alert("Ingrese su número de documento por favor.");
      document.getElementById("l_documento_numero").focus();
      return false;
   }
   if(js_trim(document.getElementById("l_contrasena").value) == ""){
      alert("Ingrese su número de documento por favor.");
      document.getElementById("l_documento_numero").focus();
      return false;
   }
   document.frm_login.submit();   
   //FAjax('ajax_login.php','div_login','accion=USUARIO_LOGIN&accion_id=<?php echo $_SESSION["accion_id"];?>&documento_tipo='+document.getElementById("l_documento_tipo").value+'&documento_numero='+document.getElementById("l_documento_numero").value+'&contrasena='+document.getElementById("l_contrasena").value,'POST');
}
function js_imprimir(vista,accion){
   if(vista == "V"){
      var cabecera = "";
      var criterio = "";
      if(document.getElementById("div_cabecera") != null) cabecera = document.getElementById("div_cabecera").innerHTML;
      if(document.getElementById("div_criterio") != null) criterio = document.getElementById("div_criterio").innerHTML;
      
      ifrm_impresion.document.getElementById("impresion_cabecera").innerHTML = cabecera + criterio;
      ifrm_impresion.document.getElementById("impresion_contenido").innerHTML = document.getElementById("div_contenido").innerHTML;
      ifrm_impresion.focus();
      ifrm_impresion.print();
   }
   if(vista == "R"){
      js_impresion_capa_carga_mostrar();
      FAjax('ajax.php','impresion_contenido','accion='+accion+'&imprimir=TRUE','POST','','js_impresion_cargar();js_impresion_capa_carga_ocultar();');
   }
}
function js_impresion_cargar(){
  
   var cabecera = "";
   var criterio = ""; 
   if(document.getElementById("div_cabecera") != null) cabecera = document.getElementById("div_cabecera").innerHTML;
   if(document.getElementById("div_criterio") != null) criterio = document.getElementById("div_criterio").innerHTML;
   
   ifrm_impresion.document.getElementById("impresion_cabecera").innerHTML = cabecera + criterio;
   ifrm_impresion.document.getElementById("impresion_contenido").innerHTML = document.getElementById("impresion_contenido").innerHTML;
   ifrm_impresion.focus();
   ifrm_impresion.print();   
}
function js_impresion_capa_carga_ocultar(){
   document.getElementById("impresion_capa_carga").style.display = 'none';
}
function js_impresion_capa_carga_mostrar(){
   document.getElementById("impresion_capa_carga").style.display = 'block';
}
function js_exportar_reporte(nombre,tipo,accion_params,ver_campos){
      //alert("hola"); 
      document.getElementById("nombrexls").value=nombre;
      if(ver_campos!=undefined) accion_params+="&ver_campos="+ver_campos;
      document.getElementById("formuxls").action="reporte_"+tipo+".php";
      //alert(" paramess: "+accion_params);
      FAjax('ajax.php','xls_contenido','accion='+accion_params+'&imprimir=TRUE','POST','','js_excel_cargar();');   

}
function js_excel_cargar(){
   
    alert("Archivo Cargado");
    var datos=document.getElementById("xls_contenido").innerHTML;
    var datos2 = document.getElementById("tablita");
    datos2.value=datos;
    document.getElementById("formuxls").submit();  
   
}
function js_calcular_edad(dia_nac,mes_nac,ano_nac){
    
    hoy=new Date();
    edad=hoy.getFullYear()- ano_nac - 1; //-1 porque no se si ha cumplido años ya este año 

    //si resto los meses y me da menor que 0 entonces no ha cumplido años. Si da mayor si ha cumplido
     
    if (hoy.getMonth() + 1 - mes_nac < 0) return edad;//+ 1 porque los meses empiezan en 0 
    if (hoy.getMonth() + 1 - mes_nac > 0) return edad+1;

    //entonces es que eran iguales. miro los dias 
    //si resto los dias y me da menor que 0 entonces no ha cumplido años. Si da mayor o igual si ha cumplido 
    if (hoy.getUTCDate() - dia_nac >= 0) return edad+1; 

    return edad
}
function js_calcular_edad_a_fecha(dia_nac,mes_nac,ano_nac,fecha_calcu){
    var fcalcu=fecha_calcu.split("/");
    var fecha_dia=fcalcu[0];
    var fecha_mes=fcalcu[1];
    var fecha_ano=fcalcu[2];
    var fmes=parseInt(fecha_mes,10)-1;//para trabajar con meses de 0 a 11
    hoy=new Date(fecha_ano,fmes,fecha_dia);
    edad=hoy.getFullYear()- ano_nac - 1; //-1 porque no se si ha cumplido años ya este año 

    //si resto los meses y me da menor que 0 entonces no ha cumplido años. Si da mayor si ha cumplido
     
    if (hoy.getMonth() + 1 - mes_nac < 0) return edad;//+ 1 porque los meses empiezan en 0 
    if (hoy.getMonth() + 1 - mes_nac > 0) return edad+1;

    //entonces es que eran iguales. miro los dias 
    //si resto los dias y me da menor que 0 entonces no ha cumplido años. Si da mayor o igual si ha cumplido 
    if (hoy.getUTCDate() - dia_nac >= 0) return edad+1; 

    return edad
}
function js_validar_talla_peso(talla, peso){
  
  var entero_talla=Math.floor(talla);
  var dec_talla=Math.floor((talla-entero_talla)*100);
  if(entero_talla>1)  dec_talla+=((entero_talla-1)*100);
  //alert("talla="+talla+", peso="+peso+", etalla="+entero_talla+", dtalla="+dec_talla);
  var diferencia=peso - dec_talla;
  if((diferencia > 15)||(diferencia < -15)){ 
    return false;
  }else{ return true; }
}
function datosformulario(formid){         
         var Formulario = document.getElementById(formid);
         var longitudFormulario = Formulario.elements.length;
         var cadenaFormulario = "";
         var sepCampos;
         sepCampos = "";
         for (var i=0; i <= Formulario.elements.length-1;i++) {
         	cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+encodeURI(Formulario.elements[i].value);
         	sepCampos="&";
         }
}