/*  valida que no sea nulo */
function isitnull(data)
{
	var something=0;
	for (var i=0;i<data.length;i++)
	{
		if (data.substring(i, i+1) != " ")
		{
			something=1;
		}
	}
	if (something == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
} 
/* valida solo números */

function valida_telefono(num) 
{
   var chr = "";
   var numeros = "0123456789";    
   if (num.length > 0)
   		 {
		   for (i=0; i < num.length; i++)  
			   {
					chr=num.charAt(i);   	
					if (numeros.indexOf(chr) < 0 ) { alert("Solo números"); return (false); }       
			   } 
		   return (true); 	
		 }
	else { 
		return (false); 
	     }  
} 

function valida_long_telefono(num) 
{
 valor = num

	if (valor.length > 6) 	
	{
		//En caso contrario (Si era un número) devuelvo el valor
		return true
	}else {
	 
	return false
	}   
} 

function valida_monto(num) 
{
   var chr = "";
   var numeros = "0123456789";      
   if (num.length > 0) 
   		 {
		   for (i=0; i < num.length; i++)   
			   {
					chr=num.charAt(i);    	
					if (numeros.indexOf(chr) < 0 ) { alert("Solo números sin , . "); return (false); }       
			   } 
		   return (true); 	
		 }
	else { 
		return (false); 
	     }  
} 
/* Valida solo nuemros*/
function valida_numeros(num) 
{
   var chr = "";
   var numeros = "0123456789";
   if (num.length > 0)
   		 {
		   for (i=0; i < num.length; i++) 
			   {
					chr=num.charAt(i); 	
					if (numeros.indexOf(chr) < 0 ) { alert("Solo números"); return (false); }       
			   } 
		   return (true); 	
		 }
	else { 
		return (false); 
	     }  
} 
/* cuenta la cantidad de números n = mayor a; m = nenor a */
function cuenta_string(n,m,cantidad) 
{ 
	var cant = cantidad;  
    var cant_len1=cant.length;  
	if ((cant_len1 < n) || (cant_len1 > m) )  
	{ 
	return false;  
	}
	return true; 
}
/* valida radio butoons y check butoons */
function radios(box)
{
	var i = 0;
	//var f = 'document.datos.'+box ;   
	var f = box ; 
	var j = f+'.length' 
	var k = f +'[i]'
		 for (i = 0; i < eval(j) ; i ++)
		 {
		 if ( eval(k).checked == true )
			 {
			 	return true;
				break;			 
			 }
		 }
	return false; 			 	 
}
/* cuenta la cantidad de dias de diferencia entre la fecha actual */
function cuenta_dias(sTextBoxValue)  
{  
	var formato =  sTextBoxValue ; // + " 00:00" 
	var ifecha = cdate(formato, 2) // trasformar a ingles Formato 2  
	ifecha = ifecha + " 00:00"   
	//alert(ifecha);       
	var date1 = new Date();  
	var date2 = new Date(ifecha);  
	diff  = new Date();         
	diff.setTime(Math.abs(date1.getTime() - date2.getTime())); 
	timediff = diff.getTime();   
	days = Math.floor(timediff / (1000 * 60 * 60 * 24));    
	if ((days > 90) && (days < 365) )        
	{  
	//alert("La fecha es mayor a 90 y menor a 365 dias calendario ");    
	return true;      
	}   
	else          
	{      
	return false;
	}   
}  
/* valida fecha mayor a la actual */
function FechaMayorActual2(sTextBoxValue) 
	{
		var sfecha = new Date(); 
		var sfecha2 = new Date(sTextBoxValue.substring(6,10),((sTextBoxValue.substring(3,5)) - 1 ),sTextBoxValue.substring(0,2) ) ;
		if (sfecha > sfecha2)     
		{       
		return false;      
		}      
	return true;     
	} 

function cdate(fecha,formato) {   
   var sFecha 
   if (formato==1) { 
      sFecha= fecha.substring(0,2) + "/" + fecha.substring(3,5) + "/" + fecha.substring(6,10); // formato español	
   } else {  
      sFecha= fecha.substring(3,5) + "/" + fecha.substring(0,2) + "/" + fecha.substring(6,10); // fromato ingles 
   }	  
   return (sFecha);  	
} 

/* Valida fechas menores a la actual*/
function Valida_Fechas_Menores(sTextFechaValue) 
{   
	var formato =  sTextFechaValue ;   
	var ifecha = cdate(formato, 2) // trasformar a ingles Formato 2  
	ifecha = ifecha + " 00:00"      
	var date1 = new Date();  
	alert(date1) ;
	var date2 = new Date(ifecha);    
	if (date2 > date1)    
	{    
	return false;   
	}   
	else         
	{    
	return true;
	}    
}  
/* Valida si es mayor de edad*/

function es_Mayor(sTextBoxValue)   
{   
	var date1 = new Date();  
	var date2 = new Date(); 
	date2 =  new Date(sTextBoxValue.substring(6,10),((sTextBoxValue.substring(3,5)) - 1 ),sTextBoxValue.substring(0,2) ); //cdate(sTextBoxValue, 2); 
	diff  = new Date();               
	diff.setTime(Math.abs(date1.getTime() - date2.getTime()));    
	timediff = diff.getTime();       
	days = Math.floor(timediff / (1000 * 60 * 60 * 24));  
	if (days > 6570)    
	 { 
	// alert("es mayor de edad " + days) ; 
	return true; 
	 }          
	else    
	{  
	return false;  
	// alert("es menor de edad " + days); 
	}        	    
}     

/* valida fecha */ 
function Validar_Fecha(theElement) 
  /***************************************************************************************************
  Author: Danilo Valero / Sooner-Technology
  Descripcion:
  	Funcion de validacion de la fecha.

  Parametros: 
    theElement: Contenido del campo.
    theElementname: Nombre del elemento de la forma.

  ***************************************************************************************************/
  {
    if (!check_format(theElement.value)) { 
      alert("Debe llenar el campo Fecha con el formato DD/MM/AAAA");
      theElement.select();
      theElement.focus();
      return false;
      }	
      if (modify_format(theElement))
      if (check_format(theElement.value))
      if (!check_dmy()) { 
		  //mejora pedro m 21/11/2005
       // alert("Debe llenar el campo Fecha con el formato DD/MM/AAAA");
        theElement.select();
		    theElement.focus();	        
        return false;
      }
      return true;
  }

  /*************************************************************************************************** 
  * Función:		check_dmy()
  * Objetivo:	Determinar si una fecha dada es válida
  * Parámetros:	Entrada: Ninguno
  *           	Salida: True, si es una fecha válida.  False en caso contrario
  * Uso:			Interno	 
  ***************************************************************************************************/
  function check_dmy() {
    var dia = RegExp.$1;
  	var mes = RegExp.$2;
    var ano = RegExp.$3;
  	if (ano < 1800) {
      alert("El año debe ser mayor a 1800");
      return false;
    }
    if (mes < 1 || mes > 12) {
      alert("Mes inválido!");
      return false;
    }
    if (dia == 0) {
      alert("Día inválido!");
      return false;
    }
    if ((mes == 1 || mes == 3 || mes == 5 || mes == 7 || mes == 8 || mes == 10 || mes == 12) && (dia > 31)) {
      alert(dia + " no es un día válido para el mes " + mes);
      return false;
    }
    if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && (dia > 30)) {
      alert(dia + " no es un día válido para el mes " + mes);
      return false;
    }
    if (mes == 2) {
      //---- chequeo para año bisiesto -----
      if (dia > 28) {
        if (ano%4 != 0) {
          alert(dia + " no es un día válido  para el mes " + mes + " del año " + ano);
          return false; 
        }
      }
      if (dia > 29) {
        if (ano%4 == 0) {
          alert(dia + " no es un día válido para el mes " + mes + " del año " + ano);
          return false;
        }
      }
    }
    return true;
  }

  /*************************************************************************************************** 
  * Función:		check_format()
  * Objetivo:	Verifica si la fecha introducida por el usuario tiene el siguiente foemato dd/mm/yyyy
  * Parámetros:	Entrada: theElement, El campo de la página a validar
  *           	Salida: True, si la fecha cumple con el formato indicado.  False en caso contrario
  * Uso:			Interno	 
  ***************************************************************************************************/
  function check_format(theElement) {
	  var solo_fecha
    solo_fecha = theElement.substr(0,10);
    var date_regex = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    if (!date_regex.test(solo_fecha))	{
      var date_regex = /^(\d{1,2})\/(\d{1,2})\/(\d{2})$/;
      if (!date_regex.test(solo_fecha)) {
        return false;
      }
    }
    return true;
  }

  /*************************************************************************************************** 
  * Función:		modify_format()
  * Objetivo:	Modificar la fecha introducida por el usuario para que tenga el formato dd/mm/yyyy
  * Parámetros:	Entrada: theElement, El campo de la página al cual se le cambiará el formato
  *           	Salida: True 
  * Uso:			Interno	 
  ***************************************************************************************************/
  function modify_format(theElement) {
    var fecha=theElement.value;
    var date_regex = /^(\d{1})\/(\d{1,2})\/(\d{2})$/;
    if (date_regex.test(fecha)) {
      fecha="0" + fecha;
    }
    var date_regex1 = /^(\d{2})\/(\d{1})\/(\d{2})$/;
    if (date_regex1.test(fecha)) {
      fecha=fecha.substr(0,2) + "/0" +  fecha.substr(3,8)
    }
    var date_regex3 = /^(\d{2})\/(\d{2})\/(\d{2})$/;
    if (date_regex3.test(fecha)) {
      var ano=fecha.substr(6,8);
      if (ano> 50) {
        fecha=fecha.substr(0,5) +"/19" +ano;
      } else {
        fecha=fecha.substr(0,5) +"/20" +ano;
      }
    }
    theElement.value=fecha;
    return true;
  }
 /**/ 
function cedulaValida(campoCedulaValue){
	//intento convertir a entero. 
	//si era un entero no le afecta, si no lo era lo intenta convertir
	var valor
	valor = parseInt(campoCedulaValue)

	//Compruebo si es un valor numérico
	//if (isNaN(valor)) {
		//entonces (no es un numero) devuelvo el valor cadena vacia
		//return true
	if ((valor > 10000) && (valor < 90000000))	
	{
		//En caso contrario (Si era un número) devuelvo el valor
		return true
	}else {
	//alert("La cédula del debe mayor a 10.000 y menor a 90 millones"); 
	return false
	}
}
         
/* valida Formato  */ 

function cdate(fecha,formato) { 
   var sFecha
   if (formato==1) { 
      sFecha= fecha.substring(0,2) + "/" + fecha.substring(3,5) + "/" + fecha.substring(6,10); // formato español	
   } else {  
      sFecha= fecha.substring(3,5) + "/" + fecha.substring(0,2) + "/" + fecha.substring(6,10); // fromato ingles 
   }	  
   return (sFecha);  	
}  

/* valida e-mail */  
function validamail(emailvalue,stremail)
{
	{
    var input_str=emailvalue; 
    var input_len1=input_str.length;
	var iserror=0;

    input_len1=input_str.length;

		if (input_len1<=5) 
		{
			alert("Debe ingresar un mail válido!\n");
			iserror=1;
			eval(stremail).focus();
			return false;
		}

		var lastdot=-1;
		var lastat=-1;
		var numberat=0
		for (var j = 0; j < input_len1; j++) {
			 var ch2 = input_str.substring(j, j + 1);
			 if (((ch2 < "a") ||  (ch2 > "z")) && ((ch2 < "A") || (ch2 > "Z")) && ((ch2 < "0") ||  (ch2 > "9")) && (ch2 != "@" ) && (ch2 != ".") && (ch2 != "_") && (ch2 != "-")){
				 alert("Debe ingresar un mail válido!\nCaracteres válidos A-Z, a-z, 0-9, -, _, . y @");
				 iserror=1;
				 eval(stremail).focus();
				 return false;
			 }
			 else if (ch2==".") {
				 if ( j == lastdot + 1) {
					 alert("Debe ingresar un mail válido!\nNo puede tener dos puntos seguidos'..'");
					 iserror=1;
					 eval(stremail).focus();
					 return false;
				 }
				 lastdot=j;
			 }
			 else if (ch2=="@") {
				 lastat=j;
				 numberat=numberat+1
			 }
			 else if (ch2==" ") {
				 alert("Debe ingresar un mail válido!\nNo puede tener espacios");
				 iserror=1;
				 eval(stremail).focus();
				 return false;
			 }
		} 
		if (lastat == -1) {
			 alert("Debe ingresar un mail válido!\nUsted no tiene un '@'");
			 iserror=1;
			 eval(stremail).focus();
			 return false;
		}
		else if (lastat == 0) {
			 alert("Debe ingresar un mail válido!\nYoNo tiene un nombre antes de '@'");
			 iserror=1;
			 eval(stremail).focus();
			 return false;
		}
		else if (numberat > 1) {
			 alert("Debe ingresar un mail válido!\nSolo debe tener un '@'");
			 iserror=1;
			 eval(stremail).focus();
			 return false;
		}
		else if ((lastdot <= lastat) || (lastdot > input_len1 - 3 ) || (input_len1 - lastdot > 5)) {
			 alert("Debe ingresar un mail válido!\nDebe proveer un nombre dominio !\nUse .com, .org, .net, .cc, etc");
			 iserror=1;
			 eval(stremail).focus();
			 return false;
		}
		else if (lastdot == lastat + 1) {
			 alert("Debe ingresar un mail válido!\nPorfavor ingrese una organización válida!\nUse nombre@organizacion.domain");
			 iserror=1;
			 eval(stremail).focus();
			 return false;
		}
		if (iserror==1) {
			 eval(stremail).focus(); 
			 return false;
		}		
	  else 
		  return true;
  }

}
	//*****************************************************************************************************************
	// Modificado por: Pedro Cordero 09/11/2005 2:30 pm
	function ValidaFecNac(formField) {
		var blnValid = true;
		var nedad = calcular_edad(formField.value);
		if ( (nedad < 18) || (nedad > 200))   
			blnValid = false; 
			
		if (!blnValid){
			alert('Revise fecha de nacimiento, debe de ser mayor de edad.');
			formField.focus();
		}
		return blnValid; 		
	}	 
	//------------------------------------------------------------------------------------------------------------------
	//calcular la edad de una persona 
	//recibe la fecha como un string en formato español 
	//devuelve un entero con la edad. Devuelve false en caso de que la fecha sea incorrecta o mayor que el dia actual 
	//------------------------------------------------------------------------------------------------------------------	
	function calcular_edad(fecha){ 

	    //calculo la fecha de hoy
		var hoy
	    hoy=new Date() 
	    //alert(hoy) 
	
	    //calculo la fecha que recibo 
	    //La descompongo en un array 
	    var array_fecha = fecha.split("/") 
	    //si el array no tiene tres partes, la fecha es incorrecta 
	    if (array_fecha.length!=3) 
	       return false 
	
	    //compruebo que los ano, mes, dia son correctos 
	    var ano 
	    ano = parseInt(array_fecha[2]); 
	    if (isNaN(ano)) 
	       return false 
	
	    var mes 
	    mes = parseInt(array_fecha[1]); 
	    if (isNaN(mes)) 
	       return false 
	
	    var dia 
	    dia = parseInt(array_fecha[0]); 
	    if (isNaN(dia)) 
	       return false 
	
	
	    //si el año de la fecha que recibo solo tiene 2 cifras hay que cambiarlo a 4 
	    if (ano<=99) 
	       ano +=1900 
	
	    //resto los años de las dos fechas 
		var edad
	    edad=hoy.getYear()- ano - 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 < 0) //+ 1 porque los meses empiezan en 0 
	       return edad 
	    if (hoy.getMonth() + 1 - mes > 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 >= 0) 
	       return edad + 1 
	
	    return edad 
	}
	// Fin modificacion.
	//******************************************************************************************************************	
var WindowObjectReference; /* Declaring a global variable */ 
function ayudas(sLink)  
{ 
WindowObjectReference = window.open(sLink, "Ayudas", "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no,width=750,height=400");    
}
