/*
// +----------------------------------------------------------------------+
// | 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                            |
// +----------------------------------------------------------------------+
//
//
// +----------------------------------------------------------------------+
// | Permission to use this script has been provided by Jeff Minard -     |
// | We greatly appreciate his generosity in allowing to use his          |
// | modifications freely                                                 |
// +----------------------------------------------------------------------+
//
// +----------------------------------------------------------------------+
// | 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
--------------------------------------*/
// This is the id on the input/textarea that you want to use as the query.
var searchFieldId = 'liveSearch'; 
									
// 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 resultFieldId = 'liveSearchResults';
									
// this is the file that you request data from.
var processURI    = '/search';
									
// 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 emptyString   = '';

/*--------------------------------------
	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;
  }
	
}

function liveReqStart() {
  if (t) {
    window.clearTimeout(t);
  }
  t = window.setTimeout("liveReqDoReq()",400);
}

function one() {
  alert('error');
}

function liveReqDoReq() {
  if (liveReqLast != document.getElementById(searchFieldId).value && document.getElementById(searchFieldId).value != "") {
		
    if (liveReq && liveReq.readyState < 4) {
      liveReq.abort();
    }
    if (isIE) {
      liveReq = new ActiveXObject("Microsoft.XMLHTTP");
      liveReq.onreadystatechange = liveReqProcessReqChange;
    } else {
      //liveReq.onload = liveReqProcessReqChange;
      liveReq.onreadystatechange = liveReqProcessReqChange;
      liveReq.onerror = one;
    }		

    var getURL = processURI + "?s=" +  escape(document.getElementById(searchFieldId).value);
		
    liveReq.open("GET", getURL);
    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) {
    if (liveReq.status==200) {
      document.getElementById(resultFieldId).innerHTML = liveReq.responseText;
      if(emptyString == '') { document.getElementById(resultFieldId).style.display = 'block'; }
    } else if (liveReq.status==404) {
      alert("Sorry, it broke!");
    } else {
      alert("What the heck broke? This did: "+xmlhttp.status);
    }
  }
}

