var oRange;
//dit is voor het zoeken van tekst in de html pagina's:
var SearchWord;
SearchWord = "";
var OldSearchWord;
if (typeof(OldSearchWord) == "undefined") 
	OldSearchWord = ""; 
//-----

function j_selectwords()
{
	if (!window.getSelection)
		oRange = document.body.createTextRange();
	
	if (parent.parent.frames.length > 0)
	{
		var makeselection = parent.parent.frames["frselect"].document.getElementById("hidmakeselection");
		var searchText = parent.parent.frames["frselect"].document.getElementById("hidsearch");

		if ((searchText != null) && (makeselection != null))
		{
			if (makeselection.value == "true")
			{
				 //Alleen woorden selecteren als via zoeken de pagina geopend is:
				if (searchText.value != "") 
					j_getstring(searchText.value);
				makeselection.value = "false";
			}
		}
	}
}

function j_getstring(SearchString)
{
	var combi = false;
	var GetChar = "";
	var Woord = "";
	var LengthDescr = SearchString.length;
	
	for (i = 0; i < LengthDescr; i++)
	{
		GetChar = SearchString.substring(i, i+1);

		if (GetChar == '"')
		{
			if (combi == true)
				combi = false;
			else					
				combi = true;
		}
		
		if (combi == true)
		{
			if (GetChar != '"' && GetChar != '*') Woord = Woord + GetChar;
		}
		else
		{
			if (GetChar != '"' && GetChar != '*' && GetChar != " " && GetChar != "(" && GetChar != ")")
			{
				Woord = Woord + GetChar;
			}
			else
			{
				if (Woord != "" && Woord.toLowerCase() != "or" && Woord.toLowerCase() != "and" && Woord.toLowerCase() != "near")
				{
					j_selecttext(Woord);
				}
				Woord = "";
			}
		}
	}
	if (Woord != "" && Woord.toLowerCase() != "or" && Woord.toLowerCase() != "and" && Woord.toLowerCase() != "near")
	{
		j_selecttext(Woord);
	}
}


function j_selecttext(SearchText)
{
	var bFound = true;
	
	while (bFound == true)
		bFound = j_searchword(SearchText, window, false);

	window.scrollTo(0,0); //ga weer naar boven in pagina
	var divContent = document.getElementById('divContent');
	if (divContent != null)
		divContent.scrollTop = 0;
}

//--Zoekvenster--
function j_getkeys()
{
//	empty
}

function j_searchdialog()
{
	OldSearchWord = "";

	window.location.href = window.location.href;

	var pos = window.location.pathname.toLowerCase().indexOf("html"); //Er is hierbij vanuit gegaan dat er een html directory bestaat!!
	if (pos==-1)
		pos = window.location.pathname.toLowerCase().indexOf("pages")
	var root = window.location.pathname.substring(0, pos);

	var width	= 280;
	var height	= 100;
	var left	= window.screen.width/2 - width/2;
	var top		= window.screen.height/2 - height/2;
	
	var new_window = window.open(root + "pages/FindDialog.htm", null, "left=" + left + ", top=" + top + ", height=" + height + ", width=" + width + ", status=no, toolbar=no, resizable=no, menubar=no, scrollbars=no, location=no");
}

function j_setWindowHeight()
{
	var divContent = document.getElementById('divContent');
	var divTop = document.getElementById('divTop');
	if (divContent != null)
	{
		try // added for PDF documents
		{
			//if (window.innerHeight == null)
			//	divContent.style.height = (document.body.clientHeight - 110) + 'px';
			//else
			//	divContent.style.height = (window.innerHeight - 140) + 'px';
			if (window.innerHeight != null)
				divContent.style.height = (window.innerHeight - 140) + 'px';
		}
		catch (e)
		{
			//alert(e.message);
		}
	}
}

function j_searchword( toFind, frameToSearch, PopUpSearch ) 
{
	var ifFound = false;
	
	if (toFind!=null && toFind!='')
	{
		if (window.find)
		{
			//Netscape compatible browsers provide the window.find method
			if (document.layers)
			{
				//Against the JS spec, Netscape 4 will produce errors if too many arguments are given
				ifFound = frameToSearch.find( toFind, false, false );
			}
			else
			{
				ifFound = frameToSearch.find( toFind, false, false, false, false, true, false );
			}

			var Selection = frameToSearch.getSelection();
			if (Selection.rangeCount > 0)
			{
				var Range = Selection.getRangeAt(0);
				var cloneRange = Range.cloneRange();
			
				var newNode = document.createElement("label");
				newNode.setAttribute('class', 'selectedword');
				newNode.appendChild(document.createTextNode(Range.toString()));

				Range.deleteContents();
				Range.insertNode(newNode);

				cloneRange.selectNode(newNode);
				cloneRange.collapse(false);
				Selection.removeAllRanges();
				Selection.addRange(cloneRange);
			}	
		}
		else if (frameToSearch.document.body && frameToSearch.document.body.createTextRange)
		{
			//IE or compatible use various TextRange features
			if (frameToSearch.document.selection && frameToSearch.document.selection.type != 'None')
			{
				//If some text is selected already (previous search or if they have selected it)
				//make that the text range. Then move to the end of it to search beyond it
				var Range = frameToSearch.document.selection.createRange();
			}
			else
			{
				//If no text is selected, start from the start of the document
				var Range = frameToSearch.oRange;

			}
			//find the next occurrence of the chosen string
			ifFound = Range.findText(toFind);
			if (ifFound)
			{ 
				Range.select();
				Range.pasteHTML("<label class='selectedword'>" + Range.htmlText + "</label>");
				Range.collapse( false );
			}
		}
		else
		{
			if (PopUpSearch==true) 
				window.alert('Gebruik de zoekfunctie van uw browser');
		}
	}
	else
	{
		if (PopUpSearch==true) 
		{
			window.alert('U heeft geen zoekwoord ingevoerd'); return false;
		}
	}
	
	if (PopUpSearch==true && !ifFound) 
		window.alert('Zoekwoord niet gevonden');
	
	return ifFound;
}


/* Functies tbv notities bij de Html documenten */
var SubDir = "../";

function j_SubDir(DocumentHref)
{
	//Als document zich in directory onder htmldirectory bevindt,
	//dan is SubDir 2 directories diep:
	var SubDir = "../";
	if (DocumentHref!=null && DocumentHref.length>0)
	{
		var test = DocumentHref.split("html/");
		if (test.length == 2)
		{
			var SubCount = test[1].split("/").length;
			SubDir = (SubCount > 1 ? "../../" : "../")
		}
	}

	return SubDir;
}

function j_NotePopup(DocumentHref, DocumentTitle)
{
	var width	= 500;
	var height	= 350;

	DocumentHref = DocumentHref.split('?')[0];
	if (window.showModalDialog)
	{
		window.showModalDialog(SubDir+'pages/popup.aspx?linkpage=NotePopUp.aspx&title=Invoeren aantekening&documenthref='+DocumentHref+'&documenttitle='+DocumentTitle,window, 'status:no;scroll:no;help:no;dialogWidth:'+width+'px;dialogHeight:'+height+'px');
	}
	else
	{
		var left	= window.screen.width/2 - width/2;
		var top		= window.screen.height/2 - height/2;
	
		window.open(SubDir+'pages/popup.aspx?linkpage=NotePopUp.aspx&title=Invoeren aantekening&documenthref='+DocumentHref+'&documenttitle='+DocumentTitle, 'name', 'left='+left+', top='+top+', height='+height+' ,width='+width+', toolbar=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, modal=yes');
	}
}

function j_ScopePopup()
{
	var width	= 500;
	var height	= 500;

	if (window.showModalDialog)
	{
		window.showModalDialog('popup.aspx?linkpage=SearchResult.aspx&scope=1&title=Selecteer',window, 'status:no;scroll:no;help:no;dialogWidth:'+width+'px;dialogHeight:'+height+'px');
		alert(window.returnValue);
	}
	else
	{
		var left	= window.screen.width/2 - width/2;
		var top		= window.screen.height/2 - height/2;
		window.open('popup.aspx?linkpage=SearchResult.aspx&scope=1&title=Selecteer', 'name', 'left='+left+', top='+top+', height='+height+' ,width='+width+', toolbar=no, directories=no, status=yes, menubar=yes, scrollbars=no, resizable=yes, modal=yes');
	}
}

function j_InitRDMGWebService(DocumentHref)
{
	var browser = new Browser();
	if (browser.isIE)
	{ 
		SubDir = j_SubDir(DocumentHref);
		if (window.event.srcElement.readyState=='complete' && typeof(window.event.srcElement.useService)!='undefined')
			window.event.srcElement.useService(SubDir+'webservices/RDMGWebService.asmx?WSDL','wsRDMGService');
	}
}

function j_CheckHasNote(DocumentHref)
{
	j_setWindowHeight();
	var browser = new Browser();
	DocumentHref = DocumentHref.split('?')[0];
	if (!browser.isIE)
		SubDir = j_SubDir(DocumentHref);
	j_callWebService("DocumentHasNote", "DocumentHref", DocumentHref);
	if (document.location.href.indexOf('#') > 0)
	{
		//alert(document.location.href);
		document.location.href = document.location.href;
	}
}

function j_logoutUser()
{
	j_callWebService("Logout", "", null);
	alert("U bent nu uitgelogd.");
}

function Browser()
{
	var ua, s, i;

	this.isIE = false;
	this.isNS = false;
	this.version = null;

	ua = navigator.userAgent;

	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return this;
	}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0)
	{
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return this;
	}

	// Treat any other "Gecko" browser as NS 6.1.

	s = "Gecko";
	if (ua.indexOf(s) >= 0)
	{
		this.isNS = true;
		this.version = 6.1;
		return this;
	}
	return this;
}

function j_getBaseUrl(url)
{
	if (url == null)
	{
	    var oBaseColl = document.all.tags('BASE');
		url = (oBaseColl && oBaseColl.length) ? oBaseColl[0].href : window.location.href;
	}

	var urlArray = url.split('?')[0].split('/');
	urlArray[urlArray.length-2] = ""; // Pages part
	urlArray[urlArray.length-1] = ""; // pagename
	var Result = urlArray.join('/');
	// skip the last / token
	return Result.substring(0, Result.length-1);
}

function j_callWebServiceWithCallback(callBack, method, paramname, paramvalue)
{
	var browser = new Browser();
	
	if (browser.isIE)
	{ 
		var service = document.getElementById("service");
		
		if (typeof(service)!="undefined" && service!=null)
		{
			if (service.wsRDMGService!=null)
			{
				service.wsRDMGService.callService(callBack, method, paramvalue);
			}
		}
	}
	else if (browser.isNS)
	{
		var BaseUrl = j_getBaseUrl(document.location.href);
		
		var soapVersion		= 0; //version 1.1;
		var object			= "uri:RDMGWebService";
		var transportURI	= BaseUrl + "webservices/RDMGWebService.asmx";
		transportURI = transportURI.replace("html/", "");
		
		var headers	= new Array( ); 
		var params	= new Array();
		if (paramname!=null && paramname.length>0 && paramvalue!=null && paramvalue.length>0)
		{
			var param1		= new SOAPParameter();
			param1.name		= paramname;
			param1.value	= paramvalue;
			params[0]		= param1;
		}

		var call = new SOAPCall();
		call.transportURI	= transportURI;
		call.actionURI		= object + "/" + method;
		call.encode(soapVersion, method, object, headers.length, headers, params.length, params);
		var currentRequest	= call.asyncInvoke(callBack);
	}
}

function j_callWebService(method, paramname, paramvalue)
{
	var browser = new Browser();
	
	if (browser.isIE)
	{ 
		var service = document.getElementById("service");
		
		if (typeof(service)!="undefined" && service!=null)
		{
			if (service.wsRDMGService!=null)
			{
				service.wsRDMGService.callService(CallBackFunction, method, paramvalue);
			}
		}
	}
	else if (browser.isNS)
	{
		var BaseUrl = j_getBaseUrl(document.location.href);
		
		var soapVersion		= 0; //version 1.1;
		var object			= "uri:RDMGWebService";
		var transportURI	= BaseUrl + "webservices/RDMGWebService.asmx";
		transportURI = transportURI.replace("html/", "");
		
		var headers	= new Array( ); 
		var params	= new Array();
		if (paramname!=null && paramname.length>0 && paramvalue!=null && paramvalue.length>0)
		{
			var param1		= new SOAPParameter();
			param1.name		= paramname;
			param1.value	= paramvalue;
			params[0]		= param1;
		}

		var call = new SOAPCall();
		call.transportURI	= transportURI;
		call.actionURI		= object + "/" + method;
		call.encode(soapVersion, method, object, headers.length, headers, params.length, params);
		var currentRequest	= call.asyncInvoke(CallBackFunction);
	}
}

function CallBackFunction(result, call, status)
{
	var note;
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		if (!result.error && result.value)
			note = true;
		else
			note = false;
	}
	else if (navigator.userAgent.indexOf("Netscape6/")>=0 || navigator.userAgent.indexOf("Firefox")>=0)
	{
		if (status==0 && !result.fault && result.body.firstChild.firstChild.firstChild.data == "true")
			note = true;
		else
			note = false;
	}

	// added for PDF documents
	try
	{
		if (document != null)
		{
			var	imgEditor = document.getElementById("imgEditor");

			if (imgEditor!=null)
			{
				if (note == true)
				{
					imgEditor.src = SubDir+"images/site/editor.gif";
					imgEditor.alt = "Aantekening tonen";
				}
				else
				{
					imgEditor.src = SubDir+"images/site/editor_gray.gif";
					imgEditor.alt = "Aantekening toevoegen";
				}
			}
		}
	}
	catch(e)
	{
	}
}

function j_setFocus(inputfield)
{
	var txtfield = document.getElementById(inputfield);
	if (txtfield != null) 
	{
		if (document.all != null)
			txtfield.focus();
		else
			txtfield.select();
	}
}

function j_catchEnter(evt)
{
	evt = (evt) ? evt : ((window.event) ? window.event : "")
	var elem  = evt ? evt.target ? evt.target : evt.srcElement : null;
	var inputfield = 'txtSearch';
	var actionbutton = 'btnSearch';
	var txtfield = document.getElementById(inputfield);
	var btnaction = document.getElementById(actionbutton);
	
	if ((txtfield != null) && (btnaction != null))
	{
		if (txtfield.id = elem.id)
		{
			var key = window.event ? evt.keyCode : evt.which;
			if (key == 13)
			{
				window.event.returnValue = false;
				//evt.cancelBubble = true;
				//if (e.stopPropagation) e.stopPropagation();
				btnaction.click();
			}
		}
	}
}

function j_openwindow(URL, width, height )
{
	var new_window = window.open(URL,null,"height=" + height + ",width=" + width + ",status=yes, toolbar=no, resizable=yes, menubar=yes,scrollbars=yes, location=no");
	if (new_window != null) 
	{
    	new_window.focus();
	}

	return new_window;
}

var colorObject;
function j_colordialog()
{
	colorObject = "background";
	showModalDialog("../pages/ColorPickerDialog.htm",window,"status:no;scroll:no;help:no;dialogWidth:300px;dialogHeight:250px");
}
function j_colorfont()
{
	colorObject = "font";
	showModalDialog("../pages/ColorPickerDialog.htm",window,"status:no;scroll:no;help:no;dialogWidth:300px;dialogHeight:250px");
}
function j_colorbutton()
{
	colorObject = "button";
	showModalDialog("../pages/ColorPickerDialog.htm",window,"status:no;scroll:no;help:no;dialogWidth:300px;dialogHeight:250px");
}
function j_colorbgbutton()
{
	colorObject = "bgbutton";
	showModalDialog("../pages/ColorPickerDialog.htm",window,"status:no;scroll:no;help:no;dialogWidth:300px;dialogHeight:250px");
}
function fnSetColor()
{
	if (selColor != null && selColor != "")
	{
		if(colorObject =="background")
		{
			var tbBackColor = document.getElementById("tbBackColor");
			var divColor = document.getElementById("divColor");

			tbBackColor.value = selColor;
			divColor.style.backgroundColor = selColor;
		}
		if(colorObject =="font")
		{
			var tbFontColor = document.getElementById("tbFontColor");
			var divColorFont = document.getElementById("divColorFont");

			tbFontColor.value = selColor;
			divColorFont.style.backgroundColor = selColor;
		}
		if(colorObject =="button")
		{
			var tbColorButton = document.getElementById("tbColorButton");
			var divColorButton = document.getElementById("divColorButton");

			tbColorButton.value = selColor;
			divColorButton.style.backgroundColor = selColor;
		}
		if(colorObject =="bgbutton")
		{
			var tbColorBGButton = document.getElementById("tbColorBGButton");
			var divColorBGButton = document.getElementById("divColorBGButton");

			tbColorBGButton.value = selColor;
			divColorBGButton.style.backgroundColor = selColor;
		}
	}
}

	
/* ***************************
** Most of this code was kindly 
** provided by
** Andrew Clover (and at doxdesk dot com)
** http://and.doxdesk.com/ 
*/
function utf8(wide) {
  var c, s;
  var enc = "";
  var i = 0;
  while(i<wide.length) {
    c= wide.charCodeAt(i++);
    // handle UTF-16 surrogates
    if (c>=0xDC00 && c<0xE000) continue;
    if (c>=0xD800 && c<0xDC00) {
      if (i>=wide.length) continue;
      s= wide.charCodeAt(i++);
      if (s<0xDC00 || c>=0xDE00) continue;
      c= ((c-0xD800)<<10)+(s-0xDC00)+0x10000;
    }
    // output value
    if (c<0x80) enc += String.fromCharCode(c);
    else if (c<0x800) enc += String.fromCharCode(0xC0+(c>>6),0x80+(c&0x3F));
    else if (c<0x10000) enc += String.fromCharCode(0xE0+(c>>12),0x80+(c>>6&0x3F),0x80+(c&0x3F));
    else enc += String.fromCharCode(0xF0+(c>>18),0x80+(c>>12&0x3F),0x80+(c>>6&0x3F),0x80+(c&0x3F));
  }
  return enc;
}

var hexchars = "0123456789ABCDEF";

function toHex(n) {
  return hexchars.charAt(n>>4)+hexchars.charAt(n & 0xF);
}

var okURIchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

function encodeURIComponentNew(s) {
  var s = utf8(s);
  var c;
  var enc = "";
  for (var i= 0; i<s.length; i++) {
    if (okURIchars.indexOf(s.charAt(i))==-1)
      enc += "%"+toHex(s.charCodeAt(i));
    else
      enc += s.charAt(i);
  }
  return enc;
}

function buildURL(fld)
{
	if (fld == "") 
		return false;
	var encodedField = "";
	var s = fld.replace(/"/g, '\\"');
	
	if (typeof encodeURIComponent == "function")
	{
		// Use JavaScript built-in function
		// IE 5.5+ and Netscape 6+ and Mozilla
		encodedField = encodeURIComponent(s);
	}
	else 
	{
		// Need to mimic the JavaScript version
		// Netscape 4 and IE 4 and IE 5.0
		encodedField = encodeURIComponentNew(s);
	}

	return encodedField;
}

