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

	// VALIDACIÓ CAMP A CAMP!!!
	// FEM LA CRIDA AL CONTROLLER VALIDATE_FORM PQ ENS RETORNI 
	// SI EL CAMP AL QUE FEM BLUR VALIDA OK
	var log = $('log_res').empty();

	//seleccionem els inputs amb class validate
	var Fields = $$('input.validate');
	//amb blur fem que cridin la funció sendForm
	Fields.forEach(function(field){
		$(field).onblur = function(event){
			new Ajax(ci_url+ci_lang+'/contacto/validate_form/'+field.id, {
				method: 'post',
				update: log,
				postBody:$('contact_form'),
				onComplete: function() {
					if($('log_res').getText() != 'ok')
					{
						Growl.Smoke({
							title: enviado_ko+':',
							text: $('log_res').getText(),
							image: ci_url+'/img/icons/error.gif',
							duration: 6
						});
					}
					$('log_res').empty();
				}
				}).request();
			};

		});

		// VALIDACIÓ AL ENVIAR EL FORMULARI!!
		// AL ENVIAR
		$('contact_form').addEvent('submit', function(e) {
			/**
			* Prevent the submit event
			*/
			new Event(e).stop();
				// ENVIEM EL FORM
				new Ajax(ci_url+ci_lang+'/contacto/validate_form/all/fields', {
					method: 'post',
					update: log,
					postBody:$('contact_form'),
					onComplete: function() {
						//seleccionem els DIVS amb class ERROR QUE RETORNA LA VALIDACIÓ AL CONTROLLER
						var Fields = $$('div.error');
						// ENVIEM TANTS MISSATGES COM CAMPS ERRONIS HI HAGI
						Fields.forEach(function(field){
							if($('log_res').getText() != 'ok')
							{
								Growl.Smoke({
									title: enviado_ko+':',
									text: field.getText(),
									image: ci_url+'/img/icons/error.gif',
									duration: 6
								});
							}
						});

						if($('log_res').getText() == 'ok')
						{
							//	$('log_res').empty();
							Growl.Bezel({
								title: enviando,
								text: '',
								image: ci_url+'/img/icons/mail.gif',
								duration: 4
							});

							new Ajax(ci_url+ci_lang+'/contacto/send_form/', {
								method: 'post',
								update: log,
								postBody:$('contact_form'),
								onComplete: function() {
									if($('log_res').getText() == 'ok')
									{
										Growl.Bezel({
											title: enviado_ok,
											text: '',
											image: ci_url+'/img/icons/mail-ok.gif',
											duration: 3
										});
									}
									if($('log_res').getText() == 'ko')
									{
										Growl.Bezel({
											title: enviado_ko+'!',
											text: '',
											image: ci_url+'/img/icons/error.gif',
											duration: 6
										});
									}

								}
								}).request();
							}
						}
						}).request();
					});

					// GROWL PARA TEL + FAX

					$('bezel').onclick = function() {
						Growl.Bezel({
							title: '(+34) 93 664 68 40',
							text: telefono,
							image: ci_url+'/img/icons/zoom.gif',
							duration: 3
						});
						return false;
					};

					$('bezel2').onclick = function() {
						Growl.Bezel({
							title: '(+34) 93 664 68 41',
							text: fax,
							image: ci_url+'/img/icons/zoom.gif',
							duration: 3
						});
						return false;
					};
					
					$('bezel3').onclick = function() {
						Growl.Bezel({
							title: '(+34) 913 205 144',
							text: telefono,
							image: ci_url+'/img/icons/zoom.gif',
							duration: 3
						});
						return false;
					};

					$('bezel4').onclick = function() {
						Growl.Bezel({
							title: '(+34) 913 205 144',
							text: fax,
							image: ci_url+'/img/icons/zoom.gif',
							duration: 3
						});
						return false;
					};

				});

