window.addEvent('domready', function() {
	
	//create our Accordion instance
	var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
		opacity: false,
		onActive: function(toggler, element){
			toggler.setStyle('color', '#000');
			toggler.setStyle('font-weight', 'bold');
		},
		onBackground: function(toggler, element){
			toggler.setStyle('color', '#953531');
		}
	});

	//add click event to the "add section" link
	/*$('add_section').addEvent('click', function(event) {
		event.stop();
		
		// create toggler
		var toggler = new Element('h3', {
			'class': 'toggler',
			'html': 'Common descent'
		});
		
		// create content
		var content = new Element('div', {
			'class': 'element',
			'html': '<p>Lorem/p>'
		});
		
		// position for the new section
		var position = 0;
		
		// add the section to our myAccordion using the addSection method
		myAccordion.addSection(toggler, content, position);
	});*/
	
	/*CODE FOR AJAX FORM*/
	// --
	$('mailForm').addEvent('submit', function(e) {
		//Prevents the default submit event from loading a new page.
		e.stop();
		
		var emailTxt = $('emailTxt');
		var nameTxt = $('nameTxt');
		var send = true;
		var error = '';
		
		if(isEmail(emailTxt.value) == false){
			send = false;
			error += 'Courriel Invalide'+"\r\n";
		}
		if(nameTxt.value == ''){
			send = false;
			error += 'Champ nom vide'+"\r\n";
		}
		
		if(send == true){
			hideError();
			//Empty the log and show the spinning indicator.
			var form = $('form_box');
			var log = $('mailFormRep').empty().addClass('ajax-loading');
			//Set the options of the form's Request handler. 
			//("this" refers to the $('mailForm') element).
			this.set('send', {onComplete: function(response) { 
				form.empty();
				log.removeClass('ajax-loading');
				log.set('html', response);
			}});
			
			//Send the form.
			this.send();
		}else{
			showError(error);
		}
	});
	
});
	
function isEmail(email){
	var verif     = /^[\.a-zA-Z0-9_\-]+@+[\.a-zA-Z0-9\-_]{2,}[.][a-zA-Z]{2,3}[\.a-zA-Z]{0,3}$/
	if (verif.exec(email) == null){
		return false;
	}else{
		return true;
	}
}

function showError(msg){
	document.getElementById('errorBox').style.display = 'block';
	document.getElementById('errorBoxMsg').innerHTML = msg;
}

function hideError(){
	document.getElementById('errorBox').style.display = 'none';
}
