  
  /* copyright (c) 2009 Paul Durdin. All rights reserved. */
  
  function CheckNumeric()
  {
     // Get ASCII value of key that user pressed
     var key = window.event.keyCode;

     // Was key that was pressed a numeric character (0-9)?
     if ( (key > 47 && key < 58) || key == 8 )
        return; // if so, do nothing
     else
        window.event.returnValue = null; // otherwise, 
  	                               // discard character
  }
  
  function expand_group( id ) {
    var elem = document.getElementById( id );
    
    if( !elem ) return false;
    
    if( !elem.style.overflow || elem.style.overflow == "hidden" ) {
      elem.style.height = "auto";
      elem.style.overflow = "auto";
      elem.style.backgroundPosition = "left -30px";
    } else {
      elem.style.height = "30px";
      elem.style.overflow = "hidden";
      elem.style.backgroundPosition = "left top";
    }
  }
  
  function expand_category ( cat_id ) {
    var q = document.getElementById("child_" + cat_id),
        r = document.getElementById("cat_" + cat_id);
    
    if( !q || !r ) return false;
    
    if( !q.style.display || q.style.display == "none" ) { 
      q.style.display = "list-item";
      r.style.height = "auto";
    } else {
      q.style.display = "none";
      r.style.height = "23px";
    }
  }
  
  function expand_current () {
    var cat_id = getURLVar( "cat" );
    
    if( cat_id && cat_id != "sys" && cat_id != "search" && cat_id != "clearance" ) {
      if( cat_id.substring( 0, 1 ) == "c" ) cat_id = cat_id.substring( 1 );
      cat_id = cat_id.replace( "#", "" );
      
      var child = document.getElementById( "cat_" + cat_id );
      if( child ) var parent = child.parentNode;
      
      if( !parent || !child ) return false;
    
      child.style.display = "list-item";
      parent.style.display = "list-item";
      
      parentParent = parent.parentNode;
      parentParent.style.height = "auto";
      
      if( parentParent.parentNode.className == "category_child" ) {
        parentParent.parentNode.style.display = "list-item";
        parentParent.parentNode.parentNode.style.height = "auto";
      }
      elem = child;
      while( elem.parentNode.id != "leftNavList" ) {
        elem = elem.parentNode;
        if( elem.className == "category_group" ) {
          expand_group( elem.id );
        }
      }
    }
  }
  
  function getURLVar( urlVarName ) {
    // divide the URL in half at the '?' 
    var urlHalves = decodeURI(window.document.location).split('?');
    var urlVarValue = '';
    if(urlHalves[1]){
      // load all the name/value pairs into an array 
      var urlVars = urlHalves[1].split('&');
      // loop over the list, and find the specified url variable 
      for(var i=0; i<=(urlVars.length); i++){
        if(urlVars[i]){
          // load the name/value pair into an array 
          var urlVarPair = urlVars[i].split('=');
          if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
            // I found a variable that matches, load it's value into the return variable 
            urlVarValue = urlVarPair[1];
          }
        }
      }
    }
    return urlVarValue;   
  }
  
  function update_build( category, stock, image, qty ) {
    var q = document.getElementById("code_" + category);
    var r = document.getElementById("desc_" + category);
    var s = document.getElementById("price_" + category);
    var t = document.getElementById("item_" + category);
    var u = document.getElementById("catimage_" + category);
    
    var v = document.getElementsByName("radio_" + category);
    var w = document.getElementById("pricedisplay_" + category + "_" + stock);
    var x = document.getElementById("itemqty_" + category);
    
    var subtotal;
    
    if( image == 1 ) {
      u.src = "image.php?q=thumb&id=" + stock;
      u.style.visibility = "visible";
    } else {
      u.style.visibility = "hidden";
    }
    
    subtotal = document.getElementById("stockprice_" + category + "_" + stock).value * qty;
    q.innerHTML = document.getElementById("stockcode_" + category + "_" + stock).innerHTML;
    r.innerHTML = "<strong>" + document.getElementById("stockdesc_" + category + "_" + stock).innerHTML + "</strong>";
    s.innerHTML = "$<strong><a name='price'>" + subtotal.toFixed(2) + "</a></strong>";
    
    if( qty > 0 ) {
      for( i = 0; i < v.length; i++ ) {
        if( v[i].value == stock ) v[i].checked = "checked";
      }
    }
    
    w.innerHTML = "$" + subtotal.toFixed(2);
    
    if( x ) x.value = qty;
    t.value = stock;
    
    update_build_total();
  }
  
  function update_build_price( checkbox, category, stock ) {
    var q = document.getElementById("price_" + stock);
    var r = document.getElementById("stockprice_" + category + "_" + stock);
    
    if( checkbox.checked == true ) {
      if( r ) q.innerHTML = "$<a name='price'>" + r.value + "</a>";
    } else {
      q.innerHTML = "";
      
    }
    
    update_build_total();
  }
  
  function update_build_total() {
    var total = 0;
    var prices = document.getElementsByName("price");
    
    for( var i = 0; i < prices.length; i++ ) {
      if( prices[i].innerHTML ) total = total + parseFloat( prices[i].innerHTML );
    }
    document.getElementById("build_total").innerHTML = "Total: $" + total.toFixed(2);
  }
  
  function expand_kit( category ) {
    var q = document.getElementById("expand_" + category);
    var r = document.getElementsByName("kit_" + category);
    
    if( q.alt == "Expand" ) {
      for( var i = 0; i < r.length; i++ ) {
        r[i].style.display = 'block';
      }
      q.src = SITEPATH + "images/system_contract.png";
      q.alt = "Contract";
    } else {
      for( var i = 0; i < r.length; i++ ) {
        r[i].style.display = 'none';
      }
      q.src = SITEPATH + "images/system_expand.png";
      q.alt = "Expand";
    }
  }
  
  function load_main() {
    if( getURLVar("cat") != "kits" && getURLVar("q") != "sys" ) {
      expand_current();
    }
    if( getURLVar("q") == "newsys" ) update_system_total();
    
    if( getURLVar("cat") == "sys" || getURLVar("cat") == "kit" ) {
      for( var q = 0; q < document.system_form.elements.length; q++ ) {
        if( document.system_form.elements[q].type == "checkbox" && document.system_form.elements[q].checked == true ) {
          var cat_stock = document.system_form.elements[q].name.substr(10);
          var u = cat_stock.indexOf("_");
          var stock = cat_stock.substr(u + 1);
          
          update_build_price( document.system_form.elements[q], stock );
        }
      }
      update_build_total();
    }
    
  }
  
  function submit_form( elem, e ) {
    var keycode = (window.event) ? window.event.keyCode : e.which;
    
    if (keycode == 13) {
      document.getElementById("loginenter").value = "1";
      elem.form.submit();
      
      return false;
    }
  }
  
  function displayPopup( popupElement, event ) {
    var elem = document.getElementById( popupElement ),
        overlay = document.getElementById( "popupOverlay" ),
        page = document.getElementById( "wrapAll" );
    
    if( !elem || !overlay || !page ) return;
  
    if( elem.style.display == "block" ) {
      elem.style.display = "none";
      overlay.style.display = "none";
      page.style.overflow = "visible";
    } else {
      elem.style.display = "block";
      overlay.style.display = "block";
      page.style.overflow = "hidden";
      page.style.position = "absolute";
      page.style.height = "100%";
      
      overlay.onclick = function() {
        document.getElementById( popupElement ).style.display = "none";
        overlay.style.display = "none";
      }
      
    }
    avoidBubble( event );
  }

  // high slide preparation stuff
  hs.registerOverlay({
    overlayId: 'closebutton',
    position: 'top left',
    fade: 2, 
    useOnHtml: true
  });
  hs.graphicsDir = 'js/highslide/graphics/';
  hs.showCredits = false;
  hs.outlineType = 'rounded-white';
  hs.wrapperClassName = 'borderless';
  hs.dimmingOpacity = 0.5;
  hs.cacheAjax = false;
  
  if (!hs.ie || hs.uaVersion > 6) hs.extend ( hs.Expander.prototype, {
  	fix: function(on) {
  		var sign = on ? -1 : 1,
  			stl = this.wrapper.style;
   
  		if (!on) hs.getPageSize(); // recalculate scroll positions
  		
  		hs.setStyles (this.wrapper, {
  			position: on ? 'fixed' : 'absolute',
  			zoom: 1, // IE7 hasLayout bug,
  			left: (parseInt(stl.left) + sign * hs.page.scrollLeft) +'px',
  			top: (parseInt(stl.top) + sign * hs.page.scrollTop) +'px'
  		});
   
  		if (this.outline) {
  			stl = this.outline.table.style;
  			hs.setStyles (this.outline.table, {
  				position: on ? 'fixed' : 'absolute',
  				zoom: 1, // IE7 hasLayout bug,
  				left: (parseInt(stl.left) + sign * hs.page.scrollLeft) +'px',
  				top: (parseInt(stl.top) + sign * hs.page.scrollTop) +'px'
  			});
   
  		}
  		this.fixed = on; // flag for use on dragging
  	},
  	onAfterExpand: function() {
      	this.fix(true); // fix the popup to viewport coordinates
        if( document.getElementById( "pageBody" ) ) {
          document.getElementById("pageBody").style.paddingRight = '18px';
          document.getElementById("pageBody").style.overflow = 'hidden';
        }
  	},
   
  	onBeforeClose: function() {
  		this.fix(false); // unfix to get the animation right
      if( document.getElementById( "pageBody" ) ) {
        document.getElementById("pageBody").style.paddingRight = '0';
        document.getElementById("pageBody").style.overflow = 'auto';
      }
  	},
   
      onDrop: function() {
      	this.fix(true); // fix it again after dragging
  	},
   
  	onDrag: function(sender, args) {
  		//if (this.fixed) { // only unfix it on the first drag event
  			this.fix(true);
  		//}
  	}
   
  });
