window.onload = function() 
{
	if (document.getElementById)
	{
		var aReceipt = document.getElementById('shoppingcart');
		var aShowReceiptBtn = document.getElementById('showcart');
		var aHideReceiptBtn = document.getElementById('closecart');
		var aCloseReceiptImg = document.getElementById('closereceipt');
		if ((aReceipt != null) && (aShowReceiptBtn != null) && (aHideReceiptBtn != null) && (aCloseReceiptImg != null))
		{
			aShowReceiptBtn.onclick = jsFnShowReceipt;
			aHideReceiptBtn.onclick = jsFnHideReceipt;
			aCloseReceiptImg.onclick = jsFnHideReceipt;
			aCloseReceiptImg.style.cursor = 'pointer';
		}
		
		if ((typeof aShowCart != "undefined") && (aShowCart == true)) aReceipt.style.display = 'block';

		/* Associate logo's onclick event with home page */
		var logo = document.getElementById("logo");
		if (logo != null)
		{
			logo.onclick = function() 
			{
				window.location = 'index.php';
				return false;
			}
		}

		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aCheckOutForm = document.getElementById('CheckOutForm');
		if (aCheckOutForm != null) aCheckOutForm.onsubmit = JSFnValidateCheckOutForm;

		var aCustomerDetails = document.getElementById('CustomerDetails');
		if (aCustomerDetails != null) aCustomerDetails.onsubmit = JSFnValidateCustomerDetails;

		var aProductLinks = document.getElementsByTagName("a");
		for (aProductIndex = 0; aProductIndex < aProductLinks.length; aProductIndex++)
		{
			if (aProductLinks[aProductIndex].className == 'picturelink') aProductLinks[aProductIndex].onclick = JSFnImageNewWindow;
		}
	}	
}

/******************************************************************************************************************************************/

function jsFnShowReceipt()
{
	var aReceipt = document.getElementById('shoppingcart');
	aReceipt.style.display = 'block';
	return false;
}

function jsFnHideReceipt()
{
	var aReceipt = document.getElementById('shoppingcart');
	aReceipt.style.display = 'none';
	return false;
}

/******************************************************************************************************************************************/
function JSFnImageNewWindow()
{
	window.open(this.href, 'image','width=550px,height=550px,toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no');
	return false;
}
/******************************************************************************************************************************************/


/* Validate the Checkout: Fields */
var aCheckoutFields = new Array ("terms","Please tick to confirm you have read the terms to continue.",
							     "privacy","Please tick to confirm you have read the privacy policy to continue");
/* Validate the Checkout Form: Function */
function JSFnValidateCheckOutForm()
{
	return JSFnValidateForm(aCheckoutFields);
}

	/********************************/

/* Validate the Customer Details: Fields */
var aCustomerDetailsFields = new Array ("CustomerName","Please enter your name to continue.",
							            "CustomerEmail","Please enter your email address to continue",
							            "DeliveryAddress1","Please enter the delivery address line 1 to continue",
							            "DeliveryAddress2","Please enter the delivery address line 2 to continue",
							            "DeliveryCity","Please enter the delivery city to continue",
							            "DeliveryPostCode","Please enter the delivery postcode to continue",
							            "ContactNumber","Please enter a contact telephone number to continue");
/* Validate the Customer Details Form: Function */
function JSFnValidateCustomerDetails()
{
	return JSFnValidateForm(aCustomerDetailsFields);
}

	/********************************/


var aContactRequiredFields = new Array ("Name","Please enter your name to continue");
function JSFnValidateContactForm()
{
	var aEmail = document.getElementById('Email');
	var aAddress = document.getElementById('Address');
	var aTelephone = document.getElementById('Telephone');
	var aFax = document.getElementById('Fax');
	if ((aEmail != null) && (aAddress != null) && (aTelephone != null) && (aFax != null))
	{
		if ((aEmail.value == '') && (aAddress.value == '') && (aTelephone.value == '') && (aFax.value == ''))
		{
			alert('You must provide either your address, telephone/fax number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aContactRequiredFields);
}

function JSFnValidateForm(aRequiredFields)
{
	for (aIndex = 0; aIndex < aRequiredFields.length; aIndex = aIndex + 2)
	{
		currElement = document.getElementById(aRequiredFields[aIndex]);
		if (currElement != null)
		{
			if  (   (   (currElement.type == 'text')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'password')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'checkbox')
				     && (currElement.checked == false))
				 || (   (currElement.type == 'file')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'textarea')
				     && (currElement.value == ''))
				 || (   (currElement.type == 'select-one')
				     && (currElement.value == '')))
			{
				alert(aRequiredFields[aIndex + 1]);
				return false;
			}
			else if (currElement.type == 'radio')
			{
				aIndex = aIndex + 2;
				if (!currElement.checked)
				{
					currElement = document.getElementById(aRequiredFields[aIndex]);
					if ((currElement.type == 'radio') && (!currElement.checked))
					{
						alert(aRequiredFields[aIndex + 1]);
						return false;
					}
				}
			}
		}
	}
	return true;
}
