var loadLog = '';

function doOnLoadStuff() {

	loadLog += '\nFunction doOnLoadStuff executing...';

}

//####################### FADING IMAGES FUNCTIONS ###############################

var slideShowCount = 8;

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

var fading = false;

function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
	  fading = true;
      setOpacity(obj, opacity);
      opacity += 3.5;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 80);
    } else {
		fading = false;
		var newNum = rand(8);
		while (newNum == slide1Num || newNum == slide2Num){
			newNum = rand(8);
		}
		document.getElementById('slide1').src = '/img/slide/' + newNum + '.jpg';
		setTimeout('fadeOut(\'' + objId + '\', 100)', 2200);
	}
  }
}

function fadeOut(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity > 0) {
	  fading = true;
      setOpacity(obj, opacity);
      opacity -= 3.5;
      window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 80);
    } else {
		fading = false;
		var newNum = rand(8);
		while (newNum == slide1Num || newNum == slide2Num){
			newNum = rand(8);
		}
		document.getElementById(objId).src = '/img/slide/' + newNum + '.jpg';
		setTimeout('fadeIn(\'' + objId + '\', 0)', 2200);
	}
  }
}

var slide1Num = 1;
var slide2Num = 1;

function fadeSlides(which) {
	
	fadeOut('slide2', 100);

}

function pickSlides() {
	var s1 = rand(8);
	var s2 = rand(8);
	while (s2 == s1){
		s2 = rand(8);
	}
	slide1Num = s1;
	slide2Num = s2;
	document.getElementById('slide1').src = '/img/slide/' + s1 + '.jpg';
	document.getElementById('slide2').src = '/img/slide/' + s2 + '.jpg';
	

}


//########################## END FADING IMAGES FUNCTIONS ############################

//########################## CONTENT WRITING ########################################


var navSections = new Array();

navSections[navSections.length] = 'shrubs';
navSections[navSections.length] = 'ferns';
navSections[navSections.length] = 'bulbs';
navSections[navSections.length] = 'roots';
navSections[navSections.length] = 'az';

function writeNav(currSect) {
	var navML = '';
	var cnt = 0;
	for (var i=0;i<navSections.length;i++) {
		if (navSections[i] != currSect) {
			navML += '<a title="click to view our selection of ' + navSections[i] + '" href="' + navSections[i] + '.html" class="navA_subPage" style="position:absolute;top:' + (cnt==0?5:cnt==1?35:cnt==2?64:92) + 'px;left:' + (cnt==0?197:cnt==1?193:cnt==2?179:157) + 'px;background-image: url(\'/img/nav_' + navSections[i] + '_small.gif\');">&nbsp;</a>';
			cnt ++;	
		}			
	}
	return(navML);
}


function getButtonML(btnText, theOnClick, flote, width, btnTxtID, leftM, rightM, title) {

	var buttonML = '<div title="' + (title?title:'') + '" style="position:relative;' + (exists(rightM)?'margin-right:' + rightM + 'px':'') + ';' + (exists(leftM)?'margin-left:' + leftM + 'px':'') + ';' + (exists(flote)?(flote=='l'?'float:left;':'float:right;'):'') + 'height:18px;padding-top:3px;padding-right:0px;margin-right:0px;font-family:trebuchet ms;font-size:11px;color:#ffffff;">' +
	'<a href="#null" onClick="' + theOnClick + '" class="btn"><div style="position:relative;float:left;width:11px;"><img src="/img/btn/leftCap.gif"></div>' +
	'<div style="position:relative;float:left;background-image:url(\'/img/btn/buttonBG.gif\');background-color:transparent;width:' + (exists(width)?width + 'px':'auto') + ';height:18px;">' +
	'<span style="position:relative;padding:3px;" class="btn" id="' + (exists(btnTxtID)?btnTxtID:'') + '">' + btnText +
	'</span></div><div style="position:relative;float:left;width:3px;"><img src="/img/btn/rightCap.gif"></div></a></div>';

	return(buttonML);
}

//########################## END CONTENT WRITING ####################################

//########################## SHOPPING CART ##########################################

var pottedPlantPrice = 4.25;

var shoppingCartItems = null;

function addToCart(itemName, itemIndex, itemDivObj, isPotted) {

	if(!exists(isPotted)) {
		isPotted = false;
	}

	if (shoppingCartItems == null) {
		shoppingCartItems = buildCart(getCookie("VVW_cartCookie"));
	}
	

	if (confirm('Are you sure you want to add ' + itemName + ' to your cart?')) {

		if (shoppingCartItems == null) {
			shoppingCartItems = new Array();
		}
		var thisItem = new Object();
		thisItem = findItemByOriginalID(itemIndex);		

		shoppingCartItems = addNewOrMore(shoppingCartItems, thisItem, isPotted);

		setCookie('VVW_cartCookie', serializeCart());
		updateTopCart();
		showCart(itemDivObj, itemName)	
	}
	
}

function addNewOrMore(shoppingCartItems, thisItem, isPotted) {
	var isNew = true;
	var sameAs = -1;

	for (var i=0;i<shoppingCartItems.length ;i++ ) {
		if (thisItem.originalID == shoppingCartItems[i].originalID) {

			if ((isPotted && shoppingCartItems[i].potted) || (!isPotted && !shoppingCartItems[i].potted)) {
				isNew = false;
				sameAs = i;	
				break;
			}			
			
		}
	}

	if (isNew) {

		shoppingCartItems[shoppingCartItems.length] = new Object();

		shoppingCartItems[shoppingCartItems.length-1].common_name = thisItem.common_name + '';
		shoppingCartItems[shoppingCartItems.length-1].sci_name = thisItem.sci_name + '';
		shoppingCartItems[shoppingCartItems.length-1].category = thisItem.category + '';		
		shoppingCartItems[shoppingCartItems.length-1].image = thisItem.image + '';
		shoppingCartItems[shoppingCartItems.length-1].on_sale = thisItem.on_sale + '';;
		shoppingCartItems[shoppingCartItems.length-1].is_new = thisItem.is_new + '';;
		shoppingCartItems[shoppingCartItems.length-1].originalID = thisItem.originalID + '';;
		shoppingCartItems[shoppingCartItems.length-1].availPotted = thisItem.availPotted + '';

		if (isPotted) {
			shoppingCartItems[shoppingCartItems.length-1].common_name = 'Potted ' + shoppingCartItems[shoppingCartItems.length-1].common_name;
			shoppingCartItems[shoppingCartItems.length-1].price = pottedPlantPrice;
			shoppingCartItems[shoppingCartItems.length-1].quantity = 1;
			shoppingCartItems[shoppingCartItems.length-1].potted = true;
		} else {
			shoppingCartItems[shoppingCartItems.length-1].price = thisItem.price + 0;
			shoppingCartItems[shoppingCartItems.length-1].quantity = getIncrementAmount(shoppingCartItems[shoppingCartItems.length-1]);
		}		

	} else {
		shoppingCartItems[sameAs].quantity = (shoppingCartItems[sameAs].quantity - 0) + getIncrementAmount(thisItem, isPotted);		
	}

	return(shoppingCartItems);

}


function getIncrementAmount(thisItem, isPotted) {

	if (!exists(isPotted)) {
		isPotted = thisItem.potted;
	}

	if (thisItem.category == 'shrub') {
		return(1);
	} else {
		if (isPotted != null && isPotted) {
			return(1);
		} else {
			return(5);
		}
		
	}
}

function buildCart(serializedCart) {

	var cartItems = new Array();
	if (serializedCart != null && serializedCart != '' && serializedCart.split('|').length != 0) {
		
		var cartBits = serializedCart.split('|');

		for (var i=0;i<cartBits.length ;i++ ) {
			var currItem = new Object();
			currItem = findItemByOriginalID(cartBits[i].split('#Q#')[1]);
			
			if (cartBits[i].split('#Q#')[0].indexOf('_P') != -1) {
				currItem.quantity = cartBits[i].split('#Q#')[0].split('_')[0] - 0;
				currItem.price = pottedPlantPrice;
				currItem.potted = true;
				currItem.common_name = 'Potted ' + currItem.common_name;
			} else {
				currItem.quantity = cartBits[i].split('#Q#')[0] - 0;
			}

			
			cartItems[cartItems.length] = currItem
		}

	}
	return(cartItems);
}

function serializeCart() {
	var serializedCart = '';

	for (var i=0;i<shoppingCartItems.length ;i++ ) {
		if (!shoppingCartItems[i].potted) {
			serializedCart += (serializedCart.length ==0?'':'|') + (shoppingCartItems[i].quantity-0) + '#Q#' + shoppingCartItems[i].originalID;
		} else {
			serializedCart += (serializedCart.length ==0?'':'|') + (shoppingCartItems[i].quantity-0) + '_P#Q#' + shoppingCartItems[i].originalID;
		}
		
	}

	if (serializedCart == '') {
		serializedCart = null;
	}

	return(serializedCart);

}

function refreshCart(afterEmpty) {
	if (!afterEmpty) {
		setCookie('VVW_cartCookie', serializeCart());
	} else {
		deleteCookie('VVW_cartCookie');
	}
	document.getElementById('cartListingDiv').innerHTML = writeCartContents();
	updateTopCart();
}

function updateCartQuantities() {	
	
	var updateMessage = 'Cart contents are <strong>unchanged</strong>.';
	var didUpdate = false;

	for (var i=0;i<shoppingCartItems.length ;i++ ) {
		var newQ = (eval('document.cForm.itemQuantity_' + i + '.value') - 0);
		if (newQ == 0) {
			didUpdate = removeItem(i);		
		} else if (newQ < getIncrementAmount(shoppingCartItems[i])) {
			alert('The minimum quanitity for this item is ' + getIncrementAmount(shoppingCartItems[i]) + '.');
			didUpdate = false;
		} else {
			shoppingCartItems[i].quantity = newQ;
			didUpdate = true;
		}
	}

	if (didUpdate) {
		updateMessage = 'Cart contents have been <strong>updated</strong>.';
	}
	document.getElementById('cartMessage').innerHTML = updateMessage;

	refreshCart();
	
}

function updateTopCart(doBuild) {

	if (doBuild) {
		shoppingCartItems = buildCart(getCookie("VVW_cartCookie"));
	}

	if (shoppingCartItems.length == 0) {
		document.getElementById('topCartContentDiv').innerHTML = '<div style="text-align:left;padding-left:10px;"><strong>Your cart is empty.</strong><br><img src="/img/blank.gif" height="8"><br><a href="/contactUs.html" style="font-weight:normal;">Join Our Mailing List &#187;</a></div>';
	} else {
		document.getElementById('topCartContentDiv').innerHTML = '<div style="text-align:left;padding-left:10px;">Your cart contains ' + shoppingCartItems.length + ' item' + (shoppingCartItems.length>1?'s':'') + '.</div>' +								
																 '<div style="text-align:left;padding-left:10px;"><strong style="color:#E99ADB;">Total: </strong><strong>' + cartTotal + '</strong></div>' +
																 '<div style="position:relative;clear:both;height:20px;padding-left:10px;padding-right:10px;">' + getButtonML('checkout', 'checkout(document.getElementById(\'header\'))', 'r', null, null, null, null, 'click to checkout') + getButtonML('view cart', 'showCart(document.getElementById(\'header\'))', 'l', null, null, null, null, 'click to view your shopping cart') + '</div>';
	}

}

var cartTotal = '';
var orderHasFern = false;
var totalShrubCount = 0;
var totalPottedCount = 0;
var totalItems = 0;
var shippingCost = 0;


function writeCartContents() {
	var cartML = '';
	totalShrubCount = 0;
	totalPottedCount = 0;
	totalItems = 0;
	shippingCost = 0;
	if (shoppingCartItems.length != 0){


		var totalPrice = 0;		

		orderHasFern = false;

		for (var i=0;i<shoppingCartItems.length ;i++ )
		{
			var thisPrice = calulatePrice(shoppingCartItems[i]);

			cartML += '<div style="font-family:trebuchet ms;font-size:13px;color:#B2F7B2;line-height:25px;height:25px;">' + 
							'<div style="position:relative;float:right;width:80px;text-align:right;">' + thisPrice.formattedDollars + '</div>' +
							'<div style="position:relative;float:right;width:150px;text-align:left;"><span style="text-decoration:none;" class="qty">qty:</span><input title="click to change this items quantity - then click update cart" type="text"class="qtyInput" name="itemQuantity_' + i + '" style="width:30px;margin-left:2px;" value="' + shoppingCartItems[i].quantity + '"> @ ' + formatDollars(getUnitPrice(shoppingCartItems[i])) + '</div>' + 
							(i + 1) + '.&nbsp; ' + 
							shoppingCartItems[i].common_name + 						
					  '</div>';

			totalPrice += thisPrice.decimalDollars;

			if (!orderHasFern && shoppingCartItems[i].category == 'fern') {
				orderHasFern = true;
			}

			if (shoppingCartItems[i].category == 'shrub') {
				totalShrubCount += (shoppingCartItems[i].quantity - 0);
			}

			if (shoppingCartItems[i].potted) {
				totalPottedCount += (shoppingCartItems[i].quantity - 0);
			}

			totalItems += (shoppingCartItems[i].quantity - 0);
		}


		//and calculate shipping

		if (totalItems > (totalShrubCount + totalPottedCount)) {
			if (orderHasFern) {
				shippingCost += 10.95;
			} else {
				shippingCost += 10.35;
			}
			
			if (totalShrubCount > 0) {
				shippingCost += (totalShrubCount-1)*1.50 + 6.95;
			}

			if (totalPottedCount > 0) {
				shippingCost += (totalPottedCount-1)*1.00 + 5.45;
			}
			

		} else {
			if (totalShrubCount > 0) {
				shippingCost += (totalShrubCount-1)*1.50 + 6.95;
			}

			if (totalPottedCount > 0) {
				shippingCost += (totalPottedCount-1)*1.00 + 5.45;
			}
		}

		shippingCost = Math.round(shippingCost*100)/100;

		totalPrice += shippingCost;

		totalPrice = Math.round(totalPrice*100)/100;

		cartTotal = formatDollars(totalPrice);

		cartML += '<div style="font-family:trebuchet ms;color:#B2F7B2;line-height:20px;height:20px;font-size:13px;">' + 
							'<div style="position:relative;float:right;width:160px;text-align:right;">Shipping: </strong>' + formatDollars(shippingCost) + '</div>' +
							'<div style="position:relative;float:right;width:100px;text-align:right;">&nbsp;</div>' + 
							'&nbsp;&nbsp;' + 						
					  '</div>';
		
		cartML += '<div style="font-family:trebuchet ms;font-size:14px;color:#B2F7B2;line-height:30px;height:30px;">' + 
							'<div style="position:relative;float:right;width:160px;text-align:right;"><strong style="color:#E99ADB;">Total: </strong>' + formatDollars(totalPrice) + '</div>' +
							'<div style="position:relative;float:right;width:100px;text-align:right;">&nbsp;</div>' + 
							'&nbsp;&nbsp;' + 						
					  '</div>';
					 
		
		cartML = '<form name="cForm">' + cartML + '</form>';

		cartML += '<div>' + getButtonML('empty cart', 'emptyCart()', 'l', null, null, null, null, 'click to empty all items from your cart') + getButtonML('checkout', 'checkout(document.getElementById(\'cartDiv\'))', 'r', null, null, 110, null, 'click to checkout') + getButtonML('update quantities', 'updateCartQuantities()', 'r', null, null, null, null, 'click to save changes to item quantities and update the cart') + '</div>';

		cartML += '<div style="position:relative;clear:both;font-family:trebuchet ms;font-size:11px;color:#B2F7B2;padding-top:15px;">To remove an item, change its quantity to zero and click <a href="javascript:updateCartQuantities();">update quantities</a>.</div>';

	} else {
		cartML += '<span style="color:#B2F7B2;font-size:12px;font-family:trebuchet ms;">Your cart is empty.</span>';
	}
	return(cartML);

}

function getOrderContents() {

	shoppingCartItems = buildCart(document.oForm.o.value);

	setCookie('VVW_cartCookie', serializeCart());

	document.getElementById('orderContents').innerHTML = writeCartContents();

	shoppingCartItems = new Array();

	deleteCookie('VVW_cartCookie');

}


function removeItem(itemID) {
	var didRemove = false;
	if (confirm('Are you sure you want to remove ' + shoppingCartItems[itemID].common_name + ' from your cart?')) {
		for (var i=itemID;i<shoppingCartItems.length-1;i++ ) {
			shoppingCartItems[i] = shoppingCartItems[i+1];
		}
		shoppingCartItems.length--;
		didRemove = true;
	}
	refreshCart();
	return(didRemove);
}

function emptyCart() {
	if (confirm('Are you sure you want to empty your shopping cart?')) {
		shoppingCartItems = new Array();
		refreshCart(true);
		document.getElementById('cartMessage').innerHTML = 'Cart contents have been <strong>emptied</strong>.';
	}
	
}

function getUnitPrice(thisItem) {

	var unitPrice = 0;

	if (thisItem.price != null && thisItem.price != '' && thisItem.price != 0) {
		unitPrice = (thisItem.price - 0);
	} else if (thisItem.category == 'shrub') {
		unitPrice = 5;
	} else {
	
		if (thisItem.category == 'fern') {
			unitPrice = 0.75;
		} else if (thisItem.category == 'bulb') {
			unitPrice = 0.65;
		} else {
			unitPrice = 0.6;
		}

		var quant = thisItem.quantity;

		if (quant > 24) {
				
			if (quant < 100) {
				unitPrice = unitPrice - 0.05;
			} else if (quant < 500) {
				unitPrice = unitPrice - 0.1;
			} else if (quant >= 500) {
				unitPrice = unitPrice - 0.15;
			}

		}

	}
	
	unitPrice = Math.round(unitPrice*100)/100; 

	return(unitPrice);

}


function getPottedUnitPrice(thisItem) {

	var unitPrice = pottedPlantPrice;

	/*
	if (thisItem.price != '') {
		unitPrice = (thisItem.price - 0);
	} else if (thisItem.category == 'shrub') {
		unitPrice = 5;
	} else if (thisItem.category == 'bulb') {
		unitPrice = 0.65;
	} else {
		unitPrice = 0.6;
	}
	*/	

	return(unitPrice);

}


function calulatePrice(thisItem) {

	var quant = thisItem.quantity;

	var returnPrice = new Object();

	var unitPrice = getUnitPrice(thisItem);

	if ((thisItem.category != 'shrub') && thisItem.price == '') {

		if (quant > 24) {
			
			if (quant < 100) {
				unitPrice = unitPrice - 0.05;
			} else if (quant < 500) {
				unitPrice = unitPrice - 0.1;
			} else if (quant >= 500) {
				unitPrice = unitPrice - 0.15;
			}

		}

	}

	returnPrice.decimalDollars =  Math.round(quant*unitPrice*1000)/1000; 

	returnPrice.formattedDollars = formatDollars(returnPrice.decimalDollars);

	return(returnPrice);

}

function formatDollars(decimalDollars) {

	var formattedDollars = decimalDollars + '';

	if (formattedDollars.indexOf('.')== -1) {
		formattedDollars += '.00';
	} else if (formattedDollars.indexOf('.') == formattedDollars.length-2) {
		formattedDollars += '0';
	}

	formattedDollars = '$' + formattedDollars;

	return(formattedDollars);

}

function showCart(itemDivObj, itemName) {

	var ePos = findPos(itemDivObj);

	//var eX = ePos[0];
	var eY = ePos[1];

	//get the window size to offset clicks near the edges
	//var oX = document.body.clientWidth + document.body.scrollLeft;
	var oY = document.body.clientHeight + document.body.scrollTop;

	//and offset the left and top of the tip box
	//if we're close to an edge

	//if ((eX + tabW) > oX) {
	//	eX = eX - (tabW - (oX - eX)) - 1;	
	//}

	if ((eY + 467) > oY) {
		eY = eY - (467 - (oY - eY));	
	}

	//and finally the scroll offset for smaller windows

	//eX += document.body.scrollLeft;
	//eY += document.body.scrollTop;

	//alert(document.body.scrollTop);
	
	writeCart(itemName, eY);
	document.getElementById('cartDiv').style.top = eY + 'px';
	document.getElementById('cartDiv').style.display = 'block';	
}

function writeCart(itemName) {
	document.getElementById('cartDiv').innerHTML =  '<div style="position:relative;border:1px solid #166524;background-color:#166524;">' +
													'<div style="position:relative;border:1px solid #B2F7B2;background-color:#166524;">' +
													'<div style="position:relative;border:1px solid #166524;background-color:#166524;">' +
													'<div style="position:relative;border:1px solid #B2F7B2;background-color:#166524;">' +
														'<div style="height:465px;">' +
															'<div style="position:relative;float:left;background-color:#B2F7B2;padding:5px;font-weight:bold;font-size:18px;font-family:trebuchet ms;color:#166524">Your Shopping Cart</div>' +
															'<span style="line-height:35px;color:#E99ADB;font-family:trebuchet ms;font-size:13px;" id="cartMessage">' + (itemName != null?'<strong>' + itemName + '</strong> has been added to your cart.': shoppingCartItems.length + ' item' + (shoppingCartItems.length==1?' is ':'s are ') + 'in your cart.') + '</span>' +
															'<div style="position:relative;float:left;height:300px;text-align:left;left:20px;top:10px;overflow:auto;"><div style="width:500px;" id="cartListingDiv">' + writeCartContents() + '</div></div>' +
														'</div>' +
													'</div></div></div></div>' +
													'<div style="position:absolute;top:7px;;left:0px;width:100%;text-align:right;cursor:hand;cursor:pointer;" onClick="document.getElementById(\'cartDiv\').style.display = \'none\';"><img title="click to close the cart view" src="/img/btn/close_img.gif">&nbsp;&nbsp;</div>';	
	document.getElementById('cartDiv').style.left = '182px';
	document.getElementById('cartDiv').style.width = '622px';
	
	updateTopCart();
}

function findItemByOriginalID(originalID) {

	var thisItem = null;

	for (var i=0;i<prodList.length;i++ ) {
		if (prodList[i].originalID == originalID) {
			thisItem = new Object();

			thisItem.common_name = prodList[i].common_name + '';
			thisItem.sci_name = prodList[i].sci_name + '';
			thisItem.category = prodList[i].category + '';
			thisItem.price = prodList[i].price + 0;
			thisItem.image = prodList[i].image + '';
			thisItem.on_sale = prodList[i].on_sale + '';;
			thisItem.is_new = prodList[i].is_new + '';;
			thisItem.originalID = prodList[i].originalID + '';;
			thisItem.availPotted = prodList[i].availPotted + '';

		}
	}

	return(thisItem);
}

var orderID = '';

function createOrderID() {
	var oID = '';

	for (var i=0;i<50 ;i++ ) {
		
		if ((rand(2)-1) == 1) {
			oID += letters.charAt(rand(25));
		} else {
			oID += (rand(10)-1);
		}
		
	}

	return(oID);

}

function checkout(itemDivObj) {

	document.getElementById('cartDiv').innerHTML += writeSaveOrderFrame();

	showCheckoutHandoff(itemDivObj);

}

function writeSaveOrderFrame() {
	var sML = '';

	sML += '<iframe id="saveOrder" name="saveOrder" src="blank.html" style="position:absolute;left:-2000px;"></iframe>';

	return(sML);

}

function proceed2Paypal() {
	orderID = createOrderID();
	
	document.getElementById('cartDiv').innerHTML += '<form name="_xclick" action="https://www.paypal.com/us/cgi-bin/webscr" method="post" style="position:absolute;left:-2000px;">' +
			'<input type="hidden" name="cmd" value="_xclick">' +
			'<input type="hidden" name="business" value="bbfults@blomand.net">' +
			'<input type="hidden" name="cn" value="Shipping Address & Notes:">' +
			'<input type="hidden" name="currency_code" value="USD">' +
			'<input type="hidden" name="item_name" value="Wildflower Order from Viola Valley Wildflowers">' +
			'<input type="hidden" name="item_number" value="' + orderID + '">' +
			'<input type="hidden" name="amount" value="' + replace(cartTotal, '$', '') + '">' +
		  '</form>';

	document.getElementById('saveOrder').src='orders/saveOrder.php?oID=' + orderID + '&iNum=' + escape(serializeCart());
}

function goToPayPal() {
	document._xclick.submit();
}

function showCheckoutHandoff(itemDivObj) {

	var ePos = findPos(itemDivObj);

	//var eX = ePos[0];
	var eY = ePos[1] + 20;

	//get the window size to offset clicks near the edges
	//var oX = document.body.clientWidth + document.body.scrollLeft;
	var oY = document.body.clientHeight + document.body.scrollTop;

	//and offset the left and top of the tip box
	//if we're close to an edge

	//if ((eX + tabW) > oX) {
	//	eX = eX - (tabW - (oX - eX)) - 1;	
	//}

	if ((eY + 467) > oY) {
		eY = eY - (467 - (oY - eY));	
	}

	//and finally the scroll offset for smaller windows

	//eX += document.body.scrollLeft;
	//eY += document.body.scrollTop;

	//alert(document.body.scrollTop);
	
	document.getElementById('checkoutHandoffDiv').style.top = eY + 'px';
	document.getElementById('checkoutHandoffDiv').style.left = '262px';
	document.getElementById('checkoutHandoffDiv').style.display = 'block';	
}

//########################## END SHOPPING CART ######################################



//########################## UTILITIES ##############################################

var letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function showDiv(divID) {
	document.getElementById(divID).style.display = 'block';
}

function hideDiv(divID) {
	document.getElementById(divID).style.display = 'none';
}

function exists(thing) {

	if (thing != null) {
		if ((thing + '') == '0') {
			return(true);
		} else if (thing) {
			return(true);
		}
	}

	return(false);
}

var thisImagePop = null;
var thisCurrImage = null;

function popPic(thisURL) {

	var imgURL = thisURL;

	thisCurrImage = new Image;
	thisCurrImage.src= imgURL;

	thisImagePop = window.open(imgURL, 'pic', 'title=0,resizable=1,scrollbars=0,status=0,width=300,height=300');

	thisImagePop.focus();
	watchResize();
} 

function watchResize() {

	if (thisCurrImage.height > 0) {
		thisImagePop.resizeTo(thisCurrImage.width + 50, thisCurrImage.height + 80);
	} else {
		setTimeout('watchResize()', 100);
	}

}

function findPos(obj) {

	obj = document.getElementById(obj.id);

	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}

	return [curleft,curtop];
}

function showImage(prodInd, imageURL, linkObj, name1, name2, enlargeMsgID) {

	//close the current tab if there is one
	//closeTab();
	document.getElementById('click_' + enlargeMsgID).innerHTML = 'enlarging...';
	var img = new Image();
	img.onload = function () {showImageDiv(prodInd, imageURL, linkObj, name1, name2, img, enlargeMsgID)};
	img.src = imageURL;

}

function showImageDiv(prodInd, imageURL, linkObj, name1, name2, img, enlargeMsgID) {

var ePos = findPos(linkObj);

	//var eX = ePos[0];
	var eY = ePos[1];

	//get the window size to offset clicks near the edges
	//var oX = document.body.clientWidth + document.body.scrollLeft;
	var oY = document.body.clientHeight + document.body.scrollTop;

	//and offset the left and top of the tip box
	//if we're close to an edge

	//if ((eX + tabW) > oX) {
	//	eX = eX - (tabW - (oX - eX)) - 1;	
	//}

	if ((eY + img.height) > oY) {
		eY = eY - (img.height - (oY - eY));	
	}

	//and finally the scroll offset for smaller windows

	//eX += document.body.scrollLeft;
	//eY += document.body.scrollTop;

	//alert(document.body.scrollTop);
	document.getElementById('fullImageDiv').innerHTML =  '<div style="position:relative;border:1px solid #166524;" ><img src="' + imageURL + '" title="click again to hide this image" onClick="document.getElementById(\'fullImageDiv\').style.display = \'none\';" style="border:1px solid #B2F7B2;"></div>' +														 
														 '<div style="position:absolute;left:' + (img.height > img.width?40:105) + 'px;top:2px;background-color:#66EF66;border:1px solid #166524;border-top:0px;padding-bottom:3px;padding-top:3px;font-family:trebuchet ms;background-color:#B2F7B2;font-weight:bold;width:400px;">' + name1 + '&nbsp; &nbsp; (' + name2 + ')</div>' +
														 '<div style="position:absolute;top:5px;left:0px;width:100%;text-align:right;cursor:hand;cursor:pointer;" onClick="document.getElementById(\'fullImageDiv\').style.display = \'none\';"><img title="click to hide this image" src="/img/btn/close_img.gif">&nbsp;</div>' +
														 '<div style="position:absolute;left:' + (img.height > img.width?190:250) + 'px;top:' + (img.height - 25) + '" id="img_addToCart">' + getButtonML('add to cart', 'addToCart(\'' + replace(name1, '\'', '\\\'') + '\', ' + prodList[prodInd].originalID + ', document.getElementById(\'item_' + prodList[prodInd].originalID + '\'))') + '</div>';
	document.getElementById('fullImageDiv').style.top = (eY-16) + 'px';
	document.getElementById('fullImageDiv').style.left = (img.height > img.width?258:182) + 'px';
	document.getElementById('fullImageDiv').style.width = img.width + 'px';
	document.getElementById('fullImageDiv').style.display = 'block';
	setTimeout('document.getElementById(\'click_' + enlargeMsgID + '\').innerHTML = \'click to enlarge\'', 100);

}

rnd.today=new Date();
rnd.seed=rnd.today.getTime();

function rnd() {
        rnd.seed = (rnd.seed*9301+49297) % 233280;
        return rnd.seed/(233280.0);
};

function rand(number) {
        return Math.ceil(rnd()*number);
};

function replace(string,text,by) {
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}


//************cookies

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

//************ end cookies

//######################### END UTILITIES ##########################################