var overview = new product_overview;

function product_overview()
{
	// Declare functions
	this.init = overview_init;
	this.click = overview_click;
	
	// Declare variables
	this.selected_field;
	this.selected_link;
	
	// The script
	function overview_init()
	{
		this.click('details');
	}
	
	function overview_click(name)
	{	
		// -- EXTRA -- go to right image:
		if(name == 'images')
			image_onclick(document.getElementById(selected + "_large"));
		
		// -- NORMAL
		that = document.getElementById("overview_" + name);
		if(this.selected_field)
			document.getElementById("product_"+this.selected_field).style.display = 'none';
		if(this.selected_link)
			this.selected_link.className = '';
		
		document.getElementById("product_"+name).style.display = 'block';
		that.className = 'selected';
		
		// Scroll to top
		if(typeof(window.scroll) == 'function')
			window.scroll(0, 0);
		else
			window.scrollTo(0, 0);
		
		this.selected_field = name;
		this.selected_link = that;
		
		return false;
	}
}

// Image functions
var selected;
var selected_large;
var image_cached = new Array();

function image_onmouseover(that)
{
	if(that)
	{
		if(selected)
			document.getElementById(selected).className = '';
		document.getElementById(that.id).className = 'selected';
		
		if(!image_cached[that.alt])
			document.getElementById("product_image").style.visibility = "hidden";
			
		document.getElementById("product_image").onload = image_onload;
		document.getElementById("product_image").src = that.alt;
		document.getElementById("product_image").alt = that.alt;
		
		selected = that.id;
	}
}
function image_onclick(that)
{
	if(selected_large)
		document.getElementById(selected_large).className = '';
	document.getElementById(that.id).className = 'selected';
	
	if(!image_cached[that.alt])
		document.getElementById("product_image_large").style.visibility = "hidden";
	
	document.getElementById("product_image_large").onload = image_onload;
	document.getElementById("product_image_large").src = that.alt;
	document.getElementById("product_image_large").alt = that.alt;
	
	selected_large = that.id;
	
	return false;
}
function image_onload()
{
	this.style.visibility = "visible";
	image_cached[this.alt] = 1;
}

// Order page functions:
function quantityChanged()
{
	// Enable and disable all options which are not allowed with this amount of quantity
	var objQuantity = document.getElementById("quantity");
	var intQuantity = Math.round(objQuantity.value * 1);
	if (!(intQuantity > 0))
	{
		// Update pricefields
		calculatePrice();
		return;
	}
	var objForm = document.getElementById("orderform");
	// Inputs
	for(var intTemp=0; intTemp < objForm.getElementsByTagName("input").length; intTemp++)
	{
		var objTemp = objForm.getElementsByTagName("input")[intTemp];
		if (objTemp.accessKey)
		{
			// Enable inputfield
			objTemp.disabled = false;
			// Make array of prices
			var arrProductAccessKey = objTemp.accessKey.split("|");
			var intTempLowestQuantity = 9999999;
			for (var intKey in arrProductAccessKey)
			{
				// Determine lowest quantity for this option
				var intTempQuantity = arrProductAccessKey[intKey].split(":")[0] * 1;
				if (intTempQuantity > 0)
				{
					if (intTempQuantity < intTempLowestQuantity)
					{
						intTempLowestQuantity = intTempQuantity;
					}
				}
			}
			// If the lowest quantity is higher than the current quantity, disable the option
			if (intTempLowestQuantity > intQuantity)
			{
				objTemp.disabled = true;
			}
		}
	}
	// Comboboxes
	for(var intTemp=0; intTemp < objForm.getElementsByTagName("option").length; intTemp++)
	{
		var objTemp = objForm.getElementsByTagName("option")[intTemp];
		if (objTemp.getAttribute("accesskey"))
		{
			// Enable option
			objTemp.disabled = false;
			objTemp.style.backgroundColor = "";
			// Make array of prices
			var arrProductAccessKey = objTemp.getAttribute("accesskey").split("|");
			var intTempLowestQuantity = 9999999;
			for (var intKey in arrProductAccessKey)
			{
				// Determine lowest quantity for this option
				var intTempQuantity = arrProductAccessKey[intKey].split(":")[0] * 1;
				if (intTempQuantity > 0)
				{
					if (intTempQuantity < intTempLowestQuantity)
					{
						intTempLowestQuantity = intTempQuantity;
					}
				}
			}
			// If the lowest quantity is higher than the current quantity, disable the option
			if (intTempLowestQuantity > intQuantity)
			{
				objTemp.disabled = true;
				if (objTemp.selected == true)
				{
					objTemp.parentNode.selectedIndex = 0;
				}
				objTemp.style.backgroundColor = "#eeeeee";
			}
		}
	}
	// Update prices
	calculatePrice();
}

function calculatePrice()
{
	var objQuantity = document.getElementById("quantity");
	var intQuantity = Math.round(objQuantity.value * 1);
	var strProductAccessKey = objQuantity.accessKey;
	var arrProductAccessKey = strProductAccessKey.split("|");
	var arrPrices = new Array();
	// Put product prices into array
	for (var intKey in arrProductAccessKey)
	{
		var intTempQuantity = arrProductAccessKey[intKey].split(":")[0];
		var intTempPrice = arrProductAccessKey[intKey].split(":")[1];
		if (typeof intTempPrice != "undefined")
		{
			arrPrices[intTempQuantity] = intTempPrice;
		}
	}
	// Get the cheapest price for the customer based on the selected quantity
	var intHighestpossible = 0;
	for (var intKey in arrPrices)
	{
		if (intQuantity * 1 >= intKey * 1)
		{
			intHighestpossible = intKey * 1;
		}
	}
	if (intHighestpossible > 0)
	{
		// Calculate price of extra's
		var intProductextrasPrice = 0;
		// BEGIN --------------------------------------------------------------------------------------------------------------------------------
		var objForm = document.getElementById("orderform");
		// Inputs
		for(var intTemp=0; intTemp < objForm.getElementsByTagName("input").length; intTemp++)
		{
			var objTemp = objForm.getElementsByTagName("input")[intTemp];
			if (objTemp.accessKey)
			{
				var strOptionAccessKey = "";
				// Only count undisabled and check checkboxes
				if ((objTemp.type == "checkbox") && (objTemp.checked == true) && (objTemp.disabled == false))
				{
					strOptionAccessKey = objTemp.accessKey;
				}
				// Only count undisabled and check radio buttons
				if ((objTemp.type == "radio") && (objTemp.checked == true) && (objTemp.disabled == false))
				{
					strOptionAccessKey = objTemp.accessKey;
				}
				// Only count undisabled and text fields which have a value and which aren't the quantity textbox
				if ((objTemp.type == "text") && (objTemp.value) && (objTemp.disabled == false) && (objTemp.id != "quantity"))
				{
					strOptionAccessKey = objTemp.accessKey;
				}
				if (strOptionAccessKey != "")
				{
					var arrOptionAccessKey = strOptionAccessKey.split("|");
					var arrOptionPrices = new Array();
					var intTempOptionHighestQuantity = 0;
					for (var intKey in arrOptionAccessKey)
					{
						// Determine highest quantity for this option
						var intTempOptionQuantity = arrOptionAccessKey[intKey].split(":")[0] * 1;
						var intTempOptionPrice = arrOptionAccessKey[intKey].split(":")[1] * 1;
						arrOptionPrices[intTempOptionQuantity] = intTempOptionPrice;
						if (intTempOptionQuantity > 0)
						{
							if ((intTempOptionQuantity <= intQuantity) && (intTempOptionQuantity > intTempOptionHighestQuantity))
							{
								intTempOptionHighestQuantity = intTempOptionQuantity * 1;
							}
						}
					}
					if (objTemp.alt == "baseprice")
					{
						// Calculate this price as a baseprice: only count it once!
						intProductextrasPrice += (arrOptionPrices[intTempOptionHighestQuantity] / intQuantity) * objTemp.value;
					}
					else
					{
						// Normal, just add up the prices per product
						intProductextrasPrice += arrOptionPrices[intTempOptionHighestQuantity];
					}
				}
			}
		}
		var objForm = document.getElementById("orderform");
		// Comboboxes
		for(var intTemp=0; intTemp < objForm.getElementsByTagName("select").length; intTemp++)
		{
			var objTemp = objForm.getElementsByTagName("select")[intTemp];
			if (objTemp.selectedIndex > 0)
			{
				var strOptionAccessKey = objTemp.options[objTemp.selectedIndex].getAttribute("accesskey");
				var arrOptionAccessKey = strOptionAccessKey.split("|");
				var arrOptionPrices = new Array();
				var intTempOptionHighestQuantity = 0;
				for (var intKey in arrOptionAccessKey)
				{
					// Determine highest quantity for this option
					var intTempOptionQuantity = arrOptionAccessKey[intKey].split(":")[0] * 1;
					var intTempOptionPrice = arrOptionAccessKey[intKey].split(":")[1] * 1;
					arrOptionPrices[intTempOptionQuantity] = intTempOptionPrice;
					if (intTempOptionQuantity > 0)
					{
						if ((intTempOptionQuantity <= intQuantity) && (intTempOptionQuantity > intTempOptionHighestQuantity))
						{
							intTempOptionHighestQuantity = intTempOptionQuantity * 1;
						}
					}
				}
				intProductextrasPrice += arrOptionPrices[intTempOptionHighestQuantity];
			}
		}
		// END ------------------------------------------------------------------------------------------------------------------------------------
		// Show price details and totals
		document.getElementById("priceperproduct").innerHTML = priceShow(arrPrices[intHighestpossible]);
		document.getElementById("productextras").innerHTML = priceShow(intProductextrasPrice);
		document.getElementById("totalprice").innerHTML = priceShow((intProductextrasPrice * 1 + arrPrices[intHighestpossible] * 1) * intQuantity);
	}
	else
	{
		// No or negative quantity selected
		document.getElementById("priceperproduct").innerHTML = "-";
		document.getElementById("productextras").innerHTML = "-";
		document.getElementById("totalprice").innerHTML = "-";
	}
}
function priceShow(intValue)
{
	// Calculate ratio
	var intMoney = intValue;
	// Round off on two decimals
	intMoney = Math.round(intMoney * 100) / 100;
	intMoney = (intMoney == Math.floor(intMoney)) ? intMoney + ".00" : ( (intMoney * 10 == Math.floor(intMoney * 10)) ? intMoney + "0" : intMoney);	
	return strMoneycode + " " + intMoney;
}
