function openNewPopUp(url, name, width, height) {
    if (width == undefined)
        width = 500;
    if (height == undefined)
        height = 400;

    popupWindow = window.open(url, name, "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,toolbar=no,directories=no,status=no,menubar=no");
    popupWindow.focus();
}

function openPopUp(url, width, height) {
    if (width == undefined)
        width = 500;

    if (height == undefined)
        height = 400;

    popupWindow = window.open(url, "popup", "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,toolbar=no,directories=no,status=no,menubar=no");
    popupWindow.focus();
}

function collapseElement(elementId) {
    var el = document.getElementById(elementId);

    if (el.style.display == 'none')
        el.style.display = '';
    else el.style.display = 'none';
}

var g_PopupIFrame;

var viewportwidth;
var viewportheight;
//startHeigth: posizione in altezza della finestra
//idElementPosiz : id dell'elemento rispetto al quale si vuole posizionare la finestra
//startLeft: posizione orizzontale della finestra (usata solo se � null il parametro idElementPosiz)
//startHeigth : posizione verticale della finestra (usata solo se � null il parametro idElementPosiz)
function toggleModalWindow(veilId, modalId, wizardBoxHeader, open, idElementPosiz, startHeigth, startLeft) {
    var pngAlpha = false
      // if IE5.5+ on Win32, then display PNGs with AlphaImageLoader
        if ((browser.isIE55 || browser.isIE6x || browser.isIE6up) && browser.isWin32) {
            pngAlpha = true;
        // else, if the browser can display PNGs normally, then do that
        }
    if(open) {
        if(pngAlpha) {
            document.getElementById(veilId).className = "veilIE";
            /*alert ('document.body.clientHeight: ' +document.body.clientHeight);*/
            document.getElementById(veilId).style.width = document.body.clientWidth;
            /*alert (document.getElementById(veilId).style.height);*/
            document.getElementById(veilId).style.height = document.body.clientHeight;
            document.getElementById(modalId).style.left = "100";
            document.getElementById(modalId).style.top = "200";
        }
        else {
            //alert(document.body.clientHeight)
            document.getElementById(veilId).style.height = document.body.clientHeight+"px";
            document.getElementById(modalId).style.left = "100px";
            document.getElementById(modalId).style.top = "200px";
        }
        //alert(startHeigth);

        var coordsElementoRiferimento = {x: 0, y: 0};
        var elementRiferimento=document.getElementById(idElementPosiz);
        //Posizionamento rispetto a pulsante di login
        if(elementRiferimento!=null)
        {
            getViewportSize();
            while (elementRiferimento) {
                coordsElementoRiferimento.x += elementRiferimento.offsetLeft;
                coordsElementoRiferimento.y += elementRiferimento.offsetTop;
                elementRiferimento = elementRiferimento.offsetParent;
            }

            if(coordsElementoRiferimento.x!=0)
            {
                document.getElementById(modalId).style.left=coordsElementoRiferimento.x + "px";
            }
            if(coordsElementoRiferimento.y!=0)
            {
//                getViewportSize();
                var y_value;
                var scrlY=0;
                if (browser.isIE) {
                    if (browser.isIE6up) {
                        scrlY += document.documentElement.scrollTop;
                    } else {
                        scrlY += document.body.scrollTop;
                    }
                } else {
                    scrlY += window.scrollY;
                }
//                alert( scrlY )
                if(viewportheight + scrlY - coordsElementoRiferimento.y > 170)
                    y_value=coordsElementoRiferimento.y+20;
                else
                    y_value=coordsElementoRiferimento.y-170;
                document.getElementById(modalId).style.top=y_value + "px";
            }
        }
        else
        {
            if(startLeft != null) {
                document.getElementById(modalId).style.top = startLeft + "px";
            }
            if(startHeigth != null) {
                document.getElementById(modalId).style.top = startHeigth + "px";
            }
        }

        document.getElementById(veilId).style.display = 'block';
        document.getElementById(modalId).style.display = 'block';
        if(pngAlpha && (browser.isIE55 || browser.isIE6x)) {
            /* creo l'iframe per nascondere le select */
            var iFrame = document.createElement("IFRAME");
            iFrame.setAttribute("src", "../inc/emptyIFrame.html");
            //Match IFrame position with divPopup
            iFrame.style.position="absolute";
            iFrame.style.left = '0px';
            iFrame.style.top = '0px';
            iFrame.style.width =document.getElementById(veilId).style.width;
            iFrame.style.height =document.body.clientHeight;
            iFrame.style.zIndex = 100;
            iFrame.style.display = 'block';
            /*alert("veil zIndex: " +document.getElementById(veilId).style.zIndex+ ", iframe zIndex: "+ iFrame.style.zIndex);*/
            /* non mi resta che applicare il filtro per la trasparenza */
            iFrame.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
            /* inserisco un iframe che nasconda le select */
            document.body.appendChild(iFrame);
            g_PopupIFrame = iFrame;
        }
        Drag.init(document.getElementById(wizardBoxHeader), document.getElementById(modalId), null, null, null, null, false, false);
    }
    else {
        if(pngAlpha && (browser.isIE55 || browser.isIE6x)) {
            document.body.removeChild(g_PopupIFrame);
            g_PopupIFrame=null;
        }
        document.getElementById(veilId).style.display = 'none';
        document.getElementById(modalId).style.display = 'none';
    }

}

function getViewportSize(){
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

     if (typeof window.innerWidth != 'undefined')
     {
          viewportwidth = window.innerWidth,
          viewportheight = window.innerHeight
     }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

     else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth !=
         'undefined' && document.documentElement.clientWidth != 0)
     {
           viewportwidth = document.documentElement.clientWidth,
           viewportheight = document.documentElement.clientHeight
     }

     // older versions of IE

     else
     {
           viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
           viewportheight = document.getElementsByTagName('body')[0].clientHeight
     }

}

function doOnComplete(div_to_reload, response){
    if(response.getResponseHeader("errorPresent") == 1){
        // login errato
        document.getElementById("wrongLoginID").innerHTML = response.responseText
        document.getElementById("wrongLoginID").style.display = 'block'
        window.setTimeout("document.getElementById('wrongLoginID').style.display='none'", 3000)
    } else {
        // login ok
        div_to_reload.innerHTML = response.responseText
        window.location.reload()
    }
}

function doOnCompleteSpalla(div_to_reload, response){
    if(response.getResponseHeader("errorPresent") == 1){
        // login errato
        document.getElementById("shopErrorDiv").innerHTML = response.responseText
        document.getElementById("shopErrorDiv").style.display = 'block'
        window.setTimeout("document.getElementById('shopErrorDiv').style.display='none'", 3000)
    } else {
        // login ok
        div_to_reload.innerHTML = response.responseText
        document.getElementById("shopErrorDiv").innerHTML = '<p style="padding: 5px; color: red; font-weight:bold;">Login effettuato!</p>'
        document.getElementById("shopErrorDiv").style.display = 'block'
        window.setTimeout("window.location.reload()", 1000)
    }
}

function reloader(div_id, url){
    new Ajax.Updater(div_id, url, {evalScripts: true});
}
