function confirmBuy(itemForm)
{
	var prMessage = "Please specify {property_name} for {product_name}.";
	var prIDs = "";
	if(itemForm.properties) {
		prIDs = itemForm.properties.value;
	}
	if (prIDs != "") {
		var properties = prIDs.split(",");
		var productName = itemForm.elements["item_name"].value;
		for ( var i = 0; i < properties.length; i++) {
			var prID = properties[i];
			var prRequired = itemForm.elements["property_required_" + prID].value;
			if(prRequired == 1) {
				var prValue = "";
				var prControl = itemForm.elements["property_control_" + prID].value;
				if (prControl == "LISTBOX") {
					prValue = itemForm.elements["property_" + prID].options[itemForm.elements["property_" + prID].selectedIndex].value;
				} else if (prControl == "RADIOBUTTON") {
					var radioControl = itemForm.elements["property_" + prID];
					if (radioControl.length) {
						for ( var ri = 0; ri < radioControl.length; ri++) {
							if (radioControl[ri].checked) {
								prValue = radioControl[ri].value;
								break;
							}
						}
					} else {
						prValue = radioControl.value;
					}
				} else if (prControl == "CHECKBOXLIST") {
					if (itemForm.elements["property_total_" + prID]) {
						var totalOptions = parseInt(itemForm.elements["property_total_" + prID].value);
						for ( var ci = 1; ci <= totalOptions; ci++) {
							if (itemForm.elements["property_" + prID + "_" + ci].checked) {
								prValue = 1;
								break;
							}
						}
					} 
				} else {
					prValue = itemForm.elements["property_" + prID].value;
				}
				if(prValue == "") {
					var propertyName = itemForm.elements["property_name_" + prID].value;
					prMessage = prMessage.replace("\{property_name\}", propertyName);
					prMessage = prMessage.replace("\{product_name\}", productName);
					alert(prMessage);	
					if (prControl != "RADIOBUTTON" && prControl != "CHECKBOXLIST") {
						itemForm.elements["property_" + prID].focus();
					}
					return false;
				}
			}
		}
	}

	return confirm('Add this product to your Shopping Cart?');
}

function changeProperty(itemForm)
{
	var priceControl = "";
	var prIDs = "";
	if(itemForm.sales_price) {
		priceControl = itemForm.sales_price;
	} else if (itemForm.price) {
		priceControl = itemForm.price;
	}
	if(itemForm.properties) { prIDs = itemForm.properties.value; }
	if(priceControl && prIDs != "" && itemForm.base_price) {
		var cl = "";
		var cr = "";
		var basePrice = parseFloat(itemForm.base_price.value);
		if(itemForm.cl) { cl = itemForm.cl.value; }
		if(itemForm.cr) { cr = itemForm.cr.value; }

		var totalAdditionalPrice = 0;
		var properties = prIDs.split(",");
		for ( var i = 0; i < properties.length; i++) {
			var prID = properties[i];
			var prValue = "";
			var additionalPrice = 0;
			var prControl = itemForm.elements["property_control_" + prID].value;
			if (prControl == "LISTBOX") {
				prValue = itemForm.elements["property_" + prID].options[itemForm.elements["property_" + prID].selectedIndex].value;
			} else if (prControl == "RADIOBUTTON") {
				var radioControl = itemForm.elements["property_" + prID];
				if (radioControl.length) {
					for ( var ri = 0; ri < radioControl.length; ri++) {
						if (radioControl[ri].checked) {
							prValue = radioControl[ri].value;
							break;
						}
					}
				} else {
					prValue = radioControl.value;
				}
			} else if (prControl == "CHECKBOXLIST") {
				if (itemForm.elements["property_total_" + prID]) {
					var totalOptions = parseInt(itemForm.elements["property_total_" + prID].value);
					for ( var ci = 1; ci <= totalOptions; ci++) {
						if (itemForm.elements["property_" + prID + "_" + ci].checked) {
							var checkedValue = itemForm.elements["property_" + prID + "_" + ci].value;
							if (itemForm.elements["option_price_" + checkedValue]) {
								var checkedPrice = parseFloat(itemForm.elements["option_price_" + checkedValue].value);
								if(!isNaN(checkedPrice) && checkedPrice!= 0) {
									additionalPrice += parseFloat(checkedPrice);
								}
							} 
						}
					}
				} 
			}
			if (prValue != "") {
				if(itemForm.elements["option_price_" + prValue]) {
					var optionPrice = itemForm.elements["option_price_" + prValue].value;
					if(optionPrice != "") {
						additionalPrice = parseFloat(optionPrice);
					}
				}
			} 

			if(!isNaN(additionalPrice) && additionalPrice != 0) {
				totalAdditionalPrice += additionalPrice;
			}
		}

		var price = basePrice + totalAdditionalPrice;
		priceControl.value = cl + formatNumber(price) + cr;
	}
}

function formatNumber(numberValue)
{
	var numberParts = "";
	var numberText = new String(numberValue);
	if(numberText.indexOf(".") == -1) {
		numberText += ".00";
	} else if (numberText.indexOf(".") == (numberText.length - 2)) {
		numberText += "0";
	} else {
		var numberParts = numberText.split(".");
		if(numberParts[1].length > 2) {
			numberText = numberParts[0] + "." + numberParts[1].substring(0, 2);
		}
	}
	var numberLength = numberText.length;
	if (numberLength >= 10) {
		numberText = numberText.substring(0, numberLength-9)+","+numberText.substring(numberLength-9, numberLength-6)+","+numberText.substring(numberLength-6, numberLength);
	} else if (numberLength >= 7) {
		numberText = numberText.substring(0, numberLength-6)+","+numberText.substring(numberLength-6, numberLength);
	}
	return numberText;
}
// compare javacript
function compareItems()
{
	var checkedNumber = 0;
	var checkedItems = "";
	var formsNames = document.forms_names.form_name;
	for (var i = 0; i < formsNames.length; i++) {
		var formName = formsNames[i].value;
		if(document.forms[formName].compare.checked) {
				checkedNumber++;
				if(checkedNumber > 1) { checkedItems += ","; }
				checkedItems += document.forms[formName].compare.value;
		}
	}
	if (checkedNumber < 2) {
		alert("You must select at least 2 products");
	} else if (checkedNumber > 5) {
		alert("You must select no more than 5 products");
	} else {
		document.compare_form.items.value = checkedItems;
		document.compare_form.submit();
	}

	return false;
}

function compareRecentItems(formName)
{
	var checkedNumber = 0;
	var checkedItems = "";
	var recentForm = document.forms[formName];
	var compareItems = recentForm.compare;
	for (var i = 0; i < compareItems.length; i++) {
		if(recentForm.compare[i].checked) {
				checkedNumber++;
				if(checkedNumber > 1) { checkedItems += ","; }
				checkedItems += recentForm.compare[i].value;
		}
	}
	if (checkedNumber < 2) {
		alert("You must select at least 2 products");
	} else if (checkedNumber > 5) {
		alert("You must select no more than 5 products");
	} else {
		recentForm.items.value = checkedItems;
		recentForm.submit();
	}

	return false;
}

