
function RateCalculator() {
	this.propertees = [];
	this.buffet = 0;
}

RateCalculator.prototype.addProperty = function(objProperty){
	this.propertees.push(objProperty);
}

RateCalculator.prototype.setBuffet = function(fltBuffet){
	this.buffet = fltBuffet;
}

RateCalculator.prototype.getPrice = function(propertyId, startDate, endDate, people, buffet) {
	var fltReturn = 0;
	try {
		var objProperty;
		
		for (var count = 0; count < this.propertees.length; count++) {
			if (isNaN(parseInt(propertyId))) {
				if (this.propertees[count].name == propertyId) {
					objProperty = this.propertees[count];
					break;
				}
			} else {
				if (this.propertees[count].id == propertyId) {
					objProperty = this.propertees[count];
					break;
				}
			}
		}
		
		fltReturn = objProperty.getPrice(startDate, endDate, people);
		if (buffet > 0) {
			//*** Fix the first day.
			startDate.setFullYear(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()+1);
			
			fltReturn += people * this.buffet * objProperty.dateDiff(startDate, endDate);
		}
	} catch (e){}
	
	return Math.round(fltReturn * 100) / 100;
}

function PropertyType(id, name, maxPeople, limitPeople, limitPrice) {
	this.id = id;
	this.name = name;
	this.maxPeople = maxPeople;
	this.limitPeople = limitPeople;
	this.limitPrice = limitPrice;
	this.seasons = [];
}

PropertyType.prototype.addSeason = function(startDate, endDate, price){
	var objSeason = {};
	objSeason.startDate = new Date(startDate);
	objSeason.endDate = new Date(endDate);
	objSeason.price = price;

	this.seasons.push(objSeason);
}

PropertyType.prototype.getPrice = function(startDate, endDate, people) {
	var intReturn = 0;
	var dtStartDate = new Date(startDate);
	var dtEndDate = new Date(endDate);
	
	//*** Fix the first day.
	dtStartDate.setFullYear(dtStartDate.getFullYear(), dtStartDate.getMonth(), dtStartDate.getDate()+1);
		
	if (people > this.limitPeople) people = this.limitPeople;
	var restPeople = (people > this.maxPeople) ? (people - this.maxPeople) : 0;
	
	//*** Get season(s);
	for (var count = 0; count < this.seasons.length; count++) {
		var season = this.seasons[count];
		if (dtStartDate >= season.startDate && dtStartDate <= season.endDate) {
			if (dtEndDate <= season.endDate) {
				intReturn += this.dateDiff(dtStartDate, dtEndDate) * season.price;
			} else {
				intReturn += this.dateDiff(dtStartDate, season.endDate) * season.price;
			}
		} else if (dtEndDate >= season.startDate && dtEndDate <= season.endDate) {
			intReturn += this.dateDiff(season.startDate, dtEndDate) * season.price;
		}
	}
	
	intReturn += (restPeople * this.limitPrice * this.dateDiff(startDate, endDate)) ;
	
	return Math.round(intReturn * 100) / 100;
}

PropertyType.prototype.dateDiff = function(startDate, endDate) {
	var dtStart = new Date(startDate);
	var dtEnd = new Date(endDate);
		
	var intDay = 1000*60*60*24;

	return Math.ceil((dtEnd.getTime() - dtStart.getTime()) / intDay) + 1;
}