var outOfStockMessage = "Out of Stock";
var inStock = "In Stock"//
var useQunatityOnHand = true

function createHTTPRequest(){
	var xmlhttp;

	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlhttp = new XMLHttpRequest();
		if (xmlhttp.overrideMimeType) {
			xmlhttp.overrideMimeType('text/xml');
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	return xmlhttp;
}
function SetQuantityText(sku, obj) {
	var xmlhttp = createHTTPRequest();
	xmlhttp.open("GET", "getquantity.php?sku=" + sku + "&rand=" + Math.random(), true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			var quantityonhand = parseInt(xmlhttp.responseXML.getElementsByTagName("result").item(0).getAttribute("quantityonhand"));
			if(isNaN(quantityonhand)){
				quantityonhand = -1;
			}
  			var outofstocklimit = parseInt(xmlhttp.responseXML.getElementsByTagName("result").item(0).getAttribute("outofstocklimit"));
  			if(isNaN(outofstocklimit)){
  				outofstocklimit = -1;
  			}
  			var quantity;
  			if(useQunatityOnHand == true){
  				quantity = quantityonhand
  			}else{
  				quantity = Math.max(quantityonhand, outofstocklimit)
  			}
  			var result = "";
 			if(quantity <= 0){
 				result = outOfStockMessage
 			}else{
 				result = inStock.replace("[AMOUNT]", quantity)
 			}
 			obj.innerHTML = result
		}
	}
	xmlhttp.send(null)
}

