﻿//// Create namespaces
if (!Creative)
	var Creative = { };
	
if (!Creative.Waad)
	Creative.Waad = { };


Creative.Waad.Popups = {
	RegisterPopup : function(popupId, backgroundId, contentId, hiddenFieldId, updatePanelId, parameterHiddenFieldId, linkButtonId, closeButtons) {
		var tPopup = new Creative.Waad.Popups.prototypes.Popup(backgroundId, contentId, hiddenFieldId, updatePanelId, parameterHiddenFieldId);
		Creative.Waad.Popups[popupId] = tPopup;
		if (linkButtonId) {
			var tLinkButton = document.getElementById(linkButtonId);
			if (tLinkButton && !tLinkButton.hasWaadPopupEventLinked) {
				tLinkButton.hasWaadPopupEventLinked = true;
				tLinkButton.href = 'javascript:void(0);';
				Creative.Waad.Events.AddEvent(tLinkButton, 'click', function() { Creative.Waad.Popups[popupId].Show(); });
			}
			if (closeButtons && closeButtons.length) {
				// Should be an array
				for (var i=0; i<closeButtons.length; i++) {
					var tCloseButton = document.getElementById(closeButtons[i]);
					if (tCloseButton && !tCloseButton.hasWaadPopupCloseEventLinked) {
						tCloseButton.hasWaadPopupCloseEventLinked = true;
						tCloseButton.href = 'javascript:void(0);';
						Creative.Waad.Events.AddEvent(tCloseButton, 'click', function() { Creative.Waad.Popups[popupId].Hide(); });
					}
				}
			}
		}
	},
	prototypes : {
		Popup : function(backgroundId, contentId, hiddenFieldId, updatePanelId, parameterHiddenFieldId) {
			this.BackgroundId = backgroundId;
			this.ContentId = contentId;
			this.HiddenFieldId = hiddenFieldId;
			this.UpdatePanelId = updatePanelId;
			this.ParameterHiddenFieldId = parameterHiddenFieldId;
			
			this.Show = function(parameter) {
				var tBg = document.getElementById(this.BackgroundId);
				var tContent = document.getElementById(this.ContentId);
				var tHidden = document.getElementById(this.HiddenFieldId);
				var tParameter = document.getElementById(this.ParameterHiddenFieldId);
				
				if (tBg && tContent && tHidden) {
					tBg.style.display = 'block';
					tContent.style.display = 'block';
					tHidden.value = 'shown';
					if (tParameter) {
						if (parameter)
							tParameter.value = parameter;
						else
							tParameter.value = '';
					}
					if (this.UpdatePanelId && __doPostBack && typeof(__doPostBack) == 'function')
						__doPostBack(this.UpdatePanelId, 'show');
				}
			}
			this.Hide = function() {
				var tBg = document.getElementById(this.BackgroundId);
				var tContent = document.getElementById(this.ContentId);
				var tHidden = document.getElementById(this.HiddenFieldId);
				
				if (tBg && tContent && tHidden) {
					tBg.style.display = 'none';
					tContent.style.display = 'none';
					tHidden.value = '';
					if (this.UpdatePanelId && __doPostBack && typeof(__doPostBack) == 'function')
						__doPostBack(this.UpdatePanelId, 'hide');
				}
			}
		}
	}
}

Creative.Waad.Forms = {
	/**
	 * Sets a default text for a textbox. 
	 *	(If the default text is present when the control gets focus the text cleared. If the control has no value when it loses focus, the text is set again)
	 *
	 * @param controlId		the id of the textbox control  
	 * @param text			the default text for the given textbox
	 *						(e.g. 'Voer een emailadres in')
	 **/
	SetDefaultText : function(controlId, text) {
		var tControl = document.getElementById(controlId);
		if (tControl) {
			tControl.value = text;
			Creative.Waad.Events.AddEvent(tControl, 'blur', function() {
				if (this.value == '')
					this.value = text;
			});
			Creative.Waad.Events.AddEvent(tControl, 'focus', function() {
				if (this.value == text)
					this.value = '';
			});
		}
	}
};
		
		
Creative.Waad.Css = {
	/**
	 * Returns an array containing references to all elements of a given tag type 
	 *	within a certain node which have a given class
	 *
	 * @param node		the node to start from 
	 *					(e.g. document, 
	 *						  getElementById('whateverStartpointYouWant')
	 *					)
	 * @param searchClass the class we're wanting
	 *					(e.g. 'some_class')
	 * @param tag		 the tag that the found elements are allowed to be
	 *					(e.g. '*', 'div', 'li')
	 **/
	GetElementsByClass : function(node, searchClass, tag) {
		var classElements = new Array();
		var els = node.getElementsByTagName(tag);
		var elsLen = els.length;
		//var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
		
		
		for (var i = 0, j = 0; i < elsLen; i++) {
			if (Creative.Waad.Css.ElementHasClass(els[i], searchClass) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	},

	/**
	 * Returns the first parent of the given node that has a particular class
	 *
	 * @param control	the node to start from 
	 *					(e.g. document, 
	 *						  getElementById('whateverStartpointYouWant')
	 *					)
	 * @param searchClass the class we're wanting
	 *					(e.g. 'some_class')
	 **/
	FindParentWithClass : function(control, searchClass) {
		// Check parameters
		if (!control)
			return null;
		if (!searchClass)
			return null;
			
		// Find and check parent
		var tParent = control.parentNode;
		if (!tParent)
			return null;
		if (Creative.Waad.Css.ElementHasClass(tParent, searchClass))
			return tParent;
		
		// Recursive call to find it's parent
		return this.FindParentWithClass(tParent, searchClass);
	},
	
	/**
	 * Returns the first child of the given node that has a particular class
	 *
	 * @param control	the node to start from 
	 *					(e.g. document, 
	 *						  getElementById('whateverStartpointYouWant')
	 *					)
	 * @param searchClass the class we're wanting
	 *					(e.g. 'some_class')
	 **/
	FindFirstChildWithClass : function(control, searchClass) {
		var tList = Creative.Waad.Css.GetElementsByClass(control, searchClass, '*');
		if (tList && tList.length > 0)
			return tList[0];
		return null;
	},
	
	/**
	 * Returns true if the given element has been assigned the given class.
	 **/
	ElementHasClass: function(el, classString) {
		if (!el) {
			return false;
		}
		
		var regex = new RegExp('\\b'+classString+'\\b');
		if (el.className.match(regex)) {
			return true;
		}

		return false;
	},

	/**
	 * Adds classString to the classes assigned to the element with id equal to
	 *	idString.
	 *
	 * @param idString		the id of the element to add the class to
	 * @param classString	the class to add to the element with the given id 
	 *						(e.g. 'some_class')
	 **/
	AddClassToId: function(idString, classString) {
		Creative.Waad.Css.AddClassToElement(document.getElementById(idString), classString);
	},

	/**
	 * Adds classString to the classes assigned to the given element.
	 * If the element already has the class which was to be added, then
	 * it is not added again.
	 *
	 * @param el			the element to add the class to 
	 * @param classString	the class to add to the given element
	 *						(e.g. 'some_class')
	 **/
	AddClassToElement: function(el, classString) {
		var classArray = Creative.Waad.Css.privateGetClassArray(el);

		if (Creative.Waad.Css.ElementHasClass(el, classString)) {
			return; // already has element so don't need to add it
		}

		classArray.push(classString);

		el.className = Creative.Waad.Css.privateCreateClassString(classArray);
	},

	/**
	 * Removes the given classString from the list of classes assigned to the
	 * element with id equal to idString. If the element has the same class assigned to it twice, 
	 * then only the first instance of that class is removed.
	 *
	 * @param idString		the id of the element to remove the class from
	 * @param classString	the class to remove from the element with the given id
	 *						(e.g. 'some_class')
	 **/
	RemoveClassFromId: function(idString, classString) {
		Creative.Waad.Css.RemoveClassFromElement(document.getElementById(idString), classString);
	},

	/**
	 * Removes the given classString from the list of classes assigned to the
	 * given element.  If the element has the same class assigned to it twice, 
	 * then only the first instance of that class is removed.
	 *
	 * @param el			the element to remove the class from
	 * @param classString	the class to remove from the given element
	 *						(e.g. 'some_class')
	 **/
	RemoveClassFromElement: function(el, classString) {
		var classArray = Creative.Waad.Css.privateGetClassArray(el);

		for (x in classArray) {
			if (classString == classArray[x]) {
				classArray[x] = '';
				break;
			}
		}

		el.className = Creative.Waad.Css.privateCreateClassString(classArray);
	},
	/**
	 * PRIVATE.  Returns an array containing all the classes applied to this
	 * element.
	 *
	 * Used internally by elementHasClass(), addClassToElement() and 
	 * removeClassFromElement().
	 **/
	privateGetClassArray: function(el) {
		return el.className.split(' '); 
	},

	/**
	 * PRIVATE.  Creates a string from an array of class names which can be used 
	 * by the className function.
	 *
	 * Used internally by addClassToElement().
	 **/
	privateCreateClassString: function(classArray) {
		return classArray.join(' ');
	}
};
	
Creative.Waad.Events = {
	/**
	 * Adds an event to an element. All previously assigned event handlers are persisted.
	 *
	 * @param element		the element to add the event handler to
	 * @param type			the event type to add the handler to
	 *						(e.g. 'click', 'blur')
	 * @param handler		the handler to add to the event
	 *						(e.g. function() { alert(''); }
	 *							 someFunction
	 *						)
	 **/
	AddEvent : function(element, type, handler) {
		if (typeof(handler) != 'function')
			return;
		// Accept the element as id aswel
		if (typeof(element) == 'string')
			element = document.getElementById(element);
		if (!element)
			return;
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = Creative.Waad.Events.Guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = Creative.Waad.Events.privateHandleEvent;
	},
	Guid : 1,
	
	/**
	 * Fires the event handlers that are associated with an element.
	 *
	 * @param element		the element for which to fire the event handlers
	 * @param type			the event type for which to fire the event handlers
	 *						(e.g. 'click', 'blur')
	 * @param args		the arguments that are passed to the event handlers
	 **/
	FireEvent : function(element, type, args) {
		if (!element || !element.events) return;
		var handlers = element.events[type];
		if (!handlers) return;
		// execute each event handler
		for (var i in handlers) {
			var tEventFunction = handlers[i];
			if (typeof(tEventFunction) == 'function')
				tEventFunction.call(null, args);
		}
	},
	
	/**
	 * Removes an event handler from an element.
	 *
	 * @param element		the element to remove the event handler from
	 * @param type			the event type to remove the handler from
	 *						(e.g. 'click', 'blur')
	 * @param handler		the handler to remove from the event
	 *						(e.g. 
	 *							 someFunction
	 *						)
	 **/
	RemoveEvent : function(element, type, handler) {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	},
	
	/**
	 * Gets the correct target for an event. 
	 *	(Some browsers place the target in the srcElement property of the event target)
	 *
	 * @param e			the event to find the source for.
	 *					(note: if ommitted, window.event is used)
	 **/
	GetEventTarget : function(e) {
		var targ;
		if (!e) {
			e = window.event;
		}
		if (!e)
			return null;
		if (e.target) {
			targ = e.target;
		} else if (e.srcElement) {
			targ = e.srcElement;
		}
		if (targ.nodeType == 3) { // defeat Safari bug
			targ = targ.parentNode;
		}

		return targ;
	},
	
	/**
	 * Calls a function if a key is pressed with a given keycode.
	 *
	 * @param e				the event that caused the keypress to be called
	 * @param keyCode		the code of the key that should trigger the function to be called
	 *						(e.g. 13 )
	 * @param func			the function that should be called when the correct key is entered
	 *						(e.g. function() { alert(''); }
	 *							 someFunction
	 *						)
	 * Note: The function is called with the e and keyCode arguments provided.
	 **/
	OnKeyPress : function(e, keyCode, func) {
		if (typeof(func) != "function")
			return;
		if (!e) e = window.event;
		if (!e) return;
			
		var key = (e.keyCode) ? e.keyCode : e.which;
		if (key == keyCode) {
			if(e.preventDefault) 
				e.preventDefault(); 
			e.returnValue = false;
			func(e, keyCode);
		}
	},

	/**
	 * Gets the mouse position of the mouse when an event occurred.
	 *
	 * @param e		the event that occurred
	 *
	 * Returns an object with two properties X and Y containing the coordinates or
	 *	returns null if the event could not be found.
	 **/
	GetEventMousePosition : function(e) {
		var posx = 0;
		var posy = 0;
		if (!e) e = window.event;
		if (!e) return null;
		
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		} else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
		}
		// posx and posy contain the mouse position relative to the document
		return { X : posx, Y : posy };
	},
	
	/**
	 * PRIVATE.  Calls all event handlers assigned to an element for a given event type.
	 *
	 * Used internally by AddEvent().
	 **/
	privateHandleEvent : function(event) {
		var returnValue = true;
		// grab the event object (IE uses a global event object)
		event = event || Creative.Waad.Events.privateFixEvent(window.event);
		// get a reference to the hash table of event handlers
		var handlers = this.events[event.type];
		// execute each event handler
		for (var i in handlers) {
			this.$$handleEvent = handlers[i];
			if (this.$$handleEvent(event) === false) {
				returnValue = false;
			}
		}
		return returnValue;
	},
	/**
	 * PRIVATE. Sets the correct preventDefault and stopPropagation methods on the event.
	 *
	 * Used internally by privateHandleEvent().
	 **/
	privateFixEvent : function(event) {
		// add W3C standard event methods
		event.preventDefault = Creative.Waad.Events.privatePreventDefault;
		event.stopPropagation = Creative.Waad.Events.privateStopPropagation;
		return event;
	},
	/**
	 * PRIVATE. Provides a correct implementation for the preventDefault method of an event.
	 *
	 * Used internally by privateFixEvent().
	 **/
	privatePreventDefault : function() {
		this.returnValue = false;
	},
	/**
	 * PRIVATE. Provides a correct implementation for the stopPropagation method of an event.
	 *
	 * Used internally by privateFixEvent().
	 **/
	privateStopPropagation : function() {
		this.cancelBubble = true;
	}
};

Creative.Waad.Utils = {
	ClickOnce : {
		/**
		 * Use this functionality to ensure a linkbutton is only clicked once.
		 *
		 * @param id		the id of the linkbutton that should only be allowed to be clicked once
		 **/
		InitializeLinkButton : function(id) {
			var sb = document.getElementById( id );
			if (sb && (sb.nodeName == 'a' || sb.nodeName == 'A')) {
				if (sb.onclick)
					sb.original_click = sb.onclick;
				var tScript = sb.href;
				if (tScript.length >= 11 && tScript.substr(0, 11).toLowerCase() == "javascript:")
					tScript = tScript.substr(11, tScript.length - 11);
				eval('sb.href_click = function() { ' + decodeURI(tScript.replace('\'', '\\\'')) + ' }');
				sb.onclick = Creative.Waad.Utils.ClickOnce.privateLinkButton_Click;
				sb.href = 'javascript:void(0);';
			}
		},
		/**
		 * PRIVATE. Handles the click of a linkbutton that is initialized to be a ClickOnce linkbutton.
		 *
		 * Used internally by InitializeLinkButton().
		 **/
		privateLinkButton_Click : function(e) {
			var src = typeof( event ) != "undefined" ? event.srcElement : e.target;
			if (!src || !src.href_click)
				return;
			src.disabled = true;
			if (src.original_click)
				src.original_click();
			src.href_click();
			src.disabled = typeof( Page_IsValid ) != "undefined" ? Page_IsValid : true;
		}
	}
};
		
		
Creative.Waad.Cookies = {
	/**
	 * Writes a cookie to the client.
	 *
	 * @param name		the name of the cookie to be created
	 *					(e.a. 'Naam')
	 *
	 * @param value		the value of the cookie to be created
	 *					(e.a. 'Janssen')
	 *
	 * @param days		the number of days before the cookie expires.
	 *					(note: if ommitted, the cookie will be a session cookie)
	 **/
	CreateCookie : function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	
	/**
	 * Reads a cookie
	 *
	 * @param name		the name of the cookie to read
	 *					(e.a. 'Naam')
	 *
	 * @returns			the value of the cookie with the given name
	 *					(e.a. 
	 *						'Janssen'
	 *						null (if not found)
	 *					)
	 **/
	ReadCookie : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	/**
	 * Deletes a cookie
	 *
	 * @param name		the name of the cookie to delete
	 *					(e.a. 'Naam')
	 **/
	EraseCookie : function(name) {
		Creative.Waad.Cookies.CreateCookie(name,"",-1);
	}
};
