if(navigator.appName.indexOf("Microsoft")!=-1){
 	if(navigator.userAgent.indexOf('Opera')== -1) isIE=true;
	else isIE=false;
}
else isIE=false;


isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

// If NS -- that is, !IE -- then set up for mouse capture
if (!isIE) document.captureEvents(Event.MOUSEMOVE)

// Set-up to use getMouseXY function onMouseMove
document.onmousemove = getMouseXY;

function clickParent(obj){
	isIE ? obj.parentNode.click():true;
	//alert(obj.parentNode.innerHTML);
}

function ddInit(e){
  topDog=isIE ? "BODY" : "HTML";
  whichDog=isIE ? document.all.theLayer : document.getElementById("theLayer"); 
  hotDog=isIE ? event.srcElement : e.target;  
  while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog){
    hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
  }  
  if (hotDog.id=="titleBar"){
    offsetx=isIE ? event.clientX : e.clientX;
    offsety=isIE ? event.clientY : e.clientY;
    nowX=parseInt(whichDog.style.left);
    nowY=parseInt(whichDog.style.top);
    ddEnabled=true;
    document.onmousemove=dd;
  }
}

function dd(e){
  if (!ddEnabled) return;
  whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; 
  whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
  return false;  
}

function ddN4(whatDog){
  if (!isN4) return;
  N4=eval(whatDog);
  N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  N4.onmousedown=function(e){
    N4.captureEvents(Event.MOUSEMOVE);
    N4x=e.x;
    N4y=e.y;
  }
  N4.onmousemove=function(e){
    if (isHot){
      N4.moveBy(e.x-N4x,e.y-N4y);
      return false;
    }
  }
  N4.onmouseup=function(){
    N4.releaseEvents(Event.MOUSEMOVE);
  }
}

function hideMe(){
  //showSelect();
  //document.getElementById("loading").style.display="none";
  $('#PopupContent').find('textarea').each(function(){
	var id = $(this).attr('id');
	if(typeof(tinyMCE) != "undefined" && tinyMCE.getInstanceById(id)){
		tinyMCE.execCommand('mceRemoveControl', false, id);  
	}
  });
  if (isIE||isNN) {
  	whichDog.style.visibility="hidden";
  }
  else if (isN4){
  	document.theLayer.visibility="hide";
  }
  document.getElementById("PopupContent").innerHTML="";
}

function showMe(){
  tempY=tempY-200;

  if (tempY < 0){tempY = 10}  

  if (isIE||isNN){
	whichDog.style.posistion = 'absolute';
    whichDog.style.left = 250;
    whichDog.style.top = tempY;
  	whichDog.style.visibility="visible";
  }
  else if (isN4){
  	document.theLayer.style.posistion = 'absolute';
    document.theLayer.style.left = 250;
    document.theLayer.style.top = tempY;
  	document.theLayer.visibility="show";
  }
}

function hideSelect() {
	if (navigator.appName.indexOf("MSIE")) {
		if(document.forms){
			for (var S = 0; S < document.forms.length; S++){
				for (var R = 0; R < document.forms[S].length; R++) {
					if (document.forms[S].elements[R].options) {
						document.forms[S].elements[R].style.visibility = "hidden";
					}
				}
			}
		}
	}
} 

function showSelect() {
	if (navigator.appName.indexOf("MSIE")) {
		if(document.forms){
			for (var S = 0; S < document.forms.length; S++){
				for (var R = 0; R < document.forms[S].length; R++) {
					if (document.forms[S].elements[R].options) {
						document.forms[S].elements[R].style.visibility = "visible";
					}
				}
			}
		}
	}
}

document.onmousedown=ddInit;
document.onmouseup= function(){
	ddEnabled=false;
	document.onmousemove = getMouseXY;
};


var urlParams=false;
var RecordID=false;

function showContextMenu(obj,acties,params){
	RecordID=obj.id.substring(2,10);
	urlParams=params;
	div = document.getElementById("contextMenu");
    if(div){
		html=makeContextMenuDiv(acties);
		//alert(div.innerHTML);
		div.innerHTML=html;
		div.style.posistion = 'absolute';
        div.style.left = tempX-10;
        div.style.top = tempY-10;
        div.style.display = '';
	}
}


function makeContextMenuDiv(acties){
	//alert(acties);
	// opbouw van een ContextMenuRow = titel,actieurl,confirmtext
	//deze gegevens worden global aangemaakt op het moment van klikken.
	//alert(menuNr);
	var ContextMenuRows;
	var ContextMenuItem;

	//acties aangegeven met actiesstring toevoegen
	var ContextMenuArrayElements = new Array();
	for(i=0;i<acties.length;i++){
		//alert(acties.substring(i,i+1));
		ContextMenuArrayElements[i]=contextMenu[acties.substring(i,i+1)];
	}

	//alert(ContextMenuArrayElements);
	if(typeof ContextMenuArrayElements !="undefined"){
		html="";
		//html+=" <div id=\"contextMenu\" style=\"DISPLAY: none; Z-INDEX: 1; POSITION: absolute\" onmouseout=\"checkMouse();\">";
		html+=" <table style=\"BORDER-RIGHT: #808080 1px solid; BORDER-TOP: buttonhighlight 1px solid; BORDER-LEFT: buttonhighlight 1px solid; BORDER-BOTTOM: #808080 1px solid\" cellspacing=\"0\" cellpadding=\"3\" width=\"160\" bgcolor=\"buttonface\" border=\"0\">";
		
		for (ContextMenuRows in ContextMenuArrayElements) {
			var ContextMenuRow = ContextMenuArrayElements[ContextMenuRows];
			//alert(ContextMenuRow );
			if(ContextMenuRow[1]=="seperator"){
				html+="<tr style=\"\"><td style=\"padding:0 3px 0 3px;height:3px;line-height:3px;font-size:1px;\"><hr></td></tr>";
			}
			else {				
				confirmText=typeof ContextMenuRow[3] !="undefined" ? ContextMenuRow[3].replace("'","`") : "";
				Url=ContextMenuRow[2];
				actie=ContextMenuRow[1]
				if(actie=="open"){
					actieUrl="window.open(\\\'"+Url+"\\\',\\\'_self\\\')";
				}
				else if(actie=="popup"){
					actieUrl="call_popup(\\\'"+Url+"\\\')";
				}
				else if(actie=="confirm"){
					actieUrl="call_delete(\\\'"+Url+"\\\')";
				}
				else if(actie=="hidden"){
					actieUrl="call_hidden(\\\'"+Url+"\\\')";
				}
				if (confirmText.length>0) actieUrl="if(confirm(\\\'"+confirmText+"\\\')) "+actieUrl+";";
				
				html+="<tr onmouseover=\"this.className='HoverContextMenu';\"  onmouseout=\"this.className='ContextMenu';\" onclick=\"ContextActionOpen(\'"+actieUrl+"\');\">";
				html+="  <td nowrap align=\"left\" width=\"100%\" style=\"padding-left:15px;\">"+ContextMenuRow[0]+"&nbsp;</td>";
				html+="</tr>";
			}

		}

		html+="</table>";
		return html;
	}

}

function ContextActionOpen(actieurl){
	div=document.getElementById("contextMenu");
	div.style.display= 'none';
	div.innerHTML= '';
	actieurl=actieurl.replace("params",urlParams);
	//alert(actieurl);
	eval(actieurl);
}

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (isIE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  //window.status='x:'+tempX+' y:'+tempY;
  return true
}

var timerID = null;

function checkMouse(){
	//alert('checkMouse');
	clearTimeout(timerID);
	div=document.getElementById("contextMenu");
	if(div){
	    divTop=parseInt(div.style.top.replace('px',''));
	    divLeft=parseInt(div.style.left.replace('px',''));
	    divHeight = div.offsetHeight;
	    divWidth = div.offsetWidth;
	    if (tempY-4<divTop || tempX-4<divLeft || tempX>=(divLeft +divWidth) || tempY>=(divTop+divHeight)){
			//alert('x:'+divLeft+' y'+divTop+' h:'+divHeight+' b:'+divWidth);
	    	div.style.display= 'none';
			itemObj=document.getElementById('t:'+RecordID);
			unselectItem(itemObj);	
	    }
		else{
			timerID=setTimeout('checkMouse()',200);
		}
	}
}

function selectItem(obj){
	contextDiv=document.getElementById("contextMenu");
	if (!contextDiv || contextDiv.style.display != ''){
		obj.className+=' SelectedRow';
	}
}

function unselectItem(obj){
	contextDiv=document.getElementById("contextMenu");
	if (!contextDiv || contextDiv.style.display != ''){
		pos=obj.className.indexOf(' SelectedRow');
		if (pos>0) obj.className=obj.className.substring(0,pos);
	}	
}



function setTree(nameEQ){
	//hier moet een check of er wel een boom is
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0){
			enid=c.split('[')[1].split(']')[0];
			//alert(enid);
			show=parseInt(c.split('=')[1]);
			if (show==1){
				openSubTree(enid);
			}
		}
	}
}

function tSubTree(enid){
	subtreeObj=document.getElementById('s:'+enid);
	if (subtreeObj.style.display == ''){
		document.cookie=Node+"["+enid+"]=0";
		hideSubTree(subtreeObj);
		document.images["i:" + enid].src = IMG_MINUS;
	}
	else {
		document.cookie=Node+"["+enid+"]=1";
		if (document.getElementById('d:'+enid).innerHTML.length==0){
			getSubTree(enid);
		}
		showSubTree(subtreeObj);
		document.images["i:" + enid].src = IMG_PLUS;
	}
}

function getSubTree(enid){
	url='CompetentieSubTree.php?parentenid='+enid;
	//alert(url);
	SetInnerHTMLFromAjaxResponse('d:'+enid,url);
}

function openSubTree(enid){
	subtreeObj=document.getElementById('s:'+enid);
	//alert(subtreeObj);
	if (subtreeObj == null){
		setTimeout("openSubTree(enid)",200);
	}
	else{
		getSubTree(enid);
		showSubTree(subtreeObj);
		document.images["i:" + enid].src = IMG_PLUS;
	}
}				

function showSubTree(subtreeObj){
	subtreeObj.style.display = '';
}

function hideSubTree(subtreeObj){
	subtreeObj.style.display = 'none';
}

function resizePopup(obj){
	obj.style.width=isIE ? obj.scrollWidth : obj.innerWidth;
	obj.style.height=isIE ? obj.scrollHeight : obj.innerHeight;
	if (obj.style.width>400) obj.style.width=400;
	if (obj.style.height>500) obj.style.height=500;
	//alert(obj.style.height);
}

function call_for_help(){
	var currenturl=this.location.toString();
	currenturl = currenturl.split("?")[0];
	call_popup("/v3/applicatie/help/pagina/PaginaHelp.php?currenturl="+currenturl)
}

function call_popup(url){
	//alert(url);
    //obj=document.getElementById("theLayer");
	SetInnerHTMLFromAjaxResponse("PopupContent",url);
	if(typeof(whichDog)=='undefined'){
		whichDog=isIE ? document.all.theLayer : document.getElementById("theLayer");
		tempY=400;
	}
	//resizePopup(PopupContent);
	showMe();
}

function call_delete(url){
    obj=document.getElementById("theLayer");
	SetInnerHTMLFromAjaxResponse("PopupContent",url);
}

function call_hidden(url){
    obj=document.getElementById("theLayer");
	//SetInnerHTMLFromAjaxResponse("PopupContent",url);
	GETRequest(url);
}

function submitForm(objForm){
    obj=document.getElementById("theLayer");
    POSTRequest("PopupContent",objForm);
	resizePopup(obj);
    return false;
}

function submitAIMForm(objForm){
	$('#PopupContent').find('textarea').each(function(){
		var id = $(this).attr('id');
		if(typeof(tinyMCE) != "undefined" && tinyMCE.getInstanceById(id)){
			tinyMCE.execCommand('mceRemoveControl', false, id);  
		}
  	});
	objForm.target='iframe';
	return AIM.submit(objForm, {'onStart' : startCallback, 'onComplete' : completeCallback});
}


