function verificaNro(nro){
for (var i=0;i<nro.length ; i++)
{
  var ch=nro.substring(i,i+1);
  if(ch<"0" || ch >"9")
     return false;
}
return true;
}
//-----------------------
function cambia_picadas(queform){
var cabecera
 cabecera= queform.elements['inicio'].value;
 if (cabecera!=0){
   mis_picadas=eval("picadas_de" + cabecera);
   cod_picadas=eval("cod_picadas" + cabecera);
   num_picadas=mis_picadas.length;
   queform.elements['fin'].length=num_picadas +1 ;
   queform.elements['fin'].options[0].value=0;
   queform.elements['fin'].options[0].text="";
   for (i=1;i<=num_picadas;i++){
      queform.elements['fin'].options[i].value=cod_picadas[i - 1];
      queform.elements['fin'].options[i].text=mis_picadas[i - 1];
   }
 }
 else {
   queform.elements['fin'].length=0;
 }
}
//------------------------------------
function addfilas(id){
var tbody = document.getElementById(id).getElementsByTagName("TBODY")[0];
var row;
var td1;
var td2;
var cabecera;
var ingreso;
var td3;
var selecttipo;
var i,  mayores;
var van=eval("rowscompa");
if (verificaNro(document.frmregistro.mayores.value)){
if (document.frmregistro.mayores.value=="")
       mayores=0;
else
	   mayores=eval(document.frmregistro.mayores.value);
var cuantos=mayores;
//si no habia inputs anterirores muestra cabecera
if ((van==0)&& (cuantos>0))
{
 row= document.createElement("TR");
 row.setAttribute("id","cabeacom2");
 cabecera=document.createElement("TH");
 cabecera.appendChild(document.createTextNode("Nombre / Name"));
 cabecera.setAttribute("colSpan","2");
 cabecera.setAttribute("align","center");
 row.appendChild(cabecera);
 cabecera=document.createElement("TH");
 cabecera.appendChild(document.createTextNode("Documento / Id Number"));
 cabecera.setAttribute("align","center");
 row.appendChild(cabecera);
 cabecera=document.createElement("TH");
 cabecera.appendChild(document.createTextNode("Edad / Age"));
 cabecera.setAttribute("align","center");
 row.appendChild(cabecera);
 tbody.appendChild(row);
}
//si faltan inputs agrego
if (van<cuantos){
 i=van + 1;
 while (i<=cuantos){
 row= document.createElement("TR");
 row.setAttribute("id","row" + i)
 td1 = document.createElement("TD");
 td1.setAttribute("align","center");
 td1.setAttribute("colSpan","2");
// td1.setAttribute("class","frm");
 td1.style.background="rgb(200,200,200)";
 ingreso=document.createElement("INPUT");
 ingreso.setAttribute("type","text");
 ingreso.setAttribute("name","nombre"+i);
 ingreso.setAttribute("id","nombre"+i);
 ingreso.setAttribute("size","50");
 ingreso.setAttribute("maxlength","50");
 row.appendChild(td1);
 td1.appendChild(ingreso);

 td2 = document.createElement("TD");
// td2.setAttribute("class","frm");
 td2.style.background="rgb(200,200,200)";
 selecttipo=document.createElement("SELECT");
 selecttipo.setAttribute("name","tipodoc"+i);
 var op1=document.createElement("option");
 var op2=document.createElement("option");
 var op3=document.createElement("option");
 var op4=document.createElement("option");
 op1.value="D.N.I";
 op2.value="L.C";
 op3.value="L.E";
 op4.value="Pasaporte";
 var txt1=document.createTextNode("D.N.I");
 var txt2=document.createTextNode("L.C");
 var txt3=document.createTextNode("L.E");
 var txt4=document.createTextNode("Pasaporte");
 op1.appendChild(txt1);
 op2.appendChild(txt2);
 op3.appendChild(txt3);
 op4.appendChild(txt4);
 selecttipo.appendChild(op1);
 selecttipo.appendChild(op2);
 selecttipo.appendChild(op3);
 selecttipo.appendChild(op4);
 ingreso=document.createElement("INPUT");
 ingreso.setAttribute("type","text");
 ingreso.setAttribute("name","doc"+i);
 ingreso.setAttribute("size","15");
 ingreso.setAttribute("maxlength","15");
 row.appendChild(td2);
 td2.appendChild(selecttipo);
 td2.appendChild(ingreso);

 td3 = document.createElement("TD");
// td3.setAttribute("class","frm");
 td3.style.background="rgb(200,200,200)";
 td3.setAttribute("align","center");
 ingreso=document.createElement("SELECT");
 ingreso.setAttribute("name","edad"+i);
 for (j=0;j<100;j++ )
 {
   var op=document.createElement("option");
   op.value=j;
   var txt=document.createTextNode(j);
   op.appendChild(txt);
   ingreso.appendChild(op);
 }

 row.appendChild(td3);
 td3.appendChild(ingreso);
 tbody.appendChild(row);
 i=i+1;
}
}
//si sobran inputs borro
if (rowscompa>cuantos)
{
 i=rowscompa;
 while(i>cuantos){
  row=document.getElementById("row" + i);
  tbody.removeChild(row);
  i=i-1;
 }
}
//si no hay que mostrar inputs borro cabeceras
if ((cuantos==0) && (van>0))
{
  row=document.getElementById("cabeacom2");
  tbody.removeChild(row);
}
rowscompa=cuantos;
}
else
  alert("Debe ingresar un Nro");
}
//-------------------------------------------------------
function valiFechas(fechad,fechah){
var aniod=fechad.getFullYear();
var anioh=fechah.getFullYear();
if (anioh<aniod)
     return false;
if (anioh>aniod)
     return true; 
if (anioh==aniod)
{
   var mesd=fechad.getMonth();
   var mesh=fechah.getMonth();
   if (mesh<mesd) return false;
   if (mesh>mesd) return true;
   if (mesh==mesd)   {
		var diad=fechad.getDate();
		var diah=fechah.getDate();
		if (diah<diad)return false;
   }
}
return true;
}
//----------------------------------------------
function Is_inNivel(vector, picada)
{// retorna true si encuentra la picada en el vector, false sino lo encuentra
 var cuantosVec=vector.length;
 var seguir=true;
 var i=1
 while(( seguir) && (i<=cuantosVec))
 {
    if (vector[i]==picada)    
	   seguir=false; //lo encontró no sigue buscando
	i=i +1;	
 }
 return !seguir;
}
//----------------------------------------------
function Nivel_enMes(quemes,picada)
{// devuelve el nivel de la picada para el mes pasado, sino esta en los primeros 3 niveles devuelve 4
   var vector;
   vector=eval("niveluno" + quemes);
   if (Is_inNivel(vector,picada))
		return 1;
   else
   {
      vector=eval("niveldos" + quemes);
	  if (Is_inNivel(vector,picada))
		return 2;
      else
	  {
	     vector=eval("niveltres" + quemes);
   	     if (Is_inNivel(vector,picada))
		   return 3;
         else
		   return 4;
	  }
   }
}
//-----------------------------------
function Max_nivelinPer(mesIni, mesFin)
{//devuelve maximo nivel requerido dentro de un periodo
   var picada=document.frmregistro.fin.value;
   var maxNivel= 0;
   var nNivel;
   var contMes;
   contMes=mesIni;
   if (mesFin<contMes) // ej diciembre enero
   {
      while (contMes<=12)
      {
         nNivel=Nivel_enMes(contMes,picada);
		 if (nNivel>maxNivel)
		 {
			maxNivel=nNivel;
		 }
		 contMes=contMes+1
      }
   }
   if (contMes>12)
   {
      contMes=1;
   }
   while (contMes<=mesFin)
   {
         nNivel=Nivel_enMes(contMes,picada);
		 if (nNivel>maxNivel)
		 {
			maxNivel=nNivel;
		 }
		 contMes=contMes+1
   }
   return maxNivel;
}
//-------------------------------------------
function Is_conGuia(mesIni, mesFin)
{//devuelve true si dentro del periodo requiere guia
   var picada=document.frmregistro.fin.value;
   var contMes;
   var vector;
   contMes=mesIni;
   if (mesFin<contMes) // ej diciembre enero
   {
      while (contMes<=12)
      {
        vector=eval("conguia" + contMes);
  	    if (Is_inNivel(vector,picada))
		 return true;
        contMes=contMes +1;
      }
   }
   if (contMes>12)
   {
      contMes=1;
   }
   while (contMes<=mesFin)
   {
        vector=eval("conguia" + contMes);
  	    if (Is_inNivel(vector,picada))
		 return true;
        contMes=contMes +1;
   }
   return false;
}
//-------------------------------------------------------
function valiFechaini(fechad){
var hoy=new Date();
var msPerdia=24*60*60*1000;
var daysLeft=(fechad.getTime() - hoy.getTime())/msPerdia;
if (daysLeft<=-1)
{
 alert("La fecha de inicio es menor a la fecha actual!")
 return false;
}
if(daysLeft>=2){
 alert("El registro solo puede tramitarse hasta con 48 horas de antelación!!")
 return false;
}
return true;
}
//-------------------------------------------------------
function validaRec(){
var quemes=new Array('','Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');
var i, nmes;
//var nf=new function ("valacom","return false;");
if (document.frmregistro.nombre.value=="")
{
    alert("Debe Ingresar el Nombre del responsable del grupo");
    document.frmregistro.nombre.focus();
    return false;
}
if (document.frmregistro.domicilio.value=="")
{
    alert("Debe Ingresar el Domicilio");
    document.frmregistro.domicilio.focus();
    return false;
}
if (document.frmregistro.localidad.value=="")
{
    alert("Debe Ingresar la Localidad");
    document.frmregistro.localidad.focus();
    return false;
}
if (document.frmregistro.pais.value=="")
{
    alert("Debe Ingresar el Pais");
    document.frmregistro.pais.focus();
    return false;
}
if (document.frmregistro.nrodoc.value=="")
{
    alert("Debe Ingresar el Numero de Documento");
    document.frmregistro.nrodoc.focus();
    return false;
}
if (document.frmregistro.correo.value=="")
{
    alert("Debe Ingresar la cuenta de Correo Electrónico");
    document.frmregistro.correo.focus();
    return false;
}
if (document.frmregistro.inicio.value=="")
{
    alert("Debe Ingresar el Lugar de Inicio");
    document.frmregistro.inicio.focus();
    return false;
}
if (document.frmregistro.fini.value=="")
{
    alert("Debe Ingresar la Fecha de Inicio ");
    document.frmregistro.fini.focus();
    return false;
}
else{
    var strfecha=document.frmregistro.fini.value; 
	var finiform= new Date(strfecha.substr(6,4),strfecha.substr(3,2)-1,strfecha.substr(0,2));
    if (valiFechaini(finiform))
		var mesini=finiform.getMonth() +1 ;
    else
	   return false;
}
if (document.frmregistro.ffin.value=="")
{
    alert("Debe Ingresar la Fecha de Finalización");
    document.frmregistro.ffin.focus();
    return false;
}
else
{ 
    var strfecha=document.frmregistro.ffin.value; 
	var ffinform= new Date(strfecha.substr(6,4),strfecha.substr(3,2)-1,strfecha.substr(0,2));
	var mesfin=ffinform.getMonth() +1;
}
if (!valiFechas(finiform,ffinform))
{
     alert("La fecha de finalización debe ser superior o igual a la fecha de inicio");
	 return false;
}
if (document.frmregistro.fin.value==0)
{
    alert("Debe Ingresar el Destino");
    document.frmregistro.fin.focus();
    return false;
}
else
{
	for (nmes=mesini;nmes<=mesfin;nmes++ )
	{//veo que no este cerrada
	    var micerrado=eval("cerradas" + nmes);
		var numcerradas=micerrado.length;
		var encontrado=false;
	    i=1;
		while (i<=numcerradas)
		{
	      if (micerrado[i]==document.frmregistro.fin.value)
	      {
			alert("La picada se encuentra cerrada en el mes de " + quemes[nmes])
			return false;
	      }  
		  i=i+1;
		}
	}
}
if (document.frmregistro.conguia.value=="Si")
{
  if (document.frmregistro.nombreguia.value==""){
    alert("Debe Ingresar el Nombre del Guía");
    document.frmregistro.nombreguia.focus();
    return false;
  }
}
else
{// veo si es necesario guia
    if (Is_conGuia(mesini,mesfin)) 
    {
	    alert("La picada requiere de un Guia Habilitado");
		return false;      
    }
	//si no es obligatorio guia, compro nivel de picada con experinecia
    if (document.frmregistro.valexp.value==0)
	{
	    alert("Debe ingresar su nivel de experiencia");
		return false;      
	}
    if (Max_nivelinPer(mesini,mesfin)>document.frmregistro.valexp.value)
	{
		    alert("Por su nivel de experiencia no puede realizar esta picada entre las fechas ingresadas sin un Guia Habilitado");
			return false;      
	}
}
if (document.frmregistro.avisar.value==""){
    alert("Debe Ingresar a quién Avisar en caso de accidente");
    document.frmregistro.avisar.focus();
    return false;
}
if (document.frmregistro.avisartel.value==""){
    alert("Debe Ingresar el Número de Teléfono");
    document.frmregistro.avisartel.focus();
    return false;
}
//valido acompañantes
if (document.frmregistro.mayores.value=="")
	    mayores=0;
else
	   mayores=eval(document.frmregistro.mayores.value);
var cuantos=menores + mayores;

for(i=1;i<=cuantos;i++)
{
	if (document.getElementById("nombre" + i).value==""){
		alert ("Debe ingresar todos los nombres de los acompañantes!");
		return false;
	}
}
}
//--------------------------------

//------------------------------------

