var new_price;
var prod_array = new Array();
var option_texts = new Array();
var discount_table = new Array();

function load_options()
{
	var f = document.forms.basket;
	j = 0;
	for(i=0;i<f.elements.length;i++)
	{
		if(f.elements[i].type=='hidden' && f.elements[i].name.substr(0,7)=='prodopt')
		{
			prod_array[j] = f.elements[i].value.split('|');
			j++;
		}
	}
	o_texts = document.getElementById('opt_texts').value.split('|');
	for(i=0;i<o_texts.length;i++)
	{
		o_line = o_texts[i].split(',');
		option_texts[o_line[0]] = o_line[1];
	}
	discount_table = document.getElementById('discount_table').value.split('|');
}

function switch_option(option_id)
{
	var f = document.forms.basket;

	srch_str = ',';

	for(i=0;i<f.elements.length;i++)
	{
		if((f.elements[i].type=='select-one') && f.elements[i].name.substr(0,3)=='opt')
		{
			srch_str += f.elements[i].name.substr(3) + '=' + f.elements[i].value + ',';
			if(option_id == f.elements[i].name.substr(3))
			{
				for(j=i+1;j<f.elements.length;j++)
				{
					if((f.elements[j].type=='select-one') && f.elements[j].name.substr(0,3)=='opt')
					{
						set_selector(f.elements[j].name.substr(3), srch_str);
						option_id = f.elements[j].name.substr(3);
						break;
					}
				}
			}
		}
	}

	new_options = ',';

	for(i=0;i<f.elements.length;i++)
	{
		if((f.elements[i].type=='select-one') && f.elements[i].name.substr(0,3)=='opt')
		{
			new_options += f.elements[i].name.substr(3) + '=' + f.elements[i].value + ',';
		}
	}
	for(i=0;i<prod_array.length;i++)
	{
		if(prod_array[i][0]==new_options)
		{
			image_name = prod_array[i][1];
			new_price = prod_array[i][2];
			document.getElementById('base_price').value = parseFloat(new_price).toFixed(2);
			document.getElementById('product_image').src = "get_image.php?img=products/"+image_name+"&type=medium";
			document.getElementById('image_name').value = image_name;
			document.getElementById('option_string').value = prod_array[i][0];
			document.getElementById('product_link').href = "Javascript:openPopWinProduct('index.php?pagename=product_view_large&product_id=" + document.getElementById('product_id').value + "&image_name=" + image_name + "','Product', " + prod_array[i][3] + "," + prod_array[i][4] + ");";
		}
	}

	if(discount_table.length > 0)
	{
		d_table = '<table class="discounttable" cellspacing="0"><tr><td>Quantity</td>';
		for(i=0;i<discount_table.length-1;i++)
		{
			d_line = discount_table[i].split(',');
			d2_line = discount_table[i+1].split(',');
			d_qty = d_line[0];
			d2_qty = d2_line[0]-1;
			d_table += '<td>' + parseInt(d_qty) + ' - ' + d2_qty + '</td>';
		}
		d_table += '</tr><tr><td>Price</td>';
		for(i=0;i<discount_table.length-1;i++)
		{
			d_line = discount_table[i].split(',');
			d_price = parseFloat(document.getElementById('base_price').value) + (parseFloat(document.getElementById('base_price').value) * parseFloat(d_line[1]) / 100.00);
			d_table += '<td>' + parseFloat(d_price).toFixed(2) + '</td>';
		}
		d_table += '</tr></table><div>All prices are ex VAT. For larger quantities, please contact us directly.</div>';

		document.getElementById('product_discounts').innerHTML = d_table;
	}
	recalc_price();

}

function set_selector(option_id, sel_str)
{
	var opt_string = sel_str + option_id + '=';
	var opt_length = opt_string.length;
	var option_string = '';
	opt_value = 0;
	opt_text = '';
	prev_opt = 0;

	ndx = 0;
	document.getElementById('opt'+option_id).options.length = 0;

	for(kkk=0;kkk<prod_array.length;kkk++)
	{
		if(prod_array[kkk][0].substr(0,opt_length)==opt_string)
		{
			comma_pos = prod_array[kkk][0].indexOf(',', opt_length);
			opt_value = prod_array[kkk][0].substring(opt_length,comma_pos);
			if(parseInt(opt_value) != parseInt(prev_opt))
			{
				opt_text = option_texts[opt_value];
				document.getElementById('opt'+option_id)[ndx] = new Option(opt_text, opt_value);
				ndx++;
				prev_opt = prod_array[kkk][0].substring(opt_length,comma_pos);
			}
		}
	}
}

function recalc_price()
{
	var input_qty = document.getElementById('qty').value;

	new_price = parseFloat(document.getElementById('base_price').value);

	if(!isNaN(parseInt(input_qty)) && parseInt(input_qty) > 0 && discount_table.length > 0)
	{
		for(i=0;i<discount_table.length;i++)
		{
			dis_line = discount_table[i].split(',');
			if(input_qty >= parseInt(dis_line[0]))
			{
				new_price = parseFloat(document.getElementById('base_price').value) + (parseFloat(document.getElementById('base_price').value) * parseFloat(dis_line[1]) / 100.00);
			}
		}
	}
	document.getElementById('price').value = parseFloat(new_price).toFixed(2);
	
}

// Pop up window
function openPopWinProduct(strFile, strName, intWidth, intHeight)
{
	var strProperties = 'toolbar=no,width='+Math.round(intWidth)+',height='+Math.round(intHeight)+',left=50,top=50,'+'status=no,scrollbars=no,resizable=no,menubar=no';
	tmpWin = window.open(strFile,strName,strProperties);
	tmpWin.focus();
}
