/*AJAX FORM powered by Fernando Souza, nandosouza.com

Usage:
1 - Atribua o metodo a ser usado para a requisição juntamente com o arquivo a ser requisitado e a classe ajaxForm:
<form action="requestfile.php" method="GET" class="ajaxForm">

2 - Atribua o seletor da classe ajaxForm à função do plugin em seu script:
$(function(){
	$('.ajaxForm').ajaxForm();
});

Obs:
Para exibir o cabeçado da requisição utilize o evento onerror="" no formulário like this:
<form action="requestfile.php" method="GET" onerror="viewError" class="ajaxForm">

*/

(function($){
	$.fn.ajaxForm = function(options){
		this.each(function(){
						
			var $this     = $(this);
			var destino   = this.action;
			var $logText  = '<label class="log"></label>';
			var method    = $this.attr('method');
			var viewError = $this.attr('onerror');
			
			$this.append($logText);		
			$('.log').fadeOut();
			$('.log').ajaxStart(function() {
				$(this).html('Dados sendo enviados...');
			});
			$(".log").ajaxError(function(event, xhr, settings){
			   $(this).html("Error " + xhr.status + " - " + xhr.statusText);
			   if(viewError){
					var inspecionar = ''; 
					inspecionar += '<table id="t1">';
					inspecionar += '<tr><th>Objeto.propriedade</th><th>Resultado</th></tr>';
					inspecionar += '<tr><td>event.type</td><td>' + event.type + '</td></tr>';
					inspecionar += '<tr><td>event.target.nodeName</td><td>' + event.target.nodeName + '</td></tr>';
					inspecionar += '<tr><td>xhr.getResponseHeader("Content-Type")</td><td>' + xhr.getResponseHeader('Content-Type') + '</td></tr>';
					inspecionar += '<tr><td>xhr.getResponseHeader("Date")</td><td>' + xhr.getResponseHeader('Date') + '</td></tr>';
					inspecionar += '<tr><td>xhr.getResponseHeader("Server")</td><td>' + xhr.getResponseHeader('Server') + '</td></tr>';
					inspecionar += '<tr><td>xhr.status</td><td>' + xhr.status+ '</td></tr>';
					inspecionar += '<tr><td>xhr.statusText</td><td>' + xhr.statusText + '</td></tr>';
					inspecionar += '<tr><td>xhr.readyState</td><td>' + xhr.readyState + '</td></tr>';
					inspecionar += '<tr><td>settings.url</td><td>' + settings.url + '</td></tr>';
					inspecionar += '<tr><td>settings.type</td><td>' + settings.type + '</td></tr>';
					inspecionar += '</table>'; 
					$(this).html(inspecionar);
			   }
				
			 });
			
			if($this.is('form')&&$this.attr('action')!=''){
				$this.submit(function(){
					if(method){
						if(destino){
							var campos = $this.serialize();
							$.ajax({
							   type: method,
							   url: destino,
							   data: campos,
							   dataType: "html",
							   cache:false,
							   timeout:6000,
							   success: function(data){
								$this.find('.log').fadeIn().html(data);
							   }
							 });
						}else{
							alert('Informe o arquivo que processará os dados');
						}
					}else{
						alert('Informe metodo usado para o envio dos dados');
					}
					return false;
				})
			}else{
				alert('Um dos formulários não está com o atributo Action Definido');
			}
		})
		
	}
})(jQuery)
