

/*------------contact.js----------*/

reg.postSetup(function() {
	// popups
	$('a[href$="/pop-privacy"]').fancybox({
		'overlayShow':true,
		'frameWidth':500,
		'frameHeight':350
	});
	$('a[href$="/map"]').fancybox({
		'overlayShow':true,
		'frameWidth':574,
		'frameHeight':470,
		'title':false
	});
	
	// :KLUDGE: allows a default value for an input with a mask
	$('#form-contact #contact_phone').blur();
	
	// focus on first form field
	focusFirst('form-contact');
});

// disable/enable form actions
function disableContactAction() {
	$('form#form-contact #contact_send').blur().attr({'disabled':'disabled'}).hide();
	$('form#form-contact #contact-send-button').append('<img id="contact-processing" src="/images/lion_running_onwhite.gif" width="47" height="20" alt="processing..." title="processing..." />');
}
function enableContactAction(container) {
	$('form#form-contact #contact-processing').remove();
	$('form#form-contact #contact_send').attr({'disabled':''}).show();
}

// clear error styled fields for a given form
function clearContactFormErrors(id) {
	var list = $('#' + id + ' ul > li[id]');
	list.each(function() {
		$(this).removeClass('error');
	});
}

// submit form via Ajax call to controller method
function submitContactFormAjax(obj) {
	// set values
	var form      = $(obj).parents('form');
	var form_id   = form.attr('id');
	var container = $(obj).parent();
	
	// disable action
	disableContactAction(container);
	
	// post form
	form.ajaxSubmit({
		dataType: 'json',
		success: function(response) {
			// re-enable actions
			enableContactAction(container);
			
			// errors?
			if (response.errors) {
				// clear previous errors
				clearContactFormErrors(form_id);
				
			  displayErrorMessage(response.errors)
				
				// scroll to the top of the page
				// so we can see the notice
				$.scrollTo('#user-notice', 800, {offset:-15});
			} else if (response.notice) {
				// clear errors
				clearContactFormErrors(form_id);
				
				// reset form
				form.resetForm();
				
				// display notice
				setNotice(escape(response.notice));
				
				// scroll to the top of the page
				// so we can see the notice
				$.scrollTo('#user-notice', 800, {offset:-15});
			} else if (response.success) {
				// reset and hide
				clearNotice();
				form.resetForm();
				$('#' + form_id).hide();
				
				// add success message
				$('#' + form_id).before('<div class="success-message">' + response.success + '</div>');
			} else if (response.redirect) {
				// redirect
			  location.href = response.redirect;
			} else {
				// display notice
				setNotice(escape('There was an error processing your request. Please try again.'), 'error');
			}
		}
	});
}
