﻿/***************************************/
/*                Popups               */
/***************************************/

var PopUpIsOpen = false

function PopupAlert(Mess) {
    SetPopupPos(360, 100)

    Mess += "<br /><div onclick='ClosePopup()' style='cursor:pointer;margin-top:24px;margin-left:16px'><img alt='Fermer' src='clients/inc/images/go-jump_32.png'> Revenir à la page</div>"
    document.getElementById("PopupBody").innerHTML = Mess
}

function showPopup(width, height, url) {
    SetPopupPos(width, height)
    document.getElementById("PopupBody").innerHTML = getHtmlFragment(url)
}

function SetPopupPos(width, height) {
    width = width + 50;
    height = height + 8;

    document.getElementById("MaskDiv").style.display = "block"
    document.getElementById("IncludePopup").style.display = "block"

    document.getElementById("IncludePopupTable").style.width = width + "px"
    document.getElementById("PopupBody").style.height = height + "px"

    //var cSize = getDocumentSize()
    var wSize = getClientSize()
    //alert(cSize[0] + " / " + cSize[1] + "\n" + wSize[0] + " / " + wSize[1]);

    var w = Math.floor((parseInt(wSize[0]) - width) / 2);
    var h = Math.floor((parseInt(wSize[1]) - height) / 2);


    document.getElementById("IncludePopup").style.left = (w - 15) + "px";
    document.getElementById("IncludePopup").style.top = (h - 20) + "px";
    //document.getElementById("MaskDiv").style.top=GetScrollTopY() + "px"
    //document.getElementById("MaskDiv").style.left=GetScrollTopX() + "px"
    PopUpIsOpen = true
}


function ClosePopup() {
    document.getElementById("MaskDiv").style.display = "none"
    document.getElementById("IncludePopup").style.display = "none"
    PopUpIsOpen = false
    curFunctionName = ""
}






/***************************************/
/*                Zoom                 */
/***************************************/

var cpt = 1;
var opt = null;
var f = null;
var imgUrl = null;

function PopUpLoaded() {
    f.setImage(imgUrl)
}

function Zoom(cUrl, curW, curH) {
    var maxW = 0;
    var maxH = 0;
    var cSize = getClientSize()
    maxW = ((Math.floor(cSize[0] / 100) - 2) * 100);
    maxH = ((Math.floor(cSize[1] / 100) - 2) * 100);

    var imgW = curW;
    var imgH = curH;
    if (curW >= curH) { // paysage
        if (curH >= maxH) {
            imgW = (maxH / curH) * curW;
            imgH = maxH;
        }
        if (curW > maxW) {
            imgH = (maxW / curW) * curH;
            imgW = maxW;
        }
    } else { // portrait
        if (curW >= maxW) {
            imgH = (maxW / curW) * curH;
            imgW = maxW;
        }
        if (curH > maxH) {
            imgW = (maxH / curH) * curW;
            imgH = maxH;
        }
    }
    imgW = Math.floor(imgW);
    imgH = Math.floor(imgH);


    //alert(cSize[0] + "/" + cSize[1] + "\n\n" + maxW + "/" + maxH + "\n\n" + curW + ' / ' + curH + "\n" + imgW + ' / ' + imgH )

    SetPopupPos(imgW, imgH);
    var img = document.createElement("img");
    img.src = cUrl;
    img.style.cursor = "pointer"
    img.title = "";
    img.width = imgW;
    img.height = imgH;
    img.onclick = ClosePopup;
    document.getElementById("PopupBody").innerHTML = ""
    document.getElementById("PopupBody").appendChild(img)
} 

//Taille du document complet
function getDocumentSize() {
    return new Array((document.documentElement && document.documentElement.scrollWidth) ? document.documentElement.scrollWidth : (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth, (document.documentElement && document.documentElement.scrollHeight) ? document.documentElement.scrollHeight : (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight);
}

//Taille du contenu de la fenetre visible
function getClientSize() {
    /* http://www.howtocreate.co.uk/tutorials/javascript/browserwindow */
    return Array((document.documentElement && document.documentElement.clientWidth) || window.innerWidth || self.innerWidth || document.body.clientWidth, (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight);
}



/***************************************/
/*               Confirm               */
/***************************************/
var curFunctionName = ""
function ConfirmPopup(Message, FunctionName) {
    curFunctionName = FunctionName
    SetPopupPos(360, 80)
    document.getElementById("ConfirmContentLabel").innerHTML = Message
    document.getElementById("PopupBody").innerHTML = document.getElementById("ConfirmContent").innerHTML
}

function SetConfirm() {
    window.setInterval(curFunctionName, 0)
    ClosePopup()
}

/***************************************/
/*                Caddie               */
/***************************************/

function AddToCaddy(IdProduit, IdLang) {
    showPopup(360, 160, "clients/inc/async/AddToBasket.aspx?IdProduit=" + IdProduit + "&IdLang=" + IdLang)
}





/***************************************/
/*                XMLHTTP              */
/***************************************/
var objHttp;

function getHtmlFragment(url) {
    if (window.XMLHttpRequest)
    { objHttp = new XMLHttpRequest(); }
    else {
        if (window.ActiveXObject)
        { objHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        else {
            alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
            return;
        }
    }

    if (objHttp != null) {
        objHttp.open("GET", url, false)
        objHttp.send(null)
        var txt = objHttp.responseText
        return txt
    }
}




/***************************************/
/*                SCROLL               */
/***************************************/
function GetScrollTopY() {
    var x, y;
    if (self.pageYOffset) // all except Explorer
    { y = self.pageYOffset; }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    { y = document.documentElement.scrollTop; }
    else if (document.body) // all other Explorers
    { y = document.body.scrollTop; }
    return y
}
function GetScrollTopX() {
    var x, y;
    if (self.pageYOffset) // all except Explorer
    { x = self.pageXOffset; }
    else if (document.documentElement && document.documentElement.scrollTop)
    // Explorer 6 Strict
    { x = document.documentElement.scrollLeft; }
    else if (document.body) // all other Explorers
    { x = document.body.scrollLeft; }
    return x
}




/***************************************/
/*                News Letter          */
/***************************************/

function NewsLetterSave() {
    document.onkeydown = null
    if (oMail == null)
    { return }
    var ret = getHtmlFragment("Clients/inc/Async/mailing.aspx?Email=" + oMail.value)
    PopupAlert(ret)
}

function NewsLetter() {
    SetPopupPos(360, 90)
    document.getElementById("PopupBody").innerHTML = document.getElementById("newsLetterContent").innerHTML
    document.onkeydown = CheckKey
}

function CheckKey() {
    var k = window.event.keyCode
    if (k == 8 || k == 46)
    { ScanMail() }
    if (k == 27)
    { ClosePopup() }
}

var oMail = null
var sMail = ""

function CheckMail(obj) {
    oMail = obj
    ScanMail()
}

function ScanMail() {
    if (oMail == null)
    { return }
    sMail = new String(oMail.value)
    sMail = sMail.toLowerCase()
    if (ValidMail(sMail))
    { oMail.style.color = "green" }
    else
    { oMail.style.color = "red" }

    oMail.value = sMail
}

function ValidMail(val) {
    var apos = val.indexOf("@")
    var dotpos = val.lastIndexOf(".")
    if (apos < 1 || dotpos - apos < 2)
    { return false }
    else
    { return true }
}