/* JavaScript used for background Ajax pricing calls */

  var req;

  function darkenPrices(productList) {
    catnums = productList.split(" ")
    for(var i=0; i < catnums.length; i++) {
      document.getElementById("price_" + catnums[i]).style.color = "#000000";
    }
  } 

  function updateStockImage(productId, transportDays, available) {
    imageElement = document.getElementById("stockImg_" + productId);
    if(imageElement == null) {
        return;
    }
    var avail = available.indexOf("true") > 0;
    if(!avail || transportDays > 6) {
      // return the out-of-stock-image
      imageElement.src = "/images/ship_unavail.gif";
      imageElement.title = unavailableToShip;
    }
    if(avail && transportDays <= 2) {
      // return the in-stock image
      imageElement.src = "/images/ship_48hrs.gif";
      imageElement.title = shipsIn48Hours;
    }
    if(avail && transportDays > 2 && transportDays <= 6) {
      // display the yellow image
      imageElement.src = "/images/ship_6days.gif";
      imageElement.title = shipsIn6Days;
    }
    if(!avail && document.getElementById("replacement_" + productId) != null) {
        // display the replacement product row if it's present and the product is out of stock
        document.getElementById("replacement_" + productId).style.visibility = "visible";
    }
  }

  function getPrice(productIds) {
    return getPrice(productIds, false, false);
  }

  function getPrice(productIds, isPromotion) {
    return getPrice(productIds, isPromotion, false);
  }

  function getPrice(productIds, isPromotion, isSearch) {
    if(productIds.length == 0) {
      return;
    }
    var url = "/app/ContractPricing?id=" + productIds
    if(isPromotion) {
        url = url + "&cp=true";
    }
    if(isSearch) {
        url = url + "&sp=true";
    }
    if (typeof XMLHttpRequest != "undefined") {
      req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    req.open("GET", url, true);
    req.onreadystatechange = callback;
    req.send(null);
  }

  function callback() {
    if (req.readyState == 4) {
      if (req.status == 200) {
        var price = "";
        var listprice = "";
        var breakprice = "";
        var avail = "";
        var availQty = "";
        var productId = "";
        var transportDays = "";
        var description = "";
        var uom = "";
        xmldoc = req.responseXML;
        products = xmldoc.getElementsByTagName("product");
        for(var x=0; x < products.length; x++) {
            var productNode = products[x];
            productId = productNode.getAttribute("id");
            description = productNode.getAttribute("description");
            uom = productNode.getAttribute("uom");
            if(productNode.getElementsByTagName("price") != null) {
                node = productNode.getElementsByTagName("price").item(0);
                if(node.hasChildNodes() && node.firstChild.length > 0) {
                    price = node.firstChild.data;
                }
            }
            if(productNode.getElementsByTagName("list_price") != null) {
                node = productNode.getElementsByTagName("list_price").item(0);
                if(node.hasChildNodes() && node.firstChild.length > 0) {
                    listprice = node.firstChild.data;
                }
            }
            if(productNode.getElementsByTagName("break_price") != null) {
                node = productNode.getElementsByTagName("break_price").item(0);
                if(node.hasChildNodes() && node.firstChild.length > 0) {
                    breakprice = node.firstChild.data;
                }
            }
            if(productNode.getElementsByTagName("available") != null) {
                node = productNode.getElementsByTagName("available").item(0);
                if(node.hasChildNodes() && node.firstChild.length > 0) {
                    avail = node.firstChild.data;
                }
            }
            if(productNode.getElementsByTagName("avail_qty") != null) {
                node = productNode.getElementsByTagName("avail_qty").item(0);
                if(node.hasChildNodes() && node.firstChild.length > 0) {
                    availQty = node.firstChild.data;
                }
            }
            if(productNode.getElementsByTagName("transport_days") != null) {
                node = productNode.getElementsByTagName("transport_days").item(0);
                if(node.hasChildNodes() && node.firstChild.length > 0) {
                    transportDays = node.firstChild.data;
                }
            }
            if(price.length >= 0 && document.getElementById("price_" + productId) != null) {
                document.getElementById("price_" + productId).innerHTML = price;
                document.getElementById("price_" + productId).style.color = "#000000";
            }
            if(document.getElementById("avail_" + productId) != null) {
                updateStockImage(productId, transportDays, avail);
                document.getElementById("avail_" + productId).style.color = "#000000";
            }
            if(document.getElementById("availqty_" + productId) != null) {
                document.getElementById("availqty_" + productId).innerHTML = availQty;
                document.getElementById("availqty_" + productId).style.color = "#000000";
            }
            if(document.getElementById("listprice_" + productId) != null) {
                document.getElementById("listprice_" + productId).innerHTML = listprice;
                document.getElementById("listprice_" + productId).style.color = "#000000";
            }
            if(document.getElementById("breakprice_" + productId) != null) {
                document.getElementById("breakprice_" + productId).innerHTML = breakprice;
                document.getElementById("breakprice_" + productId).style.color = "#000000";
            }
            if(document.getElementById("description_" + productId) != null) {
                document.getElementById("description_" + productId).innerHTML = description;
                document.getElementById("description_" + productId).style.color = "#000000";
            }
            if(document.getElementById("uom_" + productId) != null) {
                document.getElementById("uom_" + productId).innerHTML = uom;
                document.getElementById("uom_" + productId).style.color = "#000000";
            }
        }
      }
    }
  }

