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

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

RateCalculator.prototype.getPrice = function(propertyId, startDate, endDate, adults, children) {
	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;
				}
			}
		}
		
		return objProperty.getPrice(startDate, endDate, adults, children);
	} catch (e){}
}

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

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

	this.seasons.push(objSeason);
}

PropertyType.prototype.getPrice = function(startDate, endDate, adults, children) {
	var intReturn = 0;
	var dtStartDate = new Date(startDate);
	var dtEndDate = new Date(endDate);

	if (children > 2) {
		adults = (adults - 1) + 1 + (children - 2);
		children = 2;
	}
	if (adults == 1 && children > 0) {
		adults = 2;
		children = children - 1;
	}
	var restPeople = (adults > 2) ? adults - 2 : 0;
		
	//*** Fix the first day.
	dtStartDate.setFullYear(dtStartDate.getFullYear(), dtStartDate.getMonth(), dtStartDate.getDate()+1);
			
	//*** 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.basePrice) + (this.dateDiff(dtStartDate, dtEndDate) * restPeople * season.extraPrice);
				intReturn += (this.dateDiff(dtStartDate, dtEndDate) * children * (season.extraPrice / 2));
			} else {
				intReturn += (this.dateDiff(dtStartDate, season.endDate) * season.basePrice) + (this.dateDiff(dtStartDate, season.endDate) * restPeople * season.extraPrice);
				intReturn += (this.dateDiff(dtStartDate, season.endDate) * children * (season.extraPrice / 2));
			}
		} else if (dtEndDate >= season.startDate && dtEndDate <= season.endDate) {
			intReturn += (this.dateDiff(season.startDate, dtEndDate) * season.basePrice) + (this.dateDiff(season.startDate, dtEndDate) * restPeople * season.extraPrice);
			intReturn += (this.dateDiff(season.startDate, dtEndDate) * children * (season.extraPrice / 2));
		}
	}
		
	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;
}