//10/29/03
//alert("homebrewery.js");
var aryCookie=new Array("xxx");        //Needs a value???!
var mFileName;

function fnInitializeCurrentPage()
{
	//alert("fnInitializeCurrentPage()");
	//Doesn't use file name in id. Cat #s should be unique. 
	var lochref=location.href;
	//alert("fnAddShopping Cart() lochref = " + lochref);
	var lastfwdslash=lochref.lastIndexOf("/");
	//alert("lastfwdslash = " + lastfwdslash);
	var lastdot=lochref.lastIndexOf(".");
	//alert("lastdot = " + lastdot);
	mFileName=lochref.substring(lastfwdslash+1,lastdot);
	//alert("mFileName = " + mFileName);

	////////////////////////////
	fnGetCookie("ordered");
	///////////////////////////
	
	//alert("aryCookie after fnGetCookie = " + aryCookie); 
	//NOTE: TO BE SURE - MUST USE _  AFTER mFileName
	if (aryCookie!="xxx")
	{
		for (var i=0;i<aryCookie.length/5;i++)
		{
			var id=aryCookie[((5*i)+0)];
			//alert("id = " + id + "\nmFileName = " + mFileName);
			//if (id.indexOf((mFileName + "_"),0)>-1)
			//{
				var qty=aryCookie[(5*i+4)];
				//alert("qty = " + qty);
				//if (qty>0)
				{
					//alert("Before writing Qty to 'Qty_'" + id);
					if (qty==0) {qty="";}
					try
					{
						document.getElementById("Qty_" + id).value=qty;
					}
					catch(e)
					{}
				}
			//}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////
function fnOpenCheckOut()
{
	//Problem with this is that calling page cannot be updated
	//alert("fnopenCheckOut()");
	//window.open("myCheckOut.htm", "width=500, menubar=no, resizable=no, status=no, toolbar=no");
	window.open("homebrewerycheckout.htm","","left=100, width=600, scrollbars=yes, toolbars=no, menubar=no");
}


function fnReplace(val, what, withWhat)
{
	//Called from fnConvertToDecimal:
	//what string to replace - looks like it will handle strings of more than 1 char,
	//withWhat  string to replace 'what' with.
	//alert("fnReplace val = " + val + "\nwhat = " + what + "\nwithWhat = " + withWhat);
	var temp = "" + val; // temporary holder
	while (temp.indexOf(what)>-1) 
	{
		pos= temp.indexOf(what);
		temp = "" + (temp.substring(0, pos) + withWhat + temp.substring((pos + what.length), temp.length));
	}
	//alert("fnReplace Replaced = " + temp);
	return temp;
}


function fnTrim(val)
{
	//Trim back to char before space:
	//alert("val = " + val);
	var newVal=""
	if (val.length>0)
	{
		for (var i=val.length-1;i>0;i--)
		{
			var myChar=val.charAt(i);
			//alert("myChar = " + myChar);
			if (myChar==" ")
			{
				newVal=val.substr(0,i);
				//alert("newVal trimmed = " + newVal);
				break;
			}
		}
	}
	return newVal;
}


function fnUpdateCookie(val)
{
	//Writes to array aryCookie and to document.cookie:
	//alert("fnUpdateCookie val = " + val);								//this.id
	var id=val.substr(4);
	//alert("id = " + id);
	var desc=document.getElementById("Desc_" + id).innerHTML;		//Description
	var weight=document.getElementById("Wgt_" + id).value;			//Weight
	var price=document.getElementById("Price_" + id).innerHTML;		//Price
	var qty=document.getElementById("Qty_" + id).value;				//Qty
	
	
	bFound=false;

	//alert("id = " + id + "\ndesc = " + desc + "\nweight = " + weight + "\nprice = " + price + "\nqty = " + qty);
	//alert("qty = " + qty + "\nparseInt(qty) = " + parseInt(qty));
	if ( (isNaN(parseInt(qty))) & (qty!=""))
	{
		alert("Illegal quantity value"); 
		return false;
	}
	qty=parseInt(qty)					//Deal with things like 5t!
	var addEdit="";
	var i=0;
	//Array: id,Desc,Weight,Price,Qty
	//		0   1   2   3   4
	
	//alert("aryCookie (before test for fnGetCookie())= " + aryCookie);
	if (aryCookie=="xxx") 
	{
		fnGetCookie("ordered");
		//alert("aryCookie (return after fnGetCookie())= " + aryCookie);
	}
	
	if (aryCookie!="xxx")
	{
		//alert("aryCookie. = " + aryCookie);
		for (i=0;i<aryCookie.length/5;i++)
		{
			//alert("aryCookie[" + ((5*i)+0) + "] = " + aryCookie[((5*i)+0)] + "\nid = " + id);
			if (aryCookie[((5*i)+0)]==id)	 
			{
				//alert("found");
				bFound=true;
				break;
			}
		}
		if (bFound!=true) {i=aryCookie.length/5;}
	}
	else
	{
		//alert("Cookie is empty");
		//Add to empty cookie:
		//i=aryCookie.length;
		i=0;
		
	}
	
	//alert("aryCookie (before update) = " + aryCookie);
	if (aryCookie[0]=="xxx") {i=0;} //Some problem if aryCookie initializes at the top = new Array()!
	//alert("bFound = " + bFound);
	//alert("i before update = " + i);
	if (bFound!=true)
	{
		//alert("i bfound = " + i);
		aryCookie[((5*i)+0)]=id.toString();
		desc=desc.substr(0,50);  //a bit of a guess!		
		desc=fnReplace(desc,"<B>","");
		desc=fnReplace(desc,"</B>","");
		desc=fnTrim(desc);
		aryCookie[((5*i)+1)]=desc;
		aryCookie[((5*i)+2)]=weight;
		aryCookie[((5*i)+3)]=price;
	}
	aryCookie[((5*i)+4)]=qty;
	//alert("aryCookie (after addition " + qty + ") = " + aryCookie);
	
	fnSetCookie("ordered");
}


function fnGetCookie(name)
{
	//alert("fnGetCookie name = " + name);
	var myCookie = " " + document.cookie + ";";
	var searchName= " " + name + "=";
	//alert("Cookie name = " + name + "\nsearchName = " + searchName);
	var startOfCookie=myCookie.indexOf(searchName);
	var endOfCookie;
	//alert("startOfCookie = " + startOfCookie);
	if (startOfCookie > -1)
	{
		startOfCookie += searchName.length;
		endOfCookie=myCookie.indexOf(";",startOfCookie);
		var result=unescape(myCookie.substring(startOfCookie,endOfCookie));
		//Turn sCookie into Array:  
		aryCookie=result.split("~");	
	}
	//alert("aryCookie fnGetCookie = " + aryCookie);
}


function fnSetCookie(name)
{
	//alert("fnSetCookie name = " + name + ", aryCookie = " + aryCookie);
	//alert("fnSetCookie aryCookie = " + aryCookie);
	//1. Turn array value into string sCookie:
	sCookie="";
	for (var i=0;i<aryCookie.length;i++)
	{
		//alert("aryCookie[" + i + "]=" + aryCookie[i]);
		sCookie += ((sCookie=="")?"":"~") + aryCookie[i];
		//alert("sCookie = " + sCookie);
	}
	//alert("sCookie = " + sCookie);
	//alert("sCookie value to store in cookie " + name + "= " + sCookie);
	if (aryCookie.length>0)
	{
		//alert(name + "=" + escape(sCookie));
		document.cookie = name + "=" + escape(sCookie);
	}
}
