﻿/* requires conduit.js */

function window_onload()
{
	_setExtUrl(true);
	_setClearDefaults(true, false);
	_setRollOvers(true);
	if($B('msie')) // IE  only
	{
	
	}
}
window.onload = window_onload;

function window_onunload()
{
	// unset event handlers (fixes browser memory leaks)
	_setExtUrl(false);
	_setClearDefaults(false, false);
	_setRollOvers(false);
	setCategoryNav(false);
	if($B('msie')) // IE  only
	{
		_setHovers(false, 'nav', 'td');
	}
}
window.onunload = window_onunload;

/*
	activate mouseovers (for ie) and onclick for nav items.
	because nav items are table cells, and due to cross-browser css issues,
	we need to make the whole td act as though it is a link
	there is a limitation in that onclick's will not work correctly, 
	so if the link. has an onclick, nothing will happen when clicking on the td.
*/
function setNav(on)
{
	var nav = $('nav');
	if(!nav)
		return;

	if($B('msie'))
		_setHovers(on, nav, 'td');

	var tds = nav.getElementsByTagName('TD');
	if(tds)
	{
		for(var i = 0; i < tds.length; i++)
		{
			if(tds[i].className.indexOf('nolink') == -1)
			{
				if(on)
				{
					tds[i].onclick = function()
					{
						var child = this.firstChild;
						if(child.tagName.toUpperCase() == 'A')
						{
							if(!child.onclick)
								document.location = child.href;							
						}
					}
				}
				else
					td.onclick = null;
			}
		}
	}
}

var cnVisibleList = null;
var cnLinks;
function setCategoryNav(on)
{
	el = $('categorytree');
	if(!el)
		return;
	
	if(!cnLinks)
		cnLinks = new Array(document.location.href);
		
	for(var i = 0; i < cnLinks.length; i++)
		cnLinks[i] = cnLinks[i].replace(/&Page=[^&]+/i, ''); // remove page number from querystring of links to match

	cnVisibleList = null;
	var els = el.getElementsByTagName('A');
	// set visible node
	for(var i = 0; i < els.length; i++)
	{
		if(on)
		{
			for(var j = 0; j < cnLinks.length; j++)
			{
				if(cnLinks[j] == els[i].href)
				{
					// set this node visible
					if(cnVisibleList)
						cnVisibleList.className = '';
					if(els[i].parentNode.getElementsByTagName('UL').length > 0)
						cnVisibleList = els[i].parentNode.getElementsByTagName('UL')[0];
					else
						cnVisibleList = els[i].parentNode.parentNode;
					cnVisibleList.className = 'visible';
				}
			}
		}
	}
	for(var i = 0; i < els.length; i++)
	{
		if(els[i].parentNode.getElementsByTagName('UL').length > 0)
		{
			if(on)
			{
				els[i].onclick = function()
				{
					var list = this.parentNode.getElementsByTagName('UL')[0];
					if(cnVisibleList)
						cnVisibleList.className = '';
					if(list != cnVisibleList)
					{
						list.className = 'visible';
						cnVisibleList = list;
					}
					else
						cnVisibleList = null;
					return false;
				}
			}
			else
				els[i].onclick = null;
		}
	}
}

function ajaxCatNavUpdate(caller, listby)
{
	if(ajaxer.CanMakeRequests)
	{
		setCategoryNav(false);
		var url = '/functions/categorylist/ajax.get.asp?ListBy=' + listby;
		ajaxer.UpdateElement(
			url, 
			$('categorynav'), 
			function() { setCategoryNav(true); }, 
			$EU(caller)
		);
		return false;
	}
	return true;
}

function logResolution()
{
	// check that we can get screen resolution
	var width, height;
	if(window.screen)
	{
		width = window.screen.width;
		height = window.screen.height;
	}
	else if(screen) // older browsers
	{
		width = screen.width;
		height = screen.height;
	}
	if(width > 0 && height > 0)
	{
		var url = '/functions/user/logresolution.asp?Width=' + width + '&Height=' + height;
		var e = document.createElement('script');
		e.type = 'text/javascript';
		e.src = url;
		$N('body').appendChild(e);
	}
}

function ajaxCartAdd(caller, itemid)
{
	if(ajaxer.CanMakeRequests)
	{
		var url = '/functions/cart/ajax.add.asp?ProductItemID=' + itemid + '&ReturnURL=' + document.location.href;
		ajaxer.UpdateElement(
			url, 
			$('cart'), 
			null, 
			$EU(caller)
		);
		return false;
	}
	return true;
}
function ajaxCartRemove(caller, itemid, quantity)
{
	if(ajaxer.CanMakeRequests)
	{
		var url = '/functions/cart/ajax.remove.asp?ProductItemID=' + itemid + (quantity ? '&Quantity=' + quantity : '') + '';
		ajaxer.UpdateElement(
			url, 
			$('cart'), 
			null, 
			$EU(caller)
		);
		return false;
	}
	return true;
}
function ajaxCartEmpty(caller)
{
	if(ajaxer.CanMakeRequests)
	{
		var url = '/functions/cart/ajax.empty.asp?ReturnURL=' + document.location.href;
		ajaxer.UpdateElement(
			url, 
			$('cart'), 
			null, 
			$EU(caller)
		);
		return false;
	}
	return true;
}
function ajaxCartRefresh()
{
	if(ajaxer.CanMakeRequests && $('cart'))
	{
		_setRollOvers(false);
		var url = '/functions/cart/ajax.get.asp?ReturnURL=' + document.location.href;
		ajaxer.UpdateElement(
			url, 
			$('cart'),
			function() { _setRollOvers(true); }
		);
		return false;
	}
	return true;
}

function checkUnitPrice(itemid)
{
	var txtUnitPrice = $('txtUnitPrice' + itemid);
	if(txtUnitPrice)
	{
		if(isNaN(txtUnitPrice.value) || parseFloat(txtUnitPrice.value) < 0 )
		{	
			alert('Unit Price must be numeric and non-negative');
			if(txtUnitPrice.defaultValue)
				txtUnitPrice.value = txtUnitPrice.defaultValue;
			return false;
		}
		else
		{
			unitprice = parseFloat(txtUnitPrice.value);
			txtUnitPrice.value = numberFormat(unitprice, 2, false);
			setSubtotal(itemid, unitprice);
		}
	}
	return true;
}

function setSubtotal(itemid, price)
{
	var txtQuantity = $('txtQuantity' + itemid);
	var tdSubtotal = $('tdSubtotal' + itemid);
	if(txtQuantity && tdSubtotal)
	{
		if(isNaN(price))
			price = $V('txtUnitPrice' + itemid);
		
		if(isNaN(txtQuantity.value))
		{
			alert('Quantity must be numeric');
			if(txtQuantity.defaultValue)
				txtQuantity.value = txtQuantity.defaultValue;
			return false;
		}
		else if(!isNaN(price))
		{
			if(parseInt(txtQuantity.value) < 0)
				txtQuantity.value = 0;
			tdSubtotal.innerHTML = '$' + numberFormat(parseInt(txtQuantity.value) * price, 2, true);
			
			var tdTotal = $('tdTotal');
			if(tdTotal)
			{
				// update total
				var tds = $N('TD');
				var total = 0.0;
				for(var i = 0; i < tds.length; i++)
				{
					if(tds[i].id && tds[i].id.indexOf('tdSubtotal') > -1)
					{
						var subtotal = tds[i].innerHTML.replace(/[$,]/g, '');
						if(!isNaN(subtotal))
							total = total + parseFloat(subtotal);
					}
				}
				tdTotal.innerHTML = '$' + numberFormat(total, 2, true);
			}
		}		
	}
	return true;
}

function validateSearch()
{
	var txtKeywords = $('txtKeywords');
	if(txtKeywords)
	{
		if(!txtKeywords.value || txtKeywords.value == txtKeywords.defaultValue)
		{
			$F(txtKeywords);
			return false;
		}
	}
	return true;
}

function guestAddToCart()
{
	var answer = confirm("To purchase this product you must login,\n or register if you are a new user.");

	if (answer == true)
	{
		window.location="/studentit/login.asp";
	}
}

function guestPrefProd()
{
	var answer = confirm("To add to your preferred products you must login,\n or register if you are a new user.");

	if (answer == true)
	{
		window.location="/studentit/login.asp";
	}
}


