//<![CDATA[	

/* Clase para el login de Usuarios */
/* Usando jQuery */
/*
	divForm: Id capa del formulario
	divError: Id capa de mensaje de error
	divWait: Id capa de mensaje de espera
	urlValidate: Url de validacion
	urlRefresh (opcional): Url de refresco de la pagina
*/
function UserLogin(divForm, divError, divWait, urlValidate, urlRefresh, hideForm)
{
	this.DivForm = "#" + divForm;
	this.DivError = "#" + divError;
	this.DivWait = "#" + divWait;
	this.UrlValidate = urlValidate;
	this.UrlRefresh = urlRefresh;
	if(hideForm) 
		this.HideForm = hideForm;
	else 
		this.HideForm = true;
	
	$(this.DivWait).hide();
	$(this.DivError).hide();
	
	this.Login = function(idUser, idPassword)
	{
		var divError = this.DivError;
		var divForm = this.DivForm;
		var params = { user: "anonimo", password: "anonimo" };
		params.user = $('#' + idUser).val();
		params.password = hex_md5($('#' + idPassword).val());
		
		$(this.DivWait).bind("ajaxSend", function(){
			$(this).show();
		}).bind("ajaxComplete", function(){
			$(this).hide();
		});

		$(this.DivError).bind("ajaxSend", function(){
			$(this).hide();
		});

		if(hideForm)
		{
			$(this.DivForm).bind("ajaxSend", function(){
				$(this).hide();
			}).bind("ajaxError", function(){
				$(this).show();
			});
		}
		
		$.ajax({
			url: this.UrlValidate,
			cache: false,
			type: "POST",
			data: params,
			dataType: "json",
			success: function(data) {
				if(data.Logged)
				{
					if(data.UrlRedirect.length > 0)
						document.location.href = data.UrlRedirect;
					else
						document.location.href = this.UrlRefresh;
				}
				else
				{
					$(divError).html(data.ErrorMessage);
					$(divError).show();
					$(divForm).show();					
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				alert("Error: " + textStatus);
			}
		});
	};	
}


//]]>