function update_price(node, select_type, colour_count)
{
	dimensions_element = document.getElementById('product_dimensions');
	
	if(select_type == 1 || (select_type == 0 && colour_count == 0))
	{
		select_product_dimensions(dimensions_element, node, select_type);
	}
	else if(select_type == 2)
	{
		extra_element = document.getElementById('product_options');
		
		select_product_extra(extra_element, node);
	}
	else if(select_type == 0 && colour_count > 0)
	{
		colour_element = document.getElementById('product_colour');
	
		select_product_colour(colour_element, node, select_type);
	}	
}

function select_product_dimensions(dimensions_element, node, select_type)
{
	// get the selected dimensions
	var selected_dimensions = dimensions_element.options[dimensions_element.selectedIndex].value;
	
	// clear the prices
	var prices_container = document.getElementById('product_prices');
	prices_container.innerHTML = 'Please select an option from the above menus to view the price';
	
	if(select_type == 1)
	{
		// product only has dimensions
		var dimensions_element = document.getElementById('product_dimensions');
		var selected_dimensions = dimensions_element.options[dimensions_element.selectedIndex].value;
		
		if(selected_dimensions != "")
		{
			var prices = dimensions[selected_dimensions];
			
			output_the_prices(prices, node);
		}
	}
	else
	{
		// clear the colours list
		var colour_element = document.getElementById('product_colour');
	
		colour_element.options.length = 0;

		if(selected_dimensions == "")
		{
			colour_element.options[0] = new Option("Please select dimensions", "");
			
			if(document.getElementById('product_options'))
			{
				var option_element = document.getElementById('product_options');
				
				option_element.options.length = 0;
				
				option_element.options[0] = new Option("Please select colour before option", "");
			}
			
			return false;
		}
	
		// get the total number of colour options for the selected dimensions
		var total_colours = 0;
	
		for (var colour in dimensions[selected_dimensions])
		{
			if(dimensions[selected_dimensions].hasOwnProperty(colour))
			{
				total_colours++;
			}
		}
	
		if(total_colours == 0)
		{
			colour_element.options[0] = new Option("N/A", "N/A");
		}
		else
		{
			if(total_colours == 1)
			{
				var options_count = 0;
			}
			else
			{
				colour_element.options[0] = new Option("Please select a colour", "");
	
				var options_count = 1;
			}
	
			for (var colour in dimensions[selected_dimensions])
			{
				if(dimensions[selected_dimensions].hasOwnProperty(colour))
				{
					colour_element.options[options_count] = new Option(colour, colour);
					options_count++;
				}
			}
	
			// select the colour if only 1 exists
			if(total_colours == 1 && select_type == 2)
			{
				// get the one selected colour
				var selected_colour = colour_element.options[0].value;
				
				// output the options menu
				output_options(selected_dimensions, selected_colour, node);			
			}
			else if(total_colours == 1)
			{
				select_product_colour(colour_element, node);
			}
		}
	}
}

function output_options(selected_dimensions, selected_colour, node)
{
	// get the options element
	var option_element = document.getElementById('product_options');
	
	var selected_option = option_element.options[option_element.selectedIndex].value;
	
	/*if(option_element.options.length > 1)
	{
		var prices = dimensions[selected_dimensions][selected_colour][selected_option];
		
		output_the_prices(prices, node);
	}
	else
	{*/
		option_element.options.length = 0;
		
		var total_options = 0;
					
		// count up the extras
		for (var option in dimensions[selected_dimensions][selected_colour])
		{
			if(dimensions[selected_dimensions][selected_colour].hasOwnProperty(option))
			{
				total_options++;
			}
		}
	
		if(total_options == 1)
		{
			var t_options_count = 0;
		}
		else
		{
			// if theres more than 1 option then add a select to the dropdown
			option_element.options[0] = new Option("Please select an option", "");
	
			var t_options_count = 1;
		}
		
		// add the options to the dropdown
		for (var option in dimensions[selected_dimensions][selected_colour])
		{
			if(dimensions[selected_dimensions][selected_colour].hasOwnProperty(option))
			{
				option_element.options[t_options_count] = new Option(option, option);
				t_options_count++;
			}
		}
		
		if(total_options == 1)
		{
			// get the one selected option
			var selected_option = option_element.options[0].value;
			
			var prices = dimensions[selected_dimensions][selected_colour][selected_option];
			
			output_the_prices(prices, node);
		}
	//}
}

function select_product_colour(colour_element, node, the_option)
{	
	var prices_container = document.getElementById('product_prices');
	prices_container.innerHTML = 'Please select an option from the above menus to view the price';
	
	if(!colour_element.options){
		//return false;
		colour_element = document.getElementById('product_colour');
	}
	
	if(colour_element.value == "")
	{
		if(document.getElementById('product_options'))
		{
			var option_element = document.getElementById('product_options');
			
			option_element.options.length = 0;
			
			option_element.options[0] = new Option("Please select colour before option", "");
		}
		
		return false;
	}
	
	if(the_option == "")
	{
		the_option = 0;	
	}
	
	// get the selected dimensions
	var dimensions_element = document.getElementById('product_dimensions');
	var selected_dimensions = dimensions_element.options[dimensions_element.selectedIndex].value;

	// get the selected colour
	var selected_colour = colour_element.options[colour_element.selectedIndex].value;
	
	// if theres extras and colours
	if(the_option == 2)
	{		
		output_options(selected_dimensions, selected_colour, node);
	}
	else
	{
		var prices = dimensions[selected_dimensions][selected_colour];
		
		output_the_prices(prices, node);
	}
}

function select_product_extra(extra_element, node)
{	
	if(!extra_element.options){
		//return false;
		extra_element = document.getElementById('product_options');
	}
	if(extra_element.value == "")
	{
		var prices_container = document.getElementById('product_prices');
		prices_container.innerHTML = 'Please select an option from the above menus to view the price';
		
		return false;
	}
	
	// get the selected dimensions
	var dimensions_element = document.getElementById('product_dimensions');
	var selected_dimensions = dimensions_element.options[dimensions_element.selectedIndex].value;

	// get the selected colour
	var colour_element = document.getElementById('product_colour');
	var selected_colour = colour_element.options[colour_element.selectedIndex].value;
	
	// get the selected option
	var selected_option = extra_element.options[extra_element.selectedIndex].value;

	var prices = dimensions[selected_dimensions][selected_colour][selected_option];
		
	output_the_prices(prices, node);
}

function output_the_prices(prices, node)
{
	var selected_product_id = prices['id'];
	
	var stock_qty = prices['stock'];
	
	// get the quantity that they've entered
	var quantity = document.getElementById('dd_product_quantity').value;
	
	// work out the price from the quantity
	var price = get_price_from_quantity(quantity, prices);

	var html = '<table style="width:100%;">'+
					'<tr>'+
						'<td style="width:85px;" valign="top">Price</td>'+
						'<td valign="top">' + get_price_output(price, node) + '</td>'+
					'</tr>';
	
	/*html += '<table style="width:100%;">'+
					'<tr>'+
						'<td style="width:85px;" valign="top">Single Price</td>'+
						'<td valign="top">' + get_price_output(prices['price'], node) + '</td>'+
					'</tr>';

	if(prices['5off'] != "")
	{
		html += 	'<tr>'+
						'<td valign="top">5 - 9</td>'+
						'<td valign="top">' + get_price_output(prices['5off'], node) + '</td>'+
					'</tr>';
	}

	if(prices['10off'] != "")
	{
		html += 	'<tr>'+
						'<td valign="top">10 - 19</td>'+
						'<td valign="top">' + get_price_output(prices['10off'], node) + '</td>'+
					'</tr>';
	}

	if(prices['20off'] != "")
	{
		html += 	'<tr>'+
						'<td valign="top">20 - 49</td>'+
						'<td valign="top">' + get_price_output(prices['20off'], node) + '</td>'+
					'</tr>';
	}

	if(prices['50off'] != "")
	{
		html += 	'<tr>'+
						'<td valign="top">50 - 99</td>'+
						'<td valign="top">' + get_price_output(prices['50off'], node) + '</td>'+
					'</tr>';
	}

	if(prices['100off'] != "")
	{
		html += 	'<tr>'+
						'<td valign="top">100+</td>'+
						'<td valign="top">' + get_price_output(prices['100off'], node) + '</td>'+
					'</tr>';
	}*/
	
	//check if any extras selected
	extras = check_product_extras('html');
	
	var stock_message = '';
	
	if(Number(stock_qty) > 0 && Number(stock_qty) >= Number(quantity))
	{
		stock_message = '<span class="available">Available for immediate despatch</span>';	
	}
	
	if(extras != undefined)
	{
		html += extras;
	}
	
	if(price != "volume-contact" && price != "contact")
	{
		html += 		'</table><table><tr>'+
							'<td width="230">' + stock_message + '</td>'+
							'<td><input type="hidden" id="sc_product_number" name="sc_product_number" value="0_' + selected_product_id + '" /><input class="button3" type="button" id="buy" name="btn_buy_product" value="ADD TO ORDER" onclick="cart_add_item(\'0_'+ selected_product_id + '\');"/></td>'+
						'</tr>'+
					'</table>';
	}

	var prices_container = document.getElementById('product_prices').innerHTML = html;

	/*var product_number = document.getElementById('sc_product_number');
	product_number.value = prices['id'];*/
}

function get_price_from_quantity(quantity, prices)
{
	numbers_array = new Array("100", "50", "20", "10", "5", "1");
	
	for(var i=0;i<numbers_array.length;i++)
	{
		var num = numbers_array[i];
		
		if(num == "1")
		{
			price_field = "price";
		}
		else if(quantity >= Number(num))
		{
			if((trim(prices[num+'off']) == "") || (trim(prices[num+'off']) == "contact"))
			{
				price_field = "volume-contact";
			}
			else
			{
				price_field = num+"off";	
			}
			
			break;
		}
	}
	
	if(price_field == "volume-contact")
	{
		return 	price_field;
	}
	else
	{
		return prices[price_field];
	}
}

/*
Old get_price_from_quantity function

if((quantity >= 100) && (trim(prices['100off']) != "") && (trim(prices['100off']) != "n/a") && (trim(prices['100off']) != "contact") && (trim(prices['100off']) != "stockists"))
{
	price_field = "100off";
}
else if((quantity >= 50) && (trim(prices['50off']) != "") && (trim(prices['50off']) != "n/a") && (trim(prices['50off']) != "contact") && (trim(prices['50off']) != "stockists"))
{
	price_field = "50off";
}
else if((quantity >= 20) && (trim(prices['20off']) != "") && (trim(prices['20off']) != "n/a") && (trim(prices['20off']) != "contact") && (trim(prices['20off']) != "stockists"))
{
	price_field = "20off";
}
else if((quantity >= 10) && (trim(prices['10off']) != "") && (trim(prices['10off']) != "n/a") && (trim(prices['10off']) != "contact") && (trim(prices['10off']) != "stockists"))
{
	price_field = "10off";
}
else if((quantity >= 5) && (trim(prices['5off']) != "") && (trim(prices['5off']) != "n/a") && (trim(prices['5off']) != "contact") && (trim(prices['5off']) != "stockists"))
{
	price_field = "5off";
}
else
{
	price_field = "price";
}

return prices[price_field];*/

function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function check_product_extras(request){
	if (document.getElementById('num_extras')) 
	{
		var num_extras = document.getElementById('num_extras').value;
		
		if (num_extras > 0) 
		{
			extras_price = 0;
			extra_fields = "";
			num_checked = 0;
			
			for (i = 1; i <= num_extras; i++) 
			{				
				if (document.getElementById('extra_' + i).checked) 
				{					
					extra = dimensions["Extra" + i]['details'];
					extras_price += Number(extra['price']);
					
					extra_id = dimensions["Extra" + i]['details']['id'];
					
					extra_fields += '<input type="hidden" id="sc_product_number_' + i + '" name="sc_product_number[]" value="' + extra_id + '" />';
					
					num_checked++;
				}
			}
			
			if(extras_price > 0)
			{
				var price_inc_vat = getProdPricePlusVat(extras_price);	
			}
			
			extras_price = number_format(extras_price, 2, ".", "");
			
			if (num_checked > 0 && request == "html") {
				return '<tr id="extra_price">' +
				'<td valign="top" width="85">Extras total price (single)</td>' +
				'<td valign="top"><span class="prodPrice">&pound;' +
				extras_price +
				'</span> exc. VAT<br />' +
				price_inc_vat +
				extra_fields +
				'</td>' +
				'</tr>';
			}
			else 
				if (request == "html") {
					return '<tr id="extra_price"></tr>'
				}
		}
	}
}

function get_price_output(value, node)
{
	var output = '';

	switch (value.toLowerCase())
	{
		case 'n/a': output = 'Not Available'; break;
		case 'contact': output =  output = "<div class=\"volumeText\"><a title=\"Contact Us\" href=\"javascript:volume_form('"+node+"');\" class=\"prodPrice\">Click here for a price<br /> for this quantity</a></div>"; break;
		case 'stockists': output = '<a title="Stockists" href="stockists.php?p=' + node + '">Stockists</a>'; break;
		case 'volume-contact': output = "<div class=\"volumeText\"><a title=\"Contact Us\" href=\"javascript:volume_form('"+node+"');\" class=\"prodPrice\">Click here for a price<br /> for this quantity</a></div>"; break;
		default:  output = '<span class="prodPrice">&pound;' + value + ' each</span> exc. VAT<br />' + getProdPricePlusVat(value); break;
	}
	
	return output;
}

function buy_product()
{
	document.frm_buy_product.submit();
}

function getProdPricePlusVat(price)
{
	var theVAT = 20;
	
	var thePrice = eval(price);
	
	// add on the vat
	thePrice += (theVAT/100) * thePrice;
	
	thePrice = Math.round(thePrice*100)/100;
	
	thePrice = number_format(thePrice, 2, ".", "");
	
	return '(&pound;'+thePrice+' inc. vat)';
}

function number_format(number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands  
    number = (number+'').replace(',', '').replace(' ', '');
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }    return s.join(dec);
}
