/******************************************************************/
/*   This is the routine to do the display of the shopping cart   */
/******************************************************************/

var parm_in, parm_used, return_to, return_tip
var discount_base, discount_invalid
var line_total, item_total, discount_total, sub_total, snh_total
var i, pckg, addon, num_8x10, num_4x5, code1, code2, code2i, balanced

var today = new Date()


if (location.search) {
  parm_in = location.search.substring(1)
  parm_used = parm_in.split("&")
  return_to = parm_used[0] + ".html?" + parm_used[1]
  return_tip = "my event gallery"
}
else {
  parm_in = ""
  return_to = "../index.html"
  return_tip = "Home"
}

get_sc_cookies()
if (num_items == 0) { /*** shopping cart empty ***/
  document.write("<p>Your shopping cart is empty.<br /><br />Your browser must be set up to accept cookies in order for items to be saved. Items in the shopping cart are saved for 30 days.<br /><br />Please return to your gallery and select the items desired.</p>")
}
else { /*** show items in cart ***/
  item_total = 0
  discount_base = 0
  pckg = false
  addon = false
  num_8x10 = 0
  num_4x5 = 0

  document.write("<table class=\"cart\">")
  document.write("<tr><th class=\"img\"></th><th class=\"name\">Name</th><th class=\"qty\">Qty</th><th class=\"desc\">Description</th><th>Price</th><th>Total</th><th></th></tr>")
  for (i=0; i < num_items; i++) { /* each item */
    line_total = item_data[i].qty * item_data[i].price
    item_total += line_total
    if (discount_code != "") { /* add up amount to discount, if applicable */
      if (item_data[i].gallery == discount_gallery || discount_gallery == "") {
        discount_base += line_total
      }
    } /* end of discount check */
    sub_write_item_line()
    sub_count_codes()
  } /* end of item */

  sub_do_totals()
  document.write("</table>")
  sub_discount_input()

  if (discount_invalid) {
    document.write("<p class=error>Discount code " + discount_code + " has expired or is not valid for this purchase.</p>")
    delete_cookie("dlds")
  }
  sub_check_codes()
} /* end of showing shopping cart */

sub_do_buttons()


/***********************/
/*     Subroutines     */
/***********************/

// Display an item in the shopping cart

function sub_write_item_line() {

  document.write("<tr><td class=\"img\">")
  if (item_data[i].image == "") {
    document.write("no picture<br />included")
    }
  else {
    document.write("<img alt=\"\" src=\"../gallery/" + item_data[i].gallery + "/thumbnails/" + item_data[i].image + ".jpg\" />")
  }
  document.write("</td><td class=\"name\">" + item_data[i].gallery + "<br /><br />" + item_data[i].image + "</td>")
  document.write("<td class=\"qty\">" + item_data[i].qty + "</td>")
  document.write("<td class=\"desc\">" + item_data[i].descr)
  if (item_data[i].option != "") {
    document.write("<br>&nbsp;&nbsp;&nbsp;Option: " + item_data[i].option)
  }
  document.write("</td>")
  document.write("<td>" + display_dollars(item_data[i].price) + "</td>")
  document.write("<td>" + display_dollars(line_total) + "</td>")
  document.write("<td><a href=\"javascript:add_one_item(" + i + ")\" title=\"Add 1 to the quantity of this item\">add 1</a> <a href=\"javascript:sub_one_item(" + i + ")\" title=\"Subtract 1 from the quantity of this item\">subtract 1</a> <a href=\"javascript:remove_item(" + i + ")\"  title=\"Remove this item\">remove</a></td>")
  document.write("</tr>")

}


// Count package add-ons and pictures in folios

function sub_count_codes() {

  code1 = item_data[i].code.charAt(0)
  code2 = item_data[i].code.charAt(1)
  if (code2 == "J") {
    code2i = 20
  }
  else {
    code2i = parseInt(code2)
  }

  if (code1 == "P") { /*** package item ***/
    pckg = true
  }
  else if (code1 == "" || code1 == " " || code1 == "C" || code1 == "G" || code1 == "N") { /*** not a folio item ***/
    if (code2 == "P") { /*** package add-on ***/
      addon = true
    }
  }
  else if (code1 == "X") { /*** this is an added 4X5 ***/
    num_4x5 -= item_data[i].qty
  }
  else if (code1 == "0") { /*** this is a 4X5 only folio ***/
    num_4x5 += item_data[i].qty * (code2i - 1)
  }
  else {
    if (code2 == "X") { /*** this is an added 8x10 ***/
      num_8x10 -= item_data[i].qty
    }
    else { /*** this is an 8x10 included folio ***/
      num_8x10 += item_data[i].qty * (parseInt(code1) - 1)
      num_4x5 += item_data[i].qty * code2i
    }
  }

}


// Compute the totals

function sub_do_totals () {

  if (discount_code == "") { /* determine discount, if entered */
    discount_invalid = false
    discount_total = 0
    snh_total = snh_amount
  }
  else { /* compute discount */

    if (item_total < discount_purch || discount_expires < today) {
      discount_invalid = true /* not valid for this purchase */
      discount_total = 0
      snh_total = snh_amount
    }
    else {
      discount_invalid = false
      discount_total = discount_base * discount_pct
      if (discount_option == "+") { /* add amount */
        discount_total += parseFloat(discount_amt)
      }
      else {
        if (discount_total > discount_amt) { /* maximum allowed */
          discount_total = discount_amt
        }
      }
      if (discount_total > item_total) { /* only up to purchase */
        discount_total = item_total
      }
      discount_total = 0 - discount_total /* show it as negative */
      snh_total = snh_amount - discount_snh
    }
  }

  itmttl_disp = display_dollars(item_total)
  dscnt_disp = display_dollars(discount_total)
  sub_total = item_total + parseFloat(dscnt_disp)
  subttl_disp = display_dollars(sub_total)
  est_tax_total = est_tax_rate * sub_total
  est_tax_disp = display_dollars(est_tax_total)
  snh_disp = display_dollars(snh_total)
  grand_notax = display_dollars(sub_total + snh_total)
  grand_withtax = display_dollars(sub_total + parseFloat(est_tax_disp) + snh_total)

  document.write("<tr><td></td><td></td><td></td><td></td><td></td><td><hr /></td><td></td></tr>")
  document.write("<tr><td></td><td></td><td></td><td>Item Total</td><td></td><td>" + itmttl_disp + "</td><td></td></tr>")
  if (discount_total < 0) {
    document.write("<tr><td></td><td></td><td></td><td>Discount: " + discount_code + "</td><td></td><td>" + dscnt_disp + "</td><td></td></tr>")
    document.write("<tr><td></td><td></td><td></td><td></td><td></td><td><hr /></td><td></td></tr>")
    document.write("<tr><td></td><td></td><td></td><td>Subtotal</td><td></td><td>" + subttl_disp + "</td><td></td></tr>")
  }
  document.write("<tr><td></td><td></td><td></td><td>Estimated Tax*</td><td></td><td>" + est_tax_disp + "</td><td></td></tr>")
  document.write("<tr><td></td><td></td><td></td><td>Shipping and Handling</td><td></td><td>" + snh_disp + "</td><td></td></tr>")
  document.write("<tr><td></td><td></td><td></td><td></td><td></td><td><hr /></td><td></td></tr>")
  document.write("<tr><td></td><td></td><td></td><td>Estimated Total with Tax*</td><td></td><td>" + grand_withtax + "</td><td></td></tr>")
  document.write("<tr><td></td><td></td><td></td><td>Total without Tax</td><td></td><td>" + grand_notax + "</td><td></td></tr>")

}


// Apply a discount code

function sub_discount_input() {

  document.write("<br /><br />")
  document.write("<form id=\"enter_discount\" method=\"post\" action=\"../scripts/discount.php\">")
  document.write("Use discount code: <input id=\"disccode\" type=\"text\" name=\"disccode\" size=\"8\" />&nbsp;&nbsp;&nbsp;<button type=\"submit\" title=\"Apply entered discount code\">Apply</button>")
  document.write("<input id=\"comefrom\" type=\"hidden\" name=\"comefrom\" value=\"" + parm_in + "\" />")
  document.write("</form>")

}


// Check for package add-ons and folios filled correctly

function sub_check_codes() {

  balanced = true
  if (pckg == false && addon == true) { /* package add-ons without a package */
    balanced = false
    document.write("<p class=error>Note: you have selected package add-on(s) without also selecting a package. Please return to your gallery and select a package.</p>")
  }
  if (num_8x10 != 0 || num_4x5 != 0) { /*** folios not balanced ***/
    balanced = false
    document.write("<p class=error>Note: your selected folios are not full or you have removed folios without removing the added pictures.")
    if (num_8x10 > 0 || num_4x5 > 0) { /* folios aren't full */
      document.write(" Please return to your gallery and select:")
      if (num_8x10 > 0) {
        document.write(" " + num_8x10 + " additional 8X10\'s")
      }
      if (num_4x5 > 0) {
        document.write(" " + num_4x5 + " additional 4X5\'s")
      }
      document.write(" to be added to a folio.")
    }
    if (num_8x10 < 0 || num_4x5 < 0) { /* folio not selected */
      document.write(" Please remove")
      if (num_8x10 < 0) {
        num_8x10 = 0 - num_8x10
        document.write(" " + num_8x10 + " additional 8X10\'s")
      }
      if (num_4x5 < 0) {
        num_4x5 = 0 - num_4x5
        document.write(" " + num_4x5 + " additional 4X5\'s")
      }
      document.write(".")
    }
    document.write("</p>")
  }

}


// Write navigation buttons

function sub_do_buttons() {

  document.write("<p class=\"center\"")
  document.write("<br /><button title=\"Return to " + return_tip + "\" onclick=\"location.href='../gallery/" + return_to + "'\">Return</button>") /*** return to where came from (gallery or home) ***/
  if (num_items == 0) { /*** the shopping cart is empty ***/
    if (return_tip == "Home") { /*** if came from home, offer to select a gallery ***/
      document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button title=\"Select a gallery\" onclick=\"location.href='../gallery/index.html'\">Select Gallery</button></p>")
    }
  }
  else { /*** there are items in the shopping cart ***/
    if (balanced) { /* don't need any more folio items */
      document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button title=\"Start check out process\" onclick=\"start_checkout()\">Check Out</button>")
    }
    document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button title=\"Empty the shopping cart\" onclick=\"empty_cart()\">Empty Cart</button></p>")
    document.write("<p><br />*Tax is only added for sales in Oklahoma. This figure is an estimate and may change slightly, depending on your local tax rate.</p>")
  }

}
