var baseURL = 'http://www.zoekmijnbaan.nl/zmb/';

var PHOTO_SHOW_RECRUITERS = 1;
var PHOTO_SHOW_NETWORK = 2;
var PHOTO_SHOW_ALL = 4;

var baseValuesXML;

function addEvent(elem, eventName, func) {
	if (elem.attachEvent) {
		elem.attachEvent('on' + eventName, func);
	} else if(elem.addEventListener) {
		elem.addEventListener(eventName, func, false); 
	} 
}

// Checks available cookies for an old session and revives it if necessary

function checkExistingSession() {

	var loggedinCookie = getCookie('zoekmijnbaan_loggedin');
	var persistentCookie = getCookie('zoekmijnbaan_persistent_cookie');

	// alert(loggedinCookie);
	// alert(persistentCookie);

	if (loggedinCookie != null && loggedinCookie != 'none') {
		loggedIn = true;
	} else if (loggedinCookie == null && persistentCookie != null && persistentCookie != 'none') {
		var url = baseURL + 'revivesession?rnd=' + Math.random();

		var httpRequest = openHttpRequest("GET", url, true);

		httpRequest.setRequestHeader('Connection', 'close');
		httpRequest.onreadystatechange = function() {processReviveSession(httpRequest); } ;
		httpRequest.send(null);
	}

}

function processReviveSession(httpRequest) {

	if (httpRequest.readyState == 4) {
		if(httpRequest.status == 200) {

			if (httpRequest.responseXML.getElementsByTagName('result')[0].getAttribute('status') == 'success') {
				loggedIn = true;
			}
		} 
	}
}

// Sets community link for current account

function setCommunityLink(communityName, link) {

	var url = baseURL + 'setcommunitylink?community=' + escape(communityName) + '&link=' + escape(link);

	var httpRequest = openHttpRequest("GET", url, true);
	httpRequest.setRequestHeader('Connection', 'close');
	httpRequest.send(null);

	if (communityName == 'hyves') {
		url = baseURL + 'gethyvesinfo?hyvesurl=' + escape(link);
		url += '&rnd=' + Math.random();

		httpRequest = openHttpRequest("GET", url, true);
		httpRequest.setRequestHeader('Connection', 'close');
		httpRequest.onreadystatechange = function() {communityLinkDone(httpRequest); } ;
		httpRequest.send(null);
	} else if (communityName == 'linkedin') {
		url = baseURL + 'getlinkedininfo?linkedinurl=' + escape(link);
		url += '&rnd=' + Math.random();

		httpRequest = openHttpRequest("GET", url, true);
		httpRequest.setRequestHeader('Connection', 'close');
		httpRequest.onreadystatechange = function() {communityLinkDone(httpRequest); } ;
		httpRequest.send(null);
	} else if (communityName == 'blog') {
		alert('Bedankt voor het invoeren van jouw webpagina of blog. We hebben de URL verwerkt.');
	}

}

function communityLinkDone(httpRequest) {
	if (httpRequest.readyState == 4) {
		if(httpRequest.status == 200) {
			closeWindow();
		}
	}
}

// Functions for submitting the vacancy search form

function checkVacancySearchPostcode() {

	var postcode = $('idTxtPostCode').value;
	var brancheID = -1; 
	var query = $('idTxtInputQuery').value;

	if (postcode.replace(/^\s+|\s+$/g, '') == '' && brancheID == -1 && query.replace(/^\s+|\s+$/g, '') == '') {
		var innerHTML = new Array();

		var browserVersion = navigator.appVersion;
		var left = (getWindowWidth() / 2) - 215;

		if (browserVersion.indexOf('MSIE 4.') != -1 || browserVersion.indexOf('MSIE 6.') != -1) {
			var top = document.documentElement.scrollTop + 100;
			if (top < 220) top = 220;

			innerHTML.push('<div id="div_unknown_postcode" style="padding: 10px; text-align: center; position: absolute; top:' + top + 'px; z-index: 1000; width: 430px; left: 285px; background: #ffffff; padding: 0px; border: 1px; border-style: solid; border-color: #6699cc; font-family: Arial, Helvetica, sans-serif; font-size: 11px; left: ' + left + 'px">');
		} else {
			$('over').style.display = '';
			innerHTML.push('<div id="div_unknown_postcode" style="padding: 10px; text-align: center; position: fixed; top:100px; z-index: 1000; width: 430px; left: 285px; background: #ffffff; padding: 0px; border: 1px; border-style: solid; border-color: #6699cc; font-family: Arial, Helvetica, sans-serif; font-size: 11px; left: ' + left + 'px;">');
		}

		innerHTML.push('	<div style="width: 430px; clear: both; height: 22px; border-bottom: 1px solid #d2d2d2;">');
		innerHTML.push('		<div style="font-size: 15px; font-weight: bold; color: #4c4d4f; text-align: left; padding-top: 3px; float: left; clear: none;">')
		innerHTML.push('			Hmm... Iemand van weinig woorden?');
		innerHTML.push('		</div>');
		innerHTML.push('		<div style="float: right; clear: right;">');
		innerHTML.push('			<img src="images/popup_logo.gif"/>');
		innerHTML.push('			<img src="images/button_close_popup.gif" style="cursor: pointer" onClick="hideUnknownPostcodeDiv()">');
		innerHTML.push('		</div>');
		innerHTML.push('	</div>');
		innerHTML.push('	<div style="text-align: left; clear: both; font-size: 12px; width: 420px; color: #4c4c4c; margin-top: 10px; margin-bottom: 15px;">');
		innerHTML.push('		<br/>');
		innerHTML.push('		Sorry, maar we kunnen niet alle banen laten zien. Dat zou het systeem enorm traag');
		innerHTML.push('		maken voor andere gebruikers.<br/>');
		innerHTML.push('		<br/>');
		innerHTML.push('		Vul minstens &eacute;&eacute;n trefwoord en/of een plaats cq postcode in.');
		innerHTML.push('		We konden de door jou ingevoerde plaats niet vinden. Hoewel je vast goed kunt spellen,');
		innerHTML.push('		is het toch goed om te controleren of je geen vergissing hebt gemaakt.<br/>');
		innerHTML.push('		<br/>');
		innerHTML.push('		Het is natuulijk ook mogelijk dat wij ergens een fout hebben gemaakt.');
		innerHTML.push('	</div>');
		innerHTML.push('</div>');

		window.document.body.innerHTML += innerHTML.join('');
	} else {
		if (postcode == '' || isNumeric(postcode.substring(0,4))) {
			$('form_vacancy_search').submit();
		} else {
			$('input_searchlocation').value = postcode;

			var url = baseURL + 'findcity?name=' + escape(postcode);

	                var httpRequest = openHttpRequest("GET", url, true);

        	        httpRequest.setRequestHeader('Connection', 'close');
                	httpRequest.onreadystatechange = function() {processFindCityResponse(httpRequest); } ;

	                httpRequest.send(null);
		}
	}
}

function processFindCityResponse(httpRequest) {

	if (httpRequest.readyState == 4) {

		if(httpRequest.status == 200) {
			var objXMLResponse = httpRequest.responseXML;

			if (objXMLResponse.getElementsByTagName('postcode').length == 0) {

				if ($('div_unknown_postcode') != null) {
					$('div_unknown_postcode').style.display = '';

					if (!(browserVersion.indexOf('MSIE 4.') != -1 || browserVersion.indexOf('MSIE 6.') != -1)) {
						$('over').style.display = '';
					}
				} else {
					var innerHTML = new Array();

					var browserVersion = navigator.appVersion;
					var left = (getWindowWidth() / 2) - 215;

					if (browserVersion.indexOf('MSIE 4.') != -1 || browserVersion.indexOf('MSIE 6.') != -1) {
						var top = document.documentElement.scrollTop + 100;
						if (top < 220) top = 220;

						innerHTML.push('<div id="div_unknown_postcode" style="padding: 10px; text-align: center; position: absolute; top:' + top + 'px; z-index: 1000; width: 430px; left: 285px; background: #ffffff; padding: 0px; border: 1px; border-style: solid; border-color: #a9218e; font-family: Arial, Helvetica, sans-serif; font-size: 11px; left: ' + left + 'px;">');
					} else {
						$('over').style.display = '';
						innerHTML.push('<div id="div_unknown_postcode" style="padding: 10px; text-align: center; position: fixed; top:100px; z-index: 1000; width: 430px; left: 285px; background: #ffffff; padding: 0px; border: 1px; border-style: solid; border-color: #a9218e; font-family: Arial, Helvetica, sans-serif; font-size: 11px; left: ' + left + 'px;">');
					}

	 				innerHTML.push('	<div style="width: 430px; clear: both; height: 22px; border-bottom: 1px solid #d2d2d2;">');
					innerHTML.push('		<div style="font-size: 15px; font-weight: bold; color: #4c4d4f; text-align: left; padding-top: 3px; float: left; clear: none;">')
					innerHTML.push('			Hmm... die plaats kennen we niet');
					innerHTML.push('		</div>');
					innerHTML.push('		<div style="float: right; clear: right;">');
					innerHTML.push('			<img src="images/popup_logo.gif"/>');
					innerHTML.push('			<img src="images/button_close_popup.gif" style="cursor: pointer" onClick="hideUnknownPostcodeDiv()">');
					innerHTML.push('		</div>');
					innerHTML.push('	</div>');
					innerHTML.push('	<div style="text-align: left; clear: both; font-size: 12px; width: 420px; color: #4c4c4c; margin-top: 10px; margin-bottom: 15px;">');
					innerHTML.push('		<br/>');
					innerHTML.push('		We konden de door jou ingevoerde plaats niet vinden. Hoewel je vast goed kunt spellen,');
					innerHTML.push('		is het toch goed om te controleren of je geen vergissing hebt gemaakt.<br/>');
					innerHTML.push('		<br/>');
					innerHTML.push('		Het is natuulijk ook mogelijk dat wij ergens een fout hebben gemaakt.');
					innerHTML.push('	</div>');
					innerHTML.push('</div>');

					window.document.body.innerHTML += innerHTML.join('');
				}

			} else if (objXMLResponse.getElementsByTagName('postcode').length == 1) {
				var postcodeXML = objXMLResponse.getElementsByTagName('postcode')[0];
				setVacancySearchPostcode(postcodeXML.getAttribute('pc4code'), postcodeXML.getAttribute('pc4naam'));
			} else if (objXMLResponse.getElementsByTagName('postcode').length > 1) {
				var postcodesXML = objXMLResponse.getElementsByTagName('postcode');

				var innerHTML = new Array();

				// <div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>
				// <div id="over" style="display:none;"></div>

				var browserVersion = navigator.appVersion;
				var left = (getWindowWidth() / 2) - 215;

				if (browserVersion.indexOf('MSIE 4.') != -1 || browserVersion.indexOf('MSIE 6.') != -1) {
					var top = document.documentElement.scrollTop + 100;
					if (top < 220) top = 220;

					innerHTML.push('<div id="div_select_city" style="padding: 10px; text-align: center; position: absolute; top:' + top + 'px; z-index: 1000; width: 430px; left: 285px; background: #ffffff; padding: 0px; border: 1px; border-style: solid; border-color: #a9218e; font-family: Arial, Helvetica, sans-serif; font-size: 11px; left: ' + left + 'px;">');
				} else {
					$('over').style.display = '';
					innerHTML.push('<div id="div_select_city" style="padding: 10px; text-align: center; position: fixed; top:100px; z-index: 1000; width: 430px; left: 285px; background: #ffffff; padding: 0px; border: 1px; border-style: solid; border-color: #a9218e; font-family: Arial, Helvetica, sans-serif; font-size: 11px; left: ' + left + 'px;">');
				}

				innerHTML.push('	<div style="width: 430px; clear: both; height: 22px; border-bottom: 1px solid #d2d2d2;">');
				innerHTML.push('		<div id="div_select_city_title" style="font-size: 15px; font-weight: bold; color: #4c4d4f; text-align: left; padding-top: 3px; float: left; clear: none;">');
				innerHTML.push('		Welke plaats bedoel je?');
				innerHTML.push('		</div>');
				innerHTML.push('		<div style="float: right; clear: right;">');
				innerHTML.push('			<img src="images/popup_logo.gif"/>');
				innerHTML.push('			<img src="images/button_close_popup.gif" style="cursor: pointer" onClick="hideSelectCityForm()">');
				innerHTML.push('		</div>');
				innerHTML.push('	</div>');
				innerHTML.push('	<div style="text-align: left; clear: both; font-size: 12px; width: 420px; color: #4c4c4c; margin-top: 10px; margin-bottom: 20px;">');
				innerHTML.push('		De plaatsnaam die je hebt ingevuld is niet eenduidig <br />');
				innerHTML.push('		Welke plaats bedoel je?');
				innerHTML.push('	</div>');
				innerHTML.push('	<div id="div_select_city_citieslist" style="text-align: left; font-size: 12px; font-weight: bold; width: 420px; color: #4c4c4c;">');

				for (var i = 0; i < postcodesXML.length; i++) {
					var postcode = postcodesXML[i].getAttribute('pc4code');
					var plaats = postcodesXML[i].getAttribute('pc4naam');

					innerHTML.push('<span style="cursor: pointer" onClick="setVacancySearchPostcode(\'' + postcode + '\',\'' +
						plaats + '\')">' +
						plaats + '</span><br>');
				}

				innerHTML.push('		<br>');
				innerHTML.push('	</div>');
				innerHTML.push('</div>');

				window.document.body.innerHTML += innerHTML.join('');
			}
		} else {
			alert('Onverwachte fout bij het opzoeken van de postcode');
		}
	}
}

function hideSelectCityForm() {
/*
	var browserVersion = navigator.appVersion;
	$('over').style.display = 'none';

	if (document.getElementById('div_select_city') != null) {
		document.getElementById('div_select_city').style.display = 'none';
	}
*/
}

function hideUnknownPostcodeDiv() {
	var browserVersion = navigator.appVersion;
	$('over').style.display = 'none';

	if (document.getElementById('div_unknown_postcode') != null) {
		document.getElementById('div_unknown_postcode').style.display = 'none';
	}
}

function setVacancySearchPostcode(postcode, plaats) {
	hideSelectCityForm();
	$('idTxtPostCode').value = postcode;
	$('input_searchlocation').value = plaats;

	$('form_vacancy_search').submit();
}


// Evaluates the specified function String once the base values have been loaded

function usingBaseValues(functionString) {

	if (baseValuesXML != null) {
		eval(functionString);
	} else {
		var httpRequest = openHttpRequest('GET', baseURL + 'getbasevalues', true);

		httpRequest.onreadystatechange = function() {processBaseValues(httpRequest, functionString); } ;
		httpRequest.send(null);
	}
}

function processBaseValues(httpRequest, functionString) {

	if (httpRequest.readyState == 4) {

		if(httpRequest.status == 200) {
			baseValuesXML = httpRequest.responseXML.getElementsByTagName('basevalues')[0];

			if (functionString != null) {
				eval(functionString);
			}
		} else {
			alert("Fout bij het laden van de pagina\n"+ httpRequest.status +":"+ httpRequest.statusText);
		}
	}

}

function getSelectionValues(tagName) {
	var results = new Array();

	var tagsXML = baseValuesXML.getElementsByTagName(tagName);

	for (var i = 0; i < tagsXML.length; i++) {
		var tagXML = tagsXML[i];

		var idcode = tagXML.getAttribute('idcode');
		var name = tagXML.getAttribute('name');

		results[i] = new Array(idcode, name);
	}

	return results;
}

function ifLoggedIn(functionString, alternateFunctionString) {

	var httpRequest = openHttpRequest('GET', baseURL + 'getaccountinfo', true);

	httpRequest.onreadystatechange = function() {processIfLoggedIn(httpRequest, functionString, alternateFunctionString); } ;
	httpRequest.send(null);
}

function processIfLoggedIn(httpRequest, functionString, alternateFunctionString) {

	if (httpRequest.readyState == 4) {
		if(httpRequest.status == 200) {
			accountInfoXML = httpRequest.responseXML.getElementsByTagName('accountinfo')[0];

			if (accountInfoXML.getAttribute('logintype') != 'none') {
				eval(functionString);
			} else if (alternateFunctionString != null) {
				eval(alternateFunctionString);
			} else {
				if (confirm('Je kunt deze actie pas uitvoeren als je een ZoekMijnBaan account hebt. Wil je nu een account aanmaken?')) {
					document.location = '/aanmelden1/aanmelden1.html';
				}
			}

		} else {
			alert("Fout bij het laden van de pagina\n"+ httpRequest.status +":"+ httpRequest.statusText);
		}
	}
}

function insertVacancySearchForm() {

	var httpRequest = openHttpRequest('GET', 'http://www.zoekmijnbaan.nl/form_search_vacancies.html', true);

	httpRequest.onreadystatechange = function() {processVacancySearchForm(httpRequest); } ;
	httpRequest.send(null);
}

function processVacancySearchForm(httpRequest) {
	if (httpRequest.readyState == 4) {
		if(httpRequest.status == 200) {
			document.getElementById('span_vacancy_search_form').innerHTML = httpRequest.responseText;

			var select_branche = document.getElementById('idSlcBranche');

			var branchesXML = baseValuesXML.getElementsByTagName('branche');

			var branchesArray = new Array();

			for (var i = 0; i < branchesXML.length; i++) {
				branchesArray[i] = branchesXML[i];
			}

			branchesArray.sort(sortBranches);

			for (var i = 0; i < branchesArray.length; i++) {
				var brancheXML = branchesArray[i];

				var option = new Option(brancheXML.getAttribute('name'), brancheXML.getAttribute('idcode'));
				select_branche.options[i + 1] = option;
			}
		} else {
			alert("Fout bij het laden van de pagina\n"+ httpRequest.status +":"+ httpRequest.statusText);
		}
	}
}

function sortBranches(brancheXML1, brancheXML2) {

	var name1 = brancheXML1.getAttribute('name');
	var name2 = brancheXML2.getAttribute('name');

	if (name1 == 'Geen voorkeur') {
		return -1;
	} else if (name2 == 'Geen voorkeur') {
		return 1;
	}

	return  name1 > name2 ? 1:-1;
}

function insertCVSearchForm() {

	var httpRequest = openHttpRequest('GET', 'http://www.zoekmijnbaan.nl/form_search_cvs.html', true);

	httpRequest.onreadystatechange = function() {processCVSearchForm(httpRequest); } ;
	httpRequest.send(null);
}

function processCVSearchForm(httpRequest) {
	if (httpRequest.readyState == 4) {
		if(httpRequest.status == 200) {
			document.getElementById('span_cv_search_form').innerHTML = httpRequest.responseText;

			var select_branche = document.getElementById('idSlcBranche');

			var branchesXML = baseValuesXML.getElementsByTagName('branche');

			for (var i = 0; i < branchesXML.length; i++) {
				var brancheXML = branchesXML[i];

				var option = new Option(brancheXML.getAttribute('name'), brancheXML.getAttribute('idcode'));
				select_branche.options[i + 1] = option;
			}
		} else {
			alert("Fout bij het laden van de pagina\n"+ httpRequest.status +":"+ httpRequest.statusText);
		}
	}
}

function handleSearchFormKeyPress(e, formID) {
	var key = e.keyCode || e.which;

	if (key == 13) {
		if (formID == 'form_vacancy_search') {
			checkVacancySearchPostcode();
		} else {
			$(formID).submit();
		}
	}
}

function storeAgent() {

	var url = '/mijnzoekopdrachten/mijnzoekopdrachten.html?';

	url += 'query=' + escape(document.getElementById('idTxtInputQuery').value);
	url += '&branche=' + getDropdownSelection('idSlcBranche');
	url += '&postcode=' + escape(document.getElementById('idTxtPostCode').value);
	url += '&radius=' + getDropdownSelection('idSlcStraal');

	document.location = url;
}

function storeCVAgent() {

	var url = '/mijnzoekopdrachten_rec/mijnzoekopdrachten_rec.html?';

	url += 'query=' + escape(document.getElementById('idTxtInputQuery').value);
	url += '&branche=' + getDropdownSelection('idSlcBranche');
	url += '&postcode=' + escape(document.getElementById('idTxtPostCode').value);
	url += '&radius=' + getDropdownSelection('idSlcStraal');

	document.location = url;
}

function getDropdownSelection(dropdownID) {
	var dropdown = document.getElementById(dropdownID);
	return dropdown.options[dropdown.selectedIndex].value;
}

function getDropdownSelections(dropdownID) {
	var results = new Array();
	var dropdown = document.getElementById(dropdownID);

	for (var i = 0; i < dropdown.options.length; i++) {
		if (dropdown.options[i].selected) {
			results.push(dropdown.options[i].value);
		}
	}

	return results;
}

function clearDropdownSelections(dropdownID) {
	var dropdown = document.getElementById(dropdownID);

	for (var i = 0; i < dropdown.options.length; i++) {
		dropdown.options[i].selected = false;
	}
}

function setDropdownSelection(dropdownID, value) {
	var dropdown = document.getElementById(dropdownID);

	for (var i = 0; i < dropdown.options.length; i++) {
		if (dropdown.options[i].value == value) {
			dropdown.selectedIndex = i;
			return true;
		}
	}

	return false;
}

function setDropdownSelections(dropdownID, values) {
	clearDropdownSelections(dropdownID);

	var dropdown = document.getElementById(dropdownID);

	if (values != '' && values != null) {
		while (values != '') {

			if (values.indexOf(',') != -1) {
				value = values.substring(0, values.indexOf(','));
				values = values.substring(values.indexOf(',') + 1);
			} else {
				value = values;
				values = '';
			}

			for (var i = 0; i < dropdown.options.length; i++) {
				if (dropdown.options[i].value == value) {
					dropdown.options[i].selected = true;
				}
			}
		}
	} else {
		if (dropdown.options[0]) {
			dropdown.options[0].selected = true;
		}
	}
}

function removeChildren(parentElementID) {

	var parentElement = document.getElementById(parentElementID);

	if (parentElement != null) {
		var children = parentElement.childNodes;

		for (var i=0; i < children.length; i++ ) {
			parentElement.removeChild(children[i]);
			i--;
		}
	}
}

function padDigits(n, totalDigits) {

	n = n.toString();
	var pd = '';

	if (totalDigits > n.length) {
		for (var i=0; i < (totalDigits-n.length); i++) {
			pd += '0';
		}
	}

	return pd + n.toString();
}

function calculateCharacterSpace(textAreaID, indicatorElementID, maxLength) {
	var textLength = $(textAreaID).value.length;

	if (textLength > maxLength) {
		$(textAreaID).value = $(textAreaID).value.substring(0, maxLength);
		textLength = maxLength;
	}

	$(indicatorElementID).innerHTML = maxLength - textLength;
}

function openHttpRequest(method, url, async) {
	var httpRequest;

	if (window.ActiveXObject) {
		httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		httpRequest = new XMLHttpRequest();
	}

	httpRequest.open(method, url, async);
	httpRequest.setRequestHeader('Connection', 'close');

	if (method.toLowerCase() == 'post') {
		httpRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	}

	return httpRequest;
}

function getUrlParameter(name) {

	var urlquery = document.location.search;

	while (urlquery.indexOf('=') != -1) {
		var sub = urlquery;

		if (urlquery.indexOf('&') != -1) {
			sub = urlquery.substring(0, urlquery.indexOf('&'));
		}

		var varName = sub.substring(0, sub.indexOf('='));
		var value = sub.substring(sub.indexOf('=') + 1);

		if (varName.indexOf('?') == 0) varName = varName.substring(1);

		if (varName == name) {
			value = value.replace(/\+/g, ' ');
			return unescape(value);
		}

		if (urlquery.indexOf('&') != -1) {
			urlquery = urlquery.substring(urlquery.indexOf('&') + 1);
		} else urlquery = '';
	}
}

function getUrlParameterValues(name) {

	var values = new Array();
	var urlquery = document.location.search;

	while (urlquery.indexOf('=') != -1) {
		var sub = urlquery;

		if (urlquery.indexOf('&') != -1) {
			sub = urlquery.substring(0, urlquery.indexOf('&'));
		}

		var varName = sub.substring(0, sub.indexOf('='));
		var value = sub.substring(sub.indexOf('=') + 1);

		if (varName.indexOf('?') == 0) varName = varName.substring(1);

		if (varName == name) {
			value = value.replace('+', ' ');
			values.push(unescape(value));
		}

		if (urlquery.indexOf('&') != -1) {
			urlquery = urlquery.substring(urlquery.indexOf('&') + 1);
		} else urlquery = '';
	}

	return values;
}

function getCookie(search_name) {
	// note: document.cookie only returns name=value, not the other components

	var tab_cookies = document.cookie.split( ';' );

	for ( i = 0; i < tab_cookies.length; i++ ) {
		var cookie_tmp = tab_cookies[i].split('=');
		var cookie_name = cookie_tmp[0].replace(/^\s+|\s+$/g, '');

		if (cookie_name==search_name) {
			if (cookie_tmp.length>1) {
				return unescape( cookie_tmp[1].replace(/^\s+|\s+$/g, '') );
			}

			return null;
		}
	}

	return null;
} 

function getBrancheNameById(brancheId) {

	var branchesXML = baseValuesXML.getElementsByTagName('branche');

	for (var i = 0; i < branchesXML.length; i++) {
		var brancheXML = branchesXML[i];

		if (brancheXML.getAttribute('idcode') == brancheId) {
			return brancheXML.getAttribute('name');
		}
	}

	return '-';
}

function getSiteNameById(siteId) {

	var sitesXML = baseValuesXML.getElementsByTagName('site');

	for (var i = 0; i < sitesXML.length; i++) {
		var siteXML = sitesXML[i];

		if (siteXML.getAttribute('idcode') == siteId) {
			return siteXML.getAttribute('name');
		}
	}

	return '-';
}

function timeStampToDutchDate(timeStamp) {
	var dateObject = new Date(parseInt(timeStamp));

	var strDate = padDigits(dateObject.getDate(), 2) + '-' +
		padDigits(dateObject.getMonth() + 1, 2) + '-' +
		(dateObject.getYear() + 1900);

	return strDate;
}

function timeStampToDutchDateTime(timeStamp) {
	var dateObject = new Date(parseInt(timeStamp));

	var strDate = padDigits(dateObject.getDate(), 2) + '-' +
		padDigits(dateObject.getMonth() + 1, 2) + '-' +
		(dateObject.getYear() + 1900) +
		', ' + padDigits(dateObject.getHours(), 2) +
		':' + padDigits(dateObject.getMinutes(), 2);

	return strDate;
}

function isNumeric(strString) {
	//  check for valid numeric strings	

	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}

	return blnResult;
}


function removeHTMLTags(strInputCode){
	strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
		return (p1 == "lt")? "<" : ">";
	});
 
	return strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
}

function getWindowWidth() {
	var myWidth = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}

	return myWidth;
}


function getWindowHeight() {
	var myHeight = 0;

	if( typeof( window.innerHeight ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientHeight || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}

	return myHeight;
}

function html_entity_decode(str) {
        try {
                var tarea=document.createElement('textarea');
                tarea.innerHTML = str; return tarea.value;
                tarea.parentNode.removeChild(tarea);
        } catch(e) {
                //for IE add <div id="htmlconverter" style="display:none;"></div> to the page
                document.getElementById("htmlconverter").innerHTML = '<textarea id="innerConverter">' + str + '</textarea>';
                var content = document.getElementById("innerConverter").value;
                document.getElementById("htmlconverter").innerHTML = "";
                return content;
        }
}


