

/*
OrderCreator.js
Rhett Shoemaker

javascript functions to implement the client side logic of the OrderCreator.ascx control.
This is the control in the left gutter which the user uses to add images to their basket.
The javascript methods here control what shows up in the product dropdown, and the 
"Advanced Ordering Options" section.  These options change depending on the product line
selection and product selection.
*/

	//Constructor for the OrderCreator class.
	function OrderCreator()
	{
		this.GroupPairs = new Array();
		this.ddProductLine = null;
		this.ddProduct = null;
		this.products = new Array();
		this.OrderOptions = null;
		this.Toggle = null;
		
		this.ToggleOptions = ToggleOptions;
		this.Product = Product;
		this.SwitchLine = SwitchLine;
		this.SwitchProduct = SwitchProduct;
		this.AddProdOption = AddProdOption;
		this.CurrentProduct = null;

		//document.getElementById('toggle').onclick = this.ToggleOptions;

		return this;
	}

	//Open/Close the 'advanced ordering options' section
	function ToggleOptions()
	{
		var elm, tgl;
		var i;

		if (! this.OrderOptions || ! this.Toggle)
			return;
			
		if (this.OrderOptions.style.display=='none')
		{
			this.OrderOptions.style.display='block';
			this.Toggle.style.backgroundImage = 'url(../images/panels/minusIcon.gif)';
		}
		else
		{
			this.OrderOptions.style.display='none';
			this.Toggle.style.backgroundImage = 'url(../images/panels/plusIcon.gif)';
		}
	}
	
	//Constructor for a product class.
	function Product(name, id, lineId, aspectRatio)
	{
		var obj = new Object();
		
		obj.name = name;
		obj.id = id;
		obj.lineId = lineId;
		obj.aspectRatio = aspectRatio;
		
		return obj;
	}
	
	
	var last;
	function Debug(msg)
	{
		var elm = document.getElementById("dbg");
		
		
		if (msg == last && 1==0)
			msg = ". ";
		else
		{
			last = msg;				
			msg += " ";
		}
			
		if (elm)
			elm.innerHTML += msg;
	}
		
		
	
	//user changed the value in the product line
	//dropdown, so we repopulate the other elements
	function SwitchLine()
	{
		var line, option, i, j;

		line = this.ddProductLine.value;
		
		this.ddProduct.options.length = 0;
		
		while(this.ddProduct.options.length > 0)
			this.ddProduct.options[0] = null;
			
		
		j = 0;
		
		for (i = 0; i<this.products.length; i++)
			if (this.products[i].lineId == line)
			{
				this.ddProduct.options[j] = new Option(this.products[i].name, this.products[i].id);
				j++;
			}
			

		//if (this.ddProduct.options.length > 0)
		//	this.ddProduct.selectedIndex = 0;
			
		this.SwitchProduct();
		
	}
	
	
	//user changed the value in the product
	//dropdown, so we repopulate 'advanced ordering options'
	function SwitchProduct()
	{
		var prodID, group, i;		
		
		prodID = this.ddProduct.value;
		
		//find current product
		for(i=0;i<this.products.length;i++)
			if (this.products[i].id == prodID)
				this.CurrentProduct = this.products[i];

		//hide all option boxes
		for(i=0;i<this.GroupPairs.length;i++)
			document.getElementById(this.GroupPairs[i].groupName).style.display='none';

		//show option boxes for the current product
		for(i=0;i<this.GroupPairs.length;i++)
		{
			group = document.getElementById(this.GroupPairs[i].groupName);
			if (prodID == this.GroupPairs[i].prodID)
				group.style.display = 'block';
		}		
		
		
	}
	
	
	function AddProdOption(groupName, prodID)
	{
		this.GroupPairs[i] = new GroupPair(groupName, prodID);
		i++;
	}
	
	function GroupPair(groupName, prodID)
	{
		this.groupName = groupName;
		this.prodID = prodID;
		
		return this;
	}
	
