var eActiveSearchField = null;
var eActiveSearchSuggestions = null;
var keyStrokeCounter = 0;
var iFocusedSearchSuggest = null;
var sLastKeyDirection = null;
var sLastSearchFieldName = null;
var sLastSearchValue = null;
AttachEventHandler( window, 'onload', InitializeSearchSuggest );
function InitializeSearchSuggest()
{
var cForms = document.forms;
for ( var i=0; i<cForms.length; i++ )
{
InitializeSearchSuggestOnForm( cForms[ i ] );
}
document.onkeydown = function( e )
{
if ( eActiveSearchSuggestions == null )
return;
var key = null;
if ( !e )
key = window.event.keyCode;
else
key = e.keyCode;
if ( eActiveSearchSuggestions.className == 'SearchSuggestionBox' )
{
switch( key )
{
case 38:
SearchSuggestionsUp();
return false;
case 40:
SearchSuggestionsDown();
return false;
}
}
}
document.onmousedown = function( e )
{
if ( eActiveSearchSuggestions == null )
return;
var targetElement = null;
if ( e != null )
targetElement = e.target;
else
targetElement = event.srcElement;
while( targetElement != null && targetElement.tagName != "BODY" )
{
if ( targetElement.id != null && targetElement.id.indexOf( "SearchSuggestions" ) == 0 )
return;
else
targetElement = targetElement.parentNode;
}
if ( targetElement != eActiveSearchSuggestions )
eActiveSearchSuggestions.className = 'SearchSuggestionBox DisplayNone';
}
}
function InitializeSearchSuggestOnForm( eForm )
{
for ( var i=0; i<eForm.elements.length; i++ )
{
var eField = eForm.elements[ i ];
var hasCorrectRelMozilla = eField.attributes != null && eField.attributes[ "rel" ] != null && eField.attributes[ "rel" ].nodeValue == "LynkxSearchSuggest";
var hasCorrectRelIE = eField.rel != null && eField.rel == "LynkxSearchSuggest";
if ( !hasCorrectRelMozilla && !hasCorrectRelIE )
continue;
eField.setAttribute( 'autocomplete', 'off' );
var eSearchSuggestions = document.createElement( "UL" );
eSearchSuggestions.id = "SearchSuggestions[" + eField.name + "]";
eField.parentNode.insertBefore( eSearchSuggestions, eField );
eField.parentNode.style.position = 'relative';
eSearchSuggestions.style.top = ( eField.offsetTop + eField.clientHeight ) + 'px';
eSearchSuggestions.className = "SearchSuggestionBox DisplayNone";
eField.onkeyup = function( e )
{
eActiveSearchField = this;
eActiveSearchSuggestions = this.previousSibling;
if ( eActiveSearchSuggestions.tagName != "UL" )
eActiveSearchSuggestions = eActiveSearchSuggestions.previousSibling;
Search();
}
eField.oldOnFocus = eField.onfocus;
eField.onfocus = function()
{
if ( this.oldOnFocus != null )
{
var oldOnFocus = this.oldOnFocus();
if ( oldOnFocus != null )
return oldOnFocus;
}
if ( this.value == 'zoeken' )
this.value = '';
if ( eActiveSearchSuggestions != null )
eActiveSearchSuggestions.className = 'SearchSuggestionBox DisplayNone';
eActiveSearchField = this;
eActiveSearchSuggestions = this.previousSibling;
if ( eActiveSearchSuggestions.tagName != "UL" )
eActiveSearchSuggestions = eActiveSearchSuggestions.previousSibling;
if ( this.value.replace( ' ', '' ) == '' || eActiveSearchSuggestions.innerHTML == '' )
{
eActiveSearchSuggestions.innerHTML = '';
eActiveSearchSuggestions.className = 'SearchSuggestionBox DisplayNone';
}
else
eActiveSearchSuggestions.className = 'SearchSuggestionBox';
}
}
}
function SearchSuggestionsUp()
{
if ( eActiveSearchSuggestions == null )
return;
var cSearchSuggestionsItems = eActiveSearchSuggestions.childNodes;
var firstItem = -1;
if ( window.event )
firstItem = 0;
if ( iFocusedSearchSuggest != null && sLastKeyDirection == "down" )
iFocusedSearchSuggest = iFocusedSearchSuggest - 2;
if ( iFocusedSearchSuggest == null 
|| ( !window.event && iFocusedSearchSuggest <= firstItem ) 
|| ( window.event && iFocusedSearchSuggest < firstItem ) )
{
iFocusedSearchSuggest = null;
sLastKeyDirection = "up";
eActiveSearchField.focus();
return;
}
for ( var i=cSearchSuggestionsItems.length - 1; i>=0; i-- )
{
if ( cSearchSuggestionsItems[ i ].tagName != null
&& cSearchSuggestionsItems[ i ].tagName == "LI"
&& i <= iFocusedSearchSuggest )
{
var eFocusedLink = GetFirstTagElement( cSearchSuggestionsItems[ i ] );
iFocusedSearchSuggest = i - 1;
sLastKeyDirection = "up";
eFocusedLink.focus();
break;
}
}
}
function SearchSuggestionsDown()
{
if ( eActiveSearchSuggestions == null )
return;
var cSearchSuggestionsItems = eActiveSearchSuggestions.childNodes;
var firstItem = 0;
var lastItem = cSearchSuggestionsItems.length;
if ( iFocusedSearchSuggest != null && sLastKeyDirection == "up" )
iFocusedSearchSuggest = iFocusedSearchSuggest + 2;
if ( iFocusedSearchSuggest == null || iFocusedSearchSuggest >= lastItem )
iFocusedSearchSuggest = firstItem;
for ( var i=firstItem; i<cSearchSuggestionsItems.length; i++ )
{
if ( cSearchSuggestionsItems[ i ].tagName != null
&& cSearchSuggestionsItems[ i ].tagName == "LI"
&& i >= iFocusedSearchSuggest )
{
var eFocusedLink = GetFirstTagElement( cSearchSuggestionsItems[ i ] );
iFocusedSearchSuggest = i + 1;
sLastKeyDirection = "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 sSearchTerm = Base64.encode( eActiveSearchField.value );
var sSearchFieldName = eActiveSearchField.name;
if ( sSearchTerm == sLastSearchValue && sSearchFieldName == sLastSearchFieldName )
return;
var conn = new HttpConnection(
{ 
asynchronous: false,
url: "http://www.arboonline.nl/XSearchSuggest/SearchSuggestXmlHttp.lynkx",
queryString: "?event=Search&fieldName=" + sSearchFieldName + "&searchTerm=" + sSearchTerm 
});
var response = conn.GetResponse();
if ( response != '' )
{
eActiveSearchSuggestions.style.height = '';
eActiveSearchSuggestions.className = 'SearchSuggestionBox';
eActiveSearchSuggestions.innerHTML = '';
var results = response.split( '\n' );
for ( var i in results )
{
if ( isNaN( i ) )
continue;
if ( results[ i ] == '' )
continue;
var eResult = document.createElement( 'LI' );
var eResultLink = document.createElement( 'A' )
eResultLink.href = 'javascript: void(0)';
eResultLink.onclick = function( e )
{
iFocusedSearchSuggest = null;
eActiveSearchField.value = this.innerHTML;
eActiveSearchField.focus(); 
eActiveSearchSuggestions.className = 'SearchSuggestionBox DisplayNone'
var eSearchTermOnKeyUpEvent = eActiveSearchField.onkeyup;
eActiveSearchField.onkeyup = function() { this.onkeyup = eSearchTermOnKeyUpEvent; }
var eSearchTermOnFocusEvent = eActiveSearchField.onfocus;
eActiveSearchField.onfocus = function() { this.onfocus = eSearchTermOnFocusEvent; }
}
eResultLink.appendChild( document.createTextNode( results[ i ] ) );
eResult.appendChild( eResultLink );
eActiveSearchSuggestions.appendChild( eResult );
}
if ( eActiveSearchSuggestions.clientHeight > 300 )
eActiveSearchSuggestions.style.height = '300px';
}
else
{
eActiveSearchSuggestions.className = 'SearchSuggestionBox DisplayNone';
eActiveSearchSuggestions.innerHTML = '';
}
sLastSearchFieldName = sSearchFieldName;
sLastSearchValue = sSearchTerm;
}
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;
}
}
