function queryString(p, url)
{
	if (!url)
	{
		url = window.location.search;
	}
	else
	{
		try { url = url.substr(url.indexOf('?')); } catch(e) { url = " "; }
	}
	
	p = p.toLowerCase();
	var result = null;

	var items = url.substr(1).split('&');
	for(var i = 0; i < items.length; i++)
	{
		var parts = items[i].split('=');
		if (parts[0].toLowerCase() == p)
		{
			result = (parts.length > 1) ? parts[1] : '';
			break;
		}
	}
	
	return result;
}

function trim(str)
{
	return str.replace(/(^\s+)|(\s+$)/g, "");
}

function getElementByClassName(obj, tag, className)
{
	var result = null;
	
	var items = document.getElementsByTagName(tag);
	for(var i = 0; i < items.length; i++)
	{
		if (items[i].className == className)
		{
			result = items[i];
			break;
		}
	}
	
	return result;
}

function addEventHandler(obj, evt, handler)
{
	if (typeof(obj.attachEvent) != 'undefined')
	{
		obj.attachEvent('on' + evt, handler);
	}
	else if (typeof(obj.addEventListener) != 'undefined')
	{
		obj.addEventListener(evt, handler, false);
	}
}

function getCurrency(price)
{
	var currencyCode;
	var priceValue;
	
	price = trim(price);
	switch (escape(price.charAt(0)))
	{
		case '%A5':		//	Japanese Yen
			currencyCode = 392;
			priceValue = price.substr(1).replace(',', '');
		break;

		case '%24':		//	US/Australlian Dollar
			currencyCode = (price.indexOf('AUD') == -1 ? '840' : '036');
			priceValue = price.substr(1).replace(' AUD', '');
		break;

		case '%u20AC':		//	Euro
			currencyCode = 978;
			priceValue = price.substr(1);
		break;

		case '%A3':		//	British Pound
			currencyCode = 826;
			priceValue = price.substr(1);
		break;
	}
	
	if (priceValue.indexOf('.') == -1)
	{
		priceValue += '.00';
	}

	return [priceValue, currencyCode];
}

function loadPixel()
{
	var priceStr = null;
	var orderCode;
	var skuCode;
	
	if (window.location.pathname.toLowerCase().indexOf('thankyou') != -1)
	{
		var table = document.getElementById('Table6');
		priceStr = table.rows[table.rows.length - 4].cells[2].innerHTML;
		orderCode = queryString('checkoutSession');
		skuCode = queryString('SKU');
	}
	else if ((window.location.pathname.toLowerCase().indexOf('redeempage.aspx') != -1) || (window.location.pathname.toLowerCase().indexOf('cancelmembership.aspx') != -1))
	{
		var cell = getElementByClassName(document, 'td', "pzOSTblFooterCell2");
		if (cell != null)
		{
			priceStr = cell.innerHTML;
			
			orderCode = trim(getElementByClassName(document, 'td', 'pzOrderSummaryTopArea').childNodes[1].rows[1].cells[0].childNodes[1].rows[0].cells[2].childNodes[1].childNodes[2].innerHTML);
			skuCode = trim(getElementByClassName(document, 'table', 'pzOrderSummaryMajorTbl').childNodes[1].rows[0].cells[0].childNodes[1].rows[1].cells[0].childNodes[1].rows[3].cells[0].innerHTML);
		}
	}
	
	if (priceStr != null)
	{
		var currency = getCurrency(priceStr);
		var body = 
			' currency_id = "' + currency[1] + '";' +
			' price = "' + currency[0] + '";' +
			' order_code = "' + orderCode + '";' +
			' sku = "' + skuCode + '";' +
			' pageAction = "2";';

		eval(body);
	}
	
	var s = document.createElement("script");
	s.type = "text/javascript";
	s.src = '/logos/iplaygc/scripts/i2a.js';
	document.body.appendChild(s);
}

var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root != null)
{
	addEventHandler(root, 'load', loadPixel);
}

