function addCable(id) {
    nextCableId++;
    
    // setup variables
    var template = $('cableTemplate');
    var descCell = $('cableDesc');
    var pricesCell = $('cablePrices');
    var qtyCell = $('cableQty');
    var targetRow = $('targetRow');
    
    // get and parse template text
    var templateText = template.innerHTML;
    var length = products[id].get('item_no').substring(3);
    var price  = products[id].get('amount');

    templateText = templateText.replace(/__template__/g, nextCableId);
    templateText = templateText.replace(/__length__/g, length);
    
    var tbody = $('firstBody');
    var row = document.createElement("TR");
    row.setAttribute("id", "row_"+nextCableId);
    row.className = "newRow" + nextCableId % 2;

    var cell1 = document.createElement("TD");
    cell1.innerHTML = templateText;
    cell1.setAttribute('valign', 'top');

    var cell2 = document.createElement("TD");
    cell2.innerHTML = price;
    cell2.setAttribute('valign', 'top');
    
    var cell3 = document.createElement("TD");
    cell3.setAttribute('valign', 'top');
    cell3.innerHTML = '1';

    var inp1 =  document.createElement("INPUT");
    inp1.setAttribute("type", "hidden");
    inp1.setAttribute("size", "5");
    inp1.setAttribute("id", "add_"+nextCableId);
    inp1.setAttribute("name", "add_"+nextCableId);
    inp1.setAttribute("value", "1");
    
    inp1.onchange  = function() { updateCart(); };
    cell3.appendChild(inp1); 

    var inp2 =  document.createElement("INPUT");
    inp2.setAttribute("type", "hidden");
    inp2.setAttribute("name", "pid_"+nextCableId);
    inp2.setAttribute("id", "pid_"+nextCableId);
    inp2.setAttribute("value", id);
    cell3.appendChild(inp2); 

    row.appendChild(cell1);
    row.appendChild(cell2);
    row.appendChild(cell3);
    tbody.appendChild(row);
    
    // restore to default
    $('grizzly_cable_length').selectedIndex = 0;
    $('cablePrice').innerHTML = '';

    updateCart();
}

function removeCable(id)
{
    try{
        var olddiv = $("row_"+id);
        $('firstBody').removeChild(olddiv);
        updateCart();
    }
    catch(ex) {
        true;
    }
}

function updateTotal() {
    eval('var temp = shipping'+$("cart_form").shipping.value);
    
    $('total').innerHTML = number_format(1*$('subtotal').innerHTML+1*temp+1*$('tax').innerHTML, 2);
  }

  function stateChange(state) {
    if (state == 'CA') {
      $('salestax').style.display = 'block'; 
    }
    else if (state == 'AK' || state == 'HI') {
      $('salestax').style.display = 'none';
      $("cart_form").salestax.selectedIndex = 0;
      alert('Sorry, but we do not ship to Alaska or Hawaii');
    } 
    else{
      $('salestax').style.display = 'none';
      $("cart_form").salestax.selectedIndex = 0;
    }

    calculateTax();
  }

  function calculateTax() {
    var rate = $("cart_form").salestax.value;
    
    $('tax').innerHTML = number_format(.01*rate*$('subtotal').innerHTML, 2);

    updateTotal();
  }

  function number_format(num, dec) {
    mul = Math.pow(10,dec);
    num = num*mul;
    num = Math.round(num);
    num = num/mul;
    var numstr=String(num);
    if(numstr.indexOf(".") == -1) {
      numstr = numstr+".";
      
      for(nfi=0;nfi<dec;nfi++) numstr = numstr+"0";
    }
    
    decpl = numstr.length - numstr.indexOf(".");
    decpl = decpl - 1;
    
    if (decpl < dec) {
      for(nfi = decpl; nfi < dec; nfi++) numstr = numstr+"0";
    }
  
    return (numstr);
  }

  function updateCart() {
    var total = 0;
    var foundId;

    highestId = 0;
    var highestValue = 0;

    form = $("cart_form");
    
    shippingGround = shippingBlue = shippingRed = 0;

    $('cart_form').getElements().each(function(elem) {
      if (elem.name.match(/add_\d+/) && $F(elem) > 0) {
        id = $F('pid_' + elem.name.match(/add_(\d+)/)[1]);
        if (highestValue < products[id].get('shippingGround')) {
          highestId = id;
          highestValue = products[highestId].get('shippingGround');
        }
      }
    });

    $('cart_form').getElements().each(function(elem) {
      if (elem.name.match(/add_\d+/)) {
        elem.value = $F(elem).replace(/[^\d]/g, '');
        
        if ($F(elem) <= 0) {
          return;
        }
        
        id = $F('pid_' + elem.name.match(/add_(\d+)/)[1]);
        // calculate total
        total = (1*total)+$F(elem) * products[id].get('amount');
        
        // calculate shipping totals
        
        if (highestId == id) {
          shippingGround += 1 * products[id].get('shippingGround');
          shippingBlue   += 1 * products[id].get('shippingBlue');
          shippingRed    += 1 * products[id].get('shippingRed');

          shippingGround += ($F(elem) - 1) * products[id].get('shippingGroundMult');
          shippingBlue   += ($F(elem) - 1) * products[id].get('shippingBlueMult');
          shippingRed    += ($F(elem) - 1) * products[id].get('shippingRedMult');
        }
        else{
          shippingGround += $F(elem) * products[id].get('shippingGroundMult');
          shippingBlue   += $F(elem) * products[id].get('shippingBlueMult');
          shippingRed    += $F(elem) * products[id].get('shippingRedMult');
        }
      }
    });
        
    // update shipping
    form.shipping.options[1].text = 'UPS Ground 3-5 Day ('+number_format(shippingGround, 2)+')';
    form.shipping.options[2].text = 'UPS Blue 2 Day Air ('+number_format(shippingBlue, 2)+')';
    form.shipping.options[3].text = 'UPS Red Next Day Air ('+number_format(shippingRed, 2)+')';

    // update total
    $('subtotal').innerHTML = number_format(total, 2);
    calculateTax(form.salestax.value);
  }

  function verify(form) {
    // try {

      var url = 'paypalredir.php?';

      var spc = '';
      var j = 0;
      
      var passed = false;
      var skip = true;
       
      // create remainder of get request, but check for control+pc adapter info as well
      for(var i = 0; i < form.elements.length; i++) {
        if (found = form.elements[i].name.match(/add_(\d+)$/)) {
          if (form.elements[i].value > 0) {
            foundId = $F('pid_' + found[1]);
            skip = true;
            passed = true;

            if (typeof(form.elements['op0_'+foundId]) != 'undefined' && form.elements['op0_'+foundId].type != 'hidden') { 
                skip = false;
            }


            if (!skip) { // are we skipping the option part?
              var temp2;
              if (form.elements['op0_'+foundId].value == '') {
                  temp2 = form.elements['op0_'+foundId];
              }
              
              if (temp2) {
                alert('Please select an adapter');
                temp2.focus();
                return false;
              }

              temp2 = false;
              if (form.elements['op1_'+foundId].value == '') { 
                  temp2 = form.elements['op1_'+foundId];
              }
              if (temp2) {
                alert('Please select control make');
                temp2.focus();
                return false;
              }

              temp2 = false;
              if (form.elements['op1b_'+foundId].value == '') { 
                  temp2 = form.elements['op1b_'+foundId];
              }

              if (temp2) {
                alert('Please enter control model');
                temp2.focus();
                return false;
              }
            }

            j++;
            url += spc+"quantity_"+j+'='+form.elements[i].value; // add
            spc = '&';

            eval('url += spc+"item_name_'+j+'="+products['+foundId+'].get("name");'); // item_name
            
            if (!skip) { // if we're dealing with a non cable product, skip the option stuff
              eval('url += "; PC Adapter:"+ form.op0_'+foundId+'.value+"; Control: "+controls[form.op1_'+foundId+'.value]["make"]+" "+form.op1b_'+foundId+'.get("value");'); // item_name
            }

            eval('url += spc+"item_number_'+j+'="+products['+foundId+'].get("item_no");'); // item_number
            
            /* if we try to do this when shipping.value == 0, then its going to cause an error */
            if (form.shipping.value != 0) {
              if (highestId == foundId) {
                eval('url += spc+"shipping_'+j+'="+products['+foundId+'].get("shipping'+form.shipping.value+'");'); // shipping
              }
              else{
                eval('url += spc+"shipping_'+j+'="+products['+foundId+'].get("shipping'+form.shipping.value+'Mult");'); // shipping
              }

              eval('url += spc+"shipping2_'+j+'="+products['+foundId+'].get("shipping'+form.shipping.value+'Mult");'); // shipping
            }

            eval('url += spc+"amount_'+j+'="+products['+foundId+'].get("amount");'); // amount
            eval('url += spc+"tax_'+j+'="+number_format($("cart_form").salestax.value / 100 * products['+foundId+'].get("amount"), 2);'); // amount
          }
        }
      }
      
      url += spc+"total="+$('total').innerHTML;
      
      // add state
      url += spc+"state="+$("cart_form").state.value;

      // add phone, company. name
      url += spc+"phone="+$("cart_form").phone.value;
      url += spc+"company="+$("cart_form").company.value;
      url += spc+"name="+$("cart_form").name.value;
      url += spc+"shipping="+$("cart_form").shipping.value;
      url += spc+"notes="+$("cart_form").notes.value;

      if (passed) {
        // check for state information
        if (form.state.value == '') {
          alert('Please select a state');
          form.state.focus();
          return false;
        }

        // check for sales tax information
        if (form.state.value == 'CA' && form.salestax.value == 0) {
          alert('Please select a county');
          form.salestax.focus();
          return false;
        }

        if (form.state.value == 'AK' || form.state.value == 'HI') {
          alert('Sorry, but we do not ship to Alaska or Hawaii');
          return false;
        }

        // check for shipping selection
        if (form.shipping.value == 0) {
          alert('Please select a shipping method');
          form.shipping.focus();
          return false;
        }

        // check for name
        if (form.name.value == 0) {
          alert('Please enter a contact name');
          form.name.focus();
          return false;
        }

        // check for Company Name
        if (form.company.value == 0) {
          alert('Please enter a company');
          form.company.focus();
          return false;
        }

        // check for phone number
        if (form.phone.value == 0) {
          alert('Please enter a phone number');
          form.phone.focus();
          return false;
        }
                  
        if (!$('agree').checked) {
          alert('You must accept the terms and conditions before continuing');
          $('agree').focus();
          return false;
        }
        
        location.href = url;
      }
      else{
        alert('There are no items in your cart');
      }
      return false;
    // }
    // catch(ex) {
    //     alert(ex);
    // }
  }
  function showSerialImage() { 
    $('serialImage').style.display = "block";
  }

  function hideSerialImage() { 
    $('serialImage').style.display = "none";
  }