﻿//// Create namespaces
if (!Creative)
	var Creative = { };
	
if (!Creative.Waad)
	Creative.Waad = { };


Creative.Waad.Emc = {
	EmcControls : {
		RegisterNew : function(javaScriptId, clientId) {
			var tControl = new Creative.Waad.Emc.Prototypes.EmcControl(javaScriptId, clientId);
			Creative.Waad.Emc.EmcControls[javaScriptId] = tControl;
			window[javaScriptId] = tControl;
			return tControl;
		}
	},
	Prototypes : {
		EmcControl : function(javaScriptId, clientId) {
			this.JavaScriptId = javaScriptId;
			this.ClientId = clientId;
			this.Header = new Creative.Waad.Emc.Prototypes.EmcHeader(javaScriptId);
			this.Footer = new Creative.Waad.Emc.Prototypes.EmcFooter(javaScriptId);
			this.Content = new Creative.Waad.Emc.Prototypes.EmcContent(javaScriptId, clientId);
		},
		EmcHeader : function(parentJavaScriptId) {
			this.ParentJavaScriptId = parentJavaScriptId;
			this.Show = function() {
				var tHeader = document.getElementById(this.ParentJavaScriptId + '_header');
				if (tHeader)
					tHeader.style.display = 'block';
			}
			this.Hide = function() {
				var tHeader = document.getElementById(this.ParentJavaScriptId + '_header');
				if (tHeader)
					tHeader.style.display = 'none';
			}
		},
		EmcFooter : function(parentJavaScriptId) {
			this.ParentJavaScriptId = parentJavaScriptId;
			this.Show = function() {
				var tHeader = document.getElementById(this.ParentJavaScriptId + '_footer');
				if (tHeader)
					tHeader.style.display = 'block';
			}
			this.Hide = function() {
				var tHeader = document.getElementById(this.ParentJavaScriptId + '_footer');
				if (tHeader)
					tHeader.style.display = 'none';
			}
			this.AddButton = function(javaScriptId, elementId) {
				var tButton = new Creative.Waad.Emc.Prototypes.EmcFooterButton(this.ParentJavaScriptId, javaScriptId, elementId);
				this[javaScriptId] = tButton;
				return tButton;
			}
		},
		EmcFooterButton : function(parentJavaScriptId, javaScriptId, elementId) {
			this.ParentJavaScriptId = parentJavaScriptId;
			this.JavaScriptId = javaScriptId;
			this.ElementId = elementId;
			
			this.Show = function() {
				var tButton = document.getElementById(this.ElementId);
				if (tButton)
					tButton.style.display = 'inline';
			}
			this.Hide = function() {
				var tButton = document.getElementById(this.ElementId);
				if (tButton)
					tButton.style.display = 'none';
			}
			this.Click = function() {
				var tElement = document.getElementById(this.ElementId);
				if (tElement && tElement.href) {
					if (tElement.onclick && typeof(tElement.onclick) == "function")
						tElement.onclick();
						
					if (tElement.href.substr(0, 11).toLowerCase() == "javascript:") {
						// Linkbutton
						var tCode = tElement.href.substr(11, tElement.href.length);
						eval(tCode);
					} else {
						// Hyperlink
						if (tElement.target) {
							window.open(tElement.href, tElement.target);
						} else {
							window.location = tElement.href;
						}
					}
				}
			}
		},
		EmcContent : function(parentJavaScriptId, clientId) {
			this.ParentJavaScriptId = parentJavaScriptId;
			this.ClientId = clientId;
			this.Show = function() {
				var tHeader = document.getElementById(this.ParentJavaScriptId + '_content');
				if (tHeader)
					tHeader.style.display = 'block';
			}
			this.Hide = function() {
				var tHeader = document.getElementById(this.ParentJavaScriptId + '_content');
				if (tHeader)
					tHeader.style.display = 'none';
			}
			this.AddTextComponent = function(javaScriptId, spanId) {
				var tComponent = new Creative.Waad.Emc.Prototypes.EmcTextComponent(this.ClientId, this.ParentJavaScriptId, javaScriptId, spanId);
				this[javaScriptId] = tComponent;
				return tComponent;
			}
			this.AddFilterComponent = function(javaScriptId, baseUrl, filterQuery) {
				var tFilter = new Creative.Waad.Emc.Prototypes.EmcFilterComponent(this.ClientId, this.ParentJavaScriptId, javaScriptId, baseUrl, filterQuery);
				this[javaScriptId] = tFilter;
				return tFilter;
			}
			this.AddDataComponent = function(javaScriptId) {
				var tData = new Creative.Waad.Emc.Prototypes.EmcDataComponent(this.ClientId, this.ParentJavaScriptId, javaScriptId);
				this[javaScriptId] = tData;
				return tData;
			}
			this.AddListComponent = function(javaScriptId, currentPage, numberOfPages, urlWithoutPageNumber) {
				var tList = new Creative.Waad.Emc.Prototypes.EmcListComponent(this.ClientId, this.ParentJavaScriptId, javaScriptId, currentPage, numberOfPages, urlWithoutPageNumber, true, null);
				this[javaScriptId] = tList;
				return tList;
			}
			this.AddGenericComponent = function(javaScriptId, object) {
				if (!object)
					return;
				
				object.ClientId = this.ClientId;
				object.ParentJavaScriptId = this.ParentJavaScriptId;
				object.JavaScriptId = javaScriptId;
					
				object.AjaxUpdate = function() {
					if (__doPostBack && typeof(__doPostBack) == 'function') {
						__doPostBack(this.ClientId + '_' + this.ParentJavaScriptId + '_' + this.JavaScriptId + '_udp', 'emc_event');
					}
				}
				object.Show = function() {
					var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
					if (tControl)
						tControl.style.display = 'block';
				}
				object.Hide = function() {
					var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
					if (tControl)
						tControl.style.display = 'none';
				}
				object.ShowSeparator = function() {
					var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
					if (tControl)
						tControl.style.display = 'block';
				}
				object.HideSeparator = function() {
					var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
					if (tControl)
						tControl.style.display = 'none';
				}

				this[javaScriptId] = object;
				return object;
			}
			this.ShowComponent = function(componentJavaScriptId) {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + componentJavaScriptId);
				if (tControl)
					tControl.style.display = 'block';
			}
			this.HideComponent = function(componentJavaScriptId) {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + componentJavaScriptId);
				if (tControl)
					tControl.style.display = 'none';
			}
			this.ShowComponentSeparator = function(componentJavaScriptId) {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + componentJavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'block';
			}
			this.HideComponentSeparator = function(componentJavaScriptId) {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + componentJavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'none';
			}
		},
		EmcTextComponent : function(clientId, parentJavaScriptId, javaScriptId, spanId) {
			this.ClientId = clientId;
			this.ParentJavaScriptId = parentJavaScriptId;
			this.JavaScriptId = javaScriptId;
			this.SpanId = spanId;
			
			this.AjaxUpdate = function() {
				if (__doPostBack && typeof(__doPostBack) == 'function') {
					__doPostBack(this.ClientId + '_' + this.ParentJavaScriptId + '_' + this.JavaScriptId + '_udp', 'emc_event');
				}
			}
			this.SetText = function(text) {
				var tControl = document.getElementById(this.SpanId);
				if (tControl)
					tControl.innerHTML = text;
			}
			this.GetText = function() {
				var tControl = document.getElementById(this.SpanId);
				if (tControl && tControl.innerHTML)
					return tControl.innerHTML;
				return null;
			}
			this.Show = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'block';
			}
			this.Hide = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'none';
			}
			this.ShowSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'block';
			}
			this.HideSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'none';
			}
		},
		EmcFilterComponent : function(clientId, parentJavaScriptId, javaScriptId, baseUrl, filterQuery) {
			this.ClientId = clientId;
			this.ParentJavaScriptId = parentJavaScriptId;
			this.JavaScriptId = javaScriptId;
			this.BaseUrl = baseUrl;
			this.FilterQuery = filterQuery;
			this.FilterMethods = new Array();
			this.RegisterFilterMethod = function(filterId, func, param) {
				if (filterId && typeof(func) == "function") {
					this.FilterMethods.push({ FilterId : filterId, Function : func, Param : param });
				}
			}
			this.AjaxUpdate = function() {
				if (__doPostBack && typeof(__doPostBack) == 'function') {
					__doPostBack(this.ClientId + '_' + this.ParentJavaScriptId + '_' + this.JavaScriptId + '_udp', 'emc_event');
				}
			}
			this.Submit = function() {
				var tFullFilter = "";
				for (var i=0; i<this.FilterMethods.length; i++) {
					tFullFilter += this.FilterMethods[i].Function(this.FilterMethods[i].Param, this.ParentJavaScriptId, this.JavaScriptId, this.FilterMethods[i].FilterId);
				}
				if (tFullFilter)
					document.location = this.BaseUrl + this.FilterQuery + '=' + tFullFilter;
				else
					document.location = this.BaseUrl;
			}
			this.Show = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'block';
			}
			this.Hide = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'none';
			}
			this.ShowSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'block';
			}
			this.HideSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'none';
			}
		},
		EmcDataComponent : function(clientId, parentJavaScriptId, javaScriptId) {
			this.ClientId = clientId;
			this.ParentJavaScriptId = parentJavaScriptId;
			this.JavaScriptId = javaScriptId;
			this.Groups = { };
			
			this.AjaxUpdate = function() {
				if (__doPostBack && typeof(__doPostBack) == 'function') {
					__doPostBack(this.ClientId + '_' + this.ParentJavaScriptId + '_' + this.JavaScriptId + '_udp', 'emc_event');
				}
			}
			this.AddGroup = function(groupId) {
				var tGroup = new Creative.Waad.Emc.Prototypes.EmcDataComponentGroup(this.ParentJavaScriptId, this.JavaScriptId, groupId);
				this.Groups[groupId] = tGroup;
			}
			this.Show = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'block';
			}
			this.Hide = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'none';
			}
			this.ShowSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'block';
			}
			this.HideSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'none';
			}
		},
		EmcDataComponentGroup : function(emcControlJavaScriptId, dataComponentJavaScriptId, groupId) {
			this.EmcControlJavaScriptId = emcControlJavaScriptId;
			this.DataComponentJavaScriptId = dataComponentJavaScriptId;
			this.GroupId = groupId;
			this.Items = { };
			
			this.Toggle = function() {
				// Find the link and the content
				var tLink = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_link');
				if (!tLink)
					return;
				var tContent = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_content');
				if (!tContent)
					return;

				// Expand if it is currently collapsed and vice versa
				if (Creative.Waad.Css.ElementHasClass(tLink, 'emc-collapse')) {
					// The group is shown, we should collapse it
					tContent.style.display = 'none';
					// Also change the css class of the link
					Creative.Waad.Css.RemoveClassFromElement(tLink, 'emc-collapse');
					Creative.Waad.Css.AddClassToElement(tLink, 'emc-expand');
				} else {
					// The group is collapsed, we should show it
					tContent.style.display = 'block';
					// Also change the css class of the link
					Creative.Waad.Css.RemoveClassFromElement(tLink, 'emc-expand');
					Creative.Waad.Css.AddClassToElement(tLink, 'emc-collapse');
				}
			}
			this.Collapse = function() {
				// Find the link and the content
				var tLink = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_link');
				if (!tLink)
					return;
				var tContent = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_content');
				if (!tContent)
					return;
					
				// Collapse the group
				if (Creative.Waad.Css.ElementHasClass(tLink, 'emc-collapse')) {
					// The group is shown, we should collapse it
					tContent.style.display = 'none';
					// Also change the css class of the link
					Creative.Waad.Css.RemoveClassFromElement(tLink, 'emc-collapse');
					Creative.Waad.Css.AddClassToElement(tLink, 'emc-expand');
				}
			}
			this.Expand = function() {
				// Find the link and the content
				var tLink = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_link');
				if (!tLink)
					return;
				var tContent = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_content');
				if (!tContent)
					return;
					
				// Expand the group
				if (Creative.Waad.Css.ElementHasClass(tLink, 'emc-expand')) {
					// The group is collapsed, we should show it
					tContent.style.display = 'block';
					// Also change the css class of the link
					Creative.Waad.Css.RemoveClassFromElement(tLink, 'emc-expand');
					Creative.Waad.Css.AddClassToElement(tLink, 'emc-collapse');
				}
			}
			this.AddItem = function(itemId, getValueFunction, setValueFunction) {
				var tNewItem = new Creative.Waad.Emc.Prototypes.EmcDataComponentGroupItem(this.EmcControlJavaScriptId, this.DataComponentJavaScriptId, this.GroupId, itemId, getValueFunction, setValueFunction);
				this.Items[itemId] = tNewItem;
			}
		},
		EmcDataComponentGroupItem : function(emcControlJavaScriptId, dataComponentJavaScriptId, groupId, itemId, getValueFunction, setValueFunction) {
			this.EmcControlJavaScriptId = emcControlJavaScriptId;
			this.DataComponentJavaScriptId = dataComponentJavaScriptId;
			this.GroupId = groupId;
			this.GroupItemId = itemId;
			this.GetValueFunction = getValueFunction;
			this.SetValueFunction = setValueFunction;
			this.GetValue = function() {
				if (this.GetValueFunction && typeof(this.GetValueFunction) == "function")
					return this.GetValueFunction();
			}
			this.SetValue = function(value) {
				if (this.SetValueFunction && typeof(this.SetValueFunction) == "function")
					this.SetValueFunction(value);
			}
			this.Show = function() {
				var tItem = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_item_' + this.GroupItemId);
				if (tItem)
					tItem.style.display = 'block';
			}
			this.Hide = function() {
				var tItem = document.getElementById(this.EmcControlJavaScriptId + '_content_' + this.DataComponentJavaScriptId + '_group_' + this.GroupId + '_item_' + this.GroupItemId);
				if (tItem)
					tItem.style.display = 'none';
			}
		},
		EmcListComponent : function(clientId, parentJavaScriptId, javaScriptId, currentPage, numberOfPages, urlWithoutPageNumber, isSingleSelect, defaultValue) {
			this.ClientId = clientId;
			this.ParentJavaScriptId = parentJavaScriptId;
			this.JavaScriptId = javaScriptId;
			this.CurrentPage = currentPage;
			this.NumberOfPages = numberOfPages;
			this.IsSingleSelect = isSingleSelect;
			this.CurrentValue = defaultValue;
			this.Items = new Array();
			
			this.privateUrlWithoutPageNumber = urlWithoutPageNumber;
			
			this.AjaxUpdate = function() {
				if (__doPostBack && typeof(__doPostBack) == 'function') {
					__doPostBack(this.ClientId + '_' + this.ParentJavaScriptId + '_' + this.JavaScriptId + '_udp', 'emc_event');
				}
			}
			this.AddItems = function(items) {
				if (!items.length) return;
				for (var i=0; i < items.length; i++) {
					if (items[i].Value && items[i].SelectControlId) {
						var tItem = new Creative.Waad.Emc.Prototypes.EmcListComponentItem(items[i].Value, items[i].SelectControlId);
						this.Items.push(tItem);
					}
				}
			}
			this.RaiseChange = function(value, selected) {
				/*
				 * In Single Select mode, the selected value is automatically deselected when a new value is selected.
				 *	Make sure the Selected CSS class is removed from the previous value in these cases.
				 */
				if (this.IsSingleSelect) {
					// At this point, CurrentValue still contains the previous value
					if (this.CurrentValue) {
						// Get the previously selected value
						for (var i=0; i<this.Items.length; i++) {
							var tItem = this.Items[i];
							if (tItem && tItem.Value == this.CurrentValue)
								tItem.SetRowDeselected();
						}
					}
					// Store the Currently Selected value
					for (var i=0; i<this.Items.length; i++) {
						var tItem = this.Items[i];
						if (tItem && tItem.IsSelected()) {
							tItem.SetRowSelected();
							this.CurrentValue = tItem.Value;
							break;
						}
					}
				}
				
				if (typeof(this.onchange) == "function") {
					if (!value) {
						// We didn't get a value passed, find it via click event
						var tTarget = Creative.Waad.Events.GetEventTarget();
						if (tTarget && tTarget.value) {
							value = tTarget.value;
							selected = tTarget.checked;
						}
					}
					this.onchange({type : "change", Value : value, Selected : selected});
				}
			}
			this.HasSelection = function() {
				for (var i=0; i<this.Items.length; i++) {
					var tItem = this.Items[i];
					if (tItem && tItem.IsSelected())
						return true;
				}
				return false;
			}
			this.GetSelectedValue = function() {
				for (var i=0; i<this.Items.length; i++) {
					var tItem = this.Items[i];
					if (tItem && tItem.IsSelected())
						return tItem.Value;
				}
				return null;
			}
			this.ToggleValue = function(value) {
				for (var i=0; i<this.Items.length; i++) {
					var tItem = this.Items[i];
					if (tItem.Value == value) {
						tItem.Toggle();
						this.RaiseChange(value, tItem.IsSelected());
						return;
					}
				}
			}
			this.GetSelectedValues = function() {
				var tResult = new Array();
				for (var i=0; i<this.Items.length; i++) {
					var tItem = this.Items[i];
					if (tItem && tItem.IsSelected())
						tResult.push(tItem.Value);
				}
				return tResult;
			}
			this.SelectValue = function(value) {
				for (var i=0; i<this.Items.length; i++) {
					var tItem = this.Items[i];
					if (tItem.Value == value) {
						if (tItem.Select())
							this.RaiseChange(value, true);
						return;
					}
				}
			}
			this.DeselectValue = function(value) {
				for (var i=0; i<this.Items.length; i++) {
					var tItem = this.Items[i];
					if (tItem.Value == value) {
						if (tItem.Deselect())
							this.RaiseChange(value, false);
						return;
					}
				}
			}
			this.GotoPage = function(pageNumber) {
				if (typeof(pageNumber) == "number" && typeof(this.NumberOfPages) == "number" &&  pageNumber <= this.NumberOfPages && pageNumber > 0) {
					document.location = this.privateUrlWithoutPageNumber + pageNumber;
				}
			}
			this.Show = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'block';
			}
			this.Hide = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId);
				if (tControl)
					tControl.style.display = 'none';
			}
			this.ShowSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'block';
			}
			this.HideSeparator = function() {
				var tControl = document.getElementById(this.ParentJavaScriptId + '_content_' + this.JavaScriptId + '_separator');
				if (tControl)
					tControl.style.display = 'none';
			}
		},
		EmcListComponentItem : function(value, selectControlId) {
			this.Value = value;
			this.SelectControlId = selectControlId;
			
			this.IsSelected = function() {
				var tControl = document.getElementById(this.SelectControlId);
				if (tControl && tControl.checked)
					return true;
				return false;
			}
			this.SetRowSelected = function() {
				var tRow = document.getElementById(this.SelectControlId + '_row');
				if (tRow && !Creative.Waad.Css.ElementHasClass(tRow, 'emc-selected'))
					Creative.Waad.Css.AddClassToElement(tRow, 'emc-selected');
			}
			this.SetRowDeselected = function() {
				var tRow = document.getElementById(this.SelectControlId + '_row');
				if (tRow && Creative.Waad.Css.ElementHasClass(tRow, 'emc-selected'))
					Creative.Waad.Css.RemoveClassFromElement(tRow, 'emc-selected');
			}
			this.Select = function() {
				this.privateIsSelected = true;
				var tControl = document.getElementById(this.SelectControlId);
				if (tControl && !tControl.checked) {
					tControl.checked = true;
					this.SetRowSelected();
					return true;
				}
				return false;
			}
			this.Deselect = function() {
				this.privateIsSelected = false;
				var tControl = document.getElementById(this.SelectControlId);
				if (tControl && tControl.checked) {
					tControl.checked = false;
					this.SetRowDeselected();
					return true;
				}
				return false;
			}
			this.Toggle = function() {
				var tFormElementSelected = this.IsSelected();
				if (this.privateIsSelected != tFormElementSelected) {
					this.privateIsSelected = tFormElementSelected;
					if (tFormElementSelected)
						this.SetRowSelected();
					else
						this.SetRowDeselected();
				} else if (tFormElementSelected)
					this.Deselect();
				else
					this.Select();
			}
			// Set at the end so that the IsSelected method is created.
			this.privateIsSelected = this.IsSelected();
		}
	}	
};
