var cookieName = 'florianiEmbTrolley';
var wndDetails
function OnProductClick( productID )
{
	var url = "prodDetails.aspx?id=" + productID.toString();
	window.location.href = url;
}
function cleanup()
{
	if ( wndDetails ) wndDetails.close();
}
function GetColorFromDrop( recordID )
{
	var retval = "";
	var drpSel = document.getElementById( "drp" + recordID.toString() );
	if ( drpSel != null )
	{
		var colorRecID = drpSel.options[drpSel.selectedIndex].value;
		var text = drpSel.options[drpSel.selectedIndex].text;
		if ( text != "")
		{
			var splitElems = text.split(" - ");
			text = splitElems[0];
		}
		if ( colorRecID == 0 )
		{
			alert('Please choose a colour for your product');
			return retval;
		}
		retval = " `" + text + "`";
	}
	return retval;
}
function AddToTrolleyL( prodname, recordID, price, masterRecId, colChart )
{
	var lProdName = prodname;
	if ( colChart > 0 )
	{
		var addStr = GetColorFromDrop( masterRecId );
		if ( addStr == "" )
			return;
		lProdName += addStr;
	}
	
	var spanObj = document.getElementById( "trolleyspan" );
	AddToTrolley( spanObj, lProdName, recordID, price );
}
function UpdateTrolleyFromChild( recordID, qty, prodname, price, colChart )
{
	var spanObj = document.getElementById( "trolleyspan" );
	var lProdName = prodname;
	if ( colChart > 0 )
	{
		var addStr = GetColorFromDrop( recordID );
		if ( addStr == "" )
			return;
		lProdName += addStr;
	}
	AddToTrolleyQ( spanObj, lProdName, 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 )
{
    alert( "You have added " + qty + " " + name + " to your trolley.")
	var dPrice = parseFloat( price );
	var expDate = new Date();
	var future = expDate.getTime() + (12*60*60*1000);	// twelwe hours
	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 pName = splitElems[0]
				var id = parseFloat(splitElems[1])
				var qtyOld = parseInt(splitElems[2]);
				if ( name == pName )
				{
					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 );
}
function DisplayTrolley( spanObj )
{
	if ( spanObj )
	{
		var tablestr = "<table cellpadding=0 cellspacing=0 border=0 style='font-family:Verdana;font-size:8pt;color:white;width:100%;'>";
		tablestr += "<tr><td align=center style='border-bottom:black solid 1px;padding:1px;'><img alt='Trolley Contents' src='images/cartIconSmall.gif' border=1></td><td align=center style='border-bottom:black solid 1px;' colspan=2>&nbsp;<B>In your Trolley</B></td></tr></table>";
		tablestr += "<table cellpadding=0 cellspacing=0 border=0 style='font-family:Verdana;font-size:8pt;color:white;width:100%;'>";
		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:white;' width=20px valign=top>" + qty.toString() + "</td>";
						tablestr += "<td style='";
						if ( counter > 0 ) tablestr += "border-top:gray dotted 1px;";
						tablestr += "color:white;' width=85px valign=top>" + splitElems[0].toString() +"</td>";
						tablestr += "<td style='";
						if ( counter > 0 ) tablestr += "border-top:gray dotted 1px;";
						tablestr += "color:white;' width=45px valign=top align=right>AU $" + (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:white;'>Total:</td>";
		tablestr += "<td width=45px valign=top style='border-top:black solid 1px;color:black;' align=right>AU $" + total.toFixed(2) + "</td></tr></table>";
		tablestr += "<br><div style='padding:4px;' align=center><div class='divckout'><a class=achkout href='checkout.aspx'>Checkout</a></div></div>";
		spanObj.innerHTML = tablestr;
	}
}

