window.addEvent('domready', function(){

	// send the contact form
	if($('contactform')){
	
		form = $('contactform');

	}
	
	
	/* booking page scripts */
	if($('bookingform')){
	
		form = $('bookingform');
	
		$('booking').getElements('.artist').each(function(el){
			
			el.getElement('label').addEvents({
				'mouseenter': function(){
					this.addClass('over');
				},
				'mouseleave': function(){
					this.removeClass('over');
				},
				'click': function(e){
					this.toggleClass('selected');
				}
			});
			
		});	
	}
	
	// validator
	myValidator = new FormValidator(form, {
		classValid: 'fieldvalidate-valid',
		classInvalid: 'fieldvalidate-invalid',
		classNeutral: 'fieldvalidate-neutral',
		onInvalid: function(field, options){
			alertField(field, options.msg);
		},
		onValid: function(field, options){
			removeFieldAlert(field);
		},
		onNeutral: function(field, options){
		},
		onFormValid: function(){
			form.getElement('.submitalert').set('html', "");
			$('submitbtn').setStyle('display', 'block');
		},
		onFormInvalid: function(){
			form.getElement('.submitalert').set('html', "There are errors on the form");
			$('submitbtn').setStyle('display', 'none');
		}
	});
	
	
	form.addEvent('submit', function(e){
		e.stop();
		
		if(myValidator.isValid){
			form.getElement('.submitalert').set('html', 'Sending...');
			$('submitbtn').setStyle('display', 'none');
			
			var req = this.get('send');
			req.addEvent('success', function(text){
				var rsp = JSON.decode(text);
				form.getElement('.submitalert').set('html', rsp.info[0]);
				$('submitbtn').setStyle('display', 'block');
				
				if(rsp.status == "ok"){
					form.reset();
				}
					
			});
			req.send();
		}
	});

});

function alertField(field, msg){
	removeFieldAlert(field);
	var div = new Element('div', {
		'class': 'alert',
		'html': msg
	}).inject(field.getParent(), "top");
}
function removeFieldAlert(field){
	if(field.getParent().getElement('.alert')){
		field.getParent().getElement('.alert').destroy();
	}
}