var req;

function loadData(url, postData, processReqChange) {
	req = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest)
	{
		try
		{
			req = new XMLHttpRequest();
		}
		catch(e)
		{
			req = false;
		}
		// branch for IE/Windows ActiveX version
	}
	else if(window.ActiveXObject)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				req = false;
			}
		}
	}
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("GET", url+'?'+postData, true);
		req.send("");
	}
}

function AddToBasket(ProductVariantID, Quantity, SessionID)
{
	if (Quantity <= 0) return;
	if (ProductVariantID == "")
	{
		alert('You need to choose a product variation before adding this product to your basket');
	}
	else
	{
		var QueryString = 'Action=Add&PVID=' + ProductVariantID + '&Quantity=' + Quantity + '&SessionID=' + SessionID;
//		alert(QueryString);
		loadData('Includes/ActiveUpdate.asp', QueryString, ParseMiniBasket);
	}
}

function AddToVLBasket(ProductVariantID, Quantity, SessionID, Amount)
{
	if (Quantity <= 0) return;
	if (Amount < LowerBound || Amount > UpperBound)
	{
		alert("That size is not currently available");
		return;
	}
	if (ProductVariantID == "")
	{
		alert('You need to choose a product variation before adding this product to your basket');
	}
	else
	{
		var QueryString = 'Action=Add&PVID=' + ProductVariantID + '&Quantity=' + Quantity + '&SessionID=' + SessionID + '&Amount=' + Amount;
//		alert(QueryString);
		loadData('Includes/ActiveUpdate.asp', QueryString, ParseMiniBasket);
	}
}

function SetBasket(ProductVariantID, Quantity, SessionID)
{
	loadData('Includes/ActiveUpdate.asp', 'Action=Update&PVID=' + ProductVariantID + '&Quantity=' + Quantity + '&SessionID=' + SessionID, UpdateLargeBasket);
}

function SetVLBasket(ProductVariantID, Quantity, SessionID, Amount)
{
	loadData('Includes/ActiveUpdate.asp', 'Action=Update&PVID=' + ProductVariantID + '&Quantity=' + Quantity + '&SessionID=' + SessionID + '&Amount=' + Amount, UpdateLargeBasket);
}

function ParseMiniBasket()
{
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			var reply = req.responseText.split(':');
			UpdateMiniBasket(reply[0], reply[1]);
		}
		else
		{
			alert("There was a problem updating the cart:\n" + req.responseText);
		}	
	}	
}

function UpdateLargeBasket()
{
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			window.location.reload();
		}
		else
		{
			alert("There was a problem updating the cart:\n" + req.statusText);
		}	
	}
}

function CheckInitialVariants(ProductID)
{
	loadData('Includes/CheckVariants.asp', 'ProductID=' + ProductID, RespondCheckVariants);
}

function CheckVariants(ProductID)
{
	var Request = 'ProductID=' + ProductID + '&Variants=';
	var tags = document.getElementsByTagName('select');
	for (i=0; i < tags.length; i++)
	{
		if ( tags[i].value != -1 )
		{
			Request += escape(tags[i].id) + ":" + escape(tags[i].value) + "|";
		}
	}
	Request = Request.substring(0, Request.length - 1);
//	alert(Request);
	loadData('Includes/CheckVariants.asp', Request, RespondCheckVariants);
}

function RespondCheckVariants()
{
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			var reply = unescape(req.responseText);
			var priceInfo = eval("(" + reply + ")");

			if (priceInfo.productCount > 1)
			{
				if (priceInfo.minPrice != priceInfo.maxPrice)
				{
					document.getElementById('PriceInfo').innerHTML = priceInfo.productCount + ' product variations from ' + priceInfo.minPrice + ' to ' + priceInfo.maxPrice;
				}
				else
				{
					document.getElementById('PriceInfo').innerHTML = priceInfo.productCount + ' product variations at ' + priceInfo.minPrice;
				}
				document.getElementById('BasketButton').disabled = 'disabled';
			}
			if (priceInfo.productCount == 1)
			{
				if (priceInfo.minPrice != priceInfo.maxPrice)
				{
					document.getElementById('PriceInfo').innerHTML = 'Prices range from ' + priceInfo.minPrice + ' to ' + priceInfo.maxPrice;
				}
				else
				{
					document.getElementById('PriceInfo').innerHTML = priceInfo.minPrice;
				}
				document.getElementById('BasketButton').removeAttribute('disabled');
				document.getElementById('variant_id').value = priceInfo.variantID;
			}
			if (priceInfo.productCount == 0)
			{
				document.getElementById('PriceInfo').innerHTML = 'That combination is not available';
				document.getElementById('BasketButton').disabled = 'disabled';
			}
		}
		else
		{
			alert("There was a problem updating the cart:\n" + req.statusText);
		}	
	}
}

function UpdateMiniBasket(NewQuantity, NewPrice)
{
	document.getElementById('ItemCount').innerHTML = NewQuantity
	document.getElementById('TotalPrice').innerHTML = Number(NewPrice).toFixed(2)
}

function UpdateMasterImage(ID)
{
	document.getElementById('MainProductImage').src='Includes/MainProductImage.asp?ID=' + ID;
}

function ClearBasket(SessionID)
{
	loadData('Includes/ActiveUpdate.asp', 'Action=Clear&SessionID=' + SessionID, UpdateLargeBasket);
}