// $Id: site.js,v 1.2 2009/08/21 14:40:36 cmanley Exp $
// Requires:
// CookieJar.js


function Clone(what) {
	for (i in what) {
		if (typeof(what[i]) == 'object') {
			if (what[i] === null) {
				this[i] = null;
			}
			else {
				this[i] = new Clone(what[i]);
			}
		}
		else {
			this[i] = what[i];
		}
	}
}


var Site = new (function() {

	this.DOM = new (function() {

		// Finds the nearest ancestor with the given tag name.
		this.getAncestorElementByTagName = function(node, name) {
			name = name.toUpperCase();
			var result = null;
			e = node.parentNode;
			do {
				if (e && (e.nodeName == name)) {
					result = e;
					break;
				}
				e = e.parentNode;
			} while (e);
			return result;
		}

	})();

	this.HTML = new (function() { // HTML elements can't be extended in all browsers. That's why prototyping isn't used.

		// Adds an option to a Select element.
		this.selectAddOption = function(sel,text,value,selected) {
			var o = document.createElement('option');
			o.text = text;
			o.value = value;
			o.selected = selected;
			//o = new Option(text, value, false, selected);
			try {
				sel.add(o, null); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				sel.add(o); // IE only
			}
		}

		// Returns the index of the first option having the given value.
		this.selectIndexOfValue = function(sel,val) {
			for (var i = 0; i < sel.options.length; i++) {
				if (sel.options[i].value == val) {
					return i;
				}
			}
			return -1;
		}

	})();

	this.Obj = new (function() {

		// Test if 2 objects have the same properties and values.
		this.areEqual = function(obj1, obj2) {
			if (!obj1 || !obj2) {
				return false;
			}
			if (navigator.appName == 'Microsoft Internet Explorer') {
				var fixer = function(o) { // Fix for MSIE <= 7 bug.
					try {
						o.hasOwnProperty; // throws exception sometimes in MSIE.
					}
					catch(e) {
						o = new Clone(o);
					}
					return o;
				};
				obj1 = fixer(obj1);
				obj2 = fixer(obj2);
			}
			for (var i in obj1) {
				if (obj1.hasOwnProperty(i)) {
					if (!obj2.hasOwnProperty(i)) return false;
					if (obj1[i] != obj2[i]) return false;
				}
			}
			for (var i in obj2) {
				if (obj2.hasOwnProperty(i)) {
					if (!obj1.hasOwnProperty(i)) return false;
					if (obj1[i] != obj2[i]) return false;
				}
			}
			return true;
		}

	})();


	// Tests for support of the window.showModalDialog() method displays a warning alert if necessary.
	this.showModalDialogTest = function() {
		if (!window.showModalDialog || (navigator.userAgent.indexOf(' Chrome/2.0') >= 0)) { // Chrome 2.0's support of window.showModalDialog is broken: http://code.google.com/p/chromium/issues/detail?id=4202
			alert("Probleem:\n\nUw browser ondersteund de javascript Window.showModalDialog methode niet.\nMicrosoft Internet Explorer, Mozilla Firefox 3 (en hoger), en Google Chrome ondersteunen het wel.");
			return false;
		}
		return true;
	}

})();


// Check if session cookie is present. If not present, then page was loaded from browser or google cache and is probably invalid.
xAddEventListener(this, 'load',
	function() {
		var name = 'pepernoot';
		if (CookieJar.contains('PHPSESSID') || CookieJar.contains(name)) {
			return;
		}
		CookieJar.store(name, 1);
		if (CookieJar.contains(name)) {
			if (location.hostname.toLowerCase().indexOf('loyalty') >= 0) {
				//location.reload(true);
				location.href = '/';
				return;
			}
		}
		else {
			/*
			if (!window.opera && /MSIE (\d+\.\d+)/.test(navigator.userAgent) && (RegExp.$1 < 7)) {
				// Don't show warning in old IE browsers because document.cookie sometimes incorrectly returns an empty string: http://support.microsoft.com/kb/820536
				return;
			}
			*/
			var e = document.getElementById('headerCookieMsg');
			if (e) {
				e.innerHTML = 'Helaas kunt u op dit moment niet inloggen doordat uw browser geen cookies aan heeft staan.';
				e.style.display = '';
			}
		}
	},
	false
);
