/*
// +----------------------------------------------------------------------+
// | 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)                           |
// +----------------------------------------------------------------------+
// | Same stuff as above, yo!                                             |
// +----------------------------------------------------------------------+
// | Author: Jeff Minard <jeff-js@creatimation.net>                       |
// |         http://www.creatimation.net                                  |
// +----------------------------------------------------------------------+
//
//
// +----------------------------------------------------------------------+
// | What is this nonsense?? (07/09/04)                                   |
// +----------------------------------------------------------------------+
// | This is a script that, by using XMLHttpRequest javascript objects    |
// | you can quickly add some very click live interactive feed back to    |
// | your pages that reuire server side interaction.                      |
// |                                                                      |
// | For instance, you use this to emulate a "live searching" feature     |
// | wherein users type in key phrases, and once they have stopped typing |
// | the script will automatically search and retrive *without* a page    |
// | reload.
// |                                                                      |
// | In another instance, I use this to product live comments by passing  |
// | the text to a Textile class that parses it to valid HTML. After      |
// | parsing, the html is returned and displayed on the page as the       |
// | user types.                                                          | 
// +----------------------------------------------------------------------+
*/

/*--------------------------------------
	User configured variables
--------------------------------------*/
var searchFieldId = 'comment'; 
					// This is the id on the input/textarea that you want to use as the query.
									
var resultFieldId = 'previewcomment';
 					// 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    = '/pt.php';
					// this is the file that you request data from.
									
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

/*--------------------------------------
	Script Stuff
--------------------------------------*/
var liveReq = false;
var t = null;
var liveReqLast = "";
var isIE = false;

// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveReq = new XMLHttpRequest();
}

function liveReqInit() {
	
	if (navigator.userAgent.indexOf("Safari") > 0) {
		document.getElementById(searchFieldId).addEventListener("keydown",liveReqStart,false);
		
	} else if (navigator.product == "Gecko") {
		document.getElementById(searchFieldId).addEventListener("keypress",liveReqStart,false);
		
	} else {
		document.getElementById(searchFieldId).attachEvent('onkeydown',liveReqStart);
		isIE = true;
	}
	
	if(resultFieldId == '') {
		// if a result field isn't on the page, this will sneak one in...
		resultFieldId = "liveRequestResults";
		displayArea = document.createElement('div');
		displayArea.id = "liveRequestResults";
		document.getElementsByTagName('body')[0].appendChild(displayArea);		
	}
	
	if(emptyString == '') {
		// set the result field to hidden, or to default string
		document.getElementById(resultFieldId).style.display = "none";
	} else {
		document.getElementById(resultFieldId).innerHTML = emptyString;
	}
}

addLoadEvent(liveReqInit);

function liveReqStart() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveReqDoReq()",400);
}

function liveReqDoReq() {
	if (liveReqLast != document.getElementById(searchFieldId).value && document.getElementById(searchFieldId).value != "") {
		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;
		liveReq.open("GET", processURI + "?s=" + escape(document.getElementById(searchFieldId).value));
		liveReqLast = document.getElementById(searchFieldId).value;
		liveReq.send(null);
	} else if(document.getElementById(searchFieldId).value == "") {
		if(emptyString == '') {
			document.getElementById(resultFieldId).innerHTML = '';
			document.getElementById(resultFieldId).style.display = "none";
		} else {
			document.getElementById(resultFieldId).innerHTML = emptyString;
		}
	}
}

function liveReqProcessReqChange() {
	if (liveReq.readyState == 4) {
		document.getElementById(resultFieldId).innerHTML = liveReq.responseText;
		if(emptyString == '') {
			document.getElementById(resultFieldId).style.display = "block";
		}
	}
}