 /*<% 
 '*********************************************************
 '  Copyright 2002 Milenio.com          
 '  Nombre: val_tipo_dato.asp 
 '  Autor: Ulises Mújica Santiago
 '  Fecha Creación: 03/julio/2002
 '  Descripción: Página de funciones de validación de contenido para javascript 
 '---------------------------------------------------------------------------------
 'Modificado por: Ulises Mújica  	Fecha de Mod. 19/sep/2003		Ver. 1.1
 'Cambios realizados. 		Creacion de func val_input_text_long
 '*********************************************************

 '*********************************************************
 ' Proposito: Valida el tipo de dato a numerico regresando el dato validado o un default dado por parametro en caso de falla.
 ' Entrada: VarDato - Dato a validar
 '          IntDefault - Valor Default si la verificacion falla   
 ' Retorno: Valor numerico validado o default  
 '*********************************************************
 %>*/
 function caracteres(field,mode,carcteres){
   /* modo -1 - Sin validación
   	  modo 0 - personalizado
      modo 1 - numerico enteros
	  modo 2 - numerico general
	  modo 3 - letras sin espacios
	  modo 4 - alfanumerico, espacio , enteros
	  modo 5 - correo letras, _ , @, enteros
	  modo 6 - path
	  modo 7 - direcciones url
	  modo 8 - Texto general
	  modo 9 - numeros, letras
 	  modo null - nada  */
 var valid, letras, invalido='';
 letras =  'abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZáéíóúÁÉÍÓÚüÜ'
 numeros = '1234567890'
 if (mode == -1) return false;
 if (mode == 0) valid = '';
 if (mode == 1) valid = numeros ;
 if (mode == 2) valid = numeros + '.-';
 if (mode == 3) valid = letras;
 if (mode == 4) valid = letras + numeros + ' -.!"·$%&/(·#~)=?¿*+~';
 if (mode == 5) valid = letras + numeros + '@_.-';
 if (mode == 6) valid = letras + numeros + '_.()\/\\: -';
 if (mode == 7) valid = letras + numeros + '_.()\/\\:-&?#=';
 if (mode == 8) valid = letras + numeros + '_.()\'\/\\:-&¿?!¡%$\"°+*][;@<>#=,\r\n ';
 if (mode == 9) valid = letras + numeros;
 if (mode == 10) valid = letras + numeros + 'äöüßßÄÖÜ' + '_.()\'\/\\:-&¿?!¡%$\"°+*][;@<>#=,\r\n ';
 if (mode == null) return false;
 valid = valid + carcteres;
 var error = 0;
 var temp;
 for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") {
		error = 1;
		invalido = invalido + field.value.substring(i, i+1) + ' ';
	}		
 }
 if (mode == 5 & field.value.length != 0){
	if (field.value.indexOf('@') == -1 || field.value.indexOf('.') == -1) error = 5;
 }	 
 if (error != 0) {
	switch (error){
	case 5:
		alert("Invalid e-mail");
		break; 
	default:
		alert("Invalid Characters :\n\n ( " + invalido + " )");
		break; 
	}
     field.focus();
     field.select();
     return true; 
 }
 return false;
 } 

 /*<%
 '*********************************************************
 ' Proposito: Valida la fecha.
 ' Entrada: objmes - objeto del mes
 '          objdia - objeto dia
 '          objano - objeto año
 '          int_mode - 0 datetime
 ' 			           1 smalldatetime   
 ' Retorno: true - fecha no valida
 '          false - fecha valida   
 '*********************************************************
 %>*/ 
 function val_fecha(objmes,objdia,objano,int_mode){
    var objfecha,obj_fechali,obj_fechals
    objfecha = new Date(objano.value,objmes.value-1,objdia.value); 
	if (objfecha.getDate() != objdia.value ){
      alert("FECHA NO VALIDA, FAVOR DE CORREGIR EL DIA");
	  objdia.focus();
	  objdia.select();
	  return true;}
	if (objfecha.getMonth() != (objmes.value-1)){
      alert("FECHA NO VALIDA, FAVOR DE CORREGIR EL MES");
	  objmes.focus();
	  objmes.select();
	  return true;}
	if (objfecha.getFullYear() != objano.value){
      alert("FECHA NO VALIDA, FAVOR DE CORREGIR EL AÑO");
	  objano.focus();
	  objano.select();
	  return true;}	  	  
	if (int_mode == 1) {
	    obj_fechali = new Date(1753,0,1);
		obj_fechals = new Date(9999,11,31);
	}	
    if (int_mode == 2){
	    obj_fechali = new Date(1900,0,1);
		obj_fechals = new Date(2079,5,6);  
	}
	if (objfecha < obj_fechali || objfecha > obj_fechals){
	    alert("La fecha debe de ser mayor a "+obj_fechali+" y menor a:" + obj_fechals );
		objano.focus();
		objano.select();
		return true;
	}	
	return false;
 }	   

  /*<%
 '*********************************************************
 ' Proposito: Valida la fecha.
 ' Entrada: str_fecha
 '          int_mode - 0 datetime
 ' 			           1 smalldatetime   
 ' Retorno: true - fecha no valida
 '          false - fecha valida   
 '*********************************************************
 %>*/ 
 function val_input_fecha(obj_fecha,int_mode,str_mensaje){
    var obj_fechali,obj_fechals
    if (obj_fecha.value == ''){
	    alert(str_mensaje);
		obj_fecha.focus();
		obj_fecha.select();
		return true;
	}
		objfecha = new Date(obj_fecha.value); 
	if (int_mode == 1) {
	    obj_fechali = new Date(1753,0,1);
		obj_fechals = new Date(9999,11,31);
	}	
    if (int_mode == 2){
	    obj_fechali = new Date(1900,0,1);
		obj_fechals = new Date(2079,5,6);  
	}
	if (objfecha < obj_fechali || objfecha > obj_fechals){
	    alert("La fecha debe de ser mayor a "+obj_fechali+" y menor a:" + obj_fechals );
		obj_fecha.focus();
		obj_fecha.select();
		return true;
	}	
	return false;
 }	 
 
 /*<%
 '*********************************************************
 ' Proposito: Valida el contenido de un objeto input txt
 ' Entrada: obj_campo - Objeto input text
 '          int_modo - mode de chequeo de caracteres 
 '			str_caracteres - caracteres extras
 '			str_mensaje - mensaje de error
 ' Retorno: true - no valido
 '          false - valido   
 '*********************************************************
 %>*/  
 
 function val_input_text(obj_campo,int_modo,str_caracteres,str_mensaje){
     if (obj_campo.value != ''){
         if (caracteres(obj_campo,int_modo,str_caracteres))  return true;
	  }
	  else{
			    obj_campo.focus();
	            obj_campo.select();  
			    alert (str_mensaje); 
			    return true;
	  }
     return false;
 }
 /*<%
 '*********************************************************
 ' Proposito: Valida el contenido de un objeto input txt
 ' Entrada: obj_campo - Objeto input text
 '          int_modo - mode de chequeo de caracteres 
 '			str_caracteres - caracteres extras
 '			str_mensaje - mensaje de error
 ' Retorno: true - no valido
 '          false - valido   
 '*********************************************************
 %>*/  
 
 function val_input_text_long(obj_campo,int_set,int_longitud,int_modo,str_caracteres,str_mensaje){
	if (int_set <= 0){
		if (obj_campo.value != ''){
			if (caracteres(obj_campo,int_modo,str_caracteres))  return true;
		}
		else{
			obj_campo.focus();
			obj_campo.select();  
			alert (str_mensaje); 
			return true;
		}
	}	  
	if (int_set >= 0){
		if (int_longitud > 0 ){
			if (val_input_longitud(obj_campo,int_modo,int_longitud,str_mensaje + '\n Only '+int_longitud+' characters')) return true;
		}
	}
	return false;
 }

 /*<%
 '*********************************************************
 ' Proposito: Valida el contenido de un objeto input txt
 ' Entrada: obj_campo - Objeto input text
 '          int_modo - mode de chequeo de caracteres 
 '			str_caracteres - caracteres extras
 '			str_mensaje - mensaje de error
 ' Retorno: true - no valido
 '          false - valido   
 '*********************************************************
 %>*/  
 
  function val_extension(obj_campo,str_extension,str_mensaje){
		var ary_extension, int_item,str_ruta
		ary_extension = str_extension.split(",")
		str_ruta = obj_campo.value;
		str_ruta = str_ruta.toLowerCase();
		for(int_item in ary_extension) {
   			if (str_ruta.lastIndexOf(ary_extension[int_item])== (str_ruta.length-ary_extension[int_item].length)) return 0 ; 
		}
		obj_campo.focus();
		obj_campo.select();  
		alert (str_mensaje + ', extensiones válidas ' + str_extension ); 
		return true;
 }
 
 /*<%
 '*********************************************************
 ' Proposito: Valida el contenido de un objeto input num
 ' Entrada: obj_campo - Objeto input numerico
 '			str_mensaje - mensaje de error
 '			int_tipo - null,0,else =  integer
 '			           1 = float
 '					   2 = money
 '					   3 = tinyint
 '                     10 = integer positivo
 '                     11 = float positivo
 '                     12 = money positivo
 ' Retorno: true - no valido
 '          false - valido   
 '*********************************************************
 %>*/  
  function val_input_num(obj_campo,int_tipo,str_mensaje){
  	var var_numero
	 if (  (isFinite (obj_campo.value ) && obj_campo.value.length > 0) ){
         switch (int_tipo){
             case 1:  
			     if (obj_campo.value >= -1.79E308 && obj_campo.value <= 1.79E308 ) return false;
				 str_mensaje = str_mensaje + ' valores permtidos: -1.79E + 308 a 1.79E + 308'
				 break; 
             case 2:  
			     if (obj_campo.value >= -922337203685477.5808 && obj_campo.value <= 922337203685477.5808 )return false;
				 str_mensaje = str_mensaje + ' valores permtidos: -922,337,203,685,477.5808 a 922,337,203,685,477.5808'
				 break; 
             case 3:  
			     if (obj_campo.value >= 0 && obj_campo.value <= 255 ) return false;
				 str_mensaje = str_mensaje + ' valores permtidos: 0 a 255'
				 break; 
             case 10:  
			     if (obj_campo.value >= 0 && obj_campo.value <= 2147483647 ) return false;
				 str_mensaje = str_mensaje + ' valores permtidos: 0 a 2,147,483,647'
				 break; 
             case 11:  
			     if (obj_campo.value >= 0 && obj_campo.value <= 1.79E308 ) return false;
				 str_mensaje = str_mensaje + ' valores permtidos: 0 a 1.79E + 308'
				 break; 
             case 12:  
			     if (obj_campo.value >= 0 && obj_campo.value <= 922337203685477.5808 ) return false;
				 str_mensaje = str_mensaje + ' valores permtidos: 0 a 922,337,203,685,477.5808'
				 break; 
             default:
			     if (obj_campo.value >= -2147483648 && obj_campo.value <= 2147483647 ) return false;
				 str_mensaje = str_mensaje + ' valores permtidos: -2,147,483,648 a 2,147,483,647'
				 break; 
         }  
     }
     obj_campo.focus();
     obj_campo.select();  
     alert (str_mensaje); 
     return true;
 }

 /*<%
 '*********************************************************
 ' Proposito: Valida objeto select.
 ' Entrada: obj_select - objeto select
 '           int_index - indice considerado a error
 '			str_mensaje - mensaje de error
 ' Retorno: true - No valido
 '          false - Valido   
 '*********************************************************
 %>*/ 
 function val_select(obj_select,int_modo,int_index,str_mensaje){
	var int_estatus
 	int_estatus = 0;
	if (obj_select.selectedIndex == int_index && int_modo == 0 ) int_estatus = 1;
	if (obj_select.value == int_index && int_modo == 1 ) int_estatus = 1;
	if (obj_select.value < 0 && int_modo == 2 ) int_estatus = 1;
	if (int_estatus){
		alert (str_mensaje)
		obj_select.focus();
		return true;
	}	
 return false;
}
 
 /*<%
 '*********************************************************
 ' Proposito: Validar objeto radio.
 ' Entrada: obj_radio - objeto radio
 '			str_mensaje - mensaje de error
 ' Retorno: true - No valido
 '          false - Valido   
 '*********************************************************
 %>*/ 
 function val_radio(obj_radio,str_mensaje){
    for (i=0; i < obj_radio.length; i++){
	    if (obj_radio[i].checked){
		    return false
		}
    }
	alert(str_mensaje);
    return true;
 }

 /*<%
 '*********************************************************
 ' Proposito: Validar activacion  de objetos checkbox que tienen nombre en comun.
 ' Entrada: obj_forma - objeto forma
 '			str_nombre - nombre o parte delobjeto checkbox
 ' Retorno: true - No valido
 '          false - valido   
 '*********************************************************
 ' v 1.2 
 ' function val_checkbox_nom(obj_forma,str_nombre){
 '     int_cant_activo = 0; 
 '	 int_cant_total = 0;
 '     for(int_i=0;int_i< obj_forma.length;int_i++){
 '         obj_elemento = obj_forma.elements[int_i];
 '         if (obj_elemento.type=='checkbox' && Boolean(obj_elemento.name.indexOf(str_nombre) + 1) ){
 '		     int_cant_total++;
 '			 if (obj_elemento.checked) int_cant_activo++;
 '         }
 '     }
 '     if (int_cant_activo > 0){
 '	     if(int_cant_total == int_cant_activo )return 2;
 '		 return 1;
 '	 }
 '     return 0;
 ' } 
 %>*/  
 function val_checkbox_nom(obj_forma,str_nombre){
     for(int_i=0;int_i< obj_forma.length;int_i++){
         obj_elemento = obj_forma.elements[int_i];
         if (obj_elemento.type=='checkbox' && Boolean(obj_elemento.name.indexOf(str_nombre) + 1) && obj_elemento.checked) return false;	
     }
 return true;
 }
 
 /*<%
 '*********************************************************
 ' Proposito: Valida la longitud de un objeto txt
 ' Entrada: obj_campo - Objeto input text
 '          int_modo - mode de chequeo de caracteres 
 '			int_longitud - longitud de caracteres maxima
 ' Retorno: true - no valido
 '          false - valido   
 '*********************************************************
 %>*/  
 
  function val_input_longitud(obj_campo,int_modo,int_longitud,str_mensaje){
	 if (obj_campo.value.length > int_longitud){
		 obj_campo.focus();
		 if (str_mensaje == null) str_mensaje = '\n Excedió los '+int_longitud+' carácteres permitidos'
		 alert (str_mensaje); 
		 return true;
	 }
	 return false;
 }
 /*<%
 '*********************************************************
 ' Proposito: Extrae el nombre del archivo de una ruta
  ' Entrada: str_ruta - Ruta
 ' Retorno: str_archivo: nombre del archivo
 '*********************************************************
 %>*/  
 
  function val_input_archivo(obj_campo,int_modo,int_longitud,str_mensaje){
	 var str_archivo='',int_estado=0,str_ruta;
	 if (obj_campo.value.length > 0 ){
	 	str_ruta = obj_campo.value;
		str_ruta = str_ruta.replace(/\//gi,'\\')
		ary_ruta = str_ruta.split('\\');
		ary_ruta.reverse();
		str_archivo = ary_ruta[0]
	 }
	 if (str_archivo == ''){
	 	int_estado = 1;
	 }
	 if (str_archivo.length > int_longitud){
		str_mensaje = str_mensaje + '\nEl nombre del archivo "'+str_archivo+'" Excedió los '+int_longitud+' carácteres permitidos'
		int_estado = 1;
	 }
	 
	 if (int_estado){
		 obj_campo.focus();
		 alert (str_mensaje); 
		 return true;
	 }
	 return false;
 }
