/*
// +----------------------------------------------------------------------+
// | 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 inputId = 'query'; 
					// This is the id on the input/textarea that you want to use as the query.

var outputId = 'live';
 					// 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 pageId = 'p';
var processURI    = '/kereso/ft';
					// 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;

var inputElement;
var outputElement;

// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveReq = new XMLHttpRequest();
}

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be attached");
  }
} 
 

function liveReqInit() {

	inputElement  = document.getElementById(inputId);
	outputElement = document.getElementById(outputId);
	pageElement = document.getElementById(pageId);

	if( inputElement == null || outputElement == null ) 
		return;


	if (navigator.userAgent.indexOf("Safari") > 0) {
		inputElement.addEventListener("keydown",liveReqStart,false);

	} else if (navigator.product == "Gecko") {
		inputElement.addEventListener("keypress",liveReqStart,false);
		inputElement.addEventListener("focus",togglehelp,false);

	} else {
		inputElement.attachEvent('onkeydown',liveReqStart);
		inputElement.attachEvent("onfocus",togglehelp,false);
		isIE = true;
	}

	if(emptyString == '') {
		// set the result field to hidden, or to default string
		outputElement.style.display = "none";
		inputElement.style.color = '#777';
		inputElement.value = 'kereső...';
	} else {
		outputElement.innerHTML = emptyString;
	}
}

function liveReqStart(page) {
	if(window.event && window.event.keyCode == 13) {
		window.event.keyCode =0;
		return false;
	}

	if(page!=1) {
		pageElement.value = 0;
	}
	if (t) {
		window.clearTimeout(t);
	}
        outputElement.style.display = "block";
	outputElement.innerHTML = '<h3 id="status">Gyorskereső</h3><br><p>Ne üssön <b>Enter</b>-t a keresendő szöveg beírása után!</p>';
        document.getElementById('masterbanner').style.display = "none";
        if (liveReq && liveReq.readyState < 4) {
                liveReq.abort();
        }
	t = window.setTimeout("liveReqDoReq()",1000);
}

function liveReqDoReq() {
        if (inputElement.value == "" || inputElement.value == "kereső...") {
          pageElement.value=0;
        }
	if (liveReqLast != inputElement.value+pageElement.value && inputElement.value != "" && inputElement.value != "kereső...") {
		if (liveReq && liveReq.readyState < 4) {
			liveReq.abort();
		}
		if (window.XMLHttpRequest) {
			if(isIE) {
				liveReq = new XMLHttpRequest();
			}
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject || isIE) {
			liveReq = new ActiveXObject("Microsoft.XMLHTTP");
		}

		outputElement.innerHTML = '<h3 id="status">Keresés...</h3>';
		liveReq.onreadystatechange = liveReqProcessReqChange;
		liveReq.open("GET", processURI + "?query=" + encodeURI(inputElement.value) + "&p=" + pageElement.value);
		liveReqLast = inputElement.value+pageElement.value;
		liveReq.send(null);
	} else if(inputElement.value == "" || inputElement.value == "kereső...") {
                outputElement.innerHTML = '';
                outputElement.style.display = "none";
                document.getElementById('masterbanner').style.display = "block";
	}
}

function liveReqProcessReqChange() {
	if (liveReq.readyState == 4) {
		outputElement.innerHTML = liveReq.responseText;
	}
}

function liveReqPage(p) {
  pageElement.value=p;
  liveReqStart(1);
}

function togglehelp() {
	if(inputElement.value=="kereső...") {
		inputElement.value='';
		inputElement.style.color = '#000';
	} else {
		inputElement.value = '';
		liveReqDoReq();
		inputElement.style.color = '#777';
		inputElement.value = 'kereső...';
	}
}
function closesearch() {
		inputElement.value = '';
		liveReqDoReq();
		inputElement.style.color = '#777';
		inputElement.value = 'kereső...';
}

  
addEvent(window, 'load', liveReqInit, false);
