// Thanks to the many contributors of the various scripts that
// make up this web site.  They made it possible to construct the
// web site at no cost and still obtain a professional appearance.

// Obtained from Cut-N-Paste JavaScript from ISN Toolbox
// Infohighway, Inc. www.infohighway.com
//    -Dynamic Pop Up Windows Copyright 1999
//    -Click Buttons
//    -JavaScript Shopping Cart Copyright 1999, 2000
//       JavaScriptDesign.com

// Obtained from Earthweb www.earthweb.com
//    -JavaScript Shopping Cart using frames Copyright Gordon Smith
//       with thanks to Joe Burns, Ph.D.

// Cosmetic changes and the various combinations of these
// scripts Copyright 2000 Benjamin Scott Abruzzo.  All comments and
// copyrights should be included if the javascript is borrowed.  No
// portion should be directly sold

// All Rights Reserved. Copyright Notice Must Remain in
// Source files and Catalog Pages.

// ****Begin code section for catalog display****

// **Establish array variables. These must match the**
// **catalog array constructionin each page.**
var catNo = new Array(); // The catalog number
var itemName = new Array(); // The item name
var price = new Array(); // The item price
var notes = new Array(); // Notes - field not submitted
var weight = new Array(); // Weight (in ounces)
var ordQty = new Array(); // Array to store order qty

// **Object constructor to build the catalog array.**
// **Catalog entries are in an array on each page.**

function loadCatalog(num,iNum,iName,cost,weight_,info) {
 catNo[num] = iNum;
 itemName[num] = iName;
 price[num] = cost;
 notes[num] = info;
 weight[num] = weight_;
}

// **Function to write the content for each table row.**
// **This reads the array data and populates a table **
// **for catalog display.

function writeTableRow(i) {
 document.write('<div class="container3"><b class="rtop3"><b class="r1" style="border-top:2px"></b> <b class="r2"></b> <b class="r3"></b> <b class="r4"></b></b>'
  + '<div class="boxcontent">'
  + '<b>' + catNo[i]  + ': &nbsp; ' + itemName[i] + '</b><br />'
  + '<div class="to-left">' + notes[i] + '</div>'
  + '<p class="centered">PRICE: ' + price[i] + '&nbsp; &nbsp; &nbsp;WEIGHT: ' + weight[i] + ' oz.<br /><br /></p>'
  + '<p class="style1"><input type="text" size="3" value="" name="q'+i+'" /> &nbsp; '
  + '<a href="javascript:addItem(' + i + ')"><img src="photos/scart_pay.gif" border="0" alt="Click Here" /></a><br />'
  + '<img src="photos/down_arrow.gif" width="20" height="15" alt="Down Arrow" /> <b>Information Opens Below</b> <img src="photos/down_arrow.gif" width="20" height="15" alt="Down Arrow" />'
  + '</p><div class="clear"></div></div>'
  + '<b class="rbottom3"><b class="r4"></b> <b class="r3"></b> <b class="r2"></b> <b class="r1" style="border-bottom:2px"></b></b></div>');
}

// **Function to write the catalog display table to **
// **the current document.**

function writeTable() {
 document.write('<form name="form1">');
 for (i=0; i < catNo.length; i++)
  writeTableRow(i);
 document.write('</form>');
}

// ****End code section for writing catalog content.****

// ****Begin shared cookie functions****

// **Global expdate variable for cookies**
// **Cookie is set to expire in 24 hours**

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) {
			//This works in all but safari
			var cEnd = document.cookie.indexOf(escape(";"),j)
				//var cEnd = document.cookie.indexOf("expires",j)
				//alert("start: " + j + " end: " + cEnd);
			if (cEnd == -1) {
				cEnd = document.cookie.length
			}
			//alert("DOCUMENT.COOKIE.SUBSTRING: " + unescape(document.cookie.substring(j,cEnd)))
			return unescape(document.cookie.substring(j,cEnd))
		}
		i++
	}
	return ""
}

// **Write cookie data**

function setCookieData(name,value,expires) {
        counter ++
		//Test purposes
		

			//alert("In jshop.js - setCookieData 1.1: " + name + "=" + counter + "@" + value + "; expires=" + expires);
	document.cookie = name + "=" + counter + escape("@") + escape(value) + escape("; expires=") + escape(expires);
		//Test in safari if its expiration thats breaking cookie
		//document.cookie = name + "=" + counter + "@" + escape(value) + escape("; 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)
 }
}

// ****End shared cookie functions****

// ****Begin code section to update the cookie to ****
// ****add items to the shopping cart cookie 'Scart'.****

// **Global variables.**

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] + ' ~' + price[num] + '%' + weight[num] + '^'
 cookData += eval("addCart")
 //alert("In jshop.js: cookData: " + cookData);
 setCookieData("Scart", cookData, expdate.toGMTString())
 location.reload()
 }
}

// ****End code section for updating the cookie.****

// ****Function to reset the form elements everytime the page loads
// ****Remove th onLoad() event handler from the catalog body tags
// ****to eliminate this feature.

function clearQtys() {
 document.form1.reset()
}

// ****Debugging function to display cookie contents.****

function showMe() {
 display = getCookieData("Scart");
 alert(display)
}

// ****Opening a window****

function open_window(url) {
var iMyWidth;
var iMyHeight;
iMyWidth = (window.screen.width/2) - 350;
iMyHeight = (window.screen.height/2) - 300;
mywin = window.open(url,"win",'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, width=700, height=600, left=' + iMyWidth + ',top=' + iMyHeight + ',screenX=' + iMyWidth + ',screenY=' + iMyHeight);
}
