var slider, action=0, mT, toPay, orderRef, signDefault = new Array(), title1, title2, thisPage2,menuMoving=0,menuOver,mt1;;

// GENERAL

function messageBox(message,secondsOut) {
	i='messageBox';
	if (mT) clearTimeout(mT);
	mT=null;
	if ($(i) != null) {
		$(i).update(message);
		centreElement (i,1);
		$(i).show();
		mT = setTimeout(function () { $(i).hide(); }, (secondsOut*1000));
	}
	else {
		alert('Message box doesn\'t exist');	
	}
}

function centreElement (e, vert) {
	var theScrolls = document.viewport.getScrollOffsets();
	theLeft = Math.floor(((document.viewport.getWidth())-($(e).getWidth()))/2) + theScrolls[0];
	theTop = Math.floor(((document.viewport.getHeight())-($(e).getHeight()))/2) + theScrolls[1];
	if (theTop < 0) theTop = 0;
	if (vert==1)  {
		$(e).setStyle({ top: '0px' });
		$(e).setStyle({ top: theTop+'px' });
	}
	$(e).setStyle({ left: theLeft+'px' });
	
}

function pageOverlay() {
	var theScrolls = document.viewport.getScrollOffsets();
	pageWidth = document.viewport.getWidth() + theScrolls[0]; pageHeight = document.viewport.getHeight() + theScrolls[1];
	if (pageWidth < $('mainContainer').getWidth()) pageWidth = $('mainContainer').getWidth();
	if (pageHeight < ($('mainContainer').getHeight()+127)) pageHeight = ($('mainContainer').getHeight()+127);
	$('pageOverlay').setStyle({ opacity: '0.7', display: 'block', width:pageWidth+'px', height:pageHeight+'px' });
	$('pageOverlay').show();
}

function closeBox(id) {
	if (id=='videoBox') $('theVid2').update('');
	$('pageOverlay').hide();
	$(id).hide();
}

function showBox(id) {
	pageOverlay();
	centreElement (id,1);
	$(id).show();
}

function buttonMouseOver (buttonID,over,cOn,cOff) {
	var buttonWidth = $(buttonID).getWidth();
	linkID = $(buttonID).firstDescendant();
	if (over==1) {
		$(buttonID).setStyle({ backgroundPosition: '-'+buttonWidth+'px 0' });
		$(linkID.id).setStyle({ color:cOn });
	}
	else {
		$(buttonID).setStyle({ backgroundPosition: '0 0' });
		$(linkID.id).setStyle({ color:cOff });
	}
}

function sortButtons (p) {
	var a=$$('div.'+p+'Button');
	a.each(function(n){
		$(n.id).observe('mouseover', function () { buttonMouseOver (n.id,1,'#001d28','#b0b0b0'); });
		$(n.id).observe('mouseout', function () { buttonMouseOver (n.id,0,'#001d28','#b0b0b0'); });
   	});
	var a2=$$('div.'+p+'Button2');
	a2.each(function(n){
		$(n.id).observe('mouseover', function () { buttonMouseOver (n.id,1,'#2c2c2c','#00202d'); });
		$(n.id).observe('mouseout', function () { buttonMouseOver (n.id,0,'#2c2c2c','#00202d'); });
   	});
}

function careerNewsLinkMouseOver (divID,over) {
	var linkID = $(divID).firstDescendant();
	if (over==1) {
		$(divID).setStyle({ backgroundColor: '#002433' });
		$(linkID.id).setStyle({ color:'#FFF' });
	}
	else {
		$(divID).setStyle({ backgroundColor: '' });
		$(linkID.id).setStyle({ color:'#002433' });
	}
}

function clearInputBox(i,t,o,c) {
	if (o==1) {
		if ($F(i) == t) $(i).value='';
	}
	else if (o==2) {
		$(i).observe('focus', function () { clearInputBox(i,t,1,c); });
		$(i).observe('blur', function () { clearInputBox(i,t,0,c); });
	}
	else {
		if ($F(i) == '') $(i).value=t;	
	}
	if (c) { $(i).setStyle({ color: c }); }
}

function isValidEmail2(s) {
	return (s.indexOf(".") > 2) && (s.indexOf("@") > 0);
}

function isValidEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	return true;			
}

function handle(e) { // MOUSE WHEEL FOR SCROLLER
	var wheelVal = Event.wheel(e);
	wheelVal = wheelVal/100;
	slider.setValueBy(-wheelVal);
}

function openCloseMenu (t,i,d) {
	if (menuMoving<1) {
		if (t==1 && $(i).visible()==false) {
			menuMoving=1;
			Effect.BlindDown(i, { duration:d,afterFinish:function () {
				menuMoving=0
			}});
		}
		else if (i != 'dropCourseMenu' && i != 'dropVenueMenu') {
			var s = i.replace("coursesList","");
			if ($('courseType'+s) != null) window.location.href = $('courseType'+s).href;
		}
		else if (t==2) {
			menuOver=0;
			clearTimeout(mt1);
			mt1='';
			mt1 = setTimeout(function() {
				if (menuOver<1) {
					menuMoving=1;
					Effect.BlindUp(i, { duration:d,afterFinish:function () {
						menuMoving=0;
						var a = $$('ul[id^="coursesList"]');
						a.each(function(n){
							$(n.id).hide();
						});
					}});
				}
			},500);
		}
	}
}

// CAREERS PAGES

function setUpCareers () {
	openCloseCareer();
}

function openCloseCareer (n) {
	if (n != null) {
		var h = 52 + $('career_title_'+n).getHeight();
		if (h < ($('career_'+n).getHeight()-20)) {
			$('career_'+n).setStyle({ height:h+'px' });
			$('career_'+n+'_text').setStyle({ height:h+'px' });
			$('buttonLink_'+n+'_1').update('More');
			$('buttonLink_'+n+'_2').hide();
		}
		else {
			$('buttonLink_'+n+'_1').update('Close');
			$('career_'+n).setStyle({ height:'auto' });
			$('career_'+n+'_text').setStyle({ height:'auto' });
			$('buttonLink_'+n+'_2').show();
		}
	}
	else {
		var a = $$("a[id^='buttonLink']");
		a.each(function(n){
			y=n.id.replace('buttonLink_','');
			y=y.substr(0,1);
			y3 = n.id.length;
			y2=n.id.substr((y3-1),1);
			if (y2==1) {
				var m = y;
				$(n.id).observe('click', function () { openCloseCareer (m); });
				openCloseCareer (y);
			}
   		});
	}
}



// FAQS PAGES

function setUpFaqs (id) {
	openFaqs = new Array('0','1');
	var a = $$("[id^='l']"),y;
	a.each(function(n){
		$(n.id).observe('click', function () { openCloseFaq (n.id); });
		y=n.id.replace('l','');
		if (y>1) $('t'+y).hide();
   	});
}

function openCloseFaq (id) {
	var y=id.replace('l','');
	if (openFaqs[y] == 1 && action==0) {
		action=1;
/*
		$('hl'+y).setStyle({ backgroundImage: 'url('+pageURL+'images/backgrounds/faqs_title_dark_left.png)' });
		$('hr'+y).setStyle({ backgroundImage: 'url('+pageURL+'images/backgrounds/faqs_title_dark_gradient.png)' });
*/
		Effect.BlindUp('t'+y, { duration: 0.5, afterFinish: function () { action=0; } });
		openFaqs[y]=0;
	}
	else if (action==0) {
		action=1;
		Effect.BlindDown('t'+y, { duration: 0.5, afterFinish: function () { action=0; } });
/*
		$('hl'+y).setStyle({ backgroundImage: 'url('+pageURL+'images/backgrounds/faqs_title_light_left.png)' });
		$('hr'+y).setStyle({ backgroundImage: 'url('+pageURL+'images/backgrounds/faqs_title_light_gradient.png)' });
*/
		openFaqs[y]=1;
	}
}



// HOME PAGE

function setUpHome () {
	
	if ($('courseSelect') != null) {
		
		Event.observe('courseForm', 'submit', function (e) { Event.stop(e); addToCart2 (); });
		Event.observe('courseSelect', 'change', doLocationDropdown);
		$('location').disable();
		$('date').disable();
		Event.observe('location', 'change', doDateDropdown);
		Event.observe('date', 'change', getCoursePrice);
		getCoursePrice ()
	}
	if ($('newsletterSignup') != null) {
	
		$('nlemail').observe('focus', function (e) {
			
			if ( $F('nlemail') == 'Your email address here.' ) $('nlemail').setValue('');
			
		});
		$('nlemail').observe('blur', function (e) {
			
			if ( $F('nlemail') == '' ) $('nlemail').setValue('Your email address here.');
			
		});
		
		
		
		$('newsletterSignup').observe('submit', function (e) {
			Event.stop(e);
			if (isValidEmail($F('nlemail'))) {
			
			
			var allNodes = Form.serialize("newsletterSignup")
			var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'addUserToEmaillist=1&'+allNodes,onComplete: function(r) {
				
				messageBox(r.responseText,3);
				
				if (r.responseText == 'Thank you, your email address has been added.') $('nlemail').setValue('Your email address here.');
				
			}});
			
			}
			else messageBox('Please enter a valid email address',3);
			
		});
		
		
	
	}
}

function isValidEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1) return false;
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false;
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false;
	if (str.indexOf(at,(lat+1))!=-1) return false;
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false;
	if (str.indexOf(dot,(lat+2))==-1) return false;
	if (str.indexOf(" ")!=-1) return false;
	return true;			
}

function doLocationDropdown () {
	$('location').enable();
	if ($F('courseSelect') != '0') {
		var aj = new Ajax.Updater('location',pageURL+'ajaxStuff.php', {method: 'post', parameters: 'getHomeLocations='+$F('courseSelect')});
		getCoursePrice ()
	}
	else {
		$('location').update('<option value="0">Select a location</option>');
		$('location').disable();
		$('date').update('<option value="0">Select a date</option>');
		$('date').disable();
	}
}

function doDateDropdown () {
	$('date').enable();
	if ($F('location') != '0') {
		var aj = new Ajax.Updater('date',pageURL+'ajaxStuff.php', {method: 'post', parameters: 'getHomeDates='+$F('courseSelect')+'&loc='+$F('location')});
		getCoursePrice ()
	}
	else {
		$('date').update('<option value="0">Select a date</option>');
		$('date').disable();
	}
}

function getCoursePrice () {
	if ($F('location') != '0' && $F('courseSelect') != '0' && $F('date') != '0') {
		var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'getCoursePrice='+$F('courseSelect'), onComplete: function (r) { $('homeBookCourse').update('Price: &pound;'+r.responseText); $('homeBookCourse').show(); $('homeBookCourse2').show(); }});
	}
	else {
		$('homeBookCourse').hide();
		$('homeBookCourse2').hide();
	}
}

// CART FUNCTIONS

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result.toFixed(dec);
}

function setUpCart () {
	$('cartForm').reset();
	if (orderRef == null) {
		var a=$$('#cartForm input[type=checkbox]');
		a.each(function(n){
			id=n.id.replace('i','');
			$(n.id).observe('click', function () { deleteFromCart (id); });
		});
	}
	if ($('checkOutButton') != null) $('checkOutButton').observe('click',function (e) { Event.stop(e); cartRedirect(); });
}

function cartRedirect () {
	
	if (orderRef != null) {
		var allNodes = Form.serialize("cartForm"),am = parseFloat($F('cAmount'));
		
		if (am > 0 && am <= cartMaxCost) {
			action=1;
			var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'setCartPayment='+am,onComplete: function() {
				var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'orderDetails=1&'+allNodes,onComplete: function(response) {
					var response = response.responseText;
					var reply = response.split("||message||");
					if (reply[0]=='ok') {
						$('crypt').value = reply[1];
						$('signUpForm2').submit();
					}
				}});
			 action=0; }});
		}
	}
	else if (payType != null) {
		var p = 'Sign-up';
		if (payType=='dd') p = 'Sign-up/Direct-Debit';
		window.location = pageURL+p;
	}
	else messageBox('Please select a payment type',3);
}

function setCartPayment (a) {
	action=1;
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'setCartPayment='+a,onComplete: function() { action=0; }});
	
}
var payType;
function setCartPayment2 (t) {
	payType=null;
	action=1;
	//alert(t);
	switch(t) {
	case 'ccFull':
	  var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'setCartPayment='+cartMaxCost,onComplete: function() { action=0; payType='cc'; }});
	  break;
	case 'ccDep':
	  var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'setCartPayment=100.00',onComplete: function() { action=0; payType='cc'; }});
	  break;
	case 'dd':
	  payType='dd';
	  break;
	}	
}

function deleteFromCart (id) {
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'deleteFromCart='+id,onComplete: function(response) {
		var response = response.responseText;
		messageBox(response,3);
		showCart();
		setUpCart();
	}});
}

function showCart() {
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'getCart=1',onComplete: function(response) {
		var response = response.responseText;
		var reply = response.split("||message||");
		$('cartTotal').update('Total - &pound;'+reply[1]);
		$('cartContents').update(reply[0]);
		if (reply[1]<1) {
			$('paymentSelectContainer').hide();	
		}
		else {
			cartMaxCost = parseInt(reply[1])*0.9;
			//$('toPay').update('Pay now - &pound;100');
			setUpCart();
		}
		messageBox('Item deleted',3);
	}});
}

function sortMiniCart () {
	if ($('miniCart') != null) {
		var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'miniCart=1',onComplete: function(response) {
			var response = response.responseText;
			//alert(response);
			var reply = response.split("||message||");
			if (reply[0]>0) {
				var w = document.viewport.getWidth();
				t = Math.ceil((w-996)/2)+30;
				if (t<0)t=0;
				$('miniCart').setStyle({ right:t+'px' });
				if (reply[0]>1) a='s'; else a='';
				$('miniCartTop').update('Your cart - '+reply[0]+' item'+a+' - &pound;'+reply[1]);
				$('miniCart').show();
			}
			else {
				$('miniCart').hide();
			}
		}});
	}
}

function addToCart () {
	var f = "courseSelectForm";
	var allNodes = Form.serialize(f);
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'addToCart=1&'+allNodes,onComplete: function () {
		sortMiniCart();
		messageBox('Item added to cart',3)
	}});
}
function addToCart2 () {
	var f = "courseForm";
	var vars = 'courseSelect='+$F('date');
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'addToCart=1&'+vars ,onComplete: function () {
		sortMiniCart();
		messageBox('Item added to cart',3)
	}});
}
function addToCart3 (i) {
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'addToCart=1&courseSelect='+i ,onComplete: function () {
		sortMiniCart();
		messageBox('Item added to cart',3)
	}});
}
function addToCart4 () {
	if (($F('date') != '0') && ($F('date') != 'Location-one')) {
		var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'addToCart=1&courseSelect='+$F('date') ,onComplete: function () {
			window.location = pageURL+'Cart';
		}});
	}
	else return false;
}



// COURSE PAGES

function setUpCourses () {
	
	
	
}

function getDates(i) {
	var aj = new Ajax.Updater('date',pageURL+'ajaxStuff.php', {method: 'post', parameters: 'getHomeDates='+$F('courseSelect')+'&loc='+i, onComplete: function () { $('date').enable(); }});
}

function doCourseBox (t,onOff) {
	if (onOff=='on') {
		$(t).setStyle({ backgroundPosition:'-346px 0' });
		$(t+'T').setStyle({ color:'#002534' });
		$(t+'L2').setStyle({ color:'#ffffff' });
		$(t+'L').setStyle({ color:'#ffffff' });
		$(t+'L3').setStyle({ color:'#002534' });
	}
	else {
		$(t).setStyle({ backgroundPosition:'0 0' });
		$(t+'T').setStyle({ color:'#ffffff' });
		$(t+'L2').setStyle({ color:'#002534' });
		$(t+'L').setStyle({ color:'#002534' });
		$(t+'L3').setStyle({ color:'#ffffff' });
	}
}

function courseInfo (n) {
	var k,o;
	if (action<1) {
		action=1;
		if ($('cbtl'+n).hasClassName('courseBoxOffTopL')) {
			k='On'; o='Off';
			$('cbl'+n).update('Less info');
			Effect.BlindDown('cbmt'+n, { duration: 0.5, afterFinish: function () { courseInfoClasses (n,k,o); } });
		} else {
			k='Off'; o='On';
			$('cbl'+n).update('More info...');
			Effect.BlindUp('cbmt'+n, { duration: 0.5, afterFinish: function () { courseInfoClasses (n,k,o); } });
		}
	}
}

function courseInfoClasses (n,k,o) {
	action=0;
	$('cbtl'+n).addClassName('courseBox'+k+'TopL');
	$('cbtl'+n).removeClassName('courseBox'+o+'TopL');
	$('cbtr'+n).addClassName('courseBox'+k+'TopR');
	$('cbtr'+n).removeClassName('courseBox'+o+'TopR');
	$('cbm'+n).addClassName('courseBox'+k+'Middle');
	$('cbm'+n).removeClassName('courseBox'+o+'Middle');
	$('cbbr'+n).addClassName('courseBox'+k+'BaseR');
	$('cbbr'+n).removeClassName('courseBox'+o+'BaseR');
	$('cbbl'+n).addClassName('courseBox'+k+'BaseL');
	$('cbbl'+n).removeClassName('courseBox'+o+'BaseL');
}

function swapCourseTab () {
	var t;
	if ($('availabilty') != null) {
		if ($('a1').hasClassName('tabContainer')) {
			on='b'; off='a';
			$('availabilty').hide();
			$('company_bookings').show();
		}
		else {
			on='a'; off='b';
			$('availabilty').show();
			$('company_bookings').hide();
		}
	}
	$(on+'1').addClassName('tabContainer');
	$(on+'1').removeClassName('tabContainerLight');
	$(off+'1').addClassName('tabContainerLight');
	$(off+'1').removeClassName('tabContainer');
	
	$(on+'2').addClassName('tabLeft');
	$(on+'2').removeClassName('tabLeftLight');
	$(off+'2').addClassName('tabLeftLight');
	$(off+'2').removeClassName('tabLeft');
	
	$(on+'3').addClassName('tabContent');
	$(on+'3').removeClassName('tabContentLight');
	$(off+'3').addClassName('tabContentLight');
	$(off+'3').removeClassName('tabContent');
	
	$(on+'4').addClassName('tabRight');
	$(on+'4').removeClassName('tabRightLightTrans');
	$(off+'4').addClassName('tabRightLightTrans');
	$(off+'4').removeClassName('tabRight');
}



// CONTACT PAGE

function submitContact () {
	
	var allNodes = Form.serialize("contactForm");
	
	if ($('name').present() && $('email').present() && $('enquiry').present()) {
	
		if (isValidEmail($F('email'))) {
		
		var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'contact=1&'+allNodes,onComplete: function(response) {
			var response = response.responseText;
			if (response != null && response != '' ) {
				messageBox(response,3);
			}
			if (response == 'Your message has been sent.' ) {
				$('contactForm').reset();
			}
		}});
		
		}
		else messageBox('Please enter a valid email address',3);
	
	}
	else messageBox('Please enter all mandatory fields',3);
}



// SIGN UP PAGE

function setUpSignUp () {
	$('signUpForm').reset();
	var a=$$('#signUpForm input');
	a.each(function(n){
		if ($F(n.id) != null && n.id != null) {
			clearInputBox($(n.id),$F(n.id),2,'#002534');
			signDefault[n.id]=$F(n.id);
		}
   	});
	if ($('submitSignup') != null) $('submitSignup').observe('click',processSignUp);
	if ($('submitSignup2') != null) $('submitSignup2').observe('click',processSignUp2);
}


function processSignUp () {
	allOK=1;
	var reqFields = new Array ('firstname','dob','surname','address','city','county','postcode','email','email2','phone','employment_status');
	i=0;
	while (reqFields[i]) {
		if ($F(reqFields[i]) == '' || $F(reqFields[i])==signDefault[reqFields[i]]) {
			$(reqFields[i]).setStyle({ color:'#f00' });
			allOK=0;
		}
		++i;
	}
	if (allOK==1) {
		if ($('tc').checked==true) {
			if (isValidEmail($F('email')) && isValidEmail($F('email2')) && $F('email') == $F('email2')) {
				var a=$$('#signUpForm input');
				a.each(function(n){
					if (signDefault[n.id] != null && signDefault[n.id] != '' && $F(n.id) == signDefault[n.id]) {
						clearInputBox(n.id,'',2);
						$(n.id).value='';
					}
				});
				var allNodes = Form.serialize("signUpForm");
				if (payType=='dd') {
					var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'orderDetailsDD=1&'+allNodes,onComplete: function(response) {
						var response = response.responseText;
						var reply = response.split("||message||");
						if (reply[0]=='ok') {
							window.location='https://www.fastdd.co.uk/start.php?c=8d442af3e10f70fc767ac1d1b43283fb';
						}
						else {
							messageBox(reply[1],3);
						}
					}});
				}
				else {
					var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'orderDetails=1&'+allNodes,onComplete: function(response) {
						var response = response.responseText;
						var reply = response.split("||message||");
						if (reply[0]=='ok') {
							$('crypt').value = reply[1];
							$('signUpForm2').submit();
						}
						else {
							messageBox(reply[0]+' '+reply[1],3);
						}
					}});
				}
			}
			else { 
				$('email').value = 'E-mail*';
				$('email2').value = 'Confirm your e-mail*';
				$('email').setStyle({ color: '#f00' });
				$('email2').setStyle({ color: '#f00' });
				messageBox('One of your emails seems to be incorrect?',3);
			}
		}
		else messageBox('Please confirm you have read our Terms and Conditions',3);
	}
	else {
		messageBox('Please fill the fields in red to proceed.',3);
	}
}

function empStats () {
	$('empStat').setStyle({ color:'#002534' });
}

function processSignUp2 () {
	allOK=1;
	if ($('emp1').checked==true) $('employment_status').setValue('Employed');
	if ($('emp2').checked==true) $('employment_status').setValue('Unemployed');
	if ($('emp3').checked==true) $('employment_status').setValue('Student');
	if ($('emp4').checked==true) $('employment_status').setValue('Other');
	var reqFields = new Array ('firstname','dob','surname','address','city','county','postcode','email','email2','phone','employment_status');
	i=0;
	while (reqFields[i]) {
		if ($F(reqFields[i]) == '' || $F(reqFields[i])==signDefault[reqFields[i]]) {
			if (reqFields[i]=='employment_status') $('empStat').setStyle({ color:'#f00' });
			else $(reqFields[i]).setStyle({ color:'#f00' });
			allOK=0;
		}
		++i;
	}
	 
	if (allOK==1) {
		if ($('tc').checked==true) {
			
			if (isValidEmail($F('email')) && isValidEmail($F('email2')) && $F('email') == $F('email2')) {
				var a=$$('#signUpForm input');
				a.each(function(n){
					if (signDefault[n.id] != null && signDefault[n.id] != '' && $F(n.id) == signDefault[n.id]) {
						clearInputBox(n.id,'',2);
						$(n.id).value='';
					}
				});
				var allNodes = Form.serialize("signUpForm");
				//alert(allNodes);
				if (payType=='dd') {
					var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'orderDetailsDD=1&'+allNodes,onComplete: function(response) {
						var response = response.responseText;
						var reply = response.split("||message||");
						if (reply[0]=='ok') {
							window.location='https://www.fastdd.co.uk/start.php?c=8d442af3e10f70fc767ac1d1b43283fb';
						}
						else {
							messageBox(reply[1],3);
						}
					}});
				}
				else {
					var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'orderDetails=1&'+allNodes,onComplete: function(response) {
						var response = response.responseText;
						var reply = response.split("||message||");
						if (reply[0]=='ok') {
							$('crypt').value = reply[1];
							$('signUpForm2').submit();
						}
						else {
							messageBox(reply[0]+' '+reply[1]+'matt',3);
						}
					}});
				}
			}
			else { 
				$('email').value = 'E-mail*';
				$('email2').value = 'Confirm your e-mail*';
				$('email').setStyle({ color: '#f00' });
				$('email2').setStyle({ color: '#f00' });
				messageBox('One of your emails seems to be incorrect?',3);
			}
		}
		else messageBox('Please confirm you have read our Terms and Conditions',3);
	}
	else {
		messageBox('Please fill the fields in red to proceed.',3);
	}
}


// ALL PAGES

function setUpAllPages () {
	if ($('postCodeSearch') != null) {
		clearInputBox('postCodeSearch','Type your postcode',2)
	}
	if ($('loginBox') != null) {
		$('loginBox').setStyle({ cursor:'pointer' });
		$('loginBox').observe('click', function () {
			window.location=pageURL+'login.php';
		});
	}
	sortMiniCart ();
	
	if ($('link_AllCourses') != null) {
		Event.observe('link_AllCourses', 'mouseover',function () {
			menuOver=1;
			openCloseMenu (1,'dropCourseMenu',0.6)
		});
		Event.observe('dropCourseMenu', 'mouseover',function () {
			menuOver=1;
		});
		Event.observe('dropCourseMenu', 'mouseout',function () {
			openCloseMenu (2,'dropCourseMenu',0.6)
		});
		Event.observe('link_AllCourses', 'mouseout',function () {
			openCloseMenu (2,'dropCourseMenu',0.6)
		});
		var a = $$('a[id^="courseType"]');
		a.each(function(n){
			var t = n.id.replace("courseType","");
			Event.observe(n.id, 'click',function () {
				openCloseMenu (1,'coursesList'+t,0.6)
			});
		});
	}
	if ($('dropVenueMenu') != null) {
		Event.observe('link_Venues', 'mouseover',function () {
			menuOver=1;
			openCloseMenu (1,'dropVenueMenu',0.6)
		});
		Event.observe('dropVenueMenu', 'mouseover',function () {
			menuOver=1;
		});
		Event.observe('dropVenueMenu', 'mouseout',function () {
			openCloseMenu (2,'dropVenueMenu',0.6)
		});
		Event.observe('link_Venues', 'mouseout',function () {
			openCloseMenu (2,'dropVenueMenu',0.6)
		});
		
	}
	
	if ($('contactForm') != null) $('submitButton').observe('click', submitContact);
	
}


// RESIT PAGE

var daySelected, monNames = new Array("","January","February","March","April","May"," June","July","August","September","October","November","December");
function getCalendar (action) {
	$('calenderMonth').update('<img src="'+pageURL+'images/x.gif" />');
	$('calendarDates').update('');
	var aj1 = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'getCalendar=1&action='+action,onComplete: function(response) {
		var response = response.responseText;
		var reply = response.split("||message||");
		$('calenderMonth').update(reply[1]);
		$('calendarDates').update(reply[0]);
		if ($(daySelected) != null) $(daySelected).setStyle({ borderColor: '#000' });
	}});
}

function selectDate(i) {
	var dates = i.split("-");
	daySelected = i;
	$('calDate').update('date to change');
	$('calendarButton').update(ord(dates[1])+' '+monNames[dates[2]]+' '+dates[3]);
	$('calendarCont2').hide();
}

function ord(n) {
	var sfx = ["th","st","nd","rd"];
	var val = n%100;
	return n + (sfx[(val-20)%10] || sfx[val] || sfx[0]);
}

function setUpResit() {
	if ($('calendarButton') != null) $('calendarButton').observe('click', function () { $('calendarCont2').show(); });
	if ($('requirements') != null) $('submitButton').observe('click', submitResit2);
	else $('submitButton').observe('click', submitResit);
	$('venue').observe('change', function () {
		var a = $F('venue').split("|||");
		var aj1 = new Ajax.Updater('theResitDates',pageURL+'ajaxStuff.php', {method: 'post', parameters: 'getResitDates='+a[0]});
	});
}

function submitResit () {
	var allNodes = Form.serialize("resitForm");
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'resitSubmit=1&'+allNodes,onComplete: function(response) {
		var response = response.responseText;
		if (response != null && response != '' ) {
			messageBox(response,3);
		}
		if (response == 'Your message has been sent.' ) {
			$('resitForm').reset();
			$('theResitDates').update('&nbsp;');
		}
	}});
}

function submitResit2 () {
	var allNodes = Form.serialize("resitForm");
	if (action != 1) {
		action = 1;
		var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'resitSubmit=1&'+allNodes,onComplete: function(response) {
			action = 0;
			var response = response.responseText;
			var reply = response.split("||message||");
			if (reply[0]=='ok') {
				$('crypt').value = reply[1];
				$('signUpForm2').submit();
			}
			else messageBox(reply[1],3);
		}});
	}
}

function setUpProspectus () {
	
	$('submitButton').observe('click', submitProspectus);
	
}

function submitProspectus () {
	
	if ($('name').present() && $('email').present() && $('phone').present()) {
	
		if (isValidEmail($F('email'))) {
	
			var allNodes = Form.serialize("prospectusForm");
			var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'prospectusSubmit=1&'+allNodes,onComplete: function(response) {
				$('prospectusForm').reset();
				window.location = pageURL+'download.php?file=Innervate_Traing_Prospectus.pdf';
			}});
		}
		else messageBox('Please enter a valid email address',3);
	}
	else messageBox('Please enter all fields',3);
}

// ALL COURSES

function setUpAllCourses () {
	openCourses = new Array('0','1');
	var a = $$("a[id^='courseHeader']"),y;
	a.each(function(n){
		var y=n.id.replace('courseHeader_','');
		$(n.id).observe('click', function () { openCloseAllCourses (y); });
		if (y>1) {
			$('courseContent_'+y).hide();
			openCourses[y]=0;
		}
   	});
}

function openCloseAllCourses (t) {
	if (openCourses[t] == 1 && action==0) {
		action=1;
		Effect.BlindUp('courseContent_'+t, { duration: 0.5, afterFinish: function () { action=0; } });
		openCourses[t]=0;
	}
	else if (action==0) {
		action=1;
		Effect.BlindDown('courseContent_'+t, { duration: 0.5, afterFinish: function () { action=0; } });
		openCourses[t]=1;
	}
}

// LOGIN PAGE

function setUpLogin () {
	$('loginForm').reset();
	var a=$$('#loginForm input');
	a.each(function(n){
		if ($F(n.id) != null && n.id != null) {
			clearInputBox($(n.id),$F(n.id),2);
			signDefault[n.id]=$F(n.id);
		}
   	});
	$('submitSignup').observe('click',loginSubmit);
}

function loginSubmit () {
	var allNodes = Form.serialize("loginForm");
	var aj = new Ajax.Request(pageURL+'ajaxStuff.php', {method: 'post', parameters: 'loginSubmit=1&'+allNodes,onComplete: function(response) {
			var response = response.responseText;
			if (response != null && response != '' ) {
				messageBox(response,3);
			}
			if (response == 'You have been logged in.' ) {
				setTimeout(window.location=pageURL+'admin.php',2000);
			}
		}});
}

function adminLogout () {
	new Ajax.Request(pageURL+'ajaxStuff.php', { method: 'post', parameters: 'adminLogout=1',onComplete: function(response) {
		var response = response.responseText;
		setTimeout(window.location=pageURL+'login.php',2000);
	}});
}

// VENUES PAGE
function showVenue (p,b) {
	var m = $(b).cumulativeOffset();
	$('venueOverlay').setStyle ({top:(m[1]-380)+'px'});
	new Ajax.Request(pageURL+'ajaxStuff.php', { method: 'post', parameters: 'showVenue='+p,onComplete: function(response) {
		var response = response.responseText;
		$('venueOverlay').update(response);
		$('venueOverlay').show();
	}});
}

function closeVenue () {
	$('venueOverlay').update('');
	$('venueOverlay').hide();
}

function $RF(el, radioGroup) {
    if($(el).type && $(el).type.toLowerCase() == 'radio') {
        var radioGroup = $(el).name;
        var el = $(el).form;
    } else if ($(el).tagName.toLowerCase() != 'form') {
        return false;
    }

    var checked = $(el).getInputs('radio', radioGroup).find(
        function(re) {return re.checked;}
    );
    return (checked) ? $F(checked) : null;
}

function setUpPaymentOptions () {
	$('submitSignup').observe('click', function () {
		var f = $RF('paySubmitForm','payOption');
		if (f=='cc') {
			window.location=pageURL+'Cart';
		}
		else if (f=='dd') {
			window.location=pageURL+'Sign-up/Direct-Debit';
		}
		else messageBox('Please select a payment option',3);
	});
}




function isNumberKey(evt,id)	{
	 var charCode = (evt.which) ? evt.which : event.keyCode
	 if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46)
		return false;
	 if (parseInt($F(id).indexOf('.'))>=0 && charCode == 46) return false;
	 return true;
}


if (thisPage == 'careers') Event.observe(window, 'load', setUpCareers);
if (thisPage == 'faqs') Event.observe(window, 'load', setUpFaqs);
if (thisPage == 'home') Event.observe(window, 'load', setUpHome);
if (thisPage == 'signup') Event.observe(window, 'load', setUpSignUp);
if (thisPage == 'courses') Event.observe(window, 'load', setUpCourses);
if (thisPage == 'cart') Event.observe(window, 'load', setUpCart);
if (thisPage == 'resit') Event.observe(window, 'load', setUpResit);
if (thisPage == 'prospectus') Event.observe(window, 'load', setUpProspectus);
if (thisPage == 'login') Event.observe(window, 'load', setUpLogin);
if (thisPage == 'all_courses') Event.observe(window, 'load', setUpAllCourses);
if (thisPage == 'payment_options') Event.observe(window, 'load', setUpPaymentOptions);
Event.observe(window, 'load', setUpAllPages);
