/**
 * @version built on Prototype 1.5
 * @author Maxime Cowez
 * @copyright <a href="http://www.edge.be">Edge.be</a>
 */

<!-- 
	//old general.js
	function PassLanguage(Parameter){
		document.Language.language_code.value = Parameter;
		document.Language.submit();
	}
//-->

if (!Edge) var Edge = {};
Edge.Tools = {
	
	/**
	 * @namespace Ajax
	 * all data formatting helper functions
	 */
	Ajax : {
		
		get: function(resp, url) {
			return /.cfc$/i.test(url) ? Edge.Tools.Ajax.cfcResponse(resp) : Edge.Tools.Ajax.servletResponse(resp);
		},
		
		servletResponse: function(resp) {
			return resp.responseText.evalJSON();
		},
		
		cfcResponse: function(resp) {
			if (window.ActiveXObject) {
				var XMLDOM = new ActiveXObject("Microsoft.XMLDOM");
				XMLDOM.async = "false";
				XMLDOM.loadXML(resp.responseText);
				resp = XMLDOM;
				if (XMLDOM.parseError.errorCode != 0) 
					Edge.Tools.Debug.error("Error from the Microsoft XMLDOM XML parser: " + XMLDOM.parseError.reason + "; " + XMLDOM.parseError.srcText);
			} 
			else if (document.implementation && document.implementation.createDocument) resp = new DOMParser().parseFromString(resp.responseText, "text/xml");
			else Edge.Tools.Debug.error("Could not generate an XML DOM object");
			
			var respString = "";
			$A(resp.getElementsByTagName("string").item(0).childNodes).collect(function(node) {
				respString += node.nodeValue;
			});
			return respString.evalJSON();
		}
		
		/*,	
		convert: function(string) {
			var deC = Edge.Tools.Ajax.deserializeChars;
			if (!deC) {
				deC = {"&amp;": "&", "&lt;": "<", "&gt;": ">"};
				$R(0, 32).each(function(i) {
					if ((i != 9) && (i != 10) && (i != 13)) {
						var hex = i.toString(16);
						if (hex.length == 1) hex = "0" + hex;
						deC["<char code='" + hex + "'/>"] = String.fromCharCode(i);
					};
				});
				$R(128, 256).each(function(i) {deC["&#x" + i.toString(16) + ";"] = String.fromCharCode(i);});
				Edge.Tools.Ajax.deserializeChars = deC;
			}
			
			for (var c in deC) 
				if (typeof deC[c] != "function") 
					string = string.replace(c != "\\" ? new RegExp(c, "g") : /\\/g, deC[c]);
			return string;
		}*/
		
	},
	
	/**
	 * @namespace Format
	 * all data formatting helper functions
	 */
	Format : {
		
		/**
		 * @method todayString
		 * Returns the actual date as string [yyyymmdd]
		 * @return {String}
		 */
		todayString: function() {
			var today = new Date();
			return String(today.getFullYear()) + Edge.Tools.Format.addLeadingZeros(today.getMonth()+1) + Edge.Tools.Format.addLeadingZeros(today.getDate());
		},
		
		/**
		 * adds a 0 in front of a number smaller than 10 and returns it as a String
		 * @param {Number} nr
		 * @return {String}
		 */
		addLeadingZeros: function(nr) {
			return String(nr).length < 2 ? "0" + nr : nr;
		}
		
	},
	
	/**
	 * @namespace Gfx
	 * all graphics helper functions
	 */
	Gfx : {
		
		/**
		 * @method makeTransparent
		 * makes a given element transparent through CSS
		 * @param {Element,String} elem		the element to apply the transparency to
		 * @param {Number} value			the opacity value to set
		 */
		makeTransparent: function(elem, value) {
			$(elem).setStyle({
				'filter':		'alpha(opacity='+(value*100)+')',
				'-moz-opacity': value,
				'opacity': 		value
			});
		}
		
	},

	/**
	 * @namespace Error
	 * debugging helpers
	 */
	Debug : {
	
		error: function(msg) {
			try {
				console.error(msg);
				console.trace();
			}
			finally {
				throw new Error(msg);
			}
		},
		
		warn: function(msg) {
			try {
				console.warn(msg);
			}
			catch (e) {}
		},
		
		trace: function(msg) {
			try {
				console.log(msg);
			}
			catch (e) {}
		},
		
		ajaxError: function(msg) {
			Edge.Tools.Debug.error("Error " + msg.status + "(" + msg.statusText + "): " + msg.responseText);
		}
		
	},
	
	/**
	 * @method versionRange
	 * Returns whether the users browser version is within a given range
	 * @param {Number} low		the low version limit
	 * @param {Number} high		the high version limit
	 * @return {Boolean}
	 */
	IErange: function(low, high) {
		if (!Prototype.Browser.IE) return false;
		
		var regex = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
		return regex != null && Number(regex[1]) >= low && Number(regex[1]) <= high;	
	}
	
}