// Build 9.00.7773.9

Qva.Modal = function(scriptPath) {
    if(typeof scriptPath !== 'string') scriptPath = null;
    this.ScriptPath = scriptPath || '/QvAjaxZfc/htc/';
    this.HideSelects = false;
    this.DefaultPage = "modal/loading.html";
    this.TabIndexes = [];
    this.TabbableTags = ["A","BUTTON","TEXTAREA","INPUT","IFRAME"];
    this.PopupIsShown = false;
    Qva.Modal.instance = this;
}

Qva.Modal.prototype.Show = function(pagebinder, url, width, height) {
    this.PageBinder = pagebinder;
    this.Init();
    document.getElementById("popCloseBox").style.display = "block";
    this.PopupIsShown = true;
    this.DisableTabs();
    this.PopupMask.style.display = "block";
    this.PopupContainer.style.display = "block";
    
    if(width && height) this.SetSize(width, height);
    
    if(pagebinder.Session) {
        // transfer session
        url += ((url.indexOf ('?') == -1) ? '?' : '&') + 'session=' + escape (pagebinder.Session);
    }
    // transfer ticket
    if(pagebinder.Ticket) url = Qva.FixUrl(url, "ticket", pagebinder.Ticket);
    // transfer host
    if (pagebinder.Host) url = Qva.FixUrl(url, "host", pagebinder.Host);
    if (pagebinder.Unicorn) url = Qva.FixUrl(url, "unicorn", "3");
    
    if (Qva.Benchmark) {
        this.Benchmark = new Qva.Benchmark();
    }
    
    // set the url
    this.PopFrame.src = url;
    
    // for IE
    if (this.HideSelects == true) {
        this.HideSelectBoxes();
    }
}

Qva.Modal.prototype.SetSize = function(width, height) {
    // calculate where to place the window on screen
    this.CenterWin(width, height);
    
    var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
    
    this.PopupContainer.style.width = width + "px";
    this.PopupContainer.style.height = (height+titleBarHeight) + "px";
    
    this.SetMaskSize();
    
    // need to set the width of the iframe to the title bar width because of the dropshadow
    // some oddness was occuring and causing the frame to poke outside the border in IE6
    this.PopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
    this.PopFrame.style.height = (height) + "px";
}

Qva.Modal.prototype.Init = function() {
    if (this.PopupMask != null) return;
    // Add the HTML to the body
    
    theBody = document.getElementsByTagName('BODY')[0];
    popmask = document.createElement('div');
    popmask.id = 'popupMask';
    popmask.style.position = 'absolute';
    popcont = document.createElement('div');
    popcont.id = 'popupContainer';
    popcont.style.position = 'absolute';
    popcont.innerHTML = '' +
        '<div id="popupInner">' +
            '<div id="popupTitleBar">' +
                '<div id="popupTitle" style="width:90%"></div>' +
                '<div id="popupControls" style="width:15px">' +
                    '<img src="' + this.ScriptPath + 'modal/close.gif" id="popCloseBox" />' +
                '</div>' +
            '</div>' +
            '<iframe src="'+ this.ScriptPath + this.DefaultPage +'" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
        '</div>';
    theBody.appendChild(popmask);
    theBody.appendChild(popcont);
    
    this.PopupCloseBox = document.getElementById("popCloseBox");
    this.PopupMask = document.getElementById("popupMask");
    var popupContainer = this.PopupContainer = document.getElementById("popupContainer");
    this.PopFrame = document.getElementById("popupFrame");
    
    var popupTitleBar = document.getElementById("popupTitleBar");
    popupTitleBar.onmousedown = function (event) {
        if (!event) event = window.event;
        
        var offsets = Qva.GetOffsets(event, popupContainer);
        
        function EndMove(event) {
            popupTitleBar.onmousemove = null;
            popupTitleBar.onmouseup   = null;
            popupTitleBar.onmouseout  = null;
            return false;
        }
        function MouseMove(event) {
            if (!event) event = window.event;
            var mousePos = { 'x': event.clientX + Qva.GetScrollLeft(),
                             'y': event.clientY + Qva.GetScrollTop() };
            popupContainer.style.left = (mousePos.x - offsets.offsetX) + "px";
            popupContainer.style.top  = (mousePos.y - offsets.offsetY) + "px";
            return false;
        }
        popupTitleBar.onmousemove = MouseMove;
        popupTitleBar.onmouseup   = EndMove;
        popupTitleBar.onmouseout  = EndMove;
        return false;
    };
    
    this.PopupCloseBox.onmousedown = function (event) {
        Qva.Modal.instance.Close();
    }
    
    
    // check to see if this is IE version 6 or lower. hide select boxes if so
    // maybe they'll fix this in version 7?
    var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
    if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
        this.HideSelects = true;
    }
}

// For IE.  Go through predefined tags and disable tabbing into them.
Qva.Modal.prototype.DisableTabs = function() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < this.TabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(this.TabbableTags[j]);
            for (var k = 0 ; k < tagElements.length; k++) {
                this.TabIndexes[i] = tagElements[k].tabIndex;
                tagElements[k].tabIndex="-1";
                i++;
            }
        }
    }
}

// For IE. Restore tab-indexes.
Qva.Modal.prototype.RestoreTabs = function() {
    if (document.all) {
        var i = 0;
        for (var j = 0; j < this.TabbableTags.length; j++) {
            var tagElements = document.getElementsByTagName(this.TabbableTags[j]);
            for (var k = 0 ; k < tagElements.length; k++) {
                tagElements[k].tabIndex = this.TabIndexes[i];
                tagElements[k].tabEnabled = true;
                i++;
            }
        }
    }
}

Qva.Modal.prototype.CenterWin = function(width, height) {
    if (this.PopupIsShown) {
        if (width == null || isNaN(width)) {
            width = this.PopupContainer.offsetWidth;
        }
        if (height == null) {
            height = this.PopupContainer.offsetHeight;
        }
        
        var theBody = document.getElementsByTagName("BODY")[0];
        var scTop = parseInt(Qva.GetScrollTop(),10);
        var scLeft = parseInt(theBody.scrollLeft,10);
        
        this.SetMaskSize();
        
        var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
        
        var fullHeight = Qva.GetViewportHeight();
        var fullWidth = Qva.GetViewportWidth();
        
        this.PopupContainer.style.top = (scTop + ((fullHeight - (height+titleBarHeight)) / 2)) + "px";
        this.PopupContainer.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
    }
}

Qva.Modal.prototype.SetMaskSize = function() {
    var theBody = document.getElementsByTagName("BODY")[0];
    
    var fullHeight = Qva.GetViewportHeight();
    var fullWidth = Qva.GetViewportWidth();
    
    // Determine what's bigger, scrollHeight or fullHeight / width
    if (fullHeight > theBody.scrollHeight) {
        popHeight = fullHeight;
    } else {
        popHeight = theBody.scrollHeight;
    }
    
    if (fullWidth > theBody.scrollWidth) {
        popWidth = fullWidth;
    } else {
        popWidth = theBody.scrollWidth;
    }
    
    this.PopupMask.style.height = popHeight + "px";
    this.PopupMask.style.width = popWidth + "px";
}

Qva.Modal.prototype.HideSelectBoxes = function() {
    for(var i = 0; i < document.forms.length; i++) {
        for(var e = 0; e < document.forms[i].length; e++){
            if(document.forms[i].elements[e].tagName == "SELECT") {
                document.forms[i].elements[e].style.visibility="hidden";
            }
        }
    }
}

Qva.Modal.prototype.DisplaySelectBoxes = function() {
    for(var i = 0; i < document.forms.length; i++) {
        for(var e = 0; e < document.forms[i].length; e++){
            if(document.forms[i].elements[e].tagName == "SELECT") {
            document.forms[i].elements[e].style.visibility="visible";
            }
        }
    }
}

Qva.Modal.prototype.Hide = function () {
    this.PopupIsShown = false;
    var theBody = document.getElementsByTagName("BODY")[0];
    theBody.style.overflow = "";
    this.RestoreTabs();
    if (this.PopupMask == null) {
        return;
    }
    this.PopupMask.style.display = "none";
    this.PopupContainer.style.display = "none";
    this.PopFrame.src = this.ScriptPath + this.DefaultPage;
    // display all select boxes
    if (this.HideSelects == true) {
        this.DisplaySelectBoxes();
    }
}

Qva.Modal.prototype.SetTitle = function (text) {
    try {
        document.getElementById("popupTitle").innerText = text;
    } catch(e) {
    }
}

Qva.Modal.prototype.Close = function () {
    this.Hide();
    if (this.PageBinder) {
        if (this.PageBinder.LabelClick) {
            this.PageBinder.Set('.Nothing', 'add', 'nothing', true);
        } else {
            this.PageBinder.Refresh();
        }
    }
}



