var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	

function PopupDialog(namePrefix) {
    this._name = namePrefix;
        
    this._hideSelects = false;
    if (window.navigator.userAgent.indexOf("MSIE") > -1) {
        this._hideSelects = parseInt(window.navigator.appVersion.charAt(0), 10) <= 6;
    }
    
    this._showing = false;
    this._tabIndexes = new Array();
    this._loadingUrl = "about:blank";
    this._contentUrl = "about:blank";
    this._width = 400;
    this._height = 300;
    this._closeable = true;
    
    if (!document.all) {
        document.onkeypress = this.handleKeyDown;
    }
}

PopupDialog.prototype.DeleteDialog = function() {
    document.body.removeChild(this._popupMask);
    document.body.removeChild(this._popupContainer);
    
    this._popupMask = null;
    this._popupContainer = null;
    this._popupInner = null;
    this._popupTitleBar = null;
    this._popupTitle = null;
    this._popupControls = null;
    this._popupClose = null;
    this._popupFrame = null;
    
    window.CurrentDialog = null;
}

PopupDialog.prototype.CreateDialog = function() {
    this._popupMask = document.createElement("div");
    this._popupMask.id = this._name + "_popupMask";
    this._popupMask.className = "popupMask";
    
    this._popupContainer = document.createElement("div");
    this._popupContainer.id = this._name + "_popupContainer";
    this._popupContainer.className = "popupContainer";
    
    this._popupInner = document.createElement("div");
    this._popupInner.id = this._name + "_popupInner";
    this._popupInner.className = "popupInner";
    
    this._popupTitleBar = document.createElement("div");
    this._popupTitleBar.id = this._name + "_popupTitleBar";
    this._popupTitleBar.className = "popupTitleBar";
    
    this._popupTitle = document.createElement("div");
    this._popupTitle.id = this._name + "_popupTitle";
    this._popupTitle.className = "popupTitle";

    this._popupControls = document.createElement("div");
    this._popupControls.id = this._name + "_popupControls";
    this._popupControls.className = "popupControls";
    
    this._popupClose = document.createElement("img");
    this._popupClose.src = closePopupDialogImageUrl;
    this._popupClose.PopupDialog = this;
    
    this._popupFrame = document.createElement("iframe");
    this._popupFrame.id = this._name + "_popupFrame";
    this._popupFrame.name = this._name + "_popupFrame";
    this._popupFrame.scrolling = "auto";
    this._popupFrame.frameborder = "0";
    this._popupFrame.allowtransparency = true;
    this._popupFrame.width = "100%";
    this._popupFrame.height = "100%";
    this._popupFrame.style.width = "100%";
    this._popupFrame.style.height = "100%";
    this._popupFrame.style.background = "transparent";
    this._popupFrame.className = "popupFrame";
    this._popupFrame.margin = "0px";
    this._popupFrame.position = "relative";
    this._popupFrame.zindex = "202";
        
    this._popupControls.appendChild(this._popupClose);
    
    this._popupTitleBar.appendChild(this._popupTitle);
    this._popupTitleBar.appendChild(this._popupControls);
    
    this._popupInner.appendChild(this._popupTitleBar);
    this._popupInner.appendChild(this._popupFrame);
    
    this._popupContainer.appendChild(this._popupInner);
    
    document.body.appendChild(this._popupMask);
    document.body.appendChild(this._popupContainer);
    
    window.CurrentDialog = this;
}

PopupDialog.prototype.GetHeight = function() {
    return this._height;
}

PopupDialog.prototype.SetHeight = function(height) {
    this._height = height;
}

PopupDialog.prototype.GetWidth = function() {
    return this._width;
}

PopupDialog.prototype.SetWidth = function(width) {
    this._width = width;
}

PopupDialog.prototype.SetLoadingUrl = function(url) {
    this._loadingUrl = url;
}

PopupDialog.prototype.GetLoadingUrl = function() {
    return this._loadingUrl;
}

PopupDialog.prototype.SetContentUrl = function(url) {
    this._contentUrl = url;
}

PopupDialog.prototype.GetContentUrl = function() {
    return this._contentUrl;
}

PopupDialog.prototype.GetCloseable = function() {
    return this._closeable;
}

PopupDialog.prototype.SetCloseable = function(closeable) {
    this._closeable = closeable;
}


PopupDialog.prototype.OnReturn = function() {

}

PopupDialog.prototype._handleKeyDown = function(e) {
    if (this._showing && e.keyCode == 9)  return false;
}

PopupDialog.prototype.Show = function() {
    this.CreateDialog();
    
    this._showing = true;

    this._disableTabIndexes();
    
    if (this._closeable == false) {
        this._popupClose.style.visibility = "hidden";
    } else {
        this._popupClose.style.visibility = "visible";
    }
    
    this._popupMask.style.display = "block";
    this._popupContainer.style.display = "block";
    
    centerDialog();

    this._popupContainer.style.width = this._width + "px";
    this._popupContainer.style.height = (this._height +  this._popupTitleBar.offsetHeight) + "px";

    this._popupFrame.style.width = this._popupTitleBar.offsetWidth + "px";
    this._popupFrame.style.height = this._height + "px";
    this._popupFrame.src = this._contentUrl;
    
    this._popupClose.onclick = function() {
        this.PopupDialog.Close();
    }
    
    if (this._hideSelects) {
        this._hideSelectBoxes();
    }
}

PopupDialog.prototype.Close = function() {
    var accept = false;
    
    if (arguments.length == 1) {
        accept = arguments[0];
    }
    
    this._showing = false;
    this._restoreTabIndexes();
    
    if (this._popupMask == null) {
        return;
    }
    
    this._popupMask.style.display = "none";
    this._popupContainer.style.display = "none";
    
    if (accept && this.OnReturn != null) {
        this.OnReturn(this.ReturnValue);
    }
    
    this._popupFrame.src = this._loadingUrl;
    
    if (this._hideSelects) {
        this._showSelectBoxes();
    }
    
    this.DeleteDialog();
}

PopupDialog.prototype._disableTabIndexes = function() {
    if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				this._tabIndexes[i] = tagElements[k].tabIndex;
				tagElements[k].tabIndex="-1";
				i++;
			}
		}
	}
}

PopupDialog.prototype._restoreTabIndexes = function() {
    if (document.all) {
		var i = 0;
		for (var j = 0; j < gTabbableTags.length; j++) {
			var tagElements = document.getElementsByTagName(gTabbableTags[j]);
			for (var k = 0 ; k < tagElements.length; k++) {
				tagElements[k].tabIndex = this._tabIndexes[i];
				tagElements[k].tabEnabled = true;
				i++;
			}
		}
	}
}

PopupDialog.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";
			}
		}
	}
}

PopupDialog.prototype._showSelectBoxes = 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";
			}
		}
	}
}

function centerDialog() {
    if (window.CurrentDialog != null) {
        var dialog = window.CurrentDialog;

	    if (dialog._showing == true) {
		    if (dialog._width == null || isNaN(dialog._width)) {
			    dialog._width = dialog._popupContainer.offsetWidth;
		    }
		    if (dialog._height == null) {
			    dialog._height = dialog._popupContainer.offsetHeight;
		    }
                        
		    var fullHeight = getViewportHeight();
		    var fullWidth = getViewportWidth();
            
		    var theBody = document.documentElement;
            
		    var scTop = getScrollTop();
		    var scLeft = getScrollLeft();
            
		    dialog._popupMask.style.height = fullHeight + "px";
		    dialog._popupMask.style.width = fullWidth + "px";
		    dialog._popupMask.style.top = scTop + "px";
		    dialog._popupMask.style.left = scLeft + "px";

		    var titleBarHeight = parseInt(dialog._popupTitleBar.offsetHeight, 10);
            
		    dialog._popupContainer.style.top = (scTop + ((fullHeight - (dialog._height+titleBarHeight)) / 2)) + "px";
		    dialog._popupContainer.style.left =  (scLeft + ((fullWidth - dialog._width) / 2)) + "px";		    
	    }
	    
        window.setTimeout(centerDialog, 5);
	}
}
