var buttonImgs;

// Preloads all selected versions of button images
function preloadImgs()
{
  if(document.images) // Make sure browser supports the image object
  {
    buttonImgs = new Array();
    buttonImgs['banner-privsec'] = new Image();
    buttonImgs['banner-reasons'] = new Image();
    buttonImgs['banner-about'] = new Image();
    buttonImgs['banner-contact'] = new Image();
    buttonImgs['banner-home'] = new Image();
    buttonImgs['banner-search'] = new Image();
    buttonImgs['banner-performance'] = new Image();
    buttonImgs['banner-styling'] = new Image();
    buttonImgs['banner-accessories'] = new Image();
    //buttonImgs['banner-showcase'] = new Image();
    buttonImgs['banner-other-parts'] = new Image();
    buttonImgs['banner-customer-support'] = new Image();
    buttonImgs['banner-articles-news'] = new Image();
    buttonImgs['catalog-engine'] = new Image();
    buttonImgs['catalog-chassis-suspension'] = new Image();
    buttonImgs['catalog-exterior'] = new Image();
    buttonImgs['catalog-wheels-tires'] = new Image();
    buttonImgs['catalog-interior'] = new Image();
    buttonImgs['catalog-accessories'] = new Image();
    buttonImgs['catalog-misc-parts'] = new Image();
    buttonImgs['catalog-suspension'] = new Image();
    buttonImgs['catalog-brakes'] = new Image();
    
    buttonImgs['banner-privsec'].src = 'images/banner-privsec-sel.gif';
    buttonImgs['banner-reasons'].src = 'images/banner-reasons-sel.gif';
    buttonImgs['banner-about'].src = 'images/banner-about-sel.gif';
    buttonImgs['banner-contact'].src = 'images/banner-contact-sel.gif';
    buttonImgs['banner-home'].src = 'images/banner-home-sel.gif';
    buttonImgs['banner-search'].src = 'images/banner-search-sel.gif';
    buttonImgs['banner-performance'].src = 'images/banner-performance-sel.gif';
    buttonImgs['banner-styling'].src = 'images/banner-styling-sel.gif';
    buttonImgs['banner-accessories'].src = 'images/banner-accessories-sel.gif';
    //buttonImgs['banner-showcase'].src = 'images/banner-showcase-sel.gif';
    buttonImgs['banner-other-parts'].src = 'images/banner-other-parts-sel.gif';
    buttonImgs['banner-customer-support'].src = 'images/banner-customer-support-sel.gif';
    buttonImgs['banner-articles-news'].src = 'images/banner-articles-news-sel.gif';
    buttonImgs['catalog-engine'].src = 'images/categories/catalog-engine-sel.gif';
    buttonImgs['catalog-chassis-suspension'].src = 'images/categories/catalog-chassis-suspension-sel.gif';
    buttonImgs['catalog-exterior'].src = 'images/categories/catalog-exterior-sel.gif';
    buttonImgs['catalog-wheels-tires'].src = 'images/categories/catalog-wheels-tires-sel.gif';
    buttonImgs['catalog-interior'].src = 'images/categories/catalog-interior-sel.gif';
    buttonImgs['catalog-accessories'].src = 'images/categories/catalog-accessories-sel.gif';
    buttonImgs['catalog-misc-parts'].src = 'images/categories/catalog-misc-parts-sel.gif';
    
    buttonImgs['catalog-suspension'].src = 'images/categories/catalog-suspension-sel.gif';
    buttonImgs['catalog-brakes'].src = 'images/categories/catalog-brakes-sel.gif';
    
  }
}

// Swaps a button image
function swapImg(buttonObj, direction)
{
  var tempObj, buttonRootName;

  if(buttonObj.complete)
  {
    if(!(direction == 'out' && buttonObj.src.indexOf('sel.gif') < 0) && !(direction == 'in' && buttonObj.src.indexOf('sel.gif') > 0))
    {
      buttonRootName = buttonObj.src.replace(/.*images\//gmi, '').replace(/categories\//gmi, '').replace(/-sel\.gif/gmi, '').replace(/\.gif/gmi, '');
      if(buttonImgs && buttonImgs[buttonRootName] && buttonImgs[buttonRootName].complete)
      {
        tempObj = new Image();
        tempObj.src = buttonObj.src;
        buttonObj.src = buttonImgs[buttonRootName].src;
        buttonImgs[buttonRootName].src = tempObj.src;
      }
    }
  }
}

function addBookmark()
{
  if(document.all)
    window.external.AddFavorite('http://www.procivic.com', 'PRO Civic.com - Honda Civic Parts Specialists');
  else if(window.sidebar)
    window.sidebar.addPanel('PRO Civic.com - Honda Civic Parts Specialists', 'http://www.procivic.com', '');
}

function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=450,height=280,screenX=150,screenY=150,top=150,left=150')
}

function popupWindowDyn(url) {
  var application_box = document.cart_quantity.elements['id[232323]'];
  var application_id = application_box.options[application_box.selectedIndex].value;

  if(application_id != -1) {
    var car_image_id = attribute_id_to_car_image_id[application_id];

  if(car_image_id != "")
    url = url + "?image_id_selected=" + car_image_id;

  }
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150');
}

function make_visible()
{
  document.getElementById('image_context').style.visibility = 'visible';
}

function show_application_info()
{
  var app_notes, list_price, our_price, savings, content;
  var application_box = document.cart_quantity.elements['id[232323]'];
  var application_id = application_box.options[application_box.selectedIndex].value;
  var car_image_id = attribute_id_to_car_image_id[application_id];
  var image_url;

  if(application_id == -1)
    image_url = image_id_to_pic['default'];
  else
    image_url = image_id_to_pic[car_image_id];

  curImg = document.getElementById('image_context').src.replace(/.*images\//gmi, '');
  newImg = image_url.replace(/.*images\//gmi, '');

  if(curImg != newImg)
  {
    if(document.getElementById('image_context').style.visibility != 'hidden') {
      document.getElementById('image_context').style.visibility = 'hidden';
      setTimeout('show_application_info()', 10);
      return;
    }

    if(application_id == -1) {
      document.getElementById('image_context').height = image_id_to_dims_height['default'];
      document.getElementById('image_context').width = image_id_to_dims_width['default'];
    }
    else {
      document.getElementById('image_context').height = image_id_to_dims_height[car_image_id];
      document.getElementById('image_context').width = image_id_to_dims_width[car_image_id];
    }
    document.getElementById('image_context').onload = function() { this.style.visibility = 'visible' };
    document.getElementById('image_context').src = image_url;
  }

  if(application_id == -1)
    return;

  app_notes = new getObj(application_id + '_app_notes');
  app_notes = app_notes.innerHTML;

  list_price = new getObj(application_id + '_list_price');
  list_price = list_price.innerHTML.replace(/\$/gmi, '');

  our_price = new getObj(application_id + '_our_price');
  our_price = our_price.innerHTML.replace(/\$/gmi, '');

  savings = Math.round((parseFloat(list_price) - parseFloat(our_price)) * 100) / 100;
  savings = savings.toFixed(2);

  // combine everything
  content = 'Part Description:<br/>' + app_notes + '<br/>'
    + '<table cellspacing="0" cellpadding="2" class="price_box">'
    + '<tr align="right"><td style="font-size: 12px;">List Price:</td><td>&nbsp;<span class="list_price"><b>$' + list_price + '</b></span></td></tr>'
    + '<tr align="right"><td valign="top" style="font-size: 12px; border-bottom: 1px solid rgb(60, 60, 60);">Our Price:</td><td style="border-bottom: 1px solid rgb(60, 60, 60);">&nbsp;<span class="our_price"><b>$' + our_price + '</b></span></td></tr>'
    + '<tr align="right"><td style="font-size: 12px;">You Save:</td><td>&nbsp;<span class="savings"><b>$' + savings + '</b></span></td></tr>'
    + '</table>'
    + '<div style="margin-top: 2px; margin-left: 1em; font-size: 10px;">(<i><a href="pages-price_match/index.html">110% Lowest Price Guarantee</a></i>)</div>';

  writit(content, 'application_display_area');
}

function writit(text, id)
{
  if (document.getElementById)
  {
    x = document.getElementById(id);
    x.innerHTML = '';
    x.innerHTML = text;
  }
  else if (document.all)
  {
    x = document.all[id];
    x.innerHTML = text;
  }
  else if (document.layers)
  {
    x = document.layers[id];
    x.document.open();
    x.document.write(text);
    x.document.close();
  }
}

function getObj(name)
{
  if (document.getElementById)
  {
    this.obj = document.getElementById(name);
    this.style = document.getElementById(name).style;
    this.innerHTML = document.getElementById(name).innerHTML;
  }
  else if (document.all)
  {
    this.obj = document.all[name];
    this.style = document.all[name].style;
    this.innerHTML = document.all[name].innerHTML;
  }
  else if (document.layers)
  {
    this.obj = getObjNN4(document,name);
    this.style = this.obj;
  }
}

function getObjNN4(obj,name)
{
  var x = obj.layers;
  var foundLayer;
  for (var i=0;i<x.length;i++)
  {
    if (x[i].id == name)
        foundLayer = x[i];
    else if (x[i].layers.length)
        var tmp = getObjNN4(x[i],name);
    if (tmp) foundLayer = tmp;
  }
  return foundLayer;
}

function pi_check_for_application() {
  var application_box = document.cart_quantity.elements['id[232323]'];
  var application_id = application_box.options[application_box.selectedIndex].value;

  if(application_id == -1 ) {
    alert('Please select an application using the pulldown menu.');
    return false;
  }
  else {
    document.cart_quantity.submit();
  }
}

function coc_check_application() {
  var car_type_text = document.checkout_confirmation.car_type;
  var car_type = car_type_text.value;

  if(car_type.replace(/ /gmi, '') == '') {
    alert('Please enter your Civic\'s Year, Submodel, and Trim Level.');
    return false;
  }
  else {
    document.checkout_confirmation.submit();
  }
}

function garage_check() {
  var A;

  try {
    A = new ActiveXObject('Msxml2.XMLHTTP');
  }
  catch(e) {
    try {
      A = new ActiveXObject('Microsoft.XMLHTTP');
    }
    catch(oc) {
      A = null;
    }
  }
  if(!A && typeof(XMLHttpRequest) != 'undefined')
    A = new XMLHttpRequest();
  if(!A)
    alert('Your security settings are too high for the Garage to work. If you would like to use the Garage, please lower your security settings to Medium (specifically allow ActiveX scripts to run). You can always raise your security settings once you are done viewing our site.');
}
