﻿jQuery.fn.jQueryForm = function() 
{
    $(this).submit(function() 
	{
	    var _form = this;
	     
	    $.ajax({ 
		        url: $(this).attr('action'), 
		        type: $(this).attr('method'), 
		        data: $(this).serialize(), 
		        dataType: 'xml', 
		        async: false,
		        success: function(data){ 
		        
		            $(_form).find('.text').removeClass('ui-state-error');
		            
		            if($(data).find('message').attr('error') == 'true')
		            {
		                $('#generic-alert [class=text]').text($(data).find('message').text());
					    $('#generic-alert').dialog('option', 'title', 'Error').dialog('open');
					    
		                $(data).find('errorField').each(function()
	                    {
                            $(_form).find(':[name=' +  $(this).text() +  ']').addClass('ui-state-error');
                        });
		            }
		            else
		            {
					    $('#generic-check [class=text]').text($(data).find('message').text());
					    $('#generic-check').dialog('option', 'title', 'Success').dialog('open');
					    $(_form).find('.text').val('');
		            }
		        }, 
		        error:function (xhr, ajaxOptions, thrownError){ 
		            $('#generic-alert [class=text]').text('An unexpected error has occured.');
					$('#generic-alert').dialog('option', 'title', 'Error ' + xhr.status).dialog('open');
                } 
            });

	        return false;
	    });
};

