// JavaScript Document

// LOAD JQUERY FUNCTIONS
$(document).ready(function(){
	
	/* LOGIN */	
	$("#FrmLogin").loginSubmit();
	
	/* DO RESET*/
	$("input").filter("#btnCancel").click(function(){	
		$("#LoginErrors").hide("slow");
		return false;
	})
	.end()	
	
});


/* AJAX LOGIN PROCESS */
$.fn.loginSubmit = function(e) { 
/* Change a form's submission type to ajax */ 		
	this.submit(function(){
		var params = {}; 
		$(this) .find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
		.filter(":enabled").each(function() { 
			params[ this.name || this.id || this.parentNode.name || this.parentNode.id ] = this.value;
		});	
			
		$("body").addClass("curWait");
		
		$.post(appUrl + "/app/modules/member/login.ajax.php", params, function(xml){ 
			$("body").removeClass("curWait"); 
			strError = "L'envoi du formulaire a échoué. Merci de ré-essayer plus tard."; oFocus = null; 
			$("AjaxResponse", xml).each(function() {
				strRedirect = this.getAttribute("redirecturl"); 
				strError = this.getAttribute("error"); 
				//oFocus = this.getAttribute("focus"); 				
			});
			if (strError.length == 0) { 
				//window.location = appUrl+"/app/modules/member/myAccount.php";
				window.location = appUrl+strRedirect;
				// Debug Ajax Response
				//$("#LoginBox").append("POST:" + xml); 
			} else { 
				//alert("Les erreurs suivantes ont été rencontrées:\n" + strError);
				$("#LoginErrors").addClass("formErrors").html("<ul>" + strError.replace(/(\t)(.+)/g, "<li>$2<\/li>") + "<\/ul>").filter(":hidden").fadeIn("1500"); 
				//$("#LoginErrors").hide("slow");

				//$("#LoginErrors").fadeIn(1000).fadeOut(6000);
    	

				if (oFocus) $("#" + oFocus).get(0).focus(); 
			} 
		}); 
		return false; 
	}); 
	return this;
}