/*
	Id: Cookie.get('IdBasket') || Try.these(
		function() { return IdBasket },
		function() { return document.location.toQueryParams(this.separator)['IdBasket']; },
		function() { return window.location.toQueryParams(this.separator)['IdBasket']; },
		function() { return $F('IdBasket') }
	) || false,
*/

var AjaxFactory = {
	url: false,
	IdAffiliate: false,
	separator: ';',
	messages: $H({"Unknown": "Error:"}),
	callbacks: $H(),
	OnError: function(code, message) {
		if (message) alert(message);
		else if (AjaxFactory.messages.get(code)) alert(AjaxFactory.messages.get(code));
		else if (AjaxFactory.callbacks.get(code)) AjaxFactory.callbacks.get(code)();
		else alert(AjaxFactory.messages.get("Unknown") + " " + code);
	},
	UpdateDisplay: function() { },

	BasketNew: function(callback) {
		var args = [ "Action=BasketNew" ];
		if (AjaxFactory.IdAffiliate)
			args.push("IdAffiliate=" + encodeURIComponent(AjaxFactory.IdAffiliate));
		new Ajax.Request(AjaxFactory.url + "?" + args.join(AjaxFactory.separator), {
			"evalJSON": true,
			"onSuccess": function(transport) {
				var data = transport.responseJSON;
				if (!data)
					AjaxFactory.OnError("EmptyReply");
				else if (data["Error"])
					AjaxFactory.OnError(data["Error"], data["Message"]);
				else {
					Cookie.set("IdBasket", data["IdBasket"], {"path": "/"});
					if (Cookie.get("IdBasket"))
						callback();
					else
						AjaxFactory.OnError("CookieSetFailed");
				}
			}
		});
		return false;
	},
	BasketLock: function(callback) {
		new Ajax.Request(AjaxFactory.url + "?Action=BasketLock", {
			"evalJSON": true,
			"onSuccess": function(transport) {
				var data = transport.responseJSON;
				if (!data)
					AjaxFactory.OnError("EmptyReply");
				else if (data["Error"])
					AjaxFactory.OnError(data["Error"], data["Message"]);
				else
					callback();
			}
		});
	},
	BasketDrop: function(callback) {
		new Ajax.Request(AjaxFactory.url + "?Action=BasketDrop", {
			"evalJSON": true,
			"onSuccess": function(transport) {
				var data = transport.responseJSON;
				if (!data)
					AjaxFactory.OnError("EmptyReply");
				else if (data["Error"])
					AjaxFactory.OnError(data["Error"], data["Message"]);
				else
					callback();
			}
		});
	},
	BasketView: function() {
		// TODO: Recapitulatif du panier
		return false;
	},
	BasketDrop: function() {
		// TODO: Vider le panier
		return false;
	},

	ItemList: function(callback) {
		var IdBasket = Cookie.get("IdBasket");
		if (IdBasket) {
			var args = [ "Action=ItemList",
				"IdBasket=" + encodeURIComponent(IdBasket)
			];
			new Ajax.Request(AjaxFactory.url + "?" + args.join(AjaxFactory.separator), {
				"onSuccess": function(transport) {
					var data = transport.responseJSON;
					if (!data)
						AjaxFactory.OnError("EmptyReply");
					else if (data["Error"])
						AjaxFactory.OnError(data["Error"], data["Message"]);
					else
						callback(data);
				}
			});
		} else
			callback([]);
		return false;
	},
	ItemNew: function(IdItem, Quantity, options) {
		var IdBasket = Cookie.get("IdBasket");
		if (IdBasket) {
			var args = [ "Action=ItemNew",
				"IdBasket=" + encodeURIComponent(IdBasket)
			];
			if (IdItem)
				args.push("IdItem=" + encodeURIComponent(IdItem));
			if (Quantity)
				args.push("Quantity=" + encodeURIComponent(Quantity));
			if (options) {
				if (options["IdOption"])
					args.push("IdOption=" + encodeURIComponent(options["IdOption"]));
				if (options["PriceET"])
					args.push("PriceET=" + encodeURIComponent(options["PriceET"]));
				if (options["TaxRate"])
					args.push("TaxRate=" + encodeURIComponent(options["TaxRate"]));
				if (options["PriceIT"])
					args.push("PriceIT=" + encodeURIComponent(options["PriceIT"]));
				if (options["Metadata"])
					args.push("Metadata=" + encodeURIComponent(options["Metadata"]));
			}
			new Ajax.Request(AjaxFactory.url + "?" + args.join(AjaxFactory.separator), {
				"evalJSON": true,
				"onSuccess": function(transport) {
					var data = transport.responseJSON;
					if (!data)
						AjaxFactory.OnError("EmptyReply");
					else if (data["Error"])
						AjaxFactory.OnError(data["Error"], data["Message"]);
					else
						AjaxFactory.UpdateDisplay(false, "ItemNew");
				}
			});
			return false;
		} else {
			AjaxFactory.BasketNew(function() {
				AjaxFactory.ItemNew(IdItem, Quantity, options || {});
			});
			return false;
		}
	},
	ItemDrop: function(IdItem, Metadata) {
		var IdBasket = Cookie.get("IdBasket");
		if (IdBasket) {
			var args = [ "Action=ItemDrop",
				"IdBasket=" + encodeURIComponent(IdBasket)
			];
			if (IdItem)
				args.push("IdItem=" + encodeURIComponent(IdItem));
			if (Metadata)
				args.push("Metadata=" + encodeURIComponent(Metadata));
			new Ajax.Request(AjaxFactory.url + "?" + args.join(AjaxFactory.separator), {
				"evalJSON": true,
				"onSuccess": function(transport) {
					var data = transport.responseJSON;
					if (!data)
						AjaxFactory.OnError("EmptyReply");
					else if (data["Error"])
						AjaxFactory.OnError(data["Error"], data["Message"]);
					else
						AjaxFactory.UpdateDisplay(false, "ItemDrop");
				}
			});
			return false;
		} else {
			AjaxFactory.OnError("IdBasketFailed");
		}
	},

	AddressNew: function(type, data, callback) {
		var IdBasket = Cookie.get("IdBasket");
		if (IdBasket) {
			data = $H(data).update({
				Action: "AddressNew",
				IdBasket: IdBasket,
				BasketAddress: type
			});
			new Ajax.Request(AjaxFactory.url, {
				"evalJSON": true,
				"method": "post",
				"parameters": data,
				"onSuccess": function(transport) {
					var data = transport.responseJSON;
					if (!data)
						AjaxFactory.OnError("EmptyReply");
					else if (data["Error"])
						AjaxFactory.OnError(data["Error"], data["Message"]);
					else
						callback();
				}
			});
			return false;
		} else {
			AjaxFactory.OnError("IdBasketFailed");
		}
	},
	AddressDrop: function(type, callback) {
		var IdBasket = Cookie.get("IdBasket");
		if (IdBasket) {
			var args = [ "Action=AddressDrop",
				"IdBasket=" + encodeURIComponent(IdBasket),
				"BasketAddress=" + encodeURIComponent(type)
			];
			new Ajax.Request(AjaxFactory.url + "?" + args.join(AjaxFactory.separator), {
				"evalJSON": true,
				"onSuccess": function(transport) {
					var data = transport.responseJSON;
					if (!data)
						AjaxFactory.OnError("EmptyReply");
					else if (data["Error"])
						AjaxFactory.OnError(data["Error"], data["Message"]);
					else
						callback();
				}
			});
			return false;
		} else {
			AjaxFactory.OnError("IdBasketFailed");
		}
	},

	ReductionNew: function(Password) {
		// TODO: Gestion des réductions
		return false;
	},
	ReductionList: function() {
		// TODO: Gestion des réductions
		return false;
	},
	ReductionDrop: function(IdReduction) {
		// TODO: Gestion des réductions
		return false;
	}
};

