//****************************************************
// HecticRecs store-script by reZaReKt 
// Version 1
//****************************************************
var sprits = document.cookie;
var totalItems = 0;
var totalPrice = 0;

// orders erpire in one week
var today = new Date();
var expirationDate = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000); 
var cancelDate = new Date("01/01/1980").toGMTString();

// Retrieve a named value from the cookie
function getCookie(name) 
{ 
  	var index = sprits.indexOf(name + "=");
  	if (index == -1) 
  	{
	  return null;
	}
	index = sprits.indexOf("=", index) + 1;
	var endstr = sprits.indexOf(";", index);
	if (endstr == -1)
	{
		endstr = sprits.length;
	}
	return unescape(sprits.substring(index, endstr));
}

// Store a cookie value
function setCookie(name, data, expDate) 
{
	if (data != null)
	{
		document.cookie = name + "=" + escape(data) + "; expires=" + expDate + "; path=/";
	}
	// update sprits
	sprits = document.cookie;
}

// Store an ordered item as cookie
function orderItem(id, artist, title, media, price, amount, detailsUrl)
{
	setCookie(id, artist + "##" + title + "##" + media + "##" + price + "##" + amount + "##" + detailsUrl, expirationDate.toGMTString());
	window.location.reload ();
}
  
// Remove an item from the cookie
function cancelItem(id)
{
	setCookie(id, "", cancelDate);	
	window.location.reload ();
}

function emptySelection()
{
	for (i = 0; i <= 250; i++)
	{
		var item = getCookie ('item' + i);
		if (item != null)
		{
			setCookie('item' + i, "", cancelDate);	
		}
	}
}

// Format numbers like currency
function toCurrency(number)
{
	var result = number.toString();
	if (result.indexOf(".") == -1)
	{
		result = result + ".00";
	}
	else
	{
		if (result.substring(result.indexOf(".") + 1, 10).length == 1)
		{
			result = result + 0;
		}
	}
	return result;
}

// Render total amount of selected items
function totalOrdered()
{
	var total = 0;
	for (i = 0; i <= 250; i++)
	{
		var item = getCookie ('item' + i);
		if (item != null)
		{
			var parts = item.split ('##');
			var amount = Number(parts[4]);
			total += amount;
		}
	}

	document.write ("<strong><font color='Red'>" + total + " items selected.</font></strong>");
}

// Retrieve amount ordered for specified item
function getAmount(id)
{
	var amount = 1;
	var item = getCookie(id);
	if (item != null)
	{
		var parts = item.split ('##');
		amount = Number(parts[4]);
	}
	return amount;
}

// Render the orderlist
function generateOrderList()
{
	var color = 'WhiteSmoke';
	totalItems = 0;
	totalPrice = 0;
	
	for (i = 0; i <= 250; i++)
	{
		var item = getCookie ('item' + i);
		if (item != null)
		{
			var parts = item.split ('##');
			var amount = Number(parts[4]);
			if (amount > 0)
			{
				totalItems += amount;
				var price = Number(parts[3]);
				var subTotal = Math.round(amount * price * 100)/100;
				totalPrice = Math.round((totalPrice + subTotal) * 100)/100;
				
				document.writeln ('<tr valign="middle" bgcolor="' + color + '">');
				document.writeln ('<td>' + parts[0] +'</td>');
				document.writeln ('<td><a href="' + parts[5] + '">' + parts[1] +'</a></td>');
				document.writeln ('<td align="center">' + parts[2] +'</td>');
				document.writeln ('<td align="center">&euro; ' + toCurrency(price) +'</td>');
				document.writeln ('<td align="center">' + amount + '</td>');
				document.writeln ('<td align="right">&euro; ' + toCurrency(subTotal) + '</td>');
				document.writeln ('<td align="center"><input name="cancel' + i + '" type="button" onClick="cancelItem (\'item' + i + '\');" value="cancel"></td>');
				document.writeln ('</tr>');

				var orderRow = amount + "x " + parts[0] + " - " + parts[1];
				
				document.writeln ('<input type="hidden" name="item" value="' + orderRow + '">');
				
				if (color == 'WhiteSmoke')
					color = 'White';
				else
					color = 'WhiteSmoke';
			}
		}
	}
}

// Render the total columns of the orderlist
function generateTotals()
{
	document.write ('<td align="center" class="totalbox">' + totalItems + '</td>');
	document.write ('<td align="right" class="totalbox">&euro; ' + toCurrency(totalPrice) + '</td>');
}