
if (!document.all) {
    /* orhginal code by jason@jasontaylor.co.nz
       modified by Eric Joyce
       give mozilla window. showModalDialog function call compatible with ie
       fixed bug when a modal is created from a modal
       window only fires closeWindow if the popup dialog calls it, unhooked call from document.unload
       If the window reloads the close event fired, in 99% of the cases we want it when the window closes
       regardless of the windows document changing.  When the window opens the event handler is saved, then disabled
       the page must provide a way to close the window & reenable the event.
     */
    eval("var dialogArguments;");
    function modalWindow(width, height) {
		this.width=width;
		this.height=height;
		this.id = modalWindow.stack.push(this) - 1;
		this.args=null;
    }
     modalWindow.prototype._onclose = function(value) {
	    window.onfocus = function() {};
            modalWindow.current = false;

	    modalWindow.stack.pop();
	    //this.onclose(value);
	    doModalClose(value);
      };
	//this.onclose = function(value){}
      modalWindow.prototype.getDialogArguments = function() {
	    return this.args;
	  };

      modalWindow.prototype.open = function(url, _args, override) {
	    this.args = _args;
	    this.closeFunction=function(){
		doModalClose();
	    };

            var LeftPosition = (screen.width - this.width) / 2;
	    var TopPosition = (screen.height - this.height) / 2;
	    modalWindow.current =
		window.open(url, 'ModalWindow_' + this.id,'top=' + TopPosition + ',left=' + LeftPosition + ',width=' + (this.width|| 300) + ',height=' + (this.height|| 180) +',modal,dialog,scrollbars');
	    this.win = modalWindow.current;
	    window.onfocus = function() {
		modalWindow.current.focus();
	    };
    };

    modalWindow.prototype.__defineGetter__("dialogArguments", function() {
					   return this.args;
					   }
    );

    modalWindow.stack =[];
// Set up modal window features for Moz
    modalWindow.current = false;
   if (window.opener && window.name.split('_')[0] == "ModalWindow") {
	if (!window.returnValue){
	    window.returnValue = false;
        }
	var _creator = window.opener.modalWindow.stack[window.name.split('_')[1]];
//stack is null
	if (_creator) {
		dialogArguments = _creator.getDialogArguments();
	    	window.onunload = function(e) {
			_creator._onclose(returnValue);
	    	};
		window.onblur = function() {
			try {
		    		window.focus();
			}
			catch(e) {}
	    	};
	}
	//else{
	//alert("An error has occured please close all popup windows and try again.");
	//window document changes but win is still open
	//}
    } else {
	var w = new modalWindow(0, 0);
    }

   
   if(typeof(Window)!="undefined" && Window=="[object Window]"){	   
    Window.prototype.showModalDialog = function(src, parameters, options) {
    	var params = options.split(";");
    	var widthp = params[0].split(":");
    	var w = widthp[1];
    	w = w.substr(0, w.length - 2);
    	//10 px padd for possible scrollbars
    	var width = (Number(w) + 10) + "px";

    	var heightp = params[1].split(":");
    	var h = heightp[1];
    	h = h.substr(0, h.length - 2);
    	//10 px padd for possible scrollbars
    	var height = (Number(h) + 10) + "px";

    	var newwin = new modalWindow(width, height);
    	//w.onclose = function(v){doModalClose(v)};
    	newwin.open(src, parameters);
    };
   }else{
	   //Code for Safari/Chrome/Webkit
	   window.showModalDialog = function(src, parameters, options) {
	    	var params = options.split(";");
	    	var widthp = params[0].split(":");
	    	var w = widthp[1];
	    	w = w.substr(0, w.length - 2);
	    	//10 px padd for possible scrollbars
	    	var width = (Number(w) + 10) + "px";

	    	var heightp = params[1].split(":");
	    	var h = heightp[1];
	    	h = h.substr(0, h.length - 2);
	    	//10 px padd for possible scrollbars
	    	var height = (Number(h) + 10) + "px";

	    	var newwin = new modalWindow(width, height);
	    	//w.onclose = function(v){doModalClose(v)};
	    	newwin.open(src, parameters);
	    };
   }
    /*
       Code from: http://webfx.eae.net/dhtml/ieemu
       Add some of IE's DOM functionality to mozilla
     */
    HTMLElement.prototype.insertAdjacentHTML = function(sWhere, sHTML) {
// : DocumentFragment
	var df;
	var r = this.ownerDocument.createRange();
// convert to string and unify case
	switch (String(sWhere).toLowerCase()) {
	case "beforebegin":
	    r.setStartBefore(this);
	    df = r.createContextualFragment(sHTML);
	    this.parentNode.insertBefore(df, this);
	    break;

	case "afterbegin":
	    r.selectNodeContents(this);
	    r.collapse(true);
	    df = r.createContextualFragment(sHTML);
	    this.insertBefore(df, this.firstChild);
	    break;

	case "beforeend":
	    r.selectNodeContents(this);
	    r.collapse(false);
	    df = r.createContextualFragment(sHTML);
	    this.appendChild(df);
	    break;

	case "afterend":
	    r.setStartAfter(this);
	    df = r.createContextualFragment(sHTML);
	    this.parentNode.insertBefore(df, this.nextSibling);
	    break;
	}
    };

    HTMLElement.prototype.__defineGetter__("parentElement", function() {
	if (this.parentNode == this.ownerDocument){
            return null;
        }
        return this.parentNode;});

    HTMLElement.prototype.contains = function(oEl) {
	if (oEl == this){
	    return true;
        }
	if (!oEl){
	    return false;
        }
	return this.contains(oEl.parentNode);
    };

    HTMLElement.prototype.__defineSetter__("innerText", function(sText) {
					   this.innerHTML =
					   sText.replace(/\&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");}
    );
    HTMLElement.prototype.__defineGetter__("innerText", function() {
					   var r = this.ownerDocument.createRange();
					   r.selectNodeContents(this); return r.toString();}
    );

    HTMLElement.prototype.__defineGetter__("innerText", function() {
					   var r = this.ownerDocument.createRange();
					   r.selectNodeContents(this); return r.toString();}
    );

    HTMLFrameElement.prototype.__defineGetter__('document',function(){return this.contentDocument;});
    HTMLDocument.prototype.__defineGetter__('frames',function(){return this.getElementsByTagName("FRAME");});
}
