force_submit = false;

function init() {
	if (document.getElementById('nav')) {
		navItems = document.getElementById('nav').getElementsByTagName('li');
		
		for (i=0; i<navItems.length; i++) {
			if (navItems[i].parentNode.getAttribute('id') == "nav") {
				//alert(navItems[i].parentElement.id)
				if (navItems[i].addEventListener){
					navItems[i].addEventListener("mouseover", func_nav_mouseover, false);
					navItems[i].addEventListener("mouseout", func_nav_mouseout, false);
				} else if (navItems[i].attachEvent){
					navItems[i].onmouseover = func_nav_mouseover;
					navItems[i].onmouseout = func_nav_mouseout;
				}
			}
		}
	}
	
	if (window.extInit) {
		extInit();
	}
	
	if (document.forms['searchVacaturesForm'].search_normal) {
		
		getSearchResultCount();
	}
	
	if (document.forms['searchVacaturesForm'].verzamelgroep) {
		getVerzamelgroepSearchResultCount();
	}
}

var nav_timeout = "";
var nav_current = "";

function func_nav_mouseover() {
	if (nav_current != this.id && nav_current != "") {
		submenuID = nav_current.replace('nav_menu_','');
		
		if (document.getElementById(nav_current).className != "nav_li_selected") {
			document.getElementById(nav_current).className = "";
		}
		
		if (document.getElementById('nav_submenu_' + submenuID)) {
			document.getElementById('nav_submenu_' + submenuID).style.display = 'none';
		}
	}
	
	clearTimeout(nav_timeout);
	
	submenuID = this.id.replace('nav_menu_','');
	if (this.className != "nav_li_selected") {
		this.className = 'nav_li_mouseover';
	}
	
	if (document.getElementById('nav_submenu_' + submenuID)) {
		document.getElementById('nav_submenu_' + submenuID).style.display = 'block';
	}
}

function func_nav_mouseout() {
	nav_timeout = setTimeout(func_nav_hide,'500');
	nav_current = this.id;
}

function func_nav_hide() {
	submenuID = nav_current.replace('nav_menu_','');
	
	if (document.getElementById(nav_current).className != "nav_li_selected") {
		document.getElementById(nav_current).className = "";
	}
	
	if (document.getElementById('nav_submenu_' + submenuID)) {
		document.getElementById('nav_submenu_' + submenuID).style.display = 'none';
	}
}

var searchResultTimeout = "";
var googleTimeoutTrigger = 0;

function makeRequest(method,url) {
	
    var httpRequest;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) {
            httpRequest.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
           		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                    } catch (e) {}
            }
    }

    if (!httpRequest) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    
    httpRequest.onreadystatechange = function() { handleResponse(method,httpRequest); };
    httpRequest.open('GET', url, true);
    httpRequest.send(null);

}

function handleResponse(method,httpRequest) {
	
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
        	cleanWhitespace(httpRequest.responseXML);
        	if (method == "results") {
        		
        		var varAantalGevonden = httpRequest.responseXML.getElementsByTagName('aantalGevonden')[0].firstChild.nodeValue;
        		var varAantalMogelijkInteressant = httpRequest.responseXML.getElementsByTagName('aantalMogelijkInteressant')[0].firstChild.nodeValue;
        		
        		document.getElementById('aantalGevonden').innerHTML = "Gevonden vacatures: " + varAantalGevonden;
        		document.getElementById('aantalMogelijkInteressant').innerHTML = "Gerelateerde vacatures: " + varAantalMogelijkInteressant;
        	} else if (method == "searchuri") {
        		var search_uri = httpRequest.responseXML.getElementsByTagName('search_uri')[0].firstChild.nodeValue;
        		
        		document.location = search_uri;
        	}
        } else {
            alert('There was a problem with the request.');
        }
    }

}

function cleanWhitespace(node) {
	for (var x = 0; x < node.childNodes.length; x++) {
		var childNode = node.childNodes[x]
		if ((childNode.nodeType == 3)&&(!/\S/.test(childNode.nodeValue))) {
				node.removeChild(node.childNodes[x]);
				x--;
		}
	    if (childNode.nodeType == 1) {
			cleanWhitespace(childNode);
		}
	}
}

function getSearchResultCount() {
	clearTimeout(searchResultTimeout);
	
	if (document.getElementById('aantalGevonden')) {
		f = document.forms['searchVacaturesForm'];
		
		organisaties = '';
		
		for (var i=f.bedrijf.options.length-1; i >= 0;i--) {
			if (f.bedrijf.options[i].selected) {
				if (f.bedrijf.options[i].value != "") {
					organisaties = organisaties + (organisaties == "" ? "" : ",") + f.bedrijf.options[i].value;
				}
			}
		}
		
		functiegroepen = '';
		
		for (var i=f.functiegroep.options.length-1; i >= 0;i--) {
			if (f.functiegroep.options[i].selected) {
				if (f.functiegroep.options[i].value != "") {
					functiegroepen = functiegroepen + (functiegroepen == "" ? "" : ",") + f.functiegroep.options[i].value;
				}
			}
		}
		
		opleidingen = '';
		
		for (var i=f.opleiding.options.length-1; i >= 0;i--) {
			if (f.opleiding.options[i].selected) {
				if (f.opleiding.options[i].value != "") {
					opleidingen = opleidingen + (opleidingen == "" ? "" : ",") + f.opleiding.options[i].value;
				}
			}
		}
		
		urenperweek = '';
		
		for (var i=f.uren_per_week.options.length-1; i >= 0;i--) {
			if (f.uren_per_week.options[i].selected) {
				if (f.uren_per_week.options[i].value != "") {
					urenperweek = urenperweek + (urenperweek == "" ? "" : ",") + f.uren_per_week.options[i].value;
				}
			}
		}
		
		straal = f.straal.options[f.straal.selectedIndex].value;
	
		woonplaats = escape(f.woonplaats.value);
	
		if (woonplaats != "" && straal > 0 && googleTimeoutTrigger == 0 && f.current_plaats.value != f.woonplaats.value) {
			f.current_plaats.value = f.woonplaats.value;
			getGoogleCoords();
			googleTimeoutTrigger = 1;
			searchResultTimeout = setTimeout('getSearchResultCount()',1000);
		} else {
			googleTimeoutTrigger = 0;
			
			latitude = escape(f.latitude.value);
			longitude = escape(f.longitude.value);
		
			var datum = new Date();
			var tijd = datum.getTime();
			
			//document.write('/xml/xml.search_result_count.php?organisaties=' + organisaties + '&functiegroepen=' + functiegroepen + '&opleidingen=' + opleidingen + '&urenperweek=' + urenperweek + '&straal=' + straal + '&woonplaats=' + woonplaats + '&latitude=' + latitude + '&longitude=' + longitude + '&ts=' + tijd);
			makeRequest('results','/xml/xml.search_result_count.php?organisaties=' + organisaties + '&functiegroepen=' + functiegroepen + '&opleidingen=' + opleidingen + '&urenperweek=' + urenperweek + '&straal=' + straal + '&woonplaats=' + woonplaats + '&latitude=' + latitude + '&longitude=' + longitude + '&ts=' + tijd);
			//alert('/xml/xml.search_result_count.php?organisaties=' + organisaties + '&functiegroepen=' + functiegroepen + '&opleidingen=' + opleidingen + '&urenperweek=' + urenperweek + '&straal=' + straal + '&woonplaats=' + woonplaats + '&latitude=' + latitude + '&longitude=' + longitude + '&ts=' + tijd);
		}
	}
}

function getDelayedSearchResultCount() {
	clearTimeout(searchResultTimeout);
	searchResultTimeout = setTimeout('getSearchResultCount()',2000);
}

function getVerzamelgroepSearchResultCount() {
	clearTimeout(searchResultTimeout);
	
	if (document.getElementById('aantalGevonden')) {
		f = document.forms['searchVacaturesForm'];
		
		organisaties = '';
		
		for (var i=f.bedrijf.options.length-1; i >= 0;i--) {
			if (f.bedrijf.options[i].selected) {
				if (f.bedrijf.options[i].value != "") {
					organisaties = organisaties + (organisaties == "" ? "" : ",") + f.bedrijf.options[i].value;
				}
			}
		}
		
		straal = f.straal.options[f.straal.selectedIndex].value;
	
		woonplaats = escape(f.woonplaats.value);
	
		if (woonplaats != "" && straal > 0 && googleTimeoutTrigger == 0 && f.current_plaats.value != f.woonplaats.value) {
			f.current_plaats.value = f.woonplaats.value;
			getGoogleCoords();
			googleTimeoutTrigger = 1;
			searchResultTimeout = setTimeout('getVerzamelgroepSearchResultCount()',1000);
		} else {
			googleTimeoutTrigger = 0;
			
			latitude = escape(f.latitude.value);
			longitude = escape(f.longitude.value);
		
			var datum = new Date();
			var tijd = datum.getTime();
			//document.write('/xml/xml.search_result_count.php?organisaties=' + organisaties + '&functiegroepen=' + functiegroepen + '&opleidingen=' + opleidingen + '&urenperweek=' + urenperweek + '&straal=' + straal + '&woonplaats=' + woonplaats + '&latitude=' + latitude + '&longitude=' + longitude + '&ts=' + tijd);
			//alert('/xml/xml.search_result_count_verzamelgroep.php?verzamelgroep=' + f.verzamelgroep_id.value + '&organisaties=' + organisaties + '&straal=' + straal + '&woonplaats=' + woonplaats + '&latitude=' + latitude + '&longitude=' + longitude + '&ts=' + tijd);
			makeRequest('results','/xml/xml.search_result_count_verzamelgroep.php?verzamelgroep=' + f.verzamelgroep_id.value + '&organisaties=' + organisaties + '&straal=' + straal + '&woonplaats=' + woonplaats + '&latitude=' + latitude + '&longitude=' + longitude + '&ts=' + tijd);
		}
	}
}

function getVerzamelgroepDelayedSearchResultCount() {
	clearTimeout(searchResultTimeout);
	searchResultTimeout = setTimeout('getVerzamelgroepSearchResultCount()',2000);
}

function getGoogleCoords() {
	f = document.forms['searchVacaturesForm'];
	
	var geo = new GClientGeocoder();
	
	if (f.woonplaats.value != "") {
		adres = f.woonplaats.value + ", Netherlands"
		
		geo.getLocations(
			adres,
			function(result) {
				if (result.Status.code == G_GEO_SUCCESS) {
					f.latitude.value = result.Placemark[0].Point.coordinates[1];
					f.longitude.value = result.Placemark[0].Point.coordinates[0];
				} else {
					f.latitude.value = "";
					f.longitude.value = "";
					return false;
				}
			}
		);
	}
	return false;
}
/*
function preSubmitVacatureZoekenNormal() {
	f = document.forms['searchVacaturesForm'];

	getGoogleCoords();
	
	organisaties = '';
	
	for (var i=f.bedrijf.options.length-1; i >= 0;i--) {
		if (f.bedrijf.options[i].selected) {
			if (f.bedrijf.options[i].value != "") {
				organisaties = organisaties + (organisaties == "" ? "" : ",") + f.bedrijf.options[i].value;
			}
		}
	}
	
	if (organisaties == "" || !zoekresultaat[organisaties]) {
		f.action = zoekresultaathoofdpagina
	} else {
		f.action = zoekresultaat[organisaties];
	}
	
	return true
}
*/

function preSubmitVacatureZoekenNormal() {
	f = document.forms['searchVacaturesForm'];
	
	woonplaats = f.woonplaats.value;
	straal = f.straal.options[f.straal.selectedIndex].value;
	organisaties = '';
	functiegroepen = '';
	opleidingsniveaus = '';
	uren = '';
	
	if (document.getElementById('toon_resultaat_optie_2').checked) {
		tonen_als = "kaart";
	} else {
		tonen_als = 'lijst';
	}
	
	
	for (var i=f.bedrijf.options.length-1; i >= 0;i--) {
		if (f.bedrijf.options[i].selected) {
			if (f.bedrijf.options[i].value != "") {
				organisaties = organisaties + (organisaties == "" ? "" : ",") + f.bedrijf.options[i].value;
			}
		}
	}
	
	for (var i=f.functiegroep.options.length-1; i >= 0;i--) {
		if (f.functiegroep.options[i].selected) {
			if (f.functiegroep.options[i].value != "") {
				functiegroepen = functiegroepen + (functiegroepen == "" ? "" : ",") + f.functiegroep.options[i].value;
			}
		}
	}
	
	for (var i=f.opleiding.options.length-1; i >= 0;i--) {
		if (f.opleiding.options[i].selected) {
			if (f.opleiding.options[i].value != "") {
				opleidingsniveaus = opleidingsniveaus + (opleidingsniveaus == "" ? "" : ",") + f.opleiding.options[i].value;
			}
		}
	}
	
	for (var i=f.uren_per_week.options.length-1; i >= 0;i--) {
		if (f.uren_per_week.options[i].selected) {
			if (f.uren_per_week.options[i].value != "") {
				uren = uren + (uren == "" ? "" : ",") + f.uren_per_week.options[i].value;
			}
		}
	}
	
	var datum = new Date();
	var tijd = datum.getTime();
			
	makeRequest('searchuri','/xml/xml.search_uri.php?tonen_als=' + tonen_als + '&organisaties=' + organisaties + '&straal=' + straal + '&woonplaats=' + woonplaats + '&functiegroepen=' + functiegroepen + '&opleidingsniveaus=' + opleidingsniveaus + '&uren=' + uren + '&ts=' + tijd);
	
}

function preSubmitVacatureZoekenTrefwoord(organisatie) {
	f = document.forms['searchVacaturesForm'];
	
	if (!zoekresultaat[organisatie]) {
		f.action = zoekresultaathoofdpagina;
	} else {
		f.action = zoekresultaat[organisatie];
	}
	
	if (f.trefwoord_input.value == "") {
		alert("Je hebt nog geen trefwoord of vacaturenummer ingevuld.");
		f.trefwoord_input.focus();
		return false;
	} else {
		return true;
	}
}

function get_clean_uri() {
	current_uri = document.location.href;
	
	if (/\?/.test(current_uri)) {
		splitted_uri = current_uri.split("?");
		current_uri = splitted_uri[0];
	}
	
	return current_uri;
}

function preSubmitVacatureZoekenBladeren(organisatie) {
	
	f = document.forms['searchVacaturesForm'];
	if (!zoekresultaat[organisatie]) {
		f.action = zoekresultaathoofdpagina;
	} else {
		f.action = zoekresultaat[organisatie];
	}
	return true;
}

function preSubmitVacatureZoekenVerzamelgroep(organisatie) {
	f = document.forms['searchVacaturesForm'];
	
	if (force_submit) {
		return true;
	} else {
		if (f.woonplaats != undefined) {
			organisaties = '';
	
			for (var i=f.bedrijf.options.length-1; i >= 0;i--) {
				if (f.bedrijf.options[i].selected) {
					if (f.bedrijf.options[i].value != "") {
						organisaties = organisaties + (organisaties == "" ? "" : ",") + f.bedrijf.options[i].value;
					}
				}
			}
			
			if (organisaties == "" || !zoekresultaat[organisaties]) {
				f.action = zoekresultaathoofdpagina
			} else {
				f.action = zoekresultaat[organisaties];
			}
			
			f = document.forms['searchVacaturesForm'];
			
			getGoogleCoords();
			
			organisaties = '';
			
			for (var i=f.bedrijf.options.length-1; i >= 0;i--) {
				if (f.bedrijf.options[i].selected) {
					if (f.bedrijf.options[i].value != "") {
						organisaties = organisaties + (organisaties == "" ? "" : "-") + f.bedrijf.options[i].value;
					}
				}
			}
		
			f.organisaties.value = organisaties;
						
			setTimeout("submitform()",1000);
			return false;
		} else {
			f.action = zoekresultaat[organisatie];
			
			return true;
		}
	}
}

function submitform() {
	force_submit = true;
	f = document.forms['searchVacaturesForm'];
	f.submit();
}
