﻿var WallArtControlUI = new __WallArtControlUI;

function OperatingCosts()
//region
{
	this.PackingCost = 0;
	this.MatCost = 0;
	this.FrameCost = 0;
	this.ImageCost = 0;
	this.VinylCost = 0;
	this.Factor = 0;
	this.IsWholesale = false;
	this.MinimumIncome = 5;
	this.CommissionRate = .15;
	this.GUIType = "custom frames";
	
	this.GetOperatingCost = function(pFactor, pPackingCost, pMatCost, pFrameCost, pImageCost, pVinylCost)
	{
		var Cost = 0;
		
		this.MatCost = (pMatCost == null) ? 0 : parseFloat(pMatCost);
		this.FrameCost = (pFrameCost == null) ? 0 : parseFloat(pFrameCost);
		this.ImageCost = (pImageCost == null) ? 0 : parseFloat(pImageCost);
		this.VinylCost = (pVinylCost == null) ? 0 : parseFloat(pVinylCost);
		this.PackingCost = (pPackingCost == null) ? 0 : parseFloat(pPackingCost);
		this.Factor = (pFactor == null) ? 0 : parseFloat(pFactor);
		
		if (this.GUIType.toLowerCase() == "custom frames")
		{
			Cost = this.GetCFMOperatingCost();
		}
		else if (this.GUIType.toLowerCase() == "custom vinyl")
		{
			Cost = this.GetVINOperatingCosts();
		}
		else if (this.GUIType.toLowerCase() == "custom alphabet")
		{
			Cost = this.GetIAAOperatingCost();
		}
		
		return Cost;
	}
	
	this.GetBaseCost = function(pAdditionCosts)
	{
		var BaseCost = this.PackingCost + this.MatCost + this.FrameCost;
		BaseCost += (pAdditionCosts == null) ? 0 : pAdditionCosts;
		BaseCost = !this.IsWholesale ? (BaseCost / WallArtGUIClientMarkUp) : BaseCost;
		
		return BaseCost;
	}
	
	this.NecessaryIncom = function()
	{
		var TotalCost = this.GetBaseCost() / this.Factor;
		var Minimum = (this.MinimumIncome + TotalCost) / (1 - this.CommissionRate);
		
		return Minimum;
	}
	
	this.GetIAAOperatingCost = function()
	{
		var MinCost = 0;
		
		if (this.FrameCost > 0 || this.MatCost > 0)
		{
			var TotalCost = this.GetBaseCost(this.ImageCost);
			var MinimumCost = this.NecessaryIncom();
			
			if (TotalCost < MinimumCost)
			{
				MinCost = MinimumCost - TotalCost;
			}
		}
		
		return MinCost;
	}
	
	this.GetCFMOperatingCost = function()
	{
		var MinCost = 0;
		
		if (this.FrameCost > 0 || this.MatCost > 0)
		{
			var TotalCost = this.GetBaseCost();
			var MinimumCost = this.NecessaryIncom();
			
			if (TotalCost < MinimumCost)
			{
				MinCost = MinimumCost - TotalCost;
			}
		}
		
		return MinCost;
	}
	
	this.GetVINOperatingCost = function()
	{
		return 0;
	}
	
	this.GetCommission = function(pCost)
	{
		return pCost * this.CommissionRate;
	}
}
//endregion

function CleanImageName(pImageName)
{
//region
	pImageName = pImageName.replace(".gif", "").replace(".jpg", "");
	
	if (pImageName.indexOf("_") > 0)
	{
		var ImageName = pImageName.split("_");
		pImageName = ImageName[0];
	}
	
	return pImageName;
}
//endregion

function ErrorInterface()
{
	this.ShowError = function(pErrorCode, pParentControl)
	{
		WallArtCore.SetOpacity(100,"WallArtGUIErrorMessage");
		ErrorWin = this.InitError(pErrorCode, pParentControl, "WallArtGUIErrorMessage");
		WallArtCore.Fade("WallArtGUIErrorMessage", 100, 0, 1000); 
	}
	
	this.ShowWarning = function(pErrorCode, pParentControl)
	{
		this.InitError(pErrorCode, pParentControl, "WallArtGUIWarningMessage");
	}
	
	this.InitError = function(pErrorCode, pParentControl, pErrorId)
	{
		var ParentControl = (typeof(pParentControl) == "string") ? document.getElementById(pParentControl) : pParentControl;
		var ParentCoords = ParentControl ? WallArtCore.HtmlElements.GetObjectDimensions(ParentControl) : null;
		var ErrorControl = SetError(pErrorCode, pErrorId);
		
		if (ErrorControl != null && ParentControl != null)
		{
			var ErrorCoords = WallArtCore.HtmlElements.GetObjectDimensions(ErrorControl);
			var PositionY = parseInt(ParentCoords.Top) + parseInt(ParentCoords.Height);
			var PositionX = (parseInt(ParentCoords.Left) + (parseInt(ParentCoords.Width) / 2)) - (parseInt(ErrorCoords.Width) / 2);
			
			ErrorControl.style.left = PositionX + "px";
			ErrorControl.style.top = PositionY + "px";
		}
		
		return ErrorControl;
		
		function SetError(pError, pErrorId)
		{
			var ErrorControl = document.getElementById(pErrorId);
				
			if (ErrorControl != null)
			{
				var Labels = ErrorControl.getElementsByTagName("LABEL");
				if (Labels != null)
				{
					Labels[0].innerHTML = pError;
				}
				ErrorControl.style.display = "block";
			}
			
			return ErrorControl;
		}
	}
}

function MatInterface()
{
	this.FileName = "";
	this.Code = "";
	
	this.Reset = function()
	{
		this.FileName = "";
		this.Code = "";
	}
	
	this.SetImage = function(pFileName, pCode)
	{
		this.FileName = pFileName;
		this.Code = pCode;
		this.SetCaption(pFileName);
	}
	
	this.SetFromControl = function()
	{
		var ControlName = WallArtCore.HtmlElements.GetElementId("spcFrameControl_MatContainer", "DIV");
		var Control = document.getElementById(ControlName);
		var thisCost = 0;
		
		if (Control != null)
		{
			thisCost = WallArtCore.HtmlElements.GetObjectAttribute(Control, "cost");
			
			var thisCode = WallArtCore.HtmlElements.GetObjectAttribute(Control, "code");
			var thisFileName = WallArtCore.HtmlElements.GetObjectAttribute(Control, "filename");
			
			this.FileName = thisFileName;
			this.Code = thisCode;
		}
		
		return thisCost;
	}
	
	this.SetCaption = function(pFileName)
	{
	    var MatText = "<i>Mat: </i><b>" + ((pFileName == "") ? "None Selected" : CleanImageName(pFileName)) + "</b>";
		document.getElementById("BuildGUIAdd_Mat").innerHTML = MatText;
	}
	
	this.SetMatFromCart = function(pMatName)
	{
		this.FileName = pMatName;
		
		var Broken = pMatName.split("_");
		pMatName = Broken[0];
		
		var ControlName = WallArtCore.HtmlElements.GetElementId("spcFrameControl_MatContainer", "DIV");
		var Control = document.getElementById(ControlName);
		var NewCost = 0;
		
		if (Control != null)
		{
			var Images = Control.getElementsByTagName("IMG");
			if (Images.length != null)
			{
				var TopImage = null;
				
				for (var iImage = 0; iImage < Images.length; iImage++)
				{
					if (Images[iImage].id.indexOf("TopImage") > 0)
					{
						TopImage = Images[iImage];
					}
					else
					{
						var ImageValue = Images[iImage].value;
						
						if (ImageValue != null)
						{
							if (ImageValue.toUpperCase() == pMatName.toUpperCase())
							{
								this.SetCaption(pMatName);
								this.Code = Images[iImage].attributes.getNamedItem("code").value;
								
								NewCost = parseFloat(Images[iImage].attributes.getNamedItem("cost").value);
								TopImage.src = Images[iImage].src;
							}
						}
					}
				}
			}
		}
		
		return NewCost;
	}
	
	this.SetMatImage = function(pMatName)
	{
		var ControlName = WallArtCore.HtmlElements.GetElementId("spcFrameControl_MatContainer", "DIV");
		var Control = document.getElementById(ControlName);
		
		if (Control != null)
		{
			var Images = Control.getElementsByTagName("IMG");
			if (Images.length != null)
			{
				for (var iImage = 0; iImage < Images.length; iImage++)
				{
					var ImageValue = Images[iImage].value;
					
					if (ImageValue != null)
					{
						if (ImageValue.toUpperCase() == pMatName.toUpperCase())
						{
							if (Images[iImage].fireEvent) 
							{ 
								Images[iImage].fireEvent("onclick");
							} 
							else if(document.createEvent) 
							{ 
								var EventTrigger = document.createEvent("HTMLEvents");
								EventTrigger.initEvent("click", true, true);
								Images[iImage].dispatchEvent(EventTrigger);
							} 
						}
					}
				}
			}
		}
	}
	
	this.GetImage = function()
	{
		return this.FileName;
	}
}

function FrameInterface()
{
	this.FileName = "";
	this.Code = "";
	this.Cost = 0;
	this.Width = "";
	this.Render = "Stretch";
	this.Factor = 2;
	
	this.Reset = function()
	{
		this.FileName = "";
		this.Code = "";
		this.Cost = 0;
		this.Width = "";
		this.Render = "Stretch";
		this.Factor = 2;
	}
	
	this.SetImage = function(pFileName, pCost, pCode, pWidth, pRender, pFactor)
	{
		pWidth = (pWidth == null) ? "" : pWidth;
		pRender = (pRender == null) ? "" : pRender;
		
		this.Cost = pCost;
		this.Code = pCode;
		this.Factor = pFactor;
		
		this.FileName = pFileName;
		if (pRender != "")
		{
			this.FileName = this.FileName.replace(".", "_" + pRender + ".");
		}
		
		if (pWidth != "")
		{
			if (pWidth.indexOf(".") < 0)
			{
				pWidth = pWidth + ".000";
			}
			this.FileName = this.FileName.replace(".", "_" + ((parseFloat(pWidth) == 0) ? "0.5" : pWidth) + ".");
		}

        this.SetCaption(pFileName);
	}
	
	this.SetCaption = function(pFileName)
	{
	    var FrameText = "<i>Frame: </i><b>" + ((pFileName == "") ? "None Selected" : CleanImageName(pFileName)) + "</b>";
		document.getElementById("BuildGUIAdd_Frame").innerHTML = FrameText;
	}
	
	this.SetFrameFromCart = function(pFrameName)
	{
		this.FileName = pFrameName;
		
		if (pFrameName.indexOf("_") > 0)
		{
			var Broken = pFrameName.split("_");
			pFrameName = Broken[0] + ((pFrameName.toUpperCase().indexOf(".GIF") > 0) ? ".gif" : ".jpg");
		}

		var NewCost = 0;
		var NewWidth = 0;
		var NewFactor = 0;
		var ControlName = WallArtCore.HtmlElements.GetElementId("spcFrameControl_pnlFrames", "DIV");
		var Control = document.getElementById(ControlName);
		
		if (Control != null)
		{
			var Images = Control.getElementsByTagName("IMG");
			if (Images != null)
			{
				for (var iImage = 0; iImage < Images.length; iImage++)
				{
					var ImageSource = Images[iImage].src;
					while (ImageSource.indexOf("%20") > 0)
					{
						ImageSource = ImageSource.replace("%20", " ");
					}
					
					
					if (ImageSource != null)
					{
						if (ImageSource.toUpperCase().indexOf(pFrameName.toUpperCase()) > 0)
						{
							this.Cost = parseFloat(Images[iImage].attributes.getNamedItem("cost").value);
							this.Code = Images[iImage].attributes.getNamedItem("code").value;
							this.Width = parseFloat(Images[iImage].attributes.getNamedItem("framewidth").value);
							
							NewWidth = this.Width;
							NewCost = this.Cost;
							NewFactor = Images[iImage].attributes.getNamedItem("factor").value;
							
							this.SetCaption(pFrameName);
						}
					}
				}
			}
		}

		return {Cost:NewCost, Width:NewWidth, Factor:NewFactor};
	}
	
	this.SetFrameImage = function(pFrameName)
	{
		var ControlName = WallArtCore.HtmlElements.GetElementId("spcFrameControl_pnlFrames", "DIV");
		var Control = document.getElementById(ControlName);
		
		if (pFrameName.indexOf("/") == -1)
		{
			pFrameName = "/" + pFrameName;
		}
		
		if (Control != null)
		{
			var Images = Control.getElementsByTagName("IMG");
			if (Images != null)
			{
				for (var iImage = 0; iImage < Images.length; iImage++)
				{
					var ImageSource = Images[iImage].src;
					
					if (ImageSource != null)
					{
						if (this.CleanImageName(ImageSource.toUpperCase()).indexOf(this.CleanImageName(pFrameName.toUpperCase())) > 0)
						{
							if (Images[iImage].fireEvent) 
							{ 
								Images[iImage].fireEvent("onclick");
							} 
							else if(document.createEvent) 
							{ 
								var EventTrigger = document.createEvent("HTMLEvents");
								EventTrigger.initEvent("click", true, true);
								Images[iImage].dispatchEvent(EventTrigger);
							} 
						}
					}
				}
			}
		}
	}
	
	this.CleanImageName = function(pImageName)
	{
		while (pImageName.indexOf("%20") >= 0)
		{
			pImageName = pImageName.replace("%20", " ");
		}
		
		return pImageName;
	}
	
	this.GetImage = function()
	{
		return this.FileName;
	}
}

function __WallArtControlUI()
{
	this.CharacterMap = "";
	
	this.SetPrimaryFocus = function()
	{
		var PrimaryControl = document.getElementById(WallArtGUI.PrimaryTextControlName);
		
		if (PrimaryControl != null)
		{
			try
			{
				PrimaryControl.focus();
			}
			catch (err)
			{
			}
		}
	}
	
	this.TextBoxKeyStroke = function(pControl, event, pPrimary)
	{
		if (WallArtCore.IsReturnPress(event))
		{
			if (pPrimary)
			{
				WallArtGUI.UpdatePrimaryText();
			}
			else
			{
				WallArtGUI.UpdateText();
			}	
		}
		else
		{
			var ControlValue = "";
			
			for (var iLet = 0; iLet < pControl.value.length; iLet++)
			{
				if (iLet < WallArtGUI.Preview.Phrase.LetterLimit)
				{
					var TestChar = pControl.value.substring(iLet, iLet + 1).toUpperCase()
					if (this.CharacterMap.indexOf(TestChar) >= 0)
					{
						ControlValue += TestChar;
					}
				}
			}
			
			pControl.value = ControlValue;
		}
		
		return false;
	}
	
	this.TrapRightClick = function()
	{
		alert(Message_DisabledRightClick);
		return false;
	}
	
	this.TrapLeftClick = function(pEvent)
	{
		var LeftButton = (pEvent == null) ? true : (pEvent.which == 1);
		
		if (LeftButton)
		{
			pEvent = pEvent ? pEvent : window.event;
			var Source = pEvent.target ? pEvent.target : pEvent.srcElement;
			
			if (Source.id != WallArtCore.HtmlElements.GetElementId("cboMats_TopImage", "IMG") && Source.id != WallArtCore.HtmlElements.GetElementId("cboMats_DropImage", "IMG"))
			{
				var MatsControl = document.getElementById(WallArtCore.HtmlElements.GetElementId("cboMats_Container", "DIV"));
				
				if (MatsControl != null)
				{
					MatsControl.style.height = "38px";
				}
			}
		}

		return true;
	}
}

var WallArtTabberInterval = 10;
var WallArtHeightIncrease = 20;

function WallArtTabSelector()
{
	this.CanSelect = false;
	this.CanAddToCart = false;
	this.UsePreviewUI = true;

	this.Hover = function(pCell)
	{
		if (this.Selectable(pCell.id))
		{
			pCell.className += " Highlight";
		}
	}
	
	this.HoverOut = function(pCell)
	{
		pCell.className = pCell.className.replace(" Highlight", "");
	}
	
	this.Select = function(pCellName, pShowConstants)
	{
		if (this.Selectable(pCellName))
		{
			this.SetDivisionAndTab(pCellName, pShowConstants);
			this.ShowDivision(pCellName.replace("tbc", "div"), false);
		}
	}
	
	this.Open = function(pCellName, pShowConstants, pScriptOnOpened)
	{
		if (this.Selectable(pCellName))
		{
			this.SetDivisionAndTab(pCellName, pShowConstants);
			this.ShowDivision(pCellName.replace("tbc", "div"), true, pScriptOnOpened);
		}
	}
	
	this.Selectable = function(pCellName)
	{
		var IsSelectable = false;
		var Cell = document.getElementById(pCellName);
		
		if (Cell != null)
		{
			if (Cell.className.indexOf(" Selected") < 0)
			{
				if (pCellName == "tbcFinish" && this.CanAddToCart)
				{
					IsSelectable = true;
				}
				else if (pCellName != "tbcFinish" && this.CanSelect)
				{
					IsSelectable = true;
				}
			}
		}
		
		return IsSelectable;
	}
	
	this.ShowDivision = function(pDivName, pSlide, pScriptOnOpened)
	{
		pSlide = (pSlide == null) ? false : pSlide;
		pScriptOnOpened = (pScriptOnOpened == null) ? "" : pScriptOnOpened;
		
		var thisElement = document.getElementById(pDivName);
		if (thisElement != null)
		{
			if (thisElement.style.display != "block")
			{
				thisElement.style.display = "block";
				
				if (pSlide)
				{
					var Dimensions = WallArtCore.HtmlElements.GetObjectDimensions(thisElement);
					var ParentDimensions = WallArtCore.HtmlElements.GetObjectDimensions(thisElement.parentNode);

					Dimensions.Height -= 14;
					
					var sTimeOut = "WallArtGUI.TabSelector.ElementHeightTimeout('" + pDivName + "','" + Dimensions.Height + "', '" + pScriptOnOpened + "')";

					this.SetElementHeight(thisElement, WallArtHeightIncrease);
					this.SetElementPosition(thisElement, ((ParentDimensions.Top + ParentDimensions.Height) - (WallArtHeightIncrease + 15)));
					setTimeout(sTimeOut, WallArtTabberInterval);
				}
			}
		}
	}
	
	this.ElementHeightTimeout = function(pDivName, pMaxHeight, pScriptOnOpened)
	{
		var thisElement = document.getElementById(pDivName);
		
		if (thisElement != null)
		{
			var CurrentHeight = parseInt(thisElement.style.height) + WallArtHeightIncrease;
			var CurrentTop = parseInt(thisElement.style.top);
			
			this.SetElementHeight(thisElement, CurrentHeight + "px");
			this.SetElementPosition(thisElement, (CurrentTop - WallArtHeightIncrease) + "px");
			
			if ((CurrentHeight + WallArtHeightIncrease) <= parseInt(pMaxHeight))
			{
				var sTimeOut = "WallArtGUI.TabSelector.ElementHeightTimeout('" + pDivName + "', '" + pMaxHeight + "', '" + pScriptOnOpened + "')";
				setTimeout(sTimeOut, WallArtTabberInterval);
			}
			else if (CurrentHeight < parseInt(pMaxHeight))
			{ 
				this.SetElementHeight(thisElement, pMaxHeight);
				this.SetElementPosition(thisElement, parseInt(thisElement.style.top) - (pMaxHeight - CurrentHeight));
				if (this.UsePreviewUI)
				{
					WallArtGUI.ResetPreviewUI();
				}
				
				if (pScriptOnOpened != "")
				{
					setTimeout(pScriptOnOpened, 10);
				}
			}
		}
	}
	
	this.SetElementHeight = function(pElement, pHeight)
	{
		pHeight = parseInt(pHeight) + "px";
		
		pElement.style.height = pHeight;
		pElement.style.minHeight = pHeight;
	}
	
	this.SetElementPosition = function(pElement, pTop)
	{
		pTop = parseInt(pTop) + "px";
		
		pElement.style.top = pTop;
	}
	
	this.SetDivisionAndTab = function(pCellName, pShowConstants)
	{
		ResetMenu();
		HideAll();
		HighlightTab(pCellName);
		ShowConstants(((pShowConstants == null) ? true : pShowConstants));
		
		function HideAll()
		{
			var AllInterfaces = document.getElementById("trGUInterfaces");
			if (AllInterfaces != null)
			{
				var Interfaces = AllInterfaces.getElementsByTagName("DIV");
				if (Interfaces != null)
				{
					for (iInterface = 0; iInterface < Interfaces.length; iInterface++)
					{
						if (IsValidInterface(Interfaces[iInterface], pCellName.replace("tbc", "div")))
						{
							Interfaces[iInterface].style.display = "none";
						}
					}
				}
			}
		}
		
		function IsValidInterface(pInterface, pIgnore)
		{
			var IsValid = false;
		
			if (pInterface.id != pIgnore)
			{
				if (pInterface.className.indexOf("GUInterface") == 0)
				{
					if (pInterface.className.indexOf("View") > 0 || pInterface.className.indexOf("Hidden") > 0)
					{
						IsValid = true;
					}
				}
			}
			
			return IsValid;
		}
		
		function HighlightTab(pTabName)
		{
			var thisElement = document.getElementById(pTabName);
			if (thisElement != null)
			{
				thisElement.className += " Selected";
			}
		}
		
		function ResetMenu()
		{
			var TabMenu = document.getElementById("TableGUIMenu");
			if (TabMenu != null)
			{
				var AllTabs = TabMenu.getElementsByTagName("TD");
				if (AllTabs != null)
				{
					for (var iTab = 0; iTab < AllTabs.length; iTab++)
					{
						if (AllTabs[iTab].className.indexOf(" Selected") > 0)
						{
							AllTabs[iTab].className = AllTabs[iTab].className.replace(" Selected", "");
						}
					}
				}
			}
		}
		
		function ShowConstants(pShowConstantDiv)
		{
			if (pShowConstantDiv)
			{
				Constant = document.getElementById("divConstants");
				if (Constant != null)
				{
					Constant.style.display = "block";
				}
			}
		}
	}
	
	this.RemovePrimary = function(pHideElements, pShowElements)
	{
		SetElementalDisplay(pHideElements, "none");
		SetElementalDisplay(pShowElements, "block");
		
		function SetElementalDisplay(pElements, pDisplay)
		{
			pElements = (pElements == null) ? "" : pElements;
			if (pElements != "")
			{
				var docElements = pElements.split(",");
				for (var iElement = 0; iElement < docElements.length; iElement++)
				{
					var Element = document.getElementById(docElements[iElement]);
					if (Element != null)
					{
						Element.style.display = pDisplay;
					}
				}
			}
		}
	}
}

document.oncontextmenu = function() 
{
	WallArtControlUI.TrapRightClick();
	return false;
}

document.onclick = WallArtControlUI.TrapLeftClick;