/************************************************ 
DBSPTech.com 
-----------------------------
Validation Scripts
Copyright 2002-2003 Diego Bañuelos & associates. 
************************************************/


// variables globales
var Explorador = navigator.appName;







/******************************************************************************
  Function: ValidaIntegerNow
  Description: This function validates that the control's value be an integer value
******************************************************************************/

function ValidaIntegerNow(evento) {
	var Tecla;
	var valido=false;

	if (Explorador.indexOf("Explorer",0) != -1) {
		Tecla = evento.keyCode;
	} else {
		Tecla=evento.which;
	}
	if (((Tecla > 47) && (Tecla < 58)) || (Tecla==8) || (Tecla==13) ||
	    (Tecla==45) ) {
		valido=true;
	}
	if (Explorador.indexOf("Explorer",0) != -1) {
		if (!valido) {
			evento.keyCode = "";
		}
	}
	return valido;
}




/******************************************************************************
  funcion ValidaNatural
  Esta función valida que el texto capturado dentro de un control de texto sea
  un número Natural o cero.
******************************************************************************/

function ValidaNatural(evento) {
	var Tecla;
	var valido=false;

	if (Explorador.indexOf("Explorer",0) != -1) {
		Tecla = evento.keyCode;
	} else {
		Tecla=evento.which;
	}
	if (((Tecla > 47) && (Tecla < 58)) || (Tecla==8) || (Tecla==13)) {
		valido=true;
	}
	if (Explorador.indexOf("Explorer",0) != -1) {
		if (!valido) {
			evento.keyCode = "";
		}
	}
	return valido;
}







/******************************************************************************
  funcion ValidaFlotante
  Esta función valida que el texto capturado dentro de un control de texto sea
  un número real.
******************************************************************************/

function ValidaFlotante(evento, control) {
	var Tecla;
	var valido=false;

	if (Explorador.indexOf("Explorer",0) != -1) {
		Tecla = evento.keyCode;
	} else {
		Tecla=evento.which;
	}
	if (((Tecla > 47) && (Tecla < 58)) || (Tecla==8) || (Tecla==13) || 
	    (Tecla==45) || (Tecla==46)) {
    	if ( (Tecla==46) && (control.value.indexOf(".")!= -1 ) ) {
    	  valido = false 
    	 }
    	 else {
    	   valido = true;
    	 }		 
	}
	if (Explorador.indexOf("Explorer",0) != -1) {
		if (!valido) {
			evento.keyCode = "";
		}
	}
	return valido;
}







/******************************************************************************
  funcion ValidateDateNow
  This function a text box with date format. This functions also use ValidateDate
  before you submit a form.
******************************************************************************/

function ValidateDateNow (evento,control) {
	var Tecla;
	var valido=false;

	if (Explorador.indexOf("Explorer",0) != -1) {
		Tecla = evento.keyCode;
	} else {
		Tecla=evento.which;
	}

	if (((Tecla > 47) && (Tecla < 58)) || (Tecla==8) || (Tecla==13)) {
		valido=true;
	} else if (Tecla==47) {
		//Valida que no haya dos caracteres "/"
		var i=0;
		var posicion=0;
		var valor=control.value;
		var dia=0;
		var strDia="";
		while (valor.indexOf("/") != -1) {
			i++;
			posicion=valor.indexOf("/");
			valor=valor.substring(posicion+1, valor.length);
		}
		if (i==2) {
			alert("The date can't contain three times the character /");
			valido=false;
		} else if (i==1) {
			if (valor=="") {
				alert("An invalid month");
				strDia=control.value;
				strDia=strDia.substring(0, posicion);
				control.value="";
				valido=false;
			} else {
				mes = parseFloat(valor);
				strDia=control.value;
				strDia=strDia.substring(0, posicion);
				dia = parseFloat(strDia);
				if (mes > 0 && mes < 13) {
					//Ahora valida el mes contra el dia
					if ( mes==4 || mes==6 || mes==9 || mes==11) {
						if ( dia > 30 ) {						
							alert("This month doesn't have "+ dia + " days");
							//control.value="";
							valido=false;
						} else {
							valido=true;
						}
					} else if (mes==2) {
						if (dia > 29) {
								alert("This month doesn't have "+ dia +" days");
								//control.value="";
								valido=false;
							} else {
								valido=true;
							}
					} else {
						valido=true;
					}
				} else {
					alert("Invalid month");
					//control.value="";
					valido=false;
				}
			}
		} else if (i==0) {
			//Este es el primer caracter "/"
			if (valor=="") {
				alert("Invalid day");
				//control.value="";
				valido=false;
			} else {
				//valida el dia
				dia=parseFloat(valor);
				if ((dia==0) || (dia > 31)) {
					alert("Invalid day");
					//control.value="";
					valido=false;
				} else {
					valido=true;
				}
			}
		}
	}
	if (Explorador.indexOf("Explorer",0) != -1) {
		if (!valido) {
			evento.keyCode = "";
		}
	}
	return valido;
}






/******************************************************************************
  funcion URLHint
  Esta función muestra un hint en la barra de estatus del explorador. 
******************************************************************************/


 function URLHint (Mensaje){
   window.defaultStatus = '';
   window.status = Mensaje;
 }



/******************************************************************************
  funcion SelectControl
  This function selects a control of a form.
  
  Usage: javascript:SelectControl (doucument.form.controlname); 
******************************************************************************/
 
 function SelectControl (Control) {
   Control.focus();
   Control.select(); 
 }

 
 
 
 
/******************************************************************************
  Function: CheckOutCB
  Description: This function check out that at least a radio button be selected in TheForm.
  Esta función revisa que al menos un radio button o check box este 
  seleccionado dentro de una forma.  
******************************************************************************/
 
 
 function CheckOutCB(Forma,Mensaje)  {    
    var ElIdx = "";	
    for (var i = 0; i <Forma.elements.length; i++) {
      if (Forma.elements[i].checked == true)  {  
        ElIdx = Forma.elements[i].value;
      }    

    }    
    if (ElIdx == "")  {
      alert (Mensaje);
    }
    return ElIdx;
  }





/******************************************************************************
  funcion ValidateDateControls
  This function receives a control's array. The controls must have date format values.
******************************************************************************/


  function ValidateDateControls (Forma,Arreglo) {
     for (var i = 0; i < Forma.elements.length; i++) {  	
       if (InsideArray(Forma.elements[i].name,Arreglo)) {
            	if (!IsDate(Forma.elements[i].value)) {
                    alert ("Please typed a date wih the next format (DD/MM/YYYY)!");
                    Forma.elements[i].focus()
                    Forma.elements[i].select()	
                    return false           
                }         
       }         
     }
    return true;
  }
  

/******************************************************************************
  funcion ValidateNotNullControls
  This function validate that the controls declared into the array have a value. The can't be null.  
******************************************************************************/
  
  
  function ValidateNotNullControls(Forma,Arreglo) {	
     for (var i = 0; i < Forma.elements.length; i++) {  	
       if (InsideArray(Forma.elements[i].name,Arreglo)) {
            	if (EsVacio(Forma.elements[i].value)) {
                    alert ("This is an obligatory field!");
                    Forma.elements[i].focus()
                    Forma.elements[i].select()	
                    return false           
                }         
       }         
     }
    return true;
  }

  


/******************************************************************************
  funcion Mayusculas
  Esta función convierte el texto de un control a mayusculas
******************************************************************************/

function Mayusculas (Control) {
  Control.value=Control.value.toUpperCase();

}





/******************************************************************************
  funcion Minusculas
  Esta función convierte el texto de un control a mayusculas
******************************************************************************/

function Minusculas (Control) {
  Control.value=Control.value.toLowerCase();

}




/******************************************************************************
  Funciones de soporte para las funciones principales.
******************************************************************************/



  function InsideArray(Elemento, Arreglo) {
    for (var i = 0; i < Arreglo.length; i++) {  	
      if (Arreglo[i]==Elemento) {        
          return true
      }         
    }
    return false  
  }



  
  function EsVacio(Cadena) {
    if (Cadena == "" || Cadena == null) {
      return true
    }
  
   for (var i = 0; i < Cadena.length; i++) {
      var Caracter = Cadena.substring(i, i + 1)
      if (Caracter!=" " && Caracter!="\t" && Caracter!="\n") {
        return false
      }
   }
  
    return true
  }


  
  
   function IsDate(Fecha) {
   
     var Dia= SubcadenaDelim(Fecha,"/",0,1)
     var Mes= SubcadenaDelim(Fecha,"/",1,2)
     var Anio= SubcadenaDelim(Fecha,"/",2,3)


     if (EsVacio(Fecha)) {
          return true		
     }

   
     if ( (!ValidateDay(Dia)) || (!ValidateMonth(Mes)) || (!ValidateYear(Anio)) ) { 	
          return false;
     }     
     return(EsFechaCalendarioGregoriano(Dia,Mes,Anio,Fecha) ) 
   }
   
   
   
   
   
   function EsNumero(Cadena) {
   
     if (Cadena == "" || Cadena == null) {
       return true
     }
   
     for (var i = 0; i < Cadena.length; i++) {
       var Caracter = Cadena.substring(i, i + 1)
       if (Caracter < "0" || Caracter > "9") {
         return false
       }
     }
     return (!(Cadena.length==0))
   }


   
   function EnRango(Cadena, Min, Max) {
     var num = parseInt(Cadena)
     if (num < Min || num > Max) {
      return false
     }
     return true
   }
   
   
   function EliminaCeros(Cadena) {
     var Resultado = Cadena
     while (Resultado.substring(0,1) == "0") {
      Resultado = Resultado.substring(1,Resultado.length)
     }
     return Resultado
   }
   
   
   function ValidaCampoNumerico(Campo,Min,Max) {
     var Entrada = EliminaCeros(Campo)


     if ((!EsNumero(Entrada)) || (!EnRango(Entrada,Min,Max)) || EsVacio(Entrada) ) {
         return false
     }
   
     return true
   }
     
     
	 
   function ValidateDay(Campo) {
     return (ValidaCampoNumerico(Campo,1,31))
   }
   
   function ValidateMonth(Campo) {
     return (ValidaCampoNumerico(Campo,1,12))
   }
   
   function ValidateYear(Campo) {
     return (ValidaCampoNumerico(Campo,1900,9999))
   }
   
   
   
   
    function FechaCalendarioGregoriano(Dia,Mes,Anio) {
        var DiaMaximo=0;
        var MesAux=parseInt(Mes)
    
        if (Mes==1 || Mes==3 || Mes==5 || Mes==7 || Mes==8 || Mes==10 || Mes==12) {
           DiaMaximo=31
        }
        else {
           if (Mes==2) {
               if ((Anio % 4)==0 && Anio!=1900) {
                    DiaMaximo=29
               }
               else {
                    DiaMaximo=28
               }
           }
           else {
               DiaMaximo=30
           }
        }
    
        return(parseInt(Dia)<=DiaMaximo)
    }
   
   
    
    
    function EsFechaCalendarioGregoriano(Dia,Mes,Anio,Control) {
        var Mensaje="The date " +Dia+"/"+Mes+"/"+Anio+ " doesn't exists."
    
        if(FechaCalendarioGregoriano(Dia,Mes,Anio)) {
            return(true)    
        }
        else {
             alert(Mensaje)
             return false
       }
    
    }   
	
	
	
    function SubcadenaDelim(Cadena,Delimitador,Inicio,Fin) {
      var AparDelim=0
      var Subcadena=""
      for (var i = 0; i < Cadena.length; i++) {
        var Caracter = Cadena.substring(i, i + 1)
    
        if (Caracter==Delimitador) {
            AparDelim++
        }
     
        if ((AparDelim>=Inicio) && (AparDelim<Fin) ) {
            if (!(Caracter==Delimitador)) {
               Subcadena= Subcadena+Caracter
            }
        } 
        
        if (AparDelim==Fin) {
            return(Subcadena)
        }          
      }	
      return (Subcadena)  	
    }


/******************************************************************************
  funcion MuestraHint
  Esta función muestra un hint flotante.
******************************************************************************/
  function MuestraHint(current,evento,msg,tipo)
  {
    if (document.all){ 
      if(tipo==1){
         document.all.Hint.innerHTML = '<marquee>'+msg+'</marquee>';   
      }else{
         document.all.Hint.innerHTML = msg;
      }
      if(event.clientX+document.body.scrollLeft<500){
         document.all.Hint.style.pixelLeft=event.clientX+document.body.scrollLeft+10
      }else{
         document.all.Hint.style.pixelLeft=event.clientX+document.body.scrollLeft-150
      }
      document.all.Hint.style.pixelTop=event.clientY+document.body.scrollTop+10
      document.all.Hint.style.visibility="visible"
    }else if (document.layers){
      document.Hint.document.nstip.document.write('<b>'+msg+'</b>')
      document.Hint.document.nstip.document.close()
      document.Hint.document.nstip.left=0
      currentscroll=setInterval("scrolltip()",100)
      document.Hint.left=evento.pageX+10
      document.Hint.top=evento.pageY+10
      document.Hint.visibility="show"
    }
   
  }
  function EscondeHint()
  {
    if(document.all){
      Hint.style.visibility="hidden";
    }else if(document.layers){
      clearInterval(currentscroll)
      document.Hint.visibility="hidden"
    }
  }


/******************************************************************************
  funcion EsLetra
  Devuelve true si el caracter recibido es una letra.
******************************************************************************/

function EsLetra(letra){
   if(((letra>='A') && (letra<='Z')) ||((letra>='a')&&(letra<='z'))){
      return true;
   }else{
      return false;
   }
}
/******************************************************************************
  funcion EsNumero
  Devuelve true si el caracter recibido es un número.
******************************************************************************/

function EsNumero(numero){
   if((numero>='0') && (numero<='9')){
      return true;
   }else{
      return false;
   }
}


/******************************************************************************
  FUNCIONES PROMEP
******************************************************************************/

   function FechaEnRango(Control,FInferior,FSuperior,IntervaloCerrado) {   
     var FControl= QuitaCerosIzqFecha(Control.value);
     FSuperior = QuitaCerosIzqFecha(FSuperior);
     FInferior = QuitaCerosIzqFecha(FInferior);
     
     if (IntervaloCerrado) {
       return ( (ComparaFechas(FControl, FInferior)==0 || ComparaFechas(FControl, FInferior)==1) &&
            (ComparaFechas(FControl, FSuperior)==0 || ComparaFechas(FControl, FSuperior)==2)     )
     }
     else {
       return(  ComparaFechas(FControl, FInferior)==1 && 
                ( (ComparaFechas(FControl, FSuperior)==0) || (ComparaFechas(FControl, FSuperior)==2) )
                
             );
     }

   }

   function QuitaCerosIzqFecha(Fecha) {
     var Dia= new Number(SubcadenaDelim(Fecha,"/",0,1));
     var Mes= new Number(SubcadenaDelim(Fecha,"/",1,2));
     var Anio= new Number(SubcadenaDelim(Fecha,"/",2,3));
     return(Dia+"/"+Mes+"/"+Anio);
   }


   function DiffAnios(Fecha1, Fecha2) {
     var Anio1= new Number(SubcadenaDelim(Fecha1,"/",2,3));
     var Anio2= new Number(SubcadenaDelim(Fecha2,"/",2,3));
     
     return(Anio2-Anio1);  
   }

   /*
      0 = Fechas iguales
      1 = La fecha1 es mayor que la fecha2
      2 = La fecha1 es menor que la fecha2
   */
   function ComparaFechas(Fecha1, Fecha2) {
     var Dia1= new Number (SubcadenaDelim(Fecha1,"/",0,1));
     var Mes1= new Number (SubcadenaDelim(Fecha1,"/",1,2));
     var Anio1= new Number(SubcadenaDelim(Fecha1,"/",2,3));

     var Dia2= new Number(SubcadenaDelim(Fecha2,"/",0,1));
     var Mes2= new Number(SubcadenaDelim(Fecha2,"/",1,2));
     var Anio2= new Number(SubcadenaDelim(Fecha2,"/",2,3));


     if ( (Dia1+ 1 == Dia2+ 1) && (Mes1+ 1== Mes2 + 1) && (Anio1+ 1== Anio2 + 1)) {
       return(0);

     }

     if (Anio1>Anio2) {
       return(1);
     }else if (Anio1+1<Anio2+1){
       return(2);       
     } else if (Mes1+1>Mes2+1){ 
	   return(1)
	 }else if (Mes1+1<Mes2+1){ 
	   return(2)
	 }else if (Dia1+1>Dia2+1){ 
	   return(1)
	 }else if (Dia1+1<Dia2+1) {
	   return(2)
	 }
   }


   function ValidaControlesAnio(Forma,Arreglo,max) {
     for (var i = 0; i < Forma.elements.length; i++) {  	
       if (InsideArray(Forma.elements[i].name,Arreglo)) {
            	if (!EnRango(Forma.elements[i].value,'1900',max)) {
                    alert ("Por favor ingrese un año válido!");
                    SelectControl(Forma.elements[i]);
                    return (false);
                }         
       }         
     }
     return true;
   }


   function AnioEnRango(AControl,AInferior,ASuperior,IntervaloCerrado) {        
     var AControl1= new Number (AControl);   
     var AInferior1= new Number (AInferior);
     var ASuperior1= new Number (ASuperior);      
     if (IntervaloCerrado) {
       return (AControl1+1>=AInferior1+1 && AControl1+1<=ASuperior1+1);
     }
     else {
       return (AControl1+1>AInferior1+1 && AControl1+1<ASuperior1+1);
     }
   }


   function BrowserDetection() {
	if (Explorador.indexOf("Netscape",0) != -1) {
           alert(navigator.appVersion);
        }
   }

/******************************************************************************
  funcion MuestraHint
  Esta función muestra un hint flotante.
******************************************************************************/
  function MuestraHint(current,evento,msg,tipo)
  { 	
      document.getElementById("Hint").innerHTML = msg;
      if(tipo == 1){
	      if(evento.clientX+document.body.scrollLeft<500){
              document.getElementById("Hint").style.pixelLeft = evento.clientX+document.body.scrollLeft+10
          }else{
              document.getElementById("Hint").style.pixelLeft=evento.clientX+document.body.scrollLeft-150
          }
	  }else{
	      document.getElementById("Hint").style.pixelLeft = 1 
	  }  
      if(evento.clientY + document.body.scrollTop < 300)
      {
         document.getElementById("Hint").style.pixelTop=evento.clientY+document.body.scrollTop+10
      }else{
         document.getElementById("Hint").style.pixelTop=evento.clientY+document.body.scrollTop-100
      }
      document.getElementById("Hint").style.visibility="visible"
  }

  function EscondeHint()
  {
    document.getElementById("Hint").style.visibility="hidden";
  }


/******************************************************************************
  funcion ValidateEmailNow
  Esta función verifica que el correo introducido sea correcto.
******************************************************************************/

function ValidateEmailNow(control){
  var i=1
  var correo = control.value;
  var finalX = correo.length
  var valido = false
  if(finalX>0){
    if((EsLetra(correo.charAt(0)))){ 
       var token = correo.charAt(i)
       while((i<finalX)&&(token!='@')&&((EsLetra(token))||(EsNumero(token))||(token=='.')||(token=='_'))){
          i++;   
          token = correo.charAt(i);
       }
       if ((token == '@')&&(correo.charAt(i+1)!= '.')&&((i+1)<finalX)){
          i++;   
          token = correo.charAt(i)
          while((i<finalX)&&(token!='.')&&((EsLetra(token))||(EsNumero(token))||(token=='_'))){
              i++;   
              token = correo.charAt(i)
          }
          if((token == '.')&&(correo.charAt(i+1)!='.')&&((i+1)<finalX)){
            i++;   
            token = correo.charAt(i)
            while((i<finalX)&&(token!='.')&&((EsLetra(token))||(EsNumero(token)||(token=='_')))){
               i++;   
               token = correo.charAt(i)
            }
            if (i>=finalX){
               return true
            }else{
               if ((token == '.')&&((i+1)<finalX)){
                 i++;   
                 token = correo.charAt(i)
                 while((i<finalX)&&((EsLetra(token))||(EsNumero(token))||(token=='_'))){
                   i++;   
                   token = correo.charAt(i)
                 }
                 if(i>=finalX){
                    return true
                 }else{
                    //alert('Hay algo mal después del segundo punto')
                    //SelectControl(control);
                    //return false
                    return true
                 }
               }else{
                  alert('There is an unknown symbol after the first dot.')
                  SelectControl(control);
                  return false
               }
            }
          }else{
             alert('There is no dot or there is an invalid symbol.')
             SelectControl(control);
             return false
          }
       }else{
          alert('There is no \'@\', there is no domain, or there is an invalid symbol.')
          SelectControl(control);
          return false
       }
    }else{
       alert('An invalid e-mail start')
       SelectControl(control);
       return false;
    }
  }else{
     return true;
  }
}

/******************************************************************************
  funcion EsLetra
  Devuelve true si el caracter recibido es una letra.
******************************************************************************/

function EsLetra(letra){
   if(((letra>='A') && (letra<='Z')) ||((letra>='a')&&(letra<='z'))){
      return true;
   }else{
      return false;
   }
}
/******************************************************************************
  funcion EsNumero
  Devuelve true si el caracter recibido es un número.
******************************************************************************/

function EsNumero(numero){
   if((numero>='0') && (numero<='9')){
      return true;
   }else{
      return false;
   }
}


function GoTo(Direccion) {
   var ruta="page?doc="+Direccion;
   document.location.replace(ruta);  
}

function Catalogo(Direccion, forma) {
   forma.action="page?doc="+Direccion;
   forma.onsubmit="";
   forma.submit();  
}


function EnableDiv(Nombre, Enable) {
  var Elemento = document.getElementById(Nombre);
  
  if (Enable) {
    Elemento.style.display=="";
    alert("Si");
  } else {
    Elemento.style.display=="none";
    alert("No");
  }
}

/*********************************************************************************
 Funcion ValidateNotNullComboBoxs()
 Valida que los el combobox seleccionado no tenga valor = 0 
 ********************************************************************************/
  function ValidateNotNullComboBoxs(Forma,Arreglo) {	
     var Numero = new Number();
     for (var i = 0; i < Forma.elements.length; i++) {  	
       if (InsideArray(Forma.elements[i].name,Arreglo)) {
                Numero = new Number(Forma.elements[i].options[Forma.elements[i].selectedIndex].value);
                if (Numero==0) {        
                    alert ("Please select one of the options!");
                    Forma.elements[i].focus();
                    return false           
                }         
       }         
     }
    return true;
  }
 

 function GetComboBox(Forma, CBIndex) {
   var Counter;

   Counter = 0;

   for (var i = 0; i < Forma.elements.length; i++) {  
     if (Forma.elements[i].tagName=='SELECT') {
       if (Counter==CBIndex) {
         return(Forma.elements[i]); 
         break;
       }
       Counter = Counter +1;
     }
   }
 }

/*********************************************************************************
 Function: ValidateNotNullRadioButtons() 
 Description: Validate that the array elements contains not null values
 Creation date: 03/04/2003
 Author: Luis Ramírez
 ********************************************************************************/
  function ValidateNotNullRadioButtons(TheForm,TheArray) {	
     var aNumber = new Number();
     var NumChecked = 0;

     for (var i = 0; i < TheForm.elements.length; i++) {  	
       if (InsideArray(TheForm.elements[i].name,TheArray)) {
           if (!(ValidateNotNullRadioButton(TheForm,TheForm.elements[i].name)))
             return(false);
       }
     }
     return(true);
  }

  function ValidateNotNullRadioButton(TheForm,TheElement) {	
     var aNumber = new Number();
     var NumChecked = 0;

     for (var i = 0; i < TheForm.elements.length; i++) {  	
       if (TheForm.elements[i].name==TheElement) {
                if (TheForm.elements[i].checked) {
                  return(true);
                }
       }
     }

     alert ("Please select one of the options.");
     return(false);
  }

/*********************************************************************************
 Function: ValidateNotNullRadioButtons() 
 Description: returns the value of the selected radiobutton(TheElement). If the radibutton is not selected the function returns -1.
 Creation date: 03/04/2003
 Author: Luis Ramírez
 ********************************************************************************/

  function GetRadioButtonValue(TheForm, TheElement) {
     for (var i = 0; i < TheForm.elements.length; i++) {  	
       if (TheForm.elements[i].name==TheElement) {
                if (TheForm.elements[i].checked) {
                  return(TheForm.elements[i].value);
                }
       }
     }
     return(-1);
 }
