window.onload = jsFnOnload;

function jsFnOnload()
{
	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';

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

		var aCommentsForm = document.getElementById('commentsform');
		if (aCommentsForm != null) aCommentsForm.onsubmit = JSFnValidateCommentsForm;

		var aConfirmForm = document.getElementById('customerdetails');
		if (aConfirmForm != null) aConfirmForm.onsubmit = JSFnValidateConfirmForm;

		var aSnowChainForm = document.getElementById('snowchainform');
		if (aSnowChainForm != null) aSnowChainForm.onsubmit = JSFnValidateSnowChainForm;

		var aRoofBarForm = document.getElementById('roofbarform');
		if (aRoofBarForm != null) aRoofBarForm.onsubmit = JSFnValidateRoofBarForm;

		var aCheckoutForm = document.getElementById('checkoutform');
		if (aCheckoutForm != null) aCheckoutForm.onsubmit = JSFnValidateCheckout;

		var aAmTrack = document.getElementById("amtrack");
		if (aAmTrack != null) aAmTrack.onclick = JSFnAmtrackTracking;

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

CheckoutRequiredFields = new Array ("terms","You must tick to say that you agree to You want we have' terms and conditions in order to checkout.");
function JSFnValidateCheckout()
{
	return JSFnValidateForm(CheckoutRequiredFields);
}


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;
}

function JSFnAmtrackTracking()
{
	window.open('http://www.amtrak.co.uk/cgi-bin/tracking.cgi', 'OrderTracking','width=650px,height=700px,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	return false;
}


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);
}

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

/* Validate the Comments Form: Fields */
var aCommentsRequiredFields = new Array ("Name","Please enter your name to continue",
									   "EmailAddress","Please enter your email address to continue",
									   "Details","Please enter your comment to continue");
/* Validate the Commments Form: Function */
function JSFnValidateCommentsForm()
{
	return JSFnValidateForm(aCommentsRequiredFields);
}

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

var aConfirmRequiredFields = new Array ("CustomerName","Please enter your name to continue",
										"CustomerEmail", "Please enter an email address to continue",
										"DeliveryAddress", "Please enter a delivery address to continue",
										"DeliveryPostCode","Please enter a delivery postcode to continue",
										"ContactNumber","Please enter a phone number to continue");
function JSFnValidateConfirmForm()
{
	var aName = document.getElementById('CustomerName');
	if (aName != null)
	{
		if (aName.value == '')
		{
			alert('Please enter your name to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aConfirmRequiredFields);
}

var aSnowChainRequiredFields = new Array ("RimSize","Please select a rim size",
										"TyreSize", "Please select a tyre size");
function JSFnValidateSnowChainForm()
{
	var aRim = document.getElementById('RimSize');
	var aTyre = document.getElementById('TyreSize');
	
	if ((aRim != null) && (aTyre != null))
	{
		if ((aRim.value == '0') && (aTyre.value == '0'))
		{
			alert('You must provide a rim and tyre size to continue.');
			return false;
		}
	}
	return JSFnValidateForm(aSnowChainRequiredFields);
}

var aRoofBarRequiredFields = new Array ("CarModel", "Please select a car model");
function JSFnValidateRoofBarForm()
{
	var aCar = document.getElementById('CarModel');
	
	if ((aCar != null))
	{
		if ((aCar.value == '0'))
		{
			alert('You must select the model of car to continue.');
			return false;
		}
	}
	return JSFnValidateForm(aRoofBarRequiredFields);
}

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 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;
}
