Event.observe(window, 'load', function() {
	$('section02_content','section03_content','section04_content','section05_content').invoke('hide');
	$('canvas').setStyle('min-height:100%;');
	$('footer').setStyle('bottom:0;');
	$$('#content h2').each(function(element) {
		element.setStyle('cursor:pointer');
		Event.observe(element,'click',expand);
	});
	$$('input').each(function(element) {
		Event.observe(element,'focus',function() {this.addClassName('focused');});
		Event.observe(element,'blur',function() {this.removeClassName('focused');});
	});
	Event.observe('bookingform','submit',submitForm);
	$('yourphone').writeAttribute('onKeyPress','return limitinput(event, \'0123456789+-() \', true)');
});

function expand(event) {
	element = Event.findElement(event,'h2').next(0);
	Effect.toggle(element,'slide',{
		duration: 0.5,
		afterUpdate: function() {
			$('canvas').setStyle('min-height:100%;');
			$('footer').setStyle('position:');
		}
	});
}

function submitForm(e) {
	var formdata = $('bookingform').serialize();
	new Ajax.Request('ajax.php', {
		method: 'post',
		postBody: "ajax=true&"+formdata,
		onCreate: function() {$('submitbutton').update("Please wait...");},
		onSuccess: function(transport)  {
			var result = transport.responseText;
			$('yournamelabel','yourname','youremaillabel','youremail','yourphonelabel','yourphone').invoke('removeClassName','missing')
			if (result.indexOf("failed")>-1) {
				if (result.indexOf("N")>-1) {$('yournamelabel','yourname').invoke('addClassName','missing');}
				if (result.indexOf("E")>-1) {$('youremaillabel','youremail').invoke('addClassName','missing');}
				if (result.indexOf("P")>-1) {$('yourphonelabel','yourphone').invoke('addClassName','missing');}
				$('bookingfeedback').update("There has been an error.<br />Please fill in the required information, and submit your request again.").addClassName('error').removeClassName('sent');
			} else {
				$('bookingfeedback').update("Your boooking request has been sent.<br />I will contact you by email within 24 hours.").addClassName('sent').removeClassName('error');
			}
		},
		onComplete: function() {$('submitbutton').update("Arrange a booking");$('canvas').setStyle('min-height:100%;');$('footer').setStyle('bottom:0;');}
	});
	Event.stop(e);
}

function limitinput(evt, strList, bAllow) {
	var charCode = evt.keyCode;
	if (charCode==0) {charCode = evt.which;}
	var strChar = String.fromCharCode(charCode);
	var controlArray = Array(0, 8, 9, 10, 13, 27);
	var intOut = 0;	
	if (bAllow==true) {
		if (charCode==8 || charCode==9 || charCode==37 || charCode==39 || charCode==46 || charCode==116 || (strList.indexOf(strChar)!=-1)) {
			return true;
		} else {return false;}
	} else {
		if (charCode==8 || charCode==9 || charCode==37 || charCode==39 || charCode==46 || charCode==116 || (strList.indexOf(strChar)==-1)) {
			return true;
		} else {return false;}
	}
} 