﻿var WallArtCatalog = new __WallArtCatalog;
var WallArtShoppingCartUrl = "";
var WallArtCartIsWholeSale = false;

function __WallArtCatalog()
{
	this.CheckOutId = "";
	this.closeWindow = false;

	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 = WallArtCore.Window.WindowSize();
			var ObjectHeight = CartControl.offsetHeight;
			var ObjectWidth = CartControl.offsetWidth;
			var ScrollPosition = WallArtCore.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()
			{
				WallArtCore.Navigate("../../../Home.aspx");
			}	
		}
	}
	
	this.ChangeOption = function(pCell)
	{
		var thisPrice = pCell.parentNode.getAttribute("price");
		var ProductId = pCell.parentNode.getAttribute("productid");
		var ColorSku = pCell.parentNode.getAttribute("colorsku");
		var thisFont = document.getElementById("Price" + ProductId);
		var thisColorSku = document.getElementById("ColorSku" + ProductId);

		if (thisFont != null && thisColorSku != null)
		{	
			WallArtCore.HtmlElements.SetHtmlText(thisFont, WallArtCore.Math.FormatCurrency(thisPrice));
			thisColorSku.value = ColorSku;
		}
	}

	this.AdjustShoppingCart = function(pAction, pParams, Async)
	{
		var PageUrl = WallArtShoppingCartUrl;
		
		if (PageUrl != "")
		{
			PageUrl += (PageUrl.indexOf("?") ? "&" : "?") + "action=" + pAction;
			PageUrl += pParams;
	
			if (Async == false)
			{
				var ResultProcess = WallArtCore.DoCallBack(PageUrl, false);
	
				return ResultProcess;
			}
			else
			{
				WallArtCore.DoCallBack(PageUrl, true);
			}
		}
		
	}
	
	this.AddAllToCart = function(pCell, pForceProductId)
	{
		var CatalogTable = pCell.offsetParent.offsetParent;
		
		if (CatalogTable != null)
		{
			var AllDivs = CatalogTable.getElementsByTagName("DIV");
			
			if (AllDivs != null)
			{
				for (var iDiv = 0; iDiv < AllDivs.length; iDiv++)
				{
					var ProductId = WallArtCore.HtmlElements.GetObjectAttribute(AllDivs[iDiv], "productid");
					
					if (ProductId != "")
					{
						var ProductQuantityControl = document.getElementById("QuantityToAdd" + ProductId);
						
						if (ProductQuantityControl != null)
						{
							if (parseInt(ProductQuantityControl.value).toString() == "NaN")
							{
								ProductQuantityControl.value = "0";
							}
							
							if (parseInt(ProductQuantityControl.value) <= 0 && pForceProductId == ProductId)
							{
								ProductQuantityControl.value = "1";
							}
							
							if (parseInt(ProductQuantityControl.value) > 0)
							{
								this.AddToCart(ProductId);
								ProductQuantityControl.value = "0";
							}
						}
					}
				}
			}
		}
	}
	
	this.AddToCart = function(Id, UpdateReminder, pcloseWindow, pEdit, pKey, pReturnUrl)
	{
		UpdateReminder = (UpdateReminder == null) ? true : UpdateReminder;
		pEdit = (pEdit == null) ? "add" : pEdit;
		pKey = (pKey == null) ? "" : pKey;

		var thisColorSku = document.getElementById("ColorSku" + Id);
		var thisProductSku = document.getElementById("ProductSku" + Id);
		var thisFont = document.getElementById("Price" + Id);
		var thisQuantity = document.getElementById("QuantityToAdd" + Id);
		
		if (thisColorSku != null && thisProductSku != null && thisFont != null)
		{
			var Price = WallArtCore.HtmlElements.GetHtmlText(thisFont);
			var Params = "&csku=" + thisColorSku.value;
			Params += "&psku=" + thisProductSku.value;
			Params += "&price=" + Price.replace("$","");
			Params += "&pid=" + Id;
			Params += "&key=" + pKey;

			if (thisQuantity != null)
			{
				if (parseInt(thisQuantity.value) > 1)
				{
					Params += "&qty=" + thisQuantity.value;
				}
			}
			
			var AddResult = this.AdjustShoppingCart(pEdit, Params, false); 
			if (AddResult == "true")
			{
				if (UpdateReminder == true)
				{
					this.UpdateCartReminder();
				}
							
				if (pReturnUrl != null)
				{
					WallArtCore.Navigate("../../.." + pReturnUrl);
				}
				else
				{
					//this.closeWindow = (pcloseWindow == null) ? false : pcloseWindow;
					this.ConfirmAddCart();
				}
			}
			else
			{
				var Message = "Your Shopping Cart is full!<br/><br/>";
				Message += "You can still change quantities to existing<br/>";
				Message += "cart items, but you have reached the cart's<br/>";
				Message += "limit of unique products<br/><br/>";
				Message += "Please complete this order, by clicking on<br/>";
				Message += "\"Check Out\", and start a new order if you<br/>";
				Message += "need to purchase additional items<br/>";
				//Message += "\n" + AddResult;
				
				alert(Message);
			}
		}
		
		return false;
	}
	
	this.RemoveItem = function(pImg, pKey, pUpdate)
	{
		var Result = this.AdjustShoppingCart("remove", "&key=" + pKey, false);
		
		var Row = pImg.parentNode.parentNode;
		var Table = Row.parentNode;
		
		if (Row != null && Table != null)
		{
			Table.removeChild(Row);
		}
		
		if (WallArtCoreIsAdmin)
		{
			WallArtCore.Navigate(self.location.href);
		}
		else if (CanUpdate == 1)
		{
			this.UpdateTotal(pTable);
		}
						
		if (pUpdate == true)
		{
			this.UpdateCartReminder();
		}
	}
	
	this.EditItem = function(pSku, pImage, pKey, pUpdate, pRawUrl, pParams)
	{
		var Url = WallArtViewProductPage + "&key=" + pKey;
		
		switch (pSku)
		{
			case "custom":
				Url = WallArtCustomFramePage + "&pcat=cfm&key=" + pKey;
				break;
				
			case "custesc":
				Url = WallArtCustomVinylPage + "&pcat=vin&key=" + pKey;
				break;
				
			case "custia":
				Url = WallArtCustomAlphabetPage + "&pcat=iaa&key=" + pKey;
				break;
		}
		
		Url += (pParams == null) ? "" : "&" + pParams;
			
		WallArtCore.Navigate(Url + "&returnurl=" + pRawUrl);
	}
	
	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.UpdateCartQuantity = function(pText)
	{
		var Qty = parseInt(pText.value);
		
		if (Qty.toString() == "NaN")
		{
			alert("Please use whole numbers", "Value Error", "AlertWarning");
			pText.value = pText.getAttribute("storedvalue");
		}
		else if (Qty < 0)
		{
			alert("Negative quantities are not supported", "Value Error", "AlertWarning");
			pText.value = pText.getAttribute("storedvalue");	
		}
		else if (Qty == 0)
		{
			CustomDialog.Closed = function()
			{
				if (CustomDialog.Result == CustomDialog.No)
				{
					pText.value = pText.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 = pText.parentNode.parentNode;
			var Price = parseFloat(RowControl.getElementsByTagName("TD")[3].innerHTML.replace("$",""));
			var TotalControl = RowControl.getElementsByTagName("TD")[5];
			TotalControl.innerHTML = WallArtCore.Math.FormatCurrency(Price * Qty);
		}
	}
	
	this.ChangeCartQuantities = 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 (WallArtCoreIsAdmin)
		{
			WallArtCore.Navigate(self.location.href);
		}
		else if (CanUpdate == 1)
		{
			this.UpdateTotal(pTable);
		}
	}
	
	this.UpdateTotal = function(pTable)
	{
		var Rows = pTable.getElementsByTagName("TR");
		var Response = this.AdjustShoppingCart("gettotals", "", false);
		
		if (Response != "")
		{
			var xmlResponseData = WallArtCore.Xml.Parse(Response); 
			var ParentNode = xmlResponseData.getElementsByTagName("data")[0];
			
			if (ParentNode != null)
			{			
				var iRow = Rows.length - 1;
		
				while (iRow >= 0)
				{
					if (Rows[iRow].className == "Sum")
					{
						Rows[iRow].parentNode.removeChild(Rows[iRow]);
					}
					
					iRow -=1;
				}
				
				for (var iNode = 0; iNode < ParentNode.childNodes.length; iNode++)
				{
					var Elements = WallArtCore.Xml.GetNodeValue(ParentNode.childNodes[iNode]).split(":=");
					var NewRow = document.createElement("TR");
					
					NewRow.className = "Sum";
					
					var NewTitleCell = document.createElement("TD");
					var NewSumCell = document.createElement("TD");
					
					NewTitleCell.colSpan = Rows[0].cells.length - 1;
					NewTitleCell.innerHTML = Elements[0] + ":";
										
					NewSumCell.innerHTML = Elements[1];
					
					NewRow.appendChild(NewTitleCell);
					NewRow.appendChild(NewSumCell);
					
					Rows[0].parentNode.appendChild(NewRow);
				}
			}
		}
	}
	
	this.UpdateCartReminder = function()
	{
		var Items = this.AdjustShoppingCart("count", "", false);
		var Reminder = document.getElementById(WallArtShoppingCartReminder);
		var CheckOut = document.getElementById("divWallArtCheckOut");
		
		if (Reminder != null)
		{
			WallArtCore.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()
			{
				WallArtCore.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 += "</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.ConfirmAddCart = function(pRefresh)
	{
		if (!WallArtCartIsWholeSale)
		{
			CustomDialog.Closed = function()
			{
				if (CustomDialog.Result == CustomDialog.Continue)
				{
					if (pRefresh)
					{
						WallArtCore.Navigate(self.location.href);
					}
					else
					{
						WallArtCatalog.SetCheckOutPosition(true);
					}
				}
				else if (CustomDialog.Result == CustomDialog.Checkout)
				{
					WallArtCore.Navigate(WallArtCore.WrapUrl("/App_Pages/ShoppingCart/FinalCheckOut.aspx"));
				}
			}
			confirm("Would you like to continue<br/>shopping or check-out now?", "Items Successfully Added", "AlertCart", 9);
		}
	}	
}


document.body.onresize = function ()
{
	WallArtCatalog.SetCheckOutPosition();
}
window.onscroll = function()
{
	WallArtCatalog.SetCheckOutPosition();
}

//window.onload = function()
//{
//	WallArtCatalog.SetCheckOutPosition();
//}
//window.onload = function()
//{
//	alert("Custom Mesage Next");
//	CustomDialog.Closed = function()
//	{
//		confirm("Custom Confirm Message<br/>Are you sure?", "Wow!", CustomDialog.Style.YesNOCancel);
//		CustomDialog.Closed = function()
//		{
//			window.status = CustomDialog.Result;
//		}
//	}
//};