// ---------------------------------------------------------------------------------------------------
//
// BEE S.A. (c) 2002
//
// ---------------------------------------------------------------------------------------------------

  var NS4 = (document.layers) ? 1 : 0;
  var IE = (document.all) ? 1 : 0;
  var DOM = 0;
  if (parseInt(navigator.appVersion) >=5) {DOM=1};

  /*************************************************************/
  /* getLayer()                                                */
  /*************************************************************/
  function getLayer(layerID) {

      if (DOM) {
          return document.getElementById(layerID);
      }

      if (IE) {
          return (eval('document.all.' + layerID));
      }

      if (NS4) {
          return document.layers[layerID];
      }

      return null;
  }

  /*************************************************************/
  /* writeHTML()                                               */
  /*************************************************************/
  function writeHTML(layerID, str) {

      var lyr = getLayer(layerID);
      
      if (DOM) {
          lyr.innerHTML = str;
      } else if (IE) {
          lyr.innerHTML = str;
      } else if (NS4) {
          lyr.document.open();
          lyr.document.write(str);
          lyr.document.close();
      }

      return;
  }

  /*************************************************************/
  /* getSelect()                                               */
  /*************************************************************/

  function getSelect(obj)
  {
    for (var i = 0 ; i < obj.options.length ; i++)
      if (obj.options[i].selected)
        return obj.options[i].value;

    return 0;
  }

  /*************************************************************/
  /* getTextField()                                            */
  /*************************************************************/

  function getTextField(obj)
  {
    return obj.value;
  }

  /*************************************************************/
  /* validateString()                                          */
  /*************************************************************/

  function validateString(s)
  {
    var idx  = 0;
    var str  = trim(s);

    if (str == "")
      return true;

    return false;
  }

/*************************************************************/
  /* trim()                                                    */
  /*************************************************************/

  function trim(s)
  {
    var idx  = 0;
    var idxf = 0;
    var str  = String(s);

    while (str.charAt(idx) == " " && idx < str.length)
      idx++;

    if (idx == str.length)
      str = "";
    else {
      str  = str.substring(idx,str.length);
      idxf = str.length - 1;

      while(str.charAt(idxf) == " ")
        idxf--;

      str = str.substring(0, idxf + 1);
    }

    return str;
  }

  /*************************************************************/
  /* doPrint()                                                 */
  /*************************************************************/

  function doPrint()
  {
    if (!window.print) {
      var msg =       "Estimado Cliente:\n";
          msg = msg + "para utilizar el botón de impresión,\n";
          msg = msg + "debe actualizar su navegador.";

      alert(msg);

      return;
    }

    window.focus();
    window.print();

    return;
  }


  /*****************************************************************/
  /* show(object)                                                  */
  /*****************************************************************/
  function show(object) {
      if (IE) {
        object.style.visibility = "visible";
        return;
      }

      if (DOM) {
        object.style.visibility = "visible";
        return;
      }

      if (NS4) {
        object.visibility = "show";
        return;
      }    
  }

  /*******************************************************************/
  /* hide(object)                                                    */
  /*******************************************************************/
  function hide(object) {
      if (IE) {
        object.style.visibility = "hidden";
        return;
      }

      if (DOM) {
        object.style.visibility = "hidden";
        return;
      }

      if (NS4) {
        object.visibility = "hide";
        return;
      }

  }

  /*************************************************************/
  /* validateFloat()                                           */
  /*************************************************************/
  function validateFloat(s)
  {
    var idx;
    var c;

    if (trim(s) == "")
      return true;

    for (idx = 0 ; idx < s.length ; idx ++) {
      c = s.charAt(idx);

      if ((c < "0" || c > "9") && c != "." && c != "," && c!= "-" && c!= "+")
        return true;
    }

    if (String(parseFloat(s)) == "NaN")
      return true;

    return false;
  }

  /*************************************************************/
  /* validateInt()                                             */
  /*************************************************************/
  function validateInt(s)
  {
    var idx;
    var c;

    if (trim(s) == "")
      return true;

    for (idx = 0 ; idx < s.length ; idx ++) {
      c = s.charAt(idx);

      if (c < "0" || c > "9")
        return true;
    }

    if (String(parseInt(s)) == "NaN")
      return true;

    return false;
  }

  /*************************************************************/
  /* _format( x, n )                                           */
  /*************************************************************/

  function _format( x, n )
  {
    var r = x - _round( x );

    return ( (_round( r * _pow( 10, n ) ) / _pow( 10, n ))+ _round(x)*1.0 );
  }

  /*************************************************************/
  /* _round( x )                                               */
  /*************************************************************/

  function _round( x )
  {
      return Math.round( x );
  }

  /*************************************************************/
  /* _pow( e, b )                                              */
  /*************************************************************/

  function _pow( e, b )
  {
    return Math.pow( e, b );
  }
  
  /*************************************************************/
  /* getRadio()                                                */
  /*************************************************************/

  function getRadio(obj)
  {
    if (String(obj.length) == "undefined") {
      if (obj.checked)
        return obj.value;
      else
        return "0";
    }
    else {
      for (var i = 0 ; i < obj.length ; i++)
        if (obj[i].checked)
          return obj[i].value;

      return 0;
    }
  }

  /*************************************************************/
  /* validateDate()                                            */
  /*************************************************************/

  function validateDate(s)
  {
    var numdias = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    _fec = "";

    if (s.length != 10)
      return true;

    var sdia = s.substr(0,2);
    var smes = s.substr(3,2);
    var sano = s.substr(6,4);
    
    if (validateInt(sdia))
      return true;

    if (validateInt(smes))
      return true;

    if (validateInt(sano))
      return true;

    var dia = parseInt(sdia, 10);
    var mes = parseInt(smes, 10);
    var ano = parseInt(sano, 10);

    if (((ano % 4) == 0 && (ano % 100) != 0) || (ano % 400) == 0)
      numdias[2]++;

    if (mes <= 0 || mes > 12)
      return true;

    if (dia <= 0 || dia > numdias[mes])
      return true;

    return false;
  }

  /*************************************************************/
  /* validateMonto(mto)                                        */
  /*************************************************************/
  function validateMonto (mto) {
      // La parte entera como maximo 12 caracteres !

      if (trim(mto) == "")
      {
        alert ("ERROR : Debe ingresar 'Monto'");
        return true;
      }

      ind0 = mto.indexOf(',');
      ind1 = mto.indexOf('.');
      ind2 = mto.indexOf('.' , ind1 + 1);

      if (ind0 >= 0) { // Encontro ',', monto no valido
        alert ("ERROR : Monto ingresado contiene ','");
        return true;
      }

      if (validateFloat(mto)) {
        alert("ERROR: Debe ingresar adecuadamente 'Monto'");
        return true;
      }

      if (parseFloat(mto) <= 0) {
        alert("ERROR: El 'Monto' debe ser mayor que cero");
        return true;
      }

      if (ind1 >= 0) { // Encontro '.', valido que no haya otro
        if (ind2 >= 0) { // Existe mas de un '.', error
          alert ("ERROR : Monto ingresado contiene mas de un punto decimal");
          return true;
        } else {
            if (ind1 >=13 ) {
              alert ("ERROR : Importe Moneda Cargo : Largo del dato es mayor al esperado");
              return true;
            }
         } 
      } else {
          if (trim(mto).length >= 13) {
            alert ("ERROR : Importe Moneda Cargo : Largo del dato es mayor al esperado");
            return true;
          }      
      }

      return false;
    }

  /*************************************************************/
  /* doValidaeMail()                                           */
  /*************************************************************/
  function doValidaeMail(emailStr)
  {

  /*
  Validación completa de una dirección de Email

  Por: Alejandro Gassmann

  Con este script podremos validar la dirección de email, del lado del cliente.
  El código obliga que se ingrese un dirección correctamente: al estilo  nombre@dominio.com o bien nombre@[ip].
  */

  // ---------------------------------------------------------------------------------------------------
  //
  // Modifado por BEE S.A. (c) 2002
  //
  // ---------------------------------------------------------------------------------------------------

  /* Verificar si el email tiene el formato user@dominio. */
  var emailPat=/^(.+)@(.+)$/;

  /* Verificar la existencia de caracteres. ( ) < > @ , ; : \ " . [ ] */
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";

  /* Verifica los caracteres que son válidos en una dirección de email */
  var validChars="\[^\\s" + specialChars + "\]";

  var quotedUser="(\"[^\"]*\")";

  /* Verifica si la dirección de email está representada con una dirección IP Válida */
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

  /* Verificar caracteres inválidos */

  var atom=validChars + '+';
  var word="(" + atom + "|" + quotedUser + ")";
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  /*domain, as opposed to ipDomainPat, shown above. */
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");


  var matchArray=emailStr.match(emailPat);
  if (matchArray==null) {
    alert("ERROR : Dirección ingresada incorrecta (revisar @ y .'s)");
    return false;
  }
  var user=matchArray[1];
  var domain=matchArray[2];

  // Si el user "user" es valido
  if (user.match(userPat)==null) {
    // Si no
    alert("ERROR : El nombre de usuario no es válido.");
    return false;
  }

  /* Si la dirección IP es válida */
  var IPArray=domain.match(ipDomainPat);
  if (IPArray!=null) {
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        alert("ERROR : IP de destino inválida");
        return false;
      }
    }
    return true;
  }

  var domainArray=domain.match(domainPat);
  if (domainArray==null) {
    alert("ERROR : El dominio parece no ser válido.");
    return false;
  }

  var atomPat=new RegExp(atom,"g");
  var domArr=domain.match(atomPat);
  var len=domArr.length;

  if ((domArr[domArr.length-1].length<2) || (domArr[domArr.length-1].length>3)) {
    alert("ERROR : La dirección debe tener 3 letras si es .'com' o 2 si es de algún pais.");
    return false;
  }

  if (len<2) {
    var errStr="ERROR : La dirección es erronea";
    alert(errStr);
    return false;
  }

  // La dirección de email ingresada es Válida
  return true;

  }

  /*************************************************************/
  /* textCounter(field, maxlimit)                              */
  /*************************************************************/
  function textCounter(field, maxlimit) {
  
  <!-- Original:  Ronnie T. Moore -->
  <!-- Web Site:  The JavaScript Source -->

  <!-- Dynamic 'fix' by: Nannette Thacker -->
  <!-- Web Site: http://www.shiningstar.net -->
 
  <!-- This script and many more are available free online at -->
  <!-- The JavaScript Source!! http://javascript.internet.com -->
  
  // ---------------------------------------------------------------------------------------------------
  //
  // Modifado por BEE S.A. (c) 2002
  //
  // ---------------------------------------------------------------------------------------------------
  
    if (field.value.length > maxlimit) { // if too long...trim it!
      field.value = field.value.substring(0, maxlimit);
    } 
  }
  
  /*************************************************************/
  /* function soloNumeros(texto)                               */
  /*************************************************************/
  var nav4 = window.Event ? true : false;
  function soloNumeros(evt){    
    // NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57    
    var key = nav4 ? evt.which : evt.keyCode;    
    return (key <= 13 || (key >= 48 && key <= 57)); 
  }
  
  
  /*************************************************************/
  /* validaFecha()                                            */
  /*************************************************************/

  function validaFecha(s)
  {
    var numdias = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    _fec = "";

    if (s.length != 8)
      return true;

    var sdia = s.substr(0,2);
    var smes = s.substr(2,2);
    var sano = s.substr(4,4);
    
    if (validateInt(sdia))
      return true;

    if (validateInt(smes))
      return true;

    if (validateInt(sano))
      return true;

    var dia = parseInt(sdia, 10);
    var mes = parseInt(smes, 10);
    var ano = parseInt(sano, 10);

    if (((ano % 4) == 0 && (ano % 100) != 0) || (ano % 400) == 0)
      numdias[2]++;

    if (mes <= 0 || mes > 12)
      return true;

    if (dia <= 0 || dia > numdias[mes])
      return true;

    return false;
  }
