/*----------------------------------------------------------------------------*/
/* File:           UTILS.JS                                                   */
/* Description:    JavaScript common routines                                 */
/* Author:         Carlos Adolfo Ortiz Q.                                     */
/* Date:           Apr.01/2005                                                */
/* Last Modified:  May.21/2005                                                */
/* Version:        1.4                                                        */
/* Copyright (c), 2005, Cinemática Producciones Ltda.                         */
/*----------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------------
 History
 Apr.01/2004 File Created.
------------------------------------------------------------------------------*/
function isNumeric(s) {
   if (s == "") {
       return false;
   }
   else {
       var i;
       var ch;

       for (i = 0; i < s.length; i++) {
           ch = s.charAt(i);
           if (!(ch >= '0' && ch <= '9')) {
                return false;
           }
       }
   }
   return true;
}

function isNumericZero(s) {
   if (s == "") {
       return false;
   }
   else {
       var i;
       var ch;
       for (i = 0; i < s.length; i++) {
            ch = s.charAt(i);
            if (!(ch == '0')) {
                return false;
            }
       }
   }
   return true;
}

function isNumericWithFormat(s) {
  if (s == "") {
      //alert("If empty");
      return false;
  }
  else {
      // Nit must be always numeric and allow a '-'. Thus
      // 123456789-1 is valid but 123456789-a is not
      // 1a-1 is not valid, 1a- is not valid.
      var bValid;
      var i;
      var ch;

      bValid = true;
      for(i = 0; i < s.length; i++) {
        ch = s.charAt(i);
        if (!(((ch >= '0') && (ch <= '9')) || (ch == '-'))) {
           bValid = false;
        }
      }
      if (!bValid)
        return false;
      else {
          // It is valid now let's check format
          // It must not have more than one '-'
          var iCnt;
          iCnt = 0;
          for (i = 0; i < s.length; i++) {
            ch = s.charAt(i);
            if (ch == '-') {
              iCnt++;
            }
          }
          if (iCnt > 1) {
            bValid = false;
          }
          else {
            // OK, now let's validate that the '-' is prior the last digit and that the last
            // digit is in fact one digit and not two.
            iCnt = s.indexOf("-");
            if (iCnt == -1) { // it was not found, then it is valid
               return true;
            }
            // Count how many characters there are past this position
            //
            var iHowMany;
            iHowMany = 0;
            i = iCnt + 1;
            while (i < s.length) {
              iHowMany++;
              i++;
            }
            if ((iHowMany == 0) || (iHowMany > 1)) {
              bValid = false;
            }
          }
          if (!bValid) {
            return false;
         }
      }
  }
  return true;
}

function isValidEMail(szEMail)
{
  var sUser, sServer;

  // Validar que la longuitud sea mayor que 6
  if(szEMail.length > 6) {
    ;
  }
  else {
    return false;
  }

  // User identification
  sUser = szEMail.substring(0, szEMail.indexOf('@'));

  // Server identification
  sServer = szEMail.substring(szEMail.indexOf('@') + 1, szEMail.length);

  // @ present
  if(sUser == '') {
    return(false);
  }

  if(sServer.indexOf('.') == -1) {
    return(false);
  }
  return(true);
}

function checkStringFor(s, chWhich)
{
  var i;
  var ch;

  for (i = 0; i < s.length; i++) {
     ch = s.charAt(i);
     if (ch == chWhich)
       return true;
  }
  return false;
}

// Using a layer as its basis which is first parameter, it's innerHTML content
// is rewritten.
// It assumes an image tag is inside the layer.
function SetMessageLayer(lyMsg, strCode)
{
  lyMsg.innerHTML = "<img src=\"" + strCode + "\">";
}

// Clears the layer configured by SetMessageLayer
// It assumes an image tag is inside the layer.
function RestoreMessageLayer(lyMsg, strCode)
{
  lyMsg.innerHTML = "<img src=" + strCode + ">";
}

// Only works for textarea controls
function textLengthControl(obj, iLength)
{
  var s;

  if (obj.value.length != null) {
    if (obj.value.length > iLength) {
       s = obj.value;
       s = s.substring(0, iLength);
       obj.value = s;
    }
  }
}

function LongitudTextArea(objCampoTexto, intLongitud)
{
  var strTexto;

  if(objCampoTexto.value.length > intLongitud)
  {
    strTexto = objCampoTexto.value
    strTexto = strTexto.substring(0, intLongitud);
    objCampoTexto.value = strTexto;
  }
  
  if(objContador != "")
  {
    objContador.innerHTML = objCampoTexto.value.length + "/" + intLongitud;
  }
}

/* END UTILS.JS */
