function grayPopup(vis, html){
   grayOut(vis);
   displayHtml(vis, html);
}

function grayOut(vis) {
  var dark=document.getElementById('darkScreen');
  if (!dark) {
     dark = document.createElement('div');
     dark.style.zIndex = 1200;
     dark.style.position = "absolute";
     dark.style.top = "0px";
     dark.style.left = "0px";
     dark.style.overflow = "hidden";
     dark.style.display = "none";
     dark.id = "darkScreen";

     document.getElementsByTagName("body")[0].appendChild(dark);
  }

  hideDropDowns(vis);

  if (vis) {
    setOpacity(dark, 0.7);

    dark.style.zIndex = 1200;
    dark.style.backgroundColor = "#000000";
    dark.style.width = getFullWidth() + "px";
    dark.style.height = getFullHeight() + "px";
    dark.style.display = "block";
  }
  else{
     dark.style.display = "none";
  }
}

function hideDropDowns(vis){
   var dd = document.getElementsByTagName("select");

   if(!vis){
      for(x = 0; x < dd.length; x++){
         dd[x].style.display = dd[x].oldDisplay;
      }
   }
   else{
      for(x = 0; x < dd.length; x++){
         dd[x].oldDisplay = dd[x].style.display;
         dd[x].style.display = "none";
      }
   }
}

function setOpacity(obj, opacity){
   obj.style.opacity = opacity;
   obj.style.MozOpacity = opacity;
   obj.style.filter = "alpha(opacity=" + (opacity*100) + ")";
}

function getScreenHeight(){
	if (self.innerHeight)
		return self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if (document.body)
		return document.body.clientHeight;
}

function getScreenWidth(){
	if (self.innerWidth)
		return self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if (document.body)
		return document.body.clientWidth;
}

function getFullHeight(){
   var d = document.getElementsByTagName("body")[0];
   if(d.scrollHeight)
      return d.scrollHeight;
   else
      return d.offsetHeight;
}

function getFullWidth(){
   var d = document.getElementsByTagName("body")[0];
   if(d.scrollWidth)
      return d.scrollWidth;
   else
      return d.offsetWidth;
}
var keepAdjusting = false;
var adjustWait = 10;
var adjustTotalWait = 100; //10000;
var adjustCount = 0;

function adjustLocation(){
   var display = document.getElementById("displayPopup");
   var t = ((getScreenHeight() - display.offsetHeight)/2) + document.body.scrollTop;
   var l = ((getScreenWidth() - display.offsetWidth)/2) + document.body.scrollLeft;

   if(t < 0) t = 0;
   if(l < 0) l = 0;

   if(keepAdjusting && (adjustCount++*adjustWait)<adjustTotalWait){
	   display.style.top = t + "px";
	   display.style.left = l + "px";
       setTimeout("adjustLocation()",adjustWait);
   }
   else{
      keepAdjusting = false;
      adjustCount = 0;
   }
}

function displayHtml(vis, html){
   var display = document.getElementById("displayPopup");
   if(!display){
      display = document.createElement("div");
      display.style.zIndex = 1200;
      display.style.position = "absolute";
      display.style.overflow = "hidden";
      display.style.display = "none";
      display.id = "displayPopup";

      document.getElementsByTagName("body")[0].appendChild(display);
   }

   keepAdjusting = vis;

   if(vis){
	   display.style.zIndex = 1200;
	   display.style.backgroundColor = "#FFFFFF";
	   display.style.display = "block";

	   display.innerHTML = html;
	   adjustLocation();
	}
	else{
	   display.style.display = "none";
	   display.innerHTML = "";
	}
}


function close(){
   grayPopup(false);
}

window.close = close;