function urlencode (str) {
    // URL-encodes string
    //
    // version: 910.813
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +      reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Joris
    // %          note 1: This reflects PHP 5.3/6.0+ behavior
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
    var hexStr = function (dec) {
        return '%' + (dec < 16 ? '0' : '') + dec.toString(16).toUpperCase();
    };

    var ret = '',
            unreserved = /[\w.\-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();

    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters); https://developer.mozilla.org/index.php?title=en/Core_JavaScript_1.5_Reference/Global_Objects/String/charCodeAt
                ret += ((code - 0xD800) * 0x400) + (str.charCodeAt(i+1) - 0xDC00) + 0x10000;
                i++; // skip the next one as we just retrieved it as a low surrogate
            }
            // We never come across a low surrogate because we skip them, unless invalid
            // Reserved assumed to be in UTF-8, as in PHP
            else if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048) { // 3 bytes (code < 65536)
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}

function loopSelected(id)
{
  var qryStr = "";
  var selectedStr = "";
  var thisid = "location"+id;
  var selObj = document.getElementById(thisid);
  var i;
	if (selObj !== null) {
		for (i=0; i<selObj.options.length; i++) {
			if (selObj.options[i].selected) {
				if (selectedStr==="") {
					selectedStr = selObj.options[i].value;
				} else {
					selectedStr = selectedStr+"-"+selObj.options[i].value;
				}
			}
		}
		qryStr = id+"="+selectedStr;
	}
	return qryStr;
  }

function loopAll() {
	var j;
	var totalStr = "";
	var location = "";
	var arr = [96,97,98,102,104,106,107,109];
	for (j in arr) {
	if (totalStr==="") {
		totalStr = loopSelected(arr[j]);
	} else {
		totalStr = totalStr+"&"+loopSelected(arr[j]);
	}
	}

	location = "action=search&submitted=1&order_by=&ord=&2="+urlencode(document.getElementById('functie').value)+"&15="+urlencode(document.getElementById('15').value)+"&"+totalStr;

	if (document.getElementById('15').value !== "") {
		location = location+'&15='+document.getElementById('15').value.toLowerCase();
	}
	if (document.getElementById('afstand').value !== "") {
		location = location+'&afstand='+document.getElementById('afstand').value;
	}
	if (document.getElementById('user_id').value !== "") {
		location = location+"&user_id="+document.getElementById('user_id').value;
	} else if (document.getElementById('bedrijf').value !== "") {
		location = location+"&user_id="+document.getElementById('bedrijf').value;
	}
	window.location.href="/index.php?"+location;
}
/*
function checkAll(id) {
    var collection = document.getElementById(id).getElementsByTagName('INPUT');
    for (var x=0; x<collection.length; x++) {
        if (collection[x].type.toUpperCase()=='CHECKBOX') {
            if (collection[x].checked == false) {
				collection[x].checked = true;
			} else {
				collection[x].checked = false;
			}
		}
	}
}
*/
function loopChecked(id)
{
	var qryStr = "";
	var selectedStr = "";
    var collection = document.getElementById(id).getElementsByTagName('INPUT');
    for (var x=0; x<collection.length; x++) {
        if (collection[x].type.toUpperCase()=='CHECKBOX') {
            if (collection[x].checked == true) {
				if (selectedStr==="") {
					selectedStr = collection[x].value;
				} else {
					selectedStr = selectedStr+"-"+collection[x].value;
				}
			}
		}
	}

	if (selectedStr !== "") {qryStr = id.substring(1)+"="+selectedStr;}
	return qryStr;
}

function loopAllChecked() {
	var j;
	var id;
	var totalStr = "";
	var location = "";
	var arr = ["a97","a98","a102","a104","a106","a107","agroupid1","agroupid2"];
	for (j in arr) {
		if (totalStr==="") {
			totalStr = loopChecked(arr[j]);
		} else {
			if (loopChecked(arr[j]) !== "") {
				totalStr = totalStr+"&"+loopChecked(arr[j]);
			}
		}
		//alert(arr[j]+" - "+loopChecked(arr[j]));
	}

	// location = "action=search&submitted=1&order_by=&ord=&2="+urlencode(document.getElementById('functie').value)+"&15="+urlencode(document.getElementById('15').value)+"&"+totalStr;
	if (totalStr !== "") {
		location = "action=search&submitted=1&"+totalStr;
	} else {
		location = "action=search&submitted=1";
	}

	if (document.getElementById('functie').value !== "") {
		location = location+'&2='+urlencode(document.getElementById('functie').value).toLowerCase();
	}
	if (document.getElementById('15').value !== "") {
		location = location+'&15='+document.getElementById('15').value.toLowerCase();
	}
	if (document.getElementById('afstand').value !== "") {
		location = location+'&afstand='+document.getElementById('afstand').value;
	}
	if (document.getElementById('user_id').value !== "") {
		location = location+"&user_id="+document.getElementById('user_id').value;
	} else if (document.getElementById('bedrijf').value !== "") {
		location = location+"&user_id="+document.getElementById('bedrijf').value;
	}
	//alert(location);
	window.location.href="/index.php?"+location;
}

function checkEnter(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		//loopAll(); //submit the form
		loopAllChecked();
		return false;
	} else {
		return true;
	}
}

// MILLIAN
function loopSelectedMillian(str)
{
  var qryStr = "";
  var selectedStr = "";
  var selObj = document.getElementById(str);
  var i;
	if (selObj !== null) {
		for (i=0; i<selObj.options.length; i++) {
			if (selObj.options[i].selected) {
				if (selectedStr==="") {
					selectedStr = selObj.options[i].value;
				} else {
					selectedStr = selectedStr+"-"+selObj.options[i].value;
				}
			}
		}
		qryStr = str+"="+selectedStr;
	}
	return qryStr;
  }

function loopMillian() {
  var j;
  var totalStr = "";
  var location = "";
  var arr = ["handboek","vakgebied","provincie","opleidingsniveau"];
  for (j in arr) {
	if (totalStr==="") {
		totalStr = loopSelectedMillian(arr[j]);
	} else {
		totalStr = totalStr+"&"+loopSelectedMillian(arr[j]);
	}
  }
  if (document.getElementById('trefwoord').value !== "") {
	totalStr = totalStr+"&trefwoord="+urlencode(document.getElementById('trefwoord').value);
  }
  location = "/uitgebreidzoeken_millian.php?action=search&"+totalStr;
  window.location.href=location;
}

function checkEnterMillian(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		loopMillian(); //submit the form
		return false;
	} else {
		return true;
	}
}

// MILLIAN

function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
	    if (sVars=="") {
			xmlhttp.open(sMethod, sURL, true);
		} else {
			xmlhttp.open(sMethod, sURL+"?"+sVars, true);
		}
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
	  xmlhttp.setRequestHeader("Connection", "close");	// willy
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function insertMe() {
	var myConn = new XHConn();
	if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
	var currenturl = self.location.href.replace(/&/g, "%26");
	var thisurl = this.href.replace(/&/g, "%26");
	var fnWhenDone = function (oXML) { };
	//myConn.connect("/willy/insertme.php", "GET", "url="+thisurl+"&current="+currenturl, fnWhenDone);
	myConn.connect("/willy/insertme.php", "GET", encodeURI("url="+thisurl+"&current="+currenturl), fnWhenDone);
}

function submitMe() {
	var myConn = new XHConn();
	if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.");
	var currenturl = location.href.replace(/&/g, "%26");
	var thisurl = "Zoek-opdracht"
	var fnWhenDone = function (oXML) { };
	if (document.forms[0]) {
		var thisid = "";
		if (document.forms[0].trefwoord) {
			thisid = document.getElementById('trefwoord').value;
		}
		if (document.forms[0].search_term) {
			thisid = document.getElementById('search_term').value;
		}
		myConn.connect("/willy/insertme.php", "GET", "url=gezocht%20naar:%20"+escape(thisid)+"&current="+escape(currenturl), fnWhenDone);
	}
}

function addEvent(obj,evType,fn){
 var r = false;
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    r = true;
  }
  else if (obj.attachEvent) {
    var id = obj.sourceIndex || -1;

    if (!fn[evType + id]) {
      var f = fn[evType + id] = function(e) {
        var o = document.all[id] || document;
        o._f = fn;
        var s = o._f(e);
        o._f = null;
        return s;
      };

      r = obj.attachEvent("on" + evType, f);
      obj = null;
    }
  }
  return r;
};

function removeEvent(obj, evType, fn){
  var r = false;
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, false);
    r = true;
  } else if (obj.detachEvent) {
    r = obj.detachEvent("on" + evType, fn[evType + (obj.sourceIndex || -1)]);
  }
  return r;
};


window.onload=function addthem() {
	var el = document.getElementsByTagName('a');
	for(var i=0 ; i<el.length ; i++ ) {
		addEvent(el[i],"click",insertMe);
		if (el[i].toString().substring(0,4)=="http" && (el[i].toString().split(/\/+/g)[1] != "nuwerk.nl"  && el[i].toString().split(/\/+/g)[1] != "desktop" && el[i].toString().split(/\/+/g)[1] != "laptop" && el[i].toString().split(/\/+/g)[1] != "www.nuwerk.nl" && el[i].toString().split(/\/+/g)[1] != "vacatures.acceptatie.nuwerk.nl")) {
			el[i].setAttribute('target', '_blank');
		}
	}
	if (document.forms[0]) {
		if (document.forms[0].trefwoord || document.forms[0].search_term) {
			addEvent(document.forms[0],"submit",submitMe);
		}
	}
};

// Bannerbeheer


function loopSelected3(id)
{
  var qryStr = "";
  var selectedStr = "";
  var thisid = "location"+id;
  var selObj = document.getElementById(thisid);
  var i;
	if (selObj !== null) {
		for (i=0; i<selObj.options.length; i++) {
			if (selObj.options[i].selected) {
				if (selectedStr==="") {
					selectedStr = selObj.options[i].value;
				} else {
					selectedStr = selectedStr+"-"+selObj.options[i].value;
				}
			}
		}
		qryStr = id+"="+selectedStr;
	}
	return qryStr;
  }

function loopAll3() {
  var j;
  var totalStr = "";
  var location = "";
  var arr = [96,97,98,102,104,106,107,109];
  for (j in arr) {
	if (totalStr==="") {
		totalStr = loopSelected(arr[j]);
	} else {
		totalStr = totalStr+"&"+loopSelected(arr[j]);
	}
  }

  location = "action2=stap2&action=search&submitted=1&order_by=&ord=&bannerid="+urlencode(document.getElementById('bannerid').value)+"&content_id="+urlencode(document.getElementById('content_id').value)+"&bannerposition="+urlencode(document.getElementById('bannerposition').value)+"&2="+urlencode(document.getElementById('functie').value)+"&title="+urlencode(document.getElementById('title').value)+"&15="+urlencode(document.getElementById('15').value)+"&"+totalStr;

  if (document.getElementById('user_id').value !== "") {
	location = location+"&user_id="+document.getElementById('user_id').value;
  } else if (document.getElementById('bedrijf').value !== "") {
	location = location+"&user_id="+document.getElementById('bedrijf').value;
  }

  window.location.href="/werkgevers/bannerbeheertool/banner_bannertype_contentchange.php?"+location;

}

function checkEnter3(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
		e = event;
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		loopAll3(); //submit the form
		return false;
	} else {
		return true;
	}
}

function reFillSearchBox(field) {
	var str = "";
	var str0 = "";
	if (field=="15") {str0 = "Plaats of postcode..."; }
	if (field=="trefwoord") {str0 = "Vul een trefwoord in..."; }
	str = document.getElementById(field).value;
	if (str !== str0 && str.length==0) {
		document.getElementById(field).value = str0;
		document.getElementById(field).style.color  = "#888";
	}
}

function putItInBox(field,val) {
	document.getElementById(field).value=val.replace(/\+/g, ' ');
	document.getElementById(field).focus();
	document.getElementById(field).style.color='#000';
}

function putItInBox0(val) {
	document.forms[0].trefwoord.value=val.replace(/\+/g, ' ');
	document.forms[0].trefwoord.focus();
	document.forms[0].trefwoord.style.color='#000';
}

function include(arr, obj) { // test of item (obj) in javascript array voorkomt (arr)
  for(var i=0; i<arr.length; i++) {
	if (arr[i] == obj) return true;
  }
}

