var addEvent = (function(window, document) {
	if (document.addEventListener) {
		return function(elem, type, cb) {
			if ((elem && !elem.length) || elem === window) {
				elem.addEventListener(type, cb, false);
			} else if (elem && elem.length) {
				var len = elem.length;
				for (var i = 0; i < len; i++) {
					addEvent(elem[i], type, cb);
				}
			}
		};
	} else if (document.attachEvent) {
		return function(elem, type, cb) {
			if ((elem && !elem.length) || elem === window) {
				elem.attachEvent('on' + type, function() {
					return cb.call(elem, window.event)
				});
			} else if (elem.length) {
				var len = elem.length;
				for (var i = 0; i < len; i++) {
					addEvent(elem[i], type, cb);
				}
			}
		};
	}
})(this, document);


function isValidEmail(data){
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	strEmail = data;

	// search email text for regular exp matches
	if (strEmail.search(validRegExp) == -1) {
		return false;
	}
	return true;
}

function toggleBox(szDivID, iState) { // 1 visible, 0 hidden
	if(document.layers) { //NN4+
		document.layers[szDivID].visibility = iState ? "show" : "hide";
	} else if(document.getElementById) { //gecko(NN6) + IE 5+
		var obj = document.getElementById(szDivID);
		obj.style.visibility = iState ? "visible" : "hidden";
	} else if(document.all) { // IE 4
		document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
	}
}

startList = function() {
   if (document.all&&document.getElementById) {
      navRoot = document.getElementById("nav");
      for (i=0; i<navRoot.childNodes.length; i++) {
         node = navRoot.childNodes[i];
         if (node.nodeName=="LI") {
            node.onmouseover=function() {
               this.className+=" over";
            }
            node.onmouseout=function() {
               this.className=this.className.replace(" over", "");
            }
         }
      }
   }
}

/**
 * Handles external links based on rel="external"
 * @author Boye Oomens <boye@e-sites.nl>
 * @param none
 * @return {Boolean}
 */
function setExtLinks() {
	this.target = '_blank';
}

/**
 * Adds advanced Google Analytics tracking events to anchors
 *
 * @param none
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 23 feb 2011
 */
function setAdvancedGAEvents() {
	var anchors = document.getElementsByTagName('a'),
		amount = anchors.length,
		filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i,
		self, href;

	while (amount--) {
		self = anchors[amount];
		href = self.href;

		if ((href.match(/^https?\:/i)) && (!href.match(document.domain))) {
			addEvent(self, 'click', function () {
				trackCustomEvent('Uitgaande link', 'Openen', this.href.replace(/^https?\:\/\//i, ''));
			});
		} else if (href.match(/^mailto\:/i)) {
			addEvent(self, 'click', function () {
				trackCustomEvent('E-mail', 'Versturen', this.href.replace(/^mailto\:/i, ''));
			});
		} else if (href.match(filetypes)) {
			addEvent(self, 'click', function () {
				trackCustomEvent('Bestanden', 'Downloaden', parse_url(this.href, 'path'));
			});
		}
	}
}

/**
 * Calls the push method from the global gaq object.
 *
 * @param {String} category
 * @param {String} action
 * @param {String} label
 * @return void
 * @author Joris van Summeren <joris@e-sites.nl>
 * @since 23 feb 2011
 */
function trackCustomEvent(category, action, label) {
	var args = Array.prototype.slice.call(arguments);

	args.unshift('_trackEvent');

	if (typeof _gaq === 'object') {
		_gaq.push(args);
	}
}

/**
 * Parse a URL and return its components
 *
 * This function parses a URL and returns an associative array containing any of the various
 * components of the URL that are present.
 *
 * This function is not meant to validate the given URL, it only breaks it up into the above
 * listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.
 *
 * @param url The URL to parse
 * @return On seriously malformed URLs, parse_url() may return FALSE. Otherwise an associative array is returned,
 * 	whose components may be (at least one):
 * 		scheme - e.g. http
 * 		host
 * 		port
 *		user
 * 		pass
 * 		path
 * 		query - after the question mark ?
 * 		fragment - after the hashmark #
 * If the component parameter is specified a string is returned instead of an array.
 */
function parse_url(input, component) {
	var reg = /^((https|http|ftp)?:\/\/)?(([a-z0-9_-]+):(.+)@)?([^\/:]+)?(:([0-9]+))?([^\?]+)?(\?([^#]+))?(#(.+))?$/i,
		url = reg.exec(input);

	if (url.length !== 14) {
		return false;
	} else {
		var parsed = new Array();

		parsed['scheme'] = url[2];
		parsed['user'] = url[4];
		parsed['pass'] = url[5];
		parsed['host'] = url[6];
		parsed['port'] = url[8];
		parsed['path'] = url[9];
		parsed['query'] = url[11];
		parsed['fragment'] = url[13];

		if (component !== undefined) {
			return parsed[component];
		} else {
			return parsed;
		}
	}
}
