function foahkeyevent(event)
{
    switch(event.keyCode)
    {
	case 38: // up
	    event.preventDefault();
//           moveFoahSelect(-1);
       break;
       case 40: // down
           event.preventDefault();
//           moveFoahSelect(1);
       break;
       case 13: // return
           event.preventDefault(); // seems not working in mac !
           setFoahCurrent();
           setZoSet();
//           hideMe();
       break;
    }
}

function zokeyevent(event)
{
    switch(event.keyCode)
    {
	case 38: // up
	    event.preventDefault();
//           moveZoSelect(-1);
       break;
       case 40: // down
           event.preventDefault();
//           moveZoSelect(1);
       break;
       case 13: // return
           event.preventDefault(); // seems not working in mac !
           setZoCurrent();
           setFoahSet();
//           hideMe();
       break;
    }
}

function setFoahCurrent()
{
    var li = $("li.selected", $('#foah_container')).get(0);
    var el = li.id
    $('#foah_input').val($(li).html());
    $('#foah').val(el);
    hasfoahfocus = 0;
    $('#foah_container').hide(); 
    setZoSet(); 
}
function setZoCurrent()
{
    var li = $("li.selected", $('#zo_container')).get(0);
    var el = li.id
    $('#zo_input').val($(li).html());
    $('#zo').val(el);
    haszofocus = 0;
    $('#zo_container').hide();
    setFoahSet(); 
}

function setZoSet()
{
    var lis = $("li", $('#zo_container'));
    lis.hide();
    lis.removeClass('selected');
    changeZo($('#zo').val(),$('#foah').val());
}

function setFoahSet()
{
    var lis = $("li", $('#foah_container'));
    lis.hide();
    lis.removeClass('selected');
    changeFoah($('#zo').val(),$('#foah').val());
}

function moveFoahSelect(step)
{
    var lis = $("li", $('#foah_container'));
    if (!lis) return;
    foahactive += step;
    if (foahactive < 0) {
        foahactive = 0;
    } else if (foahactive >= lis.size()) {
        foahactive = lis.size() - 1;
    }
    lis.removeClass('selected');
    $(lis[foahactive]).addClass('selected');
}

function changeFoah(selzoval, selfoahval)
{
   var outDest  = Object();
   var i = 0;
   iiLand = -1;
   if ((selzoval=="") || (selzoval==0))
      for (key in aOriginStruct)
      {
         outDest[key]= aOriginStruct[key];
         i = i+1;
      }
      else
         for (key in aOriginStruct)
            if (typeof aDestOrigin[key][selzoval] == 'string')
                outDest[key]= aOriginStruct[key];
    for (ikey in outDest)
    {
        $('#'+ikey).show();
//        if (ikey==selfoahval)
//            $('#'+ikey).addClass('selected');
    }
}

function changeZo(selzoval, selfoahval)
{
   var i = 0;
   iiLand = -1;
   var optGroup = Array();
   var outDest  = Array();


   if ((selfoahval=="") || (selfoahval==0))
   {
       aOutput = Object();
       for (key in aDestOrigin)
            for (val in aDestOrigin[key])
                if (typeof aOutput[val]  != 'string')
                    aOutput[val] = "1";
   }
   else
       aOutput = aDestOrigin[selfoahval];

   for(ii=0; ii<aDestStruct.length; ii++) 
   {
      for (key in aDestStruct[ii])
      {
          var found = aOutput[key];
          if ((typeof found == 'object')||(typeof found == 'string')) 
          {
              optGroup[i] = 0;
              if (iiLand>=0)
              {
                   outDest[i]= aDestStruct[iiLand];
                   if (iiLand>0) 
                   optGroup[i] = 1;
                   i = i+1;
                   iiLand = -1;
              }
              outDest[i]  = aDestStruct[ii];
              i = i+1;
          }
          else
          {
              for (ikey in aDestStruct[ii])
              {
                  tmp_ziel = aDestStruct[ii][ikey]+"";
                  if ((tmp_ziel.substr(0,2)!="--")&&(tmp_ziel.substr(0,2)!="++"))
                      iiLand = ii;
                  if (tmp_ziel.substr(0,2)=="++" && (ikey=="ZZZ" || ikey=="123"))
                  {
                      outDest[i]  = aDestStruct[ii];
                      i = i+1;
                  }
              }
          }
      }
   }

   var bfound_optgroup = 0;
   for(ii=0; ii<outDest.length; ii++)
   {
       for (ikey in outDest[ii])
       {
           zoStr = outDest[ii][ikey];
           if (zoStr.substr(0,2)=="++")
           zoStr = zoStr.substring(3,zoStr.length);
           if (optGroup[ii]==1)
           {
               $('#optgroup'+ikey).show();
               bfound_optgroup = 1;
           }
           else
           {
               if (ikey=="123")
               {
                   $('#optgroup'+ikey).show();
                   bfound_optgroup = 1;
               }
               else
               {
                   $('.'+ikey).show();
               }
           }
       }
   }
}
