window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{	
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;

		var aGlossarySearchForm = document.getElementById('glossarysearchform');
		if (aGlossarySearchForm != null) aGlossarySearchForm.onsubmit = JSFnGetFirstListItem;

		var aBookingForm = document.getElementById('BookingForm');
		if (aBookingForm != null) aBookingForm.onsubmit = JSFnValidateBookingForm;
		
		var aNewsletterForm = document.getElementById('newsletter');
		if (aNewsletterForm != null) aNewsletterForm.onsubmit = JSFnValidateNewsletterForm;
		
		var aEmailFriendForm = document.getElementById('emailfriendform');
		if (aEmailFriendForm != null) aEmailFriendForm.onsubmit = JSFnValidateEmailFriendForm;
		
		var aImageZoom = document.getElementById('courselink');
		if (aImageZoom != null) aImageZoom.onclick = JSFnImageZoom;
		
		var aFlashPresentation = document.getElementById('presentation');
		if (aFlashPresentation != null) aFlashPresentation.onsubmit = JSFnFlashPresentation;		

		var aProductLinks = document.getElementsByTagName("a");
		for (aProductIndex = 0; aProductIndex < aProductLinks.length; aProductIndex++)
		{
			if (aProductLinks[aProductIndex].className == 'picturelink') 
			{
				aProductLinks[aProductIndex].onclick = JSFnImageNewWindow;
				aProductLinks[aProductIndex].childNodes[0].alt = 'Click here for a larger image';
			}
		}

		var aTestimonials = document.getElementById('testimonialslist');
		if (aTestimonials != null) JSFnTestimonialsDisplay();		

		JsFnAjaxOnLoad();
	}	
}
/*****************************************************************************************************************************************/
function JSFnGetFirstListItem()
{
	var aGlossaryList = document.getElementById('glossarylist');
	if (aGlossaryList != null) 
	{
		var aGlossaryListItems = aGlossaryList.getElementsByTagName("li");
		if(aGlossaryListItems.length > 0)
		{
			window.location = aGlossaryListItems[0].childNodes[0].href;
			return false;
		}
	}
}
/*****************************************************************************************************************************************/
function JSFnImageNewWindow()
{
	window.open(this.href, 'image','');
	return false;
}
//**************************************************************************************************************************************

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

function JSFnImageZoom()
{
	var aWindow = window.open(this.href, 'coursebig', '')
	aWindow.focus();
	return false;
}
//**************************************************************************************************************************************

function JSFnFlashPresentation()
{
	theObjects = document.getElementsByTagName("object");
	for (var i = 0; i < theObjects.length; i++)
	{
		theObjects[i].outerHTML = theObjects[i].outerHTML;
	}
}
//**************************************************************************************************************************************

var aContactRequiredFields = new Array ("FirstName","Please enter your first name to continue",
										"LastName","Please enter your last name to continue",
										"Company","Please enter a company to continue",
										"OfficePhone","Please enter an office phone to continue",
										"Email","Please enter an email to continue",
										"Comments","Please enter some comments to continue");

function JSFnValidateContactForm()
{
	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);
}
/****************************************************************************************************************************************/

/****************************************************************************************************************************************/
NewsletterRequiredFields = new Array ("mb-name","Please enter your name to continue.",
									  "mb-kjirk-kjirk","Please enter your email address to continue");
function JSFnValidateNewsletterForm()
{
	return JSFnValidateForm(NewsletterRequiredFields);
}
/****************************************************************************************************************************************/

/****************************************************************************************************************************************/
EmailFriendRequiredFields = new Array ("YourName","Please enter your name to continue.",
									   "YourEmailAddress","Please enter your email address to continue",
									   "FriendsName","Please enter your friends name to continue",
									   "FriendsEmailAddress","Please enter your friends email address to continue")
function JSFnValidateEmailFriendForm()
{
	return JSFnValidateForm(EmailFriendRequiredFields);
}
/****************************************************************************************************************************************/

/****************************************************************************************************************************************/
BookingFormRequiredFields = new Array ("FirstName","Please enter your first name to continue.",
									   "LastName","Please enter your last name to continue",
									   "TelephoneNo","Please enter your office phone to continue",
									   "EmailAddress","Please enter your email address to continue")
function JSFnValidateBookingForm()
{
	return JSFnValidateForm(BookingFormRequiredFields);
}
/****************************************************************************************************************************************/

/****************************************************************************************************************************************/
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 JSFnTestimonialsDisplay()
{
	var aTestimonials = document.getElementById("testimonialslist");
	if (aTestimonials != null)
	{
		var aDiv = aTestimonials.getElementsByTagName("div");
		for (aIndex = 0; aIndex < aDiv.length; aIndex++)
		{
			if (aIndex == 0) aDiv[aIndex].style.display = 'block';
			else aDiv[aIndex].style.display = 'none';
		}
	}

	setInterval("JSFnAlternateTestimonialDisplay()", 10000);
}

var fMarker = 1;

function JSFnAlternateTestimonialDisplay()
{
	var aTestimonials = document.getElementById("testimonialslist");
	if (aTestimonials != null)
	{
		var aDiv = aTestimonials.getElementsByTagName("div");
		for (aIndex = 0; aIndex < aDiv.length; aIndex++)
		{
			if(fMarker == aIndex) aDiv[aIndex].style.display = 'block';
			else aDiv[aIndex].style.display = 'none';
		}
		fMarker++;
		if(fMarker >= aDiv.length) fMarker = 0;
	}
}