$.fn.formErrors = function(errorMessage) {
	var thisObj = this;
	thisObj.before("<em class='error_message'>"+errorMessage+"</em>");
};


$.fn.submitForm = function() {


	// Hide Submit Button
	$(this).find('input').each(function() {
		if($(this).attr('type') == 'submit' || $(this).attr('type') == 'image') {$(this).hide();}
	});


	// Insert Output Element
	if($(this).find('.output').size() == 0) {
		$(this).append('<p class="output" style="display:none;">Sending...</p>');
	} else {
		if($(this).find('.output').is(':hidden')) {
			$(this).find('.output').html('Sending...');
		}
	}


	// Validate Form Fields
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var errors = new Array();
	var errcnt = 0;
	window.formId = $(this).attr('id'); // makes it global


	// Reset Erroneous Fields
	$('#'+formId+' input, #'+formId+' textarea').removeClass('error');
	$('#'+formId+' .error_message').each(function(){$(this).remove();});


	// Validate Required Fields & Email Fields
	$('#'+formId+' .required').each(function() {
		if($.trim($(this).val()).length == 0) {
			errors[errcnt] = '#'+formId+' #'+$(this).attr("id");
			$(this).formErrors("Required field is empty.");
			errcnt ++;}
		else if($(this).attr("name") == 'email'
		|| $(this).attr("name") == 'from_email'
		|| $(this).attr("name") == 'to_email') {
			if(emailRegEx.test($(this).val()) == false) {
				errors[errcnt] = '#'+formId+' #'+$(this).attr("id");
				$(this).formErrors("Please enter a valid email address (i.e. example@example.com).");
				errcnt ++;}}
	});


	// Check for invalid characters
	/*
	$('#'+formId+' input, #'+formId+' textarea').each(function() {
		if($(this).val().indexOf('<') != -1 
		|| $(this).val().indexOf('>') != -1) {
			errors[errcnt] = '#'+formId+' #'+$(this).attr("id");
			$(this).formErrors("Invalid characters. '<' and '>' are not allowed.");
			errcnt ++;}
	});
	*/


	// Highlight Errors
	if(errors.length != 0) {
		// Highlight the erroneous fields with a red border
		for (i = 0; i <= errors.length; i++)
		{
			$(errors[i]).addClass("error");
		}
		$(errors[0]).focus();

		$(this).find('input').each(function() {
			if($(this).attr('type') == 'submit' || $(this).attr('type') == 'image') {$(this).show();}
		});
	}


	// Submit Form
	else {
	    var options = { 
	        target:        '#'+formId+' .output', // target element(s) to be updated with server response 
	        success:       showResponse // post-submit callback
	    }; 

		$(this).ajaxSubmit(options);

		// Show Sending Status
		if($(this).find('.output').is(':hidden')) {
			$(this).find('.output').fadeIn('fast');
		}
	}
}


function showResponse(responseText, statusText)  {
	//alert('status: ' + statusText + '\n\nresponseText: \n' + responseText); 

	// Hide Sending Status
	$('#'+formId+' .output').delay(5000).fadeOut('fast');

	// Show Submit Button
	$('#'+formId+' input').each(function() {
		if($(this).attr('type') == 'submit' || $(this).attr('type') == 'image') {$(this).delay(5100).fadeIn('fast');}
	});
}




