/**
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.extolinn.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(){
      
      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') {
                    for (var j in myData[i]){
                      if (typeof myData[i][j] !== 'function') {
                           
                           document.getElementById('h' + i + 'room' + j.replace('/','-') + 'puv').innerHTML = myData[i][j]['prumer_cena_za_pokoj'] + '&nbsp;Kč';                   
                           if(myData[i][j]['prumerna_sleva_v_procentech'] >0 ) {
                               document.getElementById('h' + i + 'room' + j.replace('/','-')).innerHTML = '<span style="color:red">' + (myData[i][j]['price_per_room_stay']*1+0) + '&nbsp;Kč</span>';
                               document.getElementById('h' + i + 'room' + j.replace('/','-') + 'avg').innerHTML = '<span style="color:red">' + myData[i][j]['prumer_cena_za_pokoj_po_sleve'] + '&nbsp;Kč</span>';
                               document.getElementById('h' + i + 'room' + j.replace('/','-') + 'per').innerHTML = '-' + myData[i][j]['prumerna_sleva_v_procentech'] + '&nbsp;%';  
                           }
                           else
                           {
                                document.getElementById('h' + i + 'room' + j.replace('/','-')).innerHTML = (myData[i][j]['price_per_room_stay']*1+0) + '&nbsp;Kč';
                                document.getElementById('h' + i + 'room' + j.replace('/','-') + 'avg').innerHTML = myData[i][j]['prumer_cena_za_pokoj_po_sleve'] + '&nbsp;Kč';
                                document.getElementById('h' + i + 'room' + j.replace('/','-') + 'per').innerHTML = '&nbsp;';  
                           }
                           
                      }
                    }
                }
            }
            
            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 GetCookie (name) {
      var arg = name + "=";
      var alen = arg.length;
      var clen = document.cookie.length;
      var i = 0;
      while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
          return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
      }
      return null;
    }
