// open_popup.js (C) 2007 Vittorio Bertola <vb@bertola.eu>// Released under the GNU Public License version 2

// version 2.1 - 19/11/2008// Usage: to open a popup on a link, use//    <a href="http:://..." onClick="return PUOpenPopup(this.url, 100, 200);">// To open a link in the main window from the popup, use//    <a href="http:://..." onClick="return PUParentLink(this.url, false);">// or true to close the popup as wellfunction PUOpenPopup(url, width, height, name) {
    if (! name)
        name = "default";
    if (window['popup_' + name] && (! window['popup_' + name].closed))        window['popup_' + name].focus();    else {        posLeft = ((document.innerWidth ? document.innerWidth : (document.body && document.body.clientWidth ? document.body.clientWidth : (document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : 800))) - width) / 2 + (typeof window.screenX == "undefined" ? window.screenLeft : window.screenX);        posTop = ((document.innerHeight ? document.innerHeight : (document.body && document.body.clientHeight ? document.body.clientHeight : (document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : 1000))) - height) / 2 + (typeof window.screenY == "undefined" ? window.screenTop : window.screenY);        window['popup_' + name] = open(url, 'popup', 'width=' + width + 'px, height=' + height + 'px, left=' + posLeft + ', screenX=' + posLeft + ', top=' + posTop + ', screenY=' + posTop + ', resizable=no, scrollbars=no, status=no, toolbar=no, location=no, menubar=no');    }    return false;  // così ignora il link}


function PUHasCookie(cookiename) {
    if (document.cookie.indexOf('; ' + cookiename + '=') > -1)
        // c'è, in mezzo
        return true;
    if (document.cookie.indexOf(cookiename + '=') == 0)
        // c'è, all'inizio
        return true;
    return false;
}


function PUAutoPopup(url, width, height, name, path, expirehrs) {
    if (! name)
        name = "auto";
    if (! path)
        path = "/";
    if (! PUHasCookie('popup_' + name)) {
        PUOpenPopup(url, width, height, name);
        // default expire (sessione) e dominio (pagina)
         newcookie = "popup_" + name + "=1";
         if (expirehrs > 0) {
             expirehrs = expirehrs * 3600000;             var expires_date = new Date( (new Date()).getTime() + (expirehrs) );
             newcookie = newcookie + ";expires=" + expires_date.toGMTString();
         }  
         newcookie = newcookie + ";path=" + path;         
         document.cookie = newcookie;
//         alert('setting cookie: ' + newcookie);
    } else {
//        alert('has cookie');
        // se è aperto, comunque non lo poppo su, basta una volta! (però potrei)
    }}


function PUParentLink(url, closepopup) {    if (window.opener) {                window.opener.location.href = url;        if (closepopup)            window.close();        window.opener.focus();    } else        this.location.href = url;    return false;  // così ignora il link}


function PUFocusPopup(name) {
    window['popup_' + name].focus();
}


function PUDefocusPopup(name) {
    window['popup_' + name].opener.focus();
}


function PUClosePopup(name) {
    window['popup_' + name].close();
}
