// ** Eric Toledo Catalog **

var catNo = new Array(); // The catalog number
var itemName = new Array(); // The item name
var itemDesc = new Array(); // The item description
var price = new Array(); // The item price
var pic = new Array(); // Item visual
var ordQty = new Array(); // Array to store order qty

// **Catalog array.**

function loadCatalog(num,picture,iNum,iName,iDesc,cost) {
 pic[num] = picture;
 catNo[num] = iNum;
 itemName[num] = iName;
 itemDesc[num] = iDesc;
 price[num] = cost;

}

// **Function to Write Catalog Table Row.**

function writeCatalogRow(i) {
 document.write('<tr><td align=center><img src="images/' + pic[i] + '" border=0></td>'
  + '</td><td align="center"><font face=arial size=1 color=#000000>' + itemName[i]
  + '</font><td><font face=arial size=1 color=#000000>' + itemDesc[i] + '</font></td><td align="center"><font face=arial size=1 color=#000000>' + price[i] + '</font></td>'
  + '<td><input type="text" size="3" value="" name="q'+i+'">'
  + '<td align=center>'
  + '<a href="javascript:addItem(' + i + ')"><img src="images/addcart.gif" border="0" width=20 height=28'
  + 'alt="Click to Add Item To Your Shopping Cart"></a>'
  + '</font></td></tr>');
}

// ** Write the Catalog **


function writeTable() {
 document.write('<form name="form1"><table width=500 border="0" cellpadding=4 cellspacing=2>'
  + '<tr bgcolor=#988F82><td width=50 align=center><font face=arial size=1 color=#ffffff><b>STYLE</b></font></td><td><font face=arial size=1 color=#ffffff><b>ITEM#</b></font></td>'
  + '<td><font face=arial size=1 color=#ffffff><b>ITEM DESCRIPTION</b></font></td><td align=center><font face=arial size=1 color=#ffffff><b>PRICE</b></font></td><td align=center><font face=arial size=1 color=#ffffff><b>QTY</b></font>'
  + '</td><td width=75 align=center><font face=arial size=1 color=#ffffff><b>ADD TO CART</b></font></td></tr>');
 for (i=0; i < catNo.length; i++)
  writeCatalogRow(i);
 document.write('</table></form>');
}



// ****Begin shared cookie functions****


var expdate = new Date()
expdate.setTime (expdate.getTime() + (1 * 24 * 60 * 60 * 1000))

// **Read cookie data.**

function getCookieData(name) {
	var label = name + "="
	var labelLen = label.length
	var cLen = document.cookie.length
	var i = 0
	while (i < cLen) {
		var j = i + labelLen
		if (document.cookie.substring(i,j) == label) {
			var cEnd = document.cookie.indexOf(";",j)
			if (cEnd == -1) {
				cEnd = document.cookie.length
			}
			return unescape(document.cookie.substring(j,cEnd))
		}
		i++
	}
	return ""
}

// **Write Cookie Data**

function setCookieData(name,value,expires) {
        counter ++
	document.cookie = name + "=" + counter + "@" + value + "; expires=" + expires
}

// **Kill cookie function. When the order is submitted**
// **the cookie is killed via an event handler call.**

function killCookie(name) {
 if (getCookieData(name)) {
 document.cookie = name + "=" + "; expires = Thu, 01-Jan-70 00:00:01 GMT";
 cookData = ""
 counter = 0
// history.go(0)
 }
}


// ****Update Cookie ***

var counter = 0
var cookData = ""

// **Extract current value of cookie when page loads **
// **and store the values in the global variables.**

if (getCookieData("Scart")) {
 orderString = getCookieData("Scart")
 cLen = orderString.length
 countEnd = orderString.indexOf("@")
 pointer = countEnd + 1
 counter = orderString.substring(0,countEnd)
 cookData = orderString.substring(pointer,cLen)
}

// **Function to add an item to the shopping cart cookie.**

function addItem(num) {
 a = eval("catNo[num]")
 ordQty[num]=eval("document.form1.q"+num+".value");
 b = eval("ordQty[num]")
 if (ordQty[num] == 0) {
  alert("Please enter your order quantity");
  return;
 }
 for (var i = 0; i < b.length; i++) {
  var oneChar = b.substring(i, i + 1)
  if (oneChar < "0" || oneChar > "9") {
   alert("Please make sure entries are numbers only.")
   eval("document.form1.q"+num+".value = ''")
   return;
  }
 }
// return true;
 if (confirm("Add Item " + a + " To Your Shopping Cart?")) {
 addCart = '' + a + '`' + ordQty[num] + '*' + itemName[num] + ' ' + itemDesc[num] + '~' + price[num] + '^'
 cookData += eval("addCart")
 setCookieData("Scart", cookData, expdate.toGMTString())
 }
}

// ****End code section for updating the cookie.****

// ****Reset the Form Elements ***

function clearQtys() {
 document.form1.reset()
}

// ****Debugging function to display cookie contents.****

function showMe() {
 display = getCookieData("Scart");
 alert(display)
}



