document.maximumValue = 999;

function initProductQuantity(id, minusButtonSrc, plusButtonSrc) {
	var oForm = document.getElementById("fAdd_" + id);
	if(oForm) {
		oForm.className += " js";
	}
	
	var o = document.getElementById("qty_" + id);
	if(o && o.type != "hidden") {
		attachEventHandler(o, "blur", _quantityBlur);

		o.parentNode.setAttribute("productId", id);
		o.setAttribute("originalValue", o.value);

		var oType = document.getElementById("p-t_" + id);
		if(oType) {
			o.parentNode.setAttribute("productType", oType.value);
		}

		var oMinOrder = document.getElementById("p-mo_" + id);
		if(oMinOrder) {
			o.parentNode.setAttribute("productMinimumOrder", oMinOrder.value);
		}
		
		var oInc = document.getElementById("p-i_" + id);
		if(oInc) {
			o.parentNode.setAttribute("productIncrement", oInc.value);
		}

		var oElement = document.createElement("img");
		if(oElement) {
			oElement.src = minusButtonSrc;
			oElement.alt = "-";
			oElement.className = "m";
			oElement.onclick = _decreaseValue;
			
			if(o.attachEvent) {
				oElement.ondblclick = oElement.onclick;
			}
			
			var oLabel = o.parentNode.getElementsByTagName("label");
			if(oLabel && oLabel[0] && oLabel[0].nextSibling) {
				o.parentNode.insertBefore(oElement, oLabel[0].nextSibling);
			}
		}

		oElement = document.createElement("img");
		if(oElement) {
			oElement.src = plusButtonSrc;
			oElement.alt = "+";
			oElement.className = "p";
			oElement.onclick = _increaseValue;
			
			if(o.attachEvent) {
				oElement.ondblclick = oElement.onclick;
			}
			
			o.parentNode.appendChild(oElement);
		}
	}
}

function _decreaseValue(e) {
	_changeValue(-this.parentNode.getAttribute("productIncrement"), getEventSource(getEvent(e), this));
}

function _increaseValue(e) {
	_changeValue(this.parentNode.getAttribute("productIncrement"), getEventSource(getEvent(e), this));
}

function _changeValue(increment, product) {
	var id = product.parentNode.getAttribute("productId");

	var o = document.getElementById("qty_" + id);
	if(o) {
		var productMinimumOrder = product.parentNode.getAttribute("productMinimumOrder");
		var productIncrement = product.parentNode.getAttribute("productIncrement");
		
		if(increment != 0) {
			value = parseInt((parseFloat(o.value) + parseFloat(increment)) / increment) * increment;
		} else {
			value = parseInt(parseFloat(o.value) / productIncrement) * productIncrement;
		}
		
		if(product.parentNode.getAttribute("productType") == "1" || product.parentNode.getAttribute("productType") == "5") {	// Fixed price or double value
			if(isNaN(value) || value < productMinimumOrder ) {
				value = parseInt(productMinimumOrder );
			} else if(value > document.maximumValue) {
				value = parseInt(document.maximumValue);
			}
			o.value = parseInt(value);
		} else {
			if(isNaN(value) || value < productMinimumOrder ) {
				value = formatNumber(productMinimumOrder , 2);
			} else if(value > document.maximumValue) {
				value = formatNumber(parseInt(document.maximumValue / productIncrement) * productIncrement, 2);
			}
			o.value = formatNumber(new Number(value), 2);
		}
		_checkValue(id, o);
	}
}

function _checkValue(id, o) {
	var oItemLine = document.getElementById("item_" + id);
	if(oItemLine) {
		if(!oItemLine.getAttribute("originalClassName")) {
			oItemLine.setAttribute("originalClassName", oItemLine.className);
		}

		var oRecalcButton = document.getElementById("rec_" + id);

		if(o.getAttribute("originalValue") != o.value) {
			oItemLine.className = oItemLine.getAttribute("originalClassName") + " changed";
			if(oRecalcButton) {
				oRecalcButton.src = "/i/b/btnRecalculateChanged.gif";
			}
		} else {
			oItemLine.className = oItemLine.getAttribute("originalClassName");
			if(oRecalcButton) {
				oRecalcButton.src = "/i/b/btnRecalculate.gif";
			}
		}
	}
}

function _quantityBlur(e) {
	if(!document.isValidating) {
		_changeValue(0, getEventSource(getEvent(e), this));
		return;
		var id = getEventSource(getEvent(e), this).parentNode.getAttribute("productId");
		var o = document.getElementById("qty_" + id);
		if(o) {
			_checkValue(id, o);
		}
	}
}
