
// Definition de la classe 'BudgetApproach'
// NB: L'ordre des methodes est important
function BudgetApproach(id)
{
	// Proprietes publiques et événements
	this.Id = id;
	this.HtmlObject = $("#" + id);
	this.Changed = new Array();
	this.SessionId = '';

	this.IsSet = false;
	this.MinPrice = 0;
	this.MinPriceLabel = '';
	this.MaxPrice = Infinity;
	this.MonthlyBudget = 0;
	this.Energy = null;
	
	// Champs et methodes privees
	var _this = this;
	var _title = _this.HtmlObject.find(".BudgetApproachTitle");
	var _window = $(".BudgetApproachWindowOuter");
	var _windowTitle = _window.find(".BudgetApproachWindowTitle");
	var _okButton = _window.find(".OkButton");
	var _cancelButton = _window.find(".CancelButton");
	var _shadow = $(".BudgetApproachShadow");
	var _subText = _this.HtmlObject.find(".BudgetApproachSubText");
		
	this.Show = function()
	{
		// Pour IE6...
		_shadow.width($(document.body).width() + "px"); 
		_shadow.height($(document.body).height() + "px");

		_shadow.css("opacity", "0.0").show().fadeTo("fast", "0.75",
			function()
			{
				_window.css({ "display": "block", "opacity": "0.0" })
					.animate({ "opacity": "1.0", "marginTop": "-=20px" });
			});
	}
		
	this.Hide = function()
	{
		_window.animate({ "opacity": "0.0", "marginTop": "+=20px" }, "normal",
			function()
			{
				$(this).css("display", "none");
				_shadow.fadeOut("fast");
			});
	}

	var ComputeSuccess = function(response)
	{
		_this.IsSet = true;
		_this.MinPrice = parseFloat(response["mmin"]);
		_this.MinPriceLabel = response["mmin_label"];
		_this.MaxPrice = parseFloat(response["mmax"]);
		_this.MonthlyBudget = parseFloat(response["mref"]);
		_this.Hide();

		_subText.html(response["subtext"]);
		
		_this.SessionId = response["financingSessionId"];
		Tools_DispatchEvent(_this.Changed, _this, {});
	}

	var ComputeFailed = function(response)
	{
		_this.Reset();
		_this.Hide();
	}
	
	var Compute = function()
	{
		var paramIds = new Array();
		var params = Tools_GetQueryParams();
		
		_this.Energy = null;
				
		_window.find("input[id^='sfg_'], select[id^='sfg_']").each(function()
		{
			var paramId = $(this).attr("id").split('_')[1];
			paramIds.push(paramId);
						
			var error = _window.find("#error_" + paramId);
			if (error.length > 0 && Tools_IsNullOrEmpty(error.html()) == false)
			{
				var value = Tools_ParseFloat($(this).attr("value"), 0);
				var min = Tools_ParseFloat($(this).attr("min"), 0);
				var max = Tools_ParseFloat($(this).attr("max"), 0);
				if (value < min || value > max)
					error.show().addClass("Visible");
				else
					error.hide().removeClass("Visible");
			}

			var paramValue = $(this).attr("value");
			params[paramId] = paramValue;
			if (paramId == 'ENGINEAB')
				_this.Energy = paramValue;
		});
		
		params["FinancingSessionId"] = _this.SessionId;
		params["paramIds"] = paramIds.join('|');
	
		// S'il n'y a pas d'erreur on fait l'appel ajax
		if (_window.find(".Error.Visible").length == 0)
			_currentRequest = Ajax_Execute(_financingServiceUrl, "GetMonthlyBudget", params, ComputeSuccess, ComputeFailed);
	}
	
	this.Reset = function()
	{
		_this.IsSet = false;
		_this.MinPrice = 0;
		_this.MinPriceLabel = '';
		_this.MaxPrice = Infinity;
		_this.MonthlyBudget = 0;
		_this.Energy = null;
		_subText.empty();
		
		Tools_DispatchEvent(_this.Changed, _this, {});
	}

	this.OnLoad = function()
	{
		_okButton.click(function() { Compute() });
		_cancelButton.click(function()
		{ 
			_this.Reset();
			_this.Hide();
		});
		
		_windowTitle.click(_this.Hide);
		_title.click(_this.Show);
		
		
	}
}

