// Er mogen alleen nummers ingevoerd worden (ook op keypad!), en natuurlijk backspace, enter, tab, pijltjes (links en rechts) en delete
// Usage: 'onkeydown'='numericOnly(event)'
function numericOnly(evt) {
	var charCode = (evt.which) ? evt.which : evt.keyCode;
	if ((charCode>=48 && charCode<=57) || (charCode>=96 && charCode<=105) || charCode==8 || charCode==9 || charCode==13 || charCode==37 || charCode==39 || charCode==46 ) {
	} else {
		Event.stop(evt);
	}
}

// Maak de progessbar voor het uploaden van bestanden
function updateProgress(request, sid, updater) {
	var percent = request.responseText;
	var pixels = percent * 3;
	if (pixels > 300) {
		pixels = 300;
	}
	$('progress').style.width = pixels + 'px';
	if(percent >= 100) {
		updater.stop();
	}
}

// Begin met het uploaden van bestanden
function doUploadSubmit(frm, sid) {
	$('formSaving').style.display='block';

	var myUpdater = new Ajax.PeriodicalUpdater(
		{},
		'/ajax.php',
		{
			decay: 2,
			frequency: 0.5,
			method: 'post',
			parameters: 'cmd=uploadProgress&sid=' + sid,
			onSuccess: function(request){
				updateProgress(request, sid, myUpdater);
			},
			onFailure: function(request){
				alert(request.responseText);
			},
			onComplete: function(){
				klaar(sid);
			}
		}
	);

	frm.submit();
}

function calendarOnClose(cal) {
	cal.hide();
}

function initCalendarObjects() {
	for (var i=0;i<Calendar_Setup_Objects.length;i++) {
		Calendar.setup(Calendar_Setup_Objects[i]);
	}
}

var helpWindowOpen=false;
function showHelp() {
	if (helpWindowOpen==true) {
		$('helpWindow_textContainer').morph({width:'1px'});
		helpWindowOpen=false;
		$('helpWindow_handle').style.background = '#1793AE url("/images/icons/help_open.gif") center no-repeat';
	} else {
		$('helpWindow_textContainer').morph({width:'230px'});
		helpWindowOpen=true;
		$('helpWindow_handle').style.background = '#1793AE url("/images/icons/help_close.gif") center no-repeat';
	}
}

function sluitMelding(id) {
	new Effect.Morph('onderhoudsmeldingMelding',{style:'height:0px;',duration:0.6});
	setTimeout(function(){$('onderhoudsmeldingMelding').style.display = 'none'}, 600);

	var myAjax = new Ajax.Request('ajax.php',
	{
		method: 'post',
		parameters: 'cmd=sluitMelding&id='+id
	});
}

function openMelding(id) {
	$('onderhoudsmeldingMelding').style.height = '';
	$('onderhoudsmeldingMelding').style.display = 'block';

	var myAjax = new Ajax.Request('ajax.php',
	{
		method: 'post',
		parameters: 'cmd=openMelding&id='+id
	});
}

function setCaretToEnd(obj) {
  if (obj.createTextRange) {
	var range = obj.createTextRange();
	range.collapse(false);
	range.select();
  }
  else if (obj.setSelectionRange) {
	obj.focus();
	var length = obj.value.length;
	obj.setSelectionRange(length, length);
  }
}

/* redirect */
function redirect( loc ) {
	window.location=loc;
}

function openwindow( href, width, height ) {
	return window.open(href, 'newwindow', 'toolbar=no,width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes');
}

function popup( href, width, height) {
	window.open(href, 'popupwindow', 'toolbar=no,width=' + width + ',height=' + height);
}

function hidePopup() {
	$('popupcontainer').style.display = 'none';
	$('popup').innerHTML = 'Even geduld...';
}

/* form functions */
function unselectAll(boxid) {
	// unselect alle opties in de selectbox met id boxid
	var options = $(boxid).options;

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

function selectAll(boxid) {
	// selecteer alle opties in de selectbox met id boxid
	var options = $(boxid).options;

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

function findtext_select(select, txt)	{
	txt1 = txt.toLowerCase();
	for (i = select.length - 1; i >= 0; i--)	{
		txt2 = select.options[i].text.toLowerCase();

		if (txt2.substring(0, txt1.length) == txt1) {
			select.selectedIndex = i;
		}
	}
}

function fill_select(select, array_data, func) {
	select.options.length = 0;
	array_data.each( function(value, key){
		if(typeof(value) != 'undefined') {
			idx = select.options.length;
			adl = array_data.length - 1;
			select.options[idx] = new Option(value, adl);
			if (func) {
				select.options[idx].onclick = func;
			}
		}
	});

	if (func) {
		// trap Enter key
		select.onkeypress = function(e) {
			if(e.keyCode == 13) {
				if (this.selectedIndex >= 0) {
					if (typeof(this.options[this.selectedIndex].onclick) != 'undefined')
						this.options[this.selectedIndex].onclick();
				}
			}
			if (e.keyCode==9) {
				return e.shiftKey;
			}
		}
	}
}

var area_zoom = {
	min_width: 200, // minimale breedte in pixels
	max_width: 500, // maximale breedte in pixels
	padding: 6,			// de padding zoals die in CSS staat gedefineerd
	step_x: 75, 		// stap over de x as (breedte) in pixels
	step_y: 40			// stap over de y as (hoogte) in pixels
};

function enlarge_area( id ) {
	area=$(id);
	if (area.offsetWidth < area_zoom.max_width) {
		area.style.width=(area.offsetWidth-area_zoom.padding+area_zoom.step_x)+'px';
		area.style.height=(area.clientHeight+area_zoom.step_y)+'px';
	}
}

function shrink_area( id ) {
	area=$(id);
	if (area.clientWidth > area_zoom.min_width) {
		area.style.width=(area.offsetWidth-area_zoom.padding-area_zoom.step_x)+'px';
		area.style.height=(area.clientHeight-area_zoom.step_y)+'px';
	}
}

function wisZoekopdracht( pageSession ) {
	var myAjax = new Ajax.Request('ajax.php',
	{
		method: 'post',
		parameters: 'cmd=wisZoekopdracht&pageSession='+pageSession,
		onComplete: function(originalRequest) {
			location=location.pathname;
		}
	});
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function su(id, naam) {
	if (confirm('Wilt u inloggen als gebruiker '+naam+'?')) {
		var myAjax = new Ajax.Request('ajax.php',
		{
			method: 'post',
			parameters: 'cmd=su&id='+id,
			onComplete: function(originalRequest) {
				redirect('/persoonlijk');
			}
		});
	}
}

function undoSu() {
	var myAjax = new Ajax.Request('ajax.php',
	{
		method: 'post',
		parameters: 'cmd=undoSu',
		onComplete: function(originalRequest) {
			redirect('/');
		}
	});
}

function changeSlider(value,id,field) {
	$('sliderDisplay'+id).innerHTML=value;
	$(field).value=value;
}

function solveConflict(vacature_id) {
	alert('Hoe gaan het conflict voor vacature '+vacature_id+' oplossen?');
}

function showProfile(id) {
	$('profiling_detail').innerHTML = $('profile_details_'+id).innerHTML;
}

function openVoorwaarden() {
	$('grijs').style.display = 'block';

	var myAjax = new Ajax.Request('ajax.php',
	{
		method: 'post',
		parameters: 'cmd=voorwaarden',
		onComplete: function(originalRequest) {
			new Insertion.After($('grijs'), originalRequest.responseText);
		}
	});
}

function voorwaardenAccoord() {
	var myAjax = new Ajax.Request('ajax.php',
	{
		method: 'post',
		parameters: 'cmd=voorwaardenAccoord',
		onComplete: function(originalRequest) {
			$('grijs').style.display = 'none';
			$('voorwaarden').style.display = 'none';
		}
	});
}

function voorwaardenNietAccoord() {
	redirect('/logout/?uitloggen=1');
}

function setTonenPer(pageSession, aantal) {
	var myAjax = new Ajax.Request('ajax.php',
	{
		method: 'post',
		parameters: 'cmd=setCookieValue&key='+pageSession+'_perPage&value='+aantal,
		onComplete: function(originalRequest) {
			location=location.pathname;
		}
	});
}

/* globale functies voor de AJAX calls */
var myGlobalHandlers = {
	onCreate: function(){
		Element.show('status');
	},
	onComplete: function() {
		if(Ajax.activeRequestCount == 0){
			Element.hide('status');
		}
	},
	onFailure: function(request){
		alert('Sorry. There was an error.');
	}
};

Ajax.Responders.register(myGlobalHandlers);
