// JavaScript Document
var itemsDB = new Array();
var total_ordered = 0;

function getDIV(target){
	if(document.getElementById){ //DOM 2 new 
		return document.getElementById(target);
	}else if(document.all){ //ie4 DOM 1
		return document.all[target]
	}else if(document.layers){ //nn4 DOM 1
		return document.all[target]
	}else //DOM0
		alert("Browser not supported");
}

//Creats the object items that holds all of the information of the item
function menuItem(id, item_num, item_name, item_name2, description, attributes, price, quantity, category, image, imageW, imageH, type){
	this.id = id;
	this.num = item_num;
	this.name = item_name;
	this.name2 = item_name2;
	this.desc = description;
	this.attr = attributes;
	this.price = price;
	this.category = category;
	this.q = quantity;
	this.image = image;
	this.imageW = imageW;
	this.imageH = imageH;
	this.type = type;
}

function popUp (cLink, cName, W, H){
	window.open(cLink, cName,"height="+H+",width="+W+",status=no,toolbar=no,menubar=no,location=no");
}

function divInner(target, inner) {
	getDIV(target).innerHTML = inner;
}


function preloadImages(){
	preload = new Array();
	if (document.images){
		for(i=0; i<itemsDB.length;i++){
			preload[i] = new Image();
			preload[i].src = itemsDB[i].imgname + "_th.jpg";
		}

	}
}

//check quantity function
function checkQuantity()
{
	total_ordered = 0;
	var ordersCookie = findOrder("orders");
	if (!ordersCookie) {return}
	var userOrders = ordersCookie.split("&");
	for (var n = 0; n < userOrders.length; n++)
	{
		itemQ = userOrders[n].split(",");
		var orderNum = itemQ[2];
		itemsDB[itemQ[0]].q= orderNum;
		total_ordered += orderNum;				
	}
}

//find order function
function findOrder(findName)
 {
  var entireCookie;
  var cookieName;
  var cookieValue;
  var cookieArray = document.cookie.split("; ");
   for (var n = 0; n < cookieArray.length; n++)
   {
    entireCookie = cookieArray[n].split("=");
    cookieName = entireCookie[0];
    cookieValue = entireCookie[1];
    if (cookieName == findName)
    {
      return unescape(cookieValue);
    }
  }
  return null;
}

//returns a string containing the available options for the item
function getOptions(num){
	var str ="";
	for(var n = 0; n < itemsDB.length; n++){
		if(itemsDB[n].type == "o" && itemsDB[n].num == num)
			str += "<a onclick='addItem(" + itemsDB[n].id + ")'>" + itemsDB[n].name + 
			"</a> $" + itemsDB[n].price + "<br/>";
	}
	return str;
}

//fix floating point errors
function fixFloat(numValue, decimals) 
{
  var roundVal = Math.round(numValue * Math.pow(10, decimals));
  var convertVal = roundVal / Math.pow(10, decimals);
  var stringVal = convertVal.toString();
  var decimalLoc = stringVal.indexOf(".");
  if (decimalLoc == -1)
  {
   numDecimals = 0;
   stringVal += ".";   
  }
  else 
   {
   numDecimals = stringVal.length - decimalLoc - 1;
   }
   var padAmt = decimals - numDecimals;
   if (padAmt > 0)
   {
   for (var n = 1; n <= padAmt; n++)
   stringVal += "0";
   } 
  return stringVal;  
}

function addItem(num){
	itemsDB[num].q++;
	total_ordered++;
	updateCart();
}
function removeItem(num){
	itemsDB[num].q--;
	total_ordered--;	
	updateCart();
}


function deleteAll(){
	
	check = confirm("Are you sure you want to clear your shopping cart?");
	
	if(check)
		for(var b = 0; b < itemsDB.length; b++)
			itemsDB[b].q = 0;
	
	total_ordered=0;
	updateCart();
	writeCookies();
}

function writeCookies (){
	//Writes information to the cookie
	var ordered = " "; //initializes the string
	var total_ordered = 0;
	for(var b = 0; b < itemsDB.length; b++)
		{

		if (itemsDB[b].q > 0)
			{
				if(total_ordered>0)ordered += "&";
				ordered += itemsDB[b].id + "," + itemsDB[b].name + "," + itemsDB[b].q + "," + itemsDB[b].price;				
				total_ordered++;
			};
		}

	var ordercookie = "orders = " + ordered; //stores cookie in 'orders = 2 + 3 + 4' form
	document.cookie = ordercookie;
}

function checkOut(){
	if(total_ordered ==0)
		alert("Your Shopping Cart is Empty!");
	else {
		writeCookies();
		window.location="submit.php";	
	}
}