var w_privacypolicy;
var w_productinfo;
var w_login;
var w_popup;
var w_producttour;
var w_help;

var $_ = function(id) {
   return (document.getElementById ? document.getElementById(id) :
           (document.all ? eval('document.all.' + id) : null));
};

/**
 * popup - open general popup window.
 * - url (string) - URL to open
 * - width (int) - width in pixels
 * - height (int) - height in pixels
 * - scrollbars (string) - if "1", show scrollbars
 * - toolbar (string) - if "1", show toolbar
 * - x (int) - x position in pixels
 * - y (int) - y position in pixels
 * - win (var) - window object to use
 * - winname (string) - window name to use
 */
function popup(url, width, height, scrollbars, toolbar, x, y, win, winname) {

   if (win == null) win = w_popup;
   if (winname == null) winname = "popup_window";
   // check for existing popup
   if (! win || win.closed) {
      // set defaults
      if (width == null) width = 570;
      if (height == null) height = 510;
      if (scrollbars == null) scrollbars = 1;
      if (toolbar == null) toolbar = 0;
      if (x == null) x = 50;
      if (y == null) y = 20;
      win = open(url, winname, "directories=0,location=0,menubar=0,resizable=yes,scrollbars=" + scrollbars + ",status=0,toolbar=" + toolbar + ",width=" + width + ",height=" + height + ",left=" + x + ",top=" + y);
      if (! win.opener)
         win.opener = self;
      if (win)
         win.focus();
   } else {
      win.focus();
      win.location = url;
   }
}

/**
 * privacypolicy - open privacy policy window.
 * - url (string) - URL to open
 */
function privacypolicy(url) {
   popup(url, 550, 500, 1, 0, w_privacypolicy, "privacypolicy_window");
}

/**
 * productinfo - open product information request window.
 * - url (string) - URL to open
 */
function productinfo(url) {
   popup(url, 550, 500, 1, 0, w_productinfo, "productinfo_window");
}

/**
 * producttour - open product tour window.
 * - url (string) - URL to open
 */
function producttour(url) {
   popup(url, 580, 450, 0, 0, w_producttour, "producttour_window");
}

/**
 * login - open login window.
 * - url (string) - URL to open
 */
function login(url) {
   popup(url, 550, 500, 0, 0, w_login, "login_window");
}

/**
 * printhelp - open print help window.
 * - url (string) - URL to open
 */
function printhelp(url) {
   popup(url, 560, 600, null, null, null, null, w_help, 'help_window');
}

/**
 * cult3d - open cult3d popup window.
 * - url (string) - URL to open
 */
function cult3d(url) {
   self.name = "parent";
   popup(url, 600, 460, 0, 0, null, "popup_cult3d");
}

/**
 * loadpage - open URL given at dropdown jump menu.
 */
function loadpage() {
   windows.location = document.jump.menu.options[document.jump.menu.selectedIndex].value;
}

/**
 * toggleDiv - toggles given div areas to be visible or not based on a boolean
 * input value.
 * - controlValue (boolean)
 * - divOnName (string) - name of the div to turn on
 * - divOffName (string) - name of the div to turn off
 */
function toggleDiv(controlValue, divOnName, divOffName) {

   // get div objects
   var oDivOn = $_(divOnName);
   var oDivOff = $_(divOffName);
   if (controlValue) {
      if (oDivOn && oDivOn.style)
         oDivOn.style.display = '';
      if (oDivOff && oDivOff.style)
         oDivOff.style.display = 'none';
   } else {
      if (oDivOn && oDivOn.style)
         oDivOn.style.display = 'none';
      if (oDivOff && oDivOff.style)
         oDivOff.style.display = '';
   }
}

function toggleDiv2(divName) {
   var oDiv = $_(divName);
   if (oDiv) {
      var isVisible = (oDiv.style.display == '');
      if (isVisible)
         toggleDiv(true, null, divName);
      else
         toggleDiv(true, divName, null);
   }
}

function showHideDiv(divName, showFunc, hideFunc) {
   var oDiv = $_(divName);
   if (oDiv) {
      if (oDiv.style.display == '') {
         toggleDiv(true, null, divName);
         if (typeof(hideFunc) == 'function')
            hideFunc();
      } else {
         toggleDiv(true, divName, null);
         if (typeof(showFunc) == 'function')
            showFunc();
      }
   }
}

/**
 * searchFocus - called when the search box receives focus.
 * - searchBox (HTML text box) - search text box
 * - watermarkText (String) - watermark text
 */
function searchFocus(searchBox, watermarkText) {
   if (searchBox.value == watermarkText)
      searchBox.value = "";
   searchBox.style.color = "#000000";
   searchBox.style.backgroundColor = "#FFFFFF";
}

/**
 * searchBlur - called when the search box loses focus.
 * - searchBox (HTML text box) - search text box
 * - watermarkText (String) - watermark text
 */
function searchBlur(searchBox, watermarkText) {
   if (searchBox.value == "")
      searchBox.value = watermarkText;
   searchBox.style.color = "#FFFFFF";
   searchBox.style.backgroundColor = "#000000";
}

/**
 * resizeContentArea - resize content window based on browser window size.
 * - scrollArea (string) - div name of scrollable area
 * - remainHeight (int) - remaining window height not used by scroll area
 */
function resizeContentArea(scrollArea, remainHeight) {

   // get scroll area
   var oContent = $_(scrollArea);
   var varHeights = 0;
   $(".variableheight").each(function(index) {
      varHeights += $(this).height();
   });
   var contentHeight = (window.innerHeight ? window.innerHeight :
      document.body.clientHeight) - remainHeight - varHeights;
   oContent.style.height = contentHeight + "px";
}

/**
 * pleaseWait - used in conjunction with a page waiting message; this will close
 * that message page.
 * Usage:
 * 1) Include the Javascript source file within the HTML header, i.e.
 *    <script language="Javascript" src="/includes/js/pleasewait.js">
 * 2) Add the following to the HTML (before the Body tag)
 *    <%@include file="/includes/hidepage.inc" %>
 * 3) Call it within the HTML Body tag
 *    <body ... onLoad="pleaseWait();">
 */
function pleaseWait() {
	if (document.layers)
		document.hidepage.visibility = "hidden";
	else
		document.all.hidepage.style.visibility = "hidden";
}

function getFlash(movieName) {
   if (window.document[movieName])
      return window.document[movieName];
   if (navigator.appName.indexOf("Microsoft Internet") == -1) {
      if (document.embeds && document.embeds[movieName])
         return document.embeds[movieName];
      else
         return null;
   } else
      return document.getElementById(movieName);
}

function startFlash(movieName) {
   oFlash = getFlash(movieName);
   if (oFlash) {
      oFlash.playMediaInFlash();
   }
}

function stopFlash(movieName) {
   oFlash = getFlash(movieName);
   if (oFlash) {
      oFlash.pauseMediaInFlash();
   }
}

function showDateTime(elem, ticks) {
   passedDate = (ticks != null);
   showDate = new Date();
   if (passedDate)
      showDate.setTime(ticks);

   datetimeElem = $_(elem);
   if (! datetimeElem)
      return;

   year = showDate.getFullYear();
   month = showDate.getMonth();
   day = showDate.getDate();
   hours = showDate.getHours();
   minutes = showDate.getMinutes();
   seconds = showDate.getSeconds();
   timezone = "PM";
   if (hours < 12)
      timezone = "AM";
   if (hours > 12)
      hours = hours - 12;
   else if (hours == 0)
      hours = 12;
   if (minutes <= 9)
      minutes = "0" + minutes;
   if (seconds <= 9)
      seconds = "0" + seconds;
   displayDate = (month + 1) + "/" + day + "/" + year;
   displayTime = hours + ":" + minutes + ":" + seconds + " " + timezone;
   datetimeElem.innerHTML = displayDate + " " + displayTime;

   nextticks = ticks + 1000;
   setTimeout("showDateTime(\"" + elem + "\"," + nextticks + ")", 1000);
}

function showDateTimeFromUtc(elem, ticks) {
   passedDate = (ticks != null);
   showDate = new Date();
   if (passedDate)
      showDate.setTime(ticks + (showDate.getTimezoneOffset() * 60 * 1000));
   showDateTime(elem, showDate.getTime());
}

