/*priceListPlnaPenze = new Array(
      new Array(1225,103,500,450,0),
      new Array(103,410,390,350,0),
      new Array(410,1225,270,240,0)
);*/

priceListPolopenze = new Array(
      new Array(1225,102,550,0,0),
      new Array(103,131,390,0,0),
      new Array(201,315,440,0,0),
      new Array(316,630,330,0,0),
      new Array(701,831,360,0,0),
      new Array(901,1224,330,0,0)
);

priceList=priceListPolopenze;

function dateStruct(day, month, year){
   function print(){
      return this.day+"."+this.month+"."+this.year;
   }
   
   function nextMonth(){
      this.month++;
      if(this.month==13){
         this.month=1;
         this.year++;
      }
   }
   
   function nextDay(){
      if(this.day<28){this.day++;return;}
      if(this.day==31){this.day=1;this.nextMonth();return;}
      if(this.day==30 && (this.month==4 || this.month==6 || this.month==9 || this.month==11)){this.day=1;this.nextMonth();return;}
      if(this.month==2){
         if(this.day==28 && this.year%4!=0){this.day=1;this.nextMonth();return;}
         if(this.day==29){this.day=1;this.nextMonth();return;}
      }
      this.day++
   };
     
   function getNum(){
      ret = (this.month*100+parseInt(this.day));
      return ret;
   }
   
   function inRange(from, to){
      number = this.getNum();
      if(from<=number && number<to)return true;
      if(from>to){
         if(from<=number && number>to)return true;
         if(from>=number && number<to)return true;
      }
      return false;
   }
   
   function getPrice(hint){
      number = this.getNum();
      if(hint!=null){
         if(this.inRange(priceList[hint][0],priceList[hint][1]))return new priceStruct(priceList[hint][2],priceList[hint][3], hint,priceList[hint][4]);
         if(this.inRange(priceList[(hint+1)%priceList.lenght][0],priceList[(hint+1)%priceList.lenght][1]))return new priceStruct(priceList[hint][2],priceList[hint][3],(hint+1)%priceList.lenght,priceList[hint][4]);
      }
      for(x=0;x<priceList.length;x++){
         if(this.inRange(priceList[x][0],priceList[x][1]))return new priceStruct(priceList[x][2],priceList[x][3],x,priceList[x][4]);
      }
      globalError="Zadán chybný termín pobytu!";
      return null;
      //window.alert("Invalid date range!");
   }

   
   this.day=day;
   this.month=month;
   this.year = year;
   this.print=print;
   this.nextMonth=nextMonth;
   this.nextDay=nextDay;
   this.getNum=getNum;
   this.getPrice=getPrice;
   this.inRange=inRange;
}

function priceStruct(price1, price2, hint, isSomer){
   function print(){
      return this.price1+","+this.price2+","+hint;
   }
   this.price1=price1;
   this.price2=price2;
   this.hint=hint;
   this.isSomer = isSomer;
   this.print=print;
}

globalError=null;

function reCalc(){
   globalError=null;
   dospeli = parseInt(document.reservation.dospeli.value);
   deti = parseInt(document.reservation.deti.value);
   mladistvi = 0;
   
   stravovani = parseInt(document.reservation.stravovani.value);
   
   /*switch(stravovani){
      case 0:
         priceList = priceListPlnaPenze;
         break;
      case 0:
         priceList = priceListPolopenze;
         break;
      case 1:
         priceList = priceListBezPenze;
         break;
   }*/
   
   dateFrom = new dateStruct(document.reservation.odD.value,document.reservation.odM.value,document.reservation.odR.value);
   dateTmp = new dateStruct(document.reservation.odD.value,document.reservation.odM.value,document.reservation.odR.value);
   dateTo = new dateStruct(document.reservation.doD.value,document.reservation.doM.value,document.reservation.doR.value);
   
   price = 0;
   days= 0;
   toNum = dateTo.getNum(dateTo);
   
   while(dateTmp.getNum()!=toNum){
      days++;
      dateTmp.nextDay();
   }
   
   while(dateFrom.getNum()!=toNum){
      dayPrice = dateFrom.getPrice();
      if(dayPrice==null)break;

      if(days==1)price +=50*(dospeli+deti);
      
      price += dayPrice.price1*dospeli + dayPrice.price1*deti*0.8;
      if(stravovani==1)price += 60*dospeli + 48*deti;
      //if(dospeli+deti+mladistvi<2 && dospeli+deti+mladistvi>0)price += 100;
         
      dateFrom.nextDay();
   }
   
   object = document.getElementById("priceField");
   object.innerHTML=Math.round(price);
   document.reservation.cenaCelkem.value=Math.round(price);
   
   object = document.getElementById("errorField");
   if(globalError!=null){
    object.innerHTML=globalError;
  }else{
    object.innerHTML="";
  }
}

function detail(){
   globalError=null;
   dospeli = parseInt(document.reservation.dospeli.value);
   deti = parseInt(document.reservation.deti.value);
   mladistvi = 0;
   
   stravovani = document.reservation.stravovani.value;
   
   dateFrom = new dateStruct(document.reservation.odD.value,document.reservation.odM.value,document.reservation.odR.value);
   dateTo = new dateStruct(document.reservation.doD.value,document.reservation.doM.value,document.reservation.doR.value);
   
   win=window.open("","Detail","width=600,height=300");
   
   win.document.writeln("Od:"+dateFrom.print()+", Do:"+dateTo.print());
   win.document.writeln("<br><br>");
   
   price = 0;
   days=0;
   toNum = dateTo.getNum(dateTo);
   while(dateFrom.getNum()<=toNum){
      days++;
      win.document.writeln("Den "+days+". - "+dateFrom.print()+":");
      dayPrice = dateFrom.getPrice();
      if(dayPrice==null)break;
      if(dayPrice.isSomer){
         win.document.writeln("   Zaklad (leto):"+dayPrice.price1+"*"+dospeli+" + "+dayPrice.price2+"*"+deti);
         price += dayPrice.price1*dospeli + dayPrice.price2*deti;
         if(stravovani){
            price += 140*dospeli + 90*deti;
            win.document.writeln("      stravovani:"+ 140+"*"+dospeli+" + "+90+"*"+deti);
         }
         if(dospeli+deti<2 && dospeli+deti>0){
            price += 100;
            win.document.writeln("     Penalizace:"+ 100);
         }
      }
      else{
         win.document.writeln("   Zaklad (zima):"+dayPrice.price1+"*"+dospeli+" + "+dayPrice.price2+"*"+deti+"*"+0.9);
         price += dayPrice.price1*dospeli + Math.round(dayPrice.price2*deti*0.9);
         if(stravovani){
            price += 140*(dospeli+deti);
            win.document.writeln("      stravovani:"+ 140+"*"+dospeli+" + "+140+"*"+deti);
         }
         if(dospeli+deti<2 && dospeli+deti>0){
            price += 140;
            win.document.writeln("     Penalizace:"+ 140);
         }
      }
      dateFrom.nextDay();
      win.document.writeln("<br>");
   }
   if(days<3)price*=1.3;
   
   object = document.getElementById("priceField");
   object.innerHTML=Math.floor(price);
}
