var Reserv = Class.create();

Reserv.prototype =
{
	initialize : function(/* Array of Offers */offers,/* Array of persons */persons, /*bool - use extrabed*/extraBed,/*ReservationSearcher*/reservationSearcher)
    {    	
    	this.persons=persons;
    	this.offers=offers;
    	this.extraBed=extraBed;
    	this.reservOffers=[];
    	this.extraBedCnt=0;
    	this.sumPrice=0;
    	this.success=(this.execute() && this.postExecuteCheck());
    	this.reservationSearcher=reservationSearcher;
    	this.container=null;
    },
    
    addReservOffer : function(offer,reserv)
    {
    	if (typeof(offer)=='object' && offer.offerId>0)
    		return this.reservOffers[this.reservOffers.length]=new ReservOffer(offer,reserv);
    	else
    		alert('Szoba letrehozasa sikertelen, ervenytelen az offer!');    		
    },
    
    getReservOfferObjects : function()
    { 
    	return this.reservOffers;
    },
    
    getPersonObjects : function()
    {
    	return this.persons;
    },    
    
    updateSumPriceElement : function()
    {
    	sumPriceSpanElement=(this.divContainer.select('span.sumprice')[0]);
		var sumPrice=0;
		reservOfferObj=this.getReservOfferObjects();
		for (var i=0;i<reservOfferObj.length;i++)
			sumPrice+=reservOfferObj[i].calculateSumPrice();
    	sumPriceSpanElement.innerHTML=numberFormat.formatMoneyJsInput(sumPrice)+' '+this.reservationSearcher.currency;
    },
    
    getReservOfferSumPricesHTML : function(reservOfferContainer)
    {
    	
    	var totalPrice=0;
    	
    	var container=document.createElement('div');
    	
    	this.divContainer=Element.extend(container); //IE patch
    	
    	container.appendChild(table=document.createElement('table'));    	
    	table.className='list reserv_header';
    	
    	table.appendChild(tbody=document.createElement('tbody'));
    	
    	tbody.appendChild(tr=document.createElement('tr'));
    	
    	
    	
    	cell=document.createElement('th');
//    	cell.appendChild(document.createTextNode('X'));
    	tr.appendChild(cell);
    	
    	cell=document.createElement('th');
    	cell.appendChild(document.createTextNode('Tétel megnevezés'));
    	tr.appendChild(cell);
    	    	
    	cell=document.createElement('th');
    	cell.appendChild(document.createTextNode('Egységár'));
    	tr.appendChild(cell);
    	
    	cell=document.createElement('th');
    	cell.appendChild(document.createTextNode('Mennyiség'));
    	tr.appendChild(cell);
    	
    	cell=document.createElement('th');
    	cell.appendChild(document.createTextNode('Egység'));
    	tr.appendChild(cell);
    	
    	cell=document.createElement('th');
    	cell.appendChild(document.createTextNode('Összesen'));
    	tr.appendChild(cell);
    	
  
    	reservOffer=this.getReservOfferObjects();
    	for (var i=0;i<reservOffer.length;i++)
    	{   
    		 		
    		var div=document.createElement('div');
    		div.className='reserv_offer';
    		
    		//RoomInfo DIV
    		var roomInfoString=((reservOffer[i].parentOffer.normalbed+' normál ágy '+(((eb=reservOffer[i].parentOffer.totalbedcount-reservOffer[i].parentOffer.normalbed)>0)?', '+eb+' pót ágy':'')));
    		var roomInfoDiv=document.createElement('div');
    		roomInfoDiv.className='roominfodiv';
    		roomInfoDiv.innerHTML=roomInfoString;    		
			div.appendChild(roomInfoDiv);
    		
			//tablazat renderelese
    		div.appendChild(reservOffer[i].getPricesHTML());
    		
    		container.appendChild(div);    		
    	}   
    	
    	//osszesen kiirasa    	
    	table=document.createElement('table');
		tbody=document.createElement('tbody');		
		table.appendChild(tbody);
		table.className='list reserv_footer';
				
		tr=document.createElement('tr');			
		
		cell=document.createElement('td');
		cell.setAttribute('colspan','5');
		cell.appendChild(document.createTextNode('Összesen'));		
		tr.appendChild(cell);
		
		cell=document.createElement('td');		
		cell.appendChild(span=document.createElement('span'));		
		
		span.className='sumprice';
				
		Element.extend(span);
		
		
		var sumPrice=0;
		var reservOfferObj=this.getReservOfferObjects();
		for (var i=0;i<reservOfferObj.length;i++)
			sumPrice+=reservOfferObj[i].calculateSumPrice();
		
		span.innerHTML=numberFormat.formatMoneyJsInput(sumPrice)+' '+this.reservationSearcher.currency;
				
		tr.appendChild(cell);		
		tbody.appendChild(tr);
		    	
		container.appendChild(table);		
		reservOfferContainer.appendChild(container);
    },    
    
    //osszes szoban vegigmegy, es osszeadja a szobankenti arakat.
    getReservOfferSumPrices : function()
    {   
    	result=[]; sumPrice=0;
    	var reservOffer=this.getReservOfferObjects();
    	for (var i=0;i<reservOffer.length;i++)
    	{    		
    		result.push(res=reservOffer[i].initSumPriceObject());
    		sumPrice+=reservOffer[i].calculateSumPrice();
    	}
    	return {sumprice:sumPrice,objects:result};
    },   
    
    execute : function()
    {		
    	//megnyitom az elso szobat
    	this.addReservOffer(this.offers[0],this);
    	var personObj=this.getPersonObjects();
    	for (var personCount=0;personCount < personObj.length;personCount++)
    	{    		
    		var success=false;
    		var reservOfferObj=this.getReservOfferObjects();
    		for (var reservOfferCount=0;reservOfferCount < reservOfferObj.length;reservOfferCount++)
    		{
    			if (reservOfferObj[reservOfferCount].addPerson(personObj[personCount]))	
    			{
    				success=true;
    				break;
    			}
    		}   			
    		if (!success)    		    			
			{
//				alert('szemely elhelyezese sikertelen!');							

				if (this.offers.length<=this.reservOffers.length) {	return false; } //apr16 offer.length-1
				
				var newRoom=this.addReservOffer(this.offers[this.reservOffers.length],this);

				if (personObj[personCount].isChild())
				{
					
					return false;
					/*
					var maxRemoveResultOffer=null;
					var maxRemoveResult=null;
					
					for (var reservOfferCount=0;reservOfferCount<(reservOfferObj=this.getReservOfferObjects()).length;reservOfferCount++)
    				{
    					removeResult=reservOfferObj[reservOfferCount].hasRemoveableAdult();
    					
    					if (removeResult.resultCode>0 && (maxRemoveResult===null ||  removeResult.resultCode>maxRemoveResult.resultCode))
    					{
    						maxRemoveResultOffer=reservOfferObj[reservOfferCount];
    						maxRemoveResult=removeResult;
    					}
    				}

					if (maxRemoveResult===null)
					{
//						alert('Nem helyezheto el a szemely!');						
						return false;
					}
					else
					{
						maxRemoveResultOffer.removePerson(maxRemoveResult.person);
						newRoom.addPerson(maxRemoveResult.person);
						if (!newRoom.addPerson(personObj[personCount]))
						{
//							alert('Nem helyezheto el a szemely 1!');							
							return false;
						}
					}
					*/
				}
				if (!newRoom.addPerson(personObj[personCount]))
				{
//					alert('Nem helyezheto el a szemely 1!');							
					return false;
				}
			}
    	}
    	
    	sumPricesResult=this.getReservOfferSumPrices();
    	
    	this.sumPrice=sumPricesResult.sumprice;
    	
    	return true;     	
	},
	
	postExecuteCheck : function()
	{
		var reservOfferObj=this.getReservOfferObjects();
		for (var i=0;i<reservOfferObj.length;i++)
		{
			var ue=0; //usedextrabed
			var bedObj=reservOfferObj[i].bedList;
			for (var a=0;a < bedObj.length;a++)
			{
				if (bedObj[a].person===null && bedObj[a].type===-1 && bedObj[a].freeextraprice===null)
					return false;				
				if (bedObj[a].person!==null && (bedObj[a].type===0 || bedObj[a].type===1))
					ue++;				

			}
			//miert lett kiveve?? 2010.07.27.
			if (ue<reservOfferObj[i].parentOffer.extrabedrequiredcount)
				return false;
		}
		return true;
	}
};

ReservOffer = Class.create();

ReservOffer.prototype =
{
	initialize : function(/*Offer*/offer,/*Reserv*/reservObject)
	{
		this.bedList=[];
		this.parentOffer=offer;
		this.reservObject=reservObject;
		
		//normal agyak letrehozasa
		for (var i=0;i<this.parentOffer.normalbed;i++)
		{
			var freeextraprice=null;
			var extrabeddata = this.parentOffer.extrabedpricerows;
			for (var a=0;a < extrabeddata.length;a++)
				if (extrabeddata[a].extrabedpricetype==2 && extrabeddata[a].extrabedindex==i)
				{
					freeextraprice=(extrabeddata[a].price);
					freeextraprice=freeextraprice-(freeextraprice*(Math.abs(extrabeddata[a].percent)/100));
				}
			
			this.bedList.push(new Bed(-1,i+1,0,120,this.parentOffer.personcurrencyprice,0,freeextraprice,0,this.parentOffer.storno));
		}
		//potagyak letrehozasa
		if (reservObject.extraBed)
		{
			var extrabeddata=this.parentOffer.extrabedpricerows;
			for (var i=0;i < extrabeddata.length;i++)
			{		
				if (extrabeddata[i].extrabedpricetype!==2)	
				{
					this.bedList.push(new Bed(extrabeddata[i].extrabedpricetype,extrabeddata[i].extrabedindex,extrabeddata[i].minage,extrabeddata[i].maxage,extrabeddata[i].price,0,null,extrabeddata[i].percent,extrabeddata[i].storno));
				}
			}		
		}
	},
		
	//foglalt agyak szamaval ter vissza
	getOccupiedBedCount : function()
	{
		var cnt=0;
		var bedObj=this.bedList;
		for (var i=0;i< bedObj.length;i++)
		{
			if (bedObj[i].person)
				cnt++;
		}
		return cnt;
	},
	
	//szemely elhelyezese egy agyon
	addPerson : function(/*Person*/ person,test)
	{
		res=this.getFreeBeds(person);		
		
		if (res.normalbeds.length>0)
		{			
			var bedObj=res.normalbeds;
			if (!test)
			{
				this.setUsedByBedIndex(bedObj[0].index,-1,bedObj[0].type===-1);
				bedObj[0].setUsed(1);
				bedObj[0].setPerson(person);
			}
			return true;								
		}
		else if(res.extrabeds.length>0)
		{
			var bedObj = null;
			for (var i = 0; i < res.extrabeds.length;++i) if (bedObj === null || bedObj.price > res.extrabeds[i].price)
			{
				bedObj = res.extrabeds[i];
			}
			if (!test)
			{
				
				this.setUsedByBedIndex(bedObj.index,-1,bedObj.type===-1);
				bedObj.setUsed(1);
				bedObj.setPerson(person);
				this.reservObject.extraBedCnt++;
			}
			return true;								
		}
		
		return false;
	},
	
	//readonly offer totalbedcountal ter vissza
	getTotalBedCount : function()
	{
		return (this.parentOffer.totalbedcount);
	},
	
	//readonly offer normalbedcountal ter vissza
	getNormalBedCount : function()
	{
		return (this.parentOffer.normalbed);
	},
	
	/*
		0-nincs kiveheto szemely,
		1-normal kiveheto,
		2-extraagybol veheto ki
		3-szemely kivetelevel a szoba megszunik		
	*/
	hasRemoveableAdult : function()
	{
		if (this.getOccupiedBedCount()===1)	return 3;
		
		var bedObj=this.getExtraBedObjects();
		for (var i = bedObj.length-1;i>=0;--i)
			if (bedObj[i].person!==null && !bedObj[i].person.isChild())	return {resultCode:2,person:bedObj[i].person}; 
			
		var adultCount=0;
		var lastOccupiedIndex=0;
		var bedObj=this.getNormalBedObjects();
		for (var i=0;i < bedObj.length;++i)
			if (bedObj[i].person!==null && !bedObj[i].person.isChild() && ++adultCount>1)
				lastOccupiedIndex = i;
		if (adultCount > 1)
			return {resultCode:1,person:bedObj[lastOccupiedIndex].person};
			
		return {resultCode: 0}; 
	},
	
	/*
    isChild : function(person)
    {
    	var bedObj=this.bedList;
		for (var i=0;i< bedObj.length;i++)
		{
	    	if (
	    			person.age>bedObj[i].minage && 
	    			person.age<=bedObj[i].maxage &&  
	    			bedObj[i].type===1
	    		)
	    		 
	    		return true;   	
		}
		
	    return false;
	    	
    },
    */ 

    //szemely kivetele a szobabol
    removePerson : function(/*person*/person)
    {
    	//agy felszabaditasa
		var bedObj=this.bedList;
    	for (var i=0;i < bedObj.length;i++)
    	{
    		if (bedObj[i].person===person)
    		{
    			bedObj[i].setPerson(null);
    			this.setUsedByBedIndex(bedObj[i].index,0,bedObj[i].type===-1); //ujra kioszhato azaz used=0
    			return true;
    		}
    	}
    	//ha ures lesz, akkor torolni a szobat
    	return false;
    },
	
	remove : function()
	{
    	var reservObject=this.reservObject.getReservOfferObjects();
		for (var i=0;i<reservObject.length;i++)
			if (reservObject[i]===this)
			{
				this.reservObject.reservOffers.splice(i,1);
				return true;
			}
		return false;
	},
	
	getPersons : function()
	{
		var persons=[];var result=[];
		var bedObj=this.bedList;
		for (var i=0;i < bedObj.length;i++)
			if (bedObj[i].person)
				persons.push(bedObj[i].person);
		
		
		/* var personObj=this.reservObject.getPersonObjects();
		 * for (var i=0;i<personObj.length;i++)
			for (var a=0;a<persons.length;a++)
				if (personObj[i].id>=0 && personObj[i].id==persons[a].id)
					result[result.length]=persons[a];*/

		return persons;
	},
	
	getPersonIds : function()
	{
		var ids=[];
		var bedObj=this.bedList;
		for (var i=0;i < bedObj.length;i++)
				if (bedObj[i].person)
					ids[ids.length]=bedObj[i].person.getId();
		return ids;
	},
	
	findReservSumPriceItem : function(unit,label)
	{	
		if (this.reservObject.getReservOfferObjects().length>1)
		{
			if (this.reservObject.getReservOfferObjects()[0].sumPriceObj)
			{
				var sumPriceObjItem=this.reservObject.getReservOfferObjects()[0].sumPriceObj.items;
				for (var i=0;i < sumPriceObjItem.length;i++)				
				{
					if (sumPriceObjItem[i].unit===unit && sumPriceObjItem[i].label===label)
						return true;	
				}
			}
		}				
		
		return false;
	},
	
	initSumPriceObject : function()
	{
		var sumPrice=0;
		var items=[];
		
		this.sumPriceObj={sumPrice:sumPrice,items:items};
		
		var bedObj=this.bedList;
		for (var i=0;i < bedObj.length;i++)
		{
			if (bedObj[i].person)
			{
				tmpPrice=bedObj[i].price;
				sumPrice+=tmpPrice;
				
				//items.push({label:bedObj[i].index+'. '+((bedObj[i].type===-1)?'normál':'pót')+' ágy ('+bedObj[i].person.age+')',price:tmpPrice,percent:bedObj[i].getPercent(), required:1,unit:0,quantity:1});
				if (bedObj[i].type===-1)
					items.push({label:bedObj[i].index+'. normál ágy',price:tmpPrice,percent:bedObj[i].getPercent(), required:1,unit:0,quantity:1,storno:bedObj[i].getStorno()});
				else
					items.push({label:bedObj[i].index+'. pót ágy ('+bedObj[i].minage+'-'+bedObj[i].maxage+')',price:tmpPrice,percent:bedObj[i].getPercent(), required:1,unit:0,quantity:1,storno:bedObj[i].getStorno()});
			}
			else
			{				
				//ures hely araban mar bennevan a %kedvezmeny, mikor addBed-et hivok szamolom)
				sumPrice+=(fep=((typeof(bedObj[i].getFreeExtraPrice())==='undefined' || bedObj[i].getFreeExtraPrice()===null)?0:bedObj[i].getFreeExtraPrice()));
				
				if ((typeof(bedObj[i].getFreeExtraPrice())!=='undefined' && bedObj[i].getFreeExtraPrice()!==null))
					items.push({label:bedObj[i].index+'. üres hely',percent:bedObj[i].getPercent(),price:fep,required:1,unit:0,quantity:1,storno:bedObj[i].getStorno()});
			}
		}
		//0-fo,1-szoba,2-foglalas
		var specialrow = this.parentOffer.specialrows;
		for (var a=0;a < specialrow.length;a++)
		{
			if (specialrow[a].required &&
				specialrow[a].unit==1)
			{				
				items.push({label:specialrow[a].pricename,price:specialrow[a].price,required:1,unit:specialrow[a].unit,quantity:1,percent:specialrow[a].percent,relativeprice:specialrow[a].relativeprice,storno:specialrow[a].storno});
				
				if (specialrow[a].price>0)	sumPrice+=specialrow[a].price;
			}
			if (specialrow[a].required &&
				specialrow[a].unit==2)
			{
				if (!this.findReservSumPriceItem(specialrow[a].unit,specialrow[a].pricename))
				{
					items.push({label:specialrow[a].pricename,price:specialrow[a].price,required:1,unit:specialrow[a].unit,quantity:1,percent:specialrow[a].percent,relativeprice:specialrow[a].relativeprice,storno:specialrow[a].storno});
				
					if (specialrow[a].price>0)	sumPrice+=specialrow[a].price;
				}
			}
			else if (specialrow[a].required!=1)
			{
				if (specialrow[a].unit==1 || specialrow[a].unit==2)
					items.push({label:specialrow[a].pricename,price:specialrow[a].price,required:0,unit:specialrow[a].unit,quantity:1,percent:specialrow[a].percent,relativeprice:specialrow[a].relativeprice,storno:specialrow[a].storno});
				else if (specialrow[a].unit == 0)
				{
					var personCnt=0;
					var person=this.getPersons();
					for (var x=0;x < person.length;x++)
					{
						if (	person[x].age>=specialrow[a].minage && 
    							person[x].age<=specialrow[a].maxage	)
    							
							personCnt++;
					}

					if (personCnt>0)
//						items.push({label:specialrow[a].pricename,percent:specialrow[a].percent,price:specialrow[a].price,required:0,unit:0,quantity:personCnt});
						for (var x=0;x<personCnt;x++)
							items.push({label:specialrow[a].pricename,percent:specialrow[a].percent,price:specialrow[a].price,required:0,unit:0,quantity:1,storno:specialrow[a].storno});
				}									
			}

			var person=this.getPersons();
			for (var x=0;x < person.length;x++)
			if 	(
				specialrow[a].required &&
				specialrow[a].unit==0 &&
				person[x].age>=specialrow[a].minage && 
    			person[x].age<=specialrow[a].maxage
    			)
			{
				items.push({label:specialrow[a].pricename,price:specialrow[a].price,required:1,unit:0,percent:specialrow[a].percent,quantity:1,storno:specialrow[a].storno});
				sumPrice+=specialrow[a].price;
			}	
		}		

		items.push({label:'Sztornó biztosítás',price:0,required:1,unit:1,quantity:1,percent:null,relativeprice:null,storno:null});
		
		return {sumPrice:sumPrice,items:items};
	},

	onClickOptionalItem : function(e,sumPriceGrpItem,checkboxElement,spanPriceElement)
	{
		sumPriceGrpItem.checked=(checkboxElement.checked);
		
		//csoport elemein es vegrehajtom a modositasokat
		for (var a=0;a<this.sumPriceObj.items.length;a++)
			if (sumPriceGrpItem.label == this.sumPriceObj.items[a].label && sumPriceGrpItem.price == this.sumPriceObj.items[a].price && sumPriceGrpItem.percent == this.sumPriceObj.items[a].percent)
				this.sumPriceObj.items[a].checked=sumPriceGrpItem.checked;
		
		this.sumPriceObj.sumPrice=this.calculateSumPrice(); //sumprice frissitese
		
		if (checkboxElement.checked)	spanPriceElement.show();
		else	spanPriceElement.hide();

		this.updateRelativePriceRow();		
		
		//updatelem az ár mezőt (ez a fv is az eredeti sumpriceobject itemeken megy vegig!
		this.reservObject.updateSumPriceElement();
	},
	
	updateRelativePriceRow : function()
	{
		var stornoSum=0;
		Element.extend(this.container); //IE patch
		var tableRow=this.container.select('tr');
		for (var i=0;i < tableRow.length;i++)
		{			
			sumPrice=0;
			if (tableRow[i].sumPriceGrp.relativeprice)
			{	
				//sumpricegrp price mezo frissitese
				var sumPriceItem=tableRow[i].sumPriceGrp.sumpriceitems;
				for (var a=0;a < sumPriceItem.length;a++)
					tableRow[i].sumPriceGrp.price=sumPriceItem[a].price;
				
				Element.extend(tableRow[i].cells[2]); //IE patch
				Element.extend(tableRow[i].cells[5]); //IE patch
				
				//egysegar				
				tableRow[i].cells[2].update(numberFormat.formatMoneyJsInput(tableRow[i].sumPriceGrp.price)+' '+this.reservObject.reservationSearcher.currency);
				
				//osszesen
				tableRow[i].cells[5].select('span')[0].update(numberFormat.formatMoneyJsInput(tableRow[i].sumPriceGrp.price*tableRow[i].sumPriceGrp.grpcnt)+' '+this.reservObject.reservationSearcher.currency);
			}
			
			if (tableRow[i].sumPriceGrp.storno>0 && (tableRow[i].sumPriceGrp.checked || tableRow[i].sumPriceGrp.required))
			{	
				stornoSum+=tableRow[i].sumPriceGrp.price*tableRow[i].sumPriceGrp.storno/100;				
			}
		}
		
		stornoRow=tableRow[tableRow.length-1];
		
		var sumPriceItem=stornoRow.sumPriceGrp.sumpriceitems;
		for (var a=0;a < sumPriceItem.length;a++)
		{
			sumPriceItem[a].price = stornoRow.sumPriceGrp.price = stornoSum;
		}

		Element.extend(stornoRow.cells[2]); //IE patch
		Element.extend(stornoRow.cells[5]); //IE patch

		if (stornoRow.sumPriceGrp.price===0)	stornoRow.hide();
		else	stornoRow.show();
		
		//egysegar				
		stornoRow.cells[2].update(numberFormat.formatMoneyJsInput(stornoRow.sumPriceGrp.price)+' '+this.reservObject.reservationSearcher.currency);
		
		//osszesen
		stornoRow.cells[5].select('span')[0].update(numberFormat.formatMoneyJsInput(stornoRow.sumPriceGrp.price*stornoRow.sumPriceGrp.grpcnt)+' '+this.reservObject.reservationSearcher.currency);
	},

	calculateSumPrice : function()
	{
		sumPrice=0;
		
		this.calculateRelativeItemPrices();
		
		for (var i=0;i<this.sumPriceObj.items.length;i++)
		{
			if (this.sumPriceObj.items[i].required || this.sumPriceObj.items[i].checked)
			{
				//ha van kedvezmeny akkor a sumpricehoz hozzaszamolom				
				priceWithPercent=((this.sumPriceObj.items[i].percent<0)?this.sumPriceObj.items[i].price-(this.sumPriceObj.items[i].price*(Math.abs(this.sumPriceObj.items[i].percent)/100)):this.sumPriceObj.items[i].price);				
				sumPrice+=priceWithPercent*this.sumPriceObj.items[i].quantity;
				
			}			
		}
		
		return sumPrice;
	},
	
	calculateRelativeItemPrices : function()
	{
		sumPrice1=0;
		
		//osszes tetel ami nem relativepriceos
		for (var i=0;i<this.sumPriceObj.items.length;i++)
		{
			if (!this.sumPriceObj.items[i].relativeprice && (this.sumPriceObj.items[i].required || this.sumPriceObj.items[i].checked))
			{
				priceWithPercent=((this.sumPriceObj.items[i].percent<0)?this.sumPriceObj.items[i].price-(this.sumPriceObj.items[i].price*(Math.abs(this.sumPriceObj.items[i].percent)/100)):this.sumPriceObj.items[i].price);				
				sumPrice1+=priceWithPercent*this.sumPriceObj.items[i].quantity;				
			}
		}
		
		for (var i=0;i<this.sumPriceObj.items.length;i++)
		{
																			//		szoba
			if (this.sumPriceObj.items[i].relativeprice && (this.sumPriceObj.items[i].unit===1 || this.sumPriceObj.items[i].unit===2))
			{
				this.sumPriceObj.items[i].price=sumPrice1*(this.sumPriceObj.items[i].relativeprice/100);				
			}
		}
	},
	
	getSumPriceObj : function()
	{
		if (typeof(this.sumPriceObj)==='object')	return this.sumPriceObj;
		
		return false;
	},
	
	getPricesHTML : function()
	{	
		table=document.createElement('table');
		this.container=table;
		tbody=document.createElement('tbody');
		table.className='list';
		table.appendChild(tbody);
		
		var sumPriceGrp=[];
				
		for (var a=0;a<this.sumPriceObj.items.length;a++)
		{
			
			var f=false;
			
			for (var i=0;i<sumPriceGrp.length;i++)
			{				
				if (sumPriceGrp[i].label == this.sumPriceObj.items[a].label && sumPriceGrp[i].price == this.sumPriceObj.items[a].price)
				{
					sumPriceGrp[i].grpcnt++;
					sumPriceGrp[i].sumpriceitems.push(this.sumPriceObj.items[a]);
					f=true;
				}				
			}
			if (f===false)
			{				
				sumPriceGrp.push({	
									label:this.sumPriceObj.items[a].label,
									price:this.sumPriceObj.items[a].price,
									quantity:this.sumPriceObj.items[a].quantity,
									required:this.sumPriceObj.items[a].required,
									unit:this.sumPriceObj.items[a].unit,
									storno:this.sumPriceObj.items[a].storno,
									percent:this.sumPriceObj.items[a].percent,
									relativeprice:this.sumPriceObj.items[a].relativeprice,
									grpcnt:1,
									sumpriceitems:[this.sumPriceObj.items[a]]
								});
			}
		}
		
		this.calculateRelativeItemPrices();
		
		for (a=0;a<sumPriceGrp.length;a++)
		{			
			tr=document.createElement('tr');
			
			cell=document.createElement('td');
			cell.appendChild(input=new Element('input',{type:'checkbox',value:'1'}));
			tr.appendChild(cell);
			
			tr.sumPriceGrp=sumPriceGrp[a];
			
//			input.setAttribute('type','checkbox');input.setAttribute('value','1');	IE patch

			if (sumPriceGrp[a].required)
				input.setAttribute('checked','checked');
				
			input.disabled=((sumPriceGrp[a].required)?true:false);			
			
			cell=document.createElement('td');
			cell.appendChild(document.createTextNode(sumPriceGrp[a].label));
			tr.appendChild(cell);

			//-AR-//
			priceWithPercent=((sumPriceGrp[a].percent<0)?sumPriceGrp[a].price-(sumPriceGrp[a].price*(Math.abs(sumPriceGrp[a].percent)/100)):sumPriceGrp[a].price);
			
			cell=document.createElement('td');
//			cell.appendChild(document.createTextNode(numberFormat.formatMoneyJsInput(sumPriceGrp[a].price)));
			cell.appendChild(document.createTextNode(numberFormat.formatMoneyJsInput(priceWithPercent)+' '+this.reservObject.reservationSearcher.currency));
			tr.appendChild(cell);

			cell=document.createElement('td');
			cell.appendChild(document.createTextNode(sumPriceGrp[a].quantity*sumPriceGrp[a].grpcnt));
			tr.appendChild(cell);

			cell=document.createElement('td');
			cell.appendChild(document.createTextNode(sumPriceGrp[a].unit==0?'fő':sumPriceGrp[a].unit==1?'szoba':'foglalás'));
			tr.appendChild(cell);

			cell=document.createElement('td');
			cell.appendChild(span=document.createElement('span'));
			Element.extend(span);
			
//			span.appendChild(document.createTextNode(numberFormat.formatMoneyJsInput(sumPriceGrp[a].price*sumPriceGrp[a].quantity*sumPriceGrp[a].grpcnt)));
			span.appendChild(document.createTextNode(numberFormat.formatMoneyJsInput(priceWithPercent*sumPriceGrp[a].quantity*sumPriceGrp[a].grpcnt)+' '+this.reservObject.reservationSearcher.currency));
			tr.appendChild(cell);

			if (input.disabled===false)
			{
				span.hide();
				Event.observe(input,"click", this.onClickOptionalItem.bindAsEventListener(this,sumPriceGrp[a],input,span));
			}
			
			
			
			tbody.appendChild(tr);
		}				
		
		this.updateRelativePriceRow();
		
		return table;		
	},
	
	//szabad bed objektumok listaja	
	getFreeBeds : function(/*Person*/ person)
	{
		var result={
						normalbeds:[],
						extrabeds:[]
					};
		
		//normal agyak atnezese
		normalBedObj=this.getNormalBedObjects();
		for (var i=0;i<normalBedObj.length;i++)
		{
			if (
					person.age>=normalBedObj[i].minage &&
					person.age<=normalBedObj[i].maxage &&
					normalBedObj[i].used===0
				)
					result.normalbeds.push(normalBedObj[i]);
		}

		if (result.normalbeds.length===0)
		{
			//potagyak atnezese
			extraBedObj=this.getExtraBedObjects();
			for (var i=0;i<extraBedObj.length;i++)
			{
				if (
						person.age>=extraBedObj[i].minage &&
						person.age<=extraBedObj[i].maxage &&
						extraBedObj[i].used===0
					)
						result.extrabeds.push(extraBedObj[i]);
			}
		}
		
				
		return result;
	},
	
	//used flag allitasa a bedeken -1 mar nem hasznalhato,0-hasznalhato,1-hasznalt
	setUsedByBedIndex : function(/*int>0*/index,/*int(0|1)*/val,normalbed)
    {
		var bed=this.bedList;
    	for (var i=0;i < bed.length;i++)
    	{
    		if (bed[i].index===index && ((normalbed && bed[i].type===-1) || (!normalbed && bed[i].type!==-1)))
    		{
    			bed[i].setUsed(val);
    		}
    	}
    },
    	
    //osszes bed objektummal ter vissza
	getBedObjects : function()
	{
		return this.bedList;
	},
	
	//potagyakkal ter vissza
	getExtraBedObjects : function()
	{
		var result=[];
		
		for (var i=0;i<this.bedList.length;i++)
		{			
			if (this.bedList[i].type>=0 && this.bedList[i].type<2)
			{
				result.push(this.bedList[i]);
			}
		}
		
		return result;
	},
	
	//normalagyakkal ter vissza
	getNormalBedObjects : function()
	{
		var result=[];
		
		for (var i=0;i<this.bedList.length;i++)
		{
			if (this.bedList[i].type===-1)
				result.push(this.bedList[i]);
		}
		
		return result;
	}
};

function Bed(/*-1normal,0felnott,1gyermek,2ures hely*/type,index,minage,maxage,price,used,freeextraprice,percent,storno)
{
	this.type=(type);
	this.index=(index);
	this.minage=(minage);
	this.maxage=(maxage);
	this.price=(price);
	this.used=(used);
	this.freeextraprice=freeextraprice; //lehet null is!
	this.percent=(percent);//kedvezmeny %
	this.person = null;
	this.storno=storno;
}

Bed.prototype =
{		
	getFreeExtraPrice : function()
	{
		return this.freeextraprice;
	},
			
	setUsed : function(val)
	{
		this.used=val;
	},
	
	
	setPerson : function(/*person*/person)
	{
		this.person=person;
	},
	
	getPercent : function()
	{
		return this.percent;
	},
	
	getStorno : function()
	{
		return this.storno;
	},
	
	setPercent : function(percent)
	{
		this.percent=Number(percent);
	}
};

Person = Class.create();

Person.prototype =
{
	initialize : function(/*int*/age,/*int*/id)
	{
		this.age=(age);
		this.id=id;
	},	
	
	getId : function()
	{
		return this.id;
	},
	
	isChild : function()
	{
   		return this.age<18;
   	}
};

/*
reservoffer (szoba)
-agy hozzzaadasa
-agy elvetele

bed (agy)
-tipus -1-normal,0-felnott,1-gyermek,2-ures hely
-price
-index 1,2,3...
-used
-minage
-maxage

*/

