//selectindex used by mixer.htm
var selectindex = null;

//formindex is used to assign each form element a unique #
var formindex = 1;
var formlookup = new Array();

//mixer_win is reference to any open Mixer window
var mixer_win = null;
var basket_doc = null;

// Arrgh -- must implement pop() and push() for Internet Explorer!!!
var mypush = function(arg) {
	this[this.length] = arg;

	return true;
}

var myunshift = function(arg) {
	for (var i=this.length; i>0; i--) {
		this[i] = this[i-1];
	}	
	this[0] = arg;
	return true;
}

var mypop = function() {
  var retitem = this[this.length - 1];
  this[this.length - 1] = null;
  this.length--;
  return retitem;
}

// Setup basket
var basket = new Array();
basket.push = mypush;
basket.pop = mypop;
basket.unshift = myunshift;

// ****************
// Global Functions
// ****************
// showItems() -- prints HTML table with purchased items
function showItems(){
	//loadBasket();
	if (!basket[0]) {
		return;
	}
	out.open();
	out.writeln('<B>Shopping Basket</B><BR>');
	out.writeln('<B> Use the UPDATE button to update changes in the quantity. </B> Use the REMOVE button to delete an item.<BR>');
	out.writeln('<B>Note: </B>Please DO NOT use the Back button on your browser while using the shopping cart.');
	out.writeln('<FORM NAME="updateform">');
	showList();
	out.writeln("</FORM>");
	out.writeln('<FORM>');
	out.writeln('<INPUT TYPE="button" VALUE="Clear List" onClick="menuref.clearBasket();">');
	out.writeln('<INPUT TYPE="button" VALUE="Done/Place Order" onClick="top.document.location='+"'"+'gbuy.php'+"'"+';">');
	out.writeln('</FORM>');
	out.close();
	top.frames[3].menuref = top.menu;
}

function showList() {
	// Print Table header
	out.writeln("<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD>");
	out.writeln('<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 BGCOLOR="#002F00">');
	out.writeln('<TR><TD>');
	out.writeln('<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 BGCOLOR="#002F00">');
	out.writeln('<TR BGCOLOR="#DFFFDF"><TD>&nbsp;</TD><TD><B><FONT SIZE=-1>Quantity</FONT>');
	out.writeln('</B></TD><TD><B><FONT SIZE=-1>Name</FONT></B></TD><TD>');
	out.writeln('<B><FONT SIZE=-1>Price</FONT></B></TD><TD>');
	out.writeln('<B><FONT SIZE=-1>Notes</FONT></B></TD></TR>');

	// Print Table contents
	var i = 0;
      while (basket[i]) {
			basket[i++].show();
	}
	
	// Print subtotal
	if (basket.length) {
		var subtotal=0;
		out.write('<TR></TR><TR BGCOLOR="#dfffdf">');
		out.write('<TD ALIGN=center>&nbsp;');
		//out.write('<A HREF="javascript:menuref.refresh();"><IMG BORDER=0 SRC="img/update.gif"></A>');
		out.write('</TD>');
		out.write('<TD COLSPAN=2><B>Subtotal</B></TD>');
		i = 0;
		while (basket[i]) {
			subtotal += basket[i].calcPrice();
			if (basket[i].next) {
				var point = basket[i];
				while (point.next) {
					subtotal += point.next.calcPrice();
					point = point.next;
				}
			}
			i++;
		}
		basket.subtotal = subtotal;
		out.write('<TD ALIGN=RIGHT COLSPAN=1> $'+numtoDollar(subtotal)+'</TD>');
		out.write('<TD ALIGN=CENTER>&nbsp;</TD>');
		out.write('</TR>');
	}
	// Print Table footer	out.writeln("</TABLE>");
	out.writeln("</TABLE>");
	out.writeln("</TD></TR></TABLE>");
	out.writeln("</TD></TR></TABLE>");
}


//checks subtotal for minimum order
function MinimumSub() {

	if (basket.length) {
		var subtotal=0;
		i = 0;
		while (basket[i]) {
			subtotal += basket[i].calcPrice();
			if (basket[i].next) {
				var point = basket[i];
				while (point.next) {
					subtotal += point.next.calcPrice();
					point = point.next;
				}
			}
			i++;
		}
		basket.subtotal = subtotal;
	}

}

// writecookie -- rewrites cookie according to basket[]
function writecookie() {
	var tempcookie ='TheBasket=;';
	var newcookie = 'TheBasket=;';

	var i=0;
	while (basket[i]) {
   		newcookie = basket[i++].toCookie(tempcookie);
		tempcookie=newcookie;
	}
	document.cookie = newcookie;
}

// refresh() -- rewrites cookie and reloads page
function refresh() {
		writecookie();
		showItems();
		if (mixer_win) showMixer();
}

// loadBasket() -- reads TheBasket cookie and transforms
// into the global array basket
function loadBasket() {
	var bask_index = 0;
	var index=document.cookie.indexOf("TheBasket");
	var countbegin = (document.cookie.indexOf("=",index)+1);
	var countend = document.cookie.indexOf(";",index);	
	if (countend == -1)
		countend = document.cookie.length;
	var fulllist = document.cookie.substring(countbegin,countend);

	countbegin=0;
	for (var i=0; i<fulllist.length; i++) {
		if (fulllist.substring(i,i+1) == '[') {
			countbegin = i+1;
		}
		if ( (fulllist.substring(i,i+1) == ']') ) {
			var entry=fulllist.substring(countbegin,i);
			basket[bask_index] = loadEntry(entry);
		       	bask_index++;
		}
	}
}

// loadEntry() -- receives 1 entry from TheBasket cookie
// and creates an item in the global basket array
function loadEntry(entry) {
	var itemno, quantity, notes;
	var newItem=null;
	var subItem=null;

	notes = '';
	var i=0;
	var j=0;
	
	// Make newItem from first three entries
	j=entry.indexOf('|', i);
	itemno = unescape(entry.substring(i,j));
	i = j + 1;
	j = entry.indexOf('|', i);
	quantity = unescape(entry.substring(i,j));
	i=j+1;
	j=entry.indexOf('|',i);
	notes = unescape(entry.substring(i,j));
	i=j+1;
	
	newItem = makeItem(itemno,quantity,notes);

	while (i < entry.length) {
		// get next 3 entries
		j=entry.indexOf('|',i);
		itemno = unescape(entry.substring(i,j));
		i=j+1;
		j=entry.indexOf('|',i);
		quantity = unescape(entry.substring(i,j));
		i=j+1;
		j=entry.indexOf('|',i);
		notes=unescape(entry.substring(i,j));
		i=j+1;

		subItem=makeItem(itemno,quantity,notes);

		newItem.addItem(subItem);
		subItem = null;
	}

	return newItem;
}

// makeItem() -- determines class of added option, creates an
// item of this class, and returns a reference to it
function makeItem(itemno,quantity,notes) {
	var Item, itemclass;
	
	switch (getItemClass(itemno)) {
	case 'CG':
		Item = new Grain(itemno,quantity,notes);
		break;
	case 'WG':
		Item = new Grain(itemno,quantity,notes);
		break;
	case 'LME':
		Item = new Syrup(itemno,quantity,notes);
		break;
	case 'TUB':
		Item = new BasicItem(itemno,quantity,notes,'ft');
		break;
/*	case 'XK':
		Item = new IngredientKit(itemno,quantity,notes);
		break;
	case 'XKXL':
		Item = new IngredientKit(itemno,quantity,notes);
		break;
	case 'AGK':
		Item = new IngredientKit(itemno,quantity,notes);
		break;
	case 'AGKXL':
		Item = new IngredientKit(itemno,quantity,notes);
		break;
	case 'PK':
		Item = new IngredientKit(itemno, quantity,notes);
		break;
	case 'PKXL':
		Item = new IngredientKit(itemno,quantity,notes);
		break;
*/	default:
		Item = new BasicItem(itemno,quantity,notes);
		break;
	}

	Item.tax = catalog[getItemClass(itemno)].tax;
	return Item;
}

function getItemClass(itemno) {
	// Get item class (first letters in itemno)
	var index;
	var i=0;
	while (i<itemno.length) {
		var temp = itemno.substring(i,i+1);
		var temp2 = parseInt(temp);
		
		if (temp2 >0) {
			index = i;
			break;
		}
		i++;
	}
	
       return (itemno.substring(0,index));
}

// ****************
// Helper functions
// ****************

//formatLeft(str,num) -- pads str to num chars, or chops it off  if if its too long
function formatLeft(str,num) {
	if (str.length > num) { 
		return str.substring(0,num);
	}
	var ret = str;
	for (var i=0; i<(num-str.length);i++) {
		ret += ' ';
	}
	return ret;
}

//formatRight(str,num) -- moves string to the end of a num-length string
function formatRight(str,num) {
	if (str.length > num) {  // don't chop this
		return str;
	}

	var ret='';
	for (var i=0;i<(num-str.length);i++) {
		ret += ' ';
	}
	ret += str;
	return ret;
}

// getCookieValue -- extract a value given the key
function getCookieValue(str, cookie) {
	if (!cookie)
		cookie = document.cookie;
	var start,end;
	start = cookie.indexOf(str+'=') + str.length + 1;
	if (start < (str.length+1))
		return null;
	end = cookie.indexOf(';',start);
	if (end < 0)
		end = cookie.length;
	return unescape(cookie.substring(start,end));
}

// numtoDollar() - makes all numbers have 2 decimal places
function numtoDollar(num) {
	var bignum = Math.round(num*100);
	
	if (Math.round(bignum/10) != (bignum/10)) { // already have 2 decimal places
		return (bignum/100);
	}
	
	if (Math.round(bignum/100) != (bignum/100)) { // have 1 decimal place
		var temp = new String(bignum/100);
		return (temp+'0');
	}
	
	// need to add two decimal places
	var temp = new String(bignum/100);
	return (temp+'.00');
}

// **************
// Event Handlers
// **************

// buyItemNext(itemno,quantity) - called when user clicks on item to buy
function buyItemNext(itemno,quantity) {
	if (!quantity) quantity = 1;
	if (catalog[itemno].options) {
		getOptionsFirst(itemno);
		return false;
	}
	var newItem = makeItem(itemno,quantity,'');
	basket.unshift(newItem);
	return true;
}

// buyItemFromCookie(itemno, quantity) -- In case a buyItem gets thwarted by a reload
function buyItemFromCookie() {
	var cookie_item = getCookieValue('item',top.document.cookie);
	var cookie_quantity = getCookieValue('quantity',top.document.cookie);

	if (cookie_item) {
		top.document.cookie = "item=";
		top.document.cookie = "quantity=";
		buyItemNext(cookie_item,cookie_quantity);
	} else {
		return;
	}
}

function clearBasket() {
	var retval = confirm("You are about to clear the basket.");
	if (retval) {
		//document.cookie="TheBasket=;";
		basket = new Array();
		basket.push = mypush;
		basket.pop = mypop;
		basket.unshift = myunshift;
		writecookie();
	   	hideBasket();
	} else {
		return;
	}
}

function upgradeXL(index) {
	var item = formlookup[index];

	item.upgrade();
	refresh();
}

function removeItemAt(index) {
	if (basket.length == 1) {// last item in basket
		basket = new Array();
		basket.push = mypush;
		basket.pop = mypop;
		basket.unshift = myunshift;
		hideBasket();
	}
	var item;
	item=formlookup[index];

	removeItem(item);
	refresh();
}

function removeItem(deletee) {
	var item = null;
	var store = new Array();
	store.push = mypush;
	store.pop = mypop;
	store.unshift = myunshift;
	var checkcount;

	while (basket.length > 0) {
		item = basket.pop();
		while (!item) {
			item = basket.pop();
		}
		if (item == deletee) {   // We've found it
			if (item.next)
				store.push(item.next);
			break;
		}
		
		if (item.next) { // search through subs
			if (removeSubItem(item,deletee)) {
				store.push(item);
				break;
			}
		}
		store.push(item);

		checkcount++;
		if (checkcount > 100000) alert('Checkcount (in removeItem) is getting large!');
	}
	
	while (store.length > 0) {
		item = store.pop();
		basket.push(item);
		checkcount++;
		if (checkcount > 100000) alert('Checkcount(in removeItem) is getting quite large!');
	}
}

// removeSubItem(container,target) - if target is in container, delete
// and return true else return false
function removeSubItem(container, target) {
	var item;
	var retval = false;
	var store = new Array();
	var checkcount = 0;

	var point = container;
	while (point.next) {
		if (point.next == target) {// We've found it
			point.next = (point.next).next;
			retval = true;
			break;
		}
		checkcount++;
		if (checkcount > 10000) {
			alert('Checkcount(in removeSubItem) is getting quite large, aborting');
			break;	
		}
		point = point.next;

	}

	return retval;
}

// called when quantity or notes are changed
function changeHandler(element) {
	var index,item;
	if (element.name.indexOf('quant') >= 0) {
		index = element.name.substring(5,element.name.length);
		item = formlookup[index];
		item.quantity = element.value;
			if (item.quantity < 1) {
				item.quantity = 1;
			alert("You cannot order less than 1 of a quantity.");
			}
		setTimeout('formindex=1;formlookup=new Array();refresh();',250);
		//refresh();
	}
	if (element.name.indexOf('notes') >= 0) {
		index = element.name.substring(5,element.name.length);
		item = formlookup[index];
		item.notes = element.value;
		writecookie();
	}
}

function forceupdate() {
	var item, element;
	for (var i=0; i<formlookup.length; i++) {
		item = formlookup[i];
		element = self.updateform['quant'+i];
		alert(element.name);
		item.quantity = element.value;
		element = self.updateform['notes'+i];
		item.notes = element.value;
	}
	writecookie();
}

function selectHandler(element) {
	var index;

	if (element.name.indexOf('select') >= 0) 
		index = element.name.substring(6,element.name.length);

	selectlookup[index].addItem(selectlookup[index].options[element.options.selectedIndex]);
	removeItem(selectlookup[index].options[element.options.selectedIndex]);
}

function getOptionsFirst(itemno) {
	var url=catalog[itemno].options;
	var params = "";
	params = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=490,height=350";

	window.open(url,'',params);
}

function openMixer(index) {
	 if (mixer_win) {
		 var retval = confirm('You already have a mixer window open.  Click OK to close it and open a new one.');
		 if (retval) {
			 mixer_win.close();
			 mixer_win = null;
		 } else {
			 mixer_win.focus();
			 return;
		 }
	 }

	 // refresh any items that have been changed
	 writecookie();
	var item = formlookup[index];

	var url = "mixer.htm";
	var name = "Mixer";
	var params = "";
	params += "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=490,height=350";

	var win = window.open('', name, params);
	mixer_win = win;
	mixer_win.menuref = top.menu;
	basket_doc = out;
	selected = item;
	showMixer();	
}

// *****************
// Class Definitions
// *****************

// class BasicItem -- superclass 
function BasicItem(itemno,quant,notes, units) {
	if (itemno == null)  // called to initialize subclasses
		return false;

	// Instance variables
	this.item = itemno;
	this.quantity = quant;
	this.notes = notes;
	if (units) {
		this.units = units;
	} else {
		this.units = '';
	}

	this.name = catalog[this.item].name;
	
	return true;
}

BasicItem.prototype.showOptions = function () {
	return true;
}

BasicItem.prototype.show = function () {
	out.write('<tr bgcolor="#dfffdf"><td><CENTER>');
	out.write('<a href="javascript:menuref.removeItemAt('+formindex);
	out.write(');"><img border=0 src="img/remove.gif"></a> ');
	out.write(' <a ><img border=0 src="img/update.gif" alt="update quantity" > </a> ');
	this.showOptions();
	out.write('</CENTER></td>');
	out.write('<td align=left>');
	out.write('<INPUT TYPE=TEXT NAME="quant'+formindex);
	out.write('" onChange="menuref.changeHandler(this);" VALUE="'+this.quantity+'" SIZE=4>');
	if (this.units) {
		out.write(this.units);
	}
	out.write(' '+'</td>');

	out.write('<td align=left>'+this.name+'</td>');
	out.write('<td align=RIGHT> $'+numtoDollar(this.calcPrice())+'</td>');
	out.write('<td align=left>');
	out.write('<input type=text name="notes'+formindex);
	out.write('" onChange="menuref.changeHandler(this);" VALUE="'+this.notes+'" size=30> ');
	out.write('</td></tr>');
	
	// Set onChange event handler information
	formlookup[formindex]=this;
	formindex++;
}

//showHidden() -- outputs basket info in hidden fields for use by asp scripts
BasicItem.prototype.showHidden = function (identifier) {
	var itemprice = catalog[this.item].price;
	out.write('<INPUT TYPE="hidden" NAME="itemcode'+identifier+'" ');
	out.write('VALUE="'+escape(formatLeft(this.item,10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="itemname'+identifier+'" ');
	out.write('VALUE="'+escape(formatLeft(this.name,50))+'">');
	out.write('<INPUT TYPE="hidden" NAME="quantity'+identifier+'" ');
	out.write('VALUE="'+escape(formatLeft(this.quantity+' '+this.units,10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="itemprice'+identifier+'" ');
	out.write('VALUE="'+escape(formatRight('$'+numtoDollar(itemprice),10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="price'+identifier+'" ');
	out.write('VALUE="'+escape(formatRight('$'+numtoDollar(this.calcPrice()),10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="itemnotes'+identifier+'" ');
	out.write('VALUE="'+escape(this.notes)+'">');

	return ++identifier;
}

BasicItem.prototype.calcPrice = function () {
	var unitprice = catalog[this.item].price;
	var index = unitprice.indexOf('|');
	var low,high;
	
	while (index > -1) {
		var minus,equal,end;
		minus = unitprice.indexOf('-');
		equal = unitprice.indexOf('=');
		end = unitprice.indexOf('|');
		if (end < 0) {	// then this is the last entry consisting only of one number
			break;
		}
			
		low = unitprice.substring(0,minus)-0;
		high = unitprice.substring(minus+1,equal)-0;
		if ( (this.quantity >= low) && (this.quantity < high) ) {
			unitprice = unitprice.substring(equal + 1, end);
			break;
		}
		unitprice = unitprice.substring(end+1,unitprice.length);
		index = unitprice.indexOf('|');
	}
	return this.quantity*unitprice;
}

BasicItem.prototype.calcUnitPrice = function () {
	var unitprice = catalog[this.item].price;
	var index = unitprice.indexOf('|');
	var low,high;
	
	while (index > -1) {
		var minus,equal,end;
		minus = unitprice.indexOf('-');
		equal = unitprice.indexOf('=');
		end = unitprice.indexOf('|');
		if (end < 0) {	// then this is the last entry consisting only of one number
			break;
		}
			
		low = unitprice.substring(0,minus)-0;
		high = unitprice.substring(minus+1,equal)-0;
		if ( (this.quantity >= low) && (this.quantity < high) ) {
			unitprice = unitprice.substring(equal + 1, end);
			break;
		}
		unitprice = unitprice.substring(end+1,unitprice.length);
		index = unitprice.indexOf('|');
	}
	return unitprice;
}

BasicItem.prototype.calcTax = function () {
	if (this.tax == 0) {
	    return 0;
        }
	else {
	     return this.calcPrice();
	     }
}

BasicItem.prototype.toCookie = function (tempcookie) {
	var newcookie;
	// A basic item only has itemno and quantity to export
	var index,countbegin,countend;	
	index = tempcookie.indexOf("TheBasket");
	countbegin = (tempcookie.indexOf("=", index) + 1);
	countend = tempcookie.indexOf(";", index);
	if (countend == -1) {
		countend = tempcookie.length;
	}
	newcookie="TheBasket="+tempcookie.substring(countbegin, countend)+
		"["+escape(this.item)+"|"+escape(this.quantity)+
		"|"+escape(this.notes)+"|";
	newcookie = newcookie + '];';
	return newcookie;
}

// class MultItem
// This is a container class -- the base for grain, malt, ?

function MultItem(itemno,quant,notes) {
	if (itemno == null) // called to initialize subclasses
		return false;

	this.item = itemno;
	this.quantity = quant;
	this.notes = notes;

	this.units = 'lb';

	this.name = catalog[itemno.name];

      this.next = null; // Later used to hold list of multiple items
      
      return true;
}

MultItem.prototype = new BasicItem;

MultItem.prototype.addItem = function(sub) {
	if (this.next) {
		this.next.addItem(sub);
	} else {
		this.next = sub;
	}
	return true;
}

MultItem.prototype.show = function(option) {
	var bgcolor;
	if (this.next) {
		option = 'remove';
		bgcolor="#dfdfff";
	} else {
		bgcolor="dfffdf";
	}
		
	if (this.next) { // Header if there's more than one item
		out.write('<TR BGCOLOR="'+bgcolor+'">');
		out.write('<TD COLSPAN=5 ALIGN=LEFT>');
		out.write('<A HREF="javascript:menuref.openMixer('+formindex+')">');
		out.write('<IMG BORDER=0 SRC="img/mixer.gif"></A> ');
		out.write('<B>Mixed '+heading[getItemClass(this.item)]+'</B>');
		out.write('</TD>');
		out.write('</TR>');
	}

	var point = this;
	var formindexofthis = formindex;
	while (point) {
		out.write('<TR BGCOLOR="'+bgcolor+'">');

		out.write('<TD><CENTER>');
		out.write('<A HREF="javascript:menuref.removeItemAt('+formindex);
		out.write(');"><IMG BORDER=0 SRC="img/remove.gif"></A> ');
		out.write('<A HREF="javascript:menuref.openMixer('+formindexofthis+')">');
		out.write('<IMG BORDER=0 SRC="img/mixer.gif"></A> ');
		out.write('</CENTER></TD>');
		
		out.write('<TD ALIGN=LEFT>');
		out.write('<INPUT TYPE=TEXT NAME="quant'+(formindex)+'"');
		out.write('onChange="menuref.changeHandler(this);" VALUE="'+point.quantity+'" SIZE=4>');
		out.write(this.units+'</TD>');

		out.write('<TD ALIGN=LEFT>');
		if (!(this.next))
			out.write(heading[getItemClass(this.item)]);
		out.write(catalog[point.item].name);
		out.write('</TD>');

		out.write('<TD ALIGN=RIGHT>');	
		out.write('$'+numtoDollar(point.calcPrice()));
		out.write('</TD>');

		out.write('<TD ALIGN=LEFT>');
		out.write('<INPUT TYPE=TEXT NAME="notes'+(formindex)+'"');
		out.write('onChange="menuref.changeHandler(this);" VALUE="'+point.notes+'" SIZE=30>');
		out.write('</TD>');

		out.write('</TR>');

		// Set onChange event handler information
		formlookup[formindex++] = point;

		// Advance the pointer
		point = point.next;
	}
}

//showHidden() -- outputs basket info in hidden fields for use by asp scripts
MultItem.prototype.showHidden = function (identifier) {
	var itemprice = catalog[this.item].price;
	out.write('<INPUT TYPE="hidden" NAME="itemcode'+identifier+'" ');
	out.write('VALUE="'+escape(formatLeft(this.item,10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="itemname'+identifier+'" ');
	out.write('VALUE="'+escape(formatLeft(this.name,50))+'">');
	out.write('<INPUT TYPE="hidden" NAME="quantity'+identifier+'" ');
	out.write('VALUE="'+escape(formatLeft(this.quantity+' '+this.units,10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="itemprice'+identifier+'" ');
	out.write('VALUE="'+escape(formatRight('$'+numtoDollar(this.calcUnitPrice()),10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="price'+identifier+'" ');
	out.write('VALUE="'+escape(formatRight('$'+numtoDollar(this.calcPrice()),10))+'">');
	out.write('<INPUT TYPE="hidden" NAME="itemnotes'+identifier+'" ');
	out.write('VALUE="'+escape(this.notes)+'">');
	identifier++;
if (this.next) {
	   identifier = this.next.showHidden(identifier);
	  }
	  return identifier;
}
MultItem.prototype.mixerShow = function(option) {
	var point = this;
	while (point) {
		out.write('<TR BGCOLOR="dfffdf">');
		out.write('<TD ALIGN=LEFT>');
		out.write('<INPUT TYPE=TEXT NAME="quant'+(formindex)+'"');
		out.write('onChange="menuref.changeHandler(this);" VALUE="'+point.quantity+'" SIZE=4>');
		out.write(this.units+'</TD>');

		out.write('<TD ALIGN=LEFT>');
		out.write(catalog[point.item].name);
		out.write('</TD>');

		out.write('<TD ALIGN=RIGHT>');	
		out.write('$'+numtoDollar(point.calcPrice()));
		out.write('</TD>');

		out.write('<TD ALIGN=LEFT>');
		out.write('<INPUT TYPE=TEXT NAME="notes'+(formindex)+'"');
		out.write('onChange="menuref.changeHandler(this);" VALUE="'+point.notes+'" SIZE=30>');
		out.write('</TD>');

		out.write('</TR>');

		// Set onChange event handler information
		formlookup[formindex++] = point;

		// Advance the pointer
		point = point.next;
	}
}


MultItem.prototype.calcPrice = function () {
	var unitprice = catalog[this.item].price;
	var index = unitprice.indexOf('|');
	var low,high;
	
	while (index > -1) {
		var minus,equal,end;
		minus = unitprice.indexOf('-');
		equal = unitprice.indexOf('=');
		end = unitprice.indexOf('|');
		if (end < 0) {	// then this is the last entry consisting only of one number
			break;
		}
			
		low = unitprice.substring(0,minus)-0;
		high = unitprice.substring(minus+1,equal)-0;
		if ( (this.quantity >= low) && (this.quantity < high) ) {
			unitprice = unitprice.substring(equal + 1, end);
			break;
		}
		unitprice = unitprice.substring(end+1,unitprice.length);
		index = unitprice.indexOf('|');
	}
	return this.quantity*unitprice;
}

MultItem.prototype.calcUnitPrice = function () {
	var unitprice = catalog[this.item].price;
	var index = unitprice.indexOf('|');
	var low,high;
	
	while (index > -1) {
		var minus,equal,end;
		minus = unitprice.indexOf('-');
		equal = unitprice.indexOf('=');
		end = unitprice.indexOf('|');
		if (end < 0) {	// then this is the last entry consisting only of one number
			break;
		}
			
		low = unitprice.substring(0,minus)-0;
		high = unitprice.substring(minus+1,equal)-0;
		if ( (this.quantity >= low) && (this.quantity < high) ) {
			unitprice = unitprice.substring(equal + 1, end);
			break;
		}
		unitprice = unitprice.substring(end+1,unitprice.length);
		index = unitprice.indexOf('|');
	}
	return unitprice;
}

MultItem.prototype.toCookie = function(tempcookie) {
	var newcookie;
	var index,countbegin,countend;
	index=tempcookie.indexOf("TheBasket");
	countbegin = (tempcookie.indexOf("=", index) + 1);
	countend = tempcookie.indexOf(";", index);
	if (countend == -1) 
		countend = tempcookie.length;

	newcookie = "TheBasket=" + tempcookie.substring(countbegin,countend) + "[";

	var point = this;
	while (point) {
		newcookie += escape(point.item) + "|" +
				 escape(point.quantity) + "|" +
				 escape(point.notes) + "|";
		point = point.next;
	}
	newcookie += '];';

	return newcookie;
}

// class Syrup
// This is the class for malt extract and honey. 
// In particular, it doesn't allow more than 12 lbs in the bucket

function Syrup(itemno,quant,notes) {
	this.item = itemno;
	this.quantity = quant;
	this.notes = notes;
	this.totalquantity = quant - 0;

	this.units = 'lb';

	this.name = catalog[itemno].name;

      this.next = null; // Later used to hold list of multiple items
}

Syrup.prototype = new MultItem;

Syrup.prototype.addItem = function(sub) {
	if (this.next) {
		this.next.addItem(sub);
	} else {
	  // The following lines change all added syrup itemno's to bulk itemnos
	  var itemno = sub.item.substring(3,5);
	  itemno = itemno - 0;
	  if (itemno < 50) {
	    itemno += 50 - 0;
	    sub.item = 'LME'+itemno;
	  }
	  // end

	       sub.totalquantity += this.totalquantity;
		if (sub.totalquantity > 12 ) {
		   alert('Malt extract containers can hold no more than 12 lbs.  Quantities have been reduced accordingly.');
		   var n = sub.totalquantity - 12;
		   sub.quantity -= n;
		   if (sub.quantity < 0) sub.quantity = 0;
		   sub.totalquantity = sub.quantity + this.quantity - 0;
		}
		this.next = sub;
	}
	return true;
}

Syrup.prototype.calcPrice = function () {
	var unitprice = catalog[this.item].price;
	var index = unitprice.indexOf('|');
	var low,high;
	
	while (index > -1) {
		var minus,equal,end;
		minus = unitprice.indexOf('-');
		equal = unitprice.indexOf('=');
		end = unitprice.indexOf('|');
		if (end < 0) {	// then this is the last entry consisting only of one number
			break;
		}
			
		low = unitprice.substring(0,minus)-0;
		high = unitprice.substring(minus+1,equal)-0;
		if ( (this.quantity >= low) && (this.quantity < high) ) {
			unitprice = unitprice.substring(equal + 1, end);
			break;
		}
		unitprice = unitprice.substring(end+1,unitprice.length);
		index = unitprice.indexOf('|');
	}

	// Add $1 if this is a bucket
	var itemno = this.item.substring(3,5);
	if (itemno < 50) {
	  return this.quantity*unitprice+1;
	} else {
	  return this.quantity*unitprice;
	}
}

// class Grain
// This is the class for bulk grain. 

function Grain(itemno,quant,notes) {
	this.item = itemno;
	this.quantity = quant;
	this.notes = notes;
	this.units = 'lb';

	this.name = catalog[itemno].name;

	this.next = null; // Later used to hold list of multiple items
}

Grain.prototype = new BasicItem;

// minimum $0.65
Grain.prototype.calcPrice = function () {
	var unitprice = catalog[this.item].price;
	var index = unitprice.indexOf('|');
	var low,high;
	
	while (index > -1) {
		var minus,equal,end;
		minus = unitprice.indexOf('-');
		equal = unitprice.indexOf('=');
		end = unitprice.indexOf('|');
		if (end < 0) {	// then this is the last entry consisting only of one number
			break;
		}
			
		low = unitprice.substring(0,minus)-0;
		high = unitprice.substring(minus+1,equal)-0;
		if ( (this.quantity >= low) && (this.quantity < high) ) {
			unitprice = unitprice.substring(equal + 1, end);
			break;
		}
		unitprice = unitprice.substring(end+1,unitprice.length);
		index = unitprice.indexOf('|');
	}

	if (this.quantity*unitprice > 0.65) {
		return this.quantity*unitprice;
	} else {
		return 0.65;
	}

	
}

// class IngredientKit
// Adds an 'XL' button in the cart to allow upgrading to XL yeast.
// If so, adds $1.50 to price
// Appends XL to classname if option is true

function IngredientKit(itemno,quant,notes) {
	this.item = itemno;         // never has XL in itemno
	this.quantity = quant;
	this.notes = notes;
	this.units = '';

	this.XLtag = ' w/ XL yeast';

	var c = getItemClass(itemno);
	var testXL = c.substring(c.length-2,c.length);

	if (testXL == 'XL') {
		this.XL = true;
		var n = itemno.substring(c.length,itemno.length);
		this.name = catalog[c.substring(0,c.length-2)+n].name +
			this.XLtag;	     
	} else {
		this.XL = false;
		this.name = catalog[this.item].name;
	}
}

IngredientKit.prototype = new BasicItem;

IngredientKit.prototype.showOptions = function () {
	out.write('<a href="javascript:menuref.upgradeXL('+formindex);
	out.write(');"><img border=0 src="img/xl.gif"></a> ');
}

IngredientKit.prototype.upgrade = function () {
	var c = getItemClass(this.item);
	var n = this.item.substring(c.length,this.item.length);

	if (!this.XL) { // upgrading
		this.name = this.name + this.XLtag;
		this.item = c + 'XL' + n;
	} else {
		this. name = this.name.substring(0,this.name.length - 
						 this.XLtag.length);
		this.item = c.substring(0,c.length - 2) + n;
	}
	
	this.XL = !this.XL;
}

IngredientKit.prototype.basicCalcPrice = BasicItem.prototype.calcPrice;

IngredientKit.prototype.calcPrice = function () {
	var retval;

	if (!this.XL) { // itemno will be valid
		retval = this.basicCalcPrice();
	} else {        // itemno needs 'XL' removed
		var c = getItemClass(this.item);
		var n = this.item.substring(c.length,this.item.length);
		var origitem = this.item;
		this.item = c.substring(0,c.length-2) + n;
		retval = this.basicCalcPrice() + 1.50;
		this.item = origitem;
	}

	return retval;
}

// **********
// End Script
//***********

