﻿var iex = (document.all); // IE
var nn4 = (document.layers); // Netscape 4

scrollingDiv = 'DivPanier';
currentlyScrolling = false;
positionFromTop = 10;
footerHeight = 95;

function findPosY (_div)
{
    var divtop = 0;
    if (_div.offsetParent)
    {
        // Ajoute les top de tous les parents
        while (_div.offsetParent)
        {
            divtop += _div.offsetTop
            _div = _div.offsetParent;
        }
    }
    else if (_div.y)
        divtop += _div.y;
    return divtop;
}

function scrollDivToTarget (_targetY)
{
    //alert('scroll');
    //alert(_targetY);
    currentlyScrolling = true;
    var div = iex ? document.all[scrollingDiv] : document.getElementById (scrollingDiv);
    var styleelt = nn4 ? document.layers[scrollingDiv] : (iex ? document.all[scrollingDiv].style : document.getElementById (scrollingDiv).style);
    var pas = 0;

    if (typeof (styleelt.setAttribute) == "undefined")
    {
        divpos = parseFloat (styleelt.paddingTop);
        pas = (_targetY - divpos) * 0.5;
        if (Math.abs(divpos) < 100)
            pas = (_targetY - divpos) * 0.15;
        styleelt.paddingTop = (divpos + pas) + "px";
    }
    else
    {
        if (styleelt.getAttribute ("padding-top") == null)
            divpos = parseFloat (styleelt.paddingTop);
        else
            divpos = parseFloat (styleelt.getAttribute ("padding-top"));
        pas = (_targetY - divpos) * 0.5;
        if (Math.abs (divpos) < 100)
            pas = (_targetY - divpos) * 0.15;
        styleelt.setAttribute ("padding-top", (divpos + pas) + "px");
        styleelt.setAttribute ("paddingTop", (divpos + pas) + "px");
    }
    nextScroll = setTimeout ('scrollDivToTarget ('+_targetY+');', 10);
    if (pas < 0.05 && pas > -0.05)
    {
        currentlyScrolling = false;
        clearTimeout (nextScroll);
    }
}

function scrollDiv (noLoop)
{
  if ( arguments.length == 0 )
    noLoop = false;
  var div = iex ? document.all[scrollingDiv] : document.getElementById (scrollingDiv);
  var styleelt = nn4 ? document.layers[scrollingDiv] : (iex ? document.all[scrollingDiv].style : document.getElementById (scrollingDiv).style);
  var scroll = (window.pageYOffset) ? window.pageYOffset : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
  var divpos = findPosY (div);

  if (typeof (styleelt.getAttribute) == "undefined")
    divpadding = parseFloat (styleelt.paddingTop);
  else
  {
    if (styleelt.getAttribute ("padding-top") == null)
      divpadding = parseFloat (styleelt.paddingTop);
    else
      divpadding = parseFloat (styleelt.getAttribute ("padding-top"));
  }
  var divsize = div.offsetHeight - divpadding + positionFromTop + footerHeight;
  var windowheight = document.documentElement.clientHeight;

  if (scroll < div.startPos)
      scroll = div.startPos;
          
  if (divpos != scroll)
  {
    scroll = (scroll < divpos) ? 0 : (scroll - divpos + positionFromTop);
    if (currentlyScrolling == false)
    {
      //alert(divsize)
      //alert(windowheight)
      // Si le div est plus haut que la page, on ne scrolle pas
      if (divsize > windowheight)
      {
        if (divpadding > positionFromTop && scroll < divpadding)
          scrollDivToTarget (scroll);
      }
      else
      {
        scrollDivToTarget (scroll);
      }
    }
  }
  if ( !noLoop )
    setTimeout ('scrollDiv ();', 20);
}

//Panier
function AfficheArticles(Article1, Article2, Article3) {
    AfficheArticle(Article1)
    AfficheArticle(Article2)
    AfficheArticle(Article3)
}

function SupprimeArticles(Article1, Article2, Article3) {
    SupprimeArticle(Article1)
    SupprimeArticle(Article2)
    SupprimeArticle(Article3)
}

function AfficheArticle(Article) {
    if (typeof (Article) != 'undefined' && Article != '') {
        //alert(Article)
        Afficher('Panier' + Article);
        AddArticle(Article)
        VerifDivSupPanier();
    }
}

function SupprimeArticle(Article) {
    if (typeof (Article) != 'undefined' && Article != '') {
        Cacher('Panier' + Article);
        SupArticle(Article)
        VerifDivSupPanier();
    }
}

function Remplace(expr, a, b) {
    var i = 0
    while (i != -1) {
        i = expr.indexOf(a, i);
        if (i >= 0) {
            expr = expr.substring(0, i) + b + expr.substring(i + a.length);
            i += b.length;
        }
    }
    return expr
}

function AddArticle(Article) {
    Panier = document.getElementById(HidPanier).value;
    if (Panier.indexOf("," + Article, 0) < 0) {
        document.getElementById(HidPanier).value = Panier + "," + Article;
    }
}

function SupArticle(Article) {
    document.getElementById(HidPanier).value = Remplace(document.getElementById(HidPanier).value, "," + Article, "");
}

function fcGetPeriode(IdPanier) {
    for (var j = 0; j < arrPeriode.length; j++) {
        if (arrPeriode[j][0] == IdPanier) {
            if (arrPeriode[j][1] == 3)
                return 1;
            else
                return arrPeriode[j][1];
        }
    }
    return 0;
}

function fcGetPrix(IdPanier) {
    for (var j = 0; j < arrPeriode.length; j++) {
        if (arrPeriode[j][0] == IdPanier) {
            return arrPeriode[j][2];
        }
    }
    return 0;
}

function RemplirPanier() {
    if (typeof (document.getElementById(HidPanier)) != 'undefined') {
        var chaine = document.getElementById(HidPanier).value;
        if (chaine != "") {
            var tableau = chaine.split(',');
            document.getElementById(HidPanier).value = "";
            for (var i = 0; i < tableau.length; i++) {
                AfficheArticle(tableau[i]);

                if (tableau[i] == "25" || tableau[i] == "78") {
                    if (document.getElementById('DivPrixIpAdsl1') != null) {
                        Cacher('DivPrixIpAdsl1');
                        Afficher('DivPrixIpAdsl2');
                        Cacher('DivPrixIpAdsl3');
                    }
                    if (document.getElementById('DivPrixIpLight1') != null) {
                        Afficher('DivPrixIpLight1');
                        Cacher('DivPrixIpLight2');
                    }
                }
                if (tableau[i] == "29" || tableau[i] == "30") {
                    if (document.getElementById('DivPrixIpAdsl1') != null) {
                        Cacher('DivPrixIpAdsl1');
                        Cacher('DivPrixIpAdsl2');
                        Afficher('DivPrixIpAdsl3');
                    }
                }
            }
        }
    }

    if (HidDateActivation != '') {
        if (document.getElementById(HidDateActivation).value != "") {

            var MyDate = new Date();
            MyDate.setTime(document.getElementById(HidDateActivation).value);
            DateActivation(MyDate);
        }
        ManagePortage();
    }
}

function VerifDivSupPanier() {
    var chaine = document.getElementById(HidPanier).value;
    if (chaine != "") {
        var reg = new RegExp("[,]+", "g");
        var tableau = chaine.split(reg);
        var DivOptionsOk = false;
        var Div1ereFactureOk = false;
        PrixUnique = 0;
        PrixMensuel = 0;
        for (var i = 0; i < tableau.length; i++) {
            Periode = fcGetPeriode(tableau[i]);
            Prix = fcGetPrix(tableau[i]);
            if (Periode == 0) {
                PrixUnique = PrixUnique + Prix;
                Div1ereFactureOk = true;
            }
            if (Periode == 1)
                DivOptionsOk = true;
            if (Periode == 1 || Periode == 99)
                PrixMensuel = PrixMensuel + Prix;
            if (Periode == 99)
                Cacher('DivNoAbo');
        }
        if (DivOptionsOk == true)
            Afficher('DivOptions');
        else
            Cacher('DivOptions');

        if (Div1ereFactureOk == true) {
            Afficher('Div1ereFacture');
            Afficher('Div1ereFactureTotal');
        }
        else {
            Cacher('Div1ereFacture');
            Cacher('Div1ereFactureTotal');
        }

        if (PrixUnique < 0)
            PrixUnique = 0;
        if (PrixMensuel < 0)
            PrixMensuel = 0;

        document.getElementById('DivPrixMensuel').innerHTML = "Fr. " + PrixMensuel + ".—";
        document.getElementById('DivPrixMensuel2').innerHTML = "Fr. " + PrixMensuel + ".—";
        document.getElementById('DivPrix1ereFacture').innerHTML = "Fr. " + PrixUnique + ".—";
    }

    //Projet Surf & Phone : Verif données cohérentes + Ajout ticks verts
    if (document.getElementById('radSurf1M')) {
        Cacher('DivSurf2M');
        Cacher('DivSurf10M');
        Cacher('DivSurf12M');
        Cacher('DivSurf20M');
        Cacher('DivVoice10M');
        Cacher('DivVoice12M');
        Cacher('DivVoice20M');
        ShowDiv('radSurf2M', 'DivSurf2M');
        ShowDiv('radSurf10M', 'DivSurf10M');
        ShowDiv('radSurf12M', 'DivSurf12M');
        ShowDiv('radSurf20M', 'DivSurf20M');
        ShowDiv('ChkVoice10M', 'DivVoice10M');
        ShowDiv('ChkVoice12M', 'DivVoice12M');
        ShowDiv('ChkVoice20M', 'DivVoice20M');
    }
}

function ShowDiv(ObjToVerify, ObjToShow) {
    if (document.getElementById(ObjToVerify).checked == true) {
        Afficher(ObjToShow);
    }
}
