//goodsName = Array("Camera", "Flash Drive", "19 inch TV","MP3 Player","Keyboard");
//goodsLength = goodsName.length;


//goodsPrice = Array("200", "50", "350","250","15");

//goodsId = Array("g1","g2","g3","g4","g5");


// cartContents is used to port the cart contents to the hidden send form. It needs to be global so that it can be called
//  by the updateCartDisplay function as well as being used in the form 
//var cartContents = "";

// This function will dynamically display all of the goods to the user in an order form
function showOrderForm(){
var msg = "<table border=\"0\"  cellpadding=\"5\" cellspacing=\"0\"  width=\"100%\">";
msg = msg + "<tr><th>Item</th><th>&nbsp;</th><th>Price</th><th>Add</th><th>Quantity</th></tr>";
for (i=0;i<goodsLength;i++){
		msg = msg + "<tr valign='top'><td><img src='images/" + goodsPicture[i] + "'></td>";
		msg = msg + "<td><b><i>" + goodsName[i] + "</i></b><br />" + goodsDesc[i] + "</td>";
		msg = msg + "<td>$" + goodsPrice[i] + "</td>";
		msg = msg + "<td><input type=\"checkbox\" id=\"" + goodsId[i] +"\"></td>";
		msg = msg + "<td><input type=\"text\" size=\"1\" id=\"" + goodsId[i] +"Quantity\" value=\"1\"></td>";
		}
msg = msg + "</table>";
document.write(msg);
}


// This functions reads the user's cart selection and stores the cart as cookies
function updateCart(){
     currentT = new Date;
		 expDate = new Date;
		 
		 expDate.setTime(expDate.getTime()+(1000*60*60*365));
		 setCookie("Date&Time", currentT, expDate);
		 for (i=0;i<goodsLength;i++){
		      if (document.getElementById(goodsId[i]).checked){
					     setCookie(goodsName[i],goodsPrice[i], expDate);	
							 tempId = goodsId[i] + "Quantity";	
							 aQuantity = document.getElementById(tempId).value;						 	
							 setCookie(goodsName[i] + "Quantity", aQuantity, expDate);			 
						}		
					else {
						 deleteCookie(goodsName[i]);		
						 tempId = goodsId[i] + "Quantity";
					   document.getElementById(tempId).value = "1";
						 }
		 }		
}

// This function recalls what the user has in their cart and checks the appropriate boxes in the cart form
function recallOrders(){			 
		 if (document.cookie != ""){
		      var cookieItem = document.cookie.split("; ");
					for (i=0; i<cookieItem.length; i++){
					     for (x=0; x<goodsLength; x++){
					  	      if (cookieItem[i].split("=")[0]==goodsName[x]){
							           document.getElementById(goodsId[x]).checked =1; 
												 // Fetch the quantity cookie and update the appropriate input on the form
												 cookieName = goodsName[x] + "Quantity";
												 for (a=0; a<cookieItem.length; a++){
		 		 								      if (cookieName == cookieItem[a].split("=")[0]){
				 											 aQuantity =  unescape(cookieItem[a].split("=")[1]);	
															 tempId = goodsId[x] + "Quantity";														 
															 document.getElementById(tempId).value = aQuantity;
															 }// end if cookieName == ...	
		 										 }														 											 
										}// end if block
					     } // end second for loop					     
					}	// end first for loop
			}		// end if			 
}


// This functon displays the contents of the shopping cart
function showCart(){		 
		 var msg = "";
		 if (document.cookie != ""){
		 			var tempId = "";
					var total = 0;
		      var cookieItem = document.cookie.split("; ");
					for (i=0; i<cookieItem.length; i++){
					     for (x=0; x<goodsLength; x++){
					  	      if (cookieItem[i].split("=")[0]==goodsName[x]){
											  cookieName = goodsName[x] + "Quantity";
												// Fetch the quantity cookie
												for (a=0; a<cookieItem.length; a++){
		 		 											if (cookieName == cookieItem[a].split("=")[0])
				 												aQuantity =  unescape(cookieItem[a].split("=")[1]);		
		 										}			 
							           msg = msg + "<tr><td>" + goodsName[x] + "</td><td>$" + goodsPrice[x] + "</td><td>" + aQuantity + "</td></tr>";
												 total = total + parseInt(goodsPrice[x]) * aQuantity;
												 }
					     } // end second for loop					     
					}	// end first for loop
			}		// end if		
			if (msg == "")
			     document.getElementById("cartDisplay").innerHTML = "There are currently no items in your cart";	
			else {
					 msg = "<table width=\"175\"><tr><th>Item</th><th>Price</th><th>Quantity</th></tr>" + msg + "</table><br /><b>Total: $" + total +"</b>";
			     document.getElementById("cartDisplay").innerHTML = msg;			 
			}
}

// This functon displays the contents of the shopping cart for use in the textarea of the place order form
function showCart2(){		 
		 var msg = "";
		 if (document.cookie != ""){
		 			var tempId = "";
					var total = 0;
		      var cookieItem = document.cookie.split("; ");
					for (i=0; i<cookieItem.length; i++){
					     for (x=0; x<goodsLength; x++){
					  	      if (cookieItem[i].split("=")[0]==goodsName[x]){
											   tempId = goodsId[x] + "Quantity";
							           msg = msg + " " + goodsName[x] + " $" + goodsPrice[x] + " " + document.getElementById(tempId).value + " ";
												 total = total + parseInt(goodsPrice[x]) * parseInt(document.getElementById(tempId).value);
												 }
					     } // end second for loop					     
					}	// end first for loop
			}		// end if		
			msg = msg + "Total: $" + total;
		  // return msg;
			//document.getElementById("cartContentArea").innerHTML = "<textarea  name=\"UserOrder\">" +  msg + "</textarea>";	 

}

// Call this when the user adds something to the cart - add items, adjust checkboxes, show cart contents
function updateCartDisplay(){
updateCart();
recallOrders();
showCart();
showCart2();
}

// This function deletes the current cart items
function clearCart(){
      for (i=0;i<goodsLength;i++){
			     deleteCookie(goodsName[i]);
			}
			for (x=0; x<goodsLength; x++){
					 document.getElementById(goodsId[x]).checked =0; 
					 tempId = goodsId[x] + "Quantity";
					 document.getElementById(tempId).value = "1";

			} 
			showCart();
			showCart2();
}






