function Validar(tipo,valor,mensaje) {
  if (tipo == "NULO") {
    if (CampoNulo(valor,mensaje)) {
      alert(mensaje);
      return(1);
    }
    return(0);
  }
  else if (tipo == "ESPACIOS") {
    if (soloEspacios(valor)) {
      alert(mensaje);
      return(1);
    }
    return(0);
  }
  else if (tipo == "ENTERO_POSITIVO") {
    if (!(esEnteroPositivo(valor))) {
      alert(mensaje);
      return(1);
    }
    return(0);
  }
 else if (tipo == "REAL") {
    if (!(esReal(valor))) {
      alert(mensaje);
      return(1);
    }
    return(0);
  }
 else if (tipo == "FECHA") {
    if (!(ValidarFecha(valor))) {
      alert(mensaje);
      return(1);
    }
    return(0);
  }
 else if (tipo == "HORA") {
    if (!(ValidarHora(valor))) {
      alert(mensaje);
      return(1);
    }
    return(0);
  }
 else if (tipo == "EMAIL") {
    if (!(ValidarEmail(valor))) {
      alert(mensaje);
      return(1);
    }
    return(0);
 }
}
function CampoNulo(valor,mensaje) {
  if (valor == "") {
    return(1);
  }
  return(0);
}
function esEntero(numero) {

  if (numero.substr(0,1) == "-") {
    numero = numero.substr(1);
  }

  re = new RegExp("[^0-9]");

  return(!(re.test(numero)));
}
function esEnteroPositivo(numero) {
  re = new RegExp("[^0-9]");

  if (!(re.test(numero))) {
    if (numero > 0) {
	  return(1);
	}
	return(0);
  }
  return(!(re.test(numero)));
}
function esReal(numero) {

  if (numero.substr(0,1) == "-") {
    numero = numero.substr(1);
  }

  re = new RegExp("^[0-9]+[.][0-9]+$"); 

  if (!(re.test(numero))) {
    return(0);
  }
  else {
    return(1);
  }
}
function esFecha(fecha) {
  re = new RegExp("^[0-9]?[0-9]?[/][0-9]?[0-9]?[/][0-9][0-9][0-9][0-9]$");
 
  if (!(re.test(fecha))) {
    return(0);
  }
  else {
    return(1);
  }   
  return(!(re.test(fecha)))
}
function valFecha(fecha) {
   if (fecha != "") {
	  if (esFecha(fecha)) {
         test = fecha.substr(2,1); 
         if (test != "/") {
            fecha = "0"+fecha; 
         }
         test = fecha.substr(5,1);
         if (test != "/") {
            fecha = fecha.substr(0,3)+"0"+fecha.substr(3,6);
         }
         dia = fecha.substr(0,2);
         mes = fecha.substr(3,2);
         anio = fecha.substr(6,4);   
         f = new Date(anio,mes-1,dia);   
         mesF = String(f.getMonth()+1);   
         anioF = String(f.getFullYear());
         if (mes.substr(0,1) == "0") {
            mes = mes.substr(1,1);
         }
         if (mes != mesF) {
            alert("Ha ingresado una fecha invalida");
   	        return(0);
         }
         else {
            return(fecha);
	     }
      } 
	  else {
            alert("Formato de fecha invalido");
   	        return(0);
	  } 
   }
}
function formatoMoneda(numero) {
  var signo;
  signo = '';

  if (numero.substr(0,1) == "-") {
    signo = '-';
    numero = numero.substr(1);
  }
  if (esReal(numero)) {
    re = new RegExp("^[0-9]+[.][0-9]{1}$"); 
    if (re.test(numero)) {
      numero = signo + numero + '0';
      return(numero);
    }
    re = new RegExp("^[0-9]+[.][0-9]{2}$"); 
    if (re.test(numero)) {
      numero = signo + numero;
      return(numero);
    }
    re = new RegExp("^[0-9]+[.][0-9]{3,}$"); 
    if (re.test(numero)) {
	  numero = numero.toString();
      numero = signo + numero.substr(0,numero.indexOf(".")+3);
	  numero = parseFloat(numero)+0.002;
	  numero = numero.toString();
      numero = signo + numero.substr(0,numero.indexOf(".")+3);
      return(numero);
    }
  }
  else if (esEntero(numero)) {
    num = signo + numero.toString() + '.00';
    return(num);
  }
}
function tieneComilla(cadena) {
  re = new RegExp("'");

  return(re.test(cadena));
}
function soloEspacios(cadena) {
  re = new RegExp("^ +$"); 

  return(re.test(cadena));
}
function ValidarHora(hora) {
  var x;
  var cont = 0;
  var vectorHora = new Array('','','');

  for (x=0;x<hora.length;x++) {
    if (hora.charAt(x) == ':') {
      cont++;
    }
    else {
      vectorHora[cont] = vectorHora[cont] + hora.charAt(x);
    }
  }

  if (vectorHora[0].length != 2 || vectorHora[1].length != 2) {
    return(false);
  }
  if (isNaN(vectorHora[0]) || isNaN(vectorHora[1])) {
    return(false);
  }
  if (vectorHora[0] < 1 || vectorHora[0] > 24) {
    return(false);
  }
  if (vectorHora[1] < 0 || vectorHora[1] > 59) {
    return(false);
  }
  return(true);
}
function ValidarFecha(fecha) {
  var vectorFecha = SplitDate(fecha);
  if (CheckDate(vectorFecha[0],vectorFecha[1],vectorFecha[2])) {
    return(true);
  }
  else {
    return(false);
  }
}
function SplitDate(fecha) {
  var x;
  var cont = 0;
  var vectorFecha = new Array('','','');

  for (x=0;x<fecha.length;x++) {
    if (fecha.charAt(x) == '/') {
      cont++;
    }
    else {
      vectorFecha[cont] = vectorFecha[cont] + fecha.charAt(x);
    }
  }
  return(vectorFecha);
}
function CheckDate(dia,mes,ano) {

  if (dia.length != 2 || mes.length != 2 || ano.length != 4) {
    return(false);
  }
  if (isNaN(dia) || isNaN(mes) || isNaN(ano)) {
    return(false);
  }
  if ((mes < 1) || (mes > 12)) {
    return(false)
  }
  if ((dia < 1) || (dia > 31)) {
    return(false);
  }
  if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia == 31) {
    return(false);
  }
  if (mes == 2) {
    var isleap=(ano%4 == 0 && (ano%100 != 0 || ano%400 == 0));
    if ((dia > 29) || (dia == 29 && !isleap)) {
      return(false);
    }
  }
  return(true);
}
function CompareDate(fecha1,fecha2) {

  vectorFecha1 = SplitDate(fecha1,'/');
  vectorFecha2 = SplitDate(fecha2,'/');

  if (CheckDate(vectorFecha1[0],vectorFecha1[1],vectorFecha1[2]) && CheckDate(vectorFecha2[0],vectorFecha2[1],vectorFecha2[2])) {

    if (vectorFecha1[2] != vectorFecha2[2]) {
      if (vectorFecha1[2] < vectorFecha2[2]) {
        return(-1);
      }
      else {
        return(1);
      }
    }
    else if (vectorFecha1[1] != vectorFecha2[1]) {
      if (vectorFecha1[1] < vectorFecha2[1]) {
        return(-1);
      }
      else {
        return(1);
      }
    }
    else if (vectorFecha1[0] != vectorFecha2[0]) {
      if (vectorFecha1[0] < vectorFecha2[0]) {
        return(-1);
      }
      else {
        return(1);
      }
    }
    else {
      return(0);
    }
  }
}
function ValidarEmail(correo) {
  re = new RegExp("[\w+]@[\w.]+"); 

  return(re.test(correo));
}