
function PriceCalculator(tables, tablesDayCount, configRelativePath, selectedTable, selectedTableId) {
	this.tables = tables;
	this.tablesDayCount = tablesDayCount;
	this.configRelativePath = configRelativePath;
	this.selectedTable = selectedTable;
	this.selectedTableId = selectedTableId;
	this.isOkToRegister = true;
	this.alreadyGone = false;
	this.notAvailable = false;

	this.setRegisterOk = true;
	//Kurzovni rozdil v %
	this.kurzRozdil = 0;
}
 
pc = PriceCalculator.prototype;
 
pc.gGetElementById = function(s) {
  	var o = (document.getElementById ? document.getElementById(s) : document.all[s]);
	return o == null ? false : o;
}

/* rozbali/zabali vybrany blok */
pc.ZipUnzip = function (blockID, imageID) {
	var objTable = this.gGetElementById(blockID);
	var img = this.gGetElementById(imageID);
	if (objTable != null) {
		if (objTable.style.display=="block"){
			objTable.style.display="none";
			img.src = this.configRelativePath + "images/zipped_triangle.gif";
		}
		else {
			selectedTableId = this.gGetElementById('seasonTable').value;
			objTable.style.display="block";
			img.src=this.configRelativePath + "images/unzipped_triangle.gif";
		}
	}
}

/* vracia unix time zo zadaneho datumu */ 
pc.GetTime = function(date) { 
	date = date.replace(/\s/g,"");
	match = date.match(/([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})/);
	if (match==null) return;
	date = Date.UTC(match[3],match[2]-1,match[1]);
	return date;
}


/* spocita cenu podla cenniku a poctu osob pre polozku */
pc.Recalculate = function(itemId) {
	var item = this.gGetElementById(itemId);
	var itemCount = this.gGetElementById(itemId+'_count');
	itemCount = itemCount.options[itemCount.selectedIndex].text;
	var reqDateFrom = this.gGetElementById('oddata' + this.selectedTableId+'_d').value + "." + 
                    this.gGetElementById('oddata' + this.selectedTableId+'_m').value + "." + 
                    this.gGetElementById('oddata' + this.selectedTableId+'_y').value;
	var	reqDateTo = this.gGetElementById('dodata' + this.selectedTableId+'_d').value + "." +
                  this.gGetElementById('dodata' + this.selectedTableId+'_m').value + "."+ 
                  this.gGetElementById('dodata' + this.selectedTableId+'_y').value;
	var dateFrom, dateTo, reqDateFrom, reqDateTo, reqDayCount;
	var	tmpPrice, isOnBoundary;
	var is_date_ok;
	

	
	reqDateFrom = this.GetTime(reqDateFrom);
	reqDateTo = this.GetTime(reqDateTo);
	
	match = itemId.match(/item([0-9]+)_([0-9]+)/);									
	if (match!=null) {
		item.value=0;
		tableId = match[1];	 
		catId = match[2];
		tmpPrice = 0;
		isOnBoundary=false;
		
		if (isNaN(reqDateFrom))
		  is_date_ok = true;
		else
		  is_date_ok = false;
		  
		for(key in this.tables[tableId]) {
			match = key.match(/([0-9]{4})-([0-9]{2})-([0-9]{2})#([0-9]{4})-([0-9]{2})-([0-9]{2})/);
			if (match!=null) {
				dateFrom = Date.UTC(match[1],match[2]-1,match[3]);
				dateTo = Date.UTC(match[4],match[5]-1,match[6]);
				// pobyt ktory padne presne do cenoveho pasma
				if ( (reqDateFrom >= dateFrom) && (reqDateFrom <= dateTo)
					&& (reqDateTo >= dateFrom) && (reqDateTo <= dateTo) ) {						                            
						is_date_ok = true;
            reqDayCount = ( reqDateTo - reqDateFrom ) /  86400000;  // 60 * 60 * 24 * 1000
						item.value = Math.round(( this.tables[tableId][key][catId-1] / this.tablesDayCount[tableId] ) * reqDayCount * itemCount);
						continue;
				}
				
				// ak pobyt zasahuje viac pasem
				if ( (reqDateFrom < dateFrom) && (reqDateTo > dateTo)
					&& isOnBoundary ) {
          					                            
					reqDayCount = ( dateTo - dateFrom ) /  86400000;
					tmpPrice += Math.round(( this.tables[tableId][key][catId-1] / this.tablesDayCount[tableId] ) * reqDayCount * itemCount);	
				}
				
				// pobyt na rozhrani dvoch cenovych pasiem
				if ( (reqDateFrom >= dateFrom) && (reqDateFrom < dateTo)
					&& (reqDateTo > dateTo) ) {
					is_date_ok = true;
					reqDayCount = ( dateTo - reqDateFrom ) /  86400000;
					tmpPrice = Math.round(( this.tables[tableId][key][catId-1] / this.tablesDayCount[tableId] ) * reqDayCount * itemCount);	
					isOnBoundary = true;
					continue;
				}
				
				if ( (reqDateTo >= dateFrom) && (reqDateTo <= dateTo)
					&& isOnBoundary)	 {
					
					reqDayCount = ( reqDateTo - dateFrom ) /  86400000;
					tmpPrice += Math.round(( this.tables[tableId][key][catId-1] / this.tablesDayCount[tableId] ) * reqDayCount * itemCount);	
					item.value = tmpPrice;
					isOnBoundary = false;
				}
				
				
			}
		}
	}
	else {
		item = this.gGetElementById(itemId);
		match = itemId.match(/item([0-9]+)_mp_([0-9]+)/);
		if (match!=null) {
			tableId = match[1];	 
			exId = match[2];
			if (this.tables[tableId]["moreprices"][exId-1][1]=="cena1raz") {
				item.value = this.tables[tableId]["moreprices"][exId-1][0] * itemCount;					
			}
			else {
				reqDayCount = Math.ceil(( reqDateTo - reqDateFrom ) /  86400000 ); // 60 * 60 * 24 * 1000
				item.value = this.tables[tableId]["moreprices"][exId-1][0] * reqDayCount * itemCount;
			}
		}
		
		match = itemId.match(/item([0-9]+)_e_([0-9]+)/);
		if (match!=null) {
			tableId = match[1];	 
			exId = match[2];
			if (this.tables[tableId]["extras"][exId-1][1]=="priplatek1raz") {
				item.value = this.tables[tableId]["extras"][exId-1][0] * itemCount;					
			}
			else {
				reqDayCount = Math.ceil(( reqDateTo - reqDateFrom ) /  86400000 ); // 60 * 60 * 24 * 1000
				item.value = this.tables[tableId]["extras"][exId-1][0] * reqDayCount * itemCount;
			}

		}
	}
	if (isNaN(item.value)) 
  {
    item.value=0;
  }
  
  if (item.value == 0)
  {
    //var elem = document.getElementById("reserveButton");
    if (isOnBoundary == true || is_date_ok == false)
    {
	 	  this.isOkToRegister = false;
			this.setRegisterOk = false;
	 	  this.alreadyGone=false;
	 	  return;
	 	  //elem.disabled=true;
	 	}
	 	else
     {
      this.isOkToRegister = true;
      this.alreadyGone=false;
     }	
  }
  else if (item.value!=0)
  {
		//Check na drivejsi datum
    var now = new Date();
    if (reqDateFrom < now.getTime())
    {
      this.isOkToRegister = false;
			this.setRegisterOk = false;
      this.alreadyGone=true;
      return;
    }
    else
    {
      this.isOkToRegister = true;
      this.alreadyGone=false;
    }
  }
  
  //Check na obsazenost!
  if ( typeof(disabledDates) != "undefined" )
  {
      var tmp, tmp_ok=true;
      var tmp_from, tmp_to;
      
      for (tmp=0; tmp < disabledDates.length; tmp++ )
      {
        tmp_from = this.GetTime(disabledDates[tmp][0]);        
        tmp_to = this.GetTime(disabledDates[tmp][1]);
        
        
        if ( (( reqDateFrom >= tmp_from) && (reqDateTo <= tmp_to) ) ||
               ( ( reqDateFrom <= tmp_from) && (reqDateTo >= tmp_to) ) || 
               ( ( reqDateFrom <= tmp_to) && (reqDateTo >= tmp_to) ) || 
               ( ( reqDateFrom <= tmp_from) && (reqDateTo >= tmp_from) )
           )
         {
          tmp_ok = false;          
         }        
      }
      
      if (tmp_ok == true)
      {
        this.isOkToRegister = true;
        this.alreadyGone=false;
        this.notAvailable = false;
        
      }
      else
      {
        
        this.isOkToRegister = false;
				this.setRegisterOk = false;
        this.alreadyGone=false;
        this.notAvailable = true;
      }
  
  }
 }
 
/* prepocita vsetky polozky */
pc.RecalculateAll = function() {

	this.setRegisterOk = true;


	for(tkey in this.tables) {
		for(ckey in this.tables[tkey]) {
			if (ckey!="extras" && ckey!="moreprices") {
				i=0;		
				for(pkey in this.tables[tkey][ckey]) {
					i++;
					this.Recalculate("item"+tkey+"_"+i);
				}
			}
			else if(ckey=="extras") {
				j=0;
				for(ekey in this.tables[tkey]["extras"]) {
					j++;							
					this.Recalculate("item"+tkey+"_e_"+j);
				}
			}
			else if(ckey=="moreprices") {
				j=0;
				for(ekey in this.tables[tkey]["moreprices"]) {
					j++;							
					this.Recalculate("item"+tkey+"_mp_"+j);
				}
			}
			 
		}
	}

	this.isOkToRegister = this.setRegisterOk;

	this.SumAll();
}
 
/* zosumuje vsetky ceny */
pc.SumAll = function() {
	var itemSum = 0, item;
	var	i=0;
	var pridavek = 0;

	this.SumDays();											
	this.SumHeads();										

	var daysCount = this.gGetElementById('itemSumDays').value;
	var itemCount;
	var itemId;

	while (item = this.gGetElementById("item"+this.selectedTableId+"_"+ ++i))
		itemSum += parseInt(item.value);

	//pripocteni kurzovniho rozdilu
	pridavek = itemSum * (this.kurzRozdil/100);

	//Nyni je nutne pripocist pouze ty extras
	//ktere to maji nastaveno v databazi

	i=0;
	while (item = this.gGetElementById("item"+this.selectedTableId+"_mp_"+ ++i))
	{
		itemSum += parseInt(item.value);	

		//kurz rozdil
		itemId = "item"+this.selectedTableId+"_mp_"+ i;
		itemCount = this.gGetElementById(itemId+'_count');
		itemCount = itemCount.options[itemCount.selectedIndex].text;

		match = itemId.match(/item([0-9]+)_mp_([0-9]+)/);
		if (match!=null) {
			tableId = match[1];	 
			exId = match[2];
			//Pouze tehdy, jeli to nastaveno v databazi
			if (this.tables[tableId]["moreprices"][exId-1][2] == 1 )
			{
				if (this.tables[tableId]["moreprices"][exId-1][1]=="cena1raz") {
					pridavek += (this.tables[tableId]["moreprices"][exId-1][0] * itemCount) * (this.kurzRozdil/100);					
				}
				else {
					pridavek += (this.tables[tableId]["moreprices"][exId-1][0] * daysCount * itemCount) * ( this.kurzRozdil/100 );
				}
			}
		}
	}
			
	i=0;
	while (item = this.gGetElementById("item"+this.selectedTableId+"_e_"+ ++i))
	{
		itemSum += parseInt(item.value);

		//kurz
		itemId = "item"+this.selectedTableId+"_e_"+ i;
		itemCount = this.gGetElementById( itemId+'_count');
		itemCount = itemCount.options[itemCount.selectedIndex].text;

		match = itemId.match(/item([0-9]+)_e_([0-9]+)/);
		if (match!=null) {
			tableId = match[1];	 
			exId = match[2];
			if (this.tables[tableId]["extras"][exId-1][2] == 1)
			{
				if (this.tables[tableId]["extras"][exId-1][1]=="priplatek1raz") {
					pridavek += (this.tables[tableId]["extras"][exId-1][0] * itemCount) * (this.kurzRozdil/100);					
				}
				else {
					pridavek += (this.tables[tableId]["extras"][exId-1][0] * daysCount * itemCount) * (this.kurzRozdil/100);
				}
			}

		}
	}



	pridavek = Math.round(pridavek);
	itemSum += pridavek;
	
	item = this.gGetElementById("kurzRozdil");
	if (isNaN(pridavek)) item.value = 0;
	else item.value = pridavek;	

	item = this.gGetElementById("itemSum");
	if (isNaN(itemSum)) item.value = 0;
	else item.value = itemSum;	
	
}
		
/* spocita pocet dni pobytu */		
pc.SumDays = function () {
	var	item = this.gGetElementById('itemSumDays');
	item.value = 0;
	var reqDateFrom = this.gGetElementById('oddata' + this.selectedTableId+'_d').value + "." + 
                    this.gGetElementById('oddata' + this.selectedTableId+'_m').value + "." + 
                    this.gGetElementById('oddata' + this.selectedTableId+'_y').value;
	var	reqDateTo = this.gGetElementById('dodata' + this.selectedTableId+'_d').value + "." +
                  this.gGetElementById('dodata' + this.selectedTableId+'_m').value + "."+ 
                  this.gGetElementById('dodata' + this.selectedTableId+'_y').value;
	
	reqDateFrom = this.GetTime(reqDateFrom);
	reqDateTo = this.GetTime(reqDateTo);

	var dayCount = ( reqDateTo - reqDateFrom  ) / 86400000;
	if (isNaN(dayCount)) item.value = 0;
	else item.value = dayCount;	
	
	if (dayCount < 0)
	{
	 //alert("Termin neodpovida vybranemu typu pobytu. Prosim, vyberte jiny termin ci zvolte jiny typ pobytu.");
	 this.isOkToRegister = false;
		this.setRegisterOk = false;
	}
}

/* spocita pocet osob */
pc.SumHeads = function () {
	var	item = this.gGetElementById('itemSumHeads');
	item.value = 0;
	var headsCount = 0;
		
	for(ckey in this.tables[this.selectedTableId]) {
		if (ckey!="extras") {
			var	i=0;
			for(pkey in this.tables[this.selectedTableId][ckey]) {
				i++;
				itemCount = this.gGetElementById("item"+this.selectedTableId+"_"+i+"_count");
				headsCount += itemCount.options[itemCount.selectedIndex].text * 1;
			}
			break;
		}
	}
	item.value = headsCount;
	return headsCount;
}

/* vola sa po stlaceni enteru v datumu-textboxe
 * osetri neodoslanie formulara po stlaceni enteru
 * */		
pc.RecalcAll = function(e) {
	var key = window.event ? e.keyCode : e.which;
	if (parseInt(key) == 13) {
		this.RecalculateAll();
		nosubmit = true;				
	}
}

/* zobrazi cenovu tabulku a priplatky podla vybranej tabulky v comboboxe */		
pc.SetTable = function() {
	var objExtra = this.gGetElementById('priplatky' + this.selectedTableId);
	var objTable = this.gGetElementById('tableCeny' + this.selectedTableId);
	if ( objTable != false ) {
		objTable.style.display="none";
	}

	if ( objExtra != false ) {
		objExtra.style.display="none";
	}

	objTable = this.gGetElementById('tableCeny' + this.gGetElementById('seasonTable').value);
	this.selectedTableId = this.gGetElementById('seasonTable').value;
	objExtra = this.gGetElementById('priplatky' + this.selectedTableId);
	
	if ( objTable != false ) {
		objTable.style.display="block";
	}
	if ( objExtra != false ) {
		objExtra.style.display="block";
	}
	
	if ( this.gGetElementById('formError' + this.selectedTableId) != false )
	{
		this.gGetElementById('formError' + this.selectedTableId).style.display="none";
		this.gGetElementById('formError' + this.selectedTableId+'_2').style.display="none";
		this.RecalculateAll();
	}
}

// skontroluje ci su vyplnene data zaciatku a konca pobytu
pc.CheckDate = function() {
	var reqDateFrom = this.gGetElementById('oddata' + this.selectedTableId+'_d').value + "." + 
                    this.gGetElementById('oddata' + this.selectedTableId+'_m').value + "." + 
                    this.gGetElementById('oddata' + this.selectedTableId+'_y').value;
	var	reqDateTo = this.gGetElementById('dodata' + this.selectedTableId+'_d').value + "." +
                  this.gGetElementById('dodata' + this.selectedTableId+'_m').value + "."+ 
                  this.gGetElementById('dodata' + this.selectedTableId+'_y').value;
	reqDateFrom = this.GetTime(reqDateFrom);
	reqDateTo = this.GetTime(reqDateTo);
	if (reqDateTo>0 && reqDateFrom>0) return true;
	return false;
}

