// JavaScript functions widely used in many parts of the TBF web site

    var browserVersion = parseInt(navigator.appVersion)

    //--------------------
    // Code to read cookie
    //--------------------

    // get the value of a property embedded in a string;
    // this generic function will work with whatever delimiters are given

    function parsePropertyValue(text, propname, bridgechar, endchar)
    {
      var result = ""
      var start, end
      if (text) {
        start = text.indexOf(propname + bridgechar)
	if (start > -1) {
	  // to keep Navigator 2 happy, must put lengths into variables first,
	  // else adds incorrectly
	  var plen = propname.length
	  var bclen = bridgechar.length
	  start += plen + bclen
	  if (start < text.length) {	// needed for Navigator 2
	    end = text.indexOf(endchar, start)
	  } else {
	    end = -1
	  }
	  if (end == -1) {
	    end = text.length
	  } else {
	    end += 1 - endchar.length
	  }
	  result = text.substring(start, end)
	}
      }
      return result
    }
    
    // get the value of a particular cookie property

    function getCookie(propname)
    {
      if (document.cookie) {
        return parsePropertyValue(document.cookie, propname, "=", ";")
      } else {
        return null
      }
    }

    //------------------------------------------------------
    // Code to specify whether on-line ordering is enabled
    //------------------------------------------------------

    // Decide whether to enable on-line shopping functions.
    // Originally there was complex logic deciding when to
    // set this as active.

    var ordering_active
	ordering_active = true

    //------------------------------------------------------
    // Code to deal with updates to shopping cart quantities
    //------------------------------------------------------

    // some global variables

    var shopcart
    var old_shopcart

    // get value of shopcart from cookie

    function getShopcart()
    {
      // fetch value of shopcart cookie
      shopcart = getCookie("shopcart")
      if (!shopcart) shopcart=''  // don't allow shopcart to be undefined
      old_shopcart = shopcart
    }
    getShopcart()

    // get string indicating the quantity associated with a particular Cat #;
    // use blank to indicate zero

    function shopcartQuantity(catno)
    {
        var result = parsePropertyValue(shopcart, catno, ":", "|")
	if (result) {
	  return result
	} else {
	  return ""
        }
    }

    // revise the quantity of the indicated item in the shopcart string

    function setShopcart(catno, newquantity)
    {
      if (newquantity == "0") {
        newquantity = ""
      }
      if (!shopcart)
      {
        if (newquantity != "") {
	  shopcart = catno + ":" + newquantity + "|"
	}
      } else {
        var datum1 = shopcart.indexOf(catno + ":")
	if (datum1 == -1) {
	  if (newquantity != "") {
	    shopcart = shopcart + catno + ":" + newquantity + "|"
	  }
	} else {
	  datum2 = shopcart.indexOf("|", datum1)
	  if (newquantity == "") {
	    // eliminate entry for this catno
	    shopcart = shopcart.substring(0, datum1) +
	      shopcart.substring(datum2 + 1, shopcart.length)
	  } else {
	    var catnolen = catno.length  // Navigator 2 bug requires this
	    // replace old quantity with new
	    shopcart = 
	      shopcart.substring(0, datum1 + catnolen + 1) + 
	        newquantity + shopcart.substring(datum2, shopcart.length)
	  }
	}
      }
      // remember to actually set the cookie after calling this function!
    }

    // save cookie
    // zero hours means cookie is specific to browser session
    
    function saveCookie(name, value, hours)
    {
        // compute expiration time for cookie
        var exp = new Date()
        var nowPlusHours = exp.getTime() + (hours*60*60*1000)
        exp.setTime(nowPlusHours)

	// construct cookie
	    var expires = ((hours) ? ("; expires=" + exp.toGMTString()) : "")
        var cookie = name + "=" + value + expires + '; path=/'
        // now actually set the cookie
	document.cookie = cookie
    }

    // save shopcart as a cookie

    function saveShopcart()
    {
      // make the change only if shopcart value has changed;
      // if user is approving all cookies this will minimize approvals
      if (shopcart != old_shopcart) {
	     saveCookie("shopcart", shopcart, 24)
	     old_shopcart = shopcart
      }
    }

    // Deal with event that has changed quantity of an item

    function applyCatnoQuant(catno, object)
    {
      // put value in canonical form
      // (and ensure it is a number)
      var quant = parseInt(object.value,10)
      object.value = (quant ? quant : "")

      // set shopcart
      setShopcart(catno, object.value)

      // save shopcart
      saveShopcart()
    }

  // function to display a big pop-up version of photo
  function popUpBig(basename, width, height)
  {
    url = "../../gen/popups/" + basename.substring(2,5) +
             "/" + basename + ".html";
    new_window = window.open(url,
          "Window"+url.substring(1+url.lastIndexOf('/'),
	                         url.lastIndexOf('.html')),
                        "resizable,width=" + (width+30).toString() +
                        ",height=" + (height+60).toString());
    new_window.focus() // in case not really a new window, bring forward
  }
 
