var CONTACT = {
	messages: [],

	validate: function(form) {
		var form = $(form);

		$('loader').show();
		['updater','error_email','error_subject','error_message','error_captcha'].each(function(e) { $(e).update('') } );

		['email','subject','message','captcha','captcha_image'].each(function(e) {
			UTILS.showError($(e).up(1), false);
		});

		var submit_form = true;

		if(form.elements['email'].value.length < 6 || !UTILS.checkEmailFormat(form.elements['email'].value)) {
			UTILS.showError($('email').up(1), true);
			$('error_email').update(this.messages['invalid_email']);
			submit_form = false;
		}
		/*
		if(form.elements['username'].value.length < 1 || form.elements['username'].value.length > 15) {
			UTILS.showError($('username').up(1), true);
			$('error_username').update(this.messages['invalid_username']);
			submit_form = false;
		}
		*/
		if(form.elements['subject'].value.length < 1) {
			UTILS.showError($('subject').up(1), true);
			$('error_subject').update(this.messages['invalid_subject']);
			submit_form = false;
		}
		
		if(form.elements['message'].value.length < 15) {
			UTILS.showError($('message').up(1), true);
			$('error_message').update(this.messages['message_too_short']);
			submit_form = false;
		}
		
		if(form.elements['message'].value.length > 5000) {
			UTILS.showError($('message').up(1), true);
			$('error_message').update(this.messages['message_too_long']);
			submit_form = false;
		}
		
		if(form.elements['captcha'].value.length < 1 || form.elements['captcha'].value.length > 5) {
			$$('.captcha_error').each(function(e) { $(e).addClassName('errorField'); });
			$('error_captcha').update(this.messages['invalid_captcha']);
			submit_form = false;
		}

		if(submit_form) {
			var formObjects = UTILS.formToObj(form);

			new Ajax.Request(VIVASTREET._ajax_dir+'contact.php?country='+VIVASTREET._country_geo[1], {
				method:'post',
				parameters: formObjects,
				onSuccess: function(resp) {
					$('loader').hide();
					var response = resp.responseText.evalJSON();
					var data = '';
					for(i = 0; i < response.length; i++) {
						data = response[i];
						//console.log(data.message);
						switch(data.code) {
							case 'invalid_captcha':
								UTILS.showError($('captcha').up(1), true);
								$('error_captcha').update(data.message);
								VIVASTREET.resetCaptcha('contact', 'captcha_image', 'captcha', true);
								$$('.captcha_error').each(function(e) { $(e).addClassName('errorField'); });
							break;
							case 'error':
								VIVASTREET.resetCaptcha('contact', 'captcha_image', 'captcha', true);
								$$('.captcha_error').each(function(e) { $(e).addClassName('errorField'); });
							break;
							case 'success':
								CONTACT.hideForm(data.message);
							break;
						}
					}
				}
			});
		}
		else {
			$('loader').hide();
			VIVASTREET.resetCaptcha('contact', 'captcha_image', 'captcha', true);
			$$('.captcha_error').each(function(e) { $(e).addClassName('errorField'); });
		}
	},
	
	hideForm: function(message) {
		$('vs_contact').hide();
		$('vs_contact_message_holder').innerHTML = message;
		$('vs_contact_message').show();
	},
	
	showForm: function() {
		$('email').value = '';
		$('username').value = '';
		$('message').value = '';
		$('subject').selectedIndex = 0;
		VIVASTREET.resetCaptcha('contact', 'captcha_image', 'captcha', true);
		$('vs_contact').show();
		$('vs_contact_message').hide();
	}
};
