
var cart;


function aboutUs()

{

var myAboutus="<h1><p>H.M.S. is a privately owned company. H.M.S has teamed up with the best designers in the country to create a limited quantity of cusom watches.</p>  <p>From a wade range of disciplines and backgrounds, these designers are excited to create a colleciton of special timepieces</p> <p>to illustrate the commitment of H.M.S to the advance ment of design and for our you, our customers.</p></h1>";

	//myAboutus+="<img src='images/hms_hdr.jpg' />"
	myAboutus+=document.getElementById('main').innerHTML = myAboutus;
	}



function showProducts(cat, sub)
	{
	var myString = db[cat].name;
	
	for(var i in db[cat][sub])
		{
		if(db[cat][sub][i].title != undefined)
			{
			myString += "<div style='border: 1px solid black;padding:5px;width:146px;float:left;font-size:12px'>";
			myString += "<img src='" + db[cat][sub][i].image + "' alt='myPic' />" + "<br />";
			myString += "<div style='color:black; font-family:arial, helvetica, sans-serif;'>";
			myString += db[cat][sub][i].description + "<br />";
			myString += "</div>"; 
			myString += db[cat][sub][i].price + "<br />";
			myString += db[cat][sub][i].product_number + "<br />";
			myString += db[cat][sub][i].title + "<br />";
			
			
			myString += "<form><input name='qty' type='text' value='1' size='2' /><br><input type='button' onclick='add2cart(" + cat + "," + sub + "," + i + ");' value='add to cart' /><br><input type='button' onclick='showCart();' value='show cart' /></form>";
			
			myString += "</div>"; 
			}
		} 
		document.getElementById('main').innerHTML = myString;
		//alert(myString);
	}




function add2cart(cat, subCat, prod)
{	
	//store index references to products for storage.
	var prodKey = readCookie('cookieCart');
	prodKey += "," + cat + subCat + prod + document.forms[prod].qty.value;
	//alert(document.forms[prod].qty.value);
	if(prodKey.charAt(0) == ",")
	{
		prodKey = prodKey.substring(1,prodKey.length);
	}
	cart = prodKey.split(",");
	//alert(cart);
	createCookie("cookieCart",prodKey,1);
}



function showCart()
{	

var subTotal = 0.0;
if(document.cookie.indexOf("cookieCart") != -1)
{
//alert("found cookie");


var prodKey = readCookie('cookieCart');
	cart = prodKey.split(",");
	var dump = cart.shift();
	//alert(cart);

document.getElementById('main').innerHTML = "";
var myStuff = "<table border='1' cellpadding='5'>";
	myStuff += "<tr><th>Name</th><th>Price</th><th>Product #</th><th>Title</th><th>Quantity</th><th>Total</th><th>Remove</th></tr>";
	
	for(var i in cart){
	
		var cat =  cart[i].charAt(0);
		var subCat =  cart[i].charAt(1);
		var prod = cart[i].charAt(2);
		var qty = cart[i].substring(3,cart[i].length);
	
	
		myStuff += "<tr>";
		myStuff += "<td>" + db[cat][subCat][prod].name + "</td>";
		myStuff += "<td>" + db[cat][subCat][prod].price + "</td>";
		myStuff += "<td>" + db[cat][subCat][prod].product_number + "</td>";
		myStuff += "<td>" + db[cat][subCat][prod].title + "</td>";
		myStuff += "<td><input name='qty' type='text' size='10' value='" + qty + "' /></td>";
		myStuff += "<td><input name='lineTotal' type='text' size='10' value='"+ (qty * db[cat][subCat][prod].price) +"' /></td>";
		myStuff += "<td><input name='remove' type='checkbox' value=" +i+ " onclick='updateCart(this.value);showCart();' /></td>";
		myStuff += "</tr>";
		
		var lineTotal = (qty * db[cat][subCat][prod].price);
		
		subTotal = subTotal + lineTotal;
		
		
		
}
	myStuff += "<tr><td colspan='6' height='15'></td></tr>";
	myStuff += "<tr><td colspan='3'></td><th>SubTotal</th><td><input id='amnt' name='amnt' type='text' size='10' /></td></tr>";
	myStuff += "<tr><td colspan='3'></td><th>Sales Tax</th><td><input id='tax' name='tax' type='text' size=10' /></td></tr>";
	myStuff += "<tr><td colspan='3'></td><th>Shipping</th><td><input id='ship' name='ship' type='text' size='10' /></td></tr>";
	myStuff += "<tr><td colspan='3'></td><th>Grand Total</th><td><input id='total' name='total' type='text' size='10' /></td></tr>";
	myStuff += "<tr><td colspan='3'><input type='button' value='continue shopping' /></td>";
	myStuff += "<td><input type='button' onclick='showCart();' value='update cart' /></td>";
	myStuff += "<td colspan='2' align='right'><input type='button' value='check-out now' onclick='window.location=\"chkout_hms.html\"' /></td></tr>";
	myStuff += "</table>";
	
	document.getElementById('main').innerHTML = myStuff;
	document.getElementById('amnt').value = subTotal;
	document.getElementById('tax').value = .07;
	document.getElementById('ship').value = .05;
	document.getElementById('total').value = subTotal + (subTotal * .07) + (subTotal * .05);
	
}else{ alert("there is nothing in your cart!");}	
	
}



function updateSubNav(category)
{
	var myString="<tr>";
	for(var i in db[category])
	{	//alert(typeof(db[category][i]));
		if(typeof(db[category][i]) == "object")
		{
		myString += "<a href='#' onclick='showProducts(" + category + "," + i + ");'>"+   db[category][i].name  + "</a>" + "&nbsp;" + "|" + "&nbsp;";
				}
	}
	myString +="</tr>";
	document.getElementById('statusbar').innerHTML = myString;
}


function updateCart(prod){
alert(prod);
cart.splice(prod,1);
var prodKeys = cart.join();
alert(prodKeys);
createCookie("cookieCart",prodKeys,1);
}





function numberFormat(amount) {
 var rawNumStr = round(amount) + '';
 rawNumStr = (rawNumStr.charAt(0) == '.' ? '0' + rawNumStr : rawNumStr);
 if (rawNumStr.charAt(rawNumStr.length - 3) == '.') {
  return rawNumStr
  }
 else if (rawNumStr.charAt(rawNumStr.length - 2) == '.') {
  return rawNumStr + '0';
  }
 else { 
 return rawNumStr + '.00'; }
 }

// Round all passed numbers to two 
// decimal places (hundredths place)
//------------------------------------------------------
function round(number,decPlace) {
 decPlace = (!decPlace ? 2 : decPlace);
 return Math.round(number * Math.pow(10,decPlace)) / Math.pow(10,decPlace);
 }

