function addItemToCart(itemID) {
	document.getElementById('cmd').value = "addCartItem";
	document.getElementById('itemID').value = itemID;
	document.itemForm.submit();
}

function removeItemFromCart(itemID) {
	document.getElementById('cmd').value = "removeCartItem";
	document.getElementById('itemID').value = itemID;
	document.itemForm.submit();
}

function updateQty() {
	document.getElementById('cmd').value = "updateQuantities";
	document.itemForm.submit();
}

function doSearch() {
	if (document.getElementById('srch_for').value == "") {
		
		alert('Please enter an item # to search for.\n\n(Partial item numbers are allowed)');
		return;
	} else {
		document.getElementById('cmd').value = "search";
		document.itemForm.submit();
	}
}

function itemPopup(itemCode,rand) {
	var URL = "itemPopup.php?cmd=itemPopup&item="+itemCode;
	window.open(URL,"itemPopup_"+rand,"width=400,height=400,addressbar=no,resizable=no");
}

function changeOHFilter(ddObj) {
	document.getElementById('cmd').value = "orderHistory";
	document.itemForm.submit();
}

var hoverObj;
var mouseX = 0;
var mouseY = 0;

function itemHoverPopup(linkObj,itemCode) {
	hoverObj = linkObj;
	
	mouseX = tempX;
	mouseY = tempY;
	
	hideItemPopup();

	// authenticate via AJAX
	xmlHttp = GetXmlHttpObject();
	var url = "includes/itemPopup.inc.php?inAJAX=Y&cmd=itemPopup&size=thumb&withDesc=Y&item="+itemCode;

	// make our ajax call
	xmlHttp.onreadystatechange=showItemPopup;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function showItemPopup() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		var dResponse = xmlHttp.responseText;

		document.getElementById('itemHoverPopup').innerHTML = dResponse;
		
		var eTop = findPosY(hoverObj);
		var eLeft = findPosX(hoverObj);

		var cWid = document.getElementById('itemHoverPopup').style.width;
		var cHgt = document.getElementById('itemHoverPopup').style.height;

		var newTop = eTop - (parseInt(cHgt) / 2);
		var newLeft = eLeft - (parseInt(cWid) * 1.75) - 15;
		
		document.getElementById('itemHoverPopup').style.top = newTop+"px";
		document.getElementById('itemHoverPopup').style.left = newLeft+"px";
		document.getElementById('itemHoverPopup').style.display = "block";

		var tLeft = (newLeft + parseInt(cWid) + 20);
		var tTop = eTop - 8;
		
		document.getElementById('hoverTip').style.top = tTop+"px";
		document.getElementById('hoverTip').style.left = tLeft+"px";
		
		document.getElementById('hoverTip').style.display = "block";
	} 
}

function hideItemPopup() {
	document.getElementById('itemHoverPopup').style.display = "none";
	document.getElementById('itemHoverPopup').innerHTML = "";
	document.getElementById('hoverTip').style.display = "none";
}

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
	if (IE) { // grab the x-y pos.s if browser is IE
	  tempX = event.clientX + document.body.scrollLeft
	  tempY = event.clientY + document.body.scrollTop
	} else {  // grab the x-y pos.s if browser is NS
	  tempX = e.pageX
	  tempY = e.pageY
	}  
	
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
	
	return true;
}

function findPosX(obj) {
	var curleft = 0;
	if(obj.offsetParent)
		while(1) {
			curleft += obj.offsetLeft;
			if (!obj.offsetParent)
				break;
          	obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function changeHistItems(selObj) {
	var newCount = selObj.options[selObj.selectedIndex].value;
	
	var url = "includes/browse.inc.php?cmd=updateHistItems&numItems="+newCount;
	doAJAXRequest(url);
}

function checkEnter(e) {
	var evt = window.event ? event : e;
	if (!evt) { return; }
	
	if (evt.keyCode == 13) { doSearch(); }
}
