﻿// last modified by Thf/Ngl on 2008-10-21
//Include
//IncludeJavaScript('/cat/de_de/javascript/GlobalFunctions.js');
//IncludeJavaScript('/cat/de_de/javascript/MessageBox.js');
IncludeJavaScript('/rep/fi_fi/fw/js/GlobalFunctions.js');
IncludeJavaScript('/rep/fi_fi/fw/js/MessageBox.js');
//vorladen des Wartebalken
var a = new Element('img', {'src': '/rep/fi_fi/fw/css/import/img/icons/wait_mov_neu2.gif' }).update;
myclose="false";
var aktiv = true;
//zur Markierung des gewaehlten Eintrag
var searchmarker=-1;
//wenn Shift verwendet wird
var myshift=false;
//Formular nicht absenden wenn der Wert gesetzt ist - searchmarker=max
var sendnot=false;
function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="' + jsFile + '"></scr' + 'ipt>'); 
}
function showSuggest(){
    document.getElementById('suggest').style.visibility="visible";    
}
function hideSuggest(){
    document.getElementById('suggest').style.visibility="hidden";   
    myclose="false";
    aktiv = true;       
}
function shiftpressed(myevent){
    if (myevent==16)
    {
        myshift=true;  
    }    
}
function shiftreleased(ereignis){
    if (ereignis)
        myevent = ereignis.keyCode            
    else
        myevent = event.keyCode
    if (myevent==16)
    {
        myshift=false;
    }    
}
function getinput(ereignis){
    var mytab=false;
    var typeahead_laenge;
    
    if (document.getElementById("f_keywords").value.length<3){
        hideSuggest()
        sendnot=true;
     }
    
    if (ereignis)
        myevent = ereignis.keyCode            
    else
        myevent = event.keyCode
        
    shiftpressed(myevent); 
    //keyCode=13:Return
    if (myevent==13 && !myshift){
        hideSuggest();
        myclose="true";
        //startWaiting();
        if (!sendnot){
            document.getElementById('quicksearch_form').submit();
        }
        else{
            //Initialwert laden
               if (searchmarker>-1)
                   document.forms[0].reset();
            aktiv=true;
            myclose=false;
        }
    }
    
    if (myshift&&myevent==9)
    {
        myevent=38;    
        mytab=true;
    }
    if (myevent==9)
    {
        myevent=40;    
        mytab=true;        
    }
    
    if (searchmarker!=-1 && !mytab){
        //document.getElementById(searchmarker).focus();
    }
    if (document.getElementById('typeahead_laenge')!=null)
    {
        if (myevent==40){
            if (!ereignis)
                event.keyCode=9
                
            aktiv=false;
            typeahead_laenge=document.getElementById('typeahead_laenge').value;
            searchmarker=searchmarker+1;
            
            if (searchmarker>typeahead_laenge){
                searchmarker=0;    
                document.getElementById('all'+(typeahead_laenge)).style.background='';
                //document.getElementById(typeahead_laenge-1).style.color='';
            }
                
            document.getElementById('all'+searchmarker).style.background='#dcebf6';
            //document.getElementById(searchmarker).style.color='white';
            
            if (searchmarker<typeahead_laenge)
                document.getElementById("f_keywords").value=document.getElementById(searchmarker).innerHTML;
            else    
                document.getElementById("f_keywords").value="";
            
            if (searchmarker>0){
                document.getElementById('all'+(searchmarker-1)).style.background='';
                //document.getElementById(searchmarker-1).style.color='';
            }        
            
            document.getElementById(searchmarker).focus();    
            document.getElementById(searchmarker).blur();
            //window.setTimeout("document.getElementById('f_keywords').focus();",100);            
            document.getElementById('f_keywords').focus();
        }
        
         if (myevent==38 && searchmarker>-1){
                if (!ereignis)
                    event.keyCode=38
                             
                 aktiv=false;
                 typeahead_laenge=document.getElementById('typeahead_laenge').value;
                searchmarker=searchmarker-1;
    
                if (searchmarker==-1){
                    searchmarker=typeahead_laenge;        
                    document.getElementById('all'+0).style.background='';
                    //document.getElementById(0).style.color='';
                }
    
                document.getElementById('all'+searchmarker).style.background='#dcebf6';
                //document.getElementById(searchmarker).style.color='white';
                
                if (searchmarker<typeahead_laenge)
                    document.getElementById("f_keywords").value=document.getElementById(searchmarker).innerHTML;
                else    
                    document.getElementById("f_keywords").value="";                
                
                if (searchmarker>=0 && searchmarker<typeahead_laenge){
                    document.getElementById('all'+(searchmarker+1)).style.background='';
                    //document.getElementById(searchmarker+1).style.color='';
                }
                else{
                    document.getElementById('all'+0).style.background='';
                    //document.getElementById(0).style.color='';
                }    
            document.getElementById(searchmarker).focus();    
            document.getElementById(searchmarker).blur();
            if (ereignis)
                document.getElementById('f_keywords').focus();
        }
        
        if (searchmarker==typeahead_laenge){
            sendnot=true;
        }
        else{
            sendnot=false;
        }
    }    
}
function check(system_address){    
      if (document.getElementById('keywords_submit').onclick==null)
          document.getElementById('keywords_submit').onclick=hideSuggest
     
      document.onkeydown=getinput;        
      document.onkeyup=shiftreleased;
            
            if (document.getElementById('keywords_advanced_field')!=null)
                document.getElementById('keywords_advanced_field').onfocus=hideSuggest;
      
      if (myclose!="true" && document.getElementById("f_keywords").value.length>=3){
        myCall(system_address);    
        aktiv=true;
    }
    else{
        //hideSuggest();
    }
}
    
function myCall(system_address){
    searchterm=document.getElementById("f_keywords").value    
        
    if (aktiv){
        ajaxUrl = system_address + 'searchterm=' + searchterm;
            
    var myAjax=new Ajax.Request(
        ajaxUrl,
        { 
            method      : 'get',
            onSuccess   : getTypeAhead
        } );
        
    //searchmarker auf 0 setzen
      searchmarker=-1;    
  }
}
function startWaiting(){ 
    
    //Variante 2
    //document.getElementById('content_col').innerHTML='';
    //document.getElementById('content_col').innerHTML="<div align=\"left\" valign=\"middle\" style=\"margin:0px;padding:5px\"><img src=\"/rep/fi_fi/fw/css/import/img/icons/wait_mov_neu2.gif\" width=\"90px\" height=\"17px\"></div>";
    
  //Variante 1
    //Wait Box
    var aModalGray;    
    aModalGray = new modalGray('waiting'); 
    aModalGray.fadeIn();
    
    //var aBox = new basicBox('',550,250,null,null,true);
    var aBox = new basicBox('',550,250,null,null,true,'backgroundColor=\"transparent\"','wait');
    aBox.isModal = true;
    aBox._setZIndexHi();
  
    //aBox.DivContent.innerHTML = "<div align=\"center\" valign=\"middle\" style=\"background:#AAAAAA;margin:0px;padding:5px\"><img src=\"/rep/fi_fi/fw/css/import/img/icons/wait_mov_neu2.gif\" width=\"90px\" height=\"17px\"></div>";
    aBox.DivContent.innerHTML = "<div align=\"center\" valign=\"middle\" style=\"margin:0px;padding:5px\"><img src=\"/rep/fi_fi/fw/css/import/img/icons/wait_mov_neu2.gif\" width=\"90px\" height=\"17px\"></div>";
    aBox.addElements(); 
    
    aBox.resize();
  aBox._positionInScreen(true);
  aBox.position();  
  //Ende Wait Box
  //$('quicksearch_form').request({onLoading: wait })    
}
function wait(){    
    //document.getElementById("updateProgressDiv").style.display='inline';
}
 
function setValue(val){
    document.getElementById("f_keywords").value=val;
    hideSuggest();
    startWaiting();
    document.getElementById("quicksearch_form").submit()
}
    
function getTypeAhead(response) {
    output="";
    eval("var search = {"+response.responseText+"}");
      
    if (search.searchterms==null)
        hideSuggest();
    else
    {  
        for (var i = 0; i < search.searchterms.length; i++){
            //output=output+"<p style='margin-left:5px'><a href=\"javascript:document.forms[0].submit()\" onclick=\"setValue('"+search.searchterms[i].value+"')\" id=\""+i+"\" onfocus=\"document.getElementById('"+i+"').style.background='blue';if ("+i+">0)document.getElementById('"+(i-1)+"').style.background='';\">"+search.searchterms[i].value+"</a><br>";
            output=output+"<p style='margin-left:5px'><div style='margin-left:5px' id=\"all"+i+"\"><a href=\"#\" onclick=\"setValue('"+search.searchterms[i].value+"')\" id=\""+i+"\" onmouseover=\"self.status='';return true;\" onmouseout=\"self.status='';return true;\">"+search.searchterms[i].value+"</a></div><br>";
    }
        
    output=output+"<div style='margin-left:5px;margin-top:10px' id=\"all"+search.searchterms.length+"\"><a href='#' id='"+search.searchterms.length+"' style='color:darkblue' onmouseover=\"self.status='';return true;\" onmouseout=\"self.status='';return true;\" onclick=\"hideSuggest();void(0)\">close</a></div></p>"
    output=output+"<input type='hidden' id='typeahead_laenge' value='"+i+"'/>"    
        
    if (search.searchterms.length!=0)
        document.getElementById('suggest').style.visibility="visible";
    else
        hideSuggest();
            
        Element.update("suggest", "<p>" + output+ "</p>"); 
    }
}