var eSearchTerm;
var eSearchSuggestions;
var keyStrokeCounter = 0;
var focusedSearchSuggest = null;
var lastKeyDirection = null;
var lastSearchValue;
windowLoadStack.push( InitializeSearchSuggest );
function InitializeSearchSuggest()
{
eSearchTerm = document.getElementById( 'SearchTerm' );
eSearchSuggestions = document.getElementById( 'SearchSuggestions' );
document.onkeydown = function( e )
{
var key = null;
if ( !e )
key = window.event.keyCode;
else
key = e.keyCode;
if ( eSearchSuggestions.className == '' )
{
switch( key )
{
case 38:
SearchSuggestionsUp();
return false;
case 40:
SearchSuggestionsDown();
return false;
}
}
}
eSearchTerm.onkeyup = function( e )
{
Search();
}
eSearchTerm.onfocus = function()
{
if ( this.value.replace( ' ', '' ) == '' )
{
eSearchSuggestions.innerHTML = '';
eSearchSuggestions.className = 'DisplayNone';
}
else
eSearchSuggestions.className = '';
}
document.onmousedown = function( e )
{
var targetElement = null;
if ( e != null )
targetElement = e.target;
else
targetElement = event.srcElement;
while( targetElement != null && targetElement.tagName != "BODY" )
{
if ( targetElement.id == "SearchSuggestions" )
return;
else
targetElement = targetElement.parentNode;
}
eSearchSuggestions.className = 'DisplayNone';
}
}
function SearchSuggestionsUp()
{
var cSearchSuggestionsItems = eSearchSuggestions.childNodes;
var firstItem = 1;
if ( window.event )
firstItem = 0;
if ( focusedSearchSuggest != null && lastKeyDirection == "down" )
focusedSearchSuggest = focusedSearchSuggest - 2;
if ( focusedSearchSuggest == null 
|| ( !window.event && focusedSearchSuggest <= firstItem ) 
|| ( window.event && focusedSearchSuggest < firstItem ) )
{
focusedSearchSuggest = null;
lastKeyDirection = "up";
eSearchTerm.focus();
return;
}
for ( var i=cSearchSuggestionsItems.length - 1; i>=0; i-- )
{
if ( cSearchSuggestionsItems[ i ].tagName != null
&& cSearchSuggestionsItems[ i ].tagName == "LI"
&& i <= focusedSearchSuggest )
{
var eFocusedLink = GetFirstTagElement( cSearchSuggestionsItems[ i ] );
focusedSearchSuggest = i - 1;
lastKeyDirection = "up";
eFocusedLink.focus();
break;
}
}
}
function SearchSuggestionsDown()
{
var cSearchSuggestionsItems = eSearchSuggestions.childNodes;
var firstItem = 1;
var lastItem = cSearchSuggestionsItems.length - 1;
if ( window.event )
{
firstItem--;
lastItem++;
}
if ( focusedSearchSuggest != null && lastKeyDirection == "up" )
focusedSearchSuggest = focusedSearchSuggest + 2;
if ( focusedSearchSuggest == null || focusedSearchSuggest >= lastItem )
focusedSearchSuggest = firstItem;
for ( var i=firstItem; i<cSearchSuggestionsItems.length; i++ )
{
if ( cSearchSuggestionsItems[ i ].tagName != null
&& cSearchSuggestionsItems[ i ].tagName == "LI"
&& i >= focusedSearchSuggest )
{
var eFocusedLink = GetFirstTagElement( cSearchSuggestionsItems[ i ] );
focusedSearchSuggest = i + 1;
lastKeyDirection = "down";
eFocusedLink.focus();
break;
}
}
}
function GetFirstTagElement( eParent )
{
for ( var i=0; i<eParent.childNodes.length; i++ )
{
if ( eParent.childNodes[ i ].tagName != null )
return eParent.childNodes[ i ];
}
}
function Search()
{
keyStrokeCounter = keyStrokeCounter + 1;
setTimeout( "TrySearch( keyStrokeCounter-- )", ( keyStrokeCounter * 100 ) );
}
function TrySearch( keyStrokeCounterValue )
{
if ( keyStrokeCounterValue == 1 )
{
RetrieveResults();
}
}
function RetrieveResults()
{
var searchTerm = Base64.encode( eSearchTerm.value );
if ( searchTerm != lastSearchValue )
{
var xmlHttpResponse = OpenRequest( 'http://www.betabanen.nl/XmlHttp.lynkx?event=Search&searchTerm=' + searchTerm, RetrieveResultsResponseHandler );
lastSearchValue = searchTerm;
}
}
function RetrieveResultsResponseHandler( pResponse, pResponseCode, pScript )
{
if ( pResponse != '' )
{
eSearchSuggestions.style.height = '';
eSearchSuggestions.className = '';
eSearchSuggestions.innerHTML = pResponse;
for ( var i=0; i<eSearchSuggestions.childNodes.length; i++ )
{
var eSearchSuggestion = eSearchSuggestions.childNodes[ i ];
if ( eSearchSuggestion.tagName == null || eSearchSuggestion.tagName != "LI" )
continue;
var eSearchSuggestionLink = eSearchSuggestion.getElementsByTagName( "A" )[ 0 ];
if ( eSearchSuggestionLink == null )
continue;
eSearchSuggestionLink.onclick = function( e )
{
focusedSearchSuggest = null;
eSearchTerm.value = this.innerHTML;
eSearchTerm.focus(); 
eSearchSuggestions.className = 'DisplayNone'
var eSearchTermOnKeyUpEvent = eSearchTerm.onkeyup;
eSearchTerm.onkeyup = function()
{
this.onkeyup = eSearchTermOnKeyUpEvent;
return;
}
var eSearchTermOnFocusEvent = eSearchTerm.onfocus;
eSearchTerm.onfocus = function()
{
this.onfocus = eSearchTermOnFocusEvent;
return;
}
}
}
if ( eSearchSuggestions.clientHeight > 300 )
eSearchSuggestions.style.height = '300px';
}
else
{
eSearchSuggestions.className = 'DisplayNone';
eSearchSuggestions.innerHTML = '';
}
}
function OpenRequest( pScript, pHandler, pMethod, pQueryString )
{
var xmlHttp = false;
var xmlHttpResponse = null;
if ( !xmlHttp && typeof XMLHttpRequest != 'undefined' )
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch ( e )
{
xmlHttp = false;
}
}
if ( !xmlHttp && window.createRequest )
{
try
{
xmlHttp = window.createRequest();
}
catch ( e )
{
xmlHttp = false;
}
}
if ( !xmlHttp )
return;
xmlHttp.onreadystatechange = function()
{
if ( xmlHttp != null )
{
if ( xmlHttp.readyState == 4 )
{
if ( xmlHttp.status == 200 )
xmlHttpResponse = xmlHttp.responseText;
else
alert( 'Er is een verbindingsfout opgetreden tijdens het opvragen van de informatie.\r\nProbeer het a.u.b. opnieuw.' );
if ( pHandler != null )
pHandler( xmlHttpResponse, xmlHttp.status, pScript );
}
}
}
if ( !pMethod )
var pMethod = "GET";
var timestamp = new Date();
if ( pScript.indexOf( '?' ) != '-1' )
pScript += '&cache_Timestamp=' + escape( timestamp );
else
pScript += '?cache_Timestamp=' + escape( timestamp );
xmlHttp.open( pMethod, pScript, true );
if ( pMethod == "POST" )
{
if ( !pQueryString )
pQueryString = null;
xmlHttp.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8" );
xmlHttp.send( pQueryString );
}
else
{
xmlHttp.send( null );
}
}
var Base64 = {
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
