/*
 *		javascript for options pages
 */

function options_setup()
{
	var base = get_base();
	var index = 0;
	
	add_divs(base);
	animatedcollapse.init();
}

function get_base()
{
	var outer = document.getElementById("options_box");
	var base = null;
	
	for (var child = outer.firstChild; child != null ; child = child.nextSibling)
	{
		if(child.className == "option_input_box")
		{
			base = child;
			break;
		}
	}

	return base;
}
	
function add_divs
(
	node
)
{
	animatedcollapse.addDiv(node.id);
	
	for (var line = node.firstChild; line != null ; line = line.nextSibling)
	{
		var down = line.lastChild;
		
		if (down.className == "option_input_box")
		{
			add_divs(down);
		}
	}
}

function options_change()
{
	var base = get_base();
	
	set_prices(base);	
	set_display(base, true);
}

function set_prices
(
	base
)
{
	var area = parseFloat(document.getElementById("option_input_area").value);
	var quantity = parseInt(document.getElementById("copies").value);

	var price = Math.round(100 * compute_price(base, area)) / 100;

	document.getElementById("unit_price").innerHTML = format_price(price);
	document.getElementById("extended_price").innerHTML = format_price(price * quantity);
}

function format_price
(
	value
)
{
	return value.toFixed(2);
}

function set_display
(
	node, 
	parent_displayed
)
{
	if (parent_displayed)
	{
		animatedcollapse.show(node.id);
	}
	else
	{
		animatedcollapse.hide(node.id);
	}

	for (var line = node.firstChild; line != null ; line = line.nextSibling)
	{
		var down = line.lastChild;
		
		if (down.className == "option_input_box")
		{
			var prefix = line.firstChild;
				
			var input = prefix.firstChild;
					
			var set = (input.tagName == "INPUT") ? input.checked : true;

			set_display(down, parent_displayed && set);
		}
	}
}

function compute_price
(
	current,
	area
)
{
	var price = 0;
	var match_base = /^option_base_\d+$/;
	var match_variable = /^option_variable_\d+$/;		

	for (var line = current.firstChild; line != null ; line = line.nextSibling)
	{
		var prefix = line.firstChild;
		var prefix_child = prefix.firstChild;
		var input = prefix_child.tagName == "INPUT";
		
		var selected = input && prefix_child.checked;
		
		if (selected)
		{
			for (var contents = line.firstChild; contents != null ; contents = contents.nextSibling)
			{
				var id = contents.id;
	
				if (match_base.test(id))
				{
					price += parseFloat(contents.value);
				}
	
				if (match_variable.test(id))
				{
					price += parseFloat(contents.value) * area;
				}
			}
		}
		
		if (!input || selected)
		{
			var down = line.lastChild;
			
			if (down.className == "option_input_box")
			{
				price += compute_price(down, area);
			}
		}
	}

	return price;
}

function get_options
(
	current
)
{
	var match_id = /^option_input_id_\d+$/;
	
	var option_list = "";
	
	
	for (var line = current.firstChild; line != null ; line = line.nextSibling)
	{
		var prefix = line.firstChild;
		var prefix_child = prefix.firstChild;
		var input = prefix_child.tagName == "INPUT";
		
		var selected = input && prefix_child.checked;
		
		var tail = "";
		
		if (!input || selected)
		{
			var down = line.lastChild;
			
			if (down.className == "option_input_box")
			{
				tail = get_options(down);
			}
		}
		
		if (input && selected)
		{
			for (var contents = line.firstChild; contents != null ; contents = contents.nextSibling)
			{
				var id = contents.id;
				
				if (match_id.test(id))				
				{
					var option_id = contents.value;
					
					if (option_id != "")
					{
						var separator = option_list != "" ? "," : "";
						option_list += separator + option_id;
					}
					
					break;
				}
			}
		}

// This assumes there is a top level option

		option_list += option_list != "" && tail != "" ? "," : "";		
		option_list += tail != "" ? tail : "";		
	}

	return option_list;
}

