/*
// +----------------------------------------------------------------------+
// | 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>                              |
// +----------------------------------------------------------------------+

*/
var link = '';
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var saq='Wat the **';
var instRsltSpan = '';
var instShadow = '';
var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
}

function liveSearchInit() {
	
	if (navigator.userAgent.indexOf("Safari") > 0) {
		document.getElementById('user').addEventListener("keydown",liveSearchKeyPress,false);
//		document.getElementById('user').addEventListener("blur",liveSearchHide,false);
	} else if (navigator.product == "Gecko") {
		
		document.getElementById('user').addEventListener("keypress",liveSearchKeyPress,false);
		document.getElementById('user').addEventListener("blur",liveSearchHideDelayed,false);
		
	} else {
		document.getElementById('user').attachEvent('onkeydown',liveSearchKeyPress);
//		document.getElementById('user').attachEvent("onblur",liveSearchHide,false);
		isIE = true;
	}
	
	document.getElementById('user').setAttribute("autocomplete","off");

}

function liveSearchHideDelayed() {
	window.setTimeout("liveSearchHide()",400);
}
	
function liveSearchHide() {
	document.getElementById(instRsltSpan).style.display = "none";
	var highlight = document.getElementById("LSHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}

function liveSearchKeyPress(event) {

	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById(instShadow).firstChild.firstChild;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","LSHighlight");
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("LSHighlight");
		if (!highlight) {
			highlight = document.getElementById(instRsltSpan).firstChild.firstChild.lastChild;
		} 
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","LSHighlight");
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		highlight = document.getElementById("LSHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById(instRsltSpan).style.display = "none";
	} 
}
function liveSearchStart(frmName,fieldName,id_1,id_2 ) {
	
	if (typeof id_1!="undefined" && id_1!='') instRsltSpan=id_1;		else	 instRsltSpan='LSResult';
	if (typeof id_2!="undefined" && id_2!='') instShadow=id_2;		else	 instShadow='LSShadow';
	
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("liveSearchDoSearch('"+frmName+"','"+fieldName+"')",200);

}

function liveSearchDoSearch(frmName,fieldName) {

	frm = eval("document.forms."+frmName);
	fieldValue = eval("document.forms."+frmName+"."+fieldName+".value");
	//alert(fieldValue)
	
	if (typeof liveSearchRoot == "undefined") {
		liveSearchRoot = "";
	}
	if (typeof liveSearchRootSubDir == "undefined") {
		liveSearchRootSubDir = "";
	}
	if (typeof liveSearchParams == "undefined") {
		liveSearchParams = "";
	}
	
	if (liveSearchLast != fieldValue) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	if ( fieldValue == "") {
		liveSearchHide();
		return false;
	}
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	liveSearchReq.onreadystatechange= liveSearchProcessReqChange;

	liveSearchReq.open("GET", link + "ajax/institutions.lib.php?spID="+instRsltSpan+"&fieldName="+fieldName+"&search_term=" + fieldValue + liveSearchParams);
	liveSearchLast = fieldValue;
	liveSearchReq.send(null);
	}
}

function liveSearchProcessReqChange() {
	
	if (liveSearchReq.readyState == 4) {
		var  res = document.getElementById(instRsltSpan);
		res.style.display = "block";
		var  sh = document.getElementById(instShadow);
		
		sh.innerHTML = liveSearchReq.responseText;
		 
	}
}

function liveSearchSubmit() {
	var highlight = document.getElementById("LSHighlight");
	if (highlight && highlight.firstChild) {
		window.location = liveSearchRoot + liveSearchRootSubDir + highlight.firstChild.nextSibling.getAttribute("href");
		return false;
	} 
	else {
		return true;
	}
}

function showProfile(u) {
        window.location = link + "profile.php?uname=" + u;
}

// from http://www.quirksmode.org/js/eventexample.html
function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }

  else if (document.layers)
  {
	this.obj = getObjNN4(document,name);
	this.style = this.obj;
  }
}

function getObjNN4(obj,name)
{
	var x = obj.layers;
	var thereturn;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	thereturn = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) thereturn = tmp;
	}
	return thereturn;
}

function rowHighlight(id) {
  var row = new getObj(id);
  row.style.backgroundColor = "#006699";
  row.style.color = "white";
  row.style.cursor = "pointer";
  row.style.cursor = "hand";
}

function rowUnhighlight(id) {
  var row = new getObj(id);
  row.style.backgroundColor = "white";
  row.style.color = "black";
  row.style.cursor = "hand";
  row.style.cursor = "pointer";
}

function getValue(fieldName, val){		
	var len = val.length;
	for(i=0; i<len; i++){
		val = val.replace('*',"'")
	}	
	
	document.getElementById(fieldName).value = val;	
	document.getElementById(instRsltSpan).style.display = 'none';
}

function hideList(spID){
	if (document.getElementById(spID))
	 	document.getElementById(spID).style.display='none';
}
