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;
			}
		}
		
		var menu = document.getElementById("topmenu");
 		if (menu != null) 
		{
			var lis = menu.getElementsByTagName("li");
			for(aliindex=0;aliindex<lis.length;aliindex++)
			{
				if(lis[aliindex].className=='level1')
				{
					lis[aliindex].onclick = JSFnMenuLiClick;
					
					lis[aliindex].onmouseover = JSFnMenuLiOver;
					lis[aliindex].onmouseout = JSFnMenuLiOut;
				}
			}
		}		
		
		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');
		
		aLink = document.getElementById('morecountrieslink');
		aLink.onclick = JsFnShowPopup;
		
		//aLink = document.getElementById('morecountrieslink');
		//aLink.onclick = JsFnShowPopup;

	}
	JSFnAddLightboxMarkup();
	
	//var FShowBasket = false;
	
	if(FShowBasket) JsFnShowBasketPopup();
}

function chgImg(imgField,newImg)
{
	document.getElementById(imgField).src = newImg;
}
 

function JsFnShowPopup()
{
	var aOverlay = document.getElementById('overlay');
	if (aOverlay != null)
	{	
		aOverlay.style.display = 'block';
		
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = '100%';
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = '100%';
		
		aDiv = document.createElement('div');
		aDiv.id = 'popupdialog';
		aDiv.className = 'popupdialog';
		
		aText = document.getElementById('morecountries').innerHTML;
		aDiv.innerHTML = aText;
		
		aLink = document.createElement('a');
		aText = document.createTextNode('Close');
		aLink.href = '#';
		aLink.id = 'closepopup';
		aLink.onclick = JSFnHidePopup;
		aLink.appendChild(aText);
		aDiv.appendChild(aLink);

		bod.appendChild(aDiv);
	}
	
	return false;
}


function JsFnShowBasketPopup()
{
	var aOverlay = document.getElementById('overlay');
	if (aOverlay != null)
	{	
		aOverlay.style.display = 'block';
		
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = '100%';
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = '100%';
		
		aDiv = document.createElement('div');
		aDiv.id = 'popupdialog';
		aDiv.className = 'popupdialog basketadd';
		
		aText = '<h3 class="addedpopup">You have just placed:</h3><br /><h3 id="addedlink"><a href="'+ FAddedPageID +'_'+ FAddedProductID +'.php">' + FAddedProductName +'</a></h3> <br /> in your basket. <small>This window will automatically close in 3 seconds</small>';
		
		aDiv.style.width = "400px";
		aDiv.style.height = "140px";
		aDiv.style.margin = "0 0 0 -170px";
		aDiv.innerHTML = aText;
		
		aLink = document.createElement('a');
		aText = document.createTextNode('Close');
		aLink.href = '#';
		aLink.id = 'closepopup';
		
		aLink.onclick = JSFnHidePopup;
		aOverlay.onclick = JSFnHidePopup;
		
		aLink.appendChild(aText);
		aDiv.appendChild(aLink);
		
		bod.appendChild(aDiv);
		
		var close = setTimeout("JSFnHidePopup()", 3000);
	}
	
	return false;
}



function JSFnHidePopup()
{
	var aOverlay = document.getElementById('overlay');
	var aPopup = document.getElementById('popupdialog');
	if ((aOverlay != null) && (aPopup != null))
	{
		aOverlay.style.display = 'none';
		aPopup.parentNode.removeChild(aPopup);
	}
	return false;
}

function JSFnAddLightboxMarkup() 
{
	bod 			= document.getElementsByTagName('body')[0];
	overlay 		= document.createElement('div');
	overlay.id		= 'overlay';
	overlay.onclick = JSFnHidePopup;
	bod.appendChild(overlay);
}

function JSFnProductZoom(aLocation)
{
	var aWindow = window.open(aLocation, 'productbig', 'menubar=no, resizeable=no, toolbar=no, width=600px, height=600px')
	aWindow.focus();
	return false;
}

function JSFnMenuLiOver()
{
	this.className = 'level1 hover';
}

function JSFnMenuLiOut()
{
	this.className = 'level1';
}

function JSFnMenuLiClick()
{
	window.location = this.childNodes[0].href;
	//alert(this.childNodes[0]);
}

function JSFnGoBack()
{
	history.go(-1);
}

function JSFnImageZoom()
{
	var aWindow = window.open(this.href, 'productbig', 'scrollbars=yes, menubar=no, resizeable=no, toolbar=no, width=600px, height=800px')
	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 ("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 ("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);	
		}
	}
	
	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 JSFnResetForm(e, parentdistance) {
	
	if(parentdistance != null && parentdistance == 3) formname = e.parentNode.parentNode.parentNode.name;
	else formname = e.parentNode.parentNode.name;
	
	inputlength = document.forms[formname].elements.length;
	
	for (var i=0; i < inputlength; i++) {			
		var type = document.forms[formname].elements[i].type;
		
		if(type == 'select-one') { 
			var name = document.forms[formname].elements[i].name;
			var currentselectedindex = document.forms[formname].elements[name].selectedIndex;
			
			if(currentselectedindex != 0)	
			{
				document.forms[formname].elements[name].selectedIndex = 0;
			}
			
			document.forms[formname].elements.QuickSearch.value = '';
		}
	};
}