// shopping javacript
function confirmBuy(itemForm)
{
	var prMessage = requiredProperty;
	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 {
						if (radioControl.checked) {
							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;
				}
			}
		}
	}

	if (confirmAdd == "1") {
		return confirm(addProduct);
	} else {
		return true;
	}
}

function changeProperty(itemForm)
{
	var priceControl = "";
	var htmlControl = false;
	var prIDs = "";
	var itemId = "";
	var priceEdit = 0;
	var pricesType = 0;
	var taxPercent = 0;
	var taxPrices = 1;
	var pointsBase = 0;
	var pointsRate = 1;
	var pointsDecimals = 0;

	if (itemForm.pe) {
		priceEdit = itemForm.pe.value;
		if (priceEdit == "1") {
			return;
		}
	}
	if (itemForm.tax_prices_type && itemForm.tax_prices_type.value != "") {
		pricesType = parseFloat(itemForm.tax_prices_type.value);
		if (isNaN(pricesType)) { pricesType = 0; }
	}
	if (itemForm.tax_percent && itemForm.tax_percent.value != "") {
		taxPercent = parseFloat(itemForm.tax_percent.value);
		if (isNaN(taxPercent)) { taxPercent = 0; }
	}
	if (itemForm.tax_prices && itemForm.tax_prices.value != "") {
		taxPrices = parseFloat(itemForm.tax_prices.value);
		if (isNaN(taxPrices)) { taxPrices = 1; }
	}
	if (itemForm.base_points_price && itemForm.base_points_price.value != "") {
		pointsBase = parseFloat(itemForm.base_points_price.value);
		if (isNaN(pointsBase)) { pointsBase = 0; }
	}
	if (itemForm.points_rate && itemForm.points_rate.value != "") {
		pointsRate = parseFloat(itemForm.points_rate.value);
		if (isNaN(pointsRate)) { pointsRate = 1; }
	}
	if (itemForm.points_decimals && itemForm.points_decimals.value != "") {
		pointsDecimals = parseFloat(itemForm.points_decimals.value);
		if (isNaN(pointsDecimals)) { pointsDecimals = 0; }
	}

	if (itemForm.add_id) {
		itemId = itemForm.add_id.value;
	} else if (itemForm.item_id) {
		itemId = itemForm.item_id.value;
	}
	if (itemId != "" && document.getElementById) {
		priceControl = document.getElementById("sales_price_" + itemId);
		if (!priceControl) {
			priceControl = document.getElementById("price_" + itemId);
		}
	} 
	var pointsPriceControl = document.getElementById("points_price_" + itemId);

	var totalAdditionalPrice = 0; var totalAdditionalPriceRate = 0;
	if(itemForm.properties) { prIDs = itemForm.properties.value; }
	if(prIDs != "") {

		var properties = prIDs.split(",");
		for ( var i = 0; i < properties.length; i++) {
			var prID = properties[i];
			var prValue = "";
			var additionalPrice = 0; var additionalPriceRate = 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 {
					if (radioControl.checked) {
						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);
								var checkedPriceRate = parseFloat(itemForm.elements["option_price_rate_" + checkedValue].value);
								if(!isNaN(checkedPrice) && checkedPrice!= 0) {
									additionalPrice += parseFloat(checkedPrice);
									additionalPriceRate += parseFloat(checkedPriceRate);
								}
							} 
						}
					}
				} 
			}
			if (prValue != "") {
				if(itemForm.elements["option_price_" + prValue]) {
					var optionPrice = itemForm.elements["option_price_" + prValue].value;
					var optionPriceRate = itemForm.elements["option_price_rate_" + prValue].value;
					if(optionPrice != "") {
						additionalPrice = parseFloat(optionPrice);
						additionalPriceRate = parseFloat(optionPriceRate);
					}
				}
			} 

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

	if(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 compPrice = 0; var compTax = 0;
		if (itemForm.comp_price && itemForm.comp_price.value != "") { 
			compPrice = parseFloat(itemForm.comp_price.value); 
		}
		if (itemForm.comp_tax && itemForm.comp_tax.value != "") { 
			compTax = parseFloat(itemForm.comp_tax.value); 
		}

		// check product quantity
		var quantity = 1;
		if (itemForm.quantity) {
			if (itemForm.quantity.selectedIndex) {
				quantity = parseInt(itemForm.quantity.options[itemForm.quantity.selectedIndex].value);
			} else {
				quantity = parseInt(itemForm.quantity.value);
			}
			if (isNaN(quantity)) { quantity = 1; } 
		}
		if(itemForm.quantity_price) { 
			var prices = itemForm.quantity_price.value; 
			if (prices != "") {
				prices = prices.split(",");
				for (var p = 0; p < prices.length; p = p + 4) {
					var minQuantity = parseInt(prices[p]);
					var maxQuantity = parseInt(prices[p + 1]);
					if (quantity >= minQuantity && quantity <= maxQuantity) {
						basePrice = parseFloat(prices[p + 2]);
						var propertiesDiscount = parseFloat(prices[p + 3]);
						if (propertiesDiscount > 0) {
							totalAdditionalPrice -= (Math.round(totalAdditionalPrice * propertiesDiscount) / 100);
							totalAdditionalPriceRate -= (Math.round(totalAdditionalPriceRate * propertiesDiscount) / 100);
						}
						break;
					}
				}
			}
		}
		
		var price = basePrice + totalAdditionalPriceRate;
		var taxAmount = 0; var productPrice = 0; var taxPrice = 0; var priceExcl = 0;
		if (pricesType == 1) {
			// price already includes tax
			taxPrice = Math.round((price + compPrice) * 100) / 100; 
			taxAmount = (Math.round(price * 100) - Math.round(price * 10000 / ( 100 + taxPercent))) / 100; 
			productPrice = Math.round((price - taxAmount + compPrice - compTax) * 100) / 100;
			priceExcl = productPrice;
		} else {
			taxAmount = Math.round(price * taxPercent) / 100; 
			productPrice = Math.round((price + compPrice) * 100) / 100;
			taxPrice = Math.round((productPrice + taxAmount + compTax) * 100) / 100; 
			priceExcl = productPrice;
		}

		if (taxPrices == 2) {
			productPrice = taxPrice;
			taxPrice = priceExcl;
		} else if (taxPrices == 3) {
			productPrice = taxPrice;
		}

		if (priceControl) {
			priceControl.innerHTML = cl + formatNumber(productPrice, 2) + cr;
		}
		taxPriceControl = document.getElementById("tax_price_" + itemId);
		if (taxPriceControl) {
			taxPriceControl.innerHTML = cl + formatNumber(taxPrice, 2) + cr;
		}
		if (pointsPriceControl) {
			var pointsPrice = pointsBase + (totalAdditionalPrice * pointsRate);
			pointsPriceControl.innerHTML = formatNumber(pointsPrice, pointsDecimals);
		}

	}
}

function changeQuantity(itemForm)
{
	changeProperty(itemForm);
}

function properyImageUpload(uploadUrl)
{
	var uploadWin = window.open (uploadUrl, 'uploadWin', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=400,height=300');
	uploadWin.focus();
}

function openPreviewWin(previewUrl, width, height)
{
	var previewWin = window.open (previewUrl, 'previewWin', 'left=0,top=0,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height);
	previewWin.focus();
	return false;
}

function openSuperImage(imageUrl, width, height)
{
	var scrollbars = "no";
	// add margins to image size
	if (width > 0 && height > 0) {
		width += 30; height += 30;
	}
	// check available sizes
	var availableHeight = window.screen.availHeight - 60;
	var availableWidth = window.screen.availWidth - 20;
	if (isNaN(availableHeight)) { availableHeight = 520; } 
	if (isNaN(availableWidth)) { availableWidth = 760; } 
	if (height > availableHeight || height == 0) { 
		height = availableHeight;
		scrollbars = "yes"; 
	}
	if (width > availableWidth || width == 0) {
		width = availableWidth;
		scrollbars = "yes";
	}
	var superImageWin = window.open (imageUrl, 'superImageWin', 'left=0,top=0,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=' + scrollbars + ',resizable=yes,width=' + width + ',height=' + height);
	superImageWin.focus();
	return false;
}

function setFilePath(filepath, filetype, controlName, formId)
{
	if(filepath != "" && controlName != "" && formId != "")
	{
		var formName = "form_" + formId;
		document.forms[formName].elements[controlName].value = filepath;
		document.forms[formName].elements[controlName].focus();
	}
}


function formatNumber(numberValue, decimals, decimalPoint, thousandsSeparator)
{
	if (decimals == undefined) {
		decimals = 0;
	}
	if (thousandsSeparator == undefined) {
		thousandsSeparator = ",";
	}

	var numberParts = "";
	var roundValue = 1;
	for (var d = 0; d < decimals; d++) {
		roundValue *= 10;
	}
	numberValue = Math.round(numberValue * roundValue) / roundValue;
	var numberSign = "";
	if (numberValue < 0) {
		numberSign = "-";
		numberValue = Math.abs(numberValue);
	} 

	var numberText = new String(numberValue);
	var numberParts = numberText.split(".");
	var beforeDecimal = numberParts[0];
	var afterDecimal = "";
	numberText = "";
	if (numberParts.length == 2) {
		afterDecimal = numberParts[1];
	}
	while (beforeDecimal.length > 0) {
		if (beforeDecimal.length > 3) {
			numberText = thousandsSeparator + beforeDecimal.substring(beforeDecimal.length - 3, beforeDecimal.length) + numberText;
			beforeDecimal = beforeDecimal.substring(0, beforeDecimal.length - 3);
		} else {
			numberText = beforeDecimal + numberText;
			beforeDecimal = "";
		}
	}
	if (decimals > 0) {
		while (afterDecimal.length < decimals) {
			afterDecimal += "0";
		}
		if (decimalPoint == undefined) {
			decimalPoint = ".";
		}
		numberText += decimalPoint + afterDecimal;
	}
	numberText = numberSign + numberText;

	return numberText;
}