	
// CONFIGURATION

// how many matches to show at most, must be even
fh_showmatches=14;

// END CONFIGURATION
var currentFocus;
var _d=document;
var isnotopera=true;
function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie5=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:this.ie5;
	this.ie4=(document.all && !this.dom)?1:0;
	this.opera=(document.all && navigator.userAgent.toLowerCase().indexOf("opera"))?1:0
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie5 || this.ie4 || this.ns4 || this.ns5)
	return this
}
bw=new checkBrowser()

if (!(bw.dom && !bw.ie5) && !bw.ns5 && !bw.ns4) { 

document.body.onmousedown=fh_EBlur;
}
if (bw.opera) {
	width="width:160px";
} else {
	isnotopera=true;
	width="min-width:160px";
}
_d.write("<div id=funchelper style=\"background-color: white; border: 1px solid black; top: 90px;"+width+"; padding: 4px; font-size: 12px; visibility:hidden; position:absolute;z-index:10000\"></div>");



/*function ElLeft(eE)
{var DL_bIE=_d.all?true:false;var nLP=eE.offsetLeft;var ePE=eE.offsetParent;
while(ePE!=null){if(DL_bIE){if(ePE.tagName=="TD")nLP+=ePE.clientLeft;}else{if(ePE.tagName=="TABLE"){
var nPB=parseInt(ePE.border);if(isNaN(nPB)){var nPF=ePE.getAttribute('frame');if(nPF!=null)nLeftPos+=1;}else
if(nPB>0)nLP+=nPB;}}nLP+=ePE.offsetLeft;ePE=ePE.offsetParent;}return nLP;}
 */

function getLocation(w) {
	loc=0;
	if (bw.dom || bw.ie4 || bw.opera) {
	loc+=(w=='x')?eval(this.offsetLeft):eval(this.offsetTop)
	parnt=eval(this.offsetParent)
	while (parnt) {
	loc+=(w=='x')?eval(parnt.offsetLeft):eval(parnt.offsetTop)
	parnt=eval(parnt.offsetParent)
		}
	}
else { 
	loc=eval("this."+w)
	}

return loc		
}

fh_matches=new Array();
fh_inmenu=0;
fh_menupos=0;
fh_matchesjoined="";
fh_currenttext="";


function fh_IsMatch(idx,pr)
{

	if (fcl[which][idx].substring(0,pr.length)==pr) return true;
	return false;
}

function fh_FindMatches(pr)
{

	f=0;
	l=fcl[which].length-1;
	m=(f+l)>>1;
	while(f<l) {
		if (fcl[which][m]==pr) break;
		if (fcl[which][m]<pr) {
			f=m;
		} else {
			l=m;
		}
		nm=(f+l+1)>>1;
		if (m==nm) break;
		m=nm;
	}
	if (m&&fh_IsMatch(m-1,pr)) m--;
	if (!fh_IsMatch(m,pr) && m<(fcl[which].length-1) && fh_IsMatch(m+1,pr)) m++;
	res=new Array;
	while (m<fcl[which].length && fh_IsMatch(m,pr)) {
		//res.push(fcl[which][m++]);
	  res[res.length]=fcl[which][m++];
	}
	return res
}

function fh_Show(what)
{
	formula=((ylocation=='above')?(-((flen>15)?15:flen)*15-10):18)
	tdv=_d.getElementById("funchelper");
	ts=tdv.style;
	if (what=="") {
		if (ts.visibility!="hidden")
			ts.visibility="hidden";
	} else {
		ts.visibility="inherit";

		f_p.getLocation=getLocation
		ts.left=(f_p.getLocation("x"))+"px";
		ts.top=(f_p.getLocation("y"))+eval(formula)+"px";
		tdv.innerHTML=what;
	}
}

function setField(val) {
f_p.value=val;
fh_HideAll();
//f_p.clearFields=clearFields;
//f_p.clearFields()
return false; }

function fh_HideAll()
{
	fh_matches=new Array();
	fh_matchesjoined="";
	fh_inmenu=0;
	fh_menupos=0;
	fh_Show("");
}


function fh_ShowNoMatch()
{
	flen=1;
	fh_Show("<font color=\"gray\">No entry found.</font>");
}

function fh_UpdateMenu()
{
	flen=fh_matches.length;
	if (flen<=fh_showmatches) {
		first=0;
		last=flen-1;
		beforedots=0;
		afterdots=0;
	} else {
		if (fh_inmenu) {
			mid=fh_showmatches>>1;
			if (fh_menupos<=mid) {
				beforedots=0;
				first=0;
			} else {
				beforedots=1;
				first=fh_menupos-mid+1;
				if (first>(flen-fh_showmatches+1))
					first=flen-fh_showmatches+1;
			}
			if (fh_menupos>=(flen-mid-1)) {
				afterdots=0;
				last=flen-1;
			} else {
				afterdots=1;
				last=fh_menupos+mid-1;
				if (last<(fh_showmatches-2))
					last=fh_showmatches-2;
			}
		} else {
			first=0;
			last=fh_showmatches-2;
			beforedots=0;
			afterdots=1;
		}
	}

	zh="";
	if (beforedots)
		zh=zh+"...<br />";
	for (pos=first; pos<=last; pos++) {
		f=fh_matches[pos];
		zh=zh+"<a href=\"#\" onClick=\"javascript:setField(\'"+f+"\')\" style=\"text-decoration:none;"+(fh_inmenu&&fh_menupos==pos?"background-color:rgb(204,204,255);":"")+"\">"+f+"</a><br />";
	}
	if (afterdots)		zh=zh+"...";
	fh_Show(zh);
	if (fh_inmenu) {
f_p.value	=fh_matches[fh_menupos];
	}
}

function fh_NewText()
{


	t=f_p.value.toLowerCase();
	if (t=="") {
		fh_HideAll();
		return;
	}
	tmpmatches=fh_FindMatches(t);
	if (tmpmatches.length==0) {
		fh_matchesjoined="";
		fh_ShowNoMatch();
		return;
	}
	if (tmpmatches.join(",")==fh_matchesjoined) return; // do nothing
	fh_inmenu=0;
	fh_menupos=0;
	fh_matchesjoined=tmpmatches.join(",");
	fh_matches=tmpmatches;

	fh_UpdateMenu();
}

function fh_EFocus()
{
f_p	=this
	checkField(this)

	this.autocomplete="off";
if (	currentFocus!=this && this.value) {	fh_NewText(); 
	currentFocus=this }

}

function fh_EBlur()
{
f_p	.autocomplete="off";
	fh_HideAll();
}

function fh_EKeyPress(evt)
{
		
	evt=(evt)?evt:((event)?event:null);
	var charCode=(evt.charCode || evt.charCode==0)?evt.charCode:((evt.keyCode)?evt.keyCode:evt.f_p);
	if (charCode==10 || charCode==13 || charCode==0) return false;
	if (charCode==32) {
		p=f_p.value;
		if (!p.length || fh_inmenu) return false;
/*		matches=fh_FindMatches(p);
		if (matches.length==0) return true;
		if (matches.length==1) {
		f_p.value	=matches[0];
			return false;
		}
		if (isnotopera) {
			len=0;
			first=matches[0];
			//	last=matches.pop();
				last=matches[matches.length-1];matches.length-=1;
			while (len<first.length && first.substring(0,len+1)==last.substring(0,len+1)) len++;
			if (f_p.value!=first.substring(0,len)) {
			f_p.value	=first.substring(0,len);
			}
		}
		return false; */
	}
	if ((charCode>=97 && charCode<=122) || (charCode>=65 && charCode<=90) || (charCode>=48 && charCode<=57) || charCode==95)
		return true; // a-z A-Z 0-9 _

	return true;
}


function fh_EKeyDown(evt)
{
if (!window.fcl[which]) return;

	evt=(evt)?evt:((event)?event:null);
	if (!evt) return true;
	var charCode=evt.charCode?evt.charCode:((evt.keyCode)?evt.keyCode:evt.f_p);
	if (charCode==10 || charCode==13) return false;
	if (charCode==38 || charCode==57385) { // up
		if( _d.getElementById('funchelper').style.visibility!='inherit') return false;
		if (fh_inmenu) {
			fh_menupos--;
			if (fh_menupos<0) {
				fh_menupos=fh_matches.length-1;
			}
		} else {
			fh_inmenu=1;
			fh_menupos=fh_matches.length-1;
		}
		fh_UpdateMenu();
		return false;
	} else
	if (charCode==40 || charCode==57386) { // down
		if( _d.getElementById('funchelper').style.visibility!='inherit') return false;
		if (fh_inmenu) {
			fh_menupos++;
			if (fh_menupos>=fh_matches.length)
				fh_menupos=0;
		} else {
			fh_inmenu=1;
			fh_menupos=0;
		}
		fh_UpdateMenu();
		return false;
	} else
	if (charCode==39) { // right
		if (isnotopera) {
			matches=fh_FindMatches(f_p.value);
			if (matches.length==1) {
			f_p.value	=matches[0];
				return false;
			}
			len=0;
			first=matches[0];
			//	last=matches.pop();
			last=matches[matches.length-1];matches.length-=1;
			while (len<first.length && first.substring(0,len+1)==last.substring(0,len+1)) len++;
			if (f_p.value!=first.substring(0,len)) {
			f_p.value	=first.substring(0,len);
			}
		}
	return true
	}
	return true;
}

function fh_EKeyUp(evt)
{  
if (!window.fcl[which]) return;
	evt=(evt)?evt:((event)?event:null);
	if (!evt) return true;
	var charCode=evt.charCode?evt.charCode:((evt.keyCode)?evt.keyCode:evt.f_p);

	if (charCode==10 || charCode==13) { fh_HideAll();return false;  }
	if (charCode==38 || charCode==40 || charCode==39 || charCode==57385 || charCode==57386) return false;
	if (f_p.value!=fh_currenttext) {
		fh_currenttext=f_p.value; 
		fh_NewText();
	}
	return true;
}

function cancel(evt) {
window.event.cancelBubble=true
}
list=new Array();
function buildMenu(t,oF3) {
	f=t.name;
	for (i in oF3) { 
		eval("var o="+oF3[i]);
		n=o.name
		v=o.value;
		if (v) {
			eval("list[i]=window."+n+"Array[v]."+f);
			}
		else {list[i]=-1; }
		}
	if (list[0]!=-1 && list[1]!=-1) { 	fh_matches=array_intersect(list[0],list[1]); }
	else if (list[0]<'1' && list[1]<'1') { fh_ShowNoMatch();return; }
	else {	fh_matches=(list[0]!=-1 && list[0])?list[0].split('~'):list[1].split('~'); }
	fh_inmenu=0;
	fh_menupos=0;
	fh_UpdateMenu()
	list[0]='';list[1]=''
}

function checkField(t) {
	showMenu=0;
	var n=t.name
	var oF2=otherFields[n].split("~");	
	for (var i in oF2) { 
		eval("var o="+oF2[i]);	
		if (!o.value.length) continue
			showMenu=1;
		if (t.value) {
			eval("regexp=/"+t.value.replace("/","\\\/")+"/");
			eval("validV="+o.name+"Array[o.value]."+n);
			if (!validV.match(regexp))  t.value='';
			}
		}

	if (!arguments[1] && showMenu)  { buildMenu(t,oF2); }
}

function clearFields() {

	eval("if (!window."+this.name+"Array[this.value])  { this.value=''}");
	if (!this.value) return; 
	var n=this.name
	var oF=otherFields[n].split("~");	
	for (var i in oF) {
		eval("var obj="+oF[i]); //alert(obj.name);
		checkField(obj,1); 
		}	 
}


function array_intersect(a1,a2) {
	result=new Array();
	a1=a1.split('~')
	var re=new RegExp();
	for (var i in a1) {
		re.compile(a1[i],"i");
		if (re.exec(a2)) {
			result[result.length]=a1[i];
			}
		}
	return result;
}
