// Requires cookies.js

var fieldDelim = '|'
var cartFields = ['Header','PhotoNo','Size','ExhibitionNo','Quantity','Microthumb']
var sizeStr = ['4x6','4x6 borderless','5x7','5x7 borderless','8x10','8x10 borderless','8.5x11','11x14','11x17','13x19','17x25','Low res 1024x768','High resolution file']
var sizeStrCode = ['4x6','4x6B','5x7','5x7B','8x10','8x10B','8.5x11','11x14','11x17','13x19','17x25','LowRes','HighRes']
var cartPath = '/'
var cartDomain = 'www.hickingbotham.com'

function decodeCart(rows,cookie) {
 var fields = cookie.split('|')
 for (var i = 0; i < fields.length; i++) {
  xWrite(fields[i] + ' * ')
  if ((i + 1) % cartFields.length == 0) xWriteLn('')
 }
}

function checkCookieEnabled() {
 if (!window.navigator.cookieEnabled) {
  alert("Shopping cart requires cookies to be enabled.")
  return false
 }
 else return true
}

function cartCookie(name,value) {
 SetCookie(name,value,null,cartPath,cartDomain)
}

// Use quite to suppress confirmation

function emptyCart(quite) {
//  alert(quite)
  if ((!quite) && (!confirm('Empty cart'))) return false
  DeleteCookie('OrderRows',cartPath,cartDomain);
  DeleteCookie('Cart',cartPath,cartDomain);
  DeleteCookie('Comments',cartPath,cartDomain);
  return true
}

function viewCart() {
 if (!checkCookieEnabled()) return
 var rows = GetCookie('OrderRows')
 if (rows) {
  var s = new String(location)
  if (s.indexOf('gallery') >= 0) cartCookie("Shopping",s)
  location.href='http://www.hickingbotham.com/shoppingcart.htm'
 }
 else alert('Shopping cart is empty')
}

function arrayIndexOf(value,a) {
 for (var i = 0; i < a.length; i++) if (a[i] == value) return i
 return -1
}

function getCartField(name,row,cart) {
 var col = arrayIndexOf(name,cartFields)
 if (col >= 0) return cart[row * cartFields.length + col]
}

function addToCart(header,photoNo,size,exhibitionNo,quantity,microthumb) {
 if (!checkCookieEnabled()) return false
 var rows = GetCookie('OrderRows');
 if (rows) rows++
 else rows = 1
 cartCookie('OrderRows',rows);
 rows--
 if (rows==0) cookie = ''
 else cookie = GetCookie('Cart') + fieldDelim
 cookie += header + fieldDelim +
           photoNo + fieldDelim +
           size + fieldDelim +
           exhibitionNo + fieldDelim +
           quantity + fieldDelim +
           microthumb;
// alert('2 ' + cookie)
 cartCookie('Cart',cookie);
 return true
}

function itemInCart(item) {
 var rows = GetCookie('OrderRows');
 if (!rows) return false
 cookie = GetCookie('Cart') + fieldDelim
 return (cookie.indexOf(item + fieldDelim) >= 0)
}

function isBorderless(s) {
 return (s.indexOf('B') != -1)
}

function removeBorderless(s) {
 if (isBorderless(s)) return s.substr(0,s.length - 1)
 else return s
}

function unitPrice(s) {
 switch (removeBorderless(s)) {
  case "4x6"       : return 12.00
  case "5x7"       : return 15.00
  case "8x10"      : return 24.00
  case "8.5x11"    : return 28.00
  case "11x14"     : return 39.00
  case "11x17"     : return 49.00
  case "13x19"     : return 65.00
  case "17x25"     : return 95.00
  case "LowRes"    : return 15.00
  case "HighRes"   : return 75.00
  default          : return 0
 }
}

function shippingCost(s,q) {
 if (q == 0)
  return 0.00
 switch (removeBorderless(s)) {
  case "4x6"    : return 2.00
  case "5x7"    : return 2.00
  case "8x10"   : return 2.00
  case "8.5x11" : return 2.00
  case "11x14"  : return 4.00
  case "11x17"  : return 4.00
  case "13x19"  : return 4.00
  case "17x25"  : return 4.00
  default       : return 0
 }
}

function calcShipping(shipping,s,q) {
 var cost = shippingCost(s,q)
 return (cost > shipping) ? cost : shipping
}

function Round2(value) {
 return Math.round(value * 100) / 100
}

function format (expr, decplaces) {
 var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
 while (str.length <= decplaces) {
  str = "0" + str
 }
 var decpoint = str.length - decplaces
 return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function Pricing(size,quantity) {
 var price = unitPrice(size)
 if (quantity <= 1)
  return Round2(price * quantity)  // This includes zero quantity
 var total = price
 if (quantity <= 2)
  return Round2(total + (price * 0.8) * (quantity - 1))
 total += price * 0.8
 return Round2(total + price * 0.6 * (quantity - 2))
}

function getChildByAttribute(elem,attrName,attrValue) {
 var child = elem.firstChild;
 var match = null;
 alert(attrName + ' ' + attrValue)
 while ((child) && (!match)) {
  if (child.nodeName) alert(child.nodeName + ' ' + (child.getAttribute ? child.getAttribute(attrName) : '**NA**'));
  if ((child.getAttribute) && (child.getAttribute(attrName) == attrValue)) match = child
  else
   if (child.firstChild) {
    match = getChildByAttribute(child,attrName,attrValue);
  }
  child = child.nextSibling;
 }
 return match;
}

// NodeName, e.g. IMG, INPUT, TD
function getChildByNodeName(elem,name) {
 var child = elem.firstChild;
 var match = null;
 while ((child) && (!match)) {
  if (child.nodeName == name) match = child
  else
   if (child.firstChild) {
    match = getChildByNodeName(child,name);
  }
//  alert(child.nodeName)
  child = child.nextSibling;
 }
 return match;
}


