function PriceCalculator(tables, tablesDayCount, configRelativePath, selectedTable, selectedTableId) {
	this.tables = tables;
	this.tablesDayCount = tablesDayCount;
	this.configRelativePath = configRelativePath;
	this.selectedTable = selectedTable;
	this.selectedTableId = selectedTableId;
}
 
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('dateFrom' + this.selectedTableId).value;
	var reqDateTo = this.gGetElementById('dateTo' + this.selectedTableId).value;
	var dateFrom, dateTo, reqDateFrom, reqDateTo, reqDayCount;
	var	tmpPrice, isOnBoundary;
	
	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;
		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) ) {						                            
						reqDayCount = ( reqDateTo - reqDateFrom ) /  86400000;  // 60 * 60 * 24 * 1000
						item.value = Math.ceil(( 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.ceil(( this.tables[tableId][key][catId-1] / this.tablesDayCount[tableId] ) * reqDayCount * itemCount);	
				}
				
				// pobyt na rozhrani dvoch cenovych pasiem
				if ( (reqDateFrom >= dateFrom) && (reqDateFrom < dateTo)
					&& (reqDateTo > dateTo) ) {
					reqDayCount = ( dateTo - reqDateFrom ) /  86400000;
					tmpPrice = Math.ceil(( 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.ceil(( 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;
 }
 
/* prepocita vsetky polozky */
pc.RecalculateAll = function() {
	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.SumAll();

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

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

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

	var pridavek;


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

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

	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 rozdil
		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);
				}
			}

		}
	}

	//pripocteni kurzovniho rozdilu
	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('dateFrom' + this.selectedTableId).value;
	var	reqDateTo = this.gGetElementById('dateTo' + this.selectedTableId).value;
	
	reqDateFrom = this.GetTime(reqDateFrom);
	reqDateTo = this.GetTime(reqDateTo);

	var dayCount = ( reqDateTo - reqDateFrom  ) / 86400000;
	if (isNaN(dayCount)) item.value = 0;
	else item.value = dayCount;	
}

/* 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";
	}
	
	this.gGetElementById('formError' + this.selectedTableId).style.display="none";
	this.RecalculateAll();
}

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

