openPopups = new Array();
function Popup(width, title, className, height, modal, title_id, allow_scroll, close_link_function) {
    this.identifier = title;
    if (openPopups[this.identifier]) {
        return openPopups[this.identifier];
    }
    openPopups[title] = this;
    
    this.allow_scroll = (ie6 || isMacFireFox) || allow_scroll;

    if (modal == null || modal == undefined) modal = false;
    this.modal = modal;
    var element = document.createElement("DIV");
    element.id = createUID("popup");
    if (width) element.style.width = width + "px";
    if (height) element.style.height = height + "px";
    if (className == null || className == undefined) className = "wPopup";
    element.className = className;

    
    if (title) {
        var div = document.createElement('DIV');
        div.className = "header";
        var link = document.createElement('A');
        //TODO per quicklist
        //link.style.paddingRight = '5px';
        link.style.color = "white";
        link.style.textDecoration = 'none';
        link.className = 'close-link';
        link.title = 'chiudi questa finestra';
        link.href = '#';
        var popup = this;
        if (close_link_function) {
            link.onclick = function() {
                close_link_function();
                popup.close();
                return false;
            }
        } else {
            link.onclick = function () {
                popup.close();
                return false;
            }
        }
        link.innerHTML = "X";
        div.appendChild(link);
        var title_el = document.createElement('SPAN');
        if(title_id){
            title_el.setAttribute('id', title_id);
        }
        title_el.innerHTML = title;
        div.appendChild(title_el);

        div.onmousedown = function(event) {
            if (ie) {
                if (window.event.srcElement) dragStart(event, element.id);
            } else {
                if (event.target) dragStart(event, element.id);
            }
        };
        
        element.appendChild(div);
    } 

    if (this.modal) {
        var popup = this;
        window.onresize = function() {center(element.id);};
    }

    var div = document.createElement('DIV');
    div.className = "content";

    this.contentDiv = div;
    element.appendChild(div);
    
     //footer (x la quicklist...)
    var footer = document.createElement('DIV');
    footer.className = "footer";
    element.appendChild(footer);

    this.element = element;
    this.content = null;
}

Popup.prototype.setContent = function(el) {
    var links = el.getElementsByTagName('a');
    var popup = this;
    for (i=0; i<links.length; i++) {
        if (links[i].className.match(/(^| )close( |$)/)) {
            var existing_onclick = links[i].onclick;
            links[i].onclick = function() {
                popup.close();
                if(existing_onclick){
                    existing_onclick();
                }
                return false;
            }
        }
    }
    if (this.content != null) {
        this.contentDiv.removeChild(this.content);
    }
    this.content = el;
    this.contentDiv.appendChild(el);
}

Popup.prototype.show = function() {
    this.element.style.visibility = "hidden";
    this.element.style.position = this.allow_scroll ? "absolute" : "fixed";
    document.body.appendChild(this.element);
    center(this.element.id);//APPROFONDIRE
    activecontentHider(this.element, this.modal);//APPROFONDIRE

    this.element.style.zIndex = findNextZIndex(this.element);//APPROFONDIRE
    if (this.element.modal) {
        this.element.modal.style.zIndex = this.element.style.zIndex - 1;
        this.element.modal.style.visibility = "visible";
        clientSize = getScreenSize();

        this.element.modal.style.left = "0px";
        this.element.modal.style.top = "0px";
        if (ie6) {
            this.element.modal.style.height = (document.body.scrollHeight > clientSize[1] ? document.body.scrollHeight : clientSize[1]) + 'px';
            this.element.modal.style.width = (document.body.scrollWidth > clientSize[0] ? document.body.scrollWidth : clientSize[0]) + 'px';
        } else {
            this.element.modal.style.height = clientSize[1] + 'px';
            this.element.modal.style.width = clientSize[0] + 'px';
        }
    }
    
    if (this.element.ifrm) {
        this.element.ifrm.style.zIndex = this.element.style.zIndex - 2;
            
        this.element.ifrm.style.visibility = "visible";
        this.element.ifrm.style.left = (this.element.modal && ie6) ? this.element.modal.style.left : this.element.style.left;
        this.element.ifrm.style.top = (this.element.modal && ie6) ? this.element.modal.style.top : this.element.style.top;
        this.element.ifrm.style.height = (this.element.modal && ie6) ? this.element.modal.style.height : this.element.offsetHeight;
        this.element.ifrm.style.width = (this.element.modal && ie6) ? this.element.modal.style.width : this.element.offsetWidth;
    }

    this.element.style.visibility = "visible";

    if (ie) {
        var element = this.element;
        window.onscroll = function() {
            if (this.offset == undefined) this.offset = 1;
            var x = parseInt(element.style.left, 10);
            var y = parseInt(element.style.top,  10);
            if (this.offset == 1) this.offset = -1;
            else this.offset = 1;
            move(element, x+this.offset, y);
        }
    }
}

Popup.prototype.close = function() {
    if (this.element.ifrm) this.element.ifrm.style.visibility='hidden';
    if (this.element.modal) this.element.modal.style.visibility='hidden';
    this.element.style.display = 'none'; 
    document.body.removeChild(this.element);
    openPopups[this.identifier] = null;
}

Popup.prototype.getId = function() {
    return this.element.id;
}

function show_popup(id, width, title) {
    if (width == undefined || width == null) width = "300";
    if (title == undefined || title == null) title = "&nbsp;";

    var popup = new Popup(width, title);
    var div = document.createElement("DIV");
    div.id = createUID("popup_inner");
    html_content = document.getElementById(id).innerHTML;
    
    id_replace = createUID('_');
    div.innerHTML = replaceAll(html_content, '__id__', id_replace);
    
    popup.setContent(div);
    popup.show();
}

function show_alert(content, width, title, button_class ) {
    if (width == undefined || width == null) width = "300";
    if (title == undefined || title == null) title = "&nbsp;";

    var popup = new Popup(width, title);

    var div = document.createElement("DIV");
    div.id = createUID("popup_inner");
    div.innerHTML = content;

    div.appendChild(document.createElement('HR'));

    var buttons = document.createElement('DIV');
    buttons.className = 'action_buttons';
    buttons.appendChild(createCancelButton(popup, button_class) );
    div.appendChild(buttons);

    popup.setContent(div);
    popup.show();
}


function html_popup(html_content, width, title, close_fn) {
    var popup = new Popup(width, title);
    var div = document.createElement("DIV");
    div.innerHTML = html_content;
    if (!close_fn) close_fn = function () {}
    popup.old_close = popup.old_close ? popup.old_close : popup.close;
    popup.close = function() {
        popup.old_close();
        close_fn();
    }
    div.closePopup = function() {
        popup.close();
        return false;
    }
    popup.setContent(div);
    popup.show();
    return popup;
}


function getFormField(form, field) {
    for (var i = 0; i < form.elements.length; i++) {
        element = form.elements[i];
        if (element.name == field) {
            if (element.type == "checkbox" || element.type == "radio") {
                if (element.checked) {                
                    return element;
                }
            } else {
                return element;
            }
        }
    }
    return null;
}
