var ua = navigator.userAgent.toLowerCase();
var divw = 0;
var divh = 0;

if (document.getElementById || document.all)
    document.write('<div id="imgtrailer" style="z-index:10000;display:inline-block;position:absolute;visibility:hidden;"></div>')

function gettrailobject() {
    if (document.getElementById)
        return document.getElementById("imgtrailer")
    else if (document.all)
        return document.all.trailimagid
}

function Confirmation()
{
    if (confirm('Etes-vous sūr de vouloir supprimer la commande ?')) {

    }
    else {
        return false;
    }
}

function gettrailobj() {
    if (document.getElementById)
        return document.getElementById("imgtrailer").style
    else if (document.all)
        return document.all.trailimagid.style
}

function recherche(chaine, texte) {
    var reg = new RegExp("(" + chaine + ")", "g");
    texte = texte.replace(reg, "<SPAN style='background-color=yellow'>" + chaine + "</SPAN></FONT>");
}

function truebody() {
//    if (!window.opera && !window.chrome) {
//        if (!window.safari) {
//            return (document.documentElement);
//        }
//        else { return window; }
//    }
//    else {return document.body; }
    return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
    //return (!window.opera && !window.safari && !window.chrome) ? document.documentElement : document.body
}


function hidetrail() {
    document.onmousemove = '';
    gettrailobj().visibility = "hidden";
}

function trailOn(thumbimg, imgtitle, imgscription, imgsize, filesize, credit, level, thw, thh, bb) {
    if (ua.indexOf('opera') == -1 && ua.indexOf('safari') == -1) {
        maxwidth = credit;     // maximum image width.
        maxheight = credit;    // maximum image height.

        // height
        if (thh > maxheight) {
            thh1 = maxheight;
            reduch = thh1 / thh;
            thw1 = thw * reduch;
        }
        else {
            thh1 = thh;
            thw1 = thw;
        }
        // width
        if (maxwidth > 0) {
            if (thw1 > maxwidth) {
                thw1 = maxwidth;
                reducw = thw1 / thw;
                thh1 = thh * reducw;
            }
            if (thh1 > maxheight) {
                thh1 = maxheight;
                reduch = thh1 / thh;
                thw1 = thw * reduch;
            }
        }

        thw = thw1;
        thh = thh1;

        if (bb != 'bold') {
            bb1 = "";
            bb2 = "";
            pad = 0;
            load = "zzz";
            padd = 0;
        }
        else {
            bb1 = "<b>";
            bb2 = "</b>";
            pad = 3;
            load = "loader";
            padd = 5;
        }
        gettrailobj().left = "-500px";
        divthw = parseInt(thw) + 2;
        gettrailobject().innerHTML = '<div style="z-index:10000;"><table><tr><td align="center"><div style="background-color: #FFFFFF; layer-background-color: #CC0000; border: 1px solid #999999; padding:' + padd + 'px; width:' + divthw + 'px;"><div style="background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px solid #FFFFFF; background-image: url(../Images/' + load + '.gif);"><img src="' + thumbimg + '" border="0" width="' + thw + '" height="' + thh + '"><div align="center" style="padding:' + pad + 'px;">' + bb1 + ' ' + imgtitle + ' ' + bb2 + '</div></div></div></td></tr></table></div>';
        gettrailobj().visibility = "visible";
        gettrailobj().display = "block";
        divw = parseInt(thw) + 25;
        divh = parseInt(thh) + 130;
        document.onmousemove = followmouse;
    }
}

function trailOn2(thumbimg, imgtitle, imgscription, imgsize, filesize, credit, level, IMG, bb) {
    maxwidth = 350;     // maximum image width.
    maxheight = 350;    // maximum image height.

    GetSizeImage(IMG, 300, 300);

    //        // height
    //        if (thh > maxheight) {
    //            thh1 = maxheight;
    //            reduch = thh1 / thh;
    //            thw1 = thw * reduch;
    //        }
    //        else {
    //            thh1 = thh;
    //            thw1 = thw;
    //        }
    //        // width
    //        if (maxwidth > 0) {
    //            if (thw1 > maxwidth) {
    //                thw1 = maxwidth;
    //                reducw = thw1 / thw;
    //                thh1 = thh * reducw;
    //            }
    //            if (thh1 > maxheight) {
    //                thh1 = maxheight;
    //                reduch = thh1 / thh;
    //                thw1 = thw * reduch;
    //            }
    //        }

    //        thw = thw1;
    //        thh = thh1;

    if (bb != 'bold') {
        bb1 = "";
        bb2 = "";
        pad = 0;
        load = "zzz";
        padd = 0;
    }
    else {
        bb1 = "<b>";
        bb2 = "</b>";
        pad = 3;
        load = "loader";
        padd = 5;
    }
    gettrailobj().left = "-500px";
    divthw = parseInt(thw) + 2;
    gettrailobject().innerHTML = '<div style="z-index:10000;"><table><tr><td align="center"><div style="background-color: #FFFFFF; layer-background-color: #CC0000; border: 1px solid #999999; padding:' + padd + 'px; width:' + divthw + 'px;"><div style="background-color: #FFFFFF; layer-background-color: #FFFFFF; border: 1px solid #FFFFFF; background-image: url(../Images/' + load + '.gif);"><img src="' + thumbimg + '" border="0" width="' + thw + '" height="' + thh + '"><div align="center" style="padding:' + pad + 'px;">' + bb1 + ' ' + imgtitle + ' ' + bb2 + '</div></div></div></td></tr></table></div>';
    gettrailobj().visibility = "visible";
    divw = parseInt(thw) + 25;
    divh = parseInt(thh) + 130;
    document.onmousemove = followmouse;
}

function chiffres(event) {
    // Compatibilité IE / Firefox
    if (!event && window.event) {
        event = window.event;
    }
    // IE
    if (event.keyCode < 48 || event.keyCode > 57) {
        event.returnValue = false;
        event.cancelBubble = true;
    }
    // DOM
    if (event.which < 48 || event.which > 57) {
        event.preventDefault();
        event.stopPropagation();
    }
}

function followmouse(e) {
    //var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
    //var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight)

    var NomNav = window.navigator.appName;
    var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
    if (NomNav == "Microsoft Internet Explorer") {
        var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight)
    }
    else {
        if (!window.chrome && ua.indexOf('opera') == -1 && ua.indexOf('safari') == -1) {
            var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(document.body.offsetHeight, window.innerHeight)
        }
        else {
            var docheight = document.all ? truebody().scrollHeight + truebody().clientHeight : pageYOffset + window.innerHeight - 15
        }
    }
    
    if (typeof e != "undefined") {
        if (docwidth < 15 + e.pageX + divw)
            xcoord = e.pageX - divw - 5;
        else
            xcoord = 15 + e.pageX;
        if (docheight < 15 + e.pageY + divh)
            ycoord = 15 + e.pageY - Math.max(0, (divh + e.pageY - docheight - truebody().scrollTop - 30));
        else
            ycoord = 15 + e.pageY;
    }
    else if (typeof window.event != "undefined") {
        if (docwidth < 15 + truebody().scrollLeft + event.clientX + divw)
            xcoord = truebody().scrollLeft - 5 + event.clientX - divw;
        else
            xcoord = truebody().scrollLeft + 15 + event.clientX;

        if (docheight < 15 + truebody().scrollTop + event.clientY + divh)
            ycoord = 15 + truebody().scrollTop + event.clientY - Math.max(0, (divh + event.clientY - docheight - 30));
        else
            ycoord = truebody().scrollTop + 15 + event.clientY;
    }
    gettrailobj().left = xcoord + "px"
    gettrailobj().top = ycoord + "px"
}

function redimImage(IMG, inImg, inMW, inMH) {
    // Cette function recoit 3 parametres
    // inImg : Chemin relatif de l'image
    // inMW  : Largeur maximale
    // inMH   : Hauteur maximale
    var maxWidth = inMW;
    var maxHeight = inMH;
    // Declarations des variables "Nouvelle Taille"
    var dW = 0;
    var dH = 0;
    // Declaration d'un objet Image
    var oImg = new Image();
    // Affectation du chemin de l'image a l'objet
    oImg.src = inImg;
    // On recupere les tailles reelles
    var h = dH = oImg.height;
    var w = dW = oImg.width;
    // Si la largeur ou la hauteur depasse la taille maximale
    if ((h >= maxHeight) || (w >= maxWidth)) {
        // Si la largeur et la hauteur depasse la taille maximale
        if ((h >= maxHeight) && (w >= maxWidth)) {
            // On cherche la plus grande valeur
            if (h > w) {
                dH = maxHeight;
                // On recalcule la taille proportionnellement
                dW = parseInt((w * dH) / h, 10);
            } else {
                dW = maxWidth;
                // On recalcule la taille proportionnellement
                dH = parseInt((h * dW) / w, 10);
            }
        } else if ((h > maxHeight) && (w < maxWidth)) {
            // Si la hauteur depasse la taille maximale
            dH = maxHeight;
            // On recalcule la taille proportionnellement
            dW = parseInt((w * dH) / h, 10);
        } else if ((h < maxHeight) && (w > maxWidth)) {
            // Si la largeur depasse la taille maximale
            dW = maxWidth;
            // On recalcule la taille proportionnellement
            dH = parseInt((h * dW) / w, 10);
        }
    }
    // On ecrit l'image dans le document
    document.getElementById(IMG).style.width = dW;
    document.getElementById(IMG).style.height = dH;
    //document.getElementById(IMG)
    //IMG.writeln("<img src=\"" + inImg + "\" width=\"" + dW + "\" height=\"" + dH + "\" border=\"0\">");
};

function GetSizeImage(IMG, inMW, inMH) {
    // Cette function recoit 3 parametres
    // inImg : Chemin relatif de l'image
    // inMW  : Largeur maximale
    // inMH   : Hauteur maximale
    var maxWidth = inMW;
    var maxHeight = inMH;
    // Declarations des variables "Nouvelle Taille"
    var dW = 0;
    var dH = 0;
    // Declaration d'un objet Image
    var h = dH = document.getElementById(IMG).height;
    var w = dW = document.getElementById(IMG).width;

    dH = maxHeight;
    // On recalcule la taille proportionnellement
    dW = parseInt((w * dH) / h, 10);

    // Si la largeur ou la hauteur depasse la taille maximale
    //    if ((h >= maxHeight) || (w >= maxWidth)) {
    //        // Si la largeur et la hauteur depasse la taille maximale
    //        if ((h >= maxHeight) && (w >= maxWidth)) {
    //            // On cherche la plus grande valeur
    //            if (h > w) {
    //                dH = maxHeight;
    //                // On recalcule la taille proportionnellement
    //                dW = parseInt((w * dH) / h, 10);
    //            } else {
    //                dW = maxWidth;
    //                // On recalcule la taille proportionnellement
    //                dH = parseInt((h * dW) / w, 10);
    //            }
    //        } else if ((h > maxHeight) && (w < maxWidth)) {
    //            // Si la hauteur depasse la taille maximale
    //            dH = maxHeight;
    //            // On recalcule la taille proportionnellement
    //            dW = parseInt((w * dH) / h, 10);
    //        } else if ((h < maxHeight) && (w > maxWidth)) {
    //            // Si la largeur depasse la taille maximale
    //            dW = maxWidth;
    //            // On recalcule la taille proportionnellement
    //            dH = parseInt((h * dW) / w, 10);
    //        }
    //    }
    // On ecrit l'image dans le document
    thh = dH;
    thw = dW;
    //document.getElementById(IMG)
    //IMG.writeln("<img src=\"" + inImg + "\" width=\"" + dW + "\" height=\"" + dH + "\" border=\"0\">");
};

function scaleIt(IMG, v) {
    floorSize = .26;
    ceilingSize = 1.0;
    v = floorSize + (v * (ceilingSize - floorSize));

    document.getElementById(IMG).style.height = document.getElementById('scale-image').style.height + 'px';
    //document.getElementById(IMG).style.height = (v * 130) + 'px';

};

function redimImageTest(IMG, inMW, inMH) {
    // Cette function recoit 3 parametres
    // inImg : Chemin relatif de l'image
    // inMW  : Largeur maximale
    // inMH   : Hauteur maximale
    var maxWidth = inMW;
    var maxHeight = inMH;
    // Declarations des variables "Nouvelle Taille"
    var dW = 0;
    var dH = 0;
    // Declaration d'un objet Image
    // On recupere les tailles reelles
    var h = dH = document.getElementById(IMG).style.height;
    var w = dW = document.getElementById(IMG).style.width;
    // Si la largeur ou la hauteur depasse la taille maximale
    if ((h >= maxHeight) || (w >= maxWidth)) {
        // Si la largeur et la hauteur depasse la taille maximale
        if ((h >= maxHeight) && (w >= maxWidth)) {
            // On cherche la plus grande valeur
            if (h > w) {
                dH = maxHeight;
                // On recalcule la taille proportionnellement
                dW = parseInt((w * dH) / h, 10);
            } else {
                dW = maxWidth;
                // On recalcule la taille proportionnellement
                dH = parseInt((h * dW) / w, 10);
            }
        } else if ((h > maxHeight) && (w < maxWidth)) {
            // Si la hauteur depasse la taille maximale
            dH = maxHeight;
            // On recalcule la taille proportionnellement
            dW = parseInt((w * dH) / h, 10);
        } else if ((h < maxHeight) && (w > maxWidth)) {
            // Si la largeur depasse la taille maximale
            dW = maxWidth;
            // On recalcule la taille proportionnellement
            dH = parseInt((h * dW) / w, 10);
        }
    }
    // On ecrit l'image dans le document
    document.getElementById(IMG).style.width = dW;
    document.getElementById(IMG).style.height = dH;
    //document.getElementById(IMG)
    //IMG.writeln("<img src=\"" + inImg + "\" width=\"" + dW + "\" height=\"" + dH + "\" border=\"0\">");
};

function PutFraisChronopost(CHK_CHRONO, MontantFrais, MontantTotale, MontantFraisAPayer) {
    var dcMontant = 0;
    var reg1 = new RegExp(".", "g");
    var reg2 = new RegExp(",", "g");
    
    if (document.getElementById(CHK_CHRONO).checked == true) {
        document.getElementById(MontantFraisAPayer).innerText = document.getElementById(MontantFrais).innerText;
        dcMontant = (parseFloat(document.getElementById(MontantTotale).innerText.replace(reg2, ".")) + parseFloat(document.getElementById(MontantFrais).innerText.replace(reg2, ".")));
        document.getElementById(MontantTotale).innerText = dcMontant;
        document.getElementById(MontantTotale).innerText = document.getElementById(MontantTotale).innerText.replace(".", ",");
    }
    else {
        document.getElementById(MontantFraisAPayer).innerText = "0,00";
        dcMontant = (parseFloat(document.getElementById(MontantTotale).innerText.replace(reg2, ".")) - parseFloat(document.getElementById(MontantFrais).innerText.replace(reg2, ".")));
        document.getElementById(MontantTotale).innerText = dcMontant;
        document.getElementById(MontantTotale).innerText = document.getElementById(MontantTotale).innerText.replace(".", ",");
    }
}
