// JavaScript Document
function SearchEngine()
{
	this.form;
	this.tf;
	this.interval;
	
	this.init       = function(obj, formid)
	{
		this.form  = document.getElementById(formid);
		this.tf    = obj;
		
		var lScope = this;
		
		// Make sure autocomplete is off
		this.form.setAttribute('autocomplete', 'off');
		
		tf.onkeyup = function()
		{
			
			clearTimeout(lScope.interval);
			lScope.interval = setTimeout('se.searchWord()', 200);
		}
	}
	
	this.searchWord = function()
	{
		clearTimeout(this.interval);
		
		var els  = this.form.elements;
		if(els['zoekterm'].value != '')
		{
			
			var ajax = new Ajax();
			ajax.init(this.responseHandler, this.errorHandler);
			
			var url  = '/wisexs/js/SearchForm.php?zoekterm=' + els['zoekterm'].value + '&zoekmap=' + els['zoekmap'].value + '&zoekdef=' + els['zoekdef'].value + '&respp=' + els['respp'].value + '&start=' + els['start'].value;
		
			ajax.call(url);
		} else
		{
			resetBox();
			hideBox(true);
			//var list = document.getElementById('sp-results');
			//list.style.display = 'none';
		}
	}
	
	this.responseHandler = function(xml)
	{
		if(null != xml)
		{
			var root  = xml.getElementsByTagName('ajax')[0];
			var q     = root.getElementsByTagName('q')[0];
			var rows  = root.getElementsByTagName('row');
			var list  = document.getElementById('sp-results');
			var ul    = list.getElementsByTagName('ul');
			
			if(ul.length == 0)
			{
				var ul = document.createElement('ul');	
			} else
			{
				var ul = ul[0];
				
				// Remove all children
				while(ul.hasChildNodes())
				{
					ul.removeChild(ul.lastChild);
				}
			}
			
			var count = rows.length > 5 ? 5 : rows.length;
			
			
			for(var i = 0; i < count; i++)
			{
				// Deze waardes aanpassen wanneer de indeling anders moet.
				var id           = rows[i].getElementsByTagName('id')[0];
				var def          = rows[i].getElementsByTagName('objecttype')[0];
				var title        = rows[i].getElementsByTagName('title')[0];
				var values       = rows[i].getElementsByTagName('values')[0];
				var picture      = values.getElementsByTagName('foto1')[0];
				var type         = rows[i].getElementsByTagName('deftype')[0];
				
				var url          = '/index/' + type.firstChild.data + '/' + def.firstChild.data + '/' + id.firstChild.data + '/Zoekresultaat.html';
				var link         = document.createElement('a');
				link.setAttribute('href', '/wisexs/js/SearchForm.php?do=log&q=' + q.firstChild.data + '&url=' + urlencode(url));
				
				var li           = document.createElement('li');
				li.setAttribute('id', 'sp-results-' + i);
				
				var div          = document.createElement('li');
				div.setAttribute('style', 'clear: both; float: none;');
				/*
				if(!empty(picture))
				{
					var img = document.createElement('IMG');
					img.setAttribute('alt', title.firstChild.data)
					img.setAttribute('src', '/wisexs/GetImage.php?id=' + picture.firstChild.data + '&w=50');
				}
				*/
				
				var span         = document.createElement('span');
				if(document.all)
				{
					span.innerText   = title.firstChild.data;
				} else
				{
					span.textContent = title.firstChild.data;
				}
				
				/*
				if(!empty(picture) && !empty(picture.firstChild.data))
				{
					link.appendChild(img);
				}
				*/
				
				link.appendChild(span);
				link.appendChild(div);
				li.appendChild(link);
				ul.appendChild(li);
			}
			
			list.appendChild(ul);
			
			resetBox();
			showBox(true);
			//list.style.display = 'block';
		}
	}
	
	this.errorHandler   = function(msg)
	{
		alert("An error occured:\n\n" + msg);
	}
	
	this.closeResults   = function()
	{
		resetBox();
		hideBox(true);
		//var list           = document.getElementById('sp-results');
		//list.style.display = 'none';
	}
}

function urlencode(str)
{
	var histogram = {}, unicodeStr='', hexEscStr='';
	var ret = (str+'').toString();

	var replacer = function(search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};

	// The histogram is identical to the one in urldecode.
	histogram["'"]   = '%27';
	histogram['(']   = '%28';
	histogram[')']   = '%29';
	histogram['*']   = '%2A';
	histogram['~']   = '%7E';
	histogram['!']   = '%21';
	histogram['%20'] = '+';
	histogram['\u00DC'] = '%DC';
	histogram['\u00FC'] = '%FC';
	histogram['\u00C4'] = '%D4';
	histogram['\u00E4'] = '%E4';
	histogram['\u00D6'] = '%D6';
	histogram['\u00F6'] = '%F6';
	histogram['\u00DF'] = '%DF';
	histogram['\u20AC'] = '%80';
	histogram['\u0081'] = '%81';
	histogram['\u201A'] = '%82';
	histogram['\u0192'] = '%83';
	histogram['\u201E'] = '%84';
	histogram['\u2026'] = '%85';
	histogram['\u2020'] = '%86';
	histogram['\u2021'] = '%87';
	histogram['\u02C6'] = '%88';
	histogram['\u2030'] = '%89';
	histogram['\u0160'] = '%8A';
	histogram['\u2039'] = '%8B';
	histogram['\u0152'] = '%8C';
	histogram['\u008D'] = '%8D';
	histogram['\u017D'] = '%8E';
	histogram['\u008F'] = '%8F';
	histogram['\u0090'] = '%90';
	histogram['\u2018'] = '%91';
	histogram['\u2019'] = '%92';
	histogram['\u201C'] = '%93';
	histogram['\u201D'] = '%94';
	histogram['\u2022'] = '%95';
	histogram['\u2013'] = '%96';
	histogram['\u2014'] = '%97';
	histogram['\u02DC'] = '%98';
	histogram['\u2122'] = '%99';
	histogram['\u0161'] = '%9A';
	histogram['\u203A'] = '%9B';
	histogram['\u0153'] = '%9C';
	histogram['\u009D'] = '%9D';
	histogram['\u017E'] = '%9E';
	histogram['\u0178'] = '%9F';

	// Begin with encodeURIComponent, which most resembles PHP's encoding functions
	ret = encodeURIComponent(ret);

	for (unicodeStr in histogram) {
		hexEscStr = histogram[unicodeStr];
		ret = replacer(unicodeStr, hexEscStr, ret); // Custom replace. No regexing
	}

	// Uppercase for full PHP compatibility
	return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
		return "%"+m2.toUpperCase();
	});
}

function empty(mixed_var)
{
	var key;
    
    if(mixed_var === ""
        || mixed_var === 0
        || mixed_var === "0"
        || mixed_var === null
        || mixed_var === false
        || mixed_var === undefined
    )
	{
        return true;
    }
	
    if(typeof mixed_var == 'object')
	{
        for(key in mixed_var)
		{
            if(typeof mixed_var[key] !== 'function' )
			{
              return false;
            }
        }
		
        return true;
    }
	
    return false;

}
