var dofo = null;
var tmout_frmchngd = null;
var stopScroll = true;
var req; var tmp;
var scrollTel=0;

// written by Dean Edwards, 2005
// with input from Tino Zijdel - crisp@xs4all.nl
// http://dean.edwards.name/weblog/2005/10/add-event/
function addEvent(element, type, handler){
	if (element.addEventListener)
		element.addEventListener(type, handler, false);
	else{
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		if (!element.events) element.events = {};
		var handlers = element.events[type];
		if (!handlers){
			handlers = element.events[type] = {};
			if (element['on' + type]) handlers[0] = element['on' + type];
			element['on' + type] = handleEvent;
		}
		handlers[handler.$$guid] = handler;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler){
	if (element.removeEventListener)
		element.removeEventListener(type, handler, false);
	else if (element.events && element.events[type] && handler.$$guid)
		delete element.events[type][handler.$$guid];
}

function handleEvent(event){
	event = event || fixEvent(window.event);
	var returnValue = true;
	var handlers = this.events[event.type];

	for (var i in handlers)	{
		if (!Object.prototype[i]){
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) returnValue = false;
		}
	}

	if (this.$$handler) this.$$handler = null;

	return returnValue;
}

function fixEvent(event){
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function(){
	this.returnValue = false;
}
fixEvent.stopPropagation = function(){
	this.cancelBubble = true;
}

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener){
	document.onreadystatechange = function(){
		if (window.onload && window.onload != handleEvent){
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	}
}
// end addEvent() and friends

function form_changed(){
	clearTimeout(tmout_frmchngd);
	tmout_frmchngd = setTimeout("do_ajax()", 1200);
	if(stopScroll){
		stopScroll = false;
		scrollLetters();
	}
}

function do_ajax(){
	clearTimeout(tmout_frmchngd);
	openConn();
	if(req) {
		req.onreadystatechange = processReqChange;
		// alle form-waardes meesturen
		var formwaarden = getFormValues();
		req.open("POST", postVars_page, true);
		if(req.overrideMimeType){
			req.overrideMimeType('text/html');
		}
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-Length", formwaarden.length);
		req.setRequestHeader("Connection", "close");
		req.send(formwaarden);
	} else {
		// optionally do something else, because ajax doesn't work...
	}
}

// open xmlhttp connection.
function openConn(){
	req = false;
	if(typeof(XMLHttpRequest) != "undefined" && window.XMLHttpRequest) {// standards
		try { req = new XMLHttpRequest(); }
		catch(e) { req = false; }
	}
	if(window.ActiveXObject && !req) {// microsuf
		try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) {
			try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e) { req = false; }
		}
	}
}

// move the numbers, to give the counter a "gamble machine" look
function scrollLetters(){
	if(stopScroll) { return false; }
	var div = document.getElementById("vac_NRfound");
	for(var w=0;w<2;w++){// het 2e getal in deze string staat voor het max. aantal Bold-tags in de "vac_NRfound"-span.
		if(div.childNodes[w]) {
			div.childNodes[w].innerHTML = ++scrollTel % 10;
		}
	}
	setTimeout("scrollLetters()",100);
}

// handles the answer of the xmlhttp request
function processReqChange() {
	// only if req shows "loaded"
	if (req.readyState == 4) {
		// zorg dat de cijfers niet meer gescrolled worden
		stopScroll = true;
		document.getElementById("vac_NRfound").innerHTML = "<strong>&nbsp;</strong><strong>&nbsp;</strong>";
		
		var json = eval(req.responseText);
		
		// if not OK, do not give an alert, but set the counter to 'xxx'
		var tmp = (req.status == 200) ? String(json[0].counter) : 'ERR.';
		// als antwoord niet numeriek, dan is er iets fout
		if(isNaN(tmp) && tmp != 'ERR.'){
			tmp = "--";
		}
		var tmp2="";
		// zet nu om elk cijfer van de string een bold-tag
		for(var e=0;e<tmp.length;e++){
			tmp2 += "<strong>" + tmp.substr(e,1) + "</strong>";
		}
		document.getElementById("vac_NRfound").innerHTML = tmp2;
		
		for(var i = 0; i < json[0].standplaats.length; i++) 
		{
			if(document.getElementById('location_'+json[0].standplaats[i].divTitle))
			{
				if(json[0].standplaats[i].counter == 1)
				{
					document.getElementById('location_'+json[0].standplaats[i].divTitle).className = 'found';
					document.getElementById('location_'+json[0].standplaats[i].divTitle).title = 'Er is 1 vacature voor deze selectie in ' + json[0].standplaats[i].standplaats;
				}
				else if(json[0].standplaats[i].counter == 0)
				{
					document.getElementById('location_'+json[0].standplaats[i].divTitle).className = '';
					document.getElementById('location_'+json[0].standplaats[i].divTitle).title = 'Er zijn geen vacatures voor deze selectie in ' + json[0].standplaats[i].standplaats;
				} 
				else
				{	
					document.getElementById('location_'+json[0].standplaats[i].divTitle).className = 'found';
					document.getElementById('location_'+json[0].standplaats[i].divTitle).title = 'Er zijn ' + json[0].standplaats[i].counter + ' vacatures voor deze selectie in ' + json[0].standplaats[i].standplaats;	
				}
			}
		}
		
		if(!isNaN(tmp) && tmp == 0)
		{
			if(document.getElementById('btn_teller')){document.getElementById('btn_teller').disabled = 'disabled';}
			if(document.getElementById('btn_teller_stages')){document.getElementById('btn_teller_stages').disabled = 'disabled';}		
		}
		else
		{
			if(document.getElementById('btn_teller')){document.getElementById('btn_teller').disabled = false;}
			if(document.getElementById('btn_teller_stages')){document.getElementById('btn_teller_stages').disabled = false;}
		}
	}
}

// collect all the form variables available, and return them as a string
function getFormValues(){
	var dofo = document.getElementById("vacSearchForm");
	var getstr = ""; var valuenow; var tmp; var i;
	for (i=0; i<dofo.elements.length; i++) {
		tmp = dofo.elements[i];
		valuenow = "";
		if (tmp.tagName == "SELECT") {
			tmp2 = new Array();
			for(var tw=0;tw<tmp.options.length;tw++){
				if(tmp.options[tw].selected && tmp.options[tw].value != ''){
					tmp2[tmp2.length] = escape(tmp.options[tw].value);
				}
			}
			valuenow = tmp2.join(",");
		}
		else if (
			(tmp.type == "text") || (tmp.type == "hidden") ||
			(tmp.type == "checkbox" && tmp.checked) ||
			(tmp.type == "radio" && tmp.checked)
		) { valuenow = escape(tmp.value); }
		// only add to values if it is checked / selected
		if ((tmp.type != "checkbox" && tmp.type != "radio") || tmp.checked){
			getstr += tmp.name + "=" + valuenow + "&";
		}
	}
	return getstr;
}

// selects/deselects a given form element.
function setSelected(veldnaam, janee){
	var temp = dofo.elements[veldnaam];
	if(temp){
		if(temp.tagName != "SELECT"){
			temp.checked = janee;
		} else {
			if(!janee){// als alles uit moet worden gezet
				for(var i=0;i<temp.options.length;i++){
					temp.options[i].selected = false;
				}
			} else {
				for(var i=0;i<temp.options.length;i++){
					if(temp.options[i].selected){ return; }
				}
				var welkeSelected = (!isNaN(arguments[2])) ? arguments[2] : 0;
				temp.options[welkeSelected].selected = true;
			}
		}
	}
}

// check if we can create an xmlhttp instance. If so, make sure the "warning" message does not appear.
openConn();
if(req){
	document.write('<style type="text/css"><!--\r\n span.oldBrowser{ display:none; }--></style>');
// if no xmlhttp instance, give the counter a gray appearance.
} else {
	document.write('<style type="text/css"><!--\r\n span#vac_NRfound { color: #aaa; }--></style>');
}
