var csLastProductId;
var csLastInStock;
var csLastETA;
var csLastNoBackorders;
var csWishListId;
var csBackorderId;

function addToCartCheckStock(where) {
  if (where == 'product') {
    var productId = document.orderform.productid.value;
    var button = $("addToCartButton");
    button.src = "/layout/button_checking_stock.gif";
  } else if (where == "products") {
    var productId = addToCartCheckStock.arguments[1];
    var button = $("addToCartButton"+productId);
    button.src = "/layout/checking_stock60x23.gif";
  } else if (where == "wishList") {
    var productId = addToCartCheckStock.arguments[1];
    var wishListId = addToCartCheckStock.arguments[2];
    var button = $("addToCartButton"+wishListId);
    button.src = "/layout/checking_stock60x23.gif";
    csWishListId = wishListId;
  } else if (where == "backorders") {
    var productId = addToCartCheckStock.arguments[1];
    var backorderId = addToCartCheckStock.arguments[2];
    var button = $("addToCartButton"+backorderId);
    button.src = "/layout/checking_stock60x23.gif";
    csBackorderId = backorderId;
  } else if (where == "checkStock") {
    var productId = document.orderform.productid.value;
    var button = $("addToCartButton");
    button.src = "/buttons/checking_stock.gif";
  }
  checkStock(where,productId);
}

function checkStock(via,productId) {
  clearTimeout(checkingStock.timeout);
  if (via == "manual") {
    if (!$('checkStock').oldInnerHTML) {
      $('checkStock').oldInnerHTML = $('checkStock').innerHTML;
    }
  }
  if (productId == csLastProductId) {
    checkStockResponse(via,csLastProductId,csLastInStock,csLastETA,csNoBackorders);
  } else {
    checkingStock(via);
    checkStockIFrame.document.location.replace('/check_stock_status.php?productid='+productId+'&via='+via);
  }
}

function checkingStock(via) {
  if (via == "manual") {
    var base = "Checking Stock";
    var el = $("checkStock");
    if (el.innerHTML.indexOf(base) > -1 && el.innerHTML.replace(base,"").length < 4) {
      el.innerHTML = el.innerHTML + ".";
    } else {
      el.innerHTML = base;
    }
    checkingStock.timeout = setTimeout("checkingStock('"+via+"')",500);
  }
}

function checkStockResponse(via,productId,inStock,eta,noBackorders) {
  clearTimeout(checkingStock.timeout);
  checkStockIFrame.document.location.replace("blank.html");
  if (via == "manual") {
    var html = "";
    if (inStock == 1) {
      html += "In Stock";
    } else if (inStock == -1) {
      html += "Discontinued";
    } else {
      html += "Out of Stock";
      if (eta) {
        html += "<br />"+eta;
      }
      if (noBackorders != "Y") {
        html += "<br /><a href=\"cart.php?amount=1&mode=add2bo&productid="+productId+"\">&#187; Backorder</a>";
      }
    }
    html += "";
    $('checkStock').innerHTML = html;
  } else {
    if (inStock == 1) {
      if (via == "product" || via == "partsMate") {
        document.orderform.submit();
      } else if (via == "products") {
        window.location="cart.php?add&amount=1&mode=add&productid="+productId;
      } else if (via == "wishList") {
        window.location="cart.php?mode=wl2cart&wlitem="+csWishListId;
      } else if (via == "backorders") {
        window.location="cart.php?mode=bo2cart&boitem="+csBackorderId;
      }
    } else {
      var button;
      if (via == "product" || via == "partsMate") {
        button = $("addToCartButton");
      } else if (via == "products") {
        button = $("addToCartButton"+productId);
      } else if (via == "wishList") {
        button = $("addToCartButton"+csWishListId);
      } else if (via == "backorders") {
        button = $("addToCartButton"+csBackorderId);
      }
      button.src = "/graphics/spacer.gif";
      if (via == "product") {
        button.src = "layout/button_add_to_cart.png";
      } else if (via == "partsMate") {
        button.src = "/buttons/ADD_TO_CART.gif";
      } else {
        button.src = "layout/buy_now_check_stock60x23.gif";
      }
      if (inStock == -1) {
        alert("This item has been discontinued. It will be removed from our store shortly."); 
      } else {
        if (via == "backorders") {
          alert("This item is no longer in stock.");
        } else {
          if (noBackorders != "Y") {
            if (confirm("This item is out of stock. "+(eta?eta+". ":"")+" Would you like to add it to your backorder list?")) {
              if (via == "product" || via == "partsMate") {
                document.orderform.mode.value='add2bo';
                document.orderform.submit();
              } else if (via == "products") {
                window.location="cart.php?add&amount=1&mode=add2bo&productid="+productId;
              } else if (via == "wishList") {
                window.location="cart.php?mode=wl2bo&wlitem="+csWishListId;
              }
            }
          } else {
            alert("This item is out of stock. "+(eta?eta+". ":""));
          }
        }
      }
    }
  }
  csLastProductId = productId;
  csLastInStock = inStock;
  csLastETA = eta;
  csNoBackorders = noBackorders;
}

function checkStockWait(via,productId,sec) {
  setTimeout("checkStock('"+via+"','"+productId+"')",(sec*1+1)*1000);
}

function checkStockError(via,productId,error) {
  clearTimeout(checkingStock.timeout);
  alert(error);
  if (via == "manual") {
    $('checkStock').innerHTML = $('checkStock').oldInnerHTML;  
  } else {
    var button;  
    if (via == "product") {
      button = $("addToCartButton");
    } else if (via == "products") {
      button = $("addToCartButton"+productId);
    } else if (via == "wishList") {
      button = $("addToCartButton"+csWishListId);
    } else if (via == "backorders") {
      button = $("addToCartButton"+csBackorderId);
    }
    button.src = "/layout/spacer.gif";
    if (via == "product") {
      button.src = "layout/button_add_to_cart.png";
    } else {
      button.src = "layout/buy_now_check_stock60x23.gif";
    }
  }
}