
/*============================================================================*/
/* Nombre: IsNum
/* Descripción:                                                               */
/*============================================================================*/

function IsNum(theField)
{
    var val              = parseInt(theField.value);

    // It's not a number
    if (isNaN(val)) {
	theField.focus();
	theField.select();
        return false;
    }
    return true;

}   



/*============================================================================*/
/* Nombre: NuloStr                                                            */
/* Descripción:                                                               */
/*============================================================================*/

function NuloStr(theField)
{
    var isEmpty  = 1;
    // Whether the replace function (js1.2) is supported or not
    var isRegExp = (typeof(theField.value.replace) != 'undefined');

    if (!isRegExp) {
        isEmpty      = (theField.value == '') ? 1 : 0;
    } else {
        var space_re = new RegExp('\\s+');
        isEmpty      = (theField.value.replace(space_re, '') == '') ? 1 : 0;
    }
    if (isEmpty) {
	theField.focus();
	theField.select();
        return true;
    }

    return false;
} // end of the 'emptyFormElements()' function

/*============================================================================*/
/* Nombre: IsMail                                                             */
/* Descripción: Verifica el formtao del correo electrónico                    */
/*============================================================================*/

function IsMail(str){
    var ema = new String(str);
    var i=0,
	a=0,
	p=0,
	ctaPAroa=0,
	ctaPPto=0;
	for(i=0; i<ema.length ; i++)	{
		if (ema.charAt(i)=="@") {a++; ctaPAroa=i}
		if (ema.charAt(i)==".") {p++;	ctaPPto=i;}
		if (ema.charAt(i)=="'") return false;
		if (ema.charAt(i)=='"') return false;
		if (ema.charAt(i)==" ") return false;
	}
	if (a==0) return false;
	if (p==0) return false;
	if (a>1) return false;
	if ((a==1) && p>0 && ctaPPto<(ema.length-2) ) return true;
}

/*============================================================================*/
/* Nombre:                                                                    */
/* Descripción:                                                               */
/*============================================================================*/
function ValidaFecha(theField, minYear)
{
   var toks = theField.value.split('-');

   if (toks.length != 3)
   {
      theField.focus();
      theField.select();
      return false;
   }

   var dia = parseInt(toks[0],10);
   var mes = parseInt(toks[1],10);
   var year = parseInt(toks[2],10);


   if (isNaN(dia) || isNaN(mes) || isNaN(year))
   {
      theField.focus();
      theField.select();
      return false;
   }
   if (dia<1 || dia>31)
   {
      theField.focus();
      theField.select();
      return false;
   }
   if (mes<1 || mes>12)
   {
      theField.focus();
      theField.select();
      return false;
   }

   if (minYear < 0)
      return true;

   if (year < minYear)
   {
      theField.focus();
      theField.select();
      return false;
   }
  var mydate=new Date()
  var myyear=mydate.getYear()
  if (myyear < 1900)
     myyear+=1900-2000;
  if (myyear > 2000)
     myyear-=2000;
  if (year > myyear)
  {
      theField.focus();
      theField.select();
      return false;
  }
   return true;
}

/*============================================================================*/
/* Nombre:                                                                    */
/* Descripción:                                                               */
/*============================================================================*/
function opcionesSeleccionadas( theField )
{

   if (! theField) {
      return -1;
   }

   var num=0;
   if (theField.type == "checkbox") {
      if (theField.checked) {
         num = 1;
      }
   } else {
      if (typeof(theField) != 'object') {
         return 0;
      }
      var largo = theField.length;

      for (var i=0; i<largo; i++)
      {
         if (theField[i].checked)
            num++;
      }
   }
   return num;
}

function opcionSeleccionada( theField, num )
{

   if (! theField) {
      return -1;
   }

   if (theField.type == "checkbox") {
      return (theField.checked && (theField.value == num));
   } else {
      if (typeof(theField) != 'object') {
         return false;
      }
      var largo = theField.length;

      for (var i=0; i<largo; i++)
      {
         if (theField[i].checked && (theField[i].value == num))
            return true;
      }
   }
   return false;
}

/*============================================================================*/
/* Nombre:                                                                    */
/* Descripción:                                                               */
/*============================================================================*/
function RutVerificador(rut,digito)
{
   if (typeof(rut.value.replace) == 'undefined') {
      var valRut = rut.value;
   } else {
      var pto_re = new RegExp('\\.','g');
      var valRut = rut.value.replace(pto_re,'');

      rut.value = valRut;
   }

   var valDigito = digito.value;
   var factor = 2;
   var suma = 0;

   for( var i = valRut.length-1; i >= 0; i -= 1)
   {
      var ch = parseInt(valRut.substring(i, i + 1));
      if (isNaN(ch))
      {
         rut.select();
         rut.focus();
         return false;
      }
      suma = ch * factor + suma;
      factor = factor + 1;
      if( factor == 8)
      {
         factor = 2;
      }
   }
   suma = suma % 11;
   suma = 11 - suma;
   if( suma == 11 )
   {
      suma = 0;
   }
   if( suma == 10 && ( valDigito == "k" || valDigito == "K" ))
   {
      return true;
   }
   else if( suma == parseInt(valDigito) )
   {
      return true;
   }
   rut.select();
   rut.focus();
   return false;
}

/*============================================================================*/
function isBusca(string)
{
//   if (!string) return false;
   var iChars = "*|,\":<>[]{}`\';()&$#%@^_~-?/";

   for (var i = 0; i < string.length; i++)
   {
      if (iChars.indexOf(string.charAt(i)) != -1)
         {return false}
   }
   return true;
} 
