var cookieName = 'parties2GoTrolley';
var wndDetails
function OnProductClick( productID )
{
	var url = "prodDetails.aspx?id=" + productID.toString();
	window.location.href = url;
}
function cleanup()
{
	if ( wndDetails ) wndDetails.close();
}
function AddToTrolleyL( prodname, recordID, price )
{
	var spanObj = document.getElementById( "trolleyspan" );
	AddToTrolley( spanObj, prodname, recordID, price );
}
function UpdateTrolleyFromChild( recordID, qty, prodname, price )
{
	var spanObj = document.getElementById( "trolleyspan" );
	AddToTrolleyQ( spanObj, prodname, recordID, price, qty );
}
function getCookieVal(offset)
{
	var endStr = document.cookie.indexOf(";", offset);
	if ( endStr == -1 )
		endStr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endStr ));
}
function GetCookie(name)
{
	var arg = name+"=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0;
	while (i<clen)
	{
		var j = i+alen;
		if (document.cookie.substring(i,j) == arg )
			return getCookieVal(j);
		i = document.cookie.indexOf(" ",i) + 1;
		if ( i==0 )
			break;
	}
	return null;
}
function SetCookie(name,value,expires,path,domain,secure)
{
//	alert(escape(value))
	document.cookie = name + "=" + escape(value) +
		((expires)? "; expires=" + expires.toGMTString() : "") +
		((path)? "; path=" + path : "") +
		((domain)? "; domain=" + domain : "") +
		((secure)? "; secure" : "");
}
function DeleteCookie(name,path,domain)
{
	if ( GetCookie(name) )
	{
		document.cookie = name + "=" +
			((path)? "; path=" + path : "") +
			((domain)? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function AddToTrolley( spanObj, name, productID, price )
{
	qty = document.getElementById( "q" + productID.toString() ).value;
	AddToTrolleyQ( spanObj, name, productID, price, qty )
}
function AddToTrolleyQ( spanObj, name, productID, price, qty )
{
	var dPrice = parseFloat( price );
	var expDate = new Date();
	var future = expDate.getTime() + (365*24*60*60*1000);	// one year
	expDate.setTime( future );
	var cookStr = GetCookie(cookieName);
	var newCookStr = name+"~"+productID+"~"+qty.toString()+"~"+dPrice.toFixed(2)+"#";
	var alteredCookStr = "";
	if ( cookStr == null ) cookStr = "";
	var foundProd = false;
	var tempStr = cookStr;
	// find existing prod and replace qty
	while ( true )
	{
		idx = tempStr.indexOf("#");
		if ( idx != -1 )
		{
			var oneProd = tempStr.substring( 0, idx );
			if ( oneProd != null )
			{
				var splitElems = oneProd.split("~");
				var id = parseFloat(splitElems[1])
				var qtyOld = parseInt(splitElems[2]);
				if ( id == productID )
				{
					var qtyNew = parseInt(qtyOld)+parseInt(qty)
					var newCookStr = name+"~"+productID+"~"+qtyOld.toString()+"~"+dPrice.toFixed(2)+"#";
					alteredCookStr = name+"~"+productID+"~"+qtyNew.toString()+"~"+dPrice.toFixed(2)+"#";
					foundProd = true;
					break;
				}
			}
			
			if ( idx+1 >= tempStr.length )
				break;
			tempStr = tempStr.substring( idx+1 )
		}
		else break;
	}
	if ( foundProd == false )
		cookStr += newCookStr;
	else
	{
		cookStr = cookStr.replace( newCookStr, alteredCookStr );
	}
	SetCookie(cookieName,cookStr,expDate);
	DisplayTrolley( spanObj );
	alert(qty + " " + name + "(s) were added to your shopping trolley (displayed at the top in the column to the right)!");
}
function DisplayTrolley( spanObj )
{
	if ( spanObj )
	{
		var tablestr = "<table cellpadding=0 cellspacing=0 border=0 style='font-family:Verdana;font-size:8pt;color:black;width:100%;'>";
		tablestr += "<tr><td align=center style='border-bottom:black solid 1px;'><img width=25px alt='Trolley Contents' src='Images/cartIcon.gif' border=0></td><td align=center style='border-bottom:black solid 1px;' colspan=2>&nbsp;<B>In your Trolley</B></td></tr>";
		var total = 0.00;
		
		var cookStr = GetCookie(cookieName);
		if ( cookStr != null )
		{
			var counter = 0;
			while ( true )
			{
				idx = cookStr.indexOf("#");
				if ( idx != -1 )
				{
					var oneProd = cookStr.substring( 0, idx );
					if ( oneProd != null )
					{
						var splitElems = oneProd.split("~");
						var price = parseFloat(splitElems[3])
						var qty = parseInt(splitElems[2]);
						tablestr += "<tr><td style='";
						if ( counter > 0 ) tablestr += "border-top:gray dotted 1px;";
						tablestr += "padding-left:2px;color:red;' width=20px valign=top>" + qty.toString() + "</td>";
						tablestr += "<td style='";
						if ( counter > 0 ) tablestr += "border-top:gray dotted 1px;";
						tablestr += "color:black;' width=85px valign=top>" + splitElems[0].toString() +"</td>";
						tablestr += "<td style='";
						if ( counter > 0 ) tablestr += "border-top:gray dotted 1px;";
						tablestr += "color:black;' width=45px valign=top align=right>$" + (price*qty).toFixed(2) + "</td></tr>";
						total += parseFloat(splitElems[3])*qty;
					}
					
					if ( idx+1 >= cookStr.length )
						break;
					cookStr = cookStr.substring( idx+1 )
				}
				else break;
				counter++;
			}
		}
		tablestr += "<tr><td colspan=2 width=105px valign=top style='border-top:black solid 1px;color:red;'>Total:</td>";
		tablestr += "<td width=45px valign=top style='border-top:black solid 1px;color:black;' align=right>$" + total.toFixed(2) + "</td></tr></table>";
		tablestr += "<br><div style='padding:4px;' align=center><div style='border:1px solid gray;height:20px;background-color:orange;'><a class=achkout href='checkout.aspx'>Go to checkout</a></div></div>";
		spanObj.innerHTML = tablestr;
	}
}
