addToCart = function(id)
{
	AWV.shop.AddToCart(id, addToCart_Callback);
}

addToCart_Callback = function(data)
{
	updateShopbox(data.value);
}

updateQ = function(id)
{
	var tb = document.getElementById("sc"+id);
	AWV.shopcart.UpdateQuantity(id, tb.value, updateQ_Callback);
}

updateQ_Callback = function(data)
{
	var r = data.value;
	var content = document.getElementById("content");
	
	r[2] = r[2].replace("'", "\"");
	
	content.innerHTML = "<h1>shopping cart</h1><p>Welcome to your shopping cart. Here you can adjust the quantities of your orders, and also remove items by setting the quantity to zero.</p><p>Please ensure you are registered and logged in so that we can display the correct prices for your region.</p>" + r[2];
	
	updateShopbox(r[1]);
	
	var box = document.getElementById("sc" + r[0]);
	if (box != null)
		box.style.border = "solid 1px #0B0";
}

addMasterToCart = function(mpid, mcid)
{
	AWV.view.AddToCart(mpid, mcid, addMasterToCart_Callback)
}

addMasterToCart_Callback = function(data)
{
	updateShopbox(data.value);
}

var masterA;

GenerateLink = function(id)
{
	masterA = document.getElementById("MasterA" + id);
	AWV.my_masters.GenerateUrl(id, GenerateLink_Callback)
}

GenerateLink_Callback = function(data)
{
	var url = data.value;
	
	if (masterA != null)
	{
		masterA.href = url;
		masterA.innerHTML = url;
		masterA.onclick = null;
	}
}

updateShopbox = function(count)
{
	var ShopBox = document.getElementById("shopBox");
	
	if (ShopBox != null)
	ShopBox.innerHTML = "<span class=\"boxtitle\">shopping cart</span><p>You have " + count + " item(s) in <a href=\"shop-cart.aspx\">your cart</a></p>" + (count > 0 ? "<p>Proceed to <a href=\"checkout.aspx\">checkout</a></p>" : "");
}

qOnChange = function(id)
{
	var box = document.getElementById("sc" + id);
	
	if (box != null)
		box.style.border = "solid 1px #F00";
}

popup = function(contents)
{
	var dbox = document.getElementById("popupbox");
	var blankie = document.getElementById("blanket");
	
	var position = [0,0];
	var size = getSize();
	var offset = getScrollXY();
	
	dbox.style.display = "block";
	dbox.style.margin = "0px 0px 50px 0px;"
	
	position[0] = size[0]/2+offset[0]-(400);
	position[1] = size[1]/2 + offset[1]-200;
	
	dbox.style.left = position[0] + "px";
	dbox.style.top = "10%";
	
	
	dbox.innerHTML = contents;
	
	
	var closep = document.createElement("p");
	var closea = document.createElement("a");
	
	closea.href="#";
	closea.onclick = function()
	{
		cpop();
		return false;
	}
	closea.innerHTML = "Close this window";
	
	closep.appendChild(closea);
	dbox.appendChild(closep);
	
	var totalSize = getTotalSize();
	
	blankie.style.width = "100%";
	blankie.style.height = totalSize[1] + "px";
	
	return false;
}

cpop = function()
{
	var dbox = document.getElementById("popupbox");
	var blankie = document.getElementById("blanket");
	
	dbox.style.display = "none";
	blankie.style.width = 0;
	blankie.style.height = 0;
}

function getSize() {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth; myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
		myWidth = document.documentElement.clientWidth; myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth; myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}
function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset; scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop; scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop; scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function getTotalSize(){
	if (window.innerHeight && window.scrollMaxY) {// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	return [xWithScroll,yWithScroll];
}
