﻿//var WallArtShoppingCartUrl = "";
//var WallArtCartIsWholeSale = false;

var Catalog = new function()
{
	this.CheckOutId = "";
	this.closeWindow = false;
	this.IsWholeSale = false;
	this.CartTotalControlName = "";
	
	this.SetCheckOutPosition = function(pShowControl)
	{
		var CartControl = document.getElementById("divWallArtCheckOut");
		
		if (CartControl != null)
		{
			pShowControl = (pShowControl == null) ? false : pShowControl;
			
			if (pShowControl)
			{
				CartControl.style.display = "block";
			}
			
			var WindowSize = PageUI.Window.WindowSize();
			var ObjectHeight = CartControl.offsetHeight;
			var ObjectWidth = CartControl.offsetWidth;
			var ScrollPosition = PageUI.Window.GetScrollPosition();
			
			if (IE == 1 && IEVersion < 7)
			{						
				CartControl.style.top = ((WindowSize.height - ObjectHeight) + ScrollPosition.Top) + "px";
				CartControl.style.left = ((WindowSize.width - ObjectWidth) + ScrollPosition.Left) + "px";
			}
			else
			{
				CartControl.style.top = ((WindowSize.height - ObjectHeight)) + "px";
			}
		}
	}
	
	this.SetFinalCheckOutPosition = function()
	{
		window.scrollTo(0, 3000000); 
	}
	
	this.TestCart = function()
	{
		var Items = this.AdjustShoppingCart("count", "", false);
		
		if (parseInt(Items) == 0)
		{
			alert("You will now be redirected to the home page.", "Empty Cart");
			CustomDialog.Closed = function()
			{
				PageUI.Navigate("../../../Home.aspx");
			}	
		}
	}
	
	this.GotoCart = function()
	{
		var Items = this.AdjustShoppingCart("count", "", false);
		if (parseInt(Items) > 0)
		{
			PageUI.Navigate("/App_Pages/Cart/MyCart.aspx?returnurl=" + PageUI.Escape(self.location.href));
		}
		else
		{
			alert("You have no items in your cart");
		}
	}

	this.GotoCheckOut = function()
	{
		var Items = this.AdjustShoppingCart("count", "", false);
		if (parseInt(Items) > 0)
		{
			PageUI.Navigate("/App_Pages/Cart/Checkout.aspx?returnurl=" + PageUI.Escape(self.location.href));
		}
		else
		{
			alert("You have no items in your cart");
		}
	}
	
	this.AdjustShoppingCart = function(pAction, pParams, Async)
	{
		var PageUrl = "/App_Pages/ScriptSupport/AdjustShoppingCart.aspx";
		
		if (PageUrl != "")
		{
			PageUrl += (PageUrl.indexOf("?") >= 0 ? "&" : "?") + "action=" + pAction;
			PageUrl += pParams;

			if (Async == false)
			{
				var ResultProcess = PageUI.DoCallBack(PageUrl, false);
	
				return ResultProcess;
			}
			else
			{
				PageUI.DoCallBack(PageUrl, true);
			}
		}
		
	}
		
	this.AddItem = function()
	{
		var Params = this.AddItem.arguments;
		var ParamString = "";
		var iParam = 0;
		
		while (iParam < Params.length)
		{
			ParamString += "&" + Params[iParam] + "=" + PageUI.Escape(Params[iParam + 1]);
			
			iParam += 2;
		}
		
		var Result = this.AdjustShoppingCart("Add", ParamString, false);

		if (Result.trim() == "true")
		{
			if (ParamString.toLowerCase().indexOf("orderid") > 0 && ParamString.toLowerCase().indexOf("userid") > 0)
			{
				this.ConfirmChangeOrder();
			}
			else
			{
				this.ConfirmAddCart();
			}
		}
		else
		{	
			alert(ErrorCode_3010);
		}
	
		return false;
	}
	
	this.RemoveItem = function(pCell, pKey)
	{
		var Result = this.AdjustShoppingCart("remove", "&key=" + pKey, false);
		
		//self.location.href = self.location.href;
		this.UpdateCartControl();
	}
	
	this.EditItemQuantity = function(pKey, pControl)
	{
		var Qty = parseInt(pControl.value);
		
		if (Qty.toString() == "NaN")
		{
			alert("Please use whole numbers", "Value Error", "AlertWarning");
			pControl.value = pControl.getAttribute("storedvalue");
		}
		else if (Qty < 0)
		{
			alert("Negative quantities are not supported", "Value Error", "AlertWarning");
			pControl.value = pControl.getAttribute("storedvalue");	
		}
		else if (Qty == 0)
		{
			CustomDialog.Closed = function()
			{
				if (CustomDialog.Result == CustomDialog.No)
				{
					pControl.value = pControl.getAttribute("storedvalue");
				}
			}
			confirm("You have selected a \"0\" quantity.<br/>This will remove the item from your cart.<br/><br/>Are you sure?", "Zero Value", "AlertWarning")
		}
		else
		{
			var RowControl = pControl.parentNode.parentNode;
			var Price = parseFloat(RowControl.getElementsByTagName("TD")[4].attributes.getNamedItem("price").value);
			var TotalControl = RowControl.getElementsByTagName("TD")[6];
			var Symbol = RowControl.getElementsByTagName("TD")[4].innerHTML.replace(PageUI.Math.FormatCurrency(Price, ""), "");
			TotalControl.innerHTML = PageUI.Math.FormatCurrency(Price * Qty, Symbol);
		}
	}
	
	this.UpdateCart = function(pTable)
	{
		var Rows = pTable.getElementsByTagName("TR");
		var QtyCell = Rows[0].getElementsByTagName("TD").length - 2;
		var CanUpdate = 0;
		
		var iRow = Rows.length - 1;
		
		while (iRow >= 0)
		{
			var Cells = Rows[iRow].getElementsByTagName("TD");
			
			if (Rows[iRow].className != "Sum" && Rows[iRow].className != "Header")
			{	
				var thisText = Cells[QtyCell].getElementsByTagName("INPUT")[0];
				var thisQTY = parseFloat(thisText.value);
				var storedQTY = parseFloat(thisText.getAttribute("storedvalue"));
				var thisKey = thisText.getAttribute("keyvalue");
				
				if (storedQTY != thisQTY)
				{
					this.AdjustShoppingCart("adjust", "&key=" + thisKey + "&qty=" + thisQTY, false);
					CanUpdate = 1;
				}
			}
			
			iRow -=1;
		}
		
		if (CanUpdate) 
		{
			this.UpdateCartControl();
			//PageUI.Navigate(self.location.href);
		}
	}
		
	this.EditShipping = function(pControl)
	{
		pControl.disabled = true;
		var ControlId = pControl.id;
		var Response = this.AdjustShoppingCart("updateshipping", "&id=" + ControlId + "&value=" + pControl.value, false);
		window.status = "&id=" + ControlId + "&value=" + pControl.value;
		pControl.disabled = false;
		
		return false;
	}
	
	this.SendEmail = function(pOrderId, pOrderType, pFFFFOrderId)
	{
		PageUI.DoFormPostComplete = function()
		{
			if (PageUI.DoFormPostResult.toLowerCase() != "success")
			{
				alert("An email could not be generated at this time<br/><br/>" + PageUI.DoFormPostResult);
			}
		}
		PageUI.DoFormPost("/App_Pages/ScriptSupport/FormSupport.aspx", "action", "NewOrderEmail", "OrderId", pOrderId, "OrderType", pOrderType, "FFFFOrderId", pFFFFOrderId);
	}
	
	this.SendNewUserEmail = function(pUserId)
	{
		PageUI.DoFormPostComplete = function()
		{
			if (PageUI.DoFormPostResult.toLowerCase() != "success")
			{
				alert("An email could not be generate at this time<br/><br/>" + PageUI.DoFormPostResult);
			}
		}
		PageUI.DoFormPost("/App_Pages/ScriptSupport/FormSupport.aspx", "action", "NewUserEmail", "UserId", pUserId);
	}
	
	this.UpdateCartReminder = function()
	{
		var Items = this.AdjustShoppingCart("count", "", false);
		var Reminder = document.getElementById(WallArtShoppingCartReminder);
		var CheckOut = document.getElementById("divWallArtCheckOut");
		
		if (Reminder != null)
		{
			PageUI.HtmlElements.SetHtmlText(Reminder, Items + " Items");
		}
		if (CheckOut != null)
		{
			CheckOut.style.display = ((parseInt(Items) > 0) ? "block" : "none");
		}
		
		var sUrl = self.location.href;
		if (sUrl.toLowerCase().indexOf("finalcheckout") > 0 && parseInt(Items) == 0)
		{
			alert("You will now be redirected to the home page.", "Empty Cart");
			CustomDialog.Closed = function()
			{
				PageUI.Navigate("../../../Home.aspx");
			}
		}
	}	
	
	this.ExpandOption = function(pControl, pDivision)
	{
		var DivControl = document.getElementById(pDivision);
		
		if (pControl.checked == true)
		{
			DivControl.style.display = "none";
		}
		else
		{
			DivControl.style.display = "block";
		}
	}
	
	this.PrintReceipt = function(pConfirmationCode)
	{
		var divHtml = document.getElementById("WallArtReceipt");
		
		if (divHtml != null)
		{
			var WindowOptions = "toolbar=yes,location=no,directories=no,menubar=no,";
				WindowOptions += "scrollbars=yes,width=600,height=500,left=100,top=25";
			var newWindow = window.open('','Recepit' + pConfirmationCode, WindowOptions);
			
			var Script = "<html><head><style>";
			Script += "body {font-size:11px; font-family:verdana, arial;}\n";
			Script += "table {width:100%; font-size:11px;}\n";
			Script += "tr {vertical-align:top;}\n";
			Script += "td {padding-top:13px; padding-bottom:13px;}\n";
			Script += "div.Caption {font-weight:bold;}\n";
			Script += "table.Cart {border:1px solid #000;}\n";
			Script += "table.Cart tr.Header {background-color:#000; color:#FFF; padding:4px; font-weight:bold;}\n";
			Script += "table.Cart td {border-bottom:1px solid #000; padding:4px;}\n";
			Script += "table.Cart tr.CartRow td {background-color:#CCC;}\n";
			Script += "table.Cart tr.CartAlternateRow td {}\n";
			Script += "table.Cart tr.Sum td {border:none; font-weight:bold;}\n";
			Script += "table.Cart tr td img { width:60px;}\n";
			Script += "table.Cart tr td div.Name { float:left;}\n";
			Script += "table.Cart tr td div.SampleImage { float:left;}\n";
			Script += "</style>";
			Script += "</head><body>" + divHtml.innerHTML;
			Script += "</body></html>";
			
			//var WindowText = newWindow.document.documentElement.outerHTML; 
			//if (WindowText.toLowerCase().indexOf("<html><head></head>") == 0)
			//{
				newWindow.document.write(Script);
				newWindow.print();
				newWindow.document.execCommand('print', false, null);
			//}
			//else
			//{
			//	alert("A receipt window is already open. Please close your receipt window to print a new receipt");
			//}
		}
	}
	
	this.SelectNewFrame = function(pCell, TagName)
	{
		var Parent;
		
		if (TagName == "DIV")
			Parent = pCell.parentNode.parentNode.getElementsByTagName(TagName);
		else
			Parent = pCell.parentNode.parentNode.parentNode.getElementsByTagName(TagName);

		if (Parent != null)
		{
			for (var iLoop = 0; iLoop < Parent.length; iLoop++)
			{
				Parent[iLoop].style.borderColor = "transparent";
			}
		}
		
		pCell.parentNode.style.borderColor = "#FF6600";
	}

	this.ConfirmChangeOrder = function()
	{
		PageUI.Navigate("/App_pages/Cart/Checkout.aspx");
	}	

	this.ConfirmAddCart = function(pRefresh)
	{
		if (!this.IsWholeSale)
		{
			CustomDialog.Closed = function()
			{
				if (CustomDialog.Result == CustomDialog.Continue)
				{
					if (pRefresh)
					{
						PageUI.Navigate(self.location.href);
					}
					else
					{
						Catalog.SetCheckOutPosition(true);
					}
				}
				else if (CustomDialog.Result == CustomDialog.Checkout)
				{
					PageUI.Navigate("/App_Pages/Cart/CheckOut.aspx");
				}
			}
			confirm(Confirm_AddToCart, "Items Successfully Added", "", 9);
		}
	}	
	
	this.UpdateCartControl = function(pCountry, pState)
	{
		var CartControl = document.getElementById("divShoppingCartControl");
		if (CartControl != null)
		{
			var PageUrl = "/App_Pages/ScriptSupport/AdjustShoppingCart.aspx?action=get";
			PageUrl += (pCountry != null ? "&country=" + pCountry : "");
			PageUrl += (pState != null ? "&state=" + pState : "");
			
			var Response = PageUI.DoCallBack(PageUrl, false);
			
			CartControl.innerHTML = Response;
		}
		
		var TotalControl = document.getElementById(this.CartTotalControlName);
		if (TotalControl != null)
		{
			var Result = this.AdjustShoppingCart("GetTotal", "", false);
			
			TotalControl.innerHTML = Result;
		}
	}
	
}