window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{	
	  
		/* 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;
			}
		}
		
		/* Apply super group pop ups */
		var aIndexContent = document.getElementById('supergroups');
		if (aIndexContent != null)
		{
			var links = aIndexContent.getElementsByTagName("li");
			for(alinkindex=0;alinkindex<links.length;alinkindex++)
			{
				links[alinkindex].onmouseover = JSFnSuperLinkOver;
				links[alinkindex].onmouseout = JSFnSuperLinkOut;
			}			
		}	
		
		/* ---------------------------------------------- */
		
		/* Product Details */	
		var aProductContent = document.getElementById('producttextmenu');
		if (aProductContent != null)
		{
			var Productlinks = aProductContent.getElementsByTagName("a");
			for(aplinkindex=0; aplinkindex<Productlinks.length; aplinkindex++)
			{
				Productlinks[aplinkindex].onclick = JSFnProductLiClick;
			}			
		}
		/* ---------------------------------------------- */
		
				
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;
		
		var aImageZoom = document.getElementById('largeimage');
		if (aImageZoom != null) aImageZoom.onclick = JSFnImageZoom;
		
		var aDesignerLink = document.getElementById('designerlink');
		if (aDesignerLink != null) aDesignerLink.onclick = JSFnDesignerInfo;
		
		var aCheckoutForm = document.getElementById('checkoutform');
		if (aCheckoutForm != null) aCheckoutForm.onsubmit = JSFnValidateCheckout;
		
		var aDeliveryForm = document.getElementById('deliveryinformation');
		if (aDeliveryForm != null) aDeliveryForm.onsubmit = JSFnValidateDelivery;
		
		var aProductImage1 = document.getElementById('productimage1');
		if (aProductImage1 != null) aProductImage1.onclick = JSFnChangeProductImage;
	
		var aProductImage2 = document.getElementById('productimage2');
		if (aProductImage2 != null) aProductImage2.onclick = JSFnChangeProductImage;

		var aProductImage3 = document.getElementById('productimage3');
		if (aProductImage3 != null) aProductImage3.onclick = JSFnChangeProductImage;
		
		var aMainImage = document.getElementById('mainimage');
		if (aMainImage != null) aMainImage.onclick = JSFnShowMainImage;
	
		var aDeliveryLink = document.getElementById('deliverylink');
		if (aDeliveryLink != null) 
		{
			var aNewP = document.createElement('p');

			var aNewLink = document.createElement('a');
			aNewLink.onclick = JSFnShowDelivery;
			aNewLink.id = 'deliverylink';
			aNewLink.style.cursor = 'pointer';
			
			var aNewTextNode = document.createTextNode('Click here to enter different delivery details.');
						
			aNewLink.appendChild(aNewTextNode);
			aNewP.appendChild(aNewLink);
			aDeliveryLink.appendChild(aNewP);
			
			var aDelivery = document.getElementById('deliverydetails');			
			aDelivery.style.display = 'none';
		}
	}
	
}

function JSFnShowDelivery()
{
	var aDelivery = document.getElementById('deliverydetails');
	
	aDelivery.style.display = 'block';
	this.firstChild.nodeValue = 'Click here to hide delivery details.';
	this.onclick = JSFnHideDelivery;
	return false;
}

function JSFnHideDelivery()
{
	var aDelivery = document.getElementById('deliverydetails');
	
	aDelivery.style.display = 'none';
	this.firstChild.nodeValue = 'Click here to enter different delivery details.';
	this.onclick = JSFnShowDelivery;
	return false;
}

function JSFnProductLiClick()
{
 	var alinkid = this.id;
	var productdiv = document.getElementById("producttextcontainer");
	
	if (productdiv != null)
	{
		var aProductParas = productdiv.getElementsByTagName('div');
		var aMenuGfx = document.getElementById("producttextmenu");
		var aProductNotes = document.getElementById("productnotes");
		var aProductSizing = document.getElementById("productsizing");
		var aProductCare = document.getElementById("productcare");
				
		for(aproductindex=0;aproductindex<aProductParas.length;aproductindex++)
		{
			JSFnHideDivs(productdiv);

			if(alinkid == "productnotes") 
			{
				aMenuGfx.style.background = "transparent url(images/productnotestab1.gif) no-repeat";				 
				aProductParas[0].style.display = "block";
				aProductNotes.style.color = 'black';
				aProductSizing.style.color = '#666666';
				aProductCare.style.color = '#666666';		
			}
			else if(alinkid == "productsizing") 
			{
				aMenuGfx.style.background = "transparent url(images/productnotestab2.gif) no-repeat";	
				aProductParas[1].style.display = "block";
				aProductNotes.style.color = '#666666';
				aProductSizing.style.color = 'black';
				aProductCare.style.color = '#666666';	
			}
			else if(alinkid == "productcare") 
			{
				aMenuGfx.style.background = "transparent url(images/productnotestab3.gif) no-repeat";
				aProductParas[2].style.display = "block";			
				aProductNotes.style.color = '#666666';
				aProductSizing.style.color = '#666666';
				aProductCare.style.color = 'black';	
			}
		}
	}
	
}

function JSFnHideDivs(productdivs)
{
 	var divs = productdivs.getElementsByTagName("div");
	for(adivindex=0;adivindex<divs.length;adivindex++)
	{
		divs[adivindex].style.display = "none";
	} 
}

function JSFnSuperLinkOver()
{
 	var ps = this.getElementsByTagName("p");
	for(apindex=0;apindex<ps.length;apindex++)
	{
		ps[apindex].style.display = "block";
	}	
}

function JSFnSuperLinkOut()
{
 	var ps = this.getElementsByTagName("p");
	for(apindex=0;apindex<ps.length;apindex++)
	{
		ps[apindex].style.display = "none";
	}	
}

function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

function JSFnGoBack()
{
	history.go(-1);
}

var aContactRequiredFields = new Array ("firstname","Please enter your name to continue","surname","Please enter your surname to continue");

function JSFnValidateContactForm()
{
	var aEmail = document.getElementById('email');
	var aTelephone = document.getElementById('telephone');
	var aMobile = document.getElementById('mobile');
	if ((aEmail != null) && (aTelephone != null) && (aMobile != null))
	{
		if ((aEmail.value == '') && (aTelephone.value == '') && (aMobile.value == ''))
		{
			alert('You must provide either your telephone/mobile number or email address to continue.');
			return false;
		}
	}

	return JSFnValidateForm(aContactRequiredFields);
}

CheckoutRequiredFields = new Array ("terms","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 ("ShippingName","You must enter your delivery name to continue.",
									"ShippingEmail","You must enter your delivery email address to continue.",								   
									"ShippingAddressLine1","You must enter the first line of your delivery address to continue.",
									"ShippingCity","You must enter your delivery city/town to continue.",
									"ShippingPostcode","You must enter your delivery postcode to continue.");

DeliveryBillingRequiredFields = new Array ("BillingName","You must enter your billing name to continue.",
										   "BillingEmail","You must enter your billing email address to continue.",								   
										   "BillingAddressLine1","You must enter the first line of your billing address to continue.",
										   "BillingCity","You must enter your billing city/town to continue.",
										   "BillingPostcode","You must enter your billing postcode to continue.");

function JSFnValidateDelivery()
{
	aResult = JSFnValidateForm(DeliveryBillingRequiredFields);

	if (aResult != false)
	{
		var aDeliveryName = document.getElementById('ShippingName');
		var aDeliveryEmail = document.getElementById('ShippingEmail');
		var aDeliveryAddressLine1 = document.getElementById('ShippingAddressLine1');
		var aDeliveryAddressLine4 = document.getElementById('ShippingCity');
		var aDeliveryPostCode = document.getElementById('ShippingPostcode');
		if ((aDeliveryName.value != '') || (aDeliveryEmail.value != '') || (aDeliveryAddressLine1.value != '') || (aDeliveryAddressLine4.value != '') || (aDeliveryPostCode.value != ''))
		{
			aResult = JSFnValidateForm(DeliveryRequiredFields);	
		}
	}
	
	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;
}

function JSFnChangeProductImage()
{
	var aMainImage = document.getElementById('mainimage');
	if (aMainImage != null) 
	{
		aOldMainSource = aMainImage.src.replace('/productimages/medium/', '/productimages/');
		aMainImage.src = this.src.replace('/productimages/', '/productimages/medium/');
		this.src = aOldMainSource;
	}
}

function JSFnShowMainImage()
{
 	var aNewWindow;
 	var aProductID = document.getElementById('ProductID');
 	if (aProductID != null)
 	{
	 	aNewWindow = window.open('productimages.php?id=' + aProductID.value + '&start=' + this.src.substr(this.src.lastIndexOf("/") + 1, this.src.length), 'productimages', 'width=650px,height=920px,status=no,toolbar=no,scrollbars=yes'); 	 		
 	}
 	else
 	{
	 	aNewWindow = window.open(this.src.replace('/productimages/medium/', '/productimages/large/'), 'productimages', 'width=650px,height=920px,status=no,toolbar=no,scrollbars=yes');
	}
	aNewWindow.focus();
	return false;
}

function JSFnImageZoom()
{
 	var aNewWindow = window.open(this.firstChild.href, 'productimages', 'width=650px,height=920px,status=no,toolbar=no,scrollbars=yes'); 	 		
	aNewWindow.focus();
	return false;
}

function JSFnDesignerInfo()
{
 	var aNewWindow = window.open(this.firstChild.href, 'productimages', 'width=650px,height=5000px,status=no,toolbar=no,scrollbars=yes'); 	 		
	aNewWindow.focus();
	return false;
}