var csLastProductId;
var csLastInStock;
var csLastETA;
var csWishListId;

function addToCartCheckStock(where) {
  if (where == 'product') {
    var productId = document.orderform.productid.value;
    if (document.getElementById) {
      var button = document.getElementById("addToCartButton");
      button.src = "/buttons/CHECKING_STOCK.gif";
    }
    checkStock('addToCart',productId);
  } else if (where == "products") {
    var productId = addToCartCheckStock.arguments[1];
    if (document.getElementById) {
      var button = document.getElementById("addToCartButton"+productId);
      button.src = "/buttons/small/CHECKING_STOCK.gif";
    }
    checkStock('buyNow',productId);
  }
}

function wishlistToCartCheckStock(productId,wishListId) {
  if (document.getElementById) {
    var button = document.getElementById("addToCartButton"+wishListId);
    button.src = "/buttons/CHECKING_STOCK.gif";
  }
  csWishListId = wishListId;
  checkStock('wishList',productId);
}

function checkStock(via,productId) {
  if (productId == csLastProductId) {
    checkStockResponse(via,csLastProductId,csLastInStock,csLastETA);
  } else {
    checkStockIFrame.document.location.replace('/check_stock_status.php?productid='+productId+'&via='+via);
  }
}

function checkStockResponse(via,productId,inStock,eta) {
  checkStockIFrame.document.location.replace("blank.html");
  if (via == "manual") {
    var html = "<div style='border: 1px solid #cccccc;padding: 4px;background-color: #ffffcc;'><span style='font-weight: bold;'>";
    if (inStock == 1) {
      html += "In Stock";
    } else if (inStock == -1) {
      html += "Discontinued";
    } else {
      html += "Out of Stock";
      if (eta) {
        html += ": "+eta;
      }
      html += ". <a href=# onClick=\"document.orderform.mode.value='add2bo';document.orderform.submit();return false;\">Add to your backorder list</a>";
    }
    html += "</span></div>";
    document.getElementById('stockStatus').innerHTML = html;  
  } else if (via == "addToCart" || via == "wishList" || via == "buyNow") {
    if (inStock == 1) {
      if (via == "addToCart") {
        document.orderform.submit();
      } else if (via == "wishList") {
        window.location="cart.php?mode=wl2cart&wlitem="+csWishListId;
      } else if (via == "buyNow") {
        window.location="cart.php?add&amount=1&mode=add&productid="+productId;
      }
    } else {
      if (document.getElementById) {
        var button;
        if (via == "addToCart") {
          button = document.getElementById("addToCartButton");
        } else if (via == "wishList") {
          button = document.getElementById("addToCartButton"+csWishListId);
        } else if (via == "buyNow") {
          button = document.getElementById("addToCartButton"+productId);
        }
        button.src = "/graphics/spacer.gif";
        button.src = "/buttons/"+(via=="buyNow"?"small/":"")+"buy_now_check_stock.gif";
      }
      if (inStock == -1) {
        alert("This item has been discontinued. It will be removed from our store shortly."); 
      } else {
        if (confirm("This item is out of stock. "+(eta?eta+". ":"")+" Would you like to add it to your backorder list?")) {
          if (via == "addToCart") {
            document.orderform.mode.value='add2bo';
            document.orderform.submit();
          } else if (via == "wishList") {
            window.location="cart.php?mode=wl2bo&wlitem="+csWishListId;
          } else if (via == "buyNow") {
            window.location="cart.php?add&amount=1&mode=add2bo&productid="+productId;
          }
        }
      }
    }
  }
  csLastProductId = productId;
  csLastInStock = inStock;
  csLastETA = eta;
}

function checkStockWait(via,productId,sec) {
  setTimeout("checkStock('"+via+"','"+productId+"')",(sec*1+1)*1000);
  if (via == "manual") {
    response = "<b>Checking Stock</b> <img src=graphics/please_wait.gif>";
    document.getElementById('stockStatus').innerHTML;
  }
}

function checkStockError(via,productId,error) {
  alert(error);
  if (document.getElementById) {
    var button;
    if (via == "addToCart") {
      button = document.getElementById("addToCartButton");
    } else if (via == "wishList") {
      button = document.getElementById("addToCartButton"+csWishListId);
    } else if (via == "buyNow") {
      button = document.getElementById("addToCartButton"+productId);
    }
    if (via != "manual") {
      button.src = "/graphics/spacer.gif";
      button.src = "/buttons/ADD_TO_CART.gif";
    }
  }
}