// JavaScript Document
//Class
var g_xModalDialog=new XModalDialog;
var alertFlag ="";
function XModalDialog()
{
    this.show = show;
    this.hide = hide;
        
    //Variables
    var _div = null;
    var _maskDiv = null;
    var _tabIndexes = new Array();
    
    // Pre-defined list of tags we want to disable/enable tabbing into
    var _tabbableTags = new Array("a","button","textarea","input","iframe");	
     
    // If using Mozilla or Firefox, use Tab-key trap.
    if (!document.all) 
    {
	    //document.onkeypress = keyDownHandler;
    }


    function show(divID) 
    {
        var body = document.getElementsByTagName('body')[0];
    	
        TRACE("showDialog: " + divID);
        _div=document.getElementById(divID);
        
        var maskDivID="xModalDialogMask";
        _maskDiv=document.getElementById(maskDivID);
        
        TRACE("showDialog: maskDiv=" + _maskDiv);
        
        if (_maskDiv==null)
        {
            _maskDiv = document.createElement('div');
	        _maskDiv.id = maskDivID;
    	    _maskDiv.style.height = 620+'px';
	        body.appendChild(_maskDiv);
	    }
    	
	    TRACE("showDialog: maskDiv=" + _maskDiv);
       
        _maskDiv.style.display = "block";
        _div.style.display = "block";

	    //centerDialog(_div.style.Width, _div.style.Height);
	  //  center();
    	
	    disableTabIndexes();
    	
	    // for IE
	    if (isIE()) 
	    {
		    hideSelectBoxes();
	    }
    }


    function isVisible()
    { 
       // TRACE("isVisible: _div=" + _div + ", _div.style.display=" + _div.style.display);
       
        if ((_div==null) || (_div.style.display == "none"))
            return false;
        else
            return true; 
    }


    function center() 
    {
        var width=300;
        var height=300;
        
        TRACE("center: width=" + width + ", height=" + height);
        if (!isVisible())
            return;
            
	   // var fullHeight =  getViewportHeight();
		//alert(fullHeight)
		var fullHeight =  1300;
	    var fullWidth = getViewportWidth();
        TRACE("center: fullWidth=" + fullWidth + ", fullHeight=" + fullHeight);
        
	    var theBody = document.documentElement;
    		
	    var scTop = parseInt(theBody.scrollTop,10);
	    var scLeft = parseInt(theBody.scrollLeft,10);
    	
	    _maskDiv.style.height = fullHeight + "px";
	    _maskDiv.style.width = fullWidth + "px";
	    _maskDiv.style.top = scTop + "px";
	    _maskDiv.style.left = scLeft + "px";
    	
	    _div.style.top = (scTop + ((fullHeight - height) / 2)) + "px";
	    _div.style.left =  (scLeft + ((fullWidth - width) / 2)) + "px";
    	
	    TRACE("center: top=" + _div.style.top + ", left=" + _div.style.left );
        
    }


 //   addEvent(window, "scroll", center);
  //  addEvent(window, "resize", center);


    function hide() 
    {
	    if (!isVisible()) 
		    return;
    	
	    restoreTabIndexes();
    	
	    _div.style.display = "none";
	    _maskDiv.style.display = "none";

	    // display all select boxes
	    if (isIE()) 
	    {
		    displaySelectBoxes();
	    }
	   if(alertFlag =='') 
	    {
	    xmlHttpSteps = null;
        // setTimeout('getStep()',5000);
        }
        else
        {
         alertFlag ='';
         window.location="login.aspx"
        }
        
    }


    // Tab key trap. iff popup is shown and key was [TAB], suppress it.
    // @argument e - event - keyboard event that caused this function to be called.
    function keyDownHandler(e) 
    {
        if (isVisible() && e.keyCode == 9)  return false;
    }

    // For IE.  Go through predefined tags and disable tabbing into them.
    function disableTabIndexes() 
    {
	    if (document.all) 
	    {
		    var i = 0;
		    for (var j = 0; j < _tabbableTags.length; j++) 
		    {
			    var tagElements = document.getElementsByTagName(_tabbableTags[j]);
			    for (var k = 0 ; k < tagElements.length; k++) 
			    {
				    _tabIndexes[i] = tagElements[k].tabIndex;
				    tagElements[k].tabIndex="-1";
				    i++;
			    }
		    }
	    }
    }



    // For IE. Restore tab-indexes.
    function restoreTabIndexes() {
	    if (document.all) {
		    var i = 0;
		    for (var j = 0; j < _tabbableTags.length; j++) {
			    var tagElements = document.getElementsByTagName(_tabbableTags[j]);
			    for (var k = 0 ; k < tagElements.length; k++) {
				    tagElements[k].tabIndex = _tabIndexes[i];
				    tagElements[k].tabEnabled = true;
				    i++;
			    }
		    }
	    }
    }


    /**
    * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
    * IE has a problem with wanted select form tags to always be the topmost z-index or layer
    *
    * Thanks for the code Scott!
    */
    function hideSelectBoxes() 
    {
	    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";
			    }
		    }
	    }
    }

    /**
    * Makes all drop down form select boxes on the screen visible so they do not reappear after the dialog is closed.
    * IE has a problem with wanted select form tags to always be the topmost z-index or layer
    */
    function displaySelectBoxes() {
	    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";
			    }
		    }
	    }
    }




    /**
     * X-browser event handler attachment and detachment
     * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
     *
     * @argument obj - the object to attach event to
     * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
     * @argument fn - function to call
     */
    function addEvent(obj, evType, fn)
    {
        if (obj.addEventListener)
        {
            obj.addEventListener(evType, fn, false);
            return true;
        } 
        else if (obj.attachEvent)
        {
            var r = obj.attachEvent("on"+evType, fn);
            return r;
        } 
        else 
        {
            return false;
        }
    }
    
    
    
    function removeEvent(obj, evType, fn, useCapture)
    {
        if (obj.removeEventListener)
        {
            obj.removeEventListener(evType, fn, useCapture);
            return true;
        } 
        else if (obj.detachEvent)
        {
            var r = obj.detachEvent("on"+evType, fn);
            return r;
        } 
        else 
        {
            alert("Handler could not be removed");
        }
    }

    /**
     * Code below taken from - http://www.evolt.org/article/document_body_doctype_switchin_and_more/17/30655/
     *
     * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
     *
     * Gets the full width/height because it's different for most browsers.
     */
    function getViewportHeight() 
    {	//alert('ok')
	   // if (window.innerHeight!=window.undefined) return window.innerHeight;
	  //  if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	 //   if (document.body) return document.body.clientHeight; 
	    return window.undefined; 

    }
    
    
    function getViewportWidth() 
    {
	    if (window.innerWidth!=window.undefined) return window.innerWidth; 
	    if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	    if (document.body) return document.body.clientWidth; 
	    return window.undefined; 
    }

    		
    function isIE()
    {
	    return ( navigator.appName=="Microsoft Internet Explorer" ); 
    }


    function isNav()
    {
	    return ( navigator.appName=="Netscape" );
    }

    function TRACE(sText)
    {
	    var txtTrace=document.getElementById("txtTrace");
	    if (txtTrace!=null)
		    txtTrace.value = txtTrace.value + sText + "\n";
    }

}



