/*
partially programmed by hedger
hedger@yahoo-inc.com

Reworked by jeff
*/


var modal_dialog_win = false;
var modal_dialog_win_return_val = null;
var modal_dialog_interval = false;


function modal_dialog()	{
	this.WindowFeature = new Array();
	this.features = "";
	this.isModal = false;
	this.div_written = false;
	
	this.prepare_windowFeature = function() {
		this.WindowFeature["center"] = "";
		this.WindowFeature["width"] = 500;
		this.WindowFeature["height"] = 500;
		this.WindowFeature["border"] = "";
		this.WindowFeature["edge"] = "";
		this.WindowFeature["help"] = "";
		this.WindowFeature["left"] = "";
		this.WindowFeature["top"] = "";
		this.WindowFeature["url"] = "";
		this.WindowFeature["resizable"] = "";
		this.WindowFeature["unadorned"] = "";
		this.WindowFeature["directories"] = "";
		this.WindowFeature["fullScreen"] = "";
		this.WindowFeature["location"] = "";
		this.WindowFeature["menuBar"] = "";
		this.WindowFeature["scrollBars"] = "no";
		this.WindowFeature["status"] = "yes";
		this.WindowFeature["toolbar"] = "";
	}
	this.prepare_windowFeature();		// Call this immediately (has to be after where the function is defined)
	
	
	this.affirmativeInput = function(bool)	{
		if (bool == "true" || bool == true || bool == "yes" || bool == "on" || bool == 1){
			return true;
		}
		else{
			return false;
		}
	}

	this.allowModeless = function()	{
		if (window.showModelessDialog){
			return true;
		}
		else{
			return false;
		}
	}
	
	this.setCentered = function(bool){
		if (this.affirmativeInput(bool)){
			this.WindowFeature["center"] = "yes";
		}
		else{
			this.WindowFeature["center"] = "no";
		}
	}

	this.setWidth = function(width){
		this.WindowFeature["width"] = width;
	}

	this.setHeight = function(height){
		this.WindowFeature["height"] = height;
	}

	// I don't know what input types this takes, but the example is:
	// 		thick
	this.setBorder = function(borderType){
		this.WindowFeature["border"] = borderType;
	}

	// Input types are:
	//		sunken
	//		raised
	this.setEdge = function(edgeType){
		if (edgeType == "sunken" || edgeType == "raised"){
			this.WindowFeature["edge"] = edgeType;
		}
	}

	this.setHelp = function(bool){
		if (this.affirmativeInput(bool)){
			this.WindowFeature["help"] = "yes";
		}
		else{
			this.WindowFeature["help"] = "no";
		}
	}

	this.setIsModal = function(bool){
		if (this.affirmativeInput(bool)){
			this.isModal = true;
		}
		else{
			this.isModal = false;
		}
	}

	this.setLeft = function(left){
		this.WindowFeature["left"] = left;
	}

	this.setScreenX = function(screenX){
		this.WindowFeature["screenX"] = screenX;
	}

	this.setScreenY = function(screenX){
		this.WindowFeature["screenY"] = screenY;
	}

	this.setTop = function(top){
		this.WindowFeature["top"] = top;
	}

	this.setURL = function(url){
		this.WindowFeature["url"] = url;
	}

	this.setResizable = function(bool){
		if (this.affirmativeInput(bool)){
			this.WindowFeature["resizable"] = "yes";
		}
		else{
			this.WindowFeature["resizable"] = "no";
		}
	}

	this.setUnadorned = function(bool){
		if (this.affirmativeInput(bool)){
			this.WindowFeature["unadorned"] = "yes";
		}
		else{
			this.WindowFeature["unadorned"] = "no";
		}
	}

	this.showDirectories = function(bool){
		this.WindowFeature["directories"] = bool;
	}

	this.showFullScreen = function(bool){
		this.WindowFeature["fullScreen"] = bool;
	}

	this.showLocation = function(bool){
		this.WindowFeature["location"] = bool;
	}

	this.showMenuBar = function(bool){
		this.WindowFeature["menuBar"] = bool;
	}
	
	this.showScrollBars = function(bool){
		if (this.affirmativeInput(bool)){
			this.WindowFeature["scrollBars"] = "yes";
		}
		else{
			this.WindowFeature["scrollBars"] = "no";
		}
	}

	this.showStatus = function(bool){
		if (this.affirmativeInput(bool)){
			this.WindowFeature["status"] = "yes";
		}
		else{
			this.WindowFeature["status"] = "no";
		}
	}

	this.showToolbar = function(bool){
		this.WindowFeature["toolbar"] = bool;
	}

	this.getBorder = function(){
		return this.WindowFeature["border"];
	}

	this.getCenter = function(){
		return this.WindowFeature["center"];
	}

	this.getEdge = function(){
		return this.WindowFeature["edge"];
	}

	this.getWidth = function(){
		return this.WindowFeature["width"];
	}

	this.getHeight = function(){
		return this.WindowFeature["height"];
	}

	this.getHelp = function(){
		return this.WindowFeature["help"];
	}

	this.getIsModal = function(){
		return this.isModal;
	}

	this.getLeft = function(){
		return this.WindowFeature["left"];
	}

	this.getScreenX = function(){
		return this.WindowFeature["screenX"];
	}

	this.getScreenY = function(){
		return this.WindowFeature["screenY"];
	}

	this.getTop = function(){
		return this.WindowFeature["top"];
	}

	this.getResizable = function(){
		return this.WindowFeature["resizable"];
	}

	this.getShowDirectories = function(){
		return this.WindowFeature["directories"];
	}

	this.getShowFullScreen = function(){
		return this.WindowFeature["fullScreen"];
	}

	this.getShowLocation = function(){
		return this.WindowFeature["location"];
	}

	this.getShowMenuBar = function(){
		return this.WindowFeature["menuBar"];
	}

	this.getShowScrollBars = function(){
		return this.WindowFeature["scrollBars"];
	}

	this.getShowStatus = function(){
		return this.WindowFeature["status"];
	}

	this.getShowToolbar = function(){
		return this.WindowFeature["toolbar"];
	}

	this.getUnadorned = function(){
		return this.WindowFeature["unadorned"];
	}

	this.getURL = function(){
		return this.WindowFeature["url"];
	}
	
	this.parseTrueModalFeatures = function(){
		if (this.getLeft() != ""){
			this.features += "dialogLeft:" +  this.getLeft() + "px;";
		}
		if (this.getTop() != ""){
			this.features += "dialogTop:" +  this.getTop() + "px;";
		}
		if (this.getWidth() != ""){
			this.features += "dialogWidth:" + this.getWidth() + "px;";
		}
		if (this.getHeight() != ""){
			this.features += "dialogHeight:" + this.getHeight() + "px;";
		}
		if (this.getCenter() != ""){
			this.features += "center:" + this.getCenter() + ";";
		}
		if (this.getHelp() != ""){
			this.features += "help:" + this.getHelp() + ";";
		}
		if (this.getResizable() == "yes"){
			this.features += "resizable:1;";
			//ORIGINAL: this.features += "resizable:1;minimize:1;maximize:1;";
		}
		if (this.getShowStatus() != ""){
			this.features += "status:" + this.getShowStatus() + ";";
		}
		if (this.getUnadorned() != ""){
			this.features += "unadorned:" + this.getUnadorned() + ";";
		}
		if (this.getEdge() != ""){
			this.features += "edge:" + this.getEdge() + ";";
		}
		if (this.getBorder() != ""){
			this.features += "border:" + this.getBorder() + ";";
		}
	}

	this.parseFauxModalFeatures = function()	{
		if (this.getWidth() != ""){
			this.features += ",width=" + this.getWidth();
		}
		if (this.getHeight() != ""){
			this.features += ",height=" + this.getHeight();
		}
		if (this.getCenter() != "")	{		// Check for this first, before Left or Center
			LeftPosition = (screen.width) ? (screen.width-this.getWidth())/2 : 0;
			TopPosition = (screen.height) ? (screen.height-this.getHeight())/2 : 0;
			this.features += ",left=" + LeftPosition;
			this.features += ",top=" + TopPosition;
		}
		else	{
			if (this.getLeft() != ""){
				this.features += ",left=" +  this.getLeft();
			}
			if (this.getTop() != ""){
				this.features += ",top=" +  this.getTop();
			}
		}
		if (this.getResizable() != "")	{
			this.features += ",resizable="+this.getResizable();
		}
		if(this.getShowScrollBars() != "")	{
			this.features += ",scrollbars="+this.getShowScrollBars();
		}
		
		/*
		ADD THESE LATER
		if (this.getHelp() != ""){
			this.features += "help:" + this.getHelp() + ";";
		}
		
		if (this.getShowStatus() != ""){
			this.features += "status:" + this.getShowStatus() + ";";
		}
		if (this.getUnadorned() != ""){
			this.features += "unadorned:" + this.getUnadorned() + ";";
		}
		if (this.getEdge() != ""){
			this.features += "edge:" + this.getEdge() + ";";
		}
		if (this.getBorder() != ""){
			this.features += "border:" + this.getBorder() + ";";
		}
		*/
	}

	this.getFeatures = function(){
		this.features = "";
		if (this.allowModeless()){
			this.parseTrueModalFeatures();
		}
		else{
			this.parseFauxModalFeatures();
		}
		return this.features;
	}
	
	
	this.dialog = function(url,name,isModal) {
		this.setIsModal(isModal);

		if (url == null) {
			return false;
		}

		url = url;

		if (name == null) {
		name="";
		}
		
		if(this.allowModeless()){
			if(window.ModelessDialog == null){
				window.ModelessDialog = new Object();
			};

			if(name != "") {
				if(window.ModelessDialog[name] != null && !window.ModelessDialog[name].closed){
					window.ModelessDialog[name].focus();
					return window.ModelessDialog[name];
				}
			}

			var F = this.getFeatures();

			if(isModal){
				modal_dialog_win_return_val = window.showModalDialog(url,self,F);
				return false;
			}
			else
			{
				window.ModelessDialog[name] = window.showModelessDialog(url,self,F);
				return window.ModelessDialog[name];
			}
		}
		else {
			var F = this.getFeatures();
			
			if(this.getIsModal()){
				var interval_needed = false;
				var agt = navigator.userAgent.toLowerCase();
				if(agt.indexOf("firefox/2.0") != -1)	{		// We have Firefox 2.0 - need DIV
					if(!this.div_written)	{
						newdiv = document.createElement("div");
						newdiv.setAttribute("id", "div_modal_window_cover");
						newdiv.style.display = "none";
						newdiv.style.position = "absolute";
						newdiv.style.zIndex = -1;
						newdiv.style.width = "100%";
						newdiv.style.height = "100%";
						document.body.appendChild(newdiv);
						
						this.div_written = true;
					}
					
					cover_entire_window();	// "Block" our main window with an invisible overlying div
					interval_needed = true;
				}
				
				var Modal = window.open(url,name,"modal=1"+F);
				modal_dialog_win = Modal;
				var ModalFocus = function(){
					if(!Modal.closed){
						Modal.focus();
					}
					else{
						Modal = null;
						window.removeEventListener("focus", ModalFocus, false);
						ModalFocus = null;
					};
				}
				window.addEventListener("focus", ModalFocus, false);
				if(interval_needed)	{
					modal_dialog_interval = window.setInterval("maintain_modal_dialog_focus()", 5);
				}
				return false;
			}
			else{
				return window.open(url,name,"modal=0"+F);
			}
		}
		return null;
	}
	
	
	this.modal = function(name) {
		this.dialog(this.getURL(),name,true);
		return false;
	}
	
	
	this.normal = function(name){
		this.dialog(this.getURL(),name,false);
		return false;
	}
}


// ************************************************************************************************************
// ************************************ THESE ARE FOR USE WITH FIREFOX 2.0 ************************************
// ************************************************************************************************************
function cover_entire_window()	{
	div = document.getElementById("div_modal_window_cover");
	div.style.display = "block";
	div.style.position = "absolute";
	div.style.top = 0;
	div.style.left = 0;
	div.style.zIndex = 9999999;
	div.style.width = "100%";
}
	
function remove_window_cover_div()	{
	window.clearInterval(modal_dialog_interval);	// First, clear our interval
	
	// Then close the div
	div = document.getElementById("div_modal_window_cover");
	div.style.display = "none";
	div.style.position = "";
	div.style.top = 0;
	div.style.left = 0;
	div.style.zIndex = -1 ;
	div.style.width = "0%";
}
	
function maintain_modal_dialog_focus()	{
	try	{
		if(modal_dialog_win.closed)	{		// If our window is closed, clear out the interval and the invisible overlying div
			remove_window_cover_div();
			return;
		}
		modal_dialog_win.focus();
	}
	catch(all)	{		}
}
// ************************************************************************************************************
// ************************************************************************************************************
// ************************************************************************************************************





String.prototype.isArgument=function()
{
	return /^([a-zA-Z]){1,}=([0-9]){1,}$/.test(this);
}

