//====================================================
window.onscroll = popupautomove;
function showPopup(script, params)
{
  $("#poploader").addClass("popuploader").css({display: 'block', left: getCenter($("#poploader")).x+'px', top: getCenter($("#poploader")).y+'px'});
  $("#popbacklayer").attr("style", "display: block; filter:alpha(opacity=0);opacity:0;");
  $("#popbacklayer").width('100%').height(getPageSize().pH).animate({opacity:"0.6"}, 500);
  //document.getElementById("popbacklayer").onclick = hidePopup;
  $.post(script, params, response);
  
  return false;
}

function showLoader()
{
  $("#poploader").addClass("popuploader").css({display: 'block', left: getCenter($("#poploader")).x+'px', top: getCenter($("#poploader")).y+'px'});
  $("#popbacklayer").attr("style", "display: block; filter:alpha(opacity=0);opacity:0;");
  $("#popbacklayer").width('100%').height(getPageSize().pH).animate({opacity:"0.6"}, 200);
}

function hideLoader()
{
  $("#popbacklayer").fadeOut("fast");
  $("#poploader").attr("style", "display:none;");
}

function response(data)
{
  $("#popupdialog").append('<table cellspacing="1" cellpadding="1" width="100%"><tr><td width="100%" id="header"><img src="../imgs/close.gif" onClick="hidePopup();"/></td></tr></table>');
  $("#popupdialog").append(data);
  if($("#popupdialog").width() < 450) $("#popupdialog").width(450);
  $("#poploader").attr("style", "display:none;");
  $("#popupdialog").css({display: 'block', left: getCenter($("#popupdialog")).x+'px', top: getCenter($("#popupdialog")).y+'px'});
}

function hidePopup()
{
  $("#popbacklayer").fadeOut("slow");
  $("#poploader").attr("style", "display:none;");
  $("#popupdialog").attr("style", "display:none;").empty();
}

function popupautomove()
{
  $("#popupdialog").css({left: getCenter($("#popupdialog")).x+'px', top: getCenter($("#popupdialog")).y+'px'});
  $("#poploader").css({left: getCenter($("#poploader")).x+'px', top: getCenter($("#poploader")).y+'px'});
}

function getCenter(el)
{
  return {x: (getPageSize().pW - el.width())  / 2, y: (getPageSize().wH - el.height()) / 2 - 50 + (document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop)};
}

function  getPageSize()
{
  var xScroll, yScroll;
  if (window.innerHeight && window.scrollMaxY)
  {
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  }
  else if (document.body.scrollHeight > document.body.offsetHeight)// all but Explorer Mac
  { 
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  }
  else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight) // Explorer 6 strict mode
  {
    xScroll = document.documentElement.scrollWidth;
    yScroll = document.documentElement.scrollHeight;
  }
  else  // Explorer Mac...would also work in Mozilla and Safari
  {
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;
  if (self.innerHeight) // all except Explorer
  {
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
  {
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight)
  {
    pageHeight = windowHeight;
  }
  else
  {
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth)
  {
    pageWidth = windowWidth;
  }
  else
  {
    pageWidth = xScroll;
  }

  return {pW: pageWidth, pH: pageHeight, wW: windowWidth, wH: windowHeight};
}
//====================================================
