function select_product_dimensions(dimensions_element, node)
{
	// get the selected dimensions
	var selected_dimensions = dimensions_element.options[dimensions_element.selectedIndex].value;

	// clear the colours list
	var colour_element = document.getElementById('product_colour');

	colour_element.options.length = 0;

	// clear the prices
	var prices_container = document.getElementById('product_prices');
	prices_container.innerHTML = '';

	if(selected_dimensions == "")
	{
		colour_element.options[0] = new Option("Please select dimensions", "");
		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_product_colour(colour_element, node);
		}
	}
}

function select_product_colour(colour_element, node)
{
	if(!colour_element.options){
		//return false;
		colour_element = document.getElementById('product_colour');
	}
	if(colour_element.value == ""){
		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 selected_colour = colour_element.options[colour_element.selectedIndex].value;

	var prices = dimensions[selected_dimensions][selected_colour];


	var html = 	'<br />'+
				'<table style="width:100%;">'+
					'<tr class="px11 green2">'+
						'<td class="green1" style="width:125px;"><strong>Price per unit</strong></td>'+
						'<td><strong class="px14">' + get_price_output(prices['price'], node) + '</strong></td>'+
					'</tr>';

	if(prices['5off'] != "")
	{
		html += 	'<tr class="px11 green2">'+
						'<td class="green1"><strong>Price 5 - 9</strong></td>'+
						'<td><strong class="px14">' + get_price_output(prices['5off'], node) + '</strong></td>'+
					'</tr>';
	}

	if(prices['10off'] != "")
	{
		html += 	'<tr class="px11 green2">'+
						'<td class="green1"><strong>Price 10 - 19</strong></td>'+
						'<td><strong class="px14">' + get_price_output(prices['10off'], node) + '</strong></td>'+
					'</tr>';
	}

	if(prices['20off'] != "")
	{
		html += 	'<tr class="px11 green2">'+
						'<td class="green1"><strong>Price 20 - 49</strong></td>'+
						'<td><strong class="px14">' + get_price_output(prices['20off'], node) + '</strong></td>'+
					'</tr>';
	}

	if(prices['50off'] != "")
	{
		html += 	'<tr class="px11 green2">'+
						'<td class="green1"><strong>Price 50 - 99</strong></td>'+
						'<td><strong class="px14">' + get_price_output(prices['50off'], node) + '</strong></td>'+
					'</tr>';
	}

	if(prices['100off'] != "")
	{
		html += 	'<tr class="px11 green2">'+
						'<td class="green1"><strong>Price 100+</strong></td>'+
						'<td><strong class="px14">' + get_price_output(prices['100off'], node) + '</strong></td>'+
					'</tr>';
	}
	
	
	//check if any extras selected
	html += check_product_extras('html');
	
	

	html += 		'<tr class="px11 green2">'+
						'<td class="green1">&nbsp;</td>'+
						'<td><strong class="px14"><br /><input type="hidden" id="sc_product_number" name="sc_product_number[]" value="" /><input type="button" name="btn_buy_product" class="searchbox1"  value="Add to Cart" onclick="buy_product();"/></strong></td>'+
					'</tr>'+
				'</table>';

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

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

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 (num_checked > 0 && request == "html") {
				return '<tr class="px11 green2" id="extra_price">' +
				'<td class="green1"><strong>Extras price (per unit)</strong></td>' +
				'<td><strong class="px14">&pound;' +
				extras_price +
				'</strong>' +
				extra_fields +
				'</td>' +
				'</tr>';
			}
			else 
				if (request == "html") {
					return '<tr class="px11 green2" 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 = '<a title="Contact Us" href="contact_us.php">Please contact us.</a>'; break;
		case 'stockists': output = '<a title="Stockists" href="stockists.php?p=' + node + '">Stockists</a>'; break;
		default:  output = '&pound;' + value; break;
	}
	
	return output;
}

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