var cart_enabled = true //GetCookie("cart")
var call_totals = true

function openCartHelp (arg) {
	// anchor arg in form "anchor,item"
	var args = arg.split(",")
	var	anchor = "#" + args[0]
	var cartitem = args[1]  // a number indicating the item's position in the cart table, 0 based
	var file = ""
	switch (anchor) {
		case "#code":
//			var code = eval('document.frm_code_' + cartitem + '.list_code_' + cartitem + '.value')
//			var code = eval('document.frm_code_' + cartitem + '.list_code_' + cartitem + '.options[1].value')
			var list = eval('document.frm_code_' + cartitem + '.list_code_' + cartitem)
			var code = list.options[list.selectedIndex].value
			file = "help/" + GetPrintSize(item_images[cartitem], code) + ".htm"
			window.open(file, "", 
					"screenX=50,top=50,screenY=0,left=0,height=300,width=400,scrollbars=yes,resizable=yes")
			break
		
		default:
			file = "help/index.htm"
			var x = window.open(file + anchor, "", 
					"top=50,left=0,height=400,width=500,scrollbars=yes,resizable=yes")
		
	}
}
/*
function openCart_x () {
//	location = ROOT_PATH + "cart/index.php"
	window.open (ROOT_PATH + "cart/index.php", "",
		"screenX=50,screenY=0,top=50,left=0,height=600,width=950,scrollbars=yes,status=yes,resizable=yes")
}

function PrintCartLink () {
	if (cart_enabled && CookiesAreEnabled() && BrowserSupportsCart()) {
		document.write ('<a href="javascript:AddToCart(IMAGE,print_size)">')
		document.write ('<img src="' + ROOT_PATH + 'custom/cart/cart_add.gif" name="lnk_cartaddrem" width="31" height="14" border="0" alt="Add this photo to your shopping cart"></a>&nbsp;')
		document.write ('</a>')
		document.write ('<a href="' + ROOT_PATH + 'cart/index.php" target="_blank">')
		document.write ('<img src="' + ROOT_PATH + 'custom/cart/cart_view.gif" width="31" height="14" border="0" alt="View contents of your shopping cart"></a>&nbsp;')
		document.write ('</a>')
	} else
		document.write ('&nbsp;')  // ns4 will not put background in table cell if there's no content
}

function PrintViewCart () {
	if (cart_enabled && CookiesAreEnabled() && BrowserSupportsCart()) {
		document.write ('<a href="javascript:openCart()">')
		document.write ('<img src="' + ROOT_PATH + 'custom/cart/cart_view.gif" width="31" height="14" border="0" alt="View contents of your shopping cart"></a>&nbsp;')
		document.write ('</a>')
	} else
		document.write ('&nbsp;')  // ns4 will not put background in table cell if there's no content
}
*/
function ShrinkCookie() {
	// take the item_x arrays and combine the elements into a new cart cookie
	var newcookie = ""
	for (var i in item_images) {
		newcookie += item_images[i] + "|" + item_qtys[i] + "|" + item_codes[i] + "|" + item_prices[i] + "|" + item_sizes[i] + ","
	}
//	alert (newcookie)
	return newcookie
}

function ExpandCookie(cookie) {
	// take the cart cookie and split the values into the item_x arrays
	var c = cookie.split(",")
	var items
	for (var i in c) {
		items = c[i].split("|")
		item_images[i] = items[0]
		item_qtys[i] = items[1]
		item_codes[i] = items[2]
		item_prices[i] = items[3]
		item_sizes[i] = items[4]
	}
}

function UpdateTotals() {
	var tot = 0
	var x = 0
	for (var i = 0; i < nItems; i++) {
		tot += eval("parseInt(document.frm_itemcost_" + i + ".txt_itemcost.value)")
//		if (item_codes[i] == "s" && item_qtys[i] != 0) 
//			small_prints += parseInt(item_qtys[i], 10)
	}
	document.frm_totals.subtotal.value = String(tot)
	var c = ShrinkCookie()
	SetCookie ('cart', 	c)

	if (document.frm_totals.select_shipping.value == "au")
		document.frm_totals.shipping.value = 15
	if (document.frm_totals.select_shipping.value == "os1")
		document.frm_totals.shipping.value = 20
	if (document.frm_totals.select_shipping.value == "os2")
		document.frm_totals.shipping.value = 50

	document.frm_totals.total.value = 
		parseInt(document.frm_totals.subtotal.value) + 
		parseInt(document.frm_totals.shipping.value) 
//	SetCookie ("cart_total", document.frm_totals.total.value)

	SetObjectsInnerHTML("CurrencyString", document.frm_totals.total.value, "foreign_total");

//	call_totals = false // stops recursive call back here because UpdateCurrency fires this function
//	UpdateCurrency()
//	call_totals = true
//	location.reload()
	return true
}


function UpdateQty (cartitem) {
	var list = eval('document.frm_qty_' + cartitem + '.list_qty_' + cartitem)
	item_qtys[cartitem] = list.options[list.selectedIndex].value
	var txt_itemcost = eval("document.frm_itemcost_" + cartitem + ".txt_itemcost")
	txt_itemcost.value = String((item_qtys[cartitem] * item_prices[cartitem]).toFixed(2))
	UpdateTotals()
	eval ("document.form.quantity_" + (cartitem + 1) + ".value = item_qtys[cartitem]") 
}

/*
function UpdateCode (cartitem) {
	var list = eval('document.frm_code_' + cartitem + '.list_code_' + cartitem)
	item_codes[cartitem] = list.options[list.selectedIndex].value
	var txt_unitcost = eval("document.frm_unitcost_" + cartitem + ".txt_unitcost")	
	var txt_itemcost = eval("document.frm_itemcost_" + cartitem + ".txt_itemcost")
	
	txt_unitcost.value = GetProductRetail(item_codes[cartitem])
	txt_itemcost.value = String(item_qtys[cartitem] * GetProductRetail(item_codes[cartitem]))

	var but_help = eval("document.frm_code_" + cartitem + ".but_codehelp_" + cartitem)
	if (but_help)
		but_help.alt = "What does '" + GetProductAbrev(item_codes[cartitem]) + "' mean for photo #" + item_images[cartitem] + "?"

	UpdateTotals()
}
*/

function AddRemCart (image, code) {

	if (IsInCart(image)) 
		RemFromCart (image)
	else
		AddToCart (image, document.frm_code.list_code.value)
}


function DupeItemInCart (cartitem) {
	var img
	var c = GetCartCookie() 
	c = c.replace (/,$/,"")	// get rid of trailing comma
	var items = c.split(',')
	c = ""
	for (var i in items) {	
		c += items[i] + ","
		if (i == cartitem)
			c += items[i] + ","
	} 
	SetCookie ('cart', c)
}

function RemItemFromCart (cart_item) {
	var img
	var c = GetCartCookie() 
	c = c.replace (/,$/,"")	// get rid of trailing comma
	var items = c.split(',')
	c = ""
	for (var i in items) {	
		if (i != cart_item)
			c += items[i] + ","
	} 
	SetCookie ('cart', c)
}

function RemFromCart (cart_item) {
	var img
	var c = GetCartCookie() 
	c = c.replace (/,$/,"")	// get rid of trailing comma
	var items = c.split(',')
	c = ""
	for (var i in items) {	
		img = items[i].substr(0,5)
		if (img != cart_item) {
			c += items[i] + ","
		}
	} 
	SetCookie ('cart', c)
}

function AddImageToCart (image, code) {
	if (code == "") code = "a"
//alert (code)
	var c = GetCartCookie()
	if (c != null) c = c.replace (/\|undefined\|undefined\|undefined\|undefined,/,"")	

//	if (c == ".undefined.undefined.undefined.undefined," || c == null) c = ""
	SetCookie ('cart', c + image + '|1|' + code + ",", exp)
   	var objElement = document.getElementById("layerAddedtoCart");
	objElement.style.visibility = "visible"
	objElement.style.left = xMousePos
	objElement.style.top = yMousePos
//	alert (xMousePos)
	setTimeout("ClosePopupMessage()", 2000)
}

function ClosePopupMessage () {
  	var obj = document.getElementById("layerAddedtoCart");
	obj.style.visibility = "hidden"
}

function OpenCartPopupMessage() {
   	var obj = document.getElementById("layerAddedtoCart");
	obj.style.visibility = "visible"
	obj.style.left = xMousePos
	obj.style.top = yMousePos
	setTimeout("ClosePopupMessage()", 2000)
}

function AddFolioToCart (cookie_val) {
	var c = GetCartCookie()
	if (c != null) c = c.replace (/\|undefined\|undefined\|undefined\|undefined,/,"")	
	if (c == ".undefined.undefined.undefined.undefined," || c == null) c = ""
	c = c.replace (/\|undefined\|undefined\|undefined\|undefined,/,"")
	SetCookie ('cart', c + cookie_val + ",", exp)
//alert (c + cookie_val + ",")
	OpenCartPopupMessage()
}

function AddMyFolioToCart () {
	var x = all_images_in_folio.split(",")
	var s = ""
	for (var i = 0; i < x.length-1; i++) {
		if (x[i] != "") {
			s += "pp4-" + x[i] + "|1|pp4|" + GetProductRetail("pp4") + "|A4,";
		}
	}
	var c = GetCartCookie()
//	alert (s)
//	s = s.replace (/\|undefined\|undefined\|undefined\|undefined,/,"")

//	if (c != null) c = c.replace (/.undefined.undefined.undefined.undefined,/,"")	
//	if (c == ".undefined.undefined.undefined.undefined," || c == null) c = ""
	SetCookie ('cart', c + s , exp)
	OpenCartPopupMessage()
}


function GetCartCookie () {
//return "21124|1|c|79|16x4"
	var c = GetCookie('cart') 
	if (c == null) return ""
	c = c.replace (/\|undefined\|undefined\|undefined\|undefined,/,"")
	return c
}

function IsInCart (image) {
	var	c = GetCartCookie()
	if (c == null) return false
	c = c.split(',')
	for (var i in c) {	
		if (GetCartImage(c[i]) == image)	return true
	} 
}

function GetCartImage (cookie) {
	var c = cookie.split("|")
	return c[0]
}

function GetExpandedItemCode (cart_item) {
	var c = cart_item.split("|")
	return products[c[1]]
}

function GetItemCode (cart_item) {
	var c = cart_item.split("|")
	return c[1]
}

function GetCartQty (cart_item) {
	var c = cart_item.split("|")
	return c[2]
}
