// onclick event for submit


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function check_hearabout(selectbox)
{
	if (selectbox.options[selectbox.selectedIndex].value=='Other')	
	{
		MM_showHideLayers('FindoutLayer','','show')
		document.bookonline.other.focus();
	} 
	else
	{	document.bookonline.other.value='';
		MM_showHideLayers('FindoutLayer','','hide')
	}
}

function onSubmit()
{
	if (!document.getElementById("bookonline")) return false; // check if the form exists (stops error on form submitted confirmation
	document.forms["bookonline"].onsubmit = validateFields;
	return true; 
}

addLoadEvent(onSubmit);

//

// Clear existing error messages created on the page

function clearErrors(){
	if (!document.getElementById("error")) return false;
	var spanMarkup = document.getElementById("error");
	var labelMarkup = spanMarkup.parentNode;
	labelMarkup.removeChild(spanMarkup);
}

//

function validateFields() {

	var contactName = document.forms["bookonline"]["contactname"];
	
	if (contactName.value == "") {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG";
		errorSpan.id = "error";
		
		var fieldLabel = contactName.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		contactName.focus();/* focus on */	
		fadeUpErrors();
		return false;
	}
	else {
		clearErrors();
	}
	

	var companyName = document.forms["bookonline"]["companyname"];
	
	if (companyName.value == "") {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG";
		errorSpan.id = "error";
		
		var fieldLabel = companyName.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		companyName.focus();/* focus on */	
		fadeUpErrors();
		return false;
	}
	else {
		clearErrors();
	}


	// validate 'tel number' address exists	
	var telNumber = document.forms["bookonline"]["telnumber"];
	
	if (telNumber.value == "") {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG";
		errorSpan.id = "error";
		
		var fieldLabel = telNumber.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		telNumber.focus();/* focus on */	
		fadeUpErrors();
		return false;
	}
	else {
		clearErrors();
	}
	
	var emailAddress = document.forms["bookonline"]["email"];
	
// validate email address exists
	
	if (emailAddress.value == "") {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG";
		errorSpan.id = "error";
		
		var fieldLabel = emailAddress.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		fadeUpErrors();
		emailAddress.focus();/* focus on */
		return false;
	}
	else {
		clearErrors();
	}
	
// validate email address is valid
	
	if (emailAddress.value.indexOf('@') < 0 || emailAddress.value.indexOf('.') < 0 || emailAddress.value.length<5) {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Not valid");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG";
		errorSpan.id = "error";
		
		var fieldLabel = emailAddress.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		fadeUpErrors();
		emailAddress.focus();/* focus on */
		return false;
	}
	else {
		clearErrors();
	}
	
// validate additional infomation exists		

var courseNames = document.forms["bookonline"]["coursenames"];
	
	if (courseNames.value == "") {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required - please give details...");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG-inline";
		errorSpan.id = "error";
		
		var fieldLabel = courseNames.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		fadeUpErrors();
		courseNames.focus();/* focus on */
		return false;
	}
	else {
		clearErrors();
	}


// validate hear about college chosen		

var check_HearAbout = document.forms["bookonline"]["optionselect"];
	
	if (check_HearAbout.selectedIndex==0) {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG-inline";
		errorSpan.id = "error";
		
		var fieldLabel = check_HearAbout.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		fadeUpErrors();
		check_HearAbout.focus();/* focus on */
		return false;
	}
	else {
		clearErrors();
	}	
	

// validate nature of course	

var check_NatureCourse = document.forms["bookonline"]["natureofcourse"];
	
	if (check_NatureCourse.selectedIndex==0) {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG-inline";
		errorSpan.id = "error";
		
		var fieldLabel = check_NatureCourse.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		fadeUpErrors();
		check_NatureCourse.focus();/* focus on */
		return false;
	}
	else {
		clearErrors();
	}
	
// validate college contacting customer info	

var check_NatureCourse = document.forms["bookonline"]["customerintention"];
	
	if (check_NatureCourse.selectedIndex==0) {
		clearErrors();
		var errorSpan = document.createElement("span");
		var errorMessage = document.createTextNode("Required");
		
		errorSpan.appendChild(errorMessage);
		errorSpan.className = "errorMSG-inline";
		errorSpan.id = "error";
		
		var fieldLabel = check_NatureCourse.previousSibling;
		
		while (fieldLabel.nodeName.toLowerCase() != "label")
		{
		  fieldLabel = fieldLabel.previousSibling;
		}
		fieldLabel.appendChild(errorSpan);
		fadeUpErrors();
		check_NatureCourse.focus();/* focus on */
		return false;
	}
	else {
		clearErrors();
	}		
	
	// if everything validates then...	
	return true;
}
