window.onload=function(){
   var sts = document.getElementById("sts");
   sts.onclick=function(){
      var cit = document.getElementById("city");
      // danie zostanie przeprowadzone tylko wtedy, gdy pole 
      // tekstowe city posiada warto.
      if(cit.value) {getZipcode(cit.value,sts.value.toUpperCase());}

   };
};

function getZipcode(_ct,_st){
   if(_ct.length > 0 && _st.length > 0){
      httpRequest("GET","http://www.parkerriver.com/s/zip?city="+
         encodeURIComponent(_ct)+"&state="+
         encodeURIComponent(_st),
         true,handleResponse);
   } else {
      document.getElementById("zip5").value="";
   }
}

function handleResponse(){
   var xmlReturnVal;
   try{
      if(request.readyState == 4){
         if(request.status == 200){
            xmlReturnVal=request.responseXML;
            if(xmlReturnVal != null)  {
               var zip5=xmlReturnVal.getElementsByTagName("zip")[0];
               if(zip5 && zip5.childNodes.length > 0) {
                  document.getElementById("zip5").
                     value=zip5.childNodes[0].data;
               }
            }
         } else {
            // Warto waciwoci request.status wynosi 503, 
            // jeeli aplikacja jest niedostpna, oraz 
            // warto 500, jeli aplikacja zawiera bd.
            alert(
              "Wystpi problem z komunikacj midzy obiektem XMLHttpRequest, "+
               "a programem serwera.");
         }
      }// Koniec zewntrznej ptli if.
   } catch (err) {
      alert("Serwer nie jest dostpny "+
         "dla tej aplikacji. Prosz wkrtce sprbowa"+
         " ponownie. \nBd: "+err.message);

   }
}
