init_forms();

function init_forms()
{
  for (_iform=0; _iform<document.forms.length; _iform++)
  {
    _form = document.forms[_iform];
    for (_ielem=0; _ielem<_form.elements.length; _ielem++)
    {
      _elem = _form.elements[_ielem];
      if ((_elem.type == "text") && (_elem.className) && (_elem.className.length > 0))
      {
        switch(_elem.className)
        {
          case "upper":
          case "lower":
          case "capitalized":
          case "int":
          case "date":
          case "phone":
          case "email":
          case "web":
            _elem.onblur = eval(_elem.className+"_onblur");
            break;

          case "float":
          case "float3":
            _elem.style.textAlign = "right";
            _elem.onfocus = float_onfocus;
            _elem.onblur = float_onblur;
            break;

          case "time":
          case "timeHHMMSS":
            _elem.onblur = time_onblur;
            break;

          default:
            break;
        }
      }
    }
  }
}

function reload(form_p, action_p)
{
  form_p.action = action_p;
  form_p.submit();
}

function trim(string_p)
{
  while (string_p.substring(0,1) == ' ')
  {
    string_p = string_p.substring(1, string_p.length);
  }
  while (string_p.substring(string_p.length-1, string_p.length) == ' ')
  {
    string_p = string_p.substring(0, string_p.length-1);
  }
  return string_p;
}

function purge(string_p)
{
  string_p = trim(string_p);
  while (string_p.indexOf('  ') > 0)
  {
    string_p = string_p.replace(/  /g, ' ');
  }
  return string_p;
}

function upper_onblur(event)
{
  this.value = purge(this.value.toUpperCase());
}

function lower_onblur(event)
{
  this.value = purge(this.value.toLowerCase());
}

function capitalized_onblur(event)
{
  capitalized = "";
  lower = purge(this.value.toLowerCase());
  lastc = ' ';
  for (i=0; i<lower.length; i++)
  {
    chr = lower.charAt(i);
    code = parseInt(lower.charCodeAt(i));
    if ((code >= 97) && (code <= 122))
    {
      if ((lastc == ' ') || (lastc == '-'))
      {
        chr = chr.toUpperCase();
      }
    }
    capitalized += chr;
    lastc = chr;
  }
  this.value = capitalized;
}

function int_onfocus(event)
{
  this.style.textAlign = "left";
}

function int_onblur(event)
{
  if (this.value.length == 0) { return; }
  number = "";
  for (i=0; i<this.value.length; i++)
  {
    c = this.value.charAt(i);
    if ((isNaN(parseInt(c)) == false) || (c == '.') || ((c == '-') && (number.length == 0)))
    {
      number += c;
    }
    else if (c == ',')
    {
      number += '.';
    }
  }
  number = String(Math.round(number));
  this.value = number;
  //this.style.textAlign = "right";
}

function float_onfocus(event)
{
  this.style.textAlign = "left";
}

function float_onblur(event)
{
  if (this.value.length == 0) { return; }
  var decimals_p = (this.className == "float3") ? 3 : 2;
  var dec_point_p = ',';
  var thousands_sep_p = ' ';
  number = "";
  if ( (this.value.indexOf("+") > 0) || (this.value.indexOf("-") > 0)
    || (this.value.indexOf("*") > 0) || (this.value.indexOf("/") > 0) )
  {
    number = eval(this.value);
  }
  else
  {
    for (i=0; i<this.value.length; i++)
    {
      c = this.value.charAt(i);
      if ((isNaN(parseInt(c)) == false) || (c == '.') || ((c == '-') && (number.length == 0)))
      {
        number += c;
      }
      else if (c == ',')
      {
        number += '.';
      }
    }
  }
  mdiv = 1.00;
  for (i=0; i<decimals_p; i++) { mdiv *= 10.00; }
  number = String(Math.round(number*mdiv)/mdiv);
  sep = number.indexOf('.');
  if (sep < 0)
  {
    number += ".";
    sep = 0;
  }
  else
  {
    sep = number.length - (sep+1);
  }
  while (sep < decimals_p)
  {
    number += "0";
    sep++;
  }
  if (dec_point_p != '.')
  {
    number = number.replace(/\./g, dec_point_p);
  }
  if (thousands_sep_p.length > 0)
  {
    sep = number.indexOf(dec_point_p);
    while (sep > 3)
    {
      number = number.substr(0,sep-3) + thousands_sep_p + number.substr(sep-3);
      sep -= 3;
    }
  }
  this.value = number;
  this.style.textAlign = "right";
}

function date_onblur(event)
{
  if (this.value.length == 0) { return; }
  var error = "";
  var input = "";
  var currentDate = new Date();
  var yyyy = currentDate.getFullYear();
  var mm = currentDate.getMonth()+1;
  var dd = currentDate.getDate();
  if (mm < 10) { mm = "0" + String(mm); }
  if (dd < 10) { dd = "0" + String(dd); }
  for (i=0; i<this.value.length; i++)
  {
    if (isNaN(c = parseInt(this.value.charAt(i))) == false)
    {
      input += String(c);
    }
  }
  if (input.length > 0)
  {
    d = parseFloat(input.substr(0, (input.length==1)?1:2));
    if ((d >= 1) && (d <= 31))
    {
      dd = ((d < 10) ? "0" : "") + String(d);
      if (input.length > 2)
      {
        m = parseFloat(input.substr(2, (input.length==3)?1:2));
        if ((m >= 1) && (m <= 12))
        {
          if ((d == 31) && ((m == 2) || (m == 4) || (m == 6) || (m == 9) || (m == 11)))
          {
            error = "Ce mois ne compte pas 31 jours.";
          }
          else
          {
            mm = ((m < 10) ? "0" : "") + String(m);
            if (input.length > 4)
            {
              y = parseFloat(input.substr(4));
              if (y < 10) { yyyy = "200" + String(y); }
              else if (y < 100) { yyyy = "19" + String(y); }
              else if (y < 1000) { yyyy = "0" + String(y); }
              else { yyyy = String(y); }
            }
          }
        }
        else
        {
          error = m + " n'est pas un mois correct.";
        }
      }
    }
    else
    {
      error = d + " n'est pas un jour correct.";
    }
  }
  if (error.length == 0)
  {
    this.value = dd + "/" + mm + "/" + yyyy;
    this.style.backgroundColor = "";
  }
  else
  {
    this.style.backgroundColor = "#ff4040";
    alert(error);
    this.focus();
  }
}

function time_onblur(event)
{
  if (this.value.length == 0) { return; }
  var error = "";
  var input = "";
  var seconds = (this.className == "timeHHMMSS") ? true : false;
  var currentDate = new Date();
  var hh = currentDate.getHours();
  var mm = "00"; //currentDate.getMinutes()+1;
  var ss = "00"; //currentDate.getSeconds();
  if (hh < 10) { hh = "0" + String(hh); }
  //if (mm < 10) { mm = "0" + String(mm); }
  //if (ss < 10) { ss = "0" + String(ss); }
  for (i=0; i<this.value.length; i++)
  {
    if (isNaN(c = parseInt(this.value.charAt(i))) == false)
    {
      input += String(c);
    }
  }
  if (input.length > 0)
  {
    h = parseFloat(input.substr(0, (input.length==1)?1:2));
    if ((h >= 0) && (h <= 23))
    {
      hh = ((h < 10) ? "0" : "") + String(h);
      if (input.length > 2)
      {
        m = parseFloat(input.substr(2, (input.length==3)?1:2));
        if ((m >= 0) && (m <= 59))
        {
          mm = ((m < 10) ? "0" : "") + String(m);
          if ((seconds == true) && (input.length > 4))
          {
            s = parseFloat(input.substr(4));
            if ((s >= 0) && (s <= 59))
            {
              ss = ((s < 10) ? "0" : "") + String(s);
            }
            else
            {
              error = s + " n'est pas un nombre de secondes correct.";
            }
          }
        }
        else
        {
          error = m + " n'est pas un nombre de minutes correct.";
        }
      }
    }
    else
    {
      error = h + " n'est pas une heure correcte.";
    }
  }
  if (error.length == 0)
  {
    this.value = hh + ":" + mm;
    if (seconds == true) {  this.value += ":" + ss; }
    this.style.backgroundColor = "";
  }
  else
  {
    this.style.backgroundColor = "#ff4040";
    alert(error);
    this.focus();
  }
}

function phone_onblur(event)
{
  if (this.value.length == 0) { return; }
  sep_p = '.';
  var input = "";
  var digits = 0;
  for (i=0; i<this.value.length; i++)
  {
    if (isNaN(c = parseInt(this.value.charAt(i))) == false)
    {
      if ((digits > 0) && ((digits%2) == 0)) { input += sep_p; }
      input += String(c);
      digits++;
    }
  }
  this.value = input;
}

function email_onblur(event)
{
  if (this.value.length == 0) { return; }
}

function web_onblur(event)
{
  if (this.value.length == 0) { return; }
}
