// helper to leverage the browser-differences of the behaviour
// (concerning the fired events) of an iframe
function setOnLoadWrapper(frame, func) {
  this.ieIframeLoadWrapper = function(frame,func){
    if(frame.readyState == "complete"){
      func();
    }
  }
  if (isIe()==true)
    // ie fires onreadystatechange of the iframe
    // if it's source is changed programtically
    // the wrapper is necessary to correctly handle
    // the iframe's readyState attribute during loading
    frame.onreadystatechange=new ContextFixer(
      this.ieIframeLoadWrapper,this,frame,func).execute;
  else
    // moz saimply fires onload...
    frame.onload=func;
}

// removed the stuff set by setOnLoadWrapper()
function removeOnLoadWrapper(frame){
  if (isIe()){
    frame.onreadystatechange=null;
  }else{
    frame.onload=null;
  }
}

//get the function set by setOnloadWrapper
function getOnLoadWrapper(frame) {
  if (isIe()){
    return frame.onreadystatechange;
  }else{
   return frame.onload;
  }
	
}

// helper to determine whether the browser is ie
function isIe( ){
  return (document.getElementById && document.all && document.styleSheets) ? 1:0;
}

// determines IE version (http://msdn2.microsoft.com/en-us/library/ms537509.aspx)
// Returns the version of Internet Explorer or a -1 (indicating the use of another browser).
function getIeVersion(){
  var result = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      result = parseFloat( RegExp.$1 );
  }
  return result;
}

// helper to determine whether the browser is nn
function isNn() {
  return (document.getElementById && !document.all) ? 1:0;
}


// helper to calculate the dimensions (in px) of a given element.
// the calculation reflects that child elements of an element may
// be larger than it's parent. actually the function calcualtes the
// minimal rectangle containg all elements. the parameter ignoreTopLevel
// ignores the dimensions of the given element and only consideres it's
// sub-element-tree.


Dimension = function(element, ignoreTopLevel){
	var positions = new Array();
	var dimensionNoOp = function (a,b) {}; 
	var ieVersion = getIeVersion();
	var dimensionExceptions = {
		"DIV" : function (element, ignoreTopLevel){ // filter out menu-elements (their left is at -2000) AND divs with style="display: none;"
			if (element.id.indexOf("menu:")< 0 && element.style.display != "none")
				_calculateDimension(element, ignoreTopLevel);
		}, 
		"SCRIPT" : dimensionNoOp, // filter out scriptelements
		"OBJECT" : dimensionNoOp, // filter out object tags
		"INPUT" : function (element, ignoreTopLevel){  // filter out hidden inputfields
			if (element.type != "hidden")
				_calculateDimension(element, ignoreTopLevel);
		},
		"FORM" : function (element, ignoreTopLevel) {
			if (ieVersion > 6)
				_calculateDimension(element, true);
			else
				_calculateDimension(element, ignoreTopLevel);
		}
	}
	
	
    var _calculateDimension = function(element, ignoreTopLevel){
		if (ignoreTopLevel== null || ignoreTopLevel == false){
			var leftPosition = 0;
  			var topPosition = 0;
  			var width = element.offsetWidth;
  			var height =element.offsetHeight;
  			if (element.offsetParent) {
    			leftPosition = element.offsetLeft;
    			topPosition = element.offsetTop;
    			var parent = element.offsetParent;
    			if (parent != null)
      				do {
        				leftPosition += parent.offsetLeft;
        				topPosition += parent.offsetTop;
      				} while (parent = parent.offsetParent)
  				}
  			positions.push({left:leftPosition,top:topPosition,width:width,height:height});
		}
    
    	var all = element.childNodes;
    	for(var i = 0; i < all.length; i++){
    		var node = all[i];
    		var action = dimensionExceptions[node.nodeName.toUpperCase()];
    		action = action!=null?action: _calculateDimension;
	      	if (node.nodeType == 1 && // dom-node
	            node.offsetLeft >= 0 ) // filter out hidden stuff
					action(node, false);
    	}
	}
  	_calculateDimension(element, ignoreTopLevel);
  	this.left = 9999;
  	this.top = 9999;
  	var topElement = null;
  	var leftElement = null;
  	for (var i = 0; i < positions.length; i++){
    	var position = positions[i];
    	if (this.left > position.left) {
      		this.left = position.left;
      		leftElement = position;
    	}
    	if (this.top > position.top) {
      		this.top = position.top;
      		topElement = position;
    	}
  	}
  	this.width = 0;
  	this.height = 0;
  	for (var i = 0; i < positions.length; i++){
    	var position = positions[i];
    	var offsetLeft = position.left-this.left;
    	var currentWidth = offsetLeft+position.width;
    	if(currentWidth > this.width)
      		this.width= currentWidth;
    	var offsetTop = position.top-this.top;
    	var currentHeight = offsetTop+position.height;
    	if(currentHeight > this.height)
      		this.height = currentHeight;
  	}
}

// helper to get an XMLHttpRequest instance
getXmlHttpRequest = function(){
  if (window.XMLHttpRequest && window.DOMParser &&
        document.implementation && document.implementation.createDocument) {
            this.request = new XMLHttpRequest();
            return this.request;
    }
    try {
        this.request = new ActiveXObject("MSXML2.XmlHttp");
        return this.request;
    } catch (e) {}
    try {
        this.request = new ActiveXObject("Microsoft.XmlHttp");
        return this.request;
    } catch (e) {}
    try {
        this.request = new ActiveXObject("MSXML.XmlHttp");
        return this.request;
    } catch (e) {}
    try {
        this.request = new ActiveXObject("MSXML3.XmlHttp");
        return this.request;
    } catch (e) {}
}

// wrapper class for eventhandlers, ensures that "this" points
// to the instance the handler is a member of and not to "window
function ContextFixer(func, context) {
    /* Make sure 'this' inside a method points to its class */
    this.func = func;
    this.context = context;
    this.args = arguments;
    var self = this;
    this.execute = function() {
        /* execute the method */
        var args = new Array();
        // the first arguments will be the extra ones of the class
        for (var i=0; i < self.args.length - 2; i++)
            args.push(self.args[i + 2]);
        // the last are the ones passed on to the execute method
        for (var i=0; i < arguments.length; i++) 
            args.push(arguments[i]);
        self.func.apply(self.context, args);
    };
}


function UrlParameters(urlString)
{
	this.parameters = new Object();

	if (!urlString || urlString == '')
	{
		return;
	}

	var urlValue = decodeURI(urlString);
	var queryValue = "";
	
	if (urlValue.indexOf("?") >= 0)
	{
		queryValue = urlValue.substring(urlValue.indexOf("?") + 1, urlValue.length);
	}
		
	var keyValuePairs = queryValue.split("&");
	
	for (var i = 0; i < keyValuePairs.length; ++i)
	{
		var ar = keyValuePairs[i].split("=");
		this.parameters[ar[0]] = ar[1];
	}
	
	this.toString = function()
	{
		var tmp = new Array();
		
		for (var key in this.parameters)
		{
			tmp.push(key + "=" + this.parameters[key]);
		}	
		
		return "?" + encodeURI(tmp.join("&"));
	}
	
	this.get = function(parameterName)
	{
		return this.parameters[parameterName];
	}
	
	this.contains = function(parameterName)
	{
		return this.parameters[parameterName] != null;
	}
	
	this.put = function(parameterName, parameterValue)
	{
		this.parameters[parameterName] = parameterValue;
	}
}


