window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{	
    	var menu = document.getElementById("mainmenu");
 		if (menu != null) 
		{
			var aLis = menu.getElementsByTagName("li");
			for(aLiIndex = 0; aLiIndex < aLis.length; aLiIndex++)
			{
				var aSubMenus = aLis[aLiIndex].getElementsByTagName("ul");
				if (aSubMenus.length > 0)
				{
					aLis[aLiIndex].onmouseover = JSFnMenuLiOver;
					aLis[aLiIndex].onmouseout = JSFnMenuLiOut;
				}
				else
				{
					aLis[aLiIndex].onmouseover = JSFnMenuLiHover;
					aLis[aLiIndex].onmouseout = JSFnMenuLiHoverOut;
				}
			}
		}

		aProductContent = document.getElementById('content');
		if (aProductContent != null)
		{
			var aDiv = document.getElementsByTagName("div");
			var aFirst = true;
			if (aDiv != null) 
			{
				for(aDivIndex=0; aDivIndex < aDiv.length; aDivIndex++)
				{
					if(aDiv[aDivIndex].className=='tab')
					{
						if(aFirst) aFirst = false;
						else aDiv[aDivIndex].style.display = 'none';
					}
				}
			}
		}

		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;
		
		var aImageZoom = document.getElementById('productlink');
		if (aImageZoom != null) aImageZoom.onclick = JSFnImageZoom;
		
		var aCheckoutForm = document.getElementById('checkoutform');
		if (aCheckoutForm != null) aCheckoutForm.onsubmit = JSFnValidateCheckout;
		
		var aDeliveryForm = document.getElementById('deliveryinformation');
		if (aDeliveryForm != null) aDeliveryForm.onsubmit = JSFnValidateDelivery;
		
		var aProductZoom = document.getElementById('productimagezoom');
		if (aProductZoom != null) aProductZoom.onclick = JSFnProductZoom;			

		var aUserLoginForm = document.getElementById('UserLoginForm');
		if (aUserLoginForm != null) aUserLoginForm.onsubmit = JSFnValidateUserLoginForm;

		var aUserRegistrationForm = document.getElementById('UserRegistrationForm');
		if (aUserRegistrationForm != null) aUserRegistrationForm.onsubmit = JSFnValidateUserRegistrationForm;
	}
	
}

function JSFnProductZoom()
{
	var aWindow = window.open(this.href, 'productbig', 'menubar=no, resizeable=no, toolbar=no, width=650px, height=600px')
	aWindow.focus();
	return false;
}


/* User Registration Required Fields */
var aUserRegistrationRequiredFields = new Array ("FirstName","Please enter your first name to continue",
		 									 	 "Surname","Please enter a surname to continue",
		 									 	 "EmailAddress","Please enter an email address to continue",
										 		 "Password","Please enter a password to continue",
										 		 "PasswordRepeat","Please repeat password to continue");
/* User Registration Form Validation */
function JSFnValidateUserRegistrationForm()
{
	aResult =  JSFnValidateForm(aUserRegistrationRequiredFields);
	
	if(aResult == true)
	{
		if (this.Password.value.length < 6)
		{
			alert("Please enter at least 6 characters in the \"Password\" field.");
			this.Password.focus();
			aResult = false;
		}
		
		// check if both password fields are the same
		if (this.Password.value != this.PasswordRepeat.value)
		{
			alert("The two passwords are not the same.");
			this.PasswordRepeat.focus();
			aResult = false;
		}
	}
	return aResult;
}
/************************************************************************************************************************************/

/* User Registration Required Fields */
var aUserRequiredFields = new Array ("FirstName","Please enter your first name to continue",
		 							 "Surname","Please enter a surname to continue",
		 							 "EmailAddress","Please enter an email address to continue");
/* User Registration Form Validation */
function JSFnValidateUserForm()
{
	return JSFnValidateForm(aUserRequiredFields);
}
/************************************************************************************************************************************/

/* User Login Required Fields */
var aUserLoginRequiredFields = new Array ("email","Please enter an email address to continue",
									 	  "pss","Please enter a password to continue");
/* User Login Form Validation */
function JSFnValidateUserLoginForm()
{
	return JSFnValidateForm(aUserLoginRequiredFields);
}
/************************************************************************************************************************************/

function chgImg(imgField,newImg,linkField,newlink)
{
	if(document.images)
	{
		document.images[imgField].src = newImg;
		document.getElementById(linkField).setAttribute("href", newlink);
	}
}

function JSFnMenuLiOver()
{
	this.className += ' hover';
	var aSubMenus = this.getElementsByTagName("ul");
	if (aSubMenus.length > 0) 
	{	
		aSubMenus[0].parentNode.style.display = 'block';
		aSubMenus[0].style.display = 'block';
		
	}
}

function JSFnMenuLiOut()
{
	this.className = this.className.substring(0, this.className.length - 6);
	var aSubMenus = this.getElementsByTagName("ul");
	if (aSubMenus.length > 0)
	{
		aSubMenus[0].parentNode.style.display = 'none';
		aSubMenus[0].style.display = 'none';
	}
}

function JSFnMenuLiHover()
{
	this.className += ' hover';
}

function JSFnMenuLiHoverOut()
{
	this.className = this.className.substring(0, this.className.length - 6);
}
function JSFnGoBack()
{
	history.go(-1);
}

function JSFnImageZoom()
{
	var aWindow = window.open(this.href, 'productbig', 'scrollbars=yes, menubar=no, resizeable=no, toolbar=no, width=530px, height=530px')
	aWindow.focus();
	return false;
}

var aContactRequiredFields = new Array ("name","Please enter your name to continue");

function JSFnValidateContactForm()
{
	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(aContactRequiredFields);
}

CheckoutRequiredFields = new Array ("termsandconditions","You must tick to say that you agree to our terms and conditions in order to checkout.",
									"privacy","You must tick to say that you have read our privacy policy in order to continue.");
function JSFnValidateCheckout()
{
	return JSFnValidateForm(CheckoutRequiredFields);
}

DeliveryRequiredFields = new Array ("CustomerName","You must enter your name to continue.",
									"CustomerEmail","You must enter your email address to continue.",
									"DeliveryAddressLine1","You must enter the first line of your delivery address to continue.",
									"DeliveryAddressLine4","You must enter your delivery city/town to continue.",
									"DeliveryPostCode","You must enter your delivery postcode to continue.");

DeliveryBillingRequiredFields = new Array ("BillingName","You must enter your billing name to continue.",									   
										   "BillingAddressLine1","You must enter the first line of your billing address to continue.",
										   "BillingAddressLine4","You must enter your billing city/town to continue.",
										   "BillingPostCode","You must enter your billing postcode to continue.");

function JSFnValidateDelivery()
{
	aResult = JSFnValidateForm(DeliveryRequiredFields);

	if (aResult != false)
	{
		var aBillingFirstName = document.getElementById('BillingName');
		var aBillingAddress = document.getElementById('BillingAddressLine1');
		var aBillingCity = document.getElementById('BillingAddressLine4');
		var aBillingPostcode = document.getElementById('BillingPostcode');
		if ((aBillingFirstName.value != '') || (aBillingAddress.value != '') || (aBillingCity.value != '') || (aBillingPostcode.value != ''))
		{
			aResult = JSFnValidateForm(DeliveryBillingRequiredFields);	
		}
	}
	
	if (aResult != false)
	{
		aResult = JSFnValidateForm(CheckoutRequiredFields);
	}
	return aResult;
}

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;
}