//==============================================================================
// Name        	: forms.js
// Author      	: CLB De Klaver
// Date		: 09/08/2009
// Version     	: 0.1
// Description 	: Form scripts for clb de klaver site
//==============================================================================

/************************************************************************
 * SEARCH FORMS								*
 ************************************************************************/

// Check the contents of, and submit the search forms
function submitSearchForm(form)
{	
	/* PATTERNS TO TEST FOR */
	// Only whitespace
	var emptyPat = /^[\s]+$/;
	
	// Get all input fields
	var inputFields = form.getElementsByTagName("input");
	// Check input fields
	for(var i = 0; i < inputFields.length; ++i)
	{
		// Only check the text fields
		if(inputFields[i].type != "button")
		{
			// Make sure it's not an empty string'
			if(inputFields[i].value == "" || emptyPat.test(inputFields[i].value))
			{
				// Otherwise replace it with a default value
				inputFields[i].value = "NULL";
			}
			else
			{
				// Make sure " " is replaced by "_"
				inputFields[i].value = inputFields[i].value.replace(/ /g, "_");
				// Make sure "/" is replaced by ""
				inputFields[i].value = inputFields[i].value.replace(/\//g, "");
			}
		}
	}
}

/************************************************************************
 * CONTACT FORM								*
 ************************************************************************/

// Submit contact form (perform ajax request)
$( function () {
	$('#contactForm').submit(function () {
		// Show spinner to indicate server is processing the request
		$('#spinner').html("<img style=\"vertical-align: middle;\"src=\"../images/layout/spinner.gif\" alt=\"Processing request ...\" />");
		// Serialize the form data and send this to the server
		$.post("../ajax/processContactForm.php", $(this).serialize(), function(data) {
			// Do this if the server's sent a valid response
			if(data != "")
			{
				// Show the note div
				$('#note').css({'display':'block'});
				// Put the server response inside the note div
				$('#note').html(
					data
				);
			}
			// Hide the spinner
			$('#spinner').html("");
		});
		return false;
	});
});

/************************************************************************
 * LOGIN FORM								*
 ************************************************************************/

// Submit login form
$( function () {
	$('#loginForm').submit(function () {
		return false;
	});
});


/************************************************************************
 * SELECTORS								*
 ************************************************************************/

// Update page contents when select value changes
$( function () {
	$('.selectUpdateContents').change(function () {
		// Submit the form, reload the page to get the correct contents
		$(this).parent().submit();
	});
});

