/*******INICIO********
Clase : Fecha 
**********************/
function Fecha(fecha) {
  // Inicializa valores
  this.valor		= fecha;
  // Referencias a los métodos
  this.lee_valor	= lee_valor;
  this.escribe_valor	= escribe_valor;
  this.lee_separador	= lee_separador;
  this.escribe_separador= escribe_separador;
  this.dia		= dia;
  this.mes		= mes;
  this.anio		= anio;
  this.ddmmaaaa		= ddmmaaaa;
  this.mmddaaaa		= mmddaaaa;
  this.aaaaddmm		= aaaaddmm;
  this.aaaammdd		= aaaammdd;
  this.ddaaaamm		= ddaaaamm;
  this.mmaaaadd		= mmaaaadd;
}
// Definición de los métodos
function escribe_valor(fecha) {
  this.valor = fecha;
}
function lee_valor() {
  return this.valor;
}
function escribe_separador(sep) {
  this.sep = sep;
}
function lee_separador() {
  return this.sep;
}
function dia() {
  fec_aux = new Date(this.lee_valor());
  dia = fec_aux.getDate();
  if (dia<10) dia = "0" + dia;
  return dia;
}
function mes() {
  fec_aux = new Date(this.lee_valor());
  mes =  fec_aux.getMonth()+1
  if (mes<10) mes = "0" + mes;
  return mes;
}
function anio() {
  fec_aux = new Date(this.lee_valor());
  return fec_aux.getYear()+1900;
}
function ddmmaaaa() {
  var sep = this.lee_separador();
  return this.dia() + sep + this.mes() + sep + this.anio();
}
function mmddaaaa() {
  var sep = this.lee_separador();
  return this.mes() + sep + this.dia() + sep + this.anio();
}
function aaaaddmm() {
  var sep = this.lee_separador();
  return this.anio() + sep + this.dia() + sep + this.mes();
}
function aaaammdd() {
  var sep = this.lee_separador();
  return this.anio() + sep + this.mes() + sep + this.dia();
}
function ddaaaamm() {
  var sep = this.lee_separador();
  return this.dia() + sep + this.anio() + sep + this.mes();
}
function mmaaaadd() {
  var sep = this.lee_separador();
  return this.mes() + sep + this.anio() + sep + this.dia();
}
/*******FIN*****************
Clase : Fecha 
****************************/

/*******INICIO**************
Clase : Validaciones
****************************/
function Validaciones (forma) {
  this.forma = eval(forma);
  this.valor = "";
  this.condicion = "";
  this.idiom = "Sp";
  this.msg_vacio=0;
  this.msg_telefono=1;
  this.msg_domicilio=2;
  this.msg_email=3;
  this.msg_fecha=4;
  this.msg_radio=5;
  this.msg_select=6;
  this.msg_archivo=7;

  this.msgSp 	= new Array();
  this.msgSp[0] = "Es un campo obligatorio";
  this.msgSp[1] = "Teléfono incorrecto";
  this.msgSp[2] = "Domicilio incorrecto";
  this.msgSp[3] = "Email incorrecto";
  this.msgSp[4] = "Fecha incorrecta";
  this.msgSp[5] = "seleccione una opción";
  this.msgSp[6] = "seleccione una opción";
  this.msgSp[7] = "extensión de archivo inválida. Solo acepta las siguientes extensiones: ";

  this.msgEn 	= new Array();
  this.msgEn[0] = "this is a required field";
  this.msgEn[1] = "Teléfono incorrecto";
  this.msgEn[2] = "Domicilio incorrecto";
  this.msgEn[3] = "invalid email account";
  this.msgEn[4] = "invalid date";
  this.msgEn[5] = "please choose one";
  this.msgEn[6] = "please choose one";
  this.msgEn[7] = "invalid file extension";
 
  this.nombres = new Array();

  this.EsAlfabetico 	= EsAlfabetico;
  this.EsNumerico 	= EsNumerico;
  this.EsEmail		= EsEmail;
  this.EsVacio		= EsVacio;
  this.EsTelefono	= EsTelefono;
  this.FechaCorrecta	= FechaCorrecta;
  this.ValidaVacios 	= ValidaVacios;
  this.ValidaGral	= ValidaGral;
  this.ValidaTelefonos	= ValidaTelefonos;
  this.ValidaEmails	= ValidaEmails;
  this.ValidaNumericos  = ValidaNumericos;
  this.ValidaFechas	= ValidaFechas;
  this.RadiosVacios	= RadiosVacios;
  this.Redondeo		= Redondeo;
  this.SelectVacios	= SelectVacios;
  this.ExtensionesOk	= ExtensionesOk;
  this.ComparaFechas	= ComparaFechas;
}
  function EsAlfabetico (valor) {
    // Regresa 0 si no es alfabético
    // Regresa 1 si es alfabético
    condicion  = /^[a-zA-Z]+$/; 
    this.valor = valor;
    return this.ValidaGral(); 
  }
  function EsNumerico (valor) {
    // Regresa 0 si no es numérico
    // Regresa 1 si es numérico
    this.condicion  	= /^[0-9\.-]+$/; 
    this.valor = valor;
    return this.ValidaGral(); 
  }
  function EsEmail (valor) {
    // Regresa 0 si es email válido
    // Regresa 1 si es email no válido
    this.condicion 	= /^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]+\.[A-Za-z0-9_\-\.]+$/;
    this.valor = valor;
    return this.ValidaGral(); 
  }
  function EsVacio (valor) {
    // [1->vacio | 0->no vacío]
    if (valor.length==0) return 0;
    this.condicion = /^\s+$/;
    this.valor = valor;
    regresa = this.ValidaGral();
    if (regresa) return 0;
    return 1;
  }
  function EsTelefono (valor) {
    this.condicion = /^[0-9\-\s]+$/;
    this.valor = valor;
    return this.ValidaGral(); 
  }
  function ValidaGral () {
    correcto = this.valor.match(this.condicion);
    if (correcto  == null) return 0;
    return 1; 
  }
  function FechaCorrecta (fecha) {
   fecha = fecha.toString();
   fecha = fecha.split ("/");
   if (fecha.length != 3) return false; // No est'a la fecha completa
   var dia, mes, anio;
   dia  = fecha[0];
   mes  = fecha[1];
   anio = fecha[2];

   if (fecha[2].length != 2) return false; // No est'a la fecha completa

   fecha_aux = mes + "/" + dia + "/" + anio;
   fecha_msg = dia + "/" + mes + "/" + anio;
   fecha_aux = new Date(fecha_aux);

   mes = mes;
   dia = dia;
   anio = anio;
   mes2 = parseInt(fecha_aux.getMonth())+1;
   dia2 = parseInt(fecha_aux.getDate());
   anio2 = parseInt(fecha_aux.getYear());
   
   //if (document.layers) anio2 += 1900; // IE regresa el anio correcto, a Netscape hay que sumarle 1900
   if  ( mes != mes2 || dia != dia2 ||  anio != anio2 ) {
	 return 0;
   }
   return 1;
  }
  function ValidaVacios (arreglo) {
	  for (m=0;m<arreglo.length;m++) {
	    obj = eval("this.forma." + arreglo[m]);
	      if (!this.EsVacio(obj.value)) {
 	      ArrAux = eval("this.msg"+this.idiom);
	      alert(this.nombres[m] + ":" + ArrAux[this.msg_vacio]);
	      obj.focus();
	      obj.select();
	      return 0;
	    }
	  }  
	  return 1;
  } 
  function ValidaTelefonos (arreglo) {
	  for (m=0;m<arreglo.length;m++) {
	    obj = eval("this.forma." + arreglo[m]);
	    if (!this.EsTelefono(obj.value)) {
	      alert(this.msg[this.msg_telefono]);
	      obj.focus();
	      obj.select();
	      return false;
	    }
	  }   
	  return 1;	  
  }
  function ValidaNumericos (arreglo) {
          for (m=0;m<arreglo.length;m++) {
            obj = eval("this.forma." + arreglo[m]);
            if (!this.EsNumerico(obj.value)) {
              alert(this.msg[this.msg_numerico]);
              obj.focus();
              obj.select();
              return false;
            }
          }
          return 1;
  }
  function ValidaEmails (arreglo) {
          for (m=0;m<arreglo.length;m++) {
            obj = eval("this.forma." + arreglo[m]);
            if (!this.EsEmail(obj.value)) {
	      ArrAux = eval("this.msg"+this.idiom);
              alert(this.nombres[m] + ":" + ArrAux[this.msg_email]);
              obj.focus();
              obj.select();
              return false;
            }
          }
          return 1;
  }
  function ValidaFechas (arreglo) {
          for (m=0;m<arreglo.length;m++) {
            obj = eval("this.forma." + arreglo[m]);
            if (!this.FechaCorrecta(obj.value)) {
	      ArrAux = eval("this.msg"+this.idiom);
              alert(this.nombres[m] + ":" + ArrAux[this.msg_fecha]);
              obj.focus();
              obj.select();
              return false;
            }
          }
          return 1;
  }
  function RadiosVacios (arreglo) {
    for (m=0;m<arreglo.length;m++) {
      obj = eval("this.forma." + arreglo[m]);
      vacio=1;
      for (r=0;r<obj.length;r++) {
        if (obj[r].checked) {vacio=0;break;}
      }
      if(vacio) {
	ArrAux = eval("this.msg"+this.idiom);
        alert(this.nombres[m] + ":" + ArrAux[this.msg_radio]);
	obj[0].focus();
	return false;
      } 
    }
    return true;
  }
  function SelectVacios (arreglo) {
    for (m=0;m<arreglo.length;m++) {
      obj = eval("this.forma." + arreglo[m]);
      ArrAux = eval("this.msg"+this.idiom);
      if (obj.selectedIndex == -1) {
	alert(this.nombres[m] + ":" + ArrAux[this.msg_select]);
	return false; 
      }
      else {  
	if (obj.options[obj.selectedIndex].value=="") {
	  alert(this.nombres[m] + ":" + ArrAux[this.msg_select]);
	  obj.focus();
	  return false; 
        }
      }
    }
    return true;
  }
  function ExtensionesOk (arreglo, ArrExt) { 
    for (m=0;m<arreglo.length;m++) {
      obj = eval("this.forma." + arreglo[m]);
	correcto = 1;
	if (obj.value=="") continue;
	for (c=0;c<ArrExt.length;c++) {
          if (obj.value.indexOf(ArrExt[c]) != -1) {correcto=0;break};
	}
        if(correcto) {
          ArrAux = eval("this.msg"+this.idiom);
          alert(this.nombres[m] + ":" + ArrAux[this.msg_archivo] + ArrExt);
          obj.focus();
          return false;
        }
    }
    return true;
  }
  function Redondeo(valor,decimales) {
   if (!decimales) decimales=2;
   return (Math.round(valor * Math.pow(10,decimales)) / Math.pow(10,decimales));
  }
  function ComparaFechas(fecha1, fecha2) {

   fecha = fecha1.toString();
   fecha = fecha1.split ("/");
   var dia, mes, anio;
   dia  = fecha[0];
   mes  = fecha[1];
   anio = fecha[2];

   fecha_aux = mes + "/" + dia + "/" + anio;
   fecha1 = new Date(fecha_aux);

   fecha = fecha2.toString();
   fecha = fecha2.split ("/");
   var dia, mes, anio;
   dia  = fecha[0];
   mes  = fecha[1];
   anio = fecha[2];

   fecha_aux = mes + "/" + dia + "/" + anio;
   fecha2 = new Date(fecha_aux);

    mes1 = parseInt(fecha1.getMonth())+1;
    dia1 = parseInt(fecha1.getDate());
    anio1 = parseInt(fecha1.getYear()) ;
    if (anio1 <= 10) anio1 += 2000; 
	    
   if (document.layers) anio1 += 1900; // IE regresa el anio correcto, a Netscape hay que sumarle 1900    
   
    comp1 = (anio1 * 10000) + (mes1 * 100) + dia1;
	mes2 = parseInt(fecha2.getMonth())+1;
    dia2 = parseInt(fecha2.getDate());
    anio2 = parseInt(fecha2.getYear()) ;
    if (anio2 <= 10) anio2 += 2000; 

   if (document.layers) anio2 += 1900; // IE regresa el anio correcto, a Netscape hay que sumarle 1900    
    comp2 = (anio2 * 10000) + (mes2 * 100) + dia2;
    if (comp1>comp2) return 1;
    if (comp2>comp1) return 2;
    if (comp1==comp2) return 0;

  }

function FormateaFecha(fecha) {
   fecha = fecha.toString();
   fecha = fecha.split ("/");
   if (fecha.length != 3) return false; // No est'a la fecha completa
   var dia, mes, anio;
   dia  = fecha[0];
   mes  = fecha[1];
   anio = fecha[2];

   fecha_aux = mes + "/" + dia + "/" + anio;
   fecha_msg = dia + "/" + mes + "/" + anio;
   fecha_aux = new Date(fecha_aux);
  
   return fecha_msg;
  }
 
/*******FIN*****************
Clase : Validaciones
****************************/



