// cart.js - display-page boilerplate for order-form fill-in list
// requires prior inclusion of catalog.js

// v0.01 20070331 rjd:major changes for mephisto
// v0.00 20050629 rjd:initial edit

var dhtml = true;	// set false if browser can't DHTML
var listColor = "<FONT color='#660099'><B>";
var xListColor = "</B></FONT>";

var isNetscape = false;
var unhideDisplayStyle = "block";

function checkForNetscape()
{
var ua;

  ua = window.navigator.userAgent.toLowerCase();
  if (ua.indexOf("netscape") != -1 || ua.indexOf("gecko") != -1) {
    isNetscape = true;
    unhideDisplayStyle = "";	// kludge
  }
}

function isEmpty(str) {
  if (str.length == 0 || str == "&nbsp;")
    return (true);
  return (isBlank(str));
}

function isBlank(str)
{
var i, c;

  for (i = 0; i < str.length; i++) {
    c = str.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t') && (c.charCodeAt(0) != 160))
      return (false);
  }
  return (true);
}

// fetch Item object of a model alias
function getItem(modAlias)
{
var ii;
var item;

    for (ii=0; ii < catalog.length; ii++) {
    	item = catalog[ii];
    	if (item.modAlias == modAlias)
    	    return(item);
    }
    return(0);
}

//Matt's cookie reader
function getCookie(name) {
  var prefix = name + "=";
  var cookieStartIndex = document.cookie.indexOf(prefix);
  if (cookieStartIndex == -1)
    return "";
  var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
  if (cookieEndIndex == -1)
    cookieEndIndex = document.cookie.length;
  return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

// Fetch current list
function getCookieList()
{
    //alert(getCookie("cart"));
    return(getCookie("cart").split(":"));
}

function setCookieFromList(cookieList)
{
var newValue = cookieList.join(":");
    document.cookie = "cart=" + escape(newValue) + "; path=/";
}

function listIndex(modAlias, cookieList)
{
var ii;
    for (ii=0; ii < cookieList.length; ii++) {
    	if (cookieList[ii] == modAlias)
    	    return(ii);
    }
    return(-1);
}

// Convert button to 'remove'
function setRemove(element)
{
    element.src = "remove.gif";
    element.alt = "Remove";
    element.title = "Remove this shoe from your cart";
}

// Convert button to 'add'
function setAdd(element)
{
    element.src = "add.gif";
    element.alt = "Add";
    element.title = "Add this shoe to your cart and it will be filled into the order form for you automatically.  (You can remove it later if you wish.)  Your current cart is displayed below the order form button.";
}

// Extract model alias from button name
function getModAlias(element)
{
var prefix = "but_";
    return(element.id.substring(prefix.length));
}

// Add this shoe to his list
function addShoe(element)
{
    if (!dhtml) {
    	alert("We're sorry.  Your browser does not appear to support one or more of the features required for this function.  Please write down the model numbers of the shoes you are interested in and enter them manually into the order form.");
    	return;
    }
    if (element.src.indexOf("remove.gif") >= 0)
    	return(removeShoe(element));
    var modAlias = getModAlias(element);
    var cookieList = getCookieList();
    var index = listIndex(modAlias, cookieList);
    if (index >= 0)
    	return;		// paranoia, it's already there
    cookieList[cookieList.length] = modAlias;
    setCookieFromList(cookieList);
    // now see if it took
    cookieList = getCookieList();
    index = listIndex(modAlias, cookieList);
    if (index < 0) {
    	alert("We're sorry.  You'll have to enable cookies in your browser for this function to work.  Otherwise, just write down the model numbers of the shoes your interested in and enter them manually into the order form.");
    	return;
    }
    setRemove(element);
    setList(false);
}

//Remove this shoe from his list
function removeShoe(element)
{
    var modAlias = getModAlias(element);
    var cookieList = getCookieList();
    var index = listIndex(modAlias, cookieList);
    if (index < 0)
    	return;		// paranoia, it's already gone
    cookieList.splice(index, 1);
    setCookieFromList(cookieList);
    // now see if it took
    cookieList = getCookieList();
    index = listIndex(modAlias, cookieList);
    if (index >= 0) {
    	alert("We're sorry.  You'll have to enable cookies in your browser for this function to work.  Otherwise, just write down the model numbers of the shoes your interested in and enter them manually into the order form.");
    	return;
    }
    setAdd(element);
    setList(false);
}

// Display current list and optionally convert add buttons to removes
function setList(buttons)
{
var ii;
var listHTML;
    // assume cookie list will be empty
    var listElement = document.getElementById("list");
    listElement.innerHTML = listColor + "Your cart is empty<br>&nbsp;" + xListColor;
    var cookieList = getCookieList();
    for (ii=0; ii < cookieList.length; ii++) {
        var modAlias = cookieList[ii];
        if (buttons) {
	    var buttonElement = document.getElementById("but_" + modAlias);
	    if (buttonElement)
		setRemove(buttonElement);
    	}
    	var item = getItem(modAlias);
    	if (item) {
    	    if (!listHTML)
    	        listHTML = "<TABLE border='0'>\n<tr><td colspan='3'>"
    	          + listColor + "Your cart contains:"
    	          + xListColor + "</td></tr>\n";
    	    listHTML += "<tr><td valign='top' nowrap>"
    	      + listColor + item.man + xListColor
    	      + "&nbsp;</td><td nowrap valign='top'>"
    	      + listColor + item.model + xListColor
    	      + "&nbsp;&nbsp;</td><td valign='top' style='padding-right:15px'>"
    	      + listColor + item.descr + xListColor
    	      + "</td>\n";
    	    var onPage = (window.location.pathname.indexOf(item.page) >= 0);
    	    if (!onPage)
    	    	listHTML += "<td nowrap valign='top'>[<a href='" + item.page
    	    	  + "'>Go there</a>]</td></tr>\n";
	    else
	    	listHTML += "<td>&nbsp;</td></tr>\n";
    	}
    }
    if (listHTML) {
    	listHTML += "</TABLE>&nbsp;<br>\n";
    	listElement.innerHTML = listHTML;
    }
}

// Adjust our display to reflect entry cookies
function setDisplay()
{
    setList(true);	// show list and convert buttons
}


// Start up function. Must be called as part of page's onLoad()
function startUp()
{
var element;

    checkForNetscape();
    if (!document.getElementById
      || !document.getElementById("list").innerHTML) {
  	dhtml = false;
  	return;		// we can't alter display
    }
    setDisplay();
}
