<!--

var req;
var menuItems = new Array('1', '1D', '1F', '1C', '1V', '1W', '1T', '1B', '1S', '1R', '1A', '1G', 
								  '2', '2T', '2R', '2A', '2D', '2S', '2W', '2L', 
								  '3', '3A', '3B', 
								  '4', '4T', '4V', '4W', '4S', '4A', '4F', '4X', '4E', '4R', '4G', '4Y', '4K',
								  '5', '5H', '5B', '5T', '5A', '5S', '5E', '5R', '5Q', '5V', '5K',
								  '6', '6B', '6D', '6Z',
								  '8', '8D', '8E',
								  '9');
								  
var menuCollapsed = new Array();

/**
 * Generic function for AJAX request
 *
 * url: the php file to call
 *
 * return: nothing
 **/
function ajax(url)
{
	if(window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if(req)
	{
		req.onreadystatechange = whenReady;
		try
		{
			req.open("GET", url, true);
			req.send('');
		}
		catch(e)
		{
			alert(e);
		}
	}
	else
	{
		//no good
	}
}

function new_ajax(url)
{
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari/Chrome/IE7+
	if (window.XMLHttpRequest)
	{
		self.xmlHttpReq = new XMLHttpRequest();
		if (self.xmlHttpReq.overrideMimeType)
		{
			self.xmlHttpReq.overrideMimeType('text/xml');
			// See note below about this line
		}
	}
	// IE6
	else if (window.ActiveXObject)
	{
		try
		{
			self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) 
			{
			}
		}
	}
	
	if (!self.xmlHttpReq)
	{
		alert('ERROR AJAX:( Cannot create an XMLHTTP instance');
		return false;
	}    
	self.xmlHttpReq.open('GET', url, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() { 
			new_whenReady(self.xmlHttpReq); 
		};
	self.xmlHttpReq.send("");
}

function new_whenReady(http_request)
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			eval(http_request.responseText);
		}
		else
		{
			alert('ERROR: AJAX request status = ' + http_request.status);
		}
	}
}

/**
 * Generic function for AJAX callback
 *
 * return: nothing
 **/
function whenReady()
{
	if(req.readyState == 4)
	{
		if(req.status == 200)
		{
			eval(req.responseText);
		}
		else
		{
			alert(req.statusText);
		}
	}
}

/**
 * Pulls the images of the inventory available for the user to browse through.
 *
 * type: the type of inventory item. really the prefix to the SKUs of the inventory
 *       the user wants to browse through.  see "product#layout.xls"
 *
 * return: nothing
 **/
function grabInv(type)
{
	//TODO: get mutliple AJAX requests to work
	/*if(document.all.viewType.innerHTML == 'Basic View')
	{
		new_ajax('scripts/inv_browse.php?sku=' + type + getAdvParams());
		new_ajax('scripts/advanced_view.php?type=' + type);
	}
	else
	{
		ajax('scripts/inv_browse.php?sku=' + type);
	}*/
	
	
	if(document.getElementById('cur_type').value == type)
	{
		ajax('scripts/inv_browse.php?sku=' + type + getAdvParams());
	}
	else
	{
		ajax('scripts/inv_browse.php?sku=' + type + getAdvParams() + '&update_brands=true');
	}
	
	resetMenu();
	
	document.getElementById('inv' + type).className = 'menuItem_selected';
	document.getElementById('inv' + type).onclick = '';
	
	document.getElementById('cur_type').value = type;
}

/**
 * Pulls the images of the inventory available for the user to browse through. Used specifically for when the type 
 * of the inventory changes. The reason for this is the brands need to be updated in the advanced view menu
 *
 * type: the type of inventory item. really the prefix to the SKUs of the inventory
 *       the user wants to browse through.  see "product#layout.xls"
 *
 *
 * return: nothing
 **/
function changeType(type)
{
	document.getElementById('cur_type').value = type;
	
	ajax('scripts/inv_browse.php?sku=' + type + getAdvParams() + '&update_brands=true');
	
	resetMenu();
	
	document.getElementById('inv' + type).className = 'menuItem_selected';
	document.getElementById('inv' + type).onclick = '';
}

function grabInvIndex(type, begin)
{
	document.getElementById('cur_type').value = type;
	
	if(document.getElementById('cur_type').value == type)
	{
		ajax('scripts/inv_browse.php?sku=' + type + '&begin=' + begin + getAdvParams());
	}
	else
	{
		ajax('scripts/inv_browse.php?sku=' + type + '&begin=' + begin + getAdvParams() + '&update_brands=true');
	}
	
	resetMenu();
	
	document.getElementById('inv' + type).className = 'menuItem_selected';
	document.getElementById('inv' + type).onclick = '';
	
	
}

function getAdvParams()
{
	var keyword = document.getElementById('adv_keyword').value;
	
	var brands = new Array();
	
	if(document.getElementById('adv_brands') != null)
	{
		for(i = 0; i < document.getElementById('adv_brands').options.length; i++)
		{
			if (document.getElementById('adv_brands').options[i].selected)
			{
				brands.push(document.getElementById('adv_brands').options[i].value);
			}
		}
	}
	
	var price_min = document.getElementById('adv_min').options[document.getElementById('adv_min').selectedIndex].value;
	
	var price_max = document.getElementById('adv_max').options[document.getElementById('adv_max').selectedIndex].value;
	
	var pro = document.getElementById('adv_pro').options[document.getElementById('adv_pro').selectedIndex].value;
	
	var num_results = document.getElementById('adv_nres').options[document.getElementById('adv_nres').selectedIndex].value;
	
	var params = '';
	
	if(keyword != '')
	{
		params += '&keyword=' + keyword;
	}
	if(brands.length > 0)
	{
		params += '&brands=' + brands;
	}
	if(price_min != 'none')
	{
		params += '&price_min=' + price_min;
	}
	if(price_max != 'none')
	{
		params += '&price_max=' + price_max;
	}
	if(pro != 'none')
	{
		params += '&pro=' + pro;
	}
	
	params += '&nres=' + num_results;
	
	return params;
}

function getAdvViewParams()
{
	var keyword = document.getElementById('adv_keyword').value;
	var price_min_index = document.getElementById('adv_min').selectedIndex;
	var price_max_index = document.getElementById('adv_max').selectedIndex;
	var pro_index = document.getElementById('adv_pro').selectedIndex;
	var nres_index = document.getElementById('adv_nres').selectedIndex;
	
	return '&keyword=' + keyword + '&price_min_index=' + price_min_index + '&price_max_index=' + price_max_index + '&pro_index=' + pro_index + '&nres_index=' + nres_index;
}

function grabInvAdv()
{
	var type = document.getElementById('cur_type').value;
	if(type == '')
	{
		return;
	}
	
	ajax('scripts/inv_browse.php?sku=' + type + getAdvParams());
}

function purchase(sku, type, begin)
{
	//document.getElementById('advancedView').innerHTML = '';
	ajax('scripts/purchase_item.php?sku=' + sku + '&type=' + type + '&begin=' + begin);
}

function goToShop(type)
{
	document.shopmenu.sku.value = type;
	
	var collapsed = '';
	for(i = 1; i <= 8; (i==6) ? i=i+2 : i++)
	{
		if(menuCollapsed[i])
		{
			collapsed += '1';
		}
		else
		{
			collapsed += '0';
		}
		
		if(i < 8)
		{
			collapsed += ',';
		}
	}
	document.getElementById('collapsed').value = collapsed;
	
	document.shopmenu.submit();
}

function goToShopIndex(type, begin)
{
	document.backToBrowse.sku.value = type;
	document.backToBrowse.begin.value = begin;
	
	var collapsed = '';
	for(i = 1; i <= 8; (i==6) ? i=i+2 : i++)
	{
		if(menuCollapsed[i])
		{
			collapsed += '1'
		}
		else
		{
			collapsed += '0';
		}
		
		if(i < 8)
		{
			collapsed += ',';
		}
	}
	document.getElementById('collapsed').value = collapsed;
	
	document.backToBrowse.submit();
}

function jumpToPage(type, page)
{
	ajax('scripts/inv_browse.php?sku=' + type + '&page=' + page + getAdvParams());
}

function advancedView()
{
	document.all.viewType.innerHTML = 'Basic View';
	document.all.viewType.onclick = basicView;
	
	document.all.advancedView.className = 'advancedView';
	
	var type = document.getElementById('cur_type').value;
	if(type == '')
	{
		ajax('scripts/advanced_view.php');
	}
	else
	{
		ajax('scripts/advanced_view.php?type=' + type);
	}
}

function basicView()
{
	//document.all.advancedView.className = 'basicView';
	
	//document.getElementById('advancedView').innerHTML = '';
	
	var type = document.getElementById('cur_type').value;
	if(type == '')
	{
		return;
	}
	ajax('scripts/inv_browse.php?sku=' + type);
}

function resetMenu()
{
	for(var i in menuItems)
	{
		document.getElementById('inv' + menuItems[i]).className = 'menuItem';
		document.getElementById('inv' + menuItems[i]).onclick = new Function('changeType("' + menuItems[i] + '")');
	}
}

function collapseMenu(catagory)
{
	if(menuCollapsed[catagory])
	{
		document.getElementById('collapser' + catagory).innerHTML = '<img src="images/collapse.gif" />';
		document.getElementById('catagory' + catagory).className = 'menu';
		menuCollapsed[catagory] = false;
	}
	else
	{
		document.getElementById('collapser' + catagory).innerHTML = '<img src="images/expand.gif" />';
		document.getElementById('catagory' + catagory).className = 'menuCollapsed';
		menuCollapsed[catagory] = true;
	}
}

function expandAll()
{
	for(i = 1; i <= 8; (i==6) ? i=i+2 : i++)
	{
		document.getElementById('collapser' + i).innerHTML = '<img src="images/collapse.gif" />';
		document.getElementById('catagory' + i).className = 'menu';
		menuCollapsed[i] = false;
	}
}

function collapseAll()
{
	for(i = 1; i <= 8; (i==6) ? i=i+2 : i++)
	{
		document.getElementById('collapser' + i).innerHTML = '<img src="images/expand.gif" />';
		document.getElementById('catagory' + i).className = 'menuCollapsed';
		menuCollapsed[i] = true;
	}
}

/**
 * Returns whether cookies are enabled on the user's browser or not
 **/
/*
include("scripts/cookie_test.js");
function cookiesOn()
{
	// remember, these are the possible parameters for Set_Cookie:
	// name, value, expires, path, domain, secure
	Set_Cookie( 'prime_test', 'none', '', '/', '', '' );
	// if Get_Cookie succeeds, cookies are enabled, since
	//the cookie was successfully created.
	if (Get_Cookie('prime_test'))
	{
		document.write('cookies are currently enabled.');
		
		//this is an example of a set cookie variable, if
		//you want to use this on the page or on another script
		//instead of writing to the page you would just check that value
		//for true or false and then do what you need to do.
		
		// and these are the parameters for Delete_Cookie:
		// name, path, domain
		// make sure you use the same parameters in Set and Delete Cookie.
		Delete_Cookie('prime_test', '/', '');
		
		return true;
	}
	// if the Get_Cookie test fails, cookies
	//are not enabled for this session.
	else
	{
		return false;
	}
}
*/

-->
