window.onload = function() 
{
	if (document.getElementById)
	{
		var logo = document.getElementById("logo");
		if (logo != null)
		{
				logo.onclick = function() 
				{
					window.location = 'index.php';
					return false;
				}
		}

		var paypalcards = document.getElementById("paypalcards");
		if (paypalcards != null)
		{
				paypalcards.onclick = JSFnPaypalNewWindow;
		}

		var productimage = document.getElementById("ProductImage");
		if (productimage != null)
		{
				productimage.onclick = JSFnImageNewWindow;
		}
		
		var leftcolumn = document.getElementById("leftcolumn");
		if (leftcolumn != null)
		{
			var aBoxedProducts = leftcolumn.getElementsByTagName("ul");
			for (aBoxIndex = 0; aBoxIndex < aBoxedProducts.length; aBoxIndex++)
			{
				if (aBoxedProducts[aBoxIndex].className == 'indexproducts')
				{
					var aLIs = aBoxedProducts[aBoxIndex].getElementsByTagName("li");
					for (aLIIndex = 0; aLIIndex < aLIs.length; aLIIndex++)
					{
						var aProductLinks = aLIs[aLIIndex].getElementsByTagName("a");
						for (aProductIndex = 0; aProductIndex < aProductLinks.length; aProductIndex++)
						{
							aAltText = aProductLinks[aProductIndex].innerHTML;
							aLIs[aLIIndex].onclick = JSFnIndexProductLink;
							aLIs[aLIIndex].onmouseover = JSFnIndexProductHover;
							aLIs[aLIIndex].onmouseout = JSFnIndexProductExit;
						}
					}
				}
			}
		}

		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aCalcForm = document.getElementById('floorcalc');
		if (aCalcForm != null) aCalcForm.onsubmit = JSFnValidateCalcForm;

		var aProductForm = document.getElementById('productform');
		if (aProductForm != null) aProductForm.onsubmit = JSFnValidateProductForm;

		var aCheckoutForm = document.getElementById('viewcheckout');
		if (aCheckoutForm != null) aCheckoutForm.onsubmit = JSFnValidateCheckout;
	}
}

//floor calculator validation
//*********************************************************************************
var aFloorCalcRequiredFields = new Array ("Width","Please enter the width of the room",
										"Long","Please enter the length of the rooom"
										);

function JSFnValidateCalcForm()
{
	aOK =  JSFnValidateForm(aFloorCalcRequiredFields);
	
	if (aOK == true)
	{
		aOK =  JSFnFloorCalcSpecificChecks();
	}
	
	return aOK;
}


function JSFnFloorCalcSpecificChecks()
{
	Result = true;
	aWidth = document.getElementById('Width');
	aLong = document.getElementById('Long');

	if (isNaN(aWidth.value))
	{
		alert('The width field must only contain numbers');
		Result = false;
	}
	else if (aWidth.value < 1) 
	{
		alert('The width field must be positive');
		Result = false;	
	}
	else if (isNaN(aLong.value))
	{
		alert('The length field must only contain numbers');
		Result = false;
	}
	else if (aLong.value < 1) 
	{
		alert('The length field must be positive');
		Result = false;	
	}
	
	return Result;
}
//*********************************************************************************

function JSFnIndexProductLink()
{
	var aProductLinks = this.getElementsByTagName("a");
	for (aProductIndex = 0; aProductIndex < aProductLinks.length; aProductIndex++)
	{
		window.location = aProductLinks[aProductIndex].href; 
	}
	return false;
}

function JSFnIndexProductHover()
{
	this.style.backgroundColor = '#FFF';
	this.style.cursor = 'pointer';
}

function JSFnIndexProductExit()
{
	this.style.backgroundColor = '#ECECDF';
}

function JSFnPaypalNewWindow()
{
	window.open(this.href, 'paypal','width=400px,height=450px,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	return false;
}

function JSFnImageNewWindow()
{
	window.open(this.href, 'paypal','width=550px,height=380px,toolbar=no,location=no,directories=no,status=no,menubar=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);
}

ProductFormRequiredFields = new Array ("MeasuredWidth","You must enter a width to calculate the price.",
									   "MeasuredDrop","You must enter a drop to calculate the price.");
function JSFnValidateProductForm()
{
	return JSFnValidateForm(ProductFormRequiredFields);
}

CheckoutRequiredFields = new Array ("terms","You must tick to say that you agree to Real Wood Floors' terms and conditions in order to checkout.");
function JSFnValidateCheckout()
{
	return JSFnValidateForm(CheckoutRequiredFields);
}

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;
}