// JavaScript Document

function submit_contact_form () {
	// Show an activity indicator.
	Element.show('indicator');

	// Hide any visible errors.
	hide_error ( $('contact_error_name') );
	hide_error ( $('contact_error_phone') );
	hide_error ( $('contact_error_email') );
	hide_error ( $('contact_error_subject') );
	hide_error ( $('contact_error_comments') );

	// Wait a moment for the old errors to fade before testing for errors again.
	setTimeout('determine_errors()', 200);
}

function determine_errors () {
	var parameters = Form.serialize('contact_form', getHash = false);	

	new Ajax.Request ('ajax/contactform.php', {
		method: 'post',
		parameters: parameters,
		onSuccess: function (transport) {
			if (transport.responseText.match (/success/)) {
				show_message ( $('contact_form_div'), '<success>', transport.responseText);
			} else {
				show_message ( $('contact_error_name'), '<name_error>', transport.responseText );
				show_message ( $('contact_error_phone'), '<phone_error>', transport.responseText );
				show_message ( $('contact_error_email'), '<email_error>', transport.responseText );
				show_message ( $('contact_error_subject'), '<subject_error>', transport.responseText );
				show_message ( $('contact_error_comments'), '<comments_error>', transport.responseText );
				show_message ( $('contact_form_div'), '<sending_error>', transport.responseText);
			}
		}
	});

	// Wait a moment and then hide the indicator.
	setTimeout("Element.hide('indicator')", 1000);
}

function hide_error ( current_error ) {
	// Hide the error message if it's currently visible.
	if (current_error.visible()) {
		Effect.SlideUp (current_error, { duration: 0 });
	}
}

function show_message ( current_error, err_tag, resp_text ) {
	// Check for a specific "Tag" and show the error if one is found.
	if (resp_text.match (err_tag)) {
		// It's important to leave these <div> tags on either side for the effects to always work properly.
		current_error.update('<div>' + resp_text.substring( (resp_text.indexOf(err_tag) + err_tag.length), resp_text.lastIndexOf(err_tag)) + '</div>');
		Effect.SlideDown (current_error, { duration: 0.5 });
	}
}

