/**
Potřebuje script.aculo.us
**/
 
    var xmlHttp;
    var text;
    var myData;
    var slovnik = new Array();
    
   // slovnik['cs']['NotValid'] = 'Nevalidní';
    
    
    function vytvorXMLHttpRequest(){
      if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
      }
    }

    function zahajPozadavek(thisForm,lang){
      var validArrival = validDate(thisForm.arrival.value);
      var validDeparture = validDate(thisForm.departure.value);
      var departureValid = '';
      var arrivalValid = '';
      if (!validArrival || !validDeparture){
          if (validArrival == false) {
            arrivalValid = '<br>Nevalidní!';
          }
          if (!validDeparture){
            departureValid = '<br>Nevalidní!';
          }
        
      }
      else
      {
          var formatedArrival =  MyFormatDate(thisForm.arrival.value);
          var formatedDeparture =  MyFormatDate(thisForm.departure.value);
          var url = "http://www.hostel-downtown.cz/webservices/json/get_prices.php?arrival=" + formatedArrival + "&departure=" + formatedDeparture ;
          vytvorXMLHttpRequest();
          xmlHttp.onreadystatechange = zpracujZmenuStavu;
          xmlHttp.open("GET", url, true);
          xmlHttp.send(null);
          document.getElementById('h3arrival').innerHTML = thisForm.arrival.value;
          document.getElementById('h3departure').innerHTML = thisForm.departure.value;
          document.cookie = "Arrival="+escape(formatedArrival) + ";";
          document.cookie = "Departure="+escape(formatedDeparture) + ";";
      }
      
      document.getElementById('arrival_valid').innerHTML = arrivalValid;
      document.getElementById('departure_valid').innerHTML = departureValid;
      
      
    }
    
    function zpracujZmenuStavu(){
      var j;
      if(xmlHttp.readyState == 4){
        if(xmlHttp.status == 200){
          text = xmlHttp.responseText;
          myData = text.parseJSON();
          var formValid = '';
          if(myData == false)
          {
            formValid = 'V zadaném rozsahu nebylo nic nalezeno.';
          }
          else
          {
            formValid = '';
            for (var i in myData){
                if (typeof myData[i] !== 'function') {
                   var price = myData[i]['price_per_room_stay'] + '&nbsp;Kč';
                   if(myData[i]['prumerna_sleva_v_procentech'] >0 ) {
                     price += '<br><strong class="alert">(-' + myData[i]['prumerna_sleva_v_procentech'] + ' %)</strong>';
         
                   }
                   document.getElementById('room' + i).innerHTML = price;
                   document.getElementById('room' + i + 'avg').innerHTML = myData[i]['prumer_cena_za_pokoj_po_sleve'] + '&nbsp;Kč';
                }
            }
            
            new Effect.Highlight('fade_test');
          }
          document.getElementById('form_valid').innerHTML = formValid;
        }
      }
    }
    
    function textSubmit(e,thisForm,lang){
      if(e.keyCode == 13){
        zahajPozadavek(thisForm,lang);
      }
    }
    
    function validDate(formDate){
      var goodDate = true;
      var re=new RegExp("^(\\d{1,31}).(\\d{1,12}).20(07|08|09|10|11)$");
      if(formDate.search(re) < 0){
        goodDate =  false;
      }
      return goodDate;
    }
    

    function MyFormatDate(DateStr){
      var parts = DateStr.split('.');
      var month = parts[1] - 1;
      var newDateObj = new Date(parts[2],month,parts[0]);
      var monthNew = newDateObj.getMonth() + 1;
      var returnDate = newDateObj.getFullYear() + '-' + monthNew + '-' + newDateObj.getDate();
      return returnDate;
    }
    
    
    function formatPrice(Price){
      
    }