// jshop.js JavaScript Development by JavaScriptDesign.com
// email solutions@javascriptdesign.com  Copyright 2000
// All Rights Reserved. Copyright Notice Must Remain in
// Source files and Catalog Pages.

//**Functions to read a flag in the window name to tell if the back button has been pressed from secure checkout page

	function tellMe() {
	var myWin = window.open("backbutton.html","Error","status=no,width=300,height=200")
	}
	function nameWindowCheckout() {
	self.name = "CheckoutWindow"
	}
	function nameWindowFinished() {
	self.name = "FinishedWindow"
	}
	function clearWindowName() {
	self.name = ""
	}
	function checkWindowNameisCheckout() {
		if (self.name == "CheckoutWindow") {
//			confirm('Window name is ' + self.name);
			history.go(1)
			tellMe();
		}
	}
	function checkWindowNameisFinished() {
		if (self.name == "FinishedWindow") {
//			confirm('Window name is ' + self.name);
			history.go(1)
			tellMe();
		}
	}
// ****Begin code section for plain table 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 itemDesc = new Array(); // The item description
var price = new Array(); // The item price
var notes = new Array(); // Notes - field not submitted


// **Object constructor to build the catalog array.**
// **Catalog entries are in an array on each page.**

function loadCatalog(num,iNum,iName,iDesc,cost,info) {
 catNo[num] = iNum;
 itemName[num] = iName;
 itemDesc[num] = iDesc;
 price[num] = cost;
 notes[num] = info;
}

// **Function to write the content for each table row.**
// **This reads th array data and populates a table **
// **for catalog display.

function writeTableRow(i) { 
 document.write('<tr><td align=center>'
  + notes[i]
  + '</td><td>' + itemName[i] + '</td>'
  + '<td>' + itemDesc[i] + '</td><td align="center">' + price[i] + '</td>'
  + '<td>' + '<a href="javascript:addItem(' + i + ')"><img src="http://www.flutehistory.com/Site_Images/orderBtn.gif" width=38 height=10 border=0 alt="Add this item"></a>' + '</td></tr>');
}

// **Function to write the catalog display table to **
// **the current document.**

function writeTable() {
 document.write('<table width=600 border="1" cellpadding=3 cellspacing=0 bordercolor="#336699">'
  + '<tr bgcolor=#CC9933><td align=center><b>Details</b>'
  + '</td><td><b>Author/Artist</b></td><td><b>Title</b></td>'
  + '<td><b>Price</b></td><td><b>Order</b></td></tr>');
 for (i=0; i < catNo.length; i++)
  writeTableRow(i);
 document.write('</table>');
}

// ****End code section for writing plain table catalog content.****


// ****Begin code section for Picture catalog display****

// **Establish array variables. These must match the**
// **catalog array construction on the detail picture pages.**
var catNo = new Array(); // The catalog number
var itemDetailName = new Array(); // The item name for Detailed display
var itemDetailDesc = new Array(); // The item description for Detailed display
var price = new Array(); // The item price
var notesDetail = new Array(); // Notes for Detailed display - field not submitted

// **Object constructor to build the Picture catalog array.**
// **Catalog entries are in an array on detail pages, pictures must be numbered in sequence with catalog entries.**

//function loadCatalog(num,iNum,iName,iDesc,cost,info) {
// catNo[num] = iNum;
// itemName[num] = iName;
// itemDesc[num] = iDesc;
// price[num] = cost;
// notes[num] = info;
//}

// **Function to write the content for each table row.**
// **This reads th array data and populates a table **
// **for detailed catalog display including images.

function writePictureTableRow(i) {
 document.write('<tr><td align=center valign=top rowspan="2">'
  + '<a href="javascript:addItem(' + i + ')"><IMG SRC="product'+ i +'.jpg" border="0"></a>'
  + '</td><td valign="bottom"><h4>' + itemName[i] + '</h4></td>'
  + '<td align="right" valign="bottom" rowspan="2">' + price[i] + '</td>'
//  + '<td  valign="bottom" rowspan="2"><a href="javascript:addItem(' + i + ')">' + notes[i] + '</a></td></tr>'
  + '<td valign="bottom" rowspan="2"><a href="javascript:addItem(' + i + ')"><IMG SRC="http://flutehistory.com/Site_Images/order.gif" border="0"></a></td></tr>'
  
  + '<tr><td valign="bottom"><p class="productdescription">' + itemDesc[i] + '</p></td></tr>');
}

// **Function to write the catalog display table to **
// **the current document.**

function writePictureTable() {
 document.write('<table width=600 border="0" cellpadding="5" cellspacing="10" bordercolor="#336699">');
//    + '<tr><td colspan="4">&nbsp;</td></tr>');
//  + '<tr bgcolor=#CC9933><td align=center><b>Click<br>ITEM #<br>to Order</b>'
//  + '</td><td><b>Author/Artist/Title</b></td>'
//  + '<td><b>Price</b></td><td><b>Details</b></td></tr>');
 for (i=0; i < catNo.length; i++)
  writePictureTableRow(i);
 document.write('</table>');
}


// ****End code section for writing Picture 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) {
			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,path) {
        counter ++
	document.cookie = name + "=" + counter + "@" + value + "; expires=" + expires + "; path=/"
}

// **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; path=/"
 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]")
 addCart = '' + a + '`' + itemName[num] + ' ' + itemDesc[num] + '~' + price[num] + '^'
 cookData += eval("addCart")
 setCookieData("Scart", cookData, expdate.toGMTString())
 alert("This item has been added to your shopping cart. Click View Cart on any page to review or buy your shopping cart contents.")
}

// ****End code section for updating the cookie.****


// ****Debugging function to display cookie contents.****

function showCookie() {
 display = getCookieData("Scart");
 alert(display)
}



