//tooltip variables
var tooltip_id = 'div_Tooltip';
var obj_Tooltip = '';
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//			TOOLTIP FUNCTIONS
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_showTooltip(obj, event){
	if(!event){
		event=window.event;
	}

	//creates the div element once with the tootltiptext as content
	if (!obj_Tooltip){
		var element_Tooltip = document.createElement('div');
		element_Tooltip.className = 'div_Tooltip';
		element_Tooltip.setAttribute('id', tooltip_id);
//		var element_TooltipText = document.createTextNode("<b>TEST</b>");
//		element_Tooltip.appendChild(element_TooltipText);
		document.body.appendChild(element_Tooltip);

		obj_Tooltip = document.getElementById(tooltip_id);

		obj_Tooltip.style.display = 'none';
		obj_Tooltip.style.zIndex = '999';
		obj_Tooltip.style.position = 'absolute';
		obj_Tooltip.style.left = '0px';
		obj_Tooltip.style.top = '0px';
		obj_Tooltip.style.width = obj.getAttribute('u:tooltipwidth');
		obj_Tooltip.style.maxWidth = '150px';
		obj_Tooltip.style.backgroundColor = '#403B3B';
		obj_Tooltip.style.color = '#FFFFFF';
		obj_Tooltip.style.fontSize = '10px';
		obj_Tooltip.style.border = '1px solid #262626';
		obj_Tooltip.style.padding = '2px';
		obj_Tooltip.style.MozOpacity="0.8";
		obj_Tooltip.style.filter = 'alpha(opacity= 80)';


		obj_Tooltip.innerHTML = obj.getAttribute('u:tooltiptext');
	}else{
		obj_Tooltip.style.width = obj.getAttribute('u:tooltipwidth');
		obj_Tooltip.innerHTML = obj.getAttribute('u:tooltiptext');
	}

	//shows or hide the tooltip
	if (obj_Tooltip){
		if(obj_Tooltip.innerHTML != ""){
			if (obj_Tooltip.style.display == 'block'){
				obj_Tooltip.style.display = 'none';
			}else{
				obj_Tooltip.style.display = 'block';

				//show the tooltip at the cursor location
				UNI_moveTooltip(event);
			}
		}
	}

	//onmouseout the tooltip should disappear
	obj.onmouseout = UNI_hideTooltip;
	
	//obj.onclick += hideTooltip;
	//onmousemove the tooltip should move along with the cursor
	obj.onmousemove = UNI_moveTooltip;
}

function UNI_moveTooltip(event){
	if(!event){
		event=window.event;
	}
	if (obj_Tooltip){
		
		
		if (browser_type.isIE){	
			if(event.clientX + 20 + document.body.scrollLeft + obj_Tooltip.offsetWidth > document.getElementById('index').offsetWidth){
				obj_Tooltip.style.left = (event.clientX - 20 - obj_Tooltip.offsetWidth) + 'px';
			}else{
				obj_Tooltip.style.left = (event.clientX + 20 + document.body.scrollLeft) + 'px';
			}
			
			if(event.clientY + obj_Tooltip.offsetHeight > document.getElementById('index').offsetHeight){
				obj_Tooltip.style.top = (event.clientY - 30) + 'px';
			}else{
				obj_Tooltip.style.top = (event.clientY + document.body.scrollTop) + 'px';
			}
		}else{
			if(event.pageX + 20  + obj_Tooltip.offsetWidth > document.getElementById('index').offsetWidth){
				obj_Tooltip.style.left = (event.pageX - 20 - obj_Tooltip.offsetWidth) + 'px';
			}else{
				obj_Tooltip.style.left = (event.pageX + 20) + 'px';
			}
			
			if(event.pageY + 30  + obj_Tooltip.offsetHeight > document.getElementById('index').offsetHeight){
				obj_Tooltip.style.top = (event.pageY-25) + 'px';
			}else{
				obj_Tooltip.style.top = event.pageY + 'px';
			}
		}
	}
}

function UNI_hideTooltip(){
	if (obj_Tooltip){
		obj_Tooltip.style.display = 'none';
	}
}

// Array vol met XMLHttpRequests
var UNI_XML_data = new Array();

// Used for the function getdata for storing multiple getdata requests
var XMLdataM = new Array();
var urlM = new Array();
var destinationM = new Array();
var modeM = new Array();

function getCookie(NameOfCookie){

	if (document.cookie.length > 0) 	{

		begin = document.cookie.indexOf(NameOfCookie+"=");
		
		if (begin != -1) {
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(begin, end)); 
		}
	} 
	
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function createCookie(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=/";
}

function isArray(obj) {
	return (obj.constructor.toString().indexOf("Array") != -1);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function browserCheck(){
	var nav_userAgent, browser_type;

	nav_userAgent = navigator.userAgent;

	this.isIE    = false;
	this.version = null;

	browser_type = "MSIE";
	if ((i = nav_userAgent.indexOf(browser_type)) >= 0){
		this.isIE = true;
		this.version = parseFloat(nav_userAgent.substr(i + browser_type.length));
		return;
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var browser_type = new browserCheck();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_getXMLHttpRequest()	{
//	var rnd_xml_id = Math.floor(Math.random()*UNI_XML_data.length) +1;
	var UNI_XML_data_length = UNI_XML_data.length;
	
	
	if (UNI_XML_data_length == 0){
		UNI_XML_data[UNI_XML_data_length] = '';
	}else{
		for (var i=0; i< UNI_XML_data.length; i++){
			if (UNI_XML_data[i]){
				if (UNI_XML_data[i].readyState == 0 || UNI_XML_data[i].readyState == 4){
					delete UNI_XML_data[i];
					UNI_XML_data[i] = null;
					UNI_XML_data.splice(i,1);
					i--;
				}
			}
		}
		UNI_XML_data_length = UNI_XML_data.length;
	}
	
	// ActiveXObject IE en XMLHttpRequest firefox
	if(window.ActiveXObject){
		//UNI_XML_data[UNI_XML_data_length] = new ActiveXObject("Microsoft.XMLHTTP");
		
		return UNI_XML_data[UNI_XML_data_length] = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(window.XMLHttpRequest){
		// Maakt het mogelijk dat de outerHTML attribute gebruikt kan worden voor FireFox.
		HTMLElement.prototype.__defineSetter__("outerHTML", function (sHTML){
			var r = this.ownerDocument.createRange();
			r.setStartBefore(this);
			var df = r.createContextualFragment(sHTML);
			this.parentNode.replaceChild(df, this);
		});

		return UNI_XML_data[UNI_XML_data.length] = new XMLHttpRequest();
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_setXMLHttpRequestResult(destination,content,mode, callback)	{
	
	if (!document.getElementById(destination))	{
		alert ("destination: "+destination+" niet gevonden");
		return;
	} else {
		if(mode == "replacechildren"){
			try {
				document.getElementById(destination).innerHTML = content;
			} catch(err){
				alert(err.description);
			}
		} else if(mode == "addchildrenbefore"){
			document.getElementById(destination).innerHTML = content+document.getElementById(destination).innerHTML;
			
		} else if(mode == "addchildrenafter"){
			document.getElementById(destination).innerHTML = document.getElementById(destination).innerHTML+content;
			
		} else if(mode == "replace"){
			document.getElementById(destination).outerHTML = content;
		}
		
		// Kijken of er nog een callback methode aangeroepen moet worden
		if(callback != undefined && callback != ""){
			eval(callback);
		}
	}
}

function UNI_command(obj)	{
	eval(obj.attributes['u:command'].nodeValue);
}

function UNI_showLoading(type){
	if(type == 'hide'){
//		document.getElementById('loading').style.display = 'none';
	} else {
//		UNI_center('loading');
//		document.getElementById('loading').style.display = 'block';
//		document.getElementById('loading').style.top = '300px';
//		document.getElementById('loading').style.left = '350px';
		
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_getdata(object, callback, show_loading){
	if(show_loading != "no"){
		UNI_showLoading("show");
	}
	var max_close = 5;
	var normal_close = 0;
	var canShow = false;
	
	
	// Multi getdata
	for(var i=1; i <= max_close; i++){
		XMLdataM[i] = null;
		try{
			XMLdataM[i] = UNI_getXMLHttpRequest();
			try{
				if(object.attributes['u:enabled'].nodeValue == "false"){
					return;
				}
			}catch (e){
				//alert("u:enabled::"+e.description);
			}
			
			urlM[i] = object.attributes['u:url_'+i].nodeValue;
			destinationM[i] = object.attributes['u:destination_'+i].nodeValue;
			modeM[i] = object.attributes['u:mode_'+i].nodeValue;
//			alert(urlM[i]+"::"+destinationM[i]+"::"+modeM[i]);
	
			try	{
				for(var p=1; p < 5; p++){
					// Controleren of er nog parameters moeten worden meegegeven met de url
					if(object.attributes['u:urlparam_'+i+'_'+p].nodeValue != ""){
						params_string = object.attributes['u:urlparam_'+i+'_'+p].nodeValue;
						// Replacement uitvoeren
						params_string = params_string.replace("this","object");
						var params = params_string.split("|");
						if(params[1].substring(0,12) == "javascript::"){
							var javascript_eval = params[1].substring(12,params[1].length);
							var eval_string = eval(javascript_eval);
						} else {
							var eval_string = params[1];
						}
	
						urlM[i] += "&"+ params[0] + "=" + eval_string;
					}
				}
			} catch(err){
				//alert("u:urlparam::"+err.description);
			}
			canShow = true;
		}catch(err){
			canShow = false;
			normal_close++;
		}
		
		if(canShow){
//			alert (XMLdataM[i]+' '+urlM[i]+' '+destinationM[i]+' '+modeM[i]);
			UNI_createOnReadystateChange(object, XMLdataM[i], urlM[i], destinationM[i], modeM[i], callback, show_loading);
		}
	}
	
	// Single getdata
	if(normal_close == max_close){
		try{
			var XMLdata = UNI_getXMLHttpRequest();

			try{
				if(object.attributes['u:enabled'].nodeValue == "false"){
					//alert("u:enabled::"+e.description);
					return;
				}
			}catch (e){
			}
			var url = object.attributes['u:url'].nodeValue;
			var destination = object.attributes['u:destination'].nodeValue;
			var mode = object.attributes['u:mode'].nodeValue;
			
			try{
				// Controleren of er nog parameters moeten worden meegegeven met de url
				if(object.attributes['u:url_params'].nodeValue != ""){
					var params_string = object.attributes['u:url_params'].nodeValue;
					var params = params_string.substring(0,params_string.length-1).split("|");
					for(var i =0;i < params.length; i++){
	
						if(params[i+1].substring(0,12) ==  "javascript::"){
							var javascript_eval = params[i+1].substring(12,params[i+1].length);
							var eval_string = eval(javascript_eval);
							// Normale listgrid controle
							if(params[i] == "idnr" && eval_string == ""){
								return;
							}
						}else{
							var eval_string = params[i+1];
						}
						url += "&"+ params[i] + "=" + eval_string;
						i++;
					}
				}
			} catch(err){
			}
	
		}catch(err){
			var txt = "GETDATA() niet goed opgemaakt!.\n\n";
			txt += "Error description: " + err.description + "\n\n";
			alert(txt);
			return;
		}

		UNI_createOnReadystateChange(object, XMLdata, url, destination, mode, callback, show_loading);
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var number_of_loads = 1;
function UNI_createOnReadystateChange(obj, obj_xml, url, destination, mode, callback,show_loading){
	try{
		obj_xml.onreadystatechange = function(){
			if (obj_xml.readyState == 4){
				
				if(getCookie("onconstruct") != null){
					var onconstruct = getCookie("onconstruct");
					eraseCookie("onconstruct");
					eval(onconstruct);
				}
				UNI_setXMLHttpRequestResult(destination, obj_xml.responseText, mode, callback);
				if(getCookie("onloaded") != null){
					var onloaded = getCookie("onloaded");
					eraseCookie("onloaded");
					eval(onloaded);
				}

				try {
					if(number_of_loads == obj.attributes['u:total_load'].nodeValue && show_loading != "no"){
						UNI_showLoading("hide");
						number_of_loads = 1;
					} else {
						number_of_loads++;
					}
				} catch(err){
					// Attribute u:total_load niet gevonden, dus maar 1 load uitgevoerd
					if(show_loading != "no"){
						UNI_showLoading("hide");
					}
				}
				
				
				try{
					if(obj.attributes['u:listgrid_reminder'].nodeValue == "true"){
						UNI_setListgridReminder(obj, url, destination);
					}
				}catch (e){
				}
				
			} 
		}
		
		UNI_sendData(obj_xml, url, obj);
	}catch(err){
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_getdata_manual(url, destination, mode,show_loading){
	var obj = document.createElement('div');
	obj.setAttribute("u:url", url);
	obj.setAttribute("u:destination", destination);
	obj.setAttribute("u:mode", mode);
	obj.setAttribute("u:seq", "true");
	UNI_getdata(obj,"",show_loading);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_sendData(obj_xml, url, obj){
	var queryStr = url.split("?");
	if (queryStr[0] == ''){
		queryStr[0] = url;
	}

	if(browser_type.isIE){
		var seq = "";
		try {
			seq = obj.attributes['u:seq'].nodeValue;			
		} catch(err){
			seq = false;
		}
		obj_xml.open('POST', queryStr[0], true);
	} else {
		obj_xml.open('POST', queryStr[0], true);
	}
	
	obj_xml.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	obj_xml.send(queryStr[1]);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_sendHeader(object){
	if(!document.getElementById('frame_header')){
		var element_Header = document.createElement('iframe');
		element_Header.setAttribute('id', 'frame_header');
		element_Header.setAttribute('src', '');
		element_Header.style.display = 'none';
		document.body.appendChild(element_Header);
		alert('send');
	}

	if(object.getAttribute('u:url_1')){
		document.getElementById('frame_header').src = object.getAttribute('u:url_1');
	}else{
		document.getElementById('frame_header').src = object.getAttribute('u:url');
	}
	UNI_showLoading('hide');
}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_show(object, ev){
	UNI_setAttrib(object.attributes['u:target'].nodeValue,'display','block');
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_center(object_id, parent_id){
	object = document.getElementById(object_id);
	if(parent_id == ""){
		// De body pakken;
		parent_id = "index";
	}
	parent_object = document.getElementById(parent_id);
	var posParent = getObjectDimensions(parent_object);
	var posChild = getObjectDimensions(object);
	var myLeft = (posParent['w']-posChild['w'])/2;
	var myTop = (posParent['h']-posChild['h'])/2;
	object.style.left=myLeft+'px';
	object.style.top=myTop+'px';
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Geeft de x, y, width en heigth terug van een object
function getObjectDimensions(obj){
	var arr = new Array;
	var offsetX = 0;
	var offsetY = 0;
	var offsetW = obj.offsetWidth;
	var offsetH = obj.offsetHeight;

	if (obj.offsetParent){
		while(1){
			offsetX += obj.offsetLeft;
			offsetY += obj.offsetTop;

			if (!obj.offsetParent){
				break;
			}
			obj=obj.offsetParent;
		}
	}else if(obj.x) {
		offsetX += obj.x;
		offsetY += obj.y;
	}

	arr['x'] = offsetX;
	arr['y'] = offsetY;
	arr['w'] = offsetW;
	arr['h'] = offsetH;

	return arr;
}

function getDocumentSize()	{
	var size = new Array;
	//opera Netscape 6 Netscape 4x Mozilla 
	if (window.innerWidth || window.innerHeight){ 
		size['width'] = window.innerWidth; 
		size['height'] = window.innerHeight; 
	} 
	//IE Mozilla 
	if (document.body.clientWidth || document.body.clientHeight){ 
		size['width'] = document.body.clientWidth; 
		size['height'] = document.body.clientHeight; 
	} 
	
	return size;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	try {
	    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	    var oCurrent;
	    var oAttribute;
	    for(var i=0; i<arrElements.length; i++){
	        oCurrent = arrElements[i];
	        oAttribute = oCurrent.getAttribute(strAttributeName);
	        if(typeof oAttribute == "string" && oAttribute.length > 0){
	            if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
	                arrReturnElements.push(oCurrent);
	            }
	        }
	    }
	    return arrReturnElements;
	}catch(err){
		
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function UNI_setAttrib(object, attribute, value){
	if(obj = document.getElementById(object)){
		if(obj.style[attribute] != '' && obj.style[attribute]){
			obj.style[attribute] = value;
  		}else{
  			obj.setAttribute(attribute, value);

  			// render automatisch de tabs indien hiervan de attributen worden aangepast.
  			if(attribute == 'u:enabled' && obj.getAttribute('u:tab')=='true'){
  				renderTab();
  			}
  		}
	}
}


