/* ======================================================================== */
/* Name:			ShoppingBag.js											*/
/* Author:			Derrick Went											*/
/* Date:			31 July 2006											*/
/* Client:			SpecHead    											*/
/* Description:		Functions for maintaining the Shopping Bag				*/
/* ======================================================================== */
function RemoveItem(NextPage, ProductCode, Index, ItemOption)
{
	document.form1.action = NextPage;
	document.form1.SBAction.value = 'Update';
	document.form1.SBProductCode.value = ProductCode;
	document.form1.SBUpdateIndex.value = Index;
	document.form1.SBItemOption.value = ItemOption;
	
	var objField;
	objField = document.getElementById('basket_qty_' + Index);
   	if (objField)
	{
		if (objField.options)
		{
			// We have a Select field
			document.getElementById('basket_qty_' + Index).options[0].value = '0';
			document.getElementById('basket_qty_' + Index).options[0].text = '0';
	        document.getElementById('basket_qty_' + Index).options[0].selected = true;
		}
		else
		{
			// We have an input field
			objField.value = '0';
		}
		if (AJAXEnabled)
		{
			UpdateServerShoppingBag();
		}
		else
		{
			document.form1.submit();           
		}
	}
}
function ClearShoppingBag(NextPage)
{
	document.form1.action = NextPage;
	document.form1.SBAction.value = 'Clear';
	document.form1.submit();
}
function UpdateShoppingBag(NextPage, ProductCode, Index, ItemOption)
{
	document.form1.action = NextPage;
	document.form1.SBAction.value = 'Update';
	document.form1.SBProductCode.value = ProductCode;
	document.form1.SBUpdateIndex.value = Index;
	document.form1.SBItemOption.value = ItemOption;
	if (AJAXEnabled)
	{
		UpdateServerShoppingBag();
	}
	else
	{
		document.form1.submit();           
	}
}
function Checkout(NextPage)
{
	var objField;
	objField = document.getElementById('CBTANDC');

//	alert(objField.checked);
	if (objField.checked == false)
	{
		alert('You must check the box to show that you have read and agree to the Terms and Conditions');
	}
	else
	{
		document.form1.action = NextPage;
		document.form1.submit();
	}
}
function AddToShoppingBag(NextPage, ProductCode, ItemOption)
{
	if (ValidateShoppingBagItem(ProductCode))
	{
		document.form1.action = NextPage;	
		document.form1.SBProductCode.value = ProductCode;
		document.form1.SBAction.value = 'Add';
		document.form1.SBItemOption.value = ItemOption;
		if (AJAXEnabled)
		{
			UpdateServerShoppingBag();
		}
		else
		{
			document.form1.submit();           
		}
	}
}

// Async...
var PostRequest;
function UpdateServerShoppingBag()
{
	var FieldName;
	var EncodedValue;
	var MyForm;
	var i;
	var NameArray = new Array();
	var ValueArray = new Array();
	var nvpIndex;
	var j;
	var AlreadyThere;
	var TheString;
	var MyShoppingBagPage	= encodeURI(ShoppingBagPage);
	var MyCheckoutPage		= encodeURI(CheckoutPage);
	var MySecondText		= encodeURI(SecondText);

	// Encode the data to be POSTed.
	MyForm = document.getElementById("form1");
	nvpIndex = 0;
	for (i = 0; i < MyForm.length; i++)
	{
		FieldName		= MyForm.elements[i].id;
		if (FieldName == '')
		{
		}
		else
		{
			EncodedValue	= encodeURI(MyForm.elements[i].value);
			// See if the name exists in the array...
			AlreadyThere = false;
			for (j = 0; j < NameArray.length; j++)
			{
				if (NameArray[j] == FieldName)
				{
					AlreadyThere = true;
					ValueArray[j] = ValueArray[j] + ',' + EncodedValue;
				}
			}		
			if (AlreadyThere == false)
			{			
				// Add it to the array...
				NameArray[nvpIndex] = FieldName;
				ValueArray[nvpIndex] = EncodedValue;
				nvpIndex = nvpIndex + 1;
			}
		}
	}
	TheString = '';
	for (j = 0; j < NameArray.length; j++)
	{
		if (j == 0)
		{
			TheString = TheString + NameArray[j] + "=" + ValueArray[j];
		}
		else
		{
			TheString = TheString + '&' + NameArray[j] + "=" + ValueArray[j];
		}
	}		
	//alert(TheString);	

	PostRequest = new HttpRequest();
	PostRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	PostRequest.failureCallback = AsyncRequestFailed;
	PostRequest.url = UpdateBagPage;
	PostRequest.successCallback = AsyncCallback;
	PostRequest.post(TheString + "&ShoppingBagPage=" + MyShoppingBagPage + "&CheckoutPage=" + MyCheckoutPage + "&SecondText=" + MySecondText);
}
// Drag & Drop...
function DDUpdateServerShoppingBag(UpdateBagPage, ShoppingBagPage, CheckoutPage, ProductCode, Action, ItemOption)
{
	// Encode the data to be POSTed.
	var SBProductCode		= encodeURI(ProductCode);
	var SBAction			= encodeURI(Action);
	var SBItemOption		= encodeURI(ItemOption);
	var MyShoppingBagPage	= encodeURI(ShoppingBagPage);
	var MyCheckoutPage		= encodeURI(CheckoutPage);
	
	PostRequest = new HttpRequest();
	PostRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	PostRequest.failureCallback = AsyncRequestFailed;
	PostRequest.url = UpdateBagPage;
	PostRequest.successCallback = AsyncCallback;
	PostRequest.post("SBProductCode=" + SBProductCode + "&SBAction=" + SBAction + "&SBItemOption=" + SBItemOption + "&ShoppingBagPage=" + MyShoppingBagPage + "&CheckoutPage=" + MyCheckoutPage);
}
function AsyncRequestFailed(httpRequest)
{
	alert("Update failed, HTTP " + httpRequest.status + " " + httpRequest.statusText + ".");
}
function AsyncCallback(httpRequest)
{
	// Get the XML document returned from the request and fill in the fields.
	try
	{
		//alert(httpRequest.responseText);
		
		var el = document.getElementById("ShoppingBagSummary");
		el.innerHTML = httpRequest.responseText.replace("&cur;", "£");
	}
	catch (ex)
	{
	alert('EX: ' + ex.message)
	}
}
function fnDropProductIntoBasket(obj)
{
	var ProductCode;
	var UpdateBagPage;
	var ShoppingBagPage;
	var CheckoutPage;
	
	UpdateBagPage = 'AJAX/ShoppingBag.asp';
	ShoppingBagPage = 'ShoppingBag.asp';
	CheckoutPage = 'Checkout_CustomerDetails.asp';
	
	event.returnValue = false;								// Cancels default action.
	window.event.dataTransfer.dropEffect = "copy";          // Sets cursor to system copy icon.
	ProductCode = window.event.dataTransfer.getData("Text");

	//alert('ProductCode: ' + ProductCode);
	if (ValidateShoppingBagItem(ProductCode))
	{
		DDUpdateServerShoppingBag(UpdateBagPage, ShoppingBagPage, CheckoutPage, ProductCode, 'Add', '1');
	}
}