/*************************************************************************/
/*   These are the functions to process the items in the shopping cart   */
/*************************************************************************/

var num_sc, num_items
var discount_code, discount_gallery, discount_pct, discount_option
var discount_amt, discount_purch, discount_snh, discount_expires
var est_tax_total, grand_total, grand_notax, grand_withtax
var itmttl_disp, dscnt_disp, est_tax_disp, subttl_disp, snh_disp

var item_data = new Array(1)


// Read the shopping cart cookies (dlsc#'s and dlds)
//    and delete dltl cookie (only exists if we canceled out of a checkout)

function get_sc_cookies() {

  var all_cookies, cookie_pair, shopcart_cookie, discount_cookie, c_variable, c_object, i
  var cookie_array = new Array(1)

  discount_cookie = ""
  all_cookies = document.cookie.split("; ") /* look for cookies */
  for (i = 0; i < all_cookies.length; i++) {
    cookie_pair = all_cookies[i].split("=")
    if (cookie_pair[0].substr(0,4) == "dlsc") {
      cookie_array[cookie_pair[0].substr(4,1)] = unescape(cookie_pair[1])
    }
    else if (cookie_pair[0] == "dlds") {
      discount_cookie = unescape(cookie_pair[1])
    }
    else if (cookie_pair[0] == "dltl") {
      delete_cookie("dltl")
    }
  } /* end of looking for cookies */

  shopcart_cookie = cookie_array.join("") /* parse dlsc# cookies */
  if (shopcart_cookie == "") {
    num_sc = 0
    num_items = 0
  }
  else {
    c_variable = shopcart_cookie.split("|")
    num_sc = parseInt(c_variable[0])
    num_items = parseInt(c_variable[1])
    for (i=0; i < num_items; i++) {
      c_object = c_variable[i+2].split("^")
      item_data[i] = new set_item_data(c_object[0], c_object[1], c_object[2], c_object[3], c_object[4], c_object[5], c_object[6])
    }
    if (discount_cookie == "") { /* parse dlds cookie */
      discount_code = ""
      discount_snh = 0
    }
    else {
      c_object = discount_cookie.split("^")
      discount_code = c_object[0]
      discount_gallery = c_object[1]
      discount_pct = c_object[2]
      discount_option = c_object[3]
      discount_amt = c_object[4]
      discount_purch = c_object[5]
      discount_snh = c_object[6]
      discount_expires = new Date(c_object[7].substr(0, 4), c_object[7].substr(5, 2) - 1, c_object[7].substr(8, 2))
    }
  }

}


// create one instance of an item in the shopping cart

function set_item_data(gal, img, qty, dsc, prc, cod, opt) {

  this.gallery = gal
  this.image = img
  this.qty = parseInt(qty)
  this.descr = dsc
  this.price = parseFloat(prc)
  this.code = cod
  this.option = opt

}


// Write or delete the shopcart (persistent) cookie (dlsc#'s)

//   Input:  
//     expire_days - number of days to keep cookie

function put_shopcart_cookie(expire_days) {

  var new_sc, new_items, i, j, item_value, discount_cookie, expire_date, expire_adds, cookie_domain
  var cookie_array = new Array(1)

  new_sc = 0
  cookie_array[new_sc] = ""
  if (expire_days <= 0) {
    new_items = 0
    discount_code = ""
  }
  else {
    new_items = num_items
    for (i=0; i<num_items; i++) {
      if (item_data[i].qty > 0) {
        item_value = escape("|" + item_data[i].gallery + "^" + item_data[i].image + "^" + item_data[i].qty + "^" + item_data[i].descr + "^" + item_data[i].price + "^" + item_data[i].code + "^" + item_data[i].option)
        if (cookie_array[new_sc].length + item_value.length > 3800) {
          new_sc++
          cookie_array[new_sc] = ""
        }
        cookie_array[new_sc] += item_value
      }
      else {
        new_items--
      }
    }
  }

  if (new_items == 0) {
    expire_adds = -1 * 24 * 60 * 60 * 1000
    discount_code = ""
  }
  else {
    expire_adds = expire_days * 24 * 60 * 60 * 1000
  }
  if (who_am_i == "test") {
    cookie_domain = ""
  }
  else {
    cookie_domain = "; domain=.igetthepicture.biz"
  }

  num_items = new_items
  expire_date = new Date()
  expire_date.setTime(expire_date.getTime() + expire_adds)

  cookie_array[0] = new_sc + "|" + num_items + cookie_array[0]
  for (i=0; i <= new_sc; i++) {
    document.cookie = "dlsc" + i + "=" + cookie_array[i] + "; expires=" + expire_date.toGMTString() + cookie_domain + "; path=/"
  }

  for (i=i; i < num_sc; i++) {
    delete_cookie("dlsc" + i)
  }
  num_sc = new_sc

  if (discount_code == "") {
    delete_cookie("dlds")
  }

}


// Add one to the quantity of the current item

function add_one_item(which) {

  item_data[which].qty += 1
  put_shopcart_cookie(30)
  location.reload(false)

}


// Subtract one from the quantity of the current item

function sub_one_item(which) {

  item_data[which].qty -= 1
  put_shopcart_cookie(30)
  location.reload(false)

}


// Delete the current item

function remove_item(which) {

  item_data[which].qty = 0
  put_shopcart_cookie(30)
  location.reload(false)

}


// Empty the shopping cart

function empty_cart() {

  put_shopcart_cookie(0)
  location.reload(false)

}


// Start the checkout procedure

function start_checkout() {

  var new_link, code
  var nowms = new Date()

  put_totals_cookie()

  if (who_am_i == "test") {
    new_link = ""
  }
  else {
    new_link = "https://"
    if (who_am_i == "cert") {
      new_link += secure_server
    }
    else {
      new_link += "www.igetthepicture.biz"
    }
    new_link += "/shopcart/"
  }
  code = nowms.getTime()
  new_link += "checkout.html?" + code
  document.location = new_link

}


// Write the totals (session) cookie (dltl) for use by checkout


function put_totals_cookie() {

  var cookie_value, cookie_domain

  cookie_value = itmttl_disp + "|" + dscnt_disp + "|" + subttl_disp + "|" + est_tax_disp + "|" + snh_disp + "|" + grand_notax + "|" + grand_withtax + "|" + discount_code + "|" + discount_snh
  if (who_am_i == "test") {
    cookie_domain = ""
  }
  else {
    cookie_domain = "; domain=.igetthepicture.biz"
  }
  document.cookie = "dltl=" + cookie_value + cookie_domain + "; path=/"

}


// Round numbers to two decimal places and pad with zeros

//   Input:
//     num_in - decimal number to round/pad to two decimal places
//   Output:
//     num_out - input number in string format

function display_dollars(num_in) {

  var round_num, num_out, dec_loc, pad_zero, i

  round_num = Math.round(num_in * 100) / 100
  num_out = round_num.toString()

  dec_loc = num_out.indexOf(".")
  if (dec_loc == -1) { /* no decimal point; add one */
    num_out += "."
    dec_loc = num_out.length - 1
  }

  pad_zero = 2 - (num_out.length - dec_loc - 1)
  if (pad_zero > 0) { /* pad to two decimal places */
    for (i = 1; i <= pad_zero; i++) {
      num_out += "0"
    }
  }

  return num_out

}


// Delete a cookie

//   Input:
//     cookie_name - name of the cookie to delete

function delete_cookie (cookie_name) {

  var expire_date, cookie_domain

  if (who_am_i == "test") {
    cookie_domain = ""
  }
  else {
    cookie_domain = "; domain=.igetthepicture.biz"
  }

  expire_date = new Date()
  expire_date.setTime(1)
  document.cookie = cookie_name + "=\"\"; expires=" + expire_date.toGMTString() + cookie_domain + "; path=/"
}


// Switch to an https (secure) link, certificate only (maint)

function switch_https(some_link) {

  if (who_am_i != "test") {
    some_link.href = "https://igetthepicture.powweb.com" + some_link.href.substr(some_link.href.indexOf("/", 8))
  }

}


// Switch back to an http (unsecure) link

function switch_http(some_link) {

  if (who_am_i != "test") {
    some_link.href = "http://www.igetthepicture.biz" + some_link.href.substr(some_link.href.indexOf("/", 8))
  }

}


// go to the first page of the shopping cart

function go_to_shopcart() {

  if (who_am_i == "test") {
    location.href = "index.html"
  }
  else {
    location.href = "http://www.igetthepicture.biz/shopcart/index.html"
  }

}
