// ==============================================================================================
// Don't modify the next 2 functions as these are the window style elements for all grayour popus
// ==============================================================================================
function popup_prefix(window_title, window_width){
    return '<table border="0" cellpadding="0" cellspacing="0" width="' + window_width + '"><tr><td style="background-image: url(/en/images_/popup-window-tl.gif); background-repeat:no-repeat; width:4px; height:29px; background-color:#454545;" border="0"><img src="/en/images_/tr.gif" width="1" height="1" /></td><td style="background-image: url(/en/images_/popup-window-tc.gif); background-repeat:repeat-x; height: 29px;"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td style="padding-left:10px; font-weight:bold;"><span id="popup-title">' + window_title + '</span></td><td><a href="javascript:window.close();"><img id="popup-close" src="/en/images_/popup-window-close.gif" alt="Close" width="70" height="29" align="right"></a></td></tr></table></td><td style="background-image: url(/en/images_/popup-window-tr.gif); background-repeat:no-repeat; width:4px; height:29px; background-color:#454545;" border="0"><img src="/en/images_/tr.gif" width="1" height="1" /></td></tr><tr><td style="background-image: url(/en/images_/popup-window-ml.gif); width:4px; height:4px; background-repeat:repeat-y;" border="0"><img src="/en/images_/tr.gif" width="1" height="1" /></td><td border="0" valign="top" style="padding:10px;">';
}

function popup_prefix_fr(window_title, window_width){
    return '<table border="0" cellpadding="0" cellspacing="0" width="' + window_width + '"><tr><td style="background-image: url(/en/images_/popup-window-tl.gif); background-repeat:no-repeat; width:4px; height:29px; background-color:#454545;" border="0"><img src="/en/images_/tr.gif" width="1" height="1" /></td><td style="background-image: url(/en/images_/popup-window-tc.gif); background-repeat:repeat-x; height: 29px;"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr><td style="padding-left:10px; font-weight:bold;"><span id="popup-title">' + window_title + '</span></td><td><a href="javascript:window.close();"><img id="popup-close" src="/fr/images/popup-window-close.gif" width="90" height="29" alt="Close" align="right"></a></td></tr></table></td><td style="background-image: url(/en/images_/popup-window-tr.gif); background-repeat:no-repeat; width:4px; height:29px; background-color:#454545;" border="0"><img src="/fr/images_/tr.gif" width="1" height="1" /></td></tr><tr><td style="background-image: url(/en/images_/popup-window-ml.gif); width:4px; height:4px; background-repeat:repeat-y;" border="0"><img src="/en/images_/tr.gif" width="1" height="1" /></td><td border="0" valign="top" style="padding:10px;">';
}

function popup_suffix(){
    return '</td><td style="background-image: url(/en/images_/popup-window-mr.gif); width:4px; height:3px; background-repeat:repeat-y;" border="0"><img src="/en/images_/tr.gif" width="1" height="1" /></td></tr><tr><td style="background-image: url(/en/images_/popup-window-bl.gif); background-repeat:no-repeat; width:4px; height:4px;" border="0"><img src="/en/images_/tr.gif" width="1" height="1" /></td><td style="background-image: url(/en/images_/popup-window-bc.gif); background-repeat:repeat-x; height: 4px;"><img src="/en/images_/tr.gif" width="1" height="1" /></td><td style="background-image: url(/en/images_/popup-window-br.gif); background-repeat:no-repeat; width:4px; height:4px;" border="1"><img src="/en/images_/tr.gif" width="1" height="1" /></td></tr></table>';
}
// ==============================================================================================

function popup_lightbox(title, width, divId, language){
    var content = $('#' + divId).html();
    if(language == 'fr')
      grayPopup(true, popup_prefix_fr(title, width) + content + popup_suffix());
    else
      grayPopup(true, popup_prefix(title, width) + content + popup_suffix());
}

function popup(window_title, description, window_width){
    grayPopup(true, popup_prefix(window_title, window_width) + description + popup_suffix());
}

// Now for the code that actually does the grayout popup.

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.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 = 50;
    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.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 = 100;
       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;
