/* Init */

Element.observe(window, 'load', function(){
	var valid = new Validation('contactform', {immediate : true});
	
	Form.getElements('contactform').each(function(input) {
		if (input.type == 'text' || input.type == 'textarea')
		{
			Element.observe(input, 'focus', function(){
				Element.removeClassName(this, 'field');
				Element.addClassName(this, 'fieldOn');
			});
			Element.observe(input, 'blur', function(){
				Element.removeClassName(this, 'fieldOn');
				Element.addClassName(this, 'field');
			});
		}
	});
	
	Element.observe('field_contactpref_phone', 'click', function(){
		Element.show('phone_pref');
	});
	
	Element.observe('field_contactpref_email', 'click', function(){
		Element.hide('phone_pref');
	});
	
	Element.observe('contactform', 'submit', function(){
		if (!valid.validate()) return false;
		//alert('pass js validation');
		
		$('contactform').request({
			onSuccess: function(transport){
				var response = transport.responseText || 'error';
				//alert('result: ' + response);
				if (response == 'ok') {
					Effect.Appear('formSuccess', { duration: 0.50, to: 0.82 });
				} else if (response == 'ERR 002') {
					Element.addClassName('captchaFrame', 'on');
					Recaptcha.reload();
				} else {
					alert(response);
				}
			},
			onFailure: function(){
				alert('Something broke...');
			}
		});
		
		return false;
	});
});