<!--
	// if a form is required to be ajax submitted by default then add the ID here
	var ajaxSubmitForms = new Array();
	var ajaxAllForms    = false;
	var wait_img        = '<img src="http://www.plane-mad.com/pictures/please-wait.gif" alt="Please Wait" /> Loading';
	var wait_img_code   = '<div class="center wait">'+wait_img+'</div>';

	// Bind the function to the submit event on all form tags
	$(document).ready(function() {

		// pre load image for please wait
		pic1 = new Image(34,34); 
 		pic1.src = wait_img;

		// Auto submit AJAX forms
	    $('form').bind('submit', function (e) {
	    	return submitForm($(this));
		});		
	});
	
	/**
	* Find out whether the form is required to be submitted by an ajax request or not
	* @param string formId - HTML form tag id attribute of the form submitted
	* @return boolean true if the form is required to be submitted by ajax, false if not
	*/
	function ajaxSubmit(submitted_form) {
		var numForms   = ajaxSubmitForms.length;
		var i = 0;
		
		if ($(submitted_form).is('.comments')) {
			return true;
		}
		
		if (ajaxAllForms) {
			return true;
		}
		
		while (i < numForms) {
			if (ajaxSubmitForms[i] == submitted_form.attr('id')) {
				return true;
			}
			
			i++;
		}
		
		return false;
	}
	
	/**
	* Function to be run upon a successful response from the ajax request
	* Add a case statement for each form andthe actions to carry out from the ajax response
	* @param string formId - HTML form tag id attribute of the form submitted
	* @param array responseData - data received from the ajax request - array of values as set withn the PHP script
	*/
	function showAjaxRepsonse(submitted_form, responseData) {
		
		if ($(submitted_form).is('.comments')) {
			comments_response(submitted_form, responseData);
		}
		
		if (submitted_form.hasClass('forum-post-edit')) {
			show_post_edit(submitted_form, responseData);
		}
		
		if (submitted_form.hasClass('delete-post')) {
			show_post_delete(submitted_form, responseData);
		}
		
		if (submitted_form.hasClass('spot-edit')) {
			spot_edit_result(submitted_form, responseData);
		}
		
			//alert(responseData.output);
		switch (submitted_form.attr('id')) {
			case 'data-upload':
				$(submitted_form).find('div.results').html(responseData.output);
				reset_upload_form();
			break;
			
			case 'topic-reply':
				show_new_post(submitted_form, responseData);
			break;
			
			// Default behaviour - output - output within the a tag containing class="results" within the submitted form
			default: 
				$(submitted_form).find('div.results').html(responseData.output);
			break;
		
		}
		
		$('.wait').remove();
		$('.ajax-remove').removeClass('invisible');
		return true;
	}
	
	/**
	* Submit the form as an ajax reuest if required and if the form values entered are valid
	* @param string formId - HTML for element id attribute of the form being submitted
	* @return boolean true if the form needs to be submitted as a normal http post request rather than an ajax request
	* @return boolean false if the form should not be submitted as a normal http request
	*/
	function submitForm(submitted_form) {
	
    	// If this form is not required to be autosubmitted then return true so that normal http post request is made instead
    	if (!ajaxSubmit(submitted_form)) {
    		return true;
    	}
    	
    	//alert ('form autosubmitted');
    	// If form values are invalid then return false so that the form is not submitted
    	if (!validateForm(submitted_form)) {
    		return false;
    	}
    	
    	$(submitted_form).find('.ajax-remove').addClass('invisible');
    	$(submitted_form).find('.ajax-remove').after(wait_img_code);
    	
    	// If an action is entered them submit form to that page - otherwise submit form to current page
    	if ($(submitted_form).attr('action') == '') {
    		var submitUrl = location.href;
    	} else {
    		var submitUrl = $(submitted_form).attr('action');
    	}
    	   
    	var postData = '';
    	var j        = 0;
    	    	
    	// for each input type (jQuery input matches against all input, textarea, select and button elements)
    	// build a query string based on the name and values of the form elements
		$(submitted_form).find(':input').each(function (i) {
			
			if (($(this).attr('type') == 'radio') || ($(this).attr('type') == 'checkbox')) {
				if ($(this).is(':checked')) {
					if (j != 0) {
						postData += '&';
					}
					
					postData += this.name+'='+escape(this.value);
				}
			} else {
				if (j != 0) {
					postData += '&';
				}
				
				// concat {inputName}={inputValue} to our string
				postData += this.name+'='+escape(this.value);
			}
			
			j++;
	      });
		
		// Post an ajax request to the current page URL, repsonse type to be JSON and submit the query string built above
		// On success run the showAjaxRepsonse() method, on failure show alert with errors (NEEDS CHANGING BEFORE GOING LIVE)
		$.ajax({
           type: 'POST',
           dataType: 'json',
           url: submitUrl,
           data: postData,
           success: function(response){
				showAjaxRepsonse(submitted_form, response);
			},
           error: function(request, textStatus, errorThrown){
				//alert(request.status+' '+textStatus+' '+errorThrown+' '+request.responseText);
				$('.wait').remove();
				$('.ajax-remove').removeClass('invisible');
			}
           });
           
		// required to prevent form from submitting
		return false;
	}
	
	/*
	* Validate the form which is attempting to be submitted
	* To validate a form add a case statement in this function. Forms without a case statement require no validation
	* @param string id - HTML form tag id attribute of the form being submitted
	* @return boolean true if the form is valid, false if not
	*/
	function validateForm(submitted_form) {
		
		if ($(submitted_form).is('.comments')) {
			return validate_comment_form(submitted_form);
		}
		
		if (submitted_form.hasClass('delete-post')) {
			return confirm('Are you sure you want to delete this post?');
		}
		
		switch(submitted_form.attr('id')) {
			case 'data-upload':
				return upload_success;
			break;
			
			case 'topic-reply':
				return validate_reply();
			break;
			
			default:
				return true;
			break;
		}
	}

-->
