function manage_add_to_cart ()
{
	if (!document.getElementById('add_quantity')) return false;
	document.getElementById('add_quantity').onblur = check_this;
}

function check_this ()
{
	var sku = document.getElementById('add_sku').value;
	var quantity = parseInt(this.value);
	var availability = parseInt(document.getElementById(sku+'_availability').getAttribute('alt'));
	if (quantity > 0)
	{
		if (quantity > availability)
		{
			alert ('Sorry, we only have '+availability+' of that product available.');
			this.value = availability;
		}
	}
	else
	{
		alert ('You must provide a quantity greater than 0!');
		this.value = 1;
	}
}

///// Cart Functions /////
function manage_cart ()
{
	if (!document.getElementById('Cart')) return false;
//	var ajax_detected = getHTTPObject();
//	if (ajax_detected)
//	{
		var inputs = document.getElementById('Cart').getElementsByTagName('input');
		for (var i = 0; i < inputs.length; i++)
		{
			if (inputs[i].getAttribute('type') == 'text') inputs[i].onblur = recalculate_this;
		}
		
//		var links = document.getElementById('Cart').getElementsByTagName('a');
//		for (var i = 0; i < links.length; i++)
//		{
//			links[i].onclick = remove_this;
//		}		
//	}
}
/*
function remove_this () //Function on a link
{
	var link = this.getAttribute('href'); //gives '/remove/[loop_count]'
	var link_array = new Array();
	link_array = link.split('/');
	var loop_count = link_array[2];
	var request = getHTTPObject();
	if (request)
	{
		request.onreadystatechange = function()
		{
			manage_checkout(request);
		};
		request.open('GET', '/remove/'+loop_count, true); //Can I just use link?
		request.send(null);
	}
	
	var subtotal = parseFloat (this.parentNode.getElementsByTagName('span')[1].firstChild.nodeValue);
	var total = parseFloat (document.getElementById('cart_total').firstChild.nodeValue);
	document.getElementById('cart_total').firstChild.nodeValue = (total - subtotal).toFixed(2);

	this.parentNode.parentNode.removeChild(this.parentNode);
	if (document.getElementById('Cart').getElementsByTagName('li').length == 0)
	{
		document.getElementById('Cart').parentNode.removeChild(document.getElementById('Cart'));
		document.getElementById('Page').setAttribute('class', ''); //Re-center the page now that #Cart is gone.
	}
	return false;
}
*/
function recalculate_this () //Function on text inputs in #Cart
{
//	var price = parseFloat(this.parentNode.getElementsByTagName('span')[0].firstChild.nodeValue);
	var quantity = parseInt(this.value);
	var availability = parseInt(this.parentNode.parentNode.getElementsByTagName('p')[0].firstChild.nodeValue);
//	var subtotal = parseFloat(this.parentNode.getElementsByTagName('span')[1].firstChild.nodeValue);
//	var total = parseFloat(document.getElementById('cart_total').firstChild.nodeValue);
	
	if(quantity < 0)
	{
		quantity = Math.abs(quantity);
		this.value = quantity;
	}
	else if(quantity == 0)
	{
		alert ('Quantity must be greater than 0. If you\'d like to remove this item, click the remove button.');
		this.value = 1;
	}
	if(quantity > availability) //Not "else if" so that we can catch abs(negative) > availability
	{
		alert ('Sorry, we only have '+availability+' of that product available.');
		this.value = availability;
		quantity = availability;
	}
//	total += (price * quantity) - subtotal;
//	subtotal = price * quantity;
	
	//send request to /edit/[loop_count]/quantity to update cart
//	var link = this.parentNode.parentNode.getElementsByTagName('a')[0].getAttribute('href'); //gives '/remove/[loop_count]'
//	var link_array = new Array();
//	link_array = link.split('/');
//	var loop_count = link_array[2];
//	var request = getHTTPObject();
//	if (request)
//	{
//		request.onreadystatechange = function()
//		{
//			manage_checkout(request);
//		};
//		request.open('GET', '/edit/'+loop_count+'/'+quantity, true);
//		request.send(null);
//	}
//	
//	this.parentNode.getElementsByTagName('span')[1].firstChild.nodeValue = subtotal.toFixed(2);
//	document.getElementById('cart_total').firstChild.nodeValue = total.toFixed(2);
}
/*
function manage_checkout (request) //Enables/Disables #Cart's checkout button until the processing is completed.
{
	if (request.readyState == 4)
	{
		document.getElementById('checkout').onclick = function() { return true; };
	}
	else
	{
		document.getElementById('checkout').onclick = function() { return false; };
	}

}
*/


function manage_navigation ()
{
	if (!document.getElementById('SideBar')) return false;
	
	var categories = getChildListItems(document.getElementById('SideBar'));
	for (var i = 0; i < categories.length; i++)
	{
		if (categories[i].getElementsByTagName('ul').length != 0)
		{
			var arrow = document.createElement('img');
			arrow.setAttribute('class', 'navigation_arrow');
			if (categories[i].getElementsByTagName('ul').item(0).style.display != 'block')
			{
				arrow.setAttribute('src', '/images/static/arrow_collapsed.gif');
				arrow.onclick = expand_this;
			}
			else
			{
				arrow.setAttribute('src', '/images/static/arrow_expanded.gif');
				arrow.onclick = collapse_this;
			}
			categories[i].insertBefore(arrow, categories[i].firstChild);
		}
	}
}
function expand_this ()
{
	this.setAttribute('src', '/images/static/arrow_expanded.gif');
	this.onclick = collapse_this;
	
	var lists_to_expand = this.parentNode.getElementsByTagName('ul');
	for (var i = 0; i < lists_to_expand.length; i++)
	{
		lists_to_expand[i].style.display = 'block';
	}
}
function collapse_this ()
{
	this.setAttribute('src', '/images/static/arrow_collapsed.gif');
	this.onclick = expand_this;
	
	var lists_to_collapse = this.parentNode.getElementsByTagName('ul');
	for (var i = 0; i < lists_to_collapse.length; i++)
	{
		lists_to_collapse[i].style.display = 'none';
	}
}

var getChildListItems = function(node) 
{ 
    var a = []; 
    var tags = node.getElementsByTagName('li'); 
     
    for (var i = 0; i < tags.length; ++i) 
    { 
        if (node == tags[i].parentNode) 
        { 
            a.push(tags[i]); 
        } 
    } 
    return a; 
}


///// Product Photo Functions /////
function manage_photos()
{
	if (document.getElementById("PhotoGallery"))
	{
		var gallery = document.getElementById("PhotoGallery");
	}
	else
	{
		return false;
	}
	var links = gallery.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
	{
		links[i].onclick = function()
		{
			load_photo(this);
			return false;
		}
		links[i].onmouseover = function()
		{
			load_photo(this);
			return false;
		}
	}
}
function load_photo(photolink)
{
	var frame = document.getElementById("PhotoFrame");
	var source = photolink.getAttribute("href");
	frame.setAttribute("src",source);
}

function getHTTPObject()
{
	//Taken from Jeremy Keith's Bulletproof Ajax. Example can be found at http://bulletproofajax.com/code/chapter05/people/ajax.js
	var xhr = false;
	if (window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				xhr = false;
			}
		}
	}
	return xhr;
}

function addLoadEvent (func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function ()
		{
			oldonload ();
			func ();
		}
	}
}

///// Loading Functions /////
addLoadEvent (manage_navigation);
addLoadEvent (manage_photos);
addLoadEvent (manage_cart);
addLoadEvent (manage_add_to_cart);