var hovercolor="#FF3300";
var today = new Date()
today.setTime(today.getTime() + 1000*60*60*24*1); //expire 1 day later.
var cookiename="menuitem";

// ******** hover functions ****************************
function ColorMe(item) {
	if (navigator.appVersion.substring(0,3) >= "4.0" && navigator.appName.substring(0,9) == "Microsoft") {
		item.style.color = hovercolor; // changes text color
	}
}

function unColorMe(item,blue) {
	 if (navigator.appVersion.substring(0,3) >= "4.0" && navigator.appName.substring(0,9) == "Microsoft") {
		if (blue) {
			item.style.color = "#000080"; // sets color back to blue
		} else {
			item.style.color = "#000000"; // sets color back to black
		}
	 }
}

function changePic(picname,picimg) {
	document.images[picname].src=picimg;
}
// ******************************************************
// ******** cookie functions ****************************
function SetCookie(name, value) {
 var expString = "; expires=" + today.toGMTString();
 expString =""; // DO NOT SAVE COOKIE TO DISK - IN MEMORY ONLY
 document.cookie = name + "=" + escape(value) + expString;
}

function GetCookie(name) {
 var result = null;
 var myCookie = " " + document.cookie + ";";
 var searchName = " " + name + "=";
 var startOfCookie = myCookie.indexOf(searchName);
 var endOfCookie;
 if (startOfCookie != -1) {
  startOfCookie += searchName.length; 
  endOfCookie = myCookie.indexOf(";", startOfCookie);
  result = unescape(myCookie.substring(startOfCookie, endOfCookie));
 }
 return result;
}
// ******************************************************
// ******** shop cart functions**************************
function roundTo(num,pow){
  num *= Math.pow(10,pow);
  num = (Math.round(num)/Math.pow(10,pow))+ "" ;
  if(num.indexOf(".") == -1)
    num += "." ;
  while(num.length - num.indexOf(".") - 1 < pow)
    num += "0" ;
  return num ;
}

function selectitem(desc,amount){
	if (amount != null && amount != "" ) {amount= roundTo(amount,2);}
	var newitem=desc + "$" +amount;
	var itemlist = GetCookie(cookiename)
	

	if (itemlist != null && itemlist != "" ) {
		if (itemlist.indexOf(newitem) >=0) {
			location.href="order.html";
			return;
		}
		itemlist+=","+newitem;
	}else{
		itemlist =newitem;
	}
	SetCookie(cookiename,itemlist);
	location.href="order.html";
}

function get_checkeditems() {	
	var itemlist = GetCookie(cookiename)

	for (i=0;i<window.document.menuitems.length;i++) {
		if (window.document.menuitems.elements[i].checked) {
			var newitem=window.document.menuitems.elements[i].name;
			
			if (itemlist != null && itemlist != "" ) {
				if (itemlist.indexOf(newitem) <0 ) {itemlist+=","+newitem;}
			}else{
				itemlist =newitem;
			}
			
		}
			
			
	}
	
	SetCookie(cookiename,itemlist);
	location.href="order.html";
}

function getitemlist() {
	var itemlist = GetCookie(cookiename)

	document.write("<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">");
	if (itemlist == null || itemlist == "" ) {
		document.write("<tr><td><blockquote>");
		document.write("<font color=\"\#000080\" face=\"Arial, Helvetica, sans-serif\" size=\"2\">Please browse our <i><b>Pioneer Presents</b></i> booklet and select the items you are interested in for your catered event.");
		document.write("</blockquote></td></tr>");
	}else{
		itemarray=itemlist.split(",");		
		for($index in itemarray) {
			menuitem=itemarray[$index].split("$");
			if (menuitem[1].length >0) {menuitem[1]="$"+roundTo(menuitem[1],2);}
			document.write("<tr><td width=\"5\"><img border=\"0\" src=\"../graphics/spacer.gif\" width=\"5\" height=\"1\"></td>");
			document.write("<td><font color=\"\#000080\" face=\"Arial, Helvetica, sans-serif\" size=\"2\">"+menuitem[0]+"</td>");
			document.write("<td align=\"right\" width=\"150\"><font color=\"\#000080\" face=\"Arial, Helvetica, sans-serif\" size=\"2\">"+menuitem[1]+"<img border=\"0\" src=\"../graphics/spacer.gif\" width=\"15\" height=\"1\">");
			if (menuitem[1].length <=0) {menuitem[1]="$"}
			document.write("<a onmouseover=\"ColorMe(this);\" onmouseout=\"unColorMe(this,1);\" class=\"bluetext\" href=\"javascript:removeitem('"+menuitem[0]+menuitem[1]+"');\"><font color=\"\#000080\" face=\"Arial, Helvetica, sans-serif\" size=\"2\">Remove item</a></td>");
 			document.write("</tr>");
		}
	}	
	document.write("</table>");
}
function removeitem(itemdesc){
	

	var itemlist = GetCookie(cookiename)	

	if (itemlist != null && itemlist != "" ) {			
		if (itemlist.indexOf(itemdesc) >=0) {				
			var stpos=itemlist.indexOf(itemdesc);
			var strleft=itemlist.substring(0,stpos-1);
			var strright=itemlist.substring(stpos+itemdesc.length+1,itemlist.length);
			if (strleft.length >0 && strright.length >0 ) {strleft+=","}

			SetCookie(cookiename,strleft+strright);
			location.reload();
		}
	}
}
function emptycart(){
	SetCookie(cookiename,"");
	location.reload();
}