// define all the starting variables
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// id of the search form
var searchFormId = 'search-form'; 

// id of the search form input field
var searchInputId = 'search-input';

// search form input messages
var searchInputMessage1 = 'Zoek en selecteer hier';
var searchInputMessage2 = 'LiveSearch failed: submit form now?';

// id of the search form submit button
var searchSubmitId = 'search-submit';

// 'searching' image vars
var searchingImage = docroot + 'images/searching.gif';
var searchingImageDivId = 'searching-image';
var searchingImageWidth = 13;
var searchingImageHeight = 13;
var searchingImagePosX = 185;
var searchingImagePosY = 3;

// id of the div we wish to create and display the results in
//var resultsDivId = 'livesearch-results';

// set intial vars
var liveSearchReq = false;
var keyPressDelay = null;
var liveSearchLast = '';
var isIE = false;
// on IE we only have to initialize it once
if (window.XMLHttpRequest) {liveSearchReq = new XMLHttpRequest();}

// set the timing vars
var processTimerCount = 0;
var timedOut = 0;


// prep functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// create the results div _once_ on page load
// (this function is called from the global function running script inside general_scripts.js)
function runLSPrep()
	{
	// set vars to simplify code
	var searchForm = document.getElementById(searchFormId);
	var searchInput = document.getElementById(searchInputId);
	var searchSubmit = document.getElementById(searchSubmitId);
	
	// hide the submit button for the form
	searchSubmit.style.display = 'none';
	
	// disable autocomplete on the search box
	searchInput.setAttribute('autocomplete', 'off');

	// set initial search field text
	searchInput.value = searchInputMessage1;
	
	// event listener for clearing the search field text
	addEvent(searchInput, 'focus', clearSearchText, false);
	
	// event listener for starting the search
	addEvent(searchInput, 'keyup', liveSearchStart, false);
	
	// event listener for the escape key being pressed anywhere
	addEvent(document, 'keydown', escapeReset, false);
	
	// event listener for resetting things on blur
	addEvent(searchInput, 'blur', resetOnBlur, false);
	
	// build the 'searching' image and its containing div
	searchForm.style.position = 'relative';
	var newDiv = document.createElement('div');
	newDiv.id = searchingImageDivId;
	newDiv.style.backgroundColor = 'transparent';
	newDiv.style.border = '0';
	newDiv.style.display = 'none';
	newDiv.style.height = searchingImageWidth+'px';
	newDiv.style.width = searchingImageWidth+'px';
	newDiv.style.padding = '0';
	newDiv.style.position = 'absolute';
	newDiv.style.left = searchingImagePosX+'px';
	newDiv.style.top = searchingImagePosY+'px';
	searchForm.appendChild(newDiv);
	
	var newImage = document.createElement('img');
	newImage.setAttribute('src', searchingImage);
	newImage.setAttribute('height', searchingImageHeight);
	newImage.setAttribute('width', searchingImageWidth);
	newImage.style.border = '0';
	newImage.style.backgroundColor = 'transparent';
	newImage.style.margin = '0';
	newImage.style.padding = '0';
	newDiv.appendChild(newImage);
	}


// livesearch functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function liveSearchStart(e)
	{
	// if the 'escape' key has been pressed while typing in the search box
	if (e.keyCode == 27)
		{
		resetEverything();
		return false;
		}
	
	// set vars to simplify code
	var searchSubmit = document.getElementById(searchSubmitId);
	var searchInput = document.getElementById(searchInputId);
	
	// hide the submit button for the form
	searchSubmit.style.display = 'none';
	
	// clear the keyPressDelay if it exists from before
	if (keyPressDelay)
		{
		window.clearTimeout(keyPressDelay);
		}
	
//	if (searchInput.value != '')
	if (searchInput.value.length > 2)	
		{
		// wait 0.8 seconds after a keypress before running the search
		keyPressDelay = window.setTimeout('liveSearchDoSearch()', 800);
		}
	return true;
	}

function liveSearchDoSearch()
	{
		// set vars to simplify code
		var searchInput = document.getElementById(searchInputId);
		var searchingImageDiv = document.getElementById(searchingImageDivId);
		
		
		searchingImageDiv.style.display = 'block';
		processTimerCount = 0;
		processTimer();
		loadXMLDoc(docroot + 'search.php?s=' + searchInput.value);

	}

// timer functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// this times how long the whole process takes
// if it takes more that 3 seconds then it kills the xmlhttp request and lets the user submit the form as normal
function processTimer()
	{
	// if process running normally
	if (processTimerCount != -1)
		{
		// loop if process time still under 3 seconds
		if (processTimerCount < 3)
			{
			processTimerCount++;
			setTimeout('processTimer()', 1000);
			}
		// timed out, process taken 3 seconds already
		else
			{
			// set vars to simplify code
			var searchForm = document.getElementById(searchFormId);
			var searchInput = document.getElementById(searchInputId);
			var searchSubmit = document.getElementById(searchSubmitId);
			var searchingImageDiv = document.getElementById(searchingImageDivId);
			
			searchingImageDiv.style.display = 'none';
			searchSubmit.value = searchInputMessage2;
			searchSubmit.style.display = 'block';
			
			processTimerCount = 0;
			
			liveSearchHide();
			liveSearchReq.abort();
			return false;
			}
		}
	else
		{
		processTimerCount = -1;
		}
	return true;
	}






// reset and clear functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function resetEverything()
	{
	// set vars to simplify code
	var searchInput = document.getElementById(searchInputId);
	var searchSubmit = document.getElementById(searchSubmitId);
	var searchingImageDiv = document.getElementById(searchingImageDivId);
	
	searchInput.value = searchInputMessage1;
	searchSubmit.style.display = 'none';
	searchingImageDiv.style.display = 'none';
	
	liveSearchReq.abort();
	liveSearchHide();
	}

function resetOnBlur()
	{
	// set vars to simplify code
	var searchInput = document.getElementById(searchInputId);
	var searchSubmit = document.getElementById(searchSubmitId);
	var searchingImageDiv = document.getElementById(searchingImageDivId);

	if (searchInput.value == '')
		{
		searchInput.value = searchInputMessage1;
		searchSubmit.style.display = 'none';
		searchingImageDiv.style.display = 'none';
		
		liveSearchReq.abort();
		liveSearchHide();
		}
	}

// if a key has been pressed when focus was outside the search box
function escapeReset(e)
	{
	// if the 'escape' key was been pressed
	if (e.keyCode == 27)
		{
		resetEverything();
		return false;
		}
	}

function clearSearchText()
	{
	// set vars to simplify code
	var searchInput = document.getElementById(searchInputId);
	
	if (searchInput.value == searchInputMessage1)
		{
		searchInput.value = '';
		}
	}

function liveSearchHide()
	{
	// set vars to simplify code
//	var resultsDiv = document.getElementById(resultsDivId);
	
//	resultsDiv.style.display = 'none';
	}

