/*
 * sendmail 1.0 - email sender with mootools 1.2
 * By Mario Zambon (http://www.urbangap.com)
 * Copyright (c) 2008 cody urbangap
 * Licensed under the MIT License: http://www.opensource.org/licenses/mit-license.php
*/

function sendmail() {

	var form 		= $('contact')
	var e 			= $('error')
	var canc		= $('reset')
	var checkMail	= /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	var input		= $$('.mdInput', '.mdInputII', '.mdInputIII', '.mdTxtArea', '#privacy')
	var aa
	
	form.addEvent('submit', checkDati)
	canc.addEvent('click', resetError)
	input.addEvent('click', function(){ formFx(this) })
	input.addEvent('focus', function(){ formFx(this) })
	


	function formFx(a){
		var id = a.getProperty('id')
		a.focus()
		input.set('morph', {duration: 400});
		input.morph('#sendMail .deselect');
		if(id!='privacy'){
			a.morph('#sendMail .select');
		}
	}
	
	function formFxCelar(){
		input.morph('#sendMail .deselect');
	}

	function checkDati() {
		var nome		= $('name')
		var cognome		= $('lastname')
		var email		= $('email')
		var txtMail		= $('request')
		var privacy		= $('privacy')
		
		var nnome 		= nome.getProperty('value')
		var ccognome 	= cognome.getProperty('value')
		var eemail 		= email.getProperty('value')
		var ttxtMail	= txtMail.getProperty('value')
		var pprivacy	= privacy.getProperty('checked')
		
		if(!nnome) {
			e.set('html', 'Inserire il nome')
			formFx(nome)
			return false
		}
		
		if(!ccognome) {
			e.set('html', 'Inserire il cognome')
			formFx(cognome)
			return false
		}
		
		if(!eemail) {
			e.set('html', 'Inserire l\'indirizzo email')
			formFx(email)
			return false
		} else if (!checkMail.test(eemail)) {
			e.set('html', 'Email non valida')
			return false
		}
		
		if (!ttxtMail) {
			formFx(txtMail)
			e.set('html', 'Inserire la richiesta')
			return false
		}
		
		if (!pprivacy) {
			formFx(privacy)
			e.set('html', 'Autorizzare il trattamento dei dati')
			return false
		}
		//form.submit();
		return true
	}
	
	function resetError() {
		e.set('html', '&nbsp;')
		formFxCelar()
	}

}

window.addEvent('domready', sendmail)
