//
// JavaScript and Cookie Based Ecommerce Shopping Basket System
// Copyright ©2000-2006 Sandford Multimedia
// enquiries@sandfordmultimedia.com
//

ProductType = "N/A"
ProductCode = "N/A"
numEntries = 20
numWish = 0
WishProductTypes = new Array()
WishProductCodes = new Array()

function get_wish() {
  if (Get_Cookie("newWish")) {
    var str = document.location.search
    param_len = 0
    if (str != '') {
      parse_wish_parameters(str, true)
    }
    Delete_Cookie("newWish", "/")
  }
}

function set_wish() {
  var numWishCookies = 0
  var numNewWish = 0
  var footwearMsg = ''
  if (Get_Cookie("numWish")) {
    numWishCookies = Get_Cookie("numWish")
  }
  if (document.forms["AddWish"].elements["ProductType"].value == "Footwear") {
    var footwearMsg = document.forms["AddWish"].elements["ProductCode"].value
    numNewWish += 1
  }
  if (parseInt(numWishCookies)+numNewWish > numEntries) {
    alert("The Booted and Suited wish list can only hold " + numEntries + " items, \nthis addition will take the number to " + (parseInt(numWishCookies)+numNewWish) + ". Please go to the wish list \nand remove some items if you wish to add another.")
	return false
  }
  if (numNewWish < 1) {
    alert("Please select some items to add to the wish list.")
	return false
  }
	
  var todays_date = new Date()
  // 1 minute from now
  var expires_date = new Date(todays_date.getTime() + (1 * 60000))
  Set_Cookie("newWish", 1, expires_date, "/")
  if (Get_Cookie("newWish")) {
	var msg = ''
	msg += footwearMsg + '\n'
//	alert(msg)
    return true
  }
  else {
    alert("Cookies must be enabled in order to use The Booted and Suited wish list.")
    return false
  }
}

function parse_wish_parameters(str, plus) {
    param_len = str.length
    var ampsep = -1
    var tstr = str.substring(1,str.length)
    var validnames = "ProductType,ProductCode" 
    while (ampsep != tstr.length) {
      tstr = tstr.substring(ampsep+1,tstr.length)
      ampsep = tstr.indexOf("&")
      if (ampsep == -1) {ampsep = tstr.length}
      param = tstr.substring(0,ampsep)
      eqsep = param.indexOf("=")
      pname = param.substring(0,eqsep)
      if (validnames.indexOf(pname) != -1) {
        pvalue = param.substring(eqsep+1,param.length)
        if (plus) {pvalue = conv_plus(pvalue)}
        pvalue = unescape(pvalue)
        eval(pname+'='+'"'+pvalue+'"')
      }
    }
    numWish += 1
    WishProductTypes[numWish] = ProductType
    WishProductCodes[numWish] = ProductCode
}

function store_wish() {
  var todays_date = new Date()
  // 30 days hours from now
  var expires_date = new Date(todays_date.getTime() + (30 * 24 * 3600000))
  if (numWish>0) {
    for (i=1;i<numWish+1;i++) {
      var PackedItem = "ProductType=" + escape(WishProductTypes[i])
      PackedItem += "&" + "ProductCode=" + escape(WishProductCodes[i])
      Set_Cookie("Wish_" + i, PackedItem, expires_date, "/")
    }
    Set_Cookie("numWish", numWish, expires_date, "/")
  }
}

function get_new_wish() {
  var numWishCookies = 0
  if (Get_Cookie("numWish")) {
    numWishCookies = Get_Cookie("numWish")
  }
  if (numWishCookies>0) {
    iend = parseInt(numWishCookies)+1
    for (i=1;i<iend;i++) {
      if (Get_Cookie("Wish_" + i)) {
        var PackedWish = Get_Cookie("Wish_" + i)
        param_len = 0
        parse_wish_parameters("?" + PackedWish, false)
      }
    }
  }
}

function empty_wish() {
  var todays_date = new Date()
  // 2 hours from now
  var expires_date = new Date(todays_date.getTime() + (2 * 3600000))
  if (numWish>0) {
    for (i=1;i<numWish+1;i++) {
      Delete_Cookie("Wish_" + i, "/")
    }
  }
  Delete_Cookie("numWish", "/")
}

wish_init=true