window.onload = jsFnOnload;

function jsFnOnload()
{
	if (document.getElementById)
	{	
		var aBookingForm = document.getElementById('BookingsForm');
		if (aBookingForm != null) aBookingForm.onsubmit = JSFnValidateBookingForm;
		
		var aContactForm = document.getElementById('contactform');
		if (aContactForm != null) aContactForm.onsubmit = JSFnValidateContactForm;
		
		var aImageZoom = document.getElementById('largeimage');
		if (aImageZoom != null) aImageZoom.onclick = JSFnImageZoom;
		
		var aRegisterHorse = document.getElementById('RegisterHorse');
		if (aRegisterHorse != null) aRegisterHorse.onsubmit = JSFnValidateRegisterHorse;
	
		var sfEls = document.getElementById("mainmenu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className += " sfhover";
			}
			sfEls[i].onmouseout=function() {
			if (this.className == 'selected sfhover') this.className="selected";
			else this.className="";
			}
		}		
	}	
}

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

//************************************************************************************************************************
function JSFnImageZoom()
{
	var aWindow = window.open(this.href, 'productbig', 'menubar=no, resizeable=no, toolbar=no, width=600px, height=600px')
	aWindow.focus();
	return false;
}
//************************************************************************************************************************
/* Validate Form: Booking Form */
var aBookingRequiredFields = new Array ("AppointmentDate","Please enter an appointment date to continue",
										"AppointmentTime","Please enter an appointment time continue",
										"ReferringVet","Please enter the referring veterinary practice to continue",
										"ReferringVetAddress","Please enter the referring veterinary practice address to continue",
										"ReferringVetSurgeon","Please enter the referring veterinary surgeon to continue",
										"ReferringVetPhoneNo","Please enter the referring veterinary phone number to continue",
										"OwnersName","Please enter the owners name to continue",
										"OwnersAddress","Please enter the owners address to continue",
										"OwnersPhoneNo","Please enter the owners phone number to continue",
										"HorseName","Please enter the horse name to continue",
										"HorseType","Please enter the type of horse to continue",
										"HorseUse","Please enter the use of the horse to continue",
										"HorseSize","Please enter the size of the horse to continue",
										"HorseInsured","Please select if the horse is insured to continue",
										"Problem","Please enter the problem to continue")
function JSFnValidateBookingForm()
{
	aResult = JSFnValidateForm(aBookingRequiredFields);

	if(aResult == true)
	{
		if (this.HorseInsured.value == 'Yes') 
		{
			var aBookingFields = new Array ("HorseInsuredComapany","Please enter the insurance company name to continue");
			aResult = JSFnValidateForm(aBookingFields);
		}
	}

	return aResult;
}
//************************************************************************************************************************
/* Validate Form: Register Form */
var aRegisterRequiredFields = new Array ("FullName","Please enter your full name to continue",
										 "Address1","Please enter address 1 continue",
										 "Address2","Please enter address 2 to continue",
										 "Town","Please enter a town to continue",
										 "County","Please enter a county to continue",
										 "Postcode","Please enter a postcode to continue",
										 "TelephoneNo","Please enter a telephone number to continue",
										 "HorseName","Please enter the name of the horse to continue",
										 "Age","Please enter the age of the horse to continue",
										 "Breed","Please enter the breed of the horse to continue")
function JSFnValidateRegisterHorse()
{
	aResult = JSFnValidateForm(aRegisterRequiredFields);

	return aResult;
}
//************************************************************************************************************************

//************************************************************************************************************************
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);
}
//************************************************************************************************************************

//************************************************************************************************************************
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;
}
//************************************************************************************************************************
//************************************************************************************************************************