//	--- GETANANNY.NL JAVASCRIPT/FUNCTIONS.JS ---	//
//	Hier worden alle algemene javascript-functions 	//
//	aangemaakt

function swapDiv(new_div, old_div)
{	// functie voor het zichtbaar maken van de ene, en onzichtbaar maken van de andere div
	new_div = document.getElementById(new_div);
	new_div.style.display = 'block';
	
	if(old_div != '')
	{
		old_div = document.getElementById(old_div);
		old_div.style.display = 'none';
	}
}

function showhidediv(div, getFocus, overlay)
{	// functie voor het zichtbaar en onzichtbaar maken van een div
	div = document.getElementById(div);
	
	if( (!div.style.display) || (div.style.display == 'none') )
	{
		div.style.display = 'block';
	}
	else
	{
		div.style.display = 'none';
	}
	
	if( overlay )
	{
		document.getElementById('overlay').style.display = div.style.display;
	}
	
	if( getFocus )
	{
		document.getElementById(getFocus).focus();
	}
}

function check_all(mainCheckBox, checkBox)
{	// functie voor 'selecteer alles'
	i = 1;
	while( curCheckBox = document.getElementById(checkBox + i) )
	{
		curCheckBox.checked = mainCheckBox.checked;
		if(i > 100)
		{	// voor de zekerheid, mochten we onverhoopt
			// in een eindeloze lus raken
			break;
		}
		i++;
	}
}

function goto_url(str_url)
{
	document.location.href = str_url;
}

function zoek(base_url)
{
	q = document.getElementById('zoek_q').value;	
	soort = document.getElementById('zoek_soort').value;
	type = document.getElementById('zoek_type').value;
	niveau = document.getElementById('zoek_niveau').value;
	regio = document.getElementById('zoek_regio').value;
	
	q = q == 'Trefwoorden' ? '' : q;
	
	url = base_url + 'zoek/';
	
	if( soort > '' )
	{
		url += soort + '/';
	}
	
	if( type > '' )
	{
		url += type + '/';
	}
	
	if( niveau > '' )
	{
		url += niveau + '/';
	}
	
	if( regio > '' )
	{
		url += regio + '/';
	}
	
	if( q > '' )
	{
		url += '?q=' + URLEncode(q);
	}
	
	goto_url(url);
	
}

// URL encoderen en decoderen

function URLEncode(value)
{
	var SAFECHARS = "0123456789" +
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";
	var HEX = "0123456789ABCDEF";

	var plaintext = value
	var encoded = "";
	
	for (var i = 0; i < plaintext.length; i++ )
	{
		var ch = plaintext.charAt(i);
	    if (ch == " ")
		{
		    encoded += "+";
		}
		else if (SAFECHARS.indexOf(ch) != -1)
		{
		    encoded += ch;
		}
		else
		{
		    var charCode = ch.charCodeAt(0);
			
			encoded += "%";
			encoded += HEX.charAt((charCode >> 4) & 0xF);
			encoded += HEX.charAt(charCode & 0xF);
		}
	}
	
	return encoded;
}

function URLDecode(value)
{
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = value;
   
   var plaintext = "";
   var i = 0;
   
   while (i < encoded.length)
   {
       var ch = encoded.charAt(i);
	   
	   if (ch == "+")
	   {
	       plaintext += " ";
		   i++;
	   }
	   else if (ch == "%")
	   {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			}
			else
			{
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		}
		else
		{
		   plaintext += ch;
		   i++;
		}
	}
	
   return plainttext;
}

function meldMisbruik(user_id, resultDiv)
{	// het melden van misbruik
	new Ajax.Updater( resultDiv, 'ajax/misbruikmelder.php',
	{
		parameters:
		{
			user_id: user_id
		},
		method: 'get'
	});
}

function alsFavoriet(user_id, resultDiv)
{	// het melden van misbruik
	new Ajax.Updater( resultDiv, 'ajax/favoriettoevoeger.php',
	{
		parameters:
		{
			user_id: user_id
		},
		method: 'get'
	});
}

function fixInputs()
{
	
	allInputs = document.getElementsByTagName('input');
	
	for( i=0; i<allInputs.length; i++ )
	{
		curInput = allInputs[i];
		
		if( curInput.id.substring(0, 5) == 'auto_' )
		{
			curInput.onfocus = function()
			{
				this.className = '';
				if( this.value == this.title )
				{
					this.value = '';
				}
			}
			
			curInput.onblur = function()
			{
				if( this.value == '' )
				{
					this.className = 'default';
					this.value = this.title;
				}
			}
		}
	}
	
	allTextareas = document.getElementsByTagName('textarea');
	
	for( i=0; i<allTextareas.length; i++ )
	{
		curTextarea = allTextareas[i];
		
		if( curTextarea.id.substring(0, 5) == 'auto_' )
		{
			curTextarea.onfocus = function()
			{
				if( this.value == this.title )
				{
					this.value = '';
				}
			}
			
			curTextarea.onblur = function()
			{
				if( this.value == '' )
				{
					this.value = this.title;
				}
			}
		}
	}
	
}
 
// voeg de handler toe
if( window.addEventListener )
{
	window.addEventListener("load", fixInputs, false);
}
else if( window.attachEvent )
{
	window.attachEvent("onload", fixInputs);
}