$(function(){
	
	// add the events below only if the contact form exists on the page
	if( $( ".contact_form:first" ).length > 0 ) {
		
		// handle focusin in event on input elements
		// simply remove the current text if it is equal to the default text
		$( ".contact_form:first > input:text" ).focusin( function() {
			if( $( this ).val() == $( this ).attr( "name" ) ) {
				$( this ).data( "focus", $( this ).val() );
				$( this ).val( "" );
			}
		});
		
		// handle focusout in event on input elements
		// simply restores the default text if the field is left empty
		$( ".contact_form:first > input:text" ).focusout( function() {
			if( $( this ).val().length == 0 ) {
				$( this ).val( $( this ).data( "focus" ) );
			}
		});
		
		// handle focusin in event on textarea elements
		// simply remove the current text if it is equal to the default text
		$( ".contact_form:first > textarea" ).focusin( function() {
			if( $( this ).val() == $( this ).attr( "name" ) ) {
				$( this ).data( "focus", $( this ).val() );
				$( this ).val( "" );
			}
		});
		
		// handle focusout in event on textarea elements
		// simply restores the default text if the field is left empty
		$( ".contact_form:first > textarea" ).focusout( function() {
			if( $( this ).val().length == 0 ) {
				$( this ).val( $( this ).data( "focus" ) );
			}
		});
		
		// handle click on the reset button
		// the form knows what to do so the only thing
		// left is to remove the errors if there are any errors
		// displayed
		$( ".contact_form:first > #reset" ).click( function() {
			$( "#contact_form_errors" ).slideUp( "fast" );
		});
		
		// handle form submission
		$( ".contact_form:first > #send" ).click( function() {
			var error_flag = false;
			var errors = [];
			
			// check inputs
			$( ".contact_form:first > input:text" ).each( function() {
				if( $( this ).val().length == 0 || $( this ).val() == $( this ).attr( "name" ) ) {
					error_flag = true;
					errors.push( capitaliseFirstLetter( $( this ).attr( "name" ) ) + " cannot be empty." );
				}
			});
			
			// check textareas
			$( ".contact_form:first > textarea" ).each( function() {
				if( $( this ).val().length == 0 || $( this ).val() == $( this ).attr( "name" ) ) {
					error_flag = true;
					errors.push( capitaliseFirstLetter( $( this ).attr( "name" ) ) + " cannot be empty." );
				}
			});
			
			// if there are errors, notify the user
			// else:
			// 1. Block the form
			// 2. Send the contact form to our ajax handler
			// 3. When ajax handler returns check the return result
			// 4. On success display success message and unblock the form
			// 5. On error display the error message from the ajax handler and unblock the form
			if( error_flag ) {
				
				var error_message = '<ul class="error_messages">';
				$.each( errors, function( i, v ) {
					error_message += '<li class="error_message">' + v + '</li>';
				});
				error_message += '</ul>';
				
				$( "#contact_form_errors" ).slideUp( "fast", function() {
					$( "#contact_form_errors" ).html( error_message ).slideDown( "fast" );
				})
				
				return false;
			} else {
				$.post( "index.php?plugin=contact_form", {
									controller: "form",
									action: "save_contact_form",
									data: {
										"name": $( "#name" ).val(),
										"phone": $( "#phone" ).val(),
										"email": $( "#email" ).val(),
										"message": $( "#message" ).val()
									}
								},
								function( data ) {
									if( data.error == 1 ) {
										var error_message = '<ul class="error_messages">';
										error_message += '<li class="error_message">' + data.message + '</li>';
										error_message += '</ul>';
										$( "#contact_form_errors" ).slideUp( "fast", function() {
											$( "#contact_form_errors" ).html( error_message ).slideDown( "fast" );
										})
									} else {
										$( "#contact_form_errors" ).slideUp( "fast", function() {
											var success_message = '<ul class="success_messages">';
											success_message += '<li class="success_message">Thank you! We will get back to you in the next 48 hours.</li>';
											success_message += '</ul>';
											$( "#contact_form_errors" ).html( success_message ).slideDown( "fast" );
										})
									}
								},
								"json"
							);
				return false;
			}
		});
	}
});

/**
 * Capitalizes the first letter of a string
 *
 * @return string The string with the first letter capitalized
 */
function capitaliseFirstLetter( string ) {
    return string.charAt(0).toUpperCase() + string.slice(1);
}

