var wndLog           = null;
var xmlHttpReq       = false;
var wndPopup         = null;

$(function()
{
  $.noConflict      ();
  
  jQuery("body").addClass("script");
});

function centerWindow (url, name, w, h, scroll, menu, resizable)
{
  // if width or height specified as zero, then use parent's width and height
  if (w == 0) w = getWindowWidth  () - 20;
  if (h == 0) h = getWindowHeight () - 40;

  if (window.screen)
  {
    var chasm     = screen.availWidth;
    var mount     = screen.availHeight;
    var left      = (chasm - w - 10) * .5;
    var top       = (mount - h - 30) * .5;
    
    var winprops  = 'width=' + w + ',height=' + h + ',left=' + left + ',top=' + top
                  + ',scrollbars=' + scroll + ',menubar=' + menu + ',resizable=' + resizable;

    wndPopup = window.open(url, name, winprops);
    
    if(wndPopup == null) {
      alert("You will need to disable your Pop-Up Blocker to view the product details. If you're using Windows, try holding down the CTRL key while clicking the View Details button; Macintosh users should try holding down control 'z' while clicking on the button.");
    }
    else {
      wndPopup.focus();
    }
    
  }
}


function centerMinimalWindow (url, name, w, h)
{
  return (centerWindow (url, name, w, h, "no", "no", "no"));
}


function getWindowWidth ()
{
  // Returns width of current browser window. If browser is really old, then defaults 630. 
  var width = 630;

  if (parseInt(navigator.appVersion) > 3)
    if (navigator.appName=="Netscape")
      width = window.innerWidth;
    else if (navigator.appName.indexOf("Microsoft") != -1)
      width = document.body.offsetWidth;
  return (width);
}


function getWindowHeight ()
{
  // Returns height of current browser window. If browser is really old, then defaults 450. 
  var height = 450;

  if (parseInt(navigator.appVersion) > 3)
    if (navigator.appName=="Netscape")
      height = window.innerHeight;
    else if (navigator.appName.indexOf("Microsoft") != -1)
      height = document.body.offsetHeight;
  return (height);
}


function getParser (xml)
{
  var doc;

  // IE support
  if (window.ActiveXObject)
  {
    doc = new ActiveXObject ("Microsoft.XMLDOM");
    doc.async = "false";
    doc.loadXML (xml);
  }
  // code for Mozilla, Firefox, Opera, etc.
  else
  {
    var parser = new DOMParser ();
    doc = parser.parseFromString (xml,"text/xml");
  }
  return (doc);
}


function swapImages (oldImage, newImage)
{
  document.getElementById(oldImage).src = eval(newImage + ".src");
}


function logDebug (message)
{
  if (wndLog == null)
    wndLog = window.open ("", "log");
  wndLog.document.write (message + "<br/>");
}


function getXmlHttpReq ()
{
  if (window.XMLHttpRequest)
  { 
    // Mozilla, Safari,...
    xmlHttpReq = new XMLHttpRequest();
    if (xmlHttpReq.overrideMimeType)
      xmlHttpReq.overrideMimeType('text/xml');
  }
  else if (window.ActiveXObject)
  {
    // IE
    try
    {
      xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
      }
    }
  }
}

function setCookie (valueName, value, expiredays)
{
  var expiredate = new Date();
  expiredate.setDate (expiredate.getDate() + expiredays);
  document.cookie = valueName + "=" + value + ";expires=" + expiredate.toGMTString ();
}


function getCookie (valueName)
{
  var value = "";
  var ndxStart;
  var ndxEnd;

  try
  {
    if (document.cookie.length > 0)
    {
      ndxStart = document.cookie.indexOf (valueName + "=");
      if (ndxStart != -1)
      {
        ndxStart += valueName.length + 1;
        ndxEnd    = document.cookie.indexOf (";", ndxStart);
        if (ndxEnd == -1)
          ndxEnd = document.cookie.length;
        value = document.cookie.substring (ndxStart, ndxEnd);
      }
    }
  }
  catch (err)
  {
  }
  return (value);
}

function deleteCookie (valueName)
{
  var expiredate = new Date();
  expiredate.setDate (expiredate.getTime() - 1);
  document.cookie = valueName += "=; expires=" + expiredate.toGMTString();
}

function getQueryVariable(variable)
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++)
  {
    var pair = vars[i].split("=");
    if (pair[0] == variable)
    {
      return pair[1];
    }
  } 
}

