﻿function validDate()  {
    leftI = new Date()
    rightI = new Date(leftI.getTime() + (1000*60*60*24*365))
    dnesek = leftI.getTime()
    zarok = rightI.getTime()
    interval = leftI.getDate() + "." + (leftI.getMonth()+1) + "." + leftI.getFullYear() + " - " + rightI.getDate() + "." + (rightI.getMonth()+1) + "." + rightI.getFullYear()
      // prijezd
    pri_rok = document.form1.check_in_year.value
    pri_mes = document.form1.check_in_month.value
    pri_den = document.form1.check_in_day.value
    pri = new Date(pri_rok,pri_mes-1,pri_den)
    prijezd = pri.getTime()
    // odjezd
    odj_rok = document.form1.check_out_year.value
    odj_mes = document.form1.check_out_month.value
    odj_den = document.form1.check_out_day.value
    odj = new Date(odj_rok,odj_mes-1,odj_den)
    odjezd = odj.getTime()


    // počet nocí
  
    rozdil = odjezd - prijezd
    rozdil = Math.ceil(Math.floor(rozdil / (1000*60*60*24)))
    	
	if(!isValidDate(pri_den, pri_mes, pri_rok)) rozdil=0;
	if(!isValidDate(odj_den, odj_mes, odj_rok)) rozdil=0;
	
    //pocet  lidi
    i= document.form1.adults.selectedIndex
    pocet_lidi = document.form1.adults.options[i].value

    // test
    if(prijezd < dnesek || prijezd > zarok) {
        alert("Valid date must be within the interval: " + interval)
        return false
        }
    else if(odjezd < dnesek || odjezd > zarok) {
        alert("Valid date must be within the interval: " + interval)
        return false
        }
    else if(odjezd <= prijezd) {
        alert("Arrival date must be earlier then departure date.")
        return false
        }
    else  {
            if(rozdil > 0) document.form1.nb_nights.value = rozdil
            else document.form1.nb_nights.value = 0

            if(pocet_lidi > 0 ) return true
            else {
               alert("Adults")
               return false
            }

        }

    }

function isValidDate(day, month, year) {
 //alert("checking: "+ day + " " + month + " " + year)
 
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days.")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days.");
return false;
   }
}
return true;
}


function changeNumNight()  {
    // prijezd
    pri_rok = document.form1.check_in_year.value
    pri_mes = document.form1.check_in_month.value
    pri_den = document.form1.check_in_day.value
    pri = new Date(pri_rok,pri_mes-1,pri_den)
    prijezd = pri.getTime()
    // odjezd
    odj_rok = document.form1.check_out_year.value
    odj_mes = document.form1.check_out_month.value
    odj_den = document.form1.check_out_day.value
    odj = new Date(odj_rok,odj_mes-1,odj_den)
    odjezd = odj.getTime()
//alert(odj.getDate())

    // počet nocí
//  alert(pri_den+pri_mes+pri_rok)
    rozdil = odjezd - prijezd
    rozdil = Math.ceil(Math.floor(rozdil / (1000*60*60*24)))
    	
	if(!isValidDate(pri_den, pri_mes, pri_rok)) rozdil=0;
	if(!isValidDate(odj_den, odj_mes, odj_rok)) rozdil=0;
	
    // test
    if(rozdil > 0) {
        document.form1.nb_nights.value = rozdil
        }
    else document.form1.nb_nights.value = 0
    }

function setOption()
  {
    //nastaven? po??tku
         document.form1.adults.length = 1
         document.form1.adults.options[0].text = 1
         document.form1.adults.options[0].value = 1
  }

function ZmenaPokoje()
  {
      var selected_room = document.form1.typ_room.selectedIndex;

      if(selected_room==0){
         document.form1.adults.length = 1;
         max_pocet=1;
      }else if(selected_room==1){
         document.form1.adults.length = 2;
         max_pocet=2;
      }else if(selected_room==2){
         document.form1.adults.length = 2;
         max_pocet=2;         
      }else if(selected_room==3){
         document.form1.adults.length = 3;
         max_pocet=3;
      }else if(selected_room==4){
         document.form1.adults.length = 2;
         max_pocet=2;
      }        	

        for(i=0;i<max_pocet;i++)
          {             
	         document.form1.adults.options[i].text=(i+1);
             document.form1.adults.options[i].value=(i+1);
          }
  }

function Pick_up()
  {
      var selected_destination = document.form_pick.from.selectedIndex;   
      document.form_pick.nop.length = 2;
      
      if(selected_destination == 0) {
          document.form_pick.nop.options[0].value="1-4 (CZK 650)";
          document.form_pick.nop.options[0].text="1-4 (CZK 650)";          
          document.form_pick.nop.options[1].value="5-8 (CZK 1200)";
          document.form_pick.nop.options[1].text="5-8 (CZK 1200)";                    
      }else{
          document.form_pick.nop.options[0].value="1-4 (CZK 390)";
          document.form_pick.nop.options[0].text="1-4 (CZK 390)";          
          document.form_pick.nop.options[1].value="5-8 (CZK 780)";
          document.form_pick.nop.options[1].text="5-8 (CZK 780)";                          
      }
      
      

              

  }
    
  