//==============================================================================
//==============================================================================
function checkEmailPassword()
{
  var form = document.forms['login'];
  if ((form != null) && (form.email != null) && (form.password != null))
  {
    form.email.style.backgroundImage = (form.email.value.length == 0) ? "url(bg-email.png)" : "none";
    form.password.style.backgroundImage = (form.password.value.length == 0) ? "url(bg-password.png)" : "none";
  }
}

//==============================================================================
//==============================================================================
function showHideError(div_p)
{
  if (div_p.style.overflow == "visible")
  {
    div_p.style.height = "120px";
    div_p.style.overflow = "auto";
  }
  else
  {
    div_p.style.height = "auto";
    div_p.style.overflow = "visible";
  }
}

//==============================================================================
//==============================================================================
function ajaxQuery(method_p, url_p, data_p, callback_p)
{
  try
  {
    var xmlhttp = null;
	
    if(window.XMLHttpRequest) // Firefox
    {
      xmlhttp = new XMLHttpRequest();
    }
    else if(window.ActiveXObject) // Internet Explorer
    {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else // XMLHttpRequest non supporté par le navigateur
    {
      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest.");
      return;
    }
	
    if ((method_p == "GET") && (data_p != null))
    {
      url_p += "?" + data_p;
      data_p = null;
    }
	
    xmlhttp.open(method_p, url_p, true);
	
    xmlhttp.onreadystatechange = function anonymous()
    {
      if (xmlhttp.readyState == 4) // complete
      {
        if (xmlhttp.status == 200)
        {
          if (callback_p != null)
          {
            callback_p(xmlhttp.responseText);
          }
        }
      }
    }

    if (method_p == "POST")
    {
      xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    }

    xmlhttp.send(data_p);
  }
  catch (exception)
  {
    alert("Erreur (" + exception.name + ") : " + exception.message);
  }
}

//==============================================================================
//==============================================================================
function initForms()
{
  try
  {
    var iform, form, ielem, elem, classNames, iclass, className;
    var prevElement = null;
    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 != null) && (elem.type != "hidden"))
        {
          elem.prevElement = prevElement;
          elem.nextElement = null;
          if (prevElement != null)
          {
            prevElement.nextElement = elem;
          }
          prevElement = elem;
        }
        if ((elem.type != null) && (elem.type == "text") && (elem.className != null) && (elem.className.length > 0))
        {
          classNames = trim(elem.className.replace(/  /g, " ")).split(" ");
          for (iclass=0; iclass<classNames.length; iclass++)
          {
            className = classNames[iclass];
            switch(className)
            {
              case "upper":
              case "lower":
              case "capitalized":
              case "int":
              case "phone":
              case "email":
              case "web":
                var onblur = eval(className+"_onblur");
                if ((elem.onblur != null) && (elem.onblur != onblur)) { elem.onblur_org = elem.onblur; }
                elem.onblur = onblur;
                break;

              case "date":
                var onblur = eval(className+"_onblur");
                if ((elem.onblur != null) && (elem.onblur != onblur)) { elem.onblur_org = elem.onblur; }
                elem.onblur = onblur;
                break;

              case "float":
              case "float0":
              case "float1":
              case "float2":
              case "float3":
              case "float4":
              case "float5":
                elem.style.textAlign = "right";
                if ((elem.onfocus != null) && (elem.onfocus != float_onfocus)) { elem.onfocus_org = elem.onfocus; }
                if ((elem.onblur != null) && (elem.onblur != float_onblur)) { elem.onblur_org = elem.onblur; }
                elem.onfocus = float_onfocus;
                elem.onblur = float_onblur;
                break;

              default:
                break;
            }
          }
        }
      }
    }
  }
  catch (exception)
  {
    alert("Erreur (" + exception.name + ") : " + exception.message);
  }
}

//==============================================================================
//==============================================================================
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)
{
  if ((this.onblur_org != null) && (this.onblur_org.toString().indexOf("upper_onblur") == -1)) { this.onblur_org(); }
  this.value = purge(this.value.toUpperCase());
}

function lower_onblur(event)
{
  if ((this.onblur_org != null) && (this.onblur_org.toString().indexOf("lower_onblur") == -1)) { this.onblur_org(); }
  this.value = purge(this.value.toLowerCase());
}

function capitalized_onblur(event)
{
  if ((this.onblur_org != null) && (this.onblur_org.toString().indexOf("capitalized_onblur") == -1)) { this.onblur_org(); }
  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)
{
  if ((this.onfocus_org != null) && (this.onfocus_org.toString().indexOf("int_onfocus") == -1)) { this.onfocus_org(); }
  this.style.textAlign = "left";
}

function int_onblur(event)
{
  if ((this.onblur_org != null) && (this.onblur_org.toString().indexOf("int_onblur") == -1)) { this.onblur_org(); }
  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)
{
  if ((this.onfocus_org != null) && (this.onfocus_org.toString().indexOf("float_onfocus") == -1)) { this.onfocus_org(); }
  this.style.textAlign = "left";
}

function float_onblur(event)
{
  if ((this.onblur_org != null) && (this.onblur_org.toString().indexOf("float_onblur") == -1)) { this.onblur_org(); }
  if (this.value.length == 0) { return true; }
  var decimals_p = 2;
  if ((this.className != null) && (this.className.length > 0))
  {
    var classNames = trim(this.className.replace(/  /g, " ")).split(" ");
    for (var iclass=0; iclass<classNames.length; iclass++)
    {
      var className = classNames[iclass];
      if (className.substr(0,5) == "float")
      {
        decimals_p = (className == "float") ? 2 : parseInt(className.substr(5));
        break;
      }
    }
  }
  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)
  {
    if (decimals_p > 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);
    if (sep < 0) { sep = number.length; }
    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.onblur_org != null) && (this.onblur_org.toString().indexOf("date_onblur") == -1)) { this.onblur_org(); }
  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 < 20) { yyyy = "20" + 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 phone_onblur(event)
{
  if ((this.onblur_org != null) && (this.onblur_org.toString().indexOf("phone_onblur") == -1)) { this.onblur_org(); }
  if (this.value.length == 0) { return; }
  sep_p = ' ';
  var input = "";
  var digits = 0;
  var i, c;
  if (this.value.charAt(0) == '+')
  {
    input = "+";
    for (i=0; i<this.value.length; i++)
    {
      if (isNaN(c = parseInt(this.value.charAt(i))) == false)
      {
        if ((digits == 2) || ((digits > 2) && ((digits%2) == 1))) { input += sep_p; }
        input += String(c);
        digits++;
      }
    }
  }
  else if ((this.value.charAt(0) == '0') && (this.value.charAt(1) == '0'))
  {
    input = this.value;
  }
  else
  {
    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.onblur_org != null) && (this.onblur_org.toString().indexOf("email_onblur") == -1)) { this.onblur_org(); }
  if (this.value.length == 0) { return; }
}

function web_onblur(event)
{
  if ((this.onblur_org != null) && (this.onblur_org.toString().indexOf("web_onblur") == -1)) { this.onblur_org(); }
  if (this.value.length == 0) { return; }
}

