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;
				}
			}
		}		
		/* Open Audio links in new window */
		var link;
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++)
		{
			link = links[i];
			// Find all links with a class name of "externallink"
			if (/\bexternallink\b/.exec(link.className)) link.onclick = openInNewWindow;
		}
		
		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;		
	}
 	JSFnAudioPopups();
}

function openInNewWindow()
{
	var newWindow = window.open(this.getAttribute('href'), '_blank');
	newWindow.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 ("FullName","Please enter your name to continue");

function JSFnValidateContactForm()
{
	var aEmail = document.getElementById('EmailAddress');
	var aTelephone = document.getElementById('TelephoneNo');

	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()
{
	// Check Delivery Fields
	aResult = JSFnValidateForm(DeliveryRequiredFields);
	
	// Check Tick boxes
	if (aResult != false)
	{
		var aTerms = document.getElementById('terms');
		var aPrivacy = document.getElementById('privacy');
		
		if ((! aTerms.checked) || (! aPrivacy.checked))
		{
			alert("Unfortunately, you must agree to our terms and privacy policy before purchasing from this site.");
			aResult = false;
		}
	}
	
	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 JSFnAudioPopups()
{
	var aShowAudioPopup = document.getElementById("content");
	if (aShowAudioPopup != null)
	{
		var aAudioLinks = aShowAudioPopup.getElementsByTagName("a");
		for (aIndex = 0; aIndex < aAudioLinks.length; aIndex++)
		{
		 	if (aAudioLinks[aIndex].href.indexOf('audio/') != -1) aAudioLinks[aIndex].onclick = JSFnShowAudioPopupContent;
		}
	} 
	
	var aShowAudioPopupRightColumn = document.getElementById("rightcolumn");
	if (aShowAudioPopupRightColumn != null)
	{
		var aAudioLinksRightColumn = aShowAudioPopupRightColumn.getElementsByTagName("a");
		for (aIndexRightColumn = 0; aIndexRightColumn < aAudioLinksRightColumn.length; aIndexRightColumn++)
		{
		 	if (aAudioLinksRightColumn[aIndexRightColumn].href.indexOf('popup.php') != -1) aAudioLinksRightColumn[aIndexRightColumn].onclick = JSFnShowAudioPopup;
		}
	} 	
}

function JSFnShowAudioPopup()
{
	window.open(this.href, 'Audiolink', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=340,height=100,left=200,top=200');
	
	return false;
}

function JSFnShowAudioPopupContent()
{
 	var URLArray = this.href.split('www.howardsnellarts.co.uk/');
 	//http://www.howardsnellarts.co.uk/audio/Angel04.mp3

 	var URL = URLArray[0]+'www.howardsnellarts.co.uk/popup.php?file='+URLArray[1];
	window.open(URL, 'Audiolink', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=340,height=100,left=200,top=200');
	
	return false;
}
