/*** Author Natalia Bazhenova www.fh54.de (c) 2005 ***/
	
/********************************/
/*******start config****************/
/*******************************/
//array with suggest texts
//var opttext=new Array(); 
//fill array 
//opttext = ["adolescencia","adolescente","adulto","afectivo","agresividad","aprendizaje", "aprendizaje escolar", "autoestima","biología","cambios","ciudadana","cívica","comportamiento","conflictos","cotidiana","creencias","crianza","cultura","democracia","desarrollo","educación","educativas","emocional","escolar","escuela","familia","filosofía","género","global","infantil","inteligencia","maternidad","moral","motivación","motivaciones","multiculturalidad","naturaleza","niño","organización","orientación","pareja","paternidad","personalidad","políticas","positiva","práctica","prejuicios","prevención","problemas","psicología","religiosidad","resolución","responsabilidad","sexualidad","sociales","socialización","temperamento","valores","virtudes"];
//opttext[0]="Amsterdam"
//opttext[1]="Berlin"
//opttext[2]="Bern"
//opttext[3]="Prag"
//opttext[4]="Paris"
//opttext[5]="Vienna"
//opttext[6]="Warsaw"
//opttext[7]="Rome"
//opttext[8]="Riga"
//opttext[9]="Tallinn"
//opttext[10]="Vilnius"
//opttext[11]="Lisbon"
//opttext[12]="Madrid"
//opttext[13]="Athens"
//opttext[14]="Sofia"
//opttext[15]="London"
//opttext[16]="Bucharest"
//opttext[17]="Budapest"
//opttext[18]="Bratislava"
//opttext[19]="Ljublana"
//opttext[20]="Beograd"
//opttext[21]="Brussels"
//opttext[22]="Helsinki"
//opttext[23]="Stockholm"
//opttext[24]="Copenhagen"
//opttext[25]="Oslo"
//opttext[26]="Dublin"
//opttext[27]="Vaduz"
//id of the textbox element
var inputID="categoria"
//max size of "selectbox"
var maxcount=5 
/*******************************/
/**********end config***********/
/*******************************/

opttext.sort();
var scripting=false;
//initialize:
//for mozilla
if (window.captureEvents) {
    window.captureEvents(Event.LOAD)
    window.onload=suggestInput_init;
	}
//for ie
document.onreadystatechange=ieInit;
function ieInit()
{
   if (document.readyState=="complete")
   {
      document.body.onload=function() {suggestInput_init()}
   }
}
var SIs
var SItxt
var newdiv=document.createElement("DIV")
var globalN=0; //how much options scrolled up
//for ie
if (document.attachEvent)
document.attachEvent("onclick",hideSelect)
// for Mozilla
if (document.captureEvents) {
document.captureEvents(Event.CLICK);
document.onclick = hideSelect;
}
function hideSelect() {
newdiv.style.display="none"
}
function suggestInput_init() {
if (document.createElement("DIV")) {
scripting=true;
SIs=document.createElement("SELECT")
SIs.onkeyup=function(e){if(!e) e=event; setInputValue(this.selectedIndex,e)}
SIs.className="select_input"
SIs.setAttribute("id","selectInput");
SIs.style.position="absolute"
SIs.style.top="-9999px"
SIs.style.left="-9999px"
SIs.style.visibility="hidden"
document.body.appendChild(SIs)
SItxt =document.getElementById(inputID);
 SItxt.setAttribute("autocomplete","OFF")
 SItxt.onkeyup=function(e){if (!e) e=event; showSelection(this.value,e)}
SItxt.focus()
for (i=0;i<opttext.length;i++) {
o=document.createElement("OPTION");;
o.innerHTML=opttext[i];
SIs.appendChild(o)
SIs.style.visibility="visible"; // for Opera
}
// find the actual position of the input element 
topdiv=0;leftdiv=0; elt=SItxt 
do {
 b=(elt.style.borderTopWidth)?(elt.style.borderTopWidth):(0)
 b=(b)?(parseInt(b.substring(0,b.length-2))):(0);
 p=(elt.style.paddingTop)?(elt.style.paddingTop):(0);
 p=(p)?(parseInt(p.substring(0,p.length-2))):(0)
 b2=(elt.style.borderLeftWidth)?(elt.style.borderLeftWidth):(0)
 b2=(b2)?(parseInt(b2.substring(0,b2.length-2))):(0);
 p2=(elt.style.paddingLeft)?(elt.style.paddingLeft):(0);
 p2=(p2)?(parseInt(p2.substring(0,p2.length-2))):(0)
 try {
 topdiv+=elt.offsetTop+b+p; leftdiv+=elt.offsetLeft+b2+p2;  } catch(err) {} elt=elt.offsetParent; 
   } 
  while (elt.tagName!="BODY")
  
topdiv+=SItxt.offsetHeight
widthdiv=SItxt.offsetWidth

newdiv.style.backgroundColor="#ffffff"
newdiv.style.position="absolute"
newdiv.style.zIndex="100"
newdiv.style.border="1px solid black"
newdiv.style.top=topdiv;newdiv.style.left=leftdiv;newdiv.style.width=widthdiv;
newdiv.style.display="none"
buildDiv(0)
document.body.appendChild(newdiv)
 }
} 
 
function buildDiv(n) {
if (n>SIs.childNodes.length) return false; 
for (i=0;i<newdiv.childNodes.length;i++)
{newdiv.removeChild(newdiv.childNodes[i]);i--}
if (n>0) {// insert top "..." - div
d1=document.createElement("DIV");
d1.id="lessDiv"
d1.style.width="100%"
d1.style.fontSize="0.8em"
d1.onmouseover=function() {this.style.backgroundColor="#000099";this.style.color="#ffffff"}
d1.onmouseout=function() {this.style.backgroundColor="#ffffff";this.style.color="#686868"}
d1.onclick=function() {buildDiv(n-1);
d1.style.backgroundColor="#000099";d1.style.color="#ffffff"
} 
d1.innerHTML="......"; 
newdiv.appendChild(d1); 
}
m=(maxcount<SIs.childNodes.length)?(maxcount):(SIs.childNodes.length)
for(i=0;i<m;i++) {
d=document.createElement("DIV")
d.style.width="100%"
d.style.fontSize="0.8em"
d.onmouseover=function() {
	this.style.backgroundColor="#000099";this.style.color="#ffffff";
	SItxt.value=this.innerHTML
}
d.onmouseout=function() {this.style.backgroundColor="#ffffff";this.style.color="#686868"}
d.onclick=function() {SItxt.value=this.innerHTML;newdiv.style.display="none"}
try {d.innerHTML=SIs.childNodes[i+n].innerHTML;}
catch(err) {}
newdiv.appendChild(d)
};
globalN=n;
if (SIs.childNodes.length-n>maxcount) {// insert bottom "..." - div
d2=document.createElement("DIV");
d2.id="moreDiv"
d2.style.width="100%"
d2.style.fontSize="0.8em"
d2.onmouseover=function() {this.style.backgroundColor="#000099";this.style.color="#ffffff"}
d2.onmouseout=function() {this.style.backgroundColor="#ffffff";this.style.color="#686868"}
d2.onclick=function() {
 buildDiv(n+1); d2.style.backgroundColor="#000099";d2.style.color="#ffffff"
  }
 d2.innerHTML="......"; d2.style.backgroundColor="#ffffff";d2.style.color="#686868"
 newdiv.appendChild(d2) } 
 }
 
function setInputValue(m,ev) {
if (!scripting) return;
isLess=(document.getElementById("lessDiv"))?(1):(0)
if (m>globalN+maxcount+isLess+1) {m=globalN+maxcount;SIs.selectedIndex=m}
if (m<isLess) {m=globalN-1;SIs.selectedIndex=globalN-1}
a=SIs.childNodes[m].innerHTML
SItxt.value=a;
try {
if (newdiv.childNodes[m-globalN+isLess]) {  if (newdiv.childNodes[m-globalN+isLess].id=="moreDiv") {  buildDiv(globalN+1);  
newdiv.childNodes[maxcount].style.backgroundColor="#000099";
newdiv.childNodes[maxcount].style.color="#ffffff"
return
  }
 }
} catch (err) {}
try {
if (newdiv.childNodes[m-globalN+isLess]) {  if (newdiv.childNodes[m-globalN+isLess].id=="lessDiv") {  buildDiv(globalN-1);
 isLess_new=(document.getElementById("lessDiv"))?(1):(0)
 newdiv.childNodes[isLess_new].style.backgroundColor="#000099";
 newdiv.childNodes[isLess_new].style.color="#ffffff"
 return
  }
 }
} catch (err) {}
try {
for (i=0;i<newdiv.childNodes.length;i++)
{
newdiv.childNodes[i].style.backgroundColor="#ffffff";
newdiv.childNodes[i].style.color="#686868";
}
 newdiv.childNodes[m-globalN+isLess].style.backgroundColor="#000099";
 newdiv.childNodes[m-globalN+isLess].style.color="#ffffff"
} catch(err) {};
 
if ((ev.keyCode!=40) && (ev.keyCode!=38) && (ev.keyCode!=0)) // if not arrow down, arrow up or mouseclick  
{
  newdiv.style.display="none"
  SItxt.focus();
 }
}
 
function showSelection(t,ev) {
if (!scripting) return;
if (ev.keyCode==40) { //with arrow down comes into suggestion select
 if (SIs.childNodes.length>0)
 {
  newdiv.childNodes[0].style.backgroundColor="#000099"
  newdiv.childNodes[0].style.color="#ffffff"
  SItxt.value=SIs.childNodes[0].innerHTML; 
  try {SIs.focus();} catch(err){}
  SIs.childNodes[0].selected=true;
 }
  return            }
if (t=="") { return ;}
t=t.toLowerCase();
l=t.length; 
for (i=0;i<SIs.childNodes.length;i++) {SIs.removeChild(SIs.childNodes[i]);i--}
for(i=0;i<opttext.length;i++)
{
 if (opttext[i].substr(0,l).toLowerCase()==t)
 {
  oOption = document.createElement("OPTION");
  SIs.appendChild(oOption)
  oOption.innerHTML = opttext[i];
 }
}
if (SIs.childNodes.length>0)  {
newdiv.style.display=""
buildDiv(0)
 } else newdiv.style.display="none";
SItxt.focus()
}
 
