var IE4 = ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4 ) && !(navigator.appVersion.indexOf("5") > -1 ));
var numButtons = new Array();
var num10Packs = new Array();
var buttonPrices = new Array();
var packPrices = new Array();
var TotalDue = 0;
var TotalNumButtons = 0;
var TotalNum10Packs = 0;

function openStore(){
//	alert ("Opening Store");
//confirmChoices.style.position = "absolute";
	btn1qty.value = 0;
	pack1qty.value = 0;
	btn2qty.value = 0;
	pack2qty.value = 0;
	btn3qty.value = 0;
	pack3qty.value = 0;
	btn4qty.value = 0;
	pack4qty.value = 0;
	ShowSelector();
}

function ShowSelector(){
//	selector.className = "visStore";
//	selectInstructions.className = "visStore";
//	products.className = "visStore";
//	continue1.className = "visStore";
//	selectorPage.style.position = "absolute";
//	checkoutPage.className = "nonvisStore"
	selectorPage.style.zIndex = 10;
  	selectorPage.style.pixelTop = 50;
  	selectorPage.style.pixelLeft = 0;
	selectorPage.className = "visStore";
	continue1.onclick = gotoConfirm;
	
}

function HideSelector(){
//	selector.className = "nonvisStore";
//	selectInstructions.className = "nonvisStore";
//	products.className = "nonvisStore";
//	continue1.className = "nonvisStore";
	selectorPage.style.zIndex = 1;
	selectorPage.className = "nonvisStore";
}

function saveButtonNums(){
	numButtons[0] = btn1qty.value;
	numButtons[1] = btn2qty.value;
	numButtons[2] = btn3qty.value;
	numButtons[3] = btn4qty.value;
	num10Packs[0] = pack1qty.value;
	num10Packs[1] = pack2qty.value;
	num10Packs[2] = pack3qty.value;
	num10Packs[3] = pack4qty.value;
	frmCheckout.Qbtn1.value = btn1qty.value;
	frmCheckout.Qbtn2.value = btn2qty.value;
	frmCheckout.Qbtn3.value = btn3qty.value;
	frmCheckout.Qbtn4.value = btn4qty.value;
	frmCheckout.Qpack1.value = pack1qty.value;
	frmCheckout.Qpack2.value = pack2qty.value;
	frmCheckout.Qpack3.value = pack3qty.value;
	frmCheckout.Qpack4.value = pack4qty.value;
}

function CalcTotals(){
	TotalDue = 0;
	TotalNumButtons = 0;
	for (j = 0; j<numButtons.length; j++){
		buttonPrices[j] = Math.round(numButtons[j] * 4.00 * 100) / 100;
		packPrices[j] = Math.round(num10Packs[j] * 25.00 * 100) / 100;
		TotalNumButtons += numButtons[j] * 1.00;
		TotalNumButtons += num10Packs[j] * 10.00;
		TotalNum10Packs += num10Packs[j] * 1.00;
		TotalDue += buttonPrices[j];
		TotalDue += packPrices[j];
		//alert(round(TotalDue));
	}
	var realTotalDue = Math.round(TotalDue * 100) / 100;
	TotalDue = realTotalDue;
	
}

function gotoConfirm(){
	saveButtonNums();
//	alert ("Saving button needs");
//	alert ("Moving to confirm page");
	CalcTotals();
//	alert ("Total Due:  " + TotalDue);
	EmptyConfirmPage();
	SetUpConfirmPage();
	HideSelector();
	ShowConfirm();
	
}

function EmptyConfirmPage(){
	productArea.innerHTML = "";
	totalDueArea.innerHTML = "";
}

function SetUpConfirmPage(){
	var prodAreaHTML = "<table id=\"productChoices\" width = 90% align = center>";
	prodAreaHTML += "<tr><td width = 40%></td><td width = 20%></td><td width = 20%></td><td width = 20%></td></tr>";
//	productArea.innerHTML += "<table id=\"productChoices\">";
//alert (productArea.outerHTML);	
	var numLines = 0;
	for (j=0; j<numButtons.length; j++){
//		alert (j + " " + numButtons[j]);
		if (numButtons[j] > 0){
			prodAreaHTML += AddLine(j, numButtons[j], buttonPrices[j], "single");
			numLines++;
		}
		if (num10Packs[j] > 0){
			prodAreaHTML += AddLine(j, num10Packs[j], packPrices[j], "10Pack");
			numLines++;
		}
	}
	if (numLines == 0){
		prodAreaHTML += "<tr><td colspan=4><h4>You have not selected any buttons.</td></tr>";
	}
	prodAreaHTML += "</table id=\"productChoices\">";
	productArea.innerHTML += prodAreaHTML;
	
	var totDueHTML = "<table id=\"totalDue\" width=90% align=center>";
	totDueHTML += "<tr><td width = 80% align = right><h4>Total Due:  </td>";
	totDueHTML += "<td align = center><h4>$" + TotalDue + "</td></tr>";
	totDueHTML += "</table id=\"totalDue\">";
	totalDueArea.innerHTML += totDueHTML;
	
	var navButtonsHTML = "<table id=\"navButtons\" width = 90% align = center>";
	
	
//alert (productArea.outerHTML);
}

function AddLine(buttonNumber, quantity, price, prodType){
	var prodName = "Button " + (buttonNumber+1);
	var unitPrice = 4.00;
	if (prodType == "10Pack"){
		prodName = "10 Pack of " + prodName;
		unitPrice = 25.00;
	} 
	
	var lineHTML = "<tr><td><h4>" + prodName + "</td>";
	lineHTML += "<td align = center><h4>" + quantity + "</td>";
	lineHTML += "<td align = center><h4>$" + unitPrice + "</td>";
	lineHTML += "<td align = center><h4>$" + price + "</td></tr>";
//	alert (lineHTML);
	return lineHTML;
}

function ShowConfirm(){
	confirmPage.style.position = "absolute";
	confirmPage.style.zIndex = 10;
  	confirmPage.style.pixelTop = 75;
  	confirmPage.style.pixelLeft = 0;
	confirmPage.className = "visStore";
	
	makeChanges.onclick = goBackToSelector;
//	toCheckout.onclick = goToCheckout;
	
}

function HideConfirm(){
	confirmPage.style.zIndex = 1;
	confirmPage.className = "nonvisStore";
	
}

function goBackToSelector(){
	HideConfirm();
	ShowSelector();
}

function goToCheckout(){
	HideConfirm();
	SetupCheckout();
	ShowCheckout();
}

function SetupCheckout(){
	//alert("hi");
	var orderDescrip = "You have chosen to buy " + TotalNumButtons + " buttons for $" + TotalDue + ".  ";
	paymentIntro.innerText = orderDescrip + paymentIntro.innerText;
}

function ShowCheckout(){
//	alert (confirmPage.style.zIndex);
	checkoutPage.style.position = "absolute";
	checkoutPage.style.zIndex = 100;
  	checkoutPage.style.pixelTop = 75;
  	checkoutPage.style.pixelLeft = 0;
	alert("Proceeding to Checkout");
	checkoutPage.className = "visStore";

	Cancel.onclick = CancelOrder;	
	
	/*checkout.className = "visStore";
	checkoutInstructions.className = "visStore";
	checkoutButtons.className = "visStore";
	custTitle.className = "visStore";
	custAddress.className = "visStore";*/
//	alert (checkoutPage.className);
}

function CancelOrder(){

}
/*
function Menu(){
	this.name = ""; //the name of the menu
	this.myHTMLmenu = null; //the HTML element corresponding to this menu
	this.Items = new Array(); //items in this menu
	this.numItems = 0; //counts the number of items in this menu
	this.hasChildren = false; //whether or not the menu has children
	this.isChild = false; //whether or not this menu is a child
	this.parentMenu = null; //points back to parent menu
	this.parentItem = null; //points at parent item in parent menu
	this.activeItem = null; //points at currently active item in this menu
	this.visibleChildMenu = null; //points at the one visible child menu, if any
	this.hasVisChild = false; //tells you if there is a visible child or not
//	this.pixelTop = 50; //integer that represents the x coordinate of the upper-left corner
//	this.pixelLeft = 100; //integer that represents the y coordinate of the upper-left corner
}

function Item(){
	event.cancelBubble = true;
	this.name = ""; //name of this item
	this.myHTMLitem = null; //the HTML element corresponding to this item
	this.itemNum = 0; //number of this item in its menu
	this.hasChild = false; //whether or not this item has a child
	this.childMenu = null; //this item's child menu
	this.myMenu = null; //the menu that this item is in
	this.isActive = false; //whether or not this is the active item in this menu
	this.size = new Size();
	
}

function Location(xCoord, yCoord){
	this.x = xCoord;
	this.y = yCoord;
}

function Size(){
	this.width = 0;
	this.height = 0;
	
}

function showThatMenu(){
//used to test the location of a menu
	eval(menuName.value).className = "visibleMenu";
}

function initMenu(){
//	findAllMenus();
	createRoot();
	activeMenu = null;
	SetMenuPosition(Root);
	var MenuText = writeMenu(Root); //displays a text version of entire menu
//	alert (MenuText);
//	setupRoot();
	
//	var MenuHTML = writeHTML(Root);
//	alert (MenuHTML);
//	RootMenu.closeAll = closeAll;
}

function SetMenuPosition(menu){
//receives:  a menu
//action:  sets the position of a menu to the right and slightly lower than its parent item
//returns:  nothing
//	alert(menu.name + ":" + menu.isChild);
	if (menu.isChild == true){
//		menu.myHTMLmenu.style.pixelLeft = menu.parentMenu.myHTMLmenu.style.pixelLeft +menu.parentMenu.myHTMLmenu.style.pixelWidth;
		menu.myHTMLmenu.style.pixelLeft = menu.parentMenu.myHTMLmenu.style.pixelWidth - 5;
//		alert(menu.name + ":" + menu.myHTMLmenu.style.pixelLeft)
	}
//	alert(menu.numItems + " items.");
	for (var j=0; j<menu.numItems; j++){
		if (menu.Items[j].hasChild == true){
			SetMenuPosition(menu.Items[j].childMenu);
		}
	}
}

function createRoot(){
//receives:  nothing
//action:  creates the root menu and its items (ends up recursively creating all the submenus)
//returns:  nothing
	event.cancelBubble = true;
	var topMenuStuff = RootMenu.children; //an array of HTML tags that makes up the menu
	Root.name = "RootMenu";  //the Root menu always has this name
	Root.myHTMLmenu = eval(Root.name);  //hold the actual actual HTML tag
//	alert (Root.myHTMLmenu.style.pixelTop + ":" + Root.myHTMLmenu.style.pixelLeft);
	Root.myHTMLmenu.style.position = "relative";
//	Root.myHTMLmenu.style.position = "absolute";
//	Root.myHTMLmenu.style.pixelTop = 300;
//	Root.myHTMLmenu.style.pixelLeft = 100;
	//alert(Root.myHTMLmenu.style.zIndex);
	var itemCounter = 0;
	for (var j=0; j<topMenuStuff.length; j++){ //loop through all the children of the Root menu tag
		var itemHolder = topMenuStuff[j];
		//alert (itemHolder.outerHTML);
		if (itemHolder.className == "item"){  //child will be either an item or a label
			itemCounter++;
			(Root.numItems)++;
			Root.Items[itemCounter - 1] = createItem(itemHolder, Root, itemCounter); //create the item
		}
	}
	Root.myHTMLmenu.style.pixelWidth = findMaxItemWidth(Root);  //set the width of this menu
//	alert(Root.myHTMLmenu.style.pixelWidth);
}

function findMaxItemWidth(menu){
//receives:  a menu (with items)
//action:  determines width of widest item
//returns:  an integer value equal to its widest item
	var maxWidth = 0;
	for (var j = 0; j<menu.Items.length; j++){
		var itemWidth = menu.Items[j].myHTMLitem.offsetWidth;
		if (menu.Items[j].hasChild == true){
			itemWidth += 20;  //add 20 pixels for the "more arrow"
		}
//		var itemSections = menu.Items[j].children;
//		if (itemSections != null){
//			for (var k = 0; itemSections.length; k++){
//				itemWidth += itemSections[k].offsetWidth;
//			}
//		}
//		alert (itemWidth);
		if (itemWidth > maxWidth){
			maxWidth = itemWidth;
		}
	}
	return maxWidth;
}

function createItem(it, menu, counter){
//receives:  HTML tag containing an item, the menu in which that item appears, its position in that menu
//action:  creates an Item; if it has a child menu, it hangs that menu (recursively generates all menus)
//returns:  the Item
	event.cancelBubble = true;
	var itemStuff = it.children;
	var thisItem = new Item();
//	var thisItem = eval(it.id);
	thisItem.myMenu = menu;
	thisItem.name = it.id;
	thisItem.myHTMLitem = eval(thisItem.name);
	thisItem.itemNum = counter;
//	thisItem.myMenu.myHTMLmenu.style.pixelWidth = Math.max(thisItem.myMenu.myHTMLmenu.offsetWidth, thisItem.myHTMLitem.offsetWidth);//apj
//	alert (thisItem.name + " width: " + thisItem.myHTMLitem.offsetWidth + " x " + thisItem.myHTMLitem.offsetHeight);
	for (var j = 0; j<itemStuff.length; j++){
		var oneTag = itemStuff[j];
		if (oneTag.className == "label"){
			//alert ("Item name: " + oneTag.innerText);
//			alert (thisItem.name);
		}
		if (oneTag.className == "menu"){
			//alert ("Menu tag: " + oneTag.outerHTML);
			thisItem.hasChild = true;
			AddMoreArrow(thisItem.myHTMLitem);
//			alert (thisItem.innerHTML);
			thisItem.childMenu = hangMenu(eval(oneTag.id), thisItem);
		}
//		alert (itemStuff[j].outerHTML);
		
	}
//	thisItem.myHTMLitem.activate = activate;
//	thisItem.myHTMLitem.onmouseover = thisItem.myHTMLitem.activate;
	thisItem.myHTMLitem.onmouseover = activate;
//	thisItem.myHTMLitem.deactivate = deactivate;
//	thisItem.myHTMLitem.onmouseout = thisItem.myHTMLitem.deactivate;
		
	return thisItem;
}

function activate(){
//receives:  through "this," an item (HTML tag) to activate
//action:  highlights the item and displays its child menu, if any
//returns:  nothing
//	alert (this.id);
	event.cancelBubble = true;
	//alert (this.id);
	var thisItem = FindItem(this.id, Root); //finds the Item associated with the HTML tag
	activeMenu = thisItem.myMenu; //sets the new activemenu
	//alert (thisItem.name);
	if (activeItem == null){ //only immediately after the menu is initialized
		this.className = "itemOver"; //changes attributes of item
	//	alert(this.id);
	}else if (activeItem != thisItem){
		if (activeItem != thisItem.myMenu.parentItem) {
			deactivate(activeItem);
			if (thisItem.myMenu != activeItem.myMenu){
				var findThisMenu = activeItem.myMenu.parentMenu;
				var findThisParentItem = activeItem.myMenu.parentItem;
				while (findThisMenu != thisItem.myMenu) {
					findThisParentItem = findThisMenu.parentItem;
					findThisMenu = findThisMenu.parentMenu;
//					alert ("blah " + findThisParentItem.name + " in " + findThisMenu.name);
				}
//alert ("parentItem is " + findThisParentItem.name);
				deactivate(findThisParentItem);
			}
		}
		
		this.className = "itemOver";
	}
	activeItem = thisItem;
	if (thisItem.hasChild == true){
		ShowChildMenu(thisItem.childMenu);
		thisItem.myMenu.hasVisChild = true;
		thisItem.myMenu.visibleChildMenu = thisItem.childMenu;
	} else {
		thisItem.myMenu.visibleChildMenu = null;
	}
}

function deactivate(itemToDeact){
	event.cancelBubble = true;
	unhighlightItem(itemToDeact);
//	itemToDeact.myHTMLitem.className = "item";
//	if (itemToDeact.myMenu == activeMenu){
		if (itemToDeact.hasChild == true){
			HideChildMenu(itemToDeact.childMenu);
			itemToDeact.myMenu.hasVisChild = false;
		}
//	}
}

function ShowChildMenu(childToShow){
	childToShow.myHTMLmenu.className = "visibleMenu";
}

function HideChildMenu(childToHide){
	if (childToHide.hasVisChild == true) {
		HideChildMenu(childToHide.visibleChildMenu);
		childToHide.hasVisChild = false;
	}
	for (var j=0; j<childToHide.Items.length; j++){
		unhighlightItem(childToHide.Items[j]);
	}
	childToHide.myHTMLmenu.className = "menu";	
}

function unhighlightItem(itemToUnh){
	itemToUnh.myHTMLitem.className = "item";
}
function FindItem(itemName, menu){
	//alert ("finding " + itemName + " looking in menu " + menu.name);
	event.cancelBubble = true;
	var foundItem = null;
	for (var j=0; j<menu.Items.length; j++){
		if (menu.Items[j].name == itemName){
			foundItem = menu.Items[j];
			j=menu.Items.length;
		}else if (menu.Items[j].hasChild == true){
			foundItem = FindItem(itemName, menu.Items[j].childMenu);
			if (foundItem != null){
				j=menu.Items.length;
			}
		}
	}
	return foundItem;
}

function AddMoreArrow(itemWithMenu){
//	alert ("Adding an arrow");
//	alert (itemWithMenu.innerHTML);
	var	childHolder = itemWithMenu.children;
//	alert (childHolder[0].outerHTML);
	childHolder[0].innerHTML += "<span id=\"" + itemWithMenu.id + "_more\" class=\"more\">4</span>";
//	alert (childHolder[0].outerHTML);
//	itemWithMenu.innerHTML += "<span id=\"" + itemWithMenu.id + "_more\" class=\"more\">4</span>";
//alert (itemWithMenu.innerText);
}


function hangMenu(newMenu, hangFromItem){ //receives an HTML menu
	event.cancelBubble = true;
	var newMenuStuff = newMenu.children;
	//alert (newMenu.innerHTML);
//	var thisMenu = eval(newMenu.id);
	var thisMenu = new Menu();
	thisMenu.name = newMenu.id;
	thisMenu.myHTMLmenu = eval(thisMenu.name);		
	var HMenu = thisMenu.myHTMLmenu;
	//	thisMenu.id = newMenu.id;
//	alert (newMenu.outerHTML);
	thisMenu.parentItem = hangFromItem;
	thisMenu.parentMenu = hangFromItem.myMenu;
	hangFromItem.hasChild = true;
	hangFromItem.childMenu = thisMenu;
	hangFromItem.myMenu.hasChildren = true;
	thisMenu.isChild = true;
	thisMenu.activeItem = null;
	thisMenu.hasChildren = false;
	
	var itemCounter = 0;
	for (var j=0; j<newMenuStuff.length; j++){
		
		var itemHolder = newMenuStuff[j];
		//alert (itemHolder.outerHTML);
		if (itemHolder.className == "item"){
			itemCounter++;
			(thisMenu.numItems)++;
			thisMenu.Items[itemCounter - 1] = createItem(itemHolder, thisMenu, itemCounter);
		}
	}
	thisMenu.myHTMLmenu.style.pixelWidth = findMaxItemWidth(thisMenu);
	return thisMenu;
	
}

function writeMenu(menuToWrite){
	var testWrite = ""
	for (var j = 0; j<menuToWrite.Items.length; j++){
//	for (var eachItem in menuToWrite.Items){
	//	testWrite += eachItem.name + String.fromCharCode(10);
		testWrite += menuToWrite.Items[j].name + String.fromCharCode(10);
		if (menuToWrite.Items[j].hasChild == true){
			testWrite += writeMenu(menuToWrite.Items[j].childMenu);
		}
	}
//	var i=10;
//	char r = (char)i;
//	testWrite += String.fromCharCode(10);
	return testWrite;
}
*/
