﻿OrderProduct =
{
	elContainer : null,
	oMPopup : null,
	
	totalPrice : 0,
	
	init : function()
	{
		if (this.elContainer == null)
		{
			this.elContainer = utilObj.createEl("DIV");
			this.elContainer.id = "showAddProSuccess";
			this.elContainer.className = "divDisplayResult";
			this.elContainer.style.display = "none";
			utilObj.addChildToBody(this.elContainer);
		}
		this.cart.init();
	},
	
	show : function()
	{
		if (this.oMPopup == null)
		{
			this.oMPopup = new ModalPopup("showAddProSuccess", "OrderProduct.oMPopup");
		}
		this.oMPopup.display();
	},
	
	hide : function()
	{
		this.oMPopup.hide();
	},

	addToCart : function(productID, productName, typeID, typeName, price)
	{
		this.show();
		this.totalPrice += price;
		var stringRe = "Sản phẩm: <b>" + productName + "</b><br />";
		if (typeName != "")
		{
			stringRe += "Có dung lượng: <b>" + typeName + "</b><br />";
		}
		stringRe += "Đã thêm vào giỏ hàng của bạn ";
		this.elContainer.innerHTML = stringRe;
		this.action.addToCart(productID, typeID, function(re)
		{
			OrderProduct.elContainer.innerHTML += re ? "<b>thành công.</b>" : "<b>thất bại!</b>";
			setTimeout("OrderProduct.hide()", 1000);
		});
	},
	
	viewCart : function(e)
	{
		showDataWhenClick("#ViewCart", true, e);
	}
};

OrderProduct.cart = 
{
	elContainer : null,
	size : 
	{
		w : 100, 
		h : 50
	},
	
	paddingSize : 10,
	
	init : function()
	{
		var me = this;
		utilObj.addEvent(utilObj.wd, "resize", function(e) { me.show(e); });
		utilObj.addEvent(document, "scroll", function(e) { me.show(e); });
		this.createContainer();
		
	},
	
	createContainer : function()
	{
		if (this.elContainer == null)
		{
			this.elContainer = utilObj.createEl("DIV");
			this.elContainer.className = "divDisplayCart";
			this.elContainer.innerHTML = "<img class='myCart' src='Themes/Default/Images/Cart/MyCart.jpg' onclick=\"OrderProduct.viewCart(event)\" />";
			utilObj.addChildToBody(this.elContainer);
		}
	},

	scrollTop : function()
	{
		return utilObj.getDocumentScroll().scrollTop;
	},
	
	show : function(e)
	{
		var de = utilObj.getDocument();
		this.elContainer.style.display = "";
		this.elContainer.style.top = (this.scrollTop() + de.clientHeight - this.size.h - this.paddingSize) + "px";
		this.elContainer.style.left = (de.clientWidth - this.size.w - this.paddingSize) + "px";
	},
	
	hide : function()
	{
		this.elContainer.style.display = "none";
		this.elContainer.style.top = "-1000px";
		this.elContainer.style.left = "-1000px";
	}
};

OrderProduct.action = 
{
	serverAction : new OrderProductAction(),
	
	getData : function(result)
	{
		var data = null;
		if(result.hasErrors)
		{
			alert(result.errorsAlert);
		}
		else
		{
			data = result.data;
		}
		return data;
	},
	
	getDataAsyns : function(response)
	{
		var data = null;
		if (response.error != null)
		{
			alert(response.error.message);
		}
		else
		{
			data = this.getData(response.result);
		}
		return data;
	},
	
	addToCart : function(productID, typeID, fun)
	{
		var me = this;
		if (fun == null)
		{
			var result = this.serverAction.addToCart(productID, typeID);
			var data = this.getData(result);
			return data == null ? false : data;
		}
		else
		{
			this.serverAction.addToCart(productID, typeID, function(response)
			{
				var data = me.getDataAsyns(response);
				fun(data == null ? false : data);	
			});
		}
	},
	
	editQuantity : function(productID, typeID, quantity, fun)
	{
		var me = this;
		if (fun == null)
		{
			var result = this.serverAction.editQuantity(productID, typeID, quantity);
			var data = this.getData(result);
			return data == null ? false : data;
		}
		else
		{
			this.serverAction.editQuantity(productID, typeID, quantity, function(response)
			{
				var data = me.getDataAsyns(response);
				fun(data == null ? false : data);
			});
		}
	},
	
	removeProduct : function(productID, typeID, fun)
	{
		var me = this;
		if (fun == null)
		{
			var result = this.serverAction.removeProduct(productID, typeID);
			var data = this.getData(result);
			return data == null ? false : data;
		}
		else
		{
			this.serverAction.removeProduct(productID, typeID, function(response)
			{
				var data = me.getDataAsyns(response);
				fun(data == null ? false : data);	
			});
		}
	},
	
	getListProductInCart : function(fun)
	{
		var me = this;
		if (fun == null)
		{
			var result = this.serverAction.getListProductInCart();
			var data = this.getData(result);
			return data == null ? new Array() : data;
		}
		else
		{
			this.serverAction.addToCart(function(response)
			{
				var data = me.getDataAsyns(response);
				fun(data == null ? new Array() : data);	
			});
		}
	},
	
	order : function(addressOrder, noteOrder, listProOrder, fun)
	{
		var me = this;
		if (fun == null)
		{
			var result = this.serverAction.order(addressOrder, noteOrder, listProOrder);
			var data = this.getData(result);
			return data == null ? false : data;
		}
		else
		{
			this.serverAction.order(addressOrder, noteOrder, listProOrder, function(response)
			{
				var data = me.getDataAsyns(response);
				fun(data == null ? false : data);	
			});
		}
	}
};