function MM_swapImgRestore()
{ //v3.0
  var i,x,a=document.MM_sr;
  for(i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
    x.src = x.oSrc;
}

function MM_preloadImages()
{ //v3.0
  var d=document;
  if (d.images)
  {
    if (!d.MM_p)
      d.MM_p = new Array();
    var i, j = d.MM_p.length, a = MM_preloadImages.arguments;

    for (i = 0; i < a.length; i++)
      if (a[i].indexOf("#") != 0)
      {
        d.MM_p[j] = new Image;
        d.MM_p[j++].src = a[i];
      }
  }
}

function MM_findObj(n, d)
{ //v4.0
  var p, i, x;

  if (!d)
    d=document;
  if ((p = n.indexOf("?")) > 0 && parent.frames.length)
  {
    d = parent.frames[n.substring(p+1)].document;
    n = n.substring(0,p);
  }
  if (!(x = d[n]) && d.all)
    x = d.all[n];
  for (i=0; !x && i < d.forms.length; i++)
    x = d.forms[i][n];
  for (i = 0; !x && d.layers && i < d.layers.length; i++)
    x = MM_findObj(n,d.layers[i].document);
  if ( !x && document.getElementById)
    x = document.getElementById(n);
  return x;
}

function MM_swapImage()
{ //v3.0
  var i, j = 0, x, a = MM_swapImage.arguments;
  document.MM_sr = new Array;
  for (i = 0; i < (a.length - 2); i += 3)
    if ((x = MM_findObj(a[i])) != null)
    {
      document.MM_sr[j++] = x;
      if ( !x.oSrc)
        x.oSrc = x.src;
      x.src = a[i+2];
    }
}

function MM_goToURL()
{ //v3.0
  var i, args = MM_goToURL.arguments;
  document.MM_returnValue = false;
  for (i = 0; i < (args.length - 1); i += 2)
    eval(args[i] + ".location='" + args[i+1] + "'");
}

function init()
{
  document.getElementById("duration").style.display = "none";
  document.order.currency2.value = document.order.currency.value + "(£)";
  document.order.firstname.focus();
}

function validation_form(p_sMachineIDCosmicDefault,
                         p_sMachineIDProjectDefault,
                         p_sMachineIDPredictItDefault,
                         p_sMachineIDConverterDefault)
{
  valide = true;
  texte = document.order.firstname.value;
  if (!verif_non_vide(texte))
  {
    valide = false;
    alert("Please, enter your first name !!");
    document.order.firstname.focus();
  }
  if (valide == true)
  {
    texte = document.order.lastname.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your last name !!");
      document.order.lastname.focus();
    }
  }
  if (valide == true)
  {
    texte = document.order.address1.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your address !!");
      document.order.address1.focus();
    }
  }
  if (valide == true)
  {
    texte = document.order.town.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your town !!");
      document.order.town.focus();
    }
  }
  if (valide == true)
  {
    texte = document.order.state.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your state !!");
      document.order.state.focus();
    }
  }
  if (valide == true)
  {
    texte = document.order.country.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your country !!");
      document.order.country.focus();
    }
  }
  if (valide == true)
  {
    texte = document.order.postcode.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your postcode !!");
      document.order.postcode.focus();
    }
  }
  if (valide == true)
  {
    texte = document.order.email.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your email address !!");
      document.order.email.focus();
    }
    else if (texte.indexOf("@") == -1)
    {
      valide = false;
      alert("Please, your email address has to contain a character '@' !!");
      document.order.email.focus();
    }
    else if (nbOccur(document.order.email.value, "@") > 1)
    {
      valide = false;
      alert("Please, your email address has to contain an alone character '@' !!");
      document.order.email.focus();
    }
    else if (!verifFormatEmail(document.order.email.value))
    {
      valide = false;
      alert("Please, your email address has to have the format 'xxx@yyy.zz' !!");
      document.order.email.focus();
    }
  }
  if (valide == true)
  {
    texte = document.order.phone.value;
    if (!verif_non_vide(texte))
    {
      valide = false;
      alert("Please, enter your phone number !!");
      document.order.phone.focus();
    }
  }
  if (valide == true)
  {
    if (!verifMachineIdCosmic(p_sMachineIDCosmicDefault))
    {
      valide = false;
      document.order.mcmids.focus();
    }
  }
  if (valide == true)
  {
    if (!verifMachineIdProject(p_sMachineIDProjectDefault))
    {
      valide = false;
      document.order.mpmids.focus();
    }
  }
  if (valide == true)
  {
    if (!verifMachineIdPredictIt(p_sMachineIDPredictItDefault))
    {
      valide = false;
      document.order.prmids.focus();
    }
  }
  if (valide == true)
  {
    if (!verifMachineIdConverter(p_sMachineIDConverterDefault))
    {
      valide = false;
      document.order.convmids.focus();
    }
  }
  if (valide == true)
  {
    if (document.order.deliverysh.value == "Choice3" && document.order.currency.value != "GBP")
    {
      valide = false;
      alert("Please, for this shipping you have to choose absolutely the GBP currency !!");
    }
  }
  if (valide == true)
  {
    document.order.submit();
  }
}

function verifFormatEmail(texte)
{
  bOK = true;

  var tab1 = texte.split("@");

  if (tab1[0] == "" || tab1[1] == "")
    bOK = false;
  else
  {
    var part2 = tab1[1];
    var tab2 = part2.split(".");

    if (tab2.length < 2)
      bOK = false
    else if (tab2[0] == "" || tab2[1] == "")
      bOK = false;
  }
  return bOK;
}

function verifMachineIdCosmic(p_sMachineIDCosmicDefault)
{
  var iValeur = 0;
  var bOK = true;
  if (verif_non_vide(document.order.mcperp.value))
  {
    iValeur = parseInt(document.order.mcperp.value);
  }
  else if (verif_non_vide(document.order.mcyear.value))
  {
    iValeur = parseInt(document.order.mcyear.value);
  }
  else if (verif_non_vide(document.order.mcproj.value))
  {
    iValeur = parseInt(document.order.mcproj.value);
  }

  var sChaine = document.order.mcmids.value;

  if (iValeur == 0)
  {
    if (verif_non_vide(sChaine) && sChaine != p_sMachineIDCosmicDefault)
    {
      bOK = false;
      alert("Please, you don't need to enter Machine Ids for MeterIt-Cosmic !!");
    }
  }
  else
  {
    if (sChaine == p_sMachineIDCosmicDefault || sChaine == "")
    {
      bOK = false;
      alert("Please, you have to enter " + iValeur + " Machine Id(s) for MeterIt-Cosmic !!");
    }
    else
    {
      if (sChaine.substr(sChaine.length - 1, 1) == ";")
        sChaine = sChaine.substr(0, sChaine.length - 1);

      var tabMID = sChaine.split(";");

      if (tabMID.length > iValeur)
      {
        bOK = false;
        alert("Please, you entered too Machine Ids for MeterIt-Cosmic !!");
      }
      else if (tabMID.length < iValeur)
      {
        bOK = false;
        alert("Please, you didn't enter enough Machine Ids for MeterIt-Cosmic !!");
      }
      else
      {
        // Vérification que les machine ids sont tous et que leur longueur est supérieur à zéro
        var iCptMID = 0;
        var sListeMID = "";

        while (iCptMID < tabMID.length && bOK)
        {
          if (tabMID[iCptMID] == "")
          {
            bOK = false;
            alert("Please, the Machine Id's length have to be more than 0 !!");
          }
          else if (sListeMID.indexOf("[" + tabMID[iCptMID] + "]") != -1)
          {
            bOK = false;
            alert("Please, you entered two identical Machine Ids for MeterIt-Cosmic !!");
          }
          else
          {
            if (sListeMID == "")
              sListeMID = "[" + tabMID[iCptMID] + "]";
            else
              sListeMID += ";[" + tabMID[iCptMID] + "]";

            iCptMID++;
          }
        }
      }
    }
  }

  return bOK;
}

function verifMachineIdProject(p_sMachineIDProjectDefault)
{
  var iValeur = 0;
  var bOK = true;
  if (verif_non_vide(document.order.mpperp.value))
  {
    iValeur = parseInt(document.order.mpperp.value);
  }
  else if (verif_non_vide(document.order.mpyear.value))
  {
    iValeur = parseInt(document.order.mpyear.value);
  }

  var sChaine = document.order.mpmids.value;

  if (iValeur == 0)
  {
    if (verif_non_vide(sChaine) && sChaine != p_sMachineIDProjectDefault)
    {
      bOK = false;
      alert("Please, you don't need to enter Machine Ids for MeterIt-Project !!");
    }
  }
  else
  {
    if (sChaine == p_sMachineIDProjectDefault || sChaine == "")
    {
      bOK = false;
      alert("Please, you have to enter " + iValeur + " Machine Id(s) for MeterIt-Project !!");
    }
    else
    {
      if (sChaine.substr(sChaine.length - 1, 1) == ";")
        sChaine = sChaine.substr(0, sChaine.length - 1);

      var tabMID = sChaine.split(";");

      if (tabMID.length > iValeur)
      {
        bOK = false;
        alert("Please, you entered too Machine Ids for MeterIt-Project !!");
      }
      else if (tabMID.length < iValeur)
      {
        bOK = false;
        alert("Please, you didn't enter enough Machine Ids for MeterIt-Project !!");
      }
      else
      {
        // Vérification que les machine ids sont tous et que leur longueur est supérieur à zéro
        var iCptMID = 0;
        var sListeMID = "";

        while (iCptMID < tabMID.length && bOK)
        {
          if (tabMID[iCptMID] == "")
          {
            bOK = false;
            alert("Please, the Machine Id's length have to be more than 0 !!");
          }
          else if (sListeMID.indexOf("[" + tabMID[iCptMID] + "]") != -1)
          {
            bOK = false;
            alert("Please, you entered two identical Machine Ids for MeterIt-Project !!");
          }
          else
          {
            if (sListeMID == "")
              sListeMID = "[" + tabMID[iCptMID] + "]";
            else
              sListeMID += ";[" + tabMID[iCptMID] + "]";

            iCptMID++;
          }
        }
      }
    }
  }

  return bOK;
}

function verifMachineIdPredictIt(p_sMachineIDPredictItDefault)
{
  var iValeur = 0;
  var bOK = true;
  if (verif_non_vide(document.order.prperp.value))
  {
    iValeur = parseInt(document.order.prperp.value);
  }
  else if (verif_non_vide(document.order.pryear.value))
  {
    iValeur = parseInt(document.order.pryear.value);
  }

  var sChaine = document.order.prmids.value;

  if (iValeur == 0)
  {
    if (verif_non_vide(sChaine) && sChaine != p_sMachineIDPredictItDefault)
    {
      bOK = false;
      alert("Please, you don't need to enter Machine Ids for PredictIt !!");
    }
  }
  else
  {
    if (sChaine == p_sMachineIDPredictItDefault || sChaine == "")
    {
      bOK = false;
      alert("Please, you have to enter " + iValeur + " Machine Id(s) for PredictIt !!");
    }
    else
    {
      if (sChaine.substr(sChaine.length - 1, 1) == ";")
        sChaine = sChaine.substr(0, sChaine.length - 1);

      var tabMID = sChaine.split(";");

      if (tabMID.length > iValeur)
      {
        bOK = false;
        alert("Please, you entered too Machine Ids for PredictIt !!");
      }
      else if (tabMID.length < iValeur)
      {
        bOK = false;
        alert("Please, you didn't enter enough Machine Ids for PredictIt !!");
      }
      else
      {
        // Vérification que les machine ids sont tous et que leur longueur est supérieur à zéro
        var iCptMID = 0;
        var sListeMID = "";

        while (iCptMID < tabMID.length && bOK)
        {
          if (tabMID[iCptMID] == "")
          {
            bOK = false;
            alert("Please, the Machine Id's length have to be more than 0 !!");
          }
          else if (sListeMID.indexOf("[" + tabMID[iCptMID] + "]") != -1)
          {
            bOK = false;
            alert("Please, you entered two identical Machine Ids for PredictIt !!");
          }
          else
          {
            if (sListeMID == "")
              sListeMID = "[" + tabMID[iCptMID] + "]";
            else
              sListeMID += ";[" + tabMID[iCptMID] + "]";

            iCptMID++;
          }
        }
      }
    }
  }

  return bOK;
}

function verifMachineIdConverter(p_sMachineIDConverterDefault)
{
  var iValeur = 0;
  var bOK = true;
  if (verif_non_vide(document.order.convperp.value))
  {
    iValeur = parseInt(document.order.convperp.value);
  }

  var sChaine = document.order.convmids.value;

  if (iValeur == 0)
  {
    if (verif_non_vide(sChaine) && sChaine != p_sMachineIDConverterDefault)
    {
      bOK = false;
      alert("Please, you don't need to enter Machine Ids for MeterIt-Converter !!");
    }
  }
  else
  {
    if (sChaine == p_sMachineIDConverterDefault || sChaine == "")
    {
      bOK = false;
      alert("Please, you have to enter " + iValeur + " Machine Id(s) for MeterIt-Converter !!");
    }
    else
    {
      if (sChaine.substr(sChaine.length - 1, 1) == ";")
        sChaine = sChaine.substr(0, sChaine.length - 1);

      var tabMID = sChaine.split(";");

      if (tabMID.length > iValeur)
      {
        bOK = false;
        alert("Please, you entered too Machine Ids for MeterIt-Converter !!");
      }
      else if (tabMID.length < iValeur)
      {
        bOK = false;
        alert("Please, you didn't enter enough Machine Ids for MeterIt-Converter !!");
      }
      else
      {
        // Vérification que les machine ids sont tous et que leur longueur est supérieur à zéro
        var iCptMID = 0;
        var sListeMID = "";

        while (iCptMID < tabMID.length && bOK)
        {
          if (tabMID[iCptMID] == "")
          {
            bOK = false;
            alert("Please, the Machine Id's length have to be more than 0 !!");
          }
          else if (sListeMID.indexOf("[" + tabMID[iCptMID] + "]") != -1)
          {
            bOK = false;
            alert("Please, you entered two identical Machine Ids for PredictIt !!");
          }
          else
          {
            if (sListeMID == "")
              sListeMID = "[" + tabMID[iCptMID] + "]";
            else
              sListeMID += ";[" + tabMID[iCptMID] + "]";

            iCptMID++;
          }
        }
      }
    }
  }

  return bOK;
}

function verif_non_vide(text)
{
  return (text.length > 0);
}

function nbOccur(texte, car)
{
  nb = 0;
  i = 0;
  bTrouve = true;
  while (i <= texte.length - 1 && bTrouve)
  {
    test = texte.indexOf(car, i);

    if (test == -1)
    {
      bTrouve = false;
    }
    else
    {
      nb++;
      i = test + 1;
    }
  }
  return nb;
}

function carClavier(e)
{
  if (window.event)
  {
    // pour Internet Explorer
    return String.fromCharCode(window.event.keyCode);
  }
  else
  {
    // Netscape
    return String.fromCharCode(e.which);
  }
}

function testAlpha(texte, e)
{
  if (carClavier(e).charCodeAt(0) == 8)
    return true;
  else
  {
    caractere = carClavier(e);

    if (!verif_non_vide(texte))
    {
      if ((caractere >= "a" && caractere <= "z") || (caractere >= "A" && caractere <= "Z"))
        return true;
      else
        return false;
    }
    else
    {
      if ((caractere >= "a" && caractere <= "z") || (caractere >= "A" && caractere <= "Z") || caractere == " " || caractere == "-")
        return true;
      else
        return false;
    }
  }
}

function testPhone(texte, e)
{
  if (carClavier(e).charCodeAt(0) == 8)
    return true;
  else
  {
    caractere = carClavier(e);

    if (!verif_non_vide(texte))
    {
      if ((caractere >= "0" && caractere <= "9") || caractere == "(" || caractere == "+")
        return true;
      else
        return false;
    }
    else
    {
      if ((caractere >= "0" && caractere <= "9") || caractere == ")" || caractere == "." || caractere == " " || caractere == "-")
        return true;
      else
        return false;
    }
  }
}

function testNumProd(texte, type, e)
{
  if (type == "mcperp" || type == "mcyear" || type == "mcproj" )
  {
    if (type == "mcperp")
    {
      if (document.order.mcyear.value != "" || document.order.mcproj.value != "")
        return false;
      else
      {
        if (carClavier(e).charCodeAt(0) == 8)
          return true;
        else
        {
          caractere = carClavier(e);

          if (!verif_non_vide(texte))
          {
            if (caractere >= "1" && caractere <= "9")
              return true;
            else
              return false;
          }
          else
          {
            if (caractere >= "0" && caractere <= "9")
              return true;
            else
              return false;
          }
        }
      }
    }
    else if (type == "mcyear")
    {
      if (document.order.mcperp.value != "" || document.order.mcproj.value != "")
        return false;
      else
      {
        if (carClavier(e).charCodeAt(0) == 8)
          return true;
        else
        {
          caractere = carClavier(e);

          if (!verif_non_vide(texte))
          {
            if (caractere >= "1" && caractere <= "9")
              return true;
            else
              return false;
          }
          else
          {
            if (caractere >= "0" && caractere <= "9")
              return true;
            else
              return false;
          }
        }
      }
    }
    else if (type == "mcproj" || type == "mcmonths")
    {
      if (document.order.mcperp.value != "" || document.order.mcyear.value != "")
        return false;
      else
      {
        if (carClavier(e).charCodeAt(0) == 8)
          return true;
        else
        {
          caractere = carClavier(e);

          if (!verif_non_vide(texte))
          {
            if (caractere >= "1" && caractere <= "9")
              return true;
            else
              return false;
          }
          else
          {
            if (caractere >= "0" && caractere <= "9")
              return true;
            else
              return false;
          }
        }
      }
    }
  }
  else if (type == "mpperp" || type == "mpyear")
  {
    if (type == "mpperp")
    {
      if (document.order.mpyear.value != "")
        return false;
      else
      {
        if (carClavier(e).charCodeAt(0) == 8)
          return true;
        else
        {
          caractere = carClavier(e);

          if (!verif_non_vide(texte))
          {
            if (caractere >= "1" && caractere <= "9")
              return true;
            else
              return false;
          }
          else
          {
            if (caractere >= "0" && caractere <= "9")
              return true;
            else
              return false;
          }
        }
      }
    }
    else if (type == "mpyear")
    {
      if (document.order.mpperp.value != "")
        return false;
      else
      {
        if (carClavier(e).charCodeAt(0) == 8)
          return true;
        else
        {
          caractere = carClavier(e);

          if (!verif_non_vide(texte))
          {
            if (caractere >= "1" && caractere <= "9")
              return true;
            else
              return false;
          }
          else
          {
            if (caractere >= "0" && caractere <= "9")
              return true;
            else
              return false;
          }
        }
      }
    }
  }
  else if (type == "prperp" || type == "pryear")
  {
    if (type == "prperp")
    {
      if (document.order.pryear.value != "")
        return false;
      else
      {
        if (carClavier(e).charCodeAt(0) == 8)
          return true;
        else
        {
          caractere = carClavier(e);

          if (!verif_non_vide(texte))
          {
            if (caractere >= "1" && caractere <= "9")
              return true;
            else
              return false;
          }
          else
          {
            if (caractere >= "0" && caractere <= "9")
              return true;
            else
              return false;
          }
        }
      }
    }
    else if (type == "pryear")
    {
      if (document.order.prperp.value != "")
        return false;
      else
      {
        if (carClavier(e).charCodeAt(0) == 8)
          return true;
        else
        {
          caractere = carClavier(e);

          if (!verif_non_vide(texte))
          {
            if (caractere >= "1" && caractere <= "9")
              return true;
            else
              return false;
          }
          else
          {
            if (caractere >= "0" && caractere <= "9")
              return true;
            else
              return false;
          }
        }
      }
    }
  }
  else if (type == "convperp")
  {
    if (type == "convperp")
    {
      if (carClavier(e).charCodeAt(0) == 8)
        return true;
      else
      {
        caractere = carClavier(e);

        if (!verif_non_vide(texte))
        {
          if (caractere >= "1" && caractere <= "9")
            return true;
          else
            return false;
        }
        else
        {
          if (caractere >= "0" && caractere <= "9")
            return true;
          else
            return false;
        }
      }
    }
  }
  /* else if (type == "mcmonths")
  {
    if (carClavier(e).charCodeAt(0) == 8)
      return true;
    else
    {
      caractere = carClavier(e);

      if (!verif_non_vide(texte))
      {
        if (caractere >= "1" && caractere <= "9")
          return true;
        else
          return false;
      }
      else
      {
        if (caractere >= "0" && caractere <= "9")
          return true;
        else
          return false;
      }
    }
  } */
}

function ouvrirCalendrier(typeDate)
{
  var largFen = 250;
  var hautFen = 230;
  var largeur = screen.width;
  var hauteur = screen.height;

  largeur = (largeur - largFen) / 2;
  hauteur = (hauteur - hautFen) / 2;

  fenCalendrier = window.open("calendrier.php?mode=init&typeDate=" + typeDate,
                              "fenCalendrier",
                              "width=" + largFen + ", height=" + hautFen + ", left=" + largeur + ", top=" + hauteur + ", scrollbars=0,toolbar=0, menuBar=0, location=0, directories=0, status=1, resizable=1, copyhistory=0");
}

function fermerCalendrier()
{
  if (typeof(fenCalendrier)=="object" && !fenCalendrier.closed)
    fenCalendrier.close();
}

function voirCalendrier()
{
  if (typeof(fenCalendrier)=="object" && fenCalendrier.closed==false)
    fenCalendrier.setCalendrier(document.resa.mois.value,
                                document.resa.annee.value);
}

function affichePrixCosmic(iPricePerpGBP,
                           iPricePerpEUR,
                           iPricePerpUSD,
                           iPriceYearGBP,
                           iPriceYearEUR,
                           iPriceYearUSD,
                           iPriceProjectGBP,
                           iPriceProjectEUR,
                           iPriceProjectUSD,
                           dRemiseProd)
{
  //alert("affichePrixCosmic");
  if (!verif_non_vide(document.order.mcperp.value))
  {
    valPerp = 0;
  }
  else
  {
    valPerp = parseInt(document.order.mcperp.value);
  }

  if (!verif_non_vide(document.order.mcyear.value))
  {
    valYear = 0;
  }
  else
  {
    valYear = parseInt(document.order.mcyear.value);
  }

  if (!verif_non_vide(document.order.mcproj.value))
  {
    valProj = 0;
    document.getElementById("duration").style.display = "none";
    document.order.startdate.value = "";
    //document.order.mcmonths.value = "";
  }
  else
  {
    valProj = parseInt(document.order.mcproj.value);
    document.getElementById("duration").style.display = "block";
  }

  sCurrency = document.order.currency.value;

  if (!verif_non_vide(document.order.mcmonths.value))
  {
    iNbMonths = 0;
  }
  else
  {
    iNbMonths = parseInt(document.order.mcmonths.value);
  }
  
  //alert("valPerp = " + valPerp);
  //alert("valYear = " + valYear);
  //alert("valProj = " + valProj);

  if ((valPerp == 0 && valYear == 0 && valProj == 0) || sCurrency == "OTH")
  {
    document.order.mcuprice.value = "";
    document.order.mcptot.value = "";
  }
  else
  {
    if (valPerp > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.mcuprice.value = iPricePerpGBP;
        document.order.mcptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.mcuprice.value = iPricePerpEUR;
        document.order.mcptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.mcuprice.value = iPricePerpUSD;
        document.order.mcptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
    }
    else if (valYear > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.mcuprice.value = iPriceYearGBP;
        document.order.mcptot.value = arrondi(calculTotalLigne(iPriceYearGBP,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.mcuprice.value = iPriceYearEUR;
        document.order.mcptot.value = arrondi(calculTotalLigne(iPriceYearEUR,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.mcuprice.value = iPriceYearUSD;
        document.order.mcptot.value = arrondi(calculTotalLigne(iPriceYearUSD,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
    }
    else if (valProj > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.mcuprice.value = iPriceProjectGBP;
        if (iNbMonths == 0)
          document.order.mcptot.value = "";
        else
          document.order.mcptot.value = arrondi(calculTotalLigne(iNbMonths * iPriceProjectGBP,
                                                                 valProj,
                                                                 dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.mcuprice.value = iPriceProjectEUR;
        if (iNbMonths == 0)
          document.order.mcptot.value = "";
        else
          document.order.mcptot.value = arrondi(calculTotalLigne(iNbMonths * iPriceProjectEUR,
                                                                 valProj,
                                                                 dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.mcuprice.value = iPriceProjectUSD;
        if (iNbMonths == 0)
          document.order.mcptot.value = "";
        else
          document.order.mcptot.value = arrondi(calculTotalLigne(iNbMonths * iPriceProjectUSD,
                                                                 valProj,
                                                                 dRemiseProd), 2);
      }
    }
  }
}

function affichePrixProject(iPricePerpGBP,
                            iPricePerpEUR,
                            iPricePerpUSD, 
                            iPriceYearGBP, 
                            iPriceYearEUR, 
                            iPriceYearUSD,
                            dRemiseProd)
{
  if (!verif_non_vide(document.order.mpperp.value))
  {
    valPerp = 0;
  }
  else
  {
    valPerp = parseInt(document.order.mpperp.value);
  }

  if (!verif_non_vide(document.order.mpyear.value))
  {
    valYear = 0;
  }
  else
  {
    valYear = parseInt(document.order.mpyear.value);
  }

  sCurrency = document.order.currency.value;

  if ((valPerp == 0 && valYear == 0) || sCurrency == "OTH")
  {
    document.order.mpuprice.value = "";
    document.order.mpptot.value = "";
  }
  else
  {
    if (valPerp > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.mpuprice.value = iPricePerpGBP;
        document.order.mpptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.mpuprice.value = iPricePerpEUR;
        document.order.mpptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.mpuprice.value = iPricePerpUSD;
        document.order.mpptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
    }
    else if (valYear > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.mpuprice.value = iPriceYearGBP;
        document.order.mpptot.value = arrondi(calculTotalLigne(iPriceYearGBP,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.mpuprice.value = iPriceYearEUR;
        document.order.mpptot.value = arrondi(calculTotalLigne(iPriceYearEUR,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.mpuprice.value = iPriceYearUSD;
        document.order.mpptot.value = arrondi(calculTotalLigne(iPriceYearUSD,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
    }
  }
}

function affichePrixPredict(iPricePerpGBP,
                            iPricePerpEUR,
                            iPricePerpUSD,
                            iPriceYearGBP,
                            iPriceYearEUR,
                            iPriceYearUSD,
                            dRemiseProd)
{
  if (!verif_non_vide(document.order.prperp.value))
  {
    valPerp = 0;
  }
  else
  {
    valPerp = parseInt(document.order.prperp.value);
  }

  if (!verif_non_vide(document.order.pryear.value))
  {
    valYear = 0;
  }
  else
  {
    valYear = parseInt(document.order.pryear.value);
  }

  sCurrency = document.order.currency.value;

  if ((valPerp == 0 && valYear == 0) || sCurrency == "OTH")
  {
    document.order.pruprice.value = "";
    document.order.prptot.value = "";
  }
  else
  {
    if (valPerp > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.pruprice.value = iPricePerpGBP;
        document.order.prptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.pruprice.value = iPricePerpEUR;
        document.order.prptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.pruprice.value = iPricePerpUSD;
        document.order.prptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                               valPerp,
                                                               dRemiseProd), 2);
      }
    }
    else if (valYear > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.pruprice.value = iPriceYearGBP;
        document.order.prptot.value = arrondi(calculTotalLigne(iPriceYearGBP,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.pruprice.value = iPriceYearEUR;
        document.order.prptot.value = arrondi(calculTotalLigne(iPriceYearEUR,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.pruprice.value = iPriceYearUSD;
        document.order.prptot.value = arrondi(calculTotalLigne(iPriceYearUSD,
                                                               valYear,
                                                               dRemiseProd), 2);
      }
    }
  }
}

function affichePrixConverter(iPricePerpGBP,
                              iPricePerpEUR,
                              iPricePerpUSD,
                              dRemiseProd)
{
  if (!verif_non_vide(document.order.convperp.value))
  {
    valPerp = 0;
  }
  else
  {
    valPerp = parseInt(document.order.convperp.value);
  }

  sCurrency = document.order.currency.value;

  if (valPerp == 0 || sCurrency == "OTH")
  {
    document.order.convuprice.value = "";
    document.order.convptot.value = "";
  }
  else
  {
    if (valPerp > 0)
    {
      if (sCurrency == "GBP")
      {
        document.order.convuprice.value = iPricePerpGBP;
        document.order.convptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
      }
      else if (sCurrency == "EUR")
      {
        document.order.convuprice.value = iPricePerpEUR;
        document.order.convptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
      }
      else if (sCurrency == "USD")
      {
        document.order.convuprice.value = iPricePerpUSD;
        document.order.convptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
      }
    }
  }
}

function calculTotalLigne(iUnitPrice,
                          iNb,
                          dRemiseProduit)
{
  var dTotal = 0;
  
  //200 * ((1 - (1 - 0.05) ^2) / 0.05)
  
  dTotal = 1 - (dRemiseProduit / 100);
  dTotal = Math.pow(dTotal, iNb);
  dTotal = 1 - dTotal;
  dTotal = dTotal / (dRemiseProduit / 100);
  dTotal = iUnitPrice * dTotal;

  return dTotal;
}

function calculPUCosmic(iPricePerpGBP,
                        iPricePerpEUR,
                        iPricePerpUSD,
                        iPriceYearGBP,
                        iPriceYearEUR,
                        iPriceYearUSD,
                        iPriceProjectGBP,
                        iPriceProjectEUR,
                        iPriceProjectUSD,
                        dRemiseProd)
{
  if (document.order.mcperp.value != "" || document.order.mcyear.value != "" || document.order.mcproj.value != "")
  {
    sCurrency = document.order.currency.value;

    if (sCurrency != "OTH")
    {
      if (document.order.mcperp.value != "")
      {
        valPerp = parseInt(document.order.mcperp.value);

        if (sCurrency == "GBP")
        {
          document.order.mcuprice.value = iPricePerpGBP;
          document.order.mcptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.mcuprice.value = iPricePerpEUR;
          document.order.mcptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.mcuprice.value = iPricePerpUSD;
          document.order.mcptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
      }
      else if (document.order.mcyear.value != "")
      {
        valYear = parseInt(document.order.mcyear.value);

        if (sCurrency == "GBP")
        {
          document.order.mcuprice.value = iPriceYearGBP;
          document.order.mcptot.value = arrondi(calculTotalLigne(iPriceYearGBP,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.mcuprice.value = iPriceYearEUR;
          document.order.mcptot.value = arrondi(calculTotalLigne(iPriceYearEUR,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.mcuprice.value = iPriceYearUSD;
          document.order.mcptot.value = arrondi(calculTotalLigne(iPriceYearUSD,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
      }
      else if (document.order.mcproj.value != "")
      {
        valProj = parseInt(document.order.mcproj.value);
        if (document.order.mcmonths.value == "")
          valMonth = 0;
        else
          valMonth = parseInt(document.order.mcmonths.value);

        if (sCurrency == "GBP")
        {
          document.order.mcuprice.value = iPriceProjectGBP;
          if (valMonth == 0)
            document.order.mcptot.value = "";
          else
            document.order.mcptot.value = arrondi(calculTotalLigne(iPriceProjectGBP,
                                                                   valProj * valMonth,
                                                                   dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.mcuprice.value = iPriceProjectEUR;
          if (valMonth == 0)
            document.order.mcptot.value = "";
          else
            document.order.mcptot.value = arrondi(calculTotalLigne(iPriceProjectEUR,
                                                                   valProj * valMonth,
                                                                   dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.mcuprice.value = iPriceProjectUSD;
          if (valMonth == 0)
            document.order.mcptot.value = "";
          else
            document.order.mcptot.value = arrondi(calculTotalLigne(iPriceProjectUSD,
                                                                   valProj * valMonth,
                                                                   dRemiseProd), 2);
        }
      }
    }
    else
    {
      document.order.mcuprice.value = "";
      document.order.mcptot.value = "";
    }
  }
}

function calculPUProject(iPricePerpGBP,
                         iPricePerpEUR, 
                         iPricePerpUSD, 
                         iPriceYearGBP, 
                         iPriceYearEUR, 
                         iPriceYearUSD,
                         dRemiseProd)
{
  if (document.order.mpperp.value != "" || document.order.mpyear.value != "")
  {
    sCurrency = document.order.currency.value;

    if (sCurrency != "OTH")
    {
      if (document.order.mpperp.value != "")
      {
        valPerp = parseInt(document.order.mpperp.value);

        if (sCurrency == "GBP")
        {
          document.order.mpuprice.value = iPricePerpGBP;
          document.order.mpptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.mpuprice.value = iPricePerpEUR;
          document.order.mpptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.mpuprice.value = iPricePerpUSD;
          document.order.mpptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
      }
      else if (document.order.mpyear.value != "")
      {
        valYear = parseInt(document.order.mpyear.value);

        if (sCurrency == "GBP")
        {
          document.order.mpuprice.value = iPriceYearGBP;
          document.order.mpptot.value = arrondi(calculTotalLigne(iPriceYearGBP,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.mpuprice.value = iPriceYearEUR;
          document.order.mpptot.value = arrondi(calculTotalLigne(iPriceYearEUR,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.mpuprice.value = iPriceYearUSD;
          document.order.mpptot.value = arrondi(calculTotalLigne(iPriceYearUSD,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
      }
    }
    else
    {
      document.order.mpuprice.value = "";
      document.order.mpptot.value = "";
    }
  }
}

function calculPUPredict(iPricePerpGBP,
                         iPricePerpEUR,
                         iPricePerpUSD,
                         iPriceYearGBP,
                         iPriceYearEUR,
                         iPriceYearUSD,
                         dRemiseProd)
{
  if (document.order.prperp.value != "" || document.order.pryear.value != "")
  {
    sCurrency = document.order.currency.value;

    if (sCurrency != "OTH")
    {
      if (document.order.prperp.value != "")
      {
        valPerp = parseInt(document.order.prperp.value);

        if (sCurrency == "GBP")
        {
          document.order.pruprice.value = iPricePerpGBP;
          document.order.prptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.pruprice.value = iPricePerpEUR;
          document.order.prptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.pruprice.value = iPricePerpUSD;
          document.order.prptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                                 valPerp,
                                                                 dRemiseProd), 2);
        }
      }
      else if (document.order.pryear.value != "")
      {
        valYear = parseInt(document.order.pryear.value);

        if (sCurrency == "GBP")
        {
          document.order.pruprice.value = iPriceYearGBP;
          document.order.prptot.value = arrondi(calculTotalLigne(iPriceYearGBP,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.pruprice.value = iPriceYearEUR;
          document.order.prptot.value = arrondi(calculTotalLigne(iPriceYearEUR,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.pruprice.value = iPriceYearUSD;
          document.order.prptot.value = arrondi(calculTotalLigne(iPriceYearUSD,
                                                                 valYear,
                                                                 dRemiseProd), 2);
        }
      }
    }
    else
    {
      document.order.pruprice.value = "";
      document.order.prptot.value = "";
    }
  }
}

function calculPUConverter(iPricePerpGBP,
                           iPricePerpEUR,
                           iPricePerpUSD,
                           dRemiseProd)
{
  if (document.order.convperp.value != "")
  {
    sCurrency = document.order.currency.value;

    if (sCurrency != "OTH")
    {
      if (document.order.convperp.value != "")
      {
        valPerp = parseInt(document.order.convperp.value);

        if (sCurrency == "GBP")
        {
          document.order.convuprice.value = iPricePerpGBP;
          document.order.convptot.value = arrondi(calculTotalLigne(iPricePerpGBP,
                                                                   valPerp,
                                                                   dRemiseProd), 2);
        }
        else if (sCurrency == "EUR")
        {
          document.order.convuprice.value = iPricePerpEUR;
          document.order.convptot.value = arrondi(calculTotalLigne(iPricePerpEUR,
                                                                   valPerp,
                                                                   dRemiseProd), 2);
        }
        else if (sCurrency == "USD")
        {
          document.order.convuprice.value = iPricePerpUSD;
          document.order.convptot.value = arrondi(calculTotalLigne(iPricePerpUSD,
                                                                   valPerp,
                                                                   dRemiseProd), 2);
        }
      }
    }
    else
    {
      document.order.convuprice.value = "";
      document.order.convptot.value = "";
    }
  }
}

function calculShipping(iShippingCurrencyGBPOut, 
                        iShippingCurrencyEUROut, 
                        iShippingCurrencyUSDOut, 
                        iShippingCurrencyGBPIn, 
                        sDeliverySh,
                        sCurrency)
{
  var sShipping = "0";

  if (sDeliverySh == "Choice1")
    sShipping = "0";
  else if (sDeliverySh == "Choice2")
  {
    if (sCurrency == "GBP")
      sShipping = iShippingCurrencyGBPOut.toString();
    else if (sCurrency == "EUR")
      sShipping = iShippingCurrencyEUROut.toString();
    else if (sCurrency == "USD")
      sShipping = iShippingCurrencyUSDOut.toString();
    else if (sCurrency == "OTH")
      sShipping = "0";
  }
  else if (sDeliverySh == "Choice3")
  {
    if (sCurrency == "GBP")
      sShipping = iShippingCurrencyGBPIn.toString();
    else if (sCurrency == "OTH")
      sShipping = "0";
    else
    {
      alert("Please, for this shipping you have to choose absolutely the GBP currency !!");
      sShipping = "n/a";
    }
  }
  
  if (sShipping != "n/a")
    sShipping = arrondi(parseFloat(sShipping.replace(",", "")), 2);

  return sShipping;
}

function calculTotal(sMcptot, sMpptot, sPrptot, sConvptot, sShipping, sRemiseVAT)
{
  var iSomme = 0;
  var sGrandTotal = "0.00";

  if (verif_non_vide(sMcptot))
    iSomme = iSomme + parseFloat(sMcptot.replace(",", ""));
  
  if (verif_non_vide(sMpptot))
    iSomme = iSomme + parseFloat(sMpptot.replace(",", ""));

  if (verif_non_vide(sPrptot))
    iSomme = iSomme + parseFloat(sPrptot.replace(",", ""));
  
  if (verif_non_vide(sConvptot))
    iSomme = iSomme + parseFloat(sConvptot.replace(",", ""));

  if (verif_non_vide(sShipping) && sShipping != "n/a")
    iSomme = iSomme + parseFloat(sShipping.replace(",", ""));
  
  if (verif_non_vide(sRemiseVAT))
    iSomme = iSomme + parseFloat(sRemiseVAT.replace(",", ""));

  if (iSomme != 0)
    sGrandTotal = arrondi(iSomme, 2);
  
  return sGrandTotal;
}

function calculCurrency2(sCurrency)
{
  var sCurrency2 = "";

  if (sCurrency == "GBP")
    sCurrency2 = sCurrency + "(£)";
  else if (sCurrency == "EUR")
    sCurrency2 = sCurrency + "(€)";
  else if (sCurrency == "USD")
    sCurrency2 = sCurrency + "($)";
  
  return sCurrency2;
}

function calculVAT(dRemiseVAT, sMcptot, sMpptot, sPrptot, sConvptot, sShipping)
{
  var iSomme = 0;
  var sVattot = "";

  if (verif_non_vide(sMcptot))
    iSomme = iSomme + parseFloat(sMcptot.replace(",", ""));

  if (verif_non_vide(sMpptot))
    iSomme = iSomme + parseFloat(sMpptot.replace(",", ""));

  if (verif_non_vide(sPrptot))
    iSomme = iSomme + parseFloat(sPrptot.replace(",", ""));
    
  if (verif_non_vide(sConvptot))
    iSomme = iSomme + parseFloat(sConvptot.replace(",", ""));

  if (verif_non_vide(sShipping) && sShipping != "n/a")
    iSomme = iSomme + parseFloat(sShipping.replace(",", ""));

  if (iSomme != 0)
  {
    iSomme = (iSomme * dRemiseVAT) / 100;
    sVattot = arrondi(iSomme, 2);
  }

  return sVattot;
}

function arrondi(iNombre, iNbDec)
{
  var sNombre = iNombre.toString();
  var iPosDec = sNombre.indexOf(".");

  if (iPosDec == -1)
  {
    if (iNbDec > 0)
    {
      sNombre = sNombre + ".";
      for (cpt = 1; cpt <= iNbDec; cpt++)
        sNombre = sNombre + "0";
    }
  }
  else
  {
    var sEntier = sNombre.substr(0, iPosDec);
    var sDec = sNombre.substr(iPosDec + 1, sNombre.length - iPosDec - 1);

    var iLgDec = sDec.length;

    if (iLgDec < iNbDec)
      for (cpt = iLgDec + 1; cpt <= iNbDec; cpt++)
        sDec = sDec + "0";
    else if (iLgDec > iNbDec)
    {
      cpt = iLgDec;
      while (cpt > iNbDec)
      {
        var iValeur = parseInt(sDec.substr(sDec.length - 1, 1));

        if (iValeur >= 0 && iValeur <= 5)
          sDec = sDec.substr(0, sDec.length - 1);
        else if (iValeur >= 6 && iValeur <= 9)
        {
          sDec = sDec.substr(0, sDec.length - 1);
          iNewDec = parseInt(sDec) + 1;
          sNewDec = iNewDec.toString();

          if (sNewDec.length != sDec.length)
          {
            sDec = "00";
            iEntier = parseInt(sEntier) + 1;
            sEntier = iEntier.toString();
          }
          else
            sDec = sNewDec;

        }
        cpt = sDec.length;
      }
    }

    iCpt = sEntier.length - 1;
    iCptDigitEntier = 1;
    sEntierTp = "";
    while (iCpt >= 0)
    {
      sEntierTp = sEntier.substr(iCpt, 1) + sEntierTp;
      if (iCptDigitEntier % 3 == 0)
        if (iCpt > 0)
          sEntierTp = "," + sEntierTp;

      iCpt--;
      iCptDigitEntier++;
    }

    sNombre = sEntierTp + "." + sDec;
  }

  return sNombre;
}

function focusMachineIdCosmic(sDefault)
{
  if (document.order.mcmids.value == sDefault)
    document.order.mcmids.value = "";
}

function focusMachineIdProject(sDefault)
{
  if (document.order.mpmids.value == sDefault)
    document.order.mpmids.value = "";
}

function focusMachineIdPredictIt(sDefault)
{
  if (document.order.prmids.value == sDefault)
    document.order.prmids.value = "";
}

function focusMachineIdConverter(sDefault)
{
  if (document.order.convmids.value == sDefault)
    document.order.convmids.value = "";
}