//---------------------------------------------
// DO WHEN PAGE IS LOADED
//---------------------------------------------

// Assign the Articles menu

	setMenuLocation();

// Show Checkout Bar if there are items in the cart

	if (document.location.href.indexOf("cart.cgi")== -1) {
		cartitems=readCookie('cartitems');
		if (cartitems>0) {
			document.getElementById('checkoutBar').style.display='block';
			document.getElementById('toprule').style.display='none';
		}
	}

//---------------------------------------------
// SET AFFILIATE COOKIE (doing this w/CGI instead)
//---------------------------------------------

//	$slicedArray = window.location.split('?');
//	if ($slicedArray[1]) {
//		document.cookie = 'NMSaffiliate = ' + $slicedArray[1]
//	}

//---------------------------------------------
// SHOPPING CART
//---------------------------------------------
function addToCart(price,product) {
	if (document.location.href.indexOf('cart.cgi')==-1) document.cookie="returnpage="+document.location+"; path=/";

	document.orderform.price.value  = price;
	document.orderform.product.value= product;

	cartitems = readCookie('cartitems');
	cartitems++;
	document.cookie = "cartitems="+cartitems+"; path=/";
	document.cookie = "product"+cartitems+"="+price+"---"+escape(product)+"; path=/";

	if (product.indexOf('Store Credit')>-1) { window.location='cart.cgi?add';}
	else {
		// messages=["Excellent choice! That item is now in your cart, O shopping wizard.",
		// "In accordance with thy will, said item is added to your cauldron. So Mote It Be!"];
		// randomNumber = Math.floor(Math.random()*2);
		//alert(messages[randomNumber]);
		x = document.body.scrollWidth / 2 - 300;
		y = document.body.scrollTop +100;
	//alert(x+" "+y);
	//	x=50;
	//	y=50;
		theObj = document.getElementById('addedToCartBox').style;
		theObj.left=x+'px';
		theObj.top=y+'px';
		theObj.visibility="visible";
		//document.getElementById('checkoutButton').style.display="block";
		//document.orderform.submit();
	}
}

//---------------------------------------------
// CATEGORY ROLLOVERS
//---------------------------------------------
function hover(state,category) {
	iconCell =document.getElementById(category).style;
	labelCell=document.getElementById(category+'Label').style;
	link	 =document.getElementById(category+'Link').style;

	if (category.indexOf('nav')==0) { isSidebar=1; } else {isSidebar=0;}

	if (state=='over') { borderColor='red'; linkColor='red'; cursor='pointer'; }
	else { borderColor='black'; linkColor='#9f9'; cursor='default';}
	
	iconCell.borderLeft	 = '1px solid '+borderColor;
	iconCell.borderTop		= '1px solid '+borderColor;

	labelCell.borderBottom= '1px solid '+borderColor;
	labelCell.borderRight = '1px solid '+borderColor;

	if (isSidebar==1) {	
		iconCell.borderBottom	= '1px solid '+borderColor;
		labelCell.borderTop		= '1px solid '+borderColor;
	} else {
		iconCell.borderRight	= '1px solid '+borderColor;
		labelCell.borderLeft	= '1px solid '+borderColor;
	}

	link.color = linkColor;iconCell.cursor=cursor;
	labelCell.cursor=cursor;
}


//---------------------------------------------
// TOP MENU ROLLOVERS
//---------------------------------------------
function menu(state,whichMenu) {
	if (state=='on') { cellColor='maroon'; textColor='yellow'; cursorStyle='pointer';}
	else {cellColor='navy'; textColor='lightgreen'; cursorStyle='default';}

	cell=document.getElementById(whichMenu).style;
	cell.backgroundColor = cellColor;
	cell.cursor = cursorStyle;
	
	link=document.getElementById(whichMenu+'Text').style;
	link.color = textColor;
	
}

//---------------------------------------------
// ARTICLE MENU
//---------------------------------------------
function setMenuLocation() {
	menuObj = document.getElementById('articles');
	submenuObj=document.getElementById('articlemenu');
	submenuObj.style.top = findTop(menuObj)+15+'px';
	submenuObj.style.left = findLeft(menuObj)+'px';
}

function showMenu() {
	submenuObj=document.getElementById('articlemenu');
	submenuObj.style.visibility = 'visible';
}

document.onclick=hideAllMenus;

function hideAllMenus() {
	document.getElementById('articlemenu').style.visibility='hidden';
}

//---------------------------------------------
// COOKIE-IZE THE REFERER PAGE
//---------------------------------------------

getRefererPage();
function getRefererPage() {
	if (readCookie('refpage') !=null) {return;} // exit if there's already a referer

	refpage = document.referrer;
	pattern = /^http:\/\/naturalmagick/;
	if (!pattern.test(refpage) && refpage !='') {
		refpage = refpage.replace(/&/g, "_Amp_"); // Ampersands get lost somewhere along the way if I don't do this; The & is recorded correctly in Safari without this, though.
		refpage = refpage.replace(/;/g, "_SemiC_");
		refpage = refpage.replace(/,/g, "_Comma_");
		refpage = refpage.replace(/ /g, "_%20_");
		document.cookie="refpage="+refpage;
	}
}

function readCookie(name) {
	nameEQ = name + "=";
	cookieArray = document.cookie.split(';');
	for(var i=0;i < cookieArray.length;i++) {
		cookie = cookieArray[i];
		while (cookie.charAt(0)==' ') cookie = cookie.substring(1,cookie.length);
		if (cookie.indexOf(nameEQ) == 0) return cookie.substring(nameEQ.length,cookie.length);
	}
	return null;
}

//----------------------------------------------------------------
function findLeft(obj) {
//----------------------------------------------------------------
	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 findTop(obj) {
//----------------------------------------------------------------
	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;
}


