var outOfStockMessage = "Out of Stock";
var inStock = "In stock ([AMOUNT])"//
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 inventoryStatus = parseInt(xmlhttp.responseXML.getElementsByTagName("result").item(0).getAttribute("status"));
  			
  			var quantity;
  			if(useQunatityOnHand == true){
  				quantity = quantityonhand
  			}else{
  				quantity = Math.max(quantityonhand, outofstocklimit)
  			}
  			var result = "";
 			if(quantity == -1 || inventoryStatus=="off"){
 				obj.parentNode.style.display = "none"
 			}else if(quantity == 0){
 				obj.parentNode.style.display = "block"
 				result = outOfStockMessage
 			}else{
 				obj.parentNode.style.display = "block"
 				result = inStock.replace("[AMOUNT]", quantity)
 			}
 			obj.innerHTML = result
		}
	}
	xmlhttp.send(null)
}

function processSubmit(){
	if(!document.getElementById("subproducts")){
		return true;
	}
	var inputs = document.getElementById("subproducts").getElementsByTagName("input")
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].name != "itemnum"){
			var intValue = parseInt(inputs[i].value)
			if(intValue && intValue > 0){}else{
				var inputsToDel = inputs[i].parentNode.getElementsByTagName("input")
				for(var j = 0; j< inputsToDel.length; j++){
					if(inputsToDel[j].name == "itemnum"){
						inputs[i].parentNode.removeChild(inputsToDel[j])
					}
				}
				
			}
		}
	}	
	return true
}
