window.onload = jsFnOnLoad;

function jsFnOnLoad()
{	
	if (document.getElementById)
	{
		aLinks = document.getElementsByTagName("a");
		for (aIndex = 0; aIndex < aLinks.length; aIndex++)
		{
			if (   (aLinks[aIndex].href.indexOf('.jpg') != -1) 
				|| (aLinks[aIndex].href.indexOf('.jpeg') != -1)
				|| (aLinks[aIndex].href.indexOf('.gif') != -1))
			{
				aLinks[aIndex].onclick = jsFnShowBigImage;
			}
			if (aLinks[aIndex].href.indexOf('.pdf') != -1) 
			{
				aLinks[aIndex].target = 'DlogPDFWindow';
			}
		}
		
		var aImages = document.getElementsByTagName("img");
		for (aIndex = 0; aIndex < aImages.length; aIndex++)
		{
			if (aImages[aIndex].className == 'productimage') 
			{
				aImages[aIndex].onclick = jsFnShowProductImage;
				aImages[aIndex].style.cursor = 'pointer';
			}
		}
		
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;
		
		var aSupportContactForm = document.getElementById('supportcontactform');
		if (aSupportContactForm != null) aSupportContactForm.onsubmit = JSFnValidateSupportContactForm;
		
		var aMailingListForm = document.getElementById('mailinglistform');
		if (aMailingListForm != null) aMailingListForm.onsubmit = JSFnValidateMailingListForm;
	}
}	

function jsFnShowBigImage()
{
	window.open(this.href, 'bigimage', 'width=600px, height=700px');	
	return false;
}

function jsFnShowProductImage()
{
	window.open(this.src, 'productimage', 'width=600px, height=700px');	
}

var aContactRequiredFields = new Array ("Name","Please enter your name to continue",
										"Company","Please enter your company name to continue",
										"PostTown","Please enter the post town",
										"Email","Please enter your email address 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);
}

var aContactSupportRequiredFields = new Array ("Name","Please enter your name to continue",
										"Company","Please enter your company name to continue",
										"Postcode","Please enter your postcode to continue",
										"Email","Please enter your email address to continue");
function JSFnValidateSupportContactForm()
{
	var aEmail = document.getElementById('email');
	var aTelephone = document.getElementById('telephone');
	if ((aEmail != null) && (aTelephone != null))
	{
		if ((aEmail.value == '') && (aTelephone.value == ''))
		{
			alert('You must provide either your telephone number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aContactSupportRequiredFields);
}

var aMailingListRequiredFields = new Array ("Name","Please enter your name to continue",
										    "Company","Please enter your company name to continue",
										    "Telephone","Please enter your telephone number to continue",
										    "Email","Please enter your email address to continue");
function JSFnValidateMailingListForm()
{
	return JSFnValidateForm(aMailingListRequiredFields);
}

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;
}

