var sportTyp = "";
var itemsArray = null;
// Definicja Object w celu buforowania elementw li,
// jest to rozwizanie dla przegldarki IE 6, ktra
// nie zapisuje tekstu wza elementu li
// lub etykiety podczas buforowania tego elementu.
function CachedLiItem(liElement,liLabel){
   // Obiekt elementu li.
   this.liElement=liElement;
   // Cig tekstowy przedstawiajcy tekst wza li lub etykiet.
   this.liLabel=liLabel;
}
window.onload=function(){
   var sel = document.getElementById("expand");
   // Doczenie do funkcji obsugi zdarze onclick.
   if(sel != null){
      sel.onclick=function(){
         getMoreChoices(this)};
   }
   var selr = document.getElementById("restore");
   // Doczenie do funkcji obsugi zdarze onclick.
   if(selr != null){
      selr.onclick=function(){
         restore(this)};
   }
   // Umieszczenie wszystkich istniejcych elementw listy w dwch tablicach,
   // w celu zachowania moliwoci pniejszego przywrcenia.
   itemsArray = new Object();
   itemsArray.team = new Array();
   itemsArray.individual = new Array();
   var bulletArr = document.getElementsByTagName("li");
   populateArray(bulletArr,"team");
   populateArray(bulletArr,"individual");
}

// Utworzenie tablicy obiektw CachedLiItem 
// w celu pniejszego przywrcenia nieuporzdkowanej listy.
function populateArray(arr,typ)  {
   var inc = 0;
   var el = null;
   var liObj=null;
   for(var i = 0; i < arr.length; i++){
      el = arr[i].parentNode;
      if(el.id.indexOf(typ) != -1) {
         liObj=new CachedLiItem(arr[i],arr[i].childNodes[0].nodeValue);
         itemsArray[typ][inc] = liObj;
         inc++;
      }
   }
}
// Zwrcenie liczby elementw li zawartych 
// w elemencie ul.
function getULoptionsLength(_sportTyp){
   var ul = document.getElementById(_sportTyp+"_u");
   var len=0;
   for(var i =0; i < ul.childNodes.length; i++){
      if(ul.childNodes[i].nodeName == "LI" ||
         ul.childNodes[i].nodeName == "li" ){
            len++;
      }
   }
   return len;
}
function getMoreChoices(obj){
   if (obj == null ) { return; }
   var url = "";
   var optsArray = obj.options;
   var val = "";
   for(var i=0; i < optsArray.length; i++){
      if(optsArray[i].selected) {
         val=optsArray[i].value;  break;
      }
   }
   sportTyp=val;
   // Okrelenie, czy wypunktowane elementy zostay rozbudowane.
   if(itemsArray[sportTyp].length < getULoptionsLength(sportTyp)) {
      return;
   }
   url = "http://www.parkerriver.com/s/expand?expType="+val;
   httpRequest("GET",url,true,handleResponse);
}
function addToBullets(obj){
   // Element ul, ktry zawiera wypunktowane elementy.
   var ul = document.getElementById(sportTyp+"_u");
   var el = null;
   // Czas na dodanie nowych elementw, pobranych z serwera.
   for(var h = 0; h < obj.length; h++){
      el = document.createElement("li");
      el.appendChild(document.createTextNode(obj[h]));
      ul.appendChild(el);
   }
}

function restore(_sel) {
   var val;
   var opts =  _sel.options;

   for (var i = 0; i < opts.length; i++){
      if(opts[i].selected) { val=opts[i].value; break;}
   }
   sportTyp=val;
   // Przywrcenie listy jedynie wtedy, gdy elementy wypunktowane
   // zostay ju rozbudowane.
   if(itemsArray[sportTyp].length < getULoptionsLength(sportTyp)) {
      var ul = document.getElementById(val+"_u");
      if(ul != null)  {
         // Ponowne utworzenie listy oryginalnych wypunktowanych elementw.
         ul.innerHTML="";
         var tmpArr = itemsArray[val];
         var tmpLiElement = null;
         for(var j = 0; j < tmpArr.length; j++){
            tmpLiElement=tmpArr[j].liElement;
            // Rozwizanie dla przegldarki IE6.
            if(tmpLiElement.hasChildNodes()){tmpLiElement.
               removeChild(tmpLiElement.firstChild);}
            tmpLiElement.appendChild(document.
               createTextNode(tmpArr[j].liLabel))
            ul.appendChild(tmpLiElement);
         }
      }
   }
}
// Obsuga zdarze dla obiektu XMLHttpRequest.
function handleResponse(){
   try{
      if(request.readyState == 4){
         if(request.status == 200){
            var resp =  request.responseText;
            if(resp != null){
               // Warto zwrotna jest tablic.
               addToBullets(eval(resp));
            }
         } else {
            // Skrcono dla zachowania zwizoci.
         }
      }// Koniec zewntrznej ptli if.
   } catch (err) {
        alert("Serwer nie jest dostpny "+
         "dla tej aplikacji. Prosz wkrtce sprbowa"+
         " ponownie. \nBd: "+err.message);

   }
}
