//************************************COPYRIGHT BostonGreenGoods.com - All rights reserved. ****************************************************
//************************************************************************************************************************************************************
// Globals - TOP  
//************************************************************************************************************************************************************
// Screen padding from edges
var offsetPad = 10;

// Mouse position globals
var mouseX = 0;
var mouseY = 0; 

// Popup image opening flags
var popUpPopping= false;

// Image and containing Div
var popupImage = null;
var popupDiv = null;

// Floating SKU popup
var hoverPopupDiv = null;
var currentPopupElemId = "";
var checkForRemoval = null;
var removalPad = 10;
var launchMouseX = 0;
var launchMouseY = 0;
var  floatLeftOffset = 140;
//var  floatHoverWidth = 650;
var verticalOffset = 15;
var allowHoverPopupClose = true;
//var dropListActive = false;
var mouseHasExited = false;
var mouseFirstEntry = false;

// Any currently open popup image
var currentPopup = null;

var IE = document.all?true:false

//************************************************************************************************************************************
// GLOBAL vars to support hovers
//************************************************************************************************************************************
var hoverLaunchTimeout = null;
var hoverLaunchDelay = 300;

var navHoverTimeout = null;
var navHoverDelay = 2000;
var navHoverDelayExit = 250;

var NAVLEFTOFFSET = 125;
var NAVVERTOFFSET = -20;

var SKULEFTOFFSET = 160;
var SKUVERTOFFSET =10;
var SKURIGHTCOLLEFTOFFSET = 280;

var okToShowBuyPanel = true;
var buyHoverLaunchDelay = 500;
var buyHoverDelayExit = 1000;

var GRIDLEFTOFFSET = 115;
var GRIDVERTOFFSET = 30;


// 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 = locateMouse;
enableMouseTracking();


//************************************************************************************************************************************************************
function floatPromoInfo(hoverSpan) {

var imageURL = hoverSpan.getAttribute("floatImage");

startHoverLaunchTimeout(hoverLaunchDelay, "PROMO", imageURL)
}

//************************************************************************************************************************************************************
function delayedFloatPromoInfo(imageURL) {

var promoImage = document.createElement("img");
promoImage.src = imageURL;
var promoDiv = document.createElement("div");
promoDiv.appendChild(promoImage);

	floatElement(null, "popupSKUDiv", "popupSKUTable", 655, promoDiv, true, true); //promoImage.width
}

//************************************************************************************************************************************************************
function showPromoInfo(event) {

mouseX = mouseX - (popupImage.width / 2);
mouseY = mouseY + verticalOffset;
reallyPopFrameless(popupImage.width, popupImage.height)
}

//************************************************************************************************************************************************************
function unFloatPromoInfo() {

endHoverLaunchTimeout();
startHoverTimeout(navHoverDelayExit);
}

//************************************************************************************************************************************************************
function goFetch() {
setTimeout("goFetchDelayed()",1000);
}
//************************************************************************************************************************************************************
function goFetchDelayed() {

var fetchpix = document.getElementById("goFetch");

if(fetchpix)
	{
	fetchpix.src= "http://pixel.fetchback.com/serve/fb/pdc?cat=&name=landing&sid=1319";
	}
}

//************************************************************************************************************************************************************
function loadCommonUtils() {
	document.write('<script type="text/javascript" src="http://www.bostongreengoods.com/includes/utils.js"></script>'); 
}

loadCommonUtils();

//************************************************************************************************************************************************************
function navigationInvoked(link) {
	bgg_navigationInvoked(link);
}

//************************************************************************************************************************************************************
function loadPresentation() {
if(location.href.indexOf("allergybuyersclub") > -1)
	{
	document.write('<script type="text/javascript" src="http://www.allergybuyersclub.com/includes/presentation.js"></script>'); 
	}
else if(location.href.indexOf("sleepbuyersclub") > -1)
	{
	document.write('<script type="text/javascript" src="http://www.sleepbuyersclub.com/includes/presentation.js"></script>'); 
	}
}

loadPresentation();

//**********************************************************************************************************************************************
// Ensure customers see the item represented by the link they click on
//**********************************************************************************************************************************************
// Hook up the plumbing
//**********************************************************************************************************************************************
function assignItemOnClicks() {

var links = document.getElementsByTagName('a');

for (var index = 0; index < links.length; index++ )
	{
	if(links[index].className == "clickLink") 
		{
		var anchor = links[index];
		anchor.onclick= function() {bgg_addQSDecorations(this); appendItemId(this);};
		}
	}
}

//**********************************************************************************************************************************************
function assignPromoInfoFloats() {

var spans = document.getElementsByTagName('span');

for (var index = 0; index < spans.length; index++ )
	{
	if(spans[index].className == "promoInfoHover") 
		{
		var span = spans[index];
		span.onclick = function() {floatPromoInfo(this);};
		span.onmouseover = function() {floatPromoInfo(this);};
		span.onmouseout = function() {unFloatPromoInfo();};
		}
	}
}

//****************************************************************************************************
// Assign the correct identifier for the item at the other end of the link
//****************************************************************************************************
function appendItemId(link) {

bgg_decorateQS(link, "itemId", link.id);
location = link.href;
}


//************************************************************************************************************************************************************
function popUpCartHelp(url) {
var popWindow = window.open(url,'pop','width=600,height=550,top=150,left=200,scrollbars');
popWindow.focus();
}

//************************************************************************************************************************************************************
// Div swapping for landing pages
//************************************************************************************************************************************************************
debug = 0;
function moveLPSections(secId)
{
	moveCommonSections(secId, "LANDING");
	return false;
}

//************************************************************************************************************************************************************
// Div swapping for product pages
//************************************************************************************************************************************************************
debug = 0;
function moveSections(secId)
{
	moveCommonSections(secId, "PRODUCT");
	return false;
}

//************************************************************************************************************************************************************
// Changes the order of divs within the parent, 'contentarea', div.
// Div with ID divid goes to the top of the set with all others shifted down.
//************************************************************************************************************************************************************
debug = 0;
function moveCommonSections(secId, context)
{
var buyAndTop = null;
var buyOnly = null;
var topOnly = null;

var sectionKey = "sect-" + secId;
var section = document.getElementById(sectionKey);
var cont = section.parentNode;

if(debug > 0) {alert("moveDivs() moving " + secId + " that resolved to " + cont.getAttribute('id'));}

var newContainer = document.createElement('table');

if(debug > 0) {alert("moveDivs() appending first div " + div.getAttribute("id")  + " that resolved to " + cont.getAttribute('id'));}

var cullingClone = cont.cloneNode(true);

while(cullingClone.firstChild)
	{
//alert(cullingClone.firstChild.id);
	if((buyAndTop == null) && (cullingClone.firstChild.id == "buyAndTop"))
		{
		buyAndTop = cullingClone.firstChild.cloneNode(true);
		}

	if((buyOnly == null) && (cullingClone.firstChild.id == "buyOnly"))
		{
		buyOnly = cullingClone.firstChild.cloneNode(true);
		}

	if((topOnly == null) && (cullingClone.firstChild.id == "topOnly"))
		{
		topOnly = cullingClone.firstChild.cloneNode(true);
		}
	cullingClone.firstChild.parentNode.removeChild(cullingClone.firstChild);
	}

if(buyOnly != null)
	{
	newContainer.appendChild(buyOnly.cloneNode(true));
	}

newContainer.appendChild(section);

while(cont.firstChild)
	{
	if((cont.firstChild.id != "buyAndTop") && (cont.firstChild.id != "topOnly") && (cont.firstChild.id != "buyOnly"))
		{
		if(context == "PRODUCT")
			{
			if((cont.firstChild.id) && (buyAndTop != null))
				{
				newContainer.appendChild(buyAndTop.cloneNode(true));
				}
			}

		if(context == "LANDING")
			{
			if((cont.firstChild.id) && (topOnly != null))
				{
				newContainer.appendChild(topOnly.cloneNode(true));
				}
			}


		newContainer.appendChild(cont.firstChild);
		}
	else
		{
		cont.firstChild.parentNode.removeChild(cont.firstChild);
		}
	}

if(topOnly != null)
	{
	newContainer.appendChild(topOnly.cloneNode(true));
	}

while(cont.lastChild)
	{
	cont.lastChild.parentNode.removeChild(cont.lastChild);
	}

while(newContainer.firstChild)
	{
	cont.appendChild(newContainer.firstChild);
	}

labelMenuTab(secId);

return false; // To suppress natural jump function to internal anchor.
}

//************************************************************************************************************************************************************
// Changes the classname of the tab selected to shift a div to the top.
// Class name goes from "menuTab" to "selectedMenuTab."  All other tabs are set to "menuTab".
//************************************************************************************************************************************************************
function labelMenuTab(secId) {

var selectedTabId = "tab-" + secId;
//alert("selectedTabId " + selectedTabId);

var menu = document.getElementById('product_info_tabs');

var children = menu.childNodes;
//alert(children.length + " children");

for(var index = 0; index < children.length; index++)
	{
	if (("" == children[index].className) || ("on" == children[index].className))
		{
		if(children[index].id == selectedTabId)
			{
			children[index].className="on";
			}
		else
			{
			children[index].className="";
			}
		}
	}
	return false;
}



//************************************************************************************************************************
//************************************************************************************************************************
// Present the product linked to from a landing page.
//************************************************************************************************************************
//************************************************************************************************************************
// Setup the page with the correct item showing at the top, correct breadcrumb trail, etc.
//************************************************************************************************************************
function setupPage() {

	doSuperTabs();
	selectLinkedProduct() ;
	bgg_manageOffers();
	bgg_setupReviewContext("ABC");
	assignItemOnClicks();
	assignPromoInfoFloats();
	bgg_detectBrowser();
	putUpBreadcrumbs();
	putUpCallMessage();
	goFetch();
}

//************************************************************************************************************************
function doSuperTabs() {
var superTabs = document.getElementById("super_header_tabs");
if(superTabs)
	{
	setSuperHeaderTab();
	}

}

//************************************************************************************************************************
function putUpCallMessage() {

var callLandLocation = document.getElementById('callSalesLand');
var callProdLocation = document.getElementById('callSalesProd');

var CALL_LAND_MESSAGE = "Helpful Product Specialists Available Toll-Free 1-888-236-7231 - Call Now!";
var CALL_PROD_MESSAGE = "Helpful Product Specialists Available Toll-Free 1-888-236-7231";

if(callLandLocation != null)
	{
	if(CALL_LAND_MESSAGE != "")
		{
		callLandLocation.innerHTML = CALL_LAND_MESSAGE;
		}
	else
		{
		callLandLocation.parentNode.removeChild(callLandLocation);

		var landPost = document.getElementById('callSalesLandPost');
		if(landPost)
			{
			landPost.parentNode.removeChild(landPost);
			}
		}
	}

if(callProdLocation != null)
	{
	callProdLocation.innerHTML = CALL_PROD_MESSAGE;
	}
}

//************************************************************************************************************************
function putUpBreadcrumbs() {

var trailLocation = document.getElementById('breadcrumbTrail');

if((trailLocation != null) && (categoryName != null) && (pageTitle != null) && (categoryPageURL != null))
	{
	trailLocation.innerHTML = buildBreadcrumbsReally(true);
	}
}

//************************************************************************************************************************
function buildBreadcrumbsReally(showLinks) {

var trail = "";


if(location.href.indexOf("allergybuyersclub") > -1) 
	{
	if(showLinks)
		{
		trail = "<a href='http://www.allergybuyersclub.com'>";
		}
	trail = trail + "AllergyBuyersClub.com";
	};
if(location.href.indexOf("sleepbuyersclub") > -1) 
	{
	if(showLinks)
		{
		trail = "<a href='http://www.sleepbuyersclub.com'>";
		}
	trail = trail + "SleepBuyersClub.com";
	};

if(showLinks)
	{
	trail = trail + "</a>&nbsp;<img src='http://images.allergybuyersclub.com/img/icons/arrow-right.gif'>&nbsp;";
	}
else
	{
	trail = trail + " > ";
	}

if((!(window.categoryPageURL  === undefined)) && (categoryPageURL  != "http://$page.topCategoryWebpageGridVO.domainVO.url/$page.topCategoryWebpageGridVO.staticUrl"))
	{
	if(showLinks)
		{
		trail  = trail  + "<a href='" + categoryPageURL + "'>";
		}

	trail = trail + categoryName;

	if(showLinks)
		{
		trail = trail + "</a>&nbsp;<img src='http://images.allergybuyersclub.com/img/icons/arrow-right.gif'>&nbsp;";
		}
	else
		{
		trail = trail + " > ";
		}
	}

if(!(window.pageTitle  === undefined))
	{
	trail  = trail  + shortenIt(pageTitle, MAXLENGTH);
	}

return trail;
}

var MAXLENGTH = 80;

//************************************************************************************************************************
function shortenIt(longString, maxLength) {

var stringSegs = longString.split(" ");
var shortString = "";
var topIndex = 0;

for (var index = 0; (index < stringSegs.length) && (shortString.length < maxLength); index++ )
	{
	shortString += stringSegs[index];
	if(shortString.length < maxLength)
		{
		shortString += " ";
		}
	topIndex++;
	}

if((shortString.length >= maxLength) && (topIndex < stringSegs.length))
	{
	shortString += "...";
	}

return shortString;
}

//************************************************************************************************************************
// Get the item id that was passed in and swap that item to the page top.
//************************************************************************************************************************
function selectLinkedProduct() {

var itemId =  bgg_parseArg('itemId');
if(itemId)
	{
	swapItemIn(itemId)
	}

bgg_setupReviews();
}


//************************************************************************************************************************
// Get the item id that was passed in and swap that item to the page top.
//************************************************************************************************************************
function swapItemIn(itemId) {

var topSpot = document.getElementById('mc-topspot');
var targetItemData = document.getElementById('mc-' + itemId);

if((topSpot != null) && (targetItemData != null))
	{
	swapTopItem(topSpot, targetItemData);
	}
}

//************************************************************************************************************************
function swapTopItem(topSpot, targetItemData) {

while(topSpot.lastChild)
	{
	topSpot.lastChild.parentNode.removeChild(topSpot.lastChild);
	}

var nodeClone = targetItemData.cloneNode(true);

var shipImageID  = "shipping-" + nodeClone.id;

var children = nodeClone.childNodes;
for(var index = 0; index < children.length; index++)
	{
	if(children[index].id == shipImageID  )
		{
		children[index].innerHTML = "<br /><img src='http://images.allergybuyersclub.com/img/iqair-shipping.gif'/>";
		break;
		}
	}

while(nodeClone.firstChild)
	{
	topSpot.appendChild(nodeClone.firstChild);
	}

bgg_updateReviewElements(topSpot);
}

//************************************************************************************************************************************************************
function enableMouseTracking() {
	document.onmousemove = locateMouse;
}

//************************************************************************************************************************************
// Trigger the float.  Given timing delays of AJAX, use okToShowBuyPanel semaphore to ground process, if needed
//************************************************************************************************************************************
function delayedFloatBuyPanel(pageName, SKU, productId, column) {

if((location.href.indexOf(".com/vacuumcleaners.html") > -1) || (location.href.indexOf(".com/dehumidifiers-home-industrial.html") > -1))
	{
	okToShowBuyPanel = true;
	var commandString = "populateBuyPanel('" + pageName + "','" + SKU + "'," + productId + "," + column + ")";
	hoverLaunchTimeout = setTimeout(commandString,buyHoverLaunchDelay);
	}
}

//************************************************************************************************************************************
// Invoke AJAX call to collect data from related product page.  
//************************************************************************************************************************************
function populateBuyPanel(pageName, SKU, productId, column) {

var ajaxDomain = "www.allergybuyersclub.com/skus";

if(location.href.indexOf('shopping') > -1)
	{
	ajaxDomain = "www.allergybuyersclubshopping.com";
	}

var pageUrl = "http://" + ajaxDomain + "/" + pageName;

var request = getHTTPObject();

if(request && pageName && okToShowBuyPanel)  // If okToShowBuyPanel not set, don't bother loading content
	{
	var pageURLwithItemId = pageUrl + "?itemId=" + productId;
	request.onreadystatechange = function() {setupBuyPanel(request, pageURLwithItemId, SKU, column);}
	request.open("GET", pageUrl, true);
	request.send(null);
	}
}

//************************************************************************************************************************************
// Collect content from AJAX return and structure the elements of the floating div.
//************************************************************************************************************************************
function setupBuyPanel(request, pageUrl, SKU, column) {

var HOVERINFOCAMPAIGN = "landpagehovermoreinfo";
var HOVERBUYCAMPAIGN = "landpagehoverbuy";

if(request.readyState ==4)
	{
	try
		{
		if((request.status ==200 || request.status ==304) && okToShowBuyPanel)  // If okToShowBuyPanel not set, don't bother loading content
			{
			var skuArea = request.responseText.split("<!-- CLIP " + SKU + " HERE -->")[1];
			skuArea = skuArea.replace(/\n/g,'').replace(/\t/g,'').replace(/\r/g,'');

			var nameSkuAndPrice = skuArea.split("<!-- CLIP NAMEANDPRICE HERE -->")[1];
			var description = skuArea.split("<!-- CLIP DESCRIPTION HERE -->")[1];		    				
			var buyButton = skuArea.split("<!-- CLIP BUYBUTTON HERE -->")[1];


			var promoNote = skuArea.split("<!-- CLIP PROMONOTE HERE -->")[1];
			if(!promoNote)
				{
				promoNote = "&nbsp;";
				}
			else
				{
				promoNote = "<tr><td colspan='3'>" + promoNote + "</td></tr>";
				}

			var shipStatement = skuArea.split("<!-- CLIP FREESHIPPING HERE -->")[1];
			if((!shipStatement) || (shipStatement.indexOf('shipping-costs.gif') > -1))
				{
				shipStatement = "&nbsp;";
				}

			var tempDiv = document.createElement('div');
			var content = "<table width='100%' cellspacing='0' cellpadding='0'>";
			content = content + "<tr>" + nameSkuAndPrice + "</tr>";
			content = content + "<tr><td width='100%' colspan='2'>" + description + "</td></tr><tr><td colspan='3'><hr size='1' width=''98%'></td></tr>";
			content = content + "<tr><table width='100%'><tr>";
			content = content +  "<td align='left' width='50%' valign='middle'><a href=\"" + bgg_appendQSArg(pageUrl, 'ICID', HOVERINFOCAMPAIGN) + "\" ";
			content = content + "onclick=\"bgg_navigationInvoked();\"><img border='0' src='http://images.allergybuyersclub.com/img/more-info2.gif'></a></td>";

			// Add the ICID to the form action
			var buttonPortions = buyButton.split("action=\"");
			var buttonPreamble = buttonPortions[0] + "action=\"";
			var buttonSubPortions = buttonPortions[1].split(".mvc");
			var buttonAction = bgg_appendQSArg(buttonSubPortions[0] + ".mvc", 'ICID', HOVERBUYCAMPAIGN);
			var buttonEpilogue = buttonSubPortions[1];
			var buttonReconstructed = buttonPreamble + buttonAction + buttonEpilogue;

			content = content + "<td td align='right' width='50%' valign='middle'>" + buttonReconstructed + "</td></tr></table></td></tr></table>";
			tempDiv.innerHTML = content;
			floatBuyPanel(tempDiv, column, SKU);
			}
		}
	catch(e)
		{
		//alert(e);
		// No op.  Likely the customer switched off of the page before the XML content loaded.
		}
	}
}

//************************************************************************************************************************************
// Put up the sstructured panel - but only if the okToShowBuyPanel flag is still set
//************************************************************************************************************************************
function floatBuyPanel(tempDiv, column, SKU) {

if(okToShowBuyPanel)
	{
	var columnOffset = (600 / cellsPerRow);
	floatLeftOffset = GRIDLEFTOFFSET + ((column  - 1) * columnOffset) ;
	verticalOffset = GRIDVERTOFFSET;
	floatElement(SKU, "popupSKUDiv", "popupSKUTable", 300, tempDiv, false, false);
	}
}

//************************************************************************************************************************************
// Take down a floated buy panel
//************************************************************************************************************************************
function unFloatBuyPanel() {

okToShowBuyPanel = false;
endHoverLaunchTimeout();
mouseHasExited = false;
mouseFirstEntry = true;
}
//************************************************************************************************************************************************************
function startHoverTimeout(delayTime)
{
	if(typeof navHoverTimeout != "undefined")
		endHoverTimeout();

	if(allowHoverPopupClose == true)
		navHoverTimeout = setTimeout("removeHoverPopup()",delayTime);
}

//************************************************************************************************************************************************************
function endHoverTimeout()
{
	clearTimeout(navHoverTimeout);
	navHoverTimeout = null;
}

//************************************************************************************************************************************************************
function startHoverLaunchTimeout(delayTime, popupType, navID)
{

	if(typeof hoverLaunchTimeout != "undefined") { endHoverLaunchTimeout();}

	if(popupType == "NAV")
	{
		floatLeftOffset = NAVLEFTOFFSET ;
		verticalOffset = NAVVERTOFFSET;

		var commandString = "delayedFloatNav('" + navID + "')";
		hoverLaunchTimeout = setTimeout(commandString,delayTime);
	}

	else if((popupType == "SKU") || (popupType == "SKURIGHT"))
	{
		verticalOffset = SKUVERTOFFSET;

		if(popupType == "SKU")
		{
			floatLeftOffset = SKULEFTOFFSET;
		}
		else
		{
			floatLeftOffset = SKURIGHTCOLLEFTOFFSET;
		}

		var commandString = "delayedFloatSKU('" + navID + "')";
		hoverLaunchTimeout = setTimeout(commandString,delayTime);
	}

	else if(popupType == "PROMO")
	{
		verticalOffset = SKUVERTOFFSET;
		floatLeftOffset = SKULEFTOFFSET;

		var commandString = "delayedFloatPromoInfo('" + navID + "')";
		hoverLaunchTimeout = setTimeout(commandString,delayTime);
	}
}


//************************************************************************************************************************************************************
function endHoverLaunchTimeout()
{
	clearTimeout(hoverLaunchTimeout);
	hoverLaunchTimeout = null;
}

//************************************************************************************************************************************************************
function floatNav(navID) {

	startHoverLaunchTimeout(hoverLaunchDelay, "NAV", navID);
	
}
//************************************************************************************************************************************************************
function floatNavNOW(navID) {

	delayedFloatNav(navID);
}

//************************************************************************************************************************************************************
function delayedFloatNav(navID) {

	floatLeftOffset = NAVLEFTOFFSET;
	verticalOffset = NAVVERTOFFSET;

	floatElement(navID, "popupNavDiv", "popupNavTable", 540,null, true, false);
}
//************************************************************************************************************************************************************
function unFloatNav(navID) {

	endHoverLaunchTimeout();
	startHoverTimeout(navHoverDelayExit);
}

//************************************************************************************************************************************************************
function floatSKURightCol(skuID) {

	startHoverLaunchTimeout(hoverLaunchDelay, "SKURIGHT", skuID);
}

//************************************************************************************************************************************************************
function floatSKURightColNOW(skuID) {

	floatLeftOffset = SKURIGHTCOLLEFTOFFSET;
	verticalOffset = SKUVERTOFFSET;

	delayedFloatSKU(skuID);
}

//************************************************************************************************************************************************************
function floatSKUNOW(skuID) {

	floatLeftOffset = SKULEFTOFFSET;
	verticalOffset = SKUVERTOFFSET;

	delayedFloatSKU(skuID);
}
//************************************************************************************************************************************************************
function floatSKU(skuID) {

	startHoverLaunchTimeout(hoverLaunchDelay, "SKU", skuID);
}

//************************************************************************************************************************************************************
function delayedFloatSKU(skuID) {

	floatElement(skuID, "popupSKUDiv", "popupSKUTable", 650,null, true, false);
}

//************************************************************************************************************************************************************
function unFloatSKU(skuID) {

	endHoverLaunchTimeout();
	startHoverTimeout(navHoverDelayExit);
}

//************************************************************************************************************************************************************
function floatElement(elementID, floatDivClass, floatTableClass, floatHoverWidth, independentElement, showCloseBox, locationAlign) 
{

	if(currentPopupElemId != elementID)
	{
		removeHoverPopup();
		currentPopupElemId = elementID;
	}

	if(hoverPopupDiv == null)
	{
		allowHoverPopupClose = true;
		mouseHasExited = false;
		mouseFirstEntry = false;
	
		var incomingHoverSource= independentElement ? independentElement : document.getElementById(elementID);
		if(incomingHoverSource != null)
		{
			var hoverSource = incomingHoverSource.cloneNode(true);
// Keep forms separated with distinct ids
			var formChildren = hoverSource.getElementsByTagName('form');
			for(var index = 0; index < formChildren.length; index++)
			{
				var targetChild = formChildren[index];
				try
				{
					targetChild.innerHTML = targetChild.innerHTML.replace(/buyForm-/g,"hoverBuyForm-");
					targetChild.id = targetChild.id.replace(/buyForm-/g,"hoverBuyForm-");
				}
				catch(e)
				{
					//alert("PROBLEM with " + targetChild.className + "\n" + e);
				}
			}
// Remove sub-hover function from hovers
			var spanChildren = hoverSource.getElementsByTagName('span');
			for(var index = 0; index < spanChildren.length; index++)
			{
				var targetChild = spanChildren[index];
				if(targetChild.className == "promoInfoHover")
				{
					var pSibling = targetChild.previousSibling;
					var nSibling = targetChild.nextSibling;
					var newSpan = document.createElement("span");
					newSpan.innerHTML = targetChild.innerHTML.replace("promoInfoHover","");
				try
					{
					targetChild.parentNode.replaceChild(newSpan,targetChild);
					}
				catch(e)
					{
					//alert("NEXT: " + e.name + " : " + e.message);
					}
				}		
			}

			launchMouseX = mouseX;
			launchMouseY = mouseY;

			hoverPopupDiv=document.createElement("div");
			hoverPopupDiv.className = floatDivClass;

			var scrollPosX = findScrollLeft();
			var scrollPosY = findScrollTop();
			var newTable = document.createElement("table");
			newTable.className =  floatTableClass;
			newTable.id = "sizeThis";
			var newTBody = document.createElement("tbody");
			newTable.appendChild(newTBody);

			var nodeClone = hoverSource; //.cloneNode(true);
			var closeBox = null;

			var children = nodeClone.getElementsByTagName('td');
			for(var index = 0; index < children.length; index++)
			{
				if(children[index].id == "closeBox")
				{
					closeBox  = children[index];
					break;
				}
			}

			if(closeBox == null)
			{
				closeBox = document.createElement('td');
				closeBox.width = "100%";
				closeBox.align = "right";
				closeBox.valign = "top";

				var closeTR = document.createElement("tr");
				closeTR.appendChild(closeBox);

			if(showCloseBox == true)
				{
				newTBody.appendChild(closeTR);
				}
			}

			var clickClose = document.createElement('img');
			clickClose.src = "http://images.allergybuyersclub.com/img/close-x.jpg";
			clickClose.onclick = removeHoverPopup;
			closeBox.appendChild(clickClose);
			var newTR = document.createElement("tr");
			var newTD = document.createElement("td");
			newTD.appendChild(nodeClone);
			newTR.appendChild(newTD);
			newTBody.appendChild(newTR);
			hoverPopupDiv.appendChild(newTable);

			hoverPopupDiv.style.top = (launchMouseY + verticalOffset + (window.getComputedStyle ? 0: scrollPosY)) + "px";

			if(locationAlign == true)
				{
				hoverPopupDiv.style.left = floatLeftOffset + "px"; 
				}
			else
				{
				hoverPopupDiv.style.left = floatLeftOffset + "px"; //(mouseX + (window.getComputedStyle ? 0: scrollPosX) - (offsetPad * 7)) + "px"; 
				}

			if(elementID != null)
				{
				hoverPopupDiv.style.width = floatHoverWidth + "px"; 
				}

			var body = document.getElementsByTagName("body");
			body[0].appendChild(hoverPopupDiv);
			newTable.focus();

			if(!((mouseX >= (floatLeftOffset))
			  && (mouseX <= (floatLeftOffset  + hoverPopupDiv.offsetWidth)) 
			  && (mouseY >= (launchMouseY + verticalOffset)) 
			  && (mouseY <= (launchMouseY + hoverPopupDiv.offsetHeight))))
			{
//				startHoverTimeout(navHoverDelay);
			}
			else
			{
				mouseHasExited = false;
				mouseFirstEntry = true;
			}
		}
	}
}

//************************************************************************************************************************************************************
function removeHoverPopup() {

//alert("Killing Hover Popup with closeSKUPopup="+closeSKUPopup);

if(hoverPopupDiv != null) 
	{
	endHoverTimeout();
	var oldForms = hoverPopupDiv.getElementsByTagName("form")
	if((oldForms != null) && (oldForms.length > 0))
		{
		for(var index = 0; index < oldForms.length; index++)
			{
			oldForms[index].id = "XOXOXO";
			}
		}
	var body = document.getElementsByTagName("body");
	body[0].removeChild(hoverPopupDiv);
	hoverPopupDiv = null;
	}
}

//************************************************************************************************************************************************************
function popUp(URL) {

if(URL.indexOf(".html") > 0)  // Don't try to load and pop html pages like images.
	{
	popUpPrior(URL);
	}
else
	{
	popUpWithArgs(URL, null, null) ;
	}
}

//************************************************************************************************************************************************************
function popUpWithArgs(URL, width, height) 
{
document.body.style.cursor = 'wait';

popupImage = document.createElement("img");
popupImage.onload = prepPopFrameless;
popupImage.src = URL;
}

//************************************************************************************************************************************************************
function listActivated() {
	allowHoverPopupClose = false;
	mouseFirstEntry = false;
}

//************************************************************************************************************************************************************
function listDeactivated() {
//alert("DEACTIVE");
	allowHoverPopupClose = true;
	mouseFirstEntry = true;
}

//************************************************************************************************************************************************************
// Pop up a frameless version of the image selected by the user
//************************************************************************************************************************************************************
function popFrameless(img, width, height) {

if(!popUpPopping)
	{
	popUpPopping= true;

	document.body.style.cursor = 'wait';

	popupImage = document.createElement("img");
	popupImage.onload = prepPopFrameless;
	popupImage.src = img;
	}
}

//************************************************************************************************************************************************************
function prepPopFrameless(event)
{
var windowWidth =  findClientWidth();
var windowHeight =  findClientHeight();
var scrollPosX = findScrollLeft();
var scrollPosY = findScrollTop();
mouseX = (((windowWidth - popupImage.width) / 2) + offsetPad + (window.getComputedStyle ? scrollPosX : 0));
mouseY = (((windowHeight - popupImage.height) / 2) + offsetPad + (window.getComputedStyle ? scrollPosY : 0));

reallyPopFrameless(popupImage.width, popupImage.height);
}

//************************************************************************************************************************************************************
// Pop up a frameless version of the image selected by the user
//************************************************************************************************************************************************************
function reallyPopFrameless(width, height) {

if(currentPopup)
	{
	closePopFrameless(currentPopup)
	}

// Create the new div to float the image (use global for exansion function)
popupDiv=document.createElement("div");
popupDiv.className = "popupDiv";

// Set maxes for div expansion
var maxWidth =  width;
var maxHeight = height;

// Create the new image object and add it to its parent (use global for exansion function)
popupImage.style.width = maxWidth + "px";
popupImage.style.height = maxHeight + "px";
popupImage.onclick = new Function ('closePopFrameless(this)');
popupDiv.appendChild(popupImage);

// Create the new close label and add it to its parent
var popupCloseLabel = document.createElement("div");
popupCloseLabel.onclick = new Function ('closePopFrameless(this)');
popupCloseLabel.innerHTML = "<table width='100%' cellpadding='0' cellspacing='0'><tr><td><hr width='100%' size='1'></td></tr>" 
+ "<tr><td><span class='sbc-text'><nobr>- Click image to close -</nobr></span></td></tr></table>";
popupCloseLabel.style.width = maxWidth + "px";
popupCloseLabel.className = "popupDivClosure";
popupDiv.appendChild(popupCloseLabel);

// Determine the size of the current window and decide whether to shift image start up or to the right to accomodate
var windowWidth =  findClientWidth();
var windowHeight =  findClientHeight();
var xStartPoint = (mouseX - (offsetPad * 3));
var yStartPoint =  (mouseY - (offsetPad * 3));
var scrollPosX = findScrollLeft();
var scrollPosY = findScrollTop();

// Don't let popup go below the bottom of visible screen
if(((mouseY + maxHeight + (offsetPad * 6)) - (window.getComputedStyle ? scrollPosY : 0)) > windowHeight)
	{
	popupDiv.style.top = ((windowHeight + scrollPosY) - (maxHeight + (offsetPad * 4))) + "px";
	}
else
	{
	popupDiv.style.top = (mouseY + (window.getComputedStyle ? 0: scrollPosY) - (offsetPad * 1)) + "px";
	}

// Don't let popup go beyond the right hand boundary of visible screen
if(((mouseX + maxWidth + (offsetPad * 3)) - (window.getComputedStyle ? scrollPosX : 0)) > windowWidth)
	{
	popupDiv.style.left = ((windowWidth + scrollPosX) - (maxWidth + (offsetPad * 5))) + "px";
	}
else
	{
	popupDiv.style.left = (mouseX + (window.getComputedStyle ? 0: scrollPosX) - offsetPad) + "px"; //window.getComputedStyle
	}

// Set the new div to open in the right place and the right size
popupDiv.style.width = maxWidth  +  "px";
popupDiv.style.height =  maxHeight + (offsetPad * 2) + "px";

// Add the div to the body to display it
var body = document.getElementsByTagName("body");
body[0].appendChild(popupDiv);

currentPopup = popupImage;

expandPopUp(0, maxHeight)

document.body.style.cursor = 'default';
}


//************************************************************************************************************************************************************
// Expands the popup image
//************************************************************************************************************************************************************
function expandPopUp(currentHeight, maxHeight)
{

if((popupImage.style.height != null) && (popupImage.style.width != null))
	{
	if(currentHeight <= maxHeight)
		{
		popupDiv.style.height = (currentHeight + (offsetPad * 3)) + "px";
		popupImage.style.height = currentHeight + "px";
		currentHeight += (maxHeight * 0.1);
		var command = "expandPopUp(" + currentHeight + "," + maxHeight + ")";
		setTimeout(command,0);
		}
	else
		{
		popupDiv.style.height = (maxHeight + (offsetPad * 3)) + "px";
		popupImage.style.height = maxHeight + "px";
		popUpPopping = false;
		}
	}
else
	{
	var command = "expandPopUp(" + currentHeight + "," + maxHeight + ")";
	setTimeout(command,0);
	}
}

//************************************************************************************************************************************************************
// Close the image popup
//************************************************************************************************************************************************************
function closePopFrameless(img) {
if(img && img.parentNode && (currentPopup != null))
	{
	var body = document.getElementsByTagName("body");
	body[0].removeChild(currentPopup.parentNode);
	currentPopup = null;
	}
}

//************************************************************************************************************************************************************
// Browser neutrality
//************************************************************************************************************************************************************
function findClientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function findClientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function findScrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function findScrollTop() {

	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

//************************************************************************************************************************************************************
// Record where the mouse is when it is clicked on an enlargeable image or when mose is moved while a SKU popup is open.
//************************************************************************************************************************************************************
function locateMouse(e)
{
if (!e) var e = window.event;
if (e.pageX || e.pageY)
	{
	mouseX  = e.pageX;
	mouseY = e.pageY;
	}
else if (e.clientX || e.clientY)
	{
	mouseX  = e.clientX;
	mouseY = e.clientY;
	}

if((hoverPopupDiv != null) && (checkForRemoval == null))
	{
	checkForRemoval = 1;
	var scrollPosX = findScrollLeft();
	var scrollPosY = findScrollTop();

	var xOffset = (window.getComputedStyle ? 0: scrollPosX);
	var yOffset = (window.getComputedStyle ? 0: scrollPosY);

	if((mouseHasExited == true)
		&& (mouseX >= (floatLeftOffset)) 
		&& (mouseX <= (floatLeftOffset  + hoverPopupDiv.offsetWidth)) 
		&& (mouseY >= (launchMouseY + verticalOffset)) 
		&& ((mouseY <= (launchMouseY + hoverPopupDiv.offsetHeight))))
		{
			mouseHasExited = false;
			mouseFirstEntry = true;
			endHoverTimeout();
//			listDeactivated();
		}

	if ((mouseX < floatLeftOffset) 
		|| (mouseX > (floatLeftOffset  + hoverPopupDiv.offsetWidth)) 
		|| (mouseY < (launchMouseY + verticalOffset)) 
		|| (mouseY > (launchMouseY + hoverPopupDiv.offsetHeight + verticalOffset)))
		{
			mouseHasExited = true;
		}

	if((allowHoverPopupClose == true)  &&  (mouseFirstEntry == true)
		&& ((mouseX < (floatLeftOffset  - removalPad)) 
			|| (mouseX > (floatLeftOffset  + hoverPopupDiv.offsetWidth + removalPad)) 
			|| (mouseY < (launchMouseY - removalPad + verticalOffset)) 
			|| ((mouseY > (launchMouseY + hoverPopupDiv.offsetHeight + removalPad + verticalOffset)))))
		{
		if(mouseHasExited == true)
			{
			checkForRemoval = null;
			startHoverTimeout(navHoverDelayExit);
			}
		}

	checkForRemoval = null;
	}
}

//************************************************************************************************************************************************************
function preview(URL, id)
{
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1');");
}


//************************************************************************************************************************************************************
// Makes prior non-image page popups work - must remap popUp() to popUpPrior()
//************************************************************************************************************************************************************
function popUpPrior(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=450,height=500,left = 125,top = 100');");
}


//************************************************************************************************************************************************************
function popUpShip(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=475,height=435,left = 250,top = 100');");
}

//************************************************************************************************************************************************************
function ShowFamily()
{
	if (navigator.appName!="Microsoft Internet Explorer")
	{
	   var items = document.getElementsByName("item");
	   for (var i = 0; i < items.length; i++)
	   {
	     items[i].style.display = "none";
  	   }
	}
	else
	{
	    //ie kludge
	    trs = document.getElementsByTagName("TR");
	    {
	       for (i=0; i < trs.length; i++)
	       {
	           if (trs[i].name == "item") trs[i].style.display = "none";
	       }
	    }
	    trs = document.getElementsByTagName("table");
	    {
	       for (i=0; i < trs.length; i++)
	       {
	           if (trs[i].name == "item") trs[i].style.display = "none";
	       }
	    }
	    trs = document.getElementsByTagName("a");
	    {
	       for (i=0; i < trs.length; i++)
	       {
	           if (trs[i].name == "item") trs[i].style.display = "none";
	       }
	    }
	}
}

// for "family" category pages to automatically hide part of the page
//************************************************************************************************************************************************************
function DoExpand(familyName, itemName, numberOfItems) 
{
    ShowHideElements(familyName, itemName, numberOfItems, false)  
}

//************************************************************************************************************************************************************
function DoContract(familyName, itemName, numberOfItems) 
{
	ShowHideElements(familyName, itemName, numberOfItems, true)
}

//************************************************************************************************************************************************************
function ShowHideElements(familyName, itemName, numberOfItems, isShow)
{
    var itemDisplay;
    var familyDisplay;
	if (isShow)
	{
		itemDisplay="none";
		familyDisplay="";
	}
	else
	{
		itemDisplay="";
		familyDisplay="none";
	}
	
	var id0=itemName + "_0";
	var id4 = itemName + "_" + (1+1*numberOfItems);
	var element= document.getElementById(id0);
	element.style.display = familyDisplay; 
  	
   	for (var i = 0; i < numberOfItems; i++)
   	{
   		var id1 = itemName + "_" + (i+1);
   		var element = document.getElementById(id1);
   		element.style.display = itemDisplay;
  	}
    if (numberOfItems > 0)
  	{
  		document.getElementById(id4).style.display = itemDisplay; 
  	}
  
    if (navigator.appName!="Microsoft Internet Explorer")
    {
        var family = document.getElementsByName(familyName);
	    for (var i = 0; i < family.length; i++)
	    {
			family[i].style.display = familyDisplay;
	    }
     }
     else
     {
	     //ie kludge
	     spans = document.getElementsByTagName("a");
 	     for (i=0; i < spans.length; i++)
 	     {
 		    if (spans[i].name == familyName) spans[i].style.display = familyDisplay;
 	     }
	 }
}

//************************************************************************************************************************************
// Create a platform-appropriate XMLHttpRequest object
//************************************************************************************************************************************
function getHTTPObject() {

var xhr = false;

if(window.XMLHttpRequest) 
	{
	xhr = new XMLHttpRequest();
	}
else if (window.ActiveXObject)
	{
	try
		{
		xhr = new ActiveXObject("Msmxl2.XMLHTTP");
		}
	catch(e)
		{
		try
			{
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
		catch(e)
			{
			xhr = false;
			}
		}
	}
return xhr;
}


//************************************************************************************************************************
// We are leaving the shopping pages and going to the cart.  Is there something to do about it?
//************************************************************************************************************************
function goingToTheCart() {
//alert("going to cart");
rescindOffer();
}

//************************************************************************************************************************
// Customer has clicked Add to Cart.  Are there special actions to take?
//************************************************************************************************************************
function submitBuyForm(formName) {

goingToTheCart();

allowHoverPopupClose = false;
var form = document.getElementById(formName);
if(form)
	{
	form.submit();
	}
else
	{
	//alert("MISSING");
	}
}

//*******************************************************************************************************************************************************
function submitForm(form, context, app, abcFormName, method, isConfirm) 
{
    url = context + "/do/" + app + "/";
    //alert('url= ' + url);
    if (isConfirm==true)
    {
    	var isDelete=confirm('Delete it?');
     	if (isDelete==true)
	    {
	    	//alert('confirmed ' + isDelete);
		    form.action=url + abcFormName + '?method=' + method;
		    //alert(url + abcFormName + '?method=' + method);
		    form.submit();
		    return true;
	    }
	    else
	    {
	        return false;
	    	//alert('cancelled');
	    }
 	}
 	else
 	{
		//alert('just submit it');
		form.action=url + abcFormName + '?method=' + method;
		form.submit();
		return true;
	}
}

//************************************COPYRIGHT BostonGreenGoods.com - All rights reserved. ****************************************************

//************************ START Drop Down Menu- © Dynamic Drive ***************************************************************
/***********************************************
* AnyLink Drop Down Menu- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var menuwidth='165px' //default menu width
var menubgcolor='lightyellow'  //menu bgcolor
var disappeardelay=250  //menu disappear speed onMouseout (in miliseconds)
var hidemenu_onclick="yes" //hide menu when user clicks within menu?

/////No further editting needed

var ie4=document.all
var ns6=document.getElementById&&!document.all

if (ie4||ns6)
document.write('<div id="dropmenudiv" style="visibility:hidden;width:'+menuwidth+';background-color:'+menubgcolor+'" onMouseover="clearhidemenu()" onMouseout="dynamichide(event)"></div>');

function getposOffset(what, offsettype)
{
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
	totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
	parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}


function showhide(obj, e, visible, hidden, menuwidth)
{
	if (ie4||ns6)
		dropmenuobj.style.left=dropmenuobj.style.top="-500px"
	if (menuwidth!="")
	{
		dropmenuobj.widthobj=dropmenuobj.style
		dropmenuobj.widthobj.width=menuwidth
	}
	if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
	obj.visibility=visible
	else if (e.type=="click")
	obj.visibility=hidden
}

function iecompattest()
{
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge)
{
	var edgeoffset=0
	if (whichedge=="rightedge")
	{
		var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
		dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
		if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
			edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
	}
	else
	{
		var topedge=ie4 && !window.opera? iecompattest().scrollTop : window.pageYOffset
		var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
		dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
		if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
		{ //move up?
			edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
			if ((dropmenuobj.y-topedge)<dropmenuobj.contentmeasure) //up no good either?
				edgeoffset=dropmenuobj.y+obj.offsetHeight-topedge
		}
	}
	return edgeoffset
}

function populatemenu(what)
{
	if (ie4||ns6)
		dropmenuobj.innerHTML=what.join("")
}


function dropdownmenu(obj, e, menucontents, menuwidth)
{
	removeHoverPopup();
	if (window.event) event.cancelBubble=true
	else if (e.stopPropagation) e.stopPropagation()

	clearhidemenu()
	dropmenuobj=document.getElementById? document.getElementById("dropmenudiv") : dropmenudiv
	populatemenu(menucontents)

	if (ie4||ns6)
	{
		showhide(dropmenuobj.style, e, "visible", "hidden", menuwidth)
		dropmenuobj.x=getposOffset(obj, "left")
		dropmenuobj.y=getposOffset(obj, "top")
		dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
		dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
	}

	return clickreturnvalue()
}

function clickreturnvalue()
{
	if (ie4||ns6) return false
		else return true
}

function contains_ns6(a, b) 
{
	while (b.parentNode)
	if ((b = b.parentNode) == a)
		return true;
	return false;
}

function dynamichide(e)
{
	if (ie4&&!dropmenuobj.contains(e.toElement))
		delayhidemenu()
	else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget))
		delayhidemenu()
}

function hidemenu(e)
{
	if (typeof dropmenuobj!="undefined")
	{
		if (ie4||ns6)
			dropmenuobj.style.visibility="hidden"
	}
}

function delayhidemenu()
{
	if (ie4||ns6)
		delayhide=setTimeout("hidemenu()",disappeardelay)
}

function clearhidemenu()
{
	if (typeof delayhide!="undefined")
		clearTimeout(delayhide)
}

if (hidemenu_onclick=="yes")
	document.onclick=hidemenu
//************************ END Drop Down Menu- © Dynamic Drive ***************************************************************