/*
// +----------------------------------------------------------------------+
// | Orginial Code Care Of:                                               |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// |         http://blog.bitflux.ch/p1735.html                            |
// +----------------------------------------------------------------------+
//
// +----------------------------------------------------------------------+
// | Heavily Modified by Jeff Minard (07/09/04)                           |
// +----------------------------------------------------------------------+
// | Author: Jeff Minard <jeff-js@creatimation.net>                       |
// |         http://www.creatimation.net                                  |
// +----------------------------------------------------------------------+
*/

/*--------------------------------------
	User configured variables
--------------------------------------*/
var inputId = ''; 
					// This is the id on the input/textarea that you want to use as the query.
									
var outputId = '';
 					// use this to have the results populate your own ID'd tag.
					// leave it blank and a div tag will automatically be added
					// with an id="liveSearchResults"
					
var processURI    = '';
					// this is the file that you request data from.
var processQuery  = 's';
					// search key (default s) in the get string.
									
var emptyString   = '';
					// What to display in the results field when there's nothing
					// Leaving this null will cause the results field to be set to display: none
									
var workingString = '';
					// What to display in the results field while looking for updates

/*--------------------------------------
	Script Stuff
--------------------------------------*/
var liveReq 	= false;
var t 			= null;
var liveReqLast = "";
var isIE 		= false;

var inputElement;
var outputElement;

// on !IE we only have to initialize it once
if (window.XMLHttpRequest)
	liveReq = new XMLHttpRequest();

function liveReqInit() {
	//alert('init');
	
	if( processURI == '' 
	||  inputId == ''
	||  outputId == ''
	) {
		return false;
	}
	
	inputElement  = document.getElementById(inputId);
	outputElement = document.getElementById(outputId);
	
	if( inputElement == null || outputElement == null ) 
		return;
	
	
	if (navigator.userAgent.indexOf("Safari") > 0) {
		inputElement.addEventListener("keypress",liveReqStart,false);
		inputElement.addEventListener("blur",liveReqClearSoon,false);
		
	} else if (navigator.product == "Gecko") {
		inputElement.addEventListener("keypress",liveReqStart,false);
		inputElement.addEventListener("blur",liveReqClearSoon,false);
		
	} else {
		inputElement.attachEvent('onkeydown',liveReqStart);
		inputElement.attachEvent('onblur',liveReqClearSoon);
		isIE = true;
	}
	
	if(emptyString == '') {
		// set the result field to hidden, or to default string
		outputElement.style.display = "none";
	} else {
		outputElement.innerHTML = emptyString;
	}
};

addLoadEvent(liveReqInit);

function liveReqStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveReqDoReq()",400);
};

function liveReqDoReq() {
	//alert('doReq');
	if (liveReqLast != inputElement.value && inputElement.value != "") {
		outputElement.innerHTML = workingString;
		if (liveReq && liveReq.readyState < 4) {
			liveReq.abort();
		}
		if (window.XMLHttpRequest) {
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			liveReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		liveReq.onreadystatechange = liveReqProcessReqChange;
		
		var url = processURI + "?livereq=1&"+ processQuery +"=" + encodeURI(inputElement.value);
		//alert('url:'+url);
		
		liveReq.open("GET", url);
		liveReqLast = inputElement.value;
		liveReq.send(null);
	} else if(inputElement.value == "") {
		if(emptyString == '') {
			liveReqClear();
		} else {
			outputElement.innerHTML = emptyString;
		}
	}
};

function liveReqProcessReqChange() {
	//alert('change:'+liveReq.readyState);
	if (liveReq.readyState == 4) {
		outputElement.innerHTML = liveReq.responseText;
		if(emptyString == '') {
			outputElement.style.display = "block";
		}
	}
};

function liveReqClear() {
	outputElement.innerHTML = '';
	outputElement.style.display = "none";
};


function liveReqClearSoon() {
	window.setTimeout("liveReqClear()",500);
};


/* Care Of: Simon Willison
http://simon.incutio.com/archive/2004/05/26/addLoadEvent

******** USEAGE ********************************
addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
addLoadEvent(function() {
  // more code to run on page load 
});
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
};