var postform;
var hideReact=true;
$(function(){
	/**
	 * Set the baseUrl, used to redirect tot other pages from javascript. It reads the current user if the version is dev.
	 * baseUrl always ends with an '/'.
	 */
	var host = window.location.host;
	var temp = host.split('.')
	var version = temp[0];
	if(version=='dev'){
		var pathname = window.location.pathname;
		var temp = pathname.split('/');
		var user = temp[1];
		var baseUrl = '/'+user+'/htdocs/';
	}
	else
		var baseUrl = '/';
	
	/**
	 * make entire row clickable.
	 */	
	$("#latest_rooms tbody tr, #latest_wanted tbody tr").css("cursor","pointer").click(function(){window.location=$(this).find("a").attr("href");});

	/**
	 * Create a treeview.
	 */
	$(".sitemap").treeview({
		persist: "location",
		collapsed: true,
		unique: true
	});

	/**
	 * Write out confirmation.
	 */
	$('#write_out_yes').click(function(){
		var confirmation = confirm('Weet je zeker dat je wilt uitschrijven?')
		if(confirmation)
			return true;
		else{
			window.location.href=baseUrl+'profiel.html'
			return false;
		}
	});

	/**
	 * make all the url's with atleast rel='external' open in a new window/tab.
	 */
	$('a[rel="external"]').each(function(i) {this.target="_blank";});
	$('a[rel="external nofollow"]').each(function(i) {this.target="_blank";});
	
	/**
	 * Get the street and place after postal_code and house_number are submitted.
	 */
	$('#postal_code_nr,#postal_code_chr,#house_number').keyup(function(){
		var postal_code_nr = $('#postal_code_nr').val();
		var postal_code_chr = $('#postal_code_chr').val();
		var house_number = $('#house_number').val();		

		//if the user has entered 4 digits and 2 letters for the postal_code and atleast 1 digit for the houseNumber, look the addres up. 
		if (postal_code_nr.match(/^([0-9]{4})$/) &&	postal_code_chr.match(/^([a-zA-Z]{2})$/) &&	house_number.match(/^([0-9]).*?$/)){
			checkAddress();
			return;
		}
		else {
			$('#div_street,#div_place,#error').hide();
			$('#street').val('');
			$('#place').val('');
		}
	});

	/**
	 * submitting of form offer_room_step1 and step 2.
	 */
	$('#offer_room_step1,#offer_room_step2').submit(function() {
		if (postform == 1) {
			return true;
		}
		var street = $('#street').val();
		var place = $('#place').val();
		var postal_code_nr = $('#postal_code_nr').val();
		var postal_code_chr = $('#postal_code_chr').val();
		var house_number = $('#house_number').val();		
	
		//When the user submits the form check if street and place are filled, if not check if all teh required fields are filled in and look the street and place up.
		if (street == '' || place == '') {
			if (postal_code_nr.match(/^([0-9]{4})$/) && 
					postal_code_chr.match(/^([a-zA-Z]{2})$/) && 
					house_number.match(/^([0-9]).*?$/)) {

				postform = 1;	
				timeout = setTimeout(checkAddress, 1200);
				return false;
			} else {
				clearTimeout(timeout);
				$('#div_street,#div_place,#error').hide();
				$('#street').val('');
				$('#place').val('');
			}
			return false;
		}
	});
	
	/**
	 * Limit the input to non alpha characters only.
	 */
	var numeric_blacklist = new Array(32,59,106,107,109,110,111);
	numeric_blacklist=populateArray(numeric_blacklist,65,90,1);
	numeric_blacklist=populateArray(numeric_blacklist,186,192,1);
	numeric_blacklist=populateArray(numeric_blacklist,219,222,1);
	
	var value='';
	$('.input-numeric').keydown(function(e){		
		value=$(this).val();		
	})
	$('.input-numeric').keyup(function(e){		
		var elem = $(this);		
		
		if(in_array(numeric_blacklist, e.keyCode)){
			elem.val(value);
		}
		value = elem.val()
	});
	
	/**
	 * Limit the input to non alpha characters and dashes (-)
	 */
	var data_blacklist = new Array(32,59,106,107,110,111);
	data_blacklist=populateArray(data_blacklist,65,90,1);
	data_blacklist=populateArray(data_blacklist,186,192,1);
	data_blacklist=populateArray(data_blacklist,219,222,1);	

	var value='';
	$('.input-data').keydown(function(){
		value=$(this).val();
	})
	$('.input-data').keyup(function(e){		
		var elem = $(this);		
		if(in_array(data_blacklist, e.keyCode)){
			elem.val(value);
		}
		value = elem.val()
	});
	
	/**
	 * Limit the input to alpha characters
	 */
	var alpa_blacklist = new Array(32,59,106,107,109,110,111);
	alpa_blacklist=populateArray(alpa_blacklist,48,57,1);
	alpa_blacklist=populateArray(alpa_blacklist,96,105,1);
	alpa_blacklist=populateArray(alpa_blacklist,219,222,1);	
	alpa_blacklist=populateArray(alpa_blacklist,188,192,1);	

	var value='';
	$('.input-aplha').keydown(function(){
		value=$(this).val();
	})
	$('.input-alpha').keyup(function(e){		
		var elem = $(this);		
		if(in_array(alpa_blacklist, e.keyCode)){
			elem.val(value);
		}
		value = elem.val()
	});
	
	/**
	 * hide the street div.
	 */
	$('#offer_room_step1 #div_street').hide();
	
	/**
	 * Display the street div when the input filed on it is filled.
	 */
	if($('#street').val()!='' && $('#street').val()!=undefined)
		$('#offer_room_step1 #div_street').show()
	
	/**
	 * Hide the place div.
	 */
	$('#offer_room_step1 #div_place').hide();
	
	/**
	 * Display the place div if the inputfield on it is filled.
	 */
	if($('#place').val()!='' && $('#place').val()!=undefined)
		$('#offer_room_step1 #div_place').show()
		
//	$('#offer_room_step2 #div_inclusive').hide();
//	if($('#inclusive_yes:checked').length==1)
//		$('#div_inclusive').show()

  /**
	 * toggle the display of the intermediation price div.
	 */
	//hide it.
  $('#offer_room_step2 #div_intermediation').hide();
	//display it when 'yes' is checked.
  if($('#intermediation_yes:checked').length==1)
		$('#div_intermediation').show();
  
  //Display it when the inputfield has a value and the radiobutton has a value.
  if($('#intermediation').val()!='' && $('#intermediation_price').val()!=undefined)
		$('#div_intermediation').show();
		
  //Display it when 'yes' is selected.
  $('#intermediation_yes').click(function() {
  	$('#div_intermediation').show();
  });
  
  //Hide it when 'no' is selected.
  $('#intermediation_no').click(function() {
  	$('#div_intermediation').hide();
  });
  
  /**
	 * toggle the display of the available from div.
	 */
  //hide it.
  $('#offer_room_step3 #div_available_from').hide();
  
  //Display it when 'available_from' is checked.
  if($('#available_from:checked').length==1)
		$('#div_available_from').show()
		
	//Display it when 'available_from' is selected.
  $('#available_from').click(function() {
  	$('#div_available_from').show();
  });
  
  //Hide it when 'available_direct' is selected.
  $('#available_direct').click(function() {
  	$('#div_available_from').hide();
  });

  /**
	 * toggle the display of the available till div.
	 */
  //Hide it.
  $('#offer_room_step3 #div_available_till').hide();
  
  //Display it when 'available_period_till' is checked.
  if($('#available_period_till:checked').length==1)
		$('#div_available_till').show()
		
	//Display it when 'available_period_till' is selected.
	$('#available_period_till').click(function() {
  	$('#div_available_till').show();
  });
  
  //Hide it when 'available_period_unlimited' is selected.
  $('#available_period_unlimited').click(function() {
  	$('#div_available_till').hide();
  });
  
//  $('#go_step1').click(function() {  	
//  	var street = $('#street').val();
//		var place = $('#place').val();
//		var postal_code_nr = $('#postal_code_nr').val();
//		var postal_code_chr = $('#postal_code_chr').val();
//		var house_number = $('#house_number').val();		
//	
//		if (street == '' || place == '') {
//			if (postal_code_nr.match(/^([0-9]{4})$/) && 
//					postal_code_chr.match(/^([a-zA-Z]{2})$/) && 
//					house_number.match(/^([0-9]).*?$/)) {
//
//				postform = 1;	
//				timeout = setTimeout(checkAddress, 1200);
//				return false;
//			} else {
//				clearTimeout(timeout);
//				$('#div_street,#div_place,#error').hide();
//				$('#street,#place').val('');
//			}
//			return false;
//		}
//  });
  
  /**
   * Set the date fields to readonly.
   */
  $('#available_from_date, #available_period_date, #available_date, #available_till_date, #birthday').attr('readonly',true);
  
  /**
   * Create the datepicker.
   */
  $('#available_from_date, #available_period_date, #available_date, #available_till_date, #extend_subscription_date').datepicker({
  	buttonText: 'Kalender',
		showOn: 'both',
		buttonImage: baseUrl+'img/calendar.gif',
		buttonImageOnly:true,
		minDate: 0,
		regional: 'nl',
		closeText: 'Klaar',
		currentText: 'Vandaag',
		dateFormat: 'dd-mm-yy',
		dayNames: ['zondag','maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
		dayNamesMin: ['zo','ma', 'di', 'wo', 'do', 'vr', 'za'],
		dayNamesShort: ['zon','maa','din','woe','don','vrij','zat'],
		defaultDate: +1,
		firstDay: 1,
		hideIfNoPrevNext: true,
		monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],
		monthNamesShort: ['jan','feb','mrt','apr','mei','jun','jul','aug','sep','okt','nov','dec'],
		navigationAsDateFormat:false,
		nextText:'Volgende',
		PrevText:'Vorige',
		showButtonPanel:true,
		showOtherMonths: true
	});  
  
//  $('#available_date, #available_till_date, #available_from_date').attr('readonly',true);
//  
//  $('#available_date, #available_till_date, #available_from_date').datepicker({
//  	buttonText: 'Kalender',
//		showOn: 'both',
//		buttonImage: baseUrl+'img/calendar.gif',
//		buttonImageOnly:true,
//		minDate: 0,
//		regional: 'nl',
//		closeText: 'Klaar',
//		currentText: "today",
//		dateFormat: 'dd-mm-yy',
//		dayNames: ['zondag','maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
//		dayNamesMin: ['zo','ma', 'di', 'wo', 'do', 'vr', 'za'],
//		dayNamesShort: ['zon','maa','din','woe','don','vrij','zat'],
//		defaultDate: +1,
//		firstDay: 1,
//		hideIfNoPrevNext: true,
//		monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],
//		monthNamesShort: ['jan','feb','mrt','apr','mei','jun','jul','aug','sep','okt','nov','dec'],
//		navigationAsDateFormat:false,
//		nextText:'Volgende',
//		PrevText:'Vorige',
//		showButtonPanel:true,
//		showOtherMonths: true
//	});
  
  
  $('#dialog_link, ul#icons li').hover(
			function() { $(this).addClass('ui-state-hover'); }, 
			function() { $(this).removeClass('ui-state-hover'); }
		);  
  
//  $('#birthday').attr('readonly',true);
  /**
   * Create the datepicker for the birthday field. It uses a slightly different configuration then the other datepickers.
   */
  $('#birthday').datepicker({
  	buttonText: 'Kalender',
		showOn: 'both',
		buttonImage: baseUrl+'img/calendar.gif',
		buttonImageOnly:true,		
		changeMonth: true,
		changeYear: true,
		yearRange: '-90:0',
		maxDate:0,
		regional: 'nl',
		closeText: 'Klaar',
		currentText: 'Vandaag',
		dateFormat: 'dd-mm-yy',
		dayNames: ['zondag','maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
		dayNamesMin: ['zo','ma', 'di', 'wo', 'do', 'vr', 'za'],
		dayNamesShort: ['zon','maa','din','woe','don','vrij','zat'],		
		firstDay: 1,
		hideIfNoPrevNext: true,
		monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],
		monthNamesShort: ['jan','feb','mrt','apr','mei','jun','jul','aug','sep','okt','nov','dec'],
		navigationAsDateFormat:false,
		nextText:'Volgende',
		PrevText:'Vorige',
		showButtonPanel:true,
		showOtherMonths: true
	});
  
  /**
   * Toggle the eductation_direction div.
   */
  //Hide it.
  $('#profile #education').hide();
  //Store the current selected value in a var.
  var education_direction = $('#profile #education_direction').val();
  //Store the current user id in a var.
	var user=$('#u_id').val();
	
	//When level 'mbo', 'hbo' or 'wo' is selected retrieve the courses for that level and dislpay the div.
  if($('#profile #educational_lvl option:selected').val()=='mbo' || $('#profile #educational_lvl option:selected').val()=='hbo' || $('#educational_lvl option:selected').val()=='wo'){
  	retrieveCourses($('#profile #educational_lvl option:selected').val(), education_direction, user);
  	$('#profile #education').show();
  }
  
  /**
   * When the level is changed store the current direction and the user id in a var.
   * Then retrieve the courses corresponding the the level (only with levels 'mbo', 'hbo' and 'wo') and display the div.
   * If a level other then 'mbo', 'hbo', or 'wo' is selected hide the div.
   */
  $('#profile #educational_lvl').change(function(){
  	var education_direction = $('#profile #education_direction').val();
  	var user=$('#u_id').val();
  	if($('#profile #educational_lvl option:selected').val()=='mbo'){
  		$('#profile #education').show();
  		retrieveCourses('mbo', education_direction, user);
  	}
  	else if($('#profile #educational_lvl option:selected').val()=='hbo'){
  		$('#profile #education').show();
			retrieveCourses('hbo', education_direction, user);
  	}
  	else if($('#profile #educational_lvl option:selected').val()=='wo'){
  		$('#profile #education').show();
  		retrieveCourses('wo', education_direction, user);
  	}
  	else
  		$('#profile #education').hide();
  });
  
  /**
	 * Toggle the display of the street div while editing a room.
	 */
  //Hide it.
	$('#edit_room #div_street').hide();
	
	//Display it when the inputfield on it is filled
	if($('#street').val()!='' && $('#street').val()!=undefined)
		$('#edit_room #div_street').show()
	
	/**
	 * Toggle the display of the place div while editing a room.
	 */
	//Hide it.
	$('#edit_room #div_place').hide();
	//Display it when the inputfield on it is filled.
	if($('#place').val()!='' && $('#place').val()!=undefined)
		$('#edit_room #div_place').show()
		
//	$('#edit_room #div_inclusive').hide();
//	if($('#inclusive_yes:checked').length==1)
//		$('#div_inclusive').show()
		
//	if($('#inclusive_price').val()!='' && $('#inclusive_price').val()!=undefined)
//		$('#div_inclusive').show()
//	
//	/**
//	 * toggle the display of the inclusive price div
//	 */
//  $('#inclusive_yes').click(function() {  	
//  	$('#div_inclusive').show();
//  });
//  
//  $('#inclusive_no').click(function() {
//  	$('#div_inclusive').hide();
//  });
  
  /**
	 * toggle the display of the intermediation price div.
	 */
	//Hide it.
  $('#edit_room #div_intermediation').hide();
	//Display it when 'yes' is checked.
  if($('#intermediation_yes:checked').length==1)
		$('#div_intermediation_price').show()
	
	//Display it when the inputfield has a value and the radiobutton has a value.
  if($('#intermediation_price').val()!='' && $('#intermediation_price').val()!=undefined)
		$('#div_intermediation').show()
	
	//Display it when 'yes' is selected.
  $('#intermediation_yes').click(function() {
  	$('#div_intermediation').show();
  });

	//hide it when 'no' is selected.
  $('#intermediation_no').click(function() {
  	$('#div_intermediation').hide();
  });
  
  /**
	 * toggle the display of the available from div.
	 */
  //Hide it.
  $('#edit_room #div_available_from').hide();
  
  //Display it when 'available_from' is checked.
  if($('#available_from:checked').length==1)
		$('#div_available_from').show()
	
	//Display it when 'available_from' is selected.
  $('#available_from').click(function() {
  	$('#div_available_from').show();
  });

	//Hide it when 'available_direct' is selected.
  $('#available_direct').click(function() {
  	$('#div_available_from').hide();
  });

  /**
	 * toggle the display of the available till div.
	 */
  //Hide it.
  $('#edit_room #div_available_till').hide();
  
	//Display it when 'available_till' is checked.
  if($('#available_till:checked').length==1)
		$('#div_available_till').show()

	//Display it when 'available_till' is selected.
	$('#available_till').click(function() {
  	$('#div_available_till').show();
  });
  
	//Hide it when 'available_unlimited' is selected.
  $('#available_unlimited').click(function() {
  	$('#div_available_till').hide();
  });
  
  //Display a nice popup with a photo when a link with rel='prettyphoto'.
  $('a[rel="prettyPhoto"]').prettyPhoto();
  
  /**
   * Handle RSS page.
   */
  //Remove the submit button
  $('#submit_rss').remove();
  //When the form is submitted redirect to the rss feed.
  $('#rss_feed').submit(function(){
  	var place = $('#rss_feed #city').val();
  	window.location=baseUrl+"rss/"+place;
  	return false;
  });

  //When the select box changes. Redirect to the rss feed.
	$('#rss_feed #city').change(function(){
		var place = $('#rss_feed #city').val();
		window.location=baseUrl+"rss/"+place;
	});
				
	/**
	 * If hideReact is true: hide the react-box otherwise don't hide it and set hideReact back to true.
	 */
	if(hideReact){
		$('#react').hide();
	}
	else{
		hideReact=true;
	}
	/**
	 * Toggle the display of the reaction form.
	 */
	$('#react-button').click(function(){
			$('#react').slideToggle();
	});
	
	$('#mail-a-friend').hide();
	/**
	 * Toggle the display og the mail-a-friend form.
	 */
	$('.action-mail').click(function(){
		$('#mail-a-friend').slideToggle();
	});
	
	/**
	 * Handle the submit of the mail-a-friend form.
	 */
	$('#mail-a-friend-form').submit(function(){		
		mailAFriend();
		return false;
	});
		
	/**
		* Toggle faq questions
		*/

	$('.faq div').hide()
	
	$('.faq h4').click(function(){
	
	  $(this).next('div').slideToggle('fast');
	
	  return false;
	
	});
	
	$('document').ready(function(){
		if($('#offer_room_step1 #postal_code_nr')){
			$('#offer_room_step1 #postal_code_nr').focus();
		}
		if($('#emailadres')){
			$('#emailadres').focus();
		}
		if($('#old_password')){
				$('#old_password').focus();
		}
		if($('#sender-name')){
			if($('#sender-name').val()==''){
				$('#sender-name').focus();
			}
			else{
				$('#sender-subject').focus();
			}
		}
		if($('#offer_room_step2 #price')){
			$('#offer_room_step2 #price').focus();
		}
	});
	
	$('.changeActive').click(function(){
		var id = ($(this).attr("id")).replace("room","");
		if($(this).hasClass('activate')){
			activateRoom(id);
			$(this).toggleClass('activate');
			$(this).toggleClass('deactivate');
			return false;
		}
		else if($(this).hasClass('deactivate')){
			deactivateRoom(id)
			$(this).toggleClass('activate');
			$(this).toggleClass('deactivate');
			return false;
		}
	});
	
	$('.del_alert').click(function(){		
		var confirmation = confirm('Weet je zeker dat je deze e-mailalert wilt verwijderen?')
		if(confirmation)
			return true;
		else{			
			return false;
		}
	});
});

/**
 * Retrieve the street and place of a postal code/house number combination by an ajax call
 * @return
 */
function checkAddress(){
	/**
	 * Set the ajaxUrl, used for the ajax call. It reads the current user if the version is dev.
	 * ajaxUrl always ends with an '/'.
	 */
	var host = window.location.host;
	var temp = host.split('.')
	var version = temp[0];
	if(version=='dev'){
		var pathname = window.location.pathname;
		var temp = pathname.split('/');
		var user = temp[1];
		var ajaxUrl = '/'+user+'/htdocs/';
	}
	else
		var ajaxUrl = '/';
	
	var postal_code_nr = $('#postal_code_nr').val();
	var postal_code_chr = $('#postal_code_chr').val();
	var house_number = $('#house_number').val();
	var postform;
	
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: ajaxUrl+'getAddress_ajax.php',
		data: 'postal_code_nr='+postal_code_nr+'&postal_code_chr='+postal_code_chr+'&housenumber='+house_number,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {
					$('#div_street,#div_place').show();
					$('#error').html(response.substr(6,response.length-7));
					$('#error').show();
					return;
				}
				$('#error').hide();
				
				response = response.split(';');				
				$('#street').val(response[0]);
				$('#place').val(response[1]);
				
				$('#div_street,#div_place').show();
			}
			else {
				$('#div_street,#div_place,#error').hide();
				$('#street').val('');
				$('#place').val('');
			}
			if (postform == 1) {
				$('#offer_room_step1').submit();
			}
		}
	})
}

/**
 * Retrieve the courses by an ajax call
 */
function retrieveCourses(level, education_direction, user){
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'retrieve_courses.php',
		data: 'level='+level+'&direction='+education_direction+'&user='+user,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {					
					$('#error').html(response.substr(6,response.length-7));
					$('#error').show();
					return;
				}
				$('#error').hide();
				
				$('#education').html(response);
			}			
		}
	})
}

/**
 * Mail a friend
 * @return
 */
function mailAFriend(){
	var senderName = $('#senderName').val();
	var senderEmail = $('#senderEmail').val();
	var recieverName = $('#recieverName').val();
	var recieverEmail = $('#recieverEmail').val();
	var room = $('#r_id').val();

	//disable the submit button to prevent multiple submits.
	document.getElementById('mail-a-friend-submit').disabled = true;
	
	//check the input fields.
	if(senderName==''){
		alert('Voer je naam in');
		document.getElementById('mail-a-friend-submit').disabled = false;
		return;
	}
	if(senderEmail!='' && isValidEmail(senderEmail)){
		if(recieverName==''){
			alert('Voer de naam van de geadresseerde in');
			document.getElementById('mail-a-friend-submit').disabled = false;
			return;
		}
		if(recieverEmail!='' && isValidEmail(recieverEmail)){
			//setup an ajax call and execute it.
			$.ajax({
				type: 'POST',
				url: 'mail-a-friend.php',
				data: 'senderName='+senderName+'&senderEmail='+senderEmail+'&recieverName='+recieverName+'&recieverEmail='+recieverEmail+'&r_id='+room+'&return=0',
				cache: false,
				success: function(response){
					if(response.length >0){
						if(response.substr(0,6)=='ERROR:') {							
							$('#mail-a-friend').html('<p class="mail-error">Versturen mislukt</p>');
							document.getElementById('mail-a-friend-submit').disabled = false;
							$('#mail-a-friend').show();
							return;
						}
						else{						
							$('#mail-a-friend').html('<p class="mail-succes">Mail succesvol verstuurd</p>');
							$('#mail-a-friend').show();
						}
					}
				}
			});
		}
		else{
			alert('Voer een geldig e-mail adres in bij de geadresseerde');
			document.getElementById('mail-a-friend-submit').disabled = false;
			return;
		}
	}
	else{
		alert('Voer een geldig e-mailadres in bij jezelf');
		document.getElementById('mail-a-friend-submit').disabled = false;
		return;
	}
}

/*******************/
/* ADMIN FUNCTIONS */
/*******************/

/**
 *	Activate order by an ajax call
 */
function activateOrder(orderid) {
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=activateOrder&orderID='+orderid,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {					
					$('.highlight-box').html(response.substr(6,response.length-7));
					$('.highlight-box').show();
					return;
				}
				$('.highlight-box').hide();
				
				$('#antwoord'+orderid).html(response);
			}			
		}
	})
}

/**
 *	Delete order by an ajax call
 */
function deleteOrder(orderId) {
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=deleteOrder&orderID='+orderId,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {					
					$('.highlight-box').html(response.substr(6,response.length-7));
					$('.highlight-box').show();
					return;
				}
				$('.highlight-box').hide();
				
				$('#antwoord'+orderId).html(response);
			}			
		}
	})
}

/**
 *	Activate user by an ajax call
 */
function activateUser(userId) {
//	return;
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=activateUser&userID='+userId,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {					
					$('.highlight-box').html(response.substr(6,response.length-7));
					$('.highlight-box').show();
					return;
				}
				$('.highlight-box').hide();

				$('#activationResponse'+userId).html(response);
			}			
		}
	})
	return false;
}

/**
 *	Deactivate user by an ajax call
 */
function deactivateUser(userId) {	
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=deactivateUser&userID='+userId,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {
					$('.highlight-box').html(response.substr(6,response.length-7));
					$('.highlight-box').show();
					return;
				}
				$('.highlight-box').hide();
	
				$('#activationResponse'+userId).html(response);
			}
		}
	})
	return false;
}

/**
 *	Delete user by an ajax call
 */
function deleteUser(userId) {
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=deleteUser&userID='+userId,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {					
					$('.highlight-box').html(response.substr(6,response.length-7));
					$('.highlight-box').show();
					return;
				}
				$('.highlight-box').hide();
				
				// Hide (de-)activation buttons
				$('#activateResponse'+userId).hide();
				$('#deactivateResponse'+userId).hide();
				
				// Print result
				$('#deleteResponse'+userId).html(response);
			}			
		}
	})
}

/**
 *	Deactivate room by an ajax call
 */
function deactivateRoom(roomId) {	
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=deactivateRoom&roomID='+roomId,
		cache: false,
		success: function(response){
		if (response.length > 0) {
			if (response.substr(0,6) == 'ERROR:') {					
				$('.highlight-box').html(response.substr(6,response.length-7));
				$('.highlight-box').show();
				return;
			}
			$('.highlight-box').hide();
			
			$('#room'+roomId).text('Activeer kamer');
			var today = new Date();
			$('#room-date').html('<span>Verwijderd op</span> '+response);
		}			
	}
	})
	return false;
}

/**
 *	Deactivate room by an ajax call
 */
function activateRoom(roomId) {	
	//Setup and execute the ajax call
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=activateRoom&roomID='+roomId,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {					
					$('.highlight-box').html(response.substr(6,response.length-7));
					$('.highlight-box').show();
					return;
				}
				$('.highlight-box').hide();
				
				$('#room'+roomId).text('Deactiveer kamer');
				$('#room-date').html('<span>Geplaatst op</span> '+response);
			}			
		}
	})
	return false;
}

function changeUserlevel(userID) {
	var userLevel = document.getElementById('userLevel'+userID).value;
	//Setup and execute the ajax call.
	$.ajax({
		type: 'GET',
		url: 'ajax.php',
		data: 'action=changeUserlevel&userID='+userID+'&userLevel='+userLevel,
		cache: false,
		success: function(response){
			if (response.length > 0) {
				if (response.substr(0,6) == 'ERROR:') {					
					$('.highlight-box').html(response.substr(6,response.length-7));
					$('.highlight-box').show();
					return;
				}
				$('.highlight-box').show();
				$('.highlight-box').html(response);
			}			
		}
	})
	return false;
}
/*************/
/* END ADMIN */
/*************/

/**
 * Check if a value exists in an array. Similar to the php equivalent in_array().
 * @param array - The array to search in.
 * @param value - The value to search for.
 * @return boolean - True when found, false otherwise.
 */
function in_array(array, value){
	for(var i in array){
		if(value==array[i]){
			return true;
		}
	}
	return false;
}

/**
 * Populate an array with values.
 * @param array - The array to add the values to.
 * @param min - The minimum value.
 * @param max - The maximum value.
 * @param stepSize - The size of the itterration steps.
 * @return array - The array with the new values.
 */
function populateArray(array, min, max, stepSize){
	for(var i=min;i<=max;i+=stepSize){
		array.push(i);
	}
	return array;
}

/**
 * check if an emailadres has a valid form.
 * @param mailAddress - the email adres to validate.
 * @return boolean - True on valid, false otherwise.
 */
function isValidEmail(mailAddress){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(mailAddress)) {
		return true;
	}
	return false;
}

/**
 * Set a global variable to false to display the react box on a detail page
 * @return void
 */
function showReactBox(){
	hideReact=false;
}