//UI Stuff
function initProductMenu()
{

	// Check current section
	currentItem = window.location.href.split("#");
	if (currentItem.length >= 2)
	{
		currentItem = currentItem[1];	
		
		currentElement = document.getElementsByAttribute("name", currentItem);
		
		if (hasClassName(currentElement[0].parentNode, "category"))
		{
			addClass(currentElement[0].parentNode, "active");
		}
		else
		{
			addClass(currentElement[0].parentNode.parentNode.parentNode, "active");
		}
		addClass(currentElement[0], "current");
		loadCategoryContent(currentItem.replace('c',''),'');
	}	
	
	navItems = $('productMenu').getElementsByTagName('a');
	
	for (i = 0; i < navItems.length; i++)
	{
		navItems[i].onclick = function()
		{
			
			// Mark all as Inactive
			navLinks = $('productMenu').getElementsByTagName('a');
			{
				for (i = 0; i<navLinks.length; i++)
				{
					removeClass(navLinks[i], "current");
				}
			}
			
			navItems = $('productMenu').getElementsByTagName('li');
			if (hasClassName(this.parentNode, "category"))
			{
				for (i = 0; i<navItems.length; i++)
				{
					if (hasClassName(navItems[i], "category")) removeClass(navItems[i],"active");
				}
			}
			
			if (hasClassName(this.parentNode,"category"))
			{
				if (hasClassName(this.parentNode, "active"))
				{
					removeClass(this.parentNode,"active");	
				}
				else
				{
					addClass(this.parentNode,"active");
				}
			}
			
			addClass(this, "current");
			
			loadCategoryContent(this.getAttribute('name').replace('c',''),'');
			//return false;
		}
	}
}

function loadItem(ItemNo, T)
{
	currentItem = window.location.href.split('#');
	if (currentItem.length >= 2)
		currentItem = '#' + currentItem[1];
	else
		currentItem = '';
		
	window.location.href = 'Product.aspx?ItemNo=' + ItemNo + '&T=' + T + currentItem;
}

function initShoppingCart()
{	
	$('hlReviewOrder').onclick = function()	
	{
		if (hasClassName("shoppingCart","inactive"))
		{
			removeClass("shoppingCart","inactive");
		}
		else
		{
			addClass("shoppingCart","inactive");
		}
		return false;
	}
	
	$('hlBasketSummary').onclick = function()	
	{
		if (hasClassName("shoppingCart","inactive"))
		{
			removeClass("shoppingCart","inactive");
		}
		else
		{
			addClass("shoppingCart","inactive");
		}
		return false;
	}
}

window.onload = function ()
{
	initShoppingCart();
	initProductMenu();
}
/*
addEvent(window, "load", initShoppingCart);
addEvent(window, "load", initProductMenu);
*/
//AJAX Stuff
var xmlhttp;
function loadCategoryContent(categoryNo, searchValues)
{						
	if (categoryNo == '-1')
	{
		document.getElementById('lblMessage').innerHTML = 'Searching...';
		if (document.getElementById('artworkSpec'))
			document.getElementById('artworkSpec').style.display = 'none';
		if (document.getElementById('rblQty'))
			document.getElementById('rblQty').style.display = 'none';
		if (document.getElementById('btnOrder'))
			document.getElementById('btnOrder').style.display = 'none';
			
		var ddls = document.getElementById('contentContainer').getElementsByTagName('select');
		for (var i = 0; i < ddls.length; i++)
			ddls[i].disabled = true;
	}
	else
	{
		document.getElementById('mainContent').innerHTML = 'Loading...';
	}
				
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
		xmlhttp.onreadystatechange = setContent;
		if (categoryNo == '-1')
			xmlhttp.open('GET', 'Content.aspx?SearchValues=' + searchValues + '&T=' + Math.random(), true);
		else
			xmlhttp.open('GET', 'Content.aspx?CategoryNo=' + categoryNo + '&T=' + Math.random(), true);
			xmlhttp.send(null);
	}
	else if (window.ActiveXObject)
	{
		xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		if (xmlhttp)
		{
			xmlhttp.onreadystatechange = setContent;
			if (categoryNo == '-1')
				xmlhttp.open('GET', 'Content.aspx?SearchValues=' + searchValues + '&T=' + Math.random(), true);
			else
				xmlhttp.open('GET', 'Content.aspx?CategoryNo=' + categoryNo + '&T=' + Math.random(), true);
			xmlhttp.send();
		}
	}
}

function setContent()
{
	if (xmlhttp.readyState == 4)
	{
		if (xmlhttp.status == 200)
		{
			var xml = xmlhttp.responseText;
			var start = xml.indexOf('<div');
			var end = xml.lastIndexOf('</div>') + 6;
			document.getElementById('mainContent').innerHTML = xml.substring(start, end);
		}
	}
}

function submitSearchValues()
{
	var values = '';
	for (var i = 0; i <= 6; i++)
	{
		var control = document.getElementById('ddl' + i);
		if (control)
		{	
			if (control.options[control.selectedIndex].value != 0)
				values = values + control.options[control.selectedIndex].value + '-';
		}
	}
		
	loadCategoryContent('-1', values);
}

function selectProduct()
{
	var values = '';
	var productQtyOptions = document.getElementById('rblQty').getElementsByTagName('input');
	var productQty;
	for (var i = 0; i < productQtyOptions.length && !productQty; i++)
	{
		if (productQtyOptions[i].name = 'rblQty' && productQtyOptions[i].checked == true)
			productQty = productQtyOptions[i].value;
	}
	for (var i = 0; i <= 6; i++)
	{
		var control = document.getElementById('ddl' + i);
		if (control)
		{
			if (control.options[control.selectedIndex].value != 0)
				values = values + control.options[control.selectedIndex].value + '-';
		}
	}
	var currentItem = window.location.href.split('#');
	if (currentItem.length >= 2)
		currentItem = '#' + currentItem[1];
	else
		currentItem = '';
		
	var productURL = 'Product.aspx?ProductNo=' + productQty.substring(0, productQty.indexOf('-'));
	productURL += '&QtyPartNo=' + productQty.substring(productQty.indexOf('-') + 1);
	productURL += '&SearchValues=' + values + currentItem;
	window.location.href = productURL;
}
