// Parking calculator object
function ParkingCalc()
{
	// inputs
	this.arrivalDate;
	this.departureDate;
	
	// outputs
	this.duration;
	this.shortTermCost;
	this.longTermCost;
}

ParkingCalc.prototype.calculate = function()
{
	// find duration
	this.duration = new Duration();
	this.duration.fromTo(this.arrivalDate, this.departureDate);
	
	// calculate cost
	this.shortTermCost = calcShortTermCost(this.duration);
	this.longTermCost = calcLongTermCost(this.duration);
}

//calculate short term cost
// d is duration
function calcShortTermCost(d)
{
	// rates
	var first20 = 0;
	var next20 = 2;
	var firstHour = 3;
	var daily = 16;
	
	var cost = 0;
	
	// 'duration.seconds' can be omitted because the input dates could not be less than a minute.
	
	// 1st 1 hour of day 0
	if(d.days==0 && d.hours==0){
		if(d.minutes<=20){	cost = first20; }
		else if(d.minutes<=40){	cost = next20; }
		else {	cost = firstHour; }
		return cost;
	}

	// else
	cost = d.hours * firstHour;
	if(d.minutes>0){
		cost += firstHour;
	}
	
	if(cost>16){
		cost = 16;
	}
	
	cost += d.days * daily;
	
	return cost;
}

//calculate long term cost
function calcLongTermCost(d)
{
	// rates
	var hourly = 2;
	var daily = 11;
	var firstWeek = 60;
	var daily2 = 4;

	var cost = 0;

	if(d.days<7){
		if(d.minutes>0) { cost += hourly; }
		cost += d.hours * hourly;
		if(cost>daily) {cost = daily;}
		cost += d.days * daily;
		if(cost>firstWeek) { cost = firstWeek; }
	}
	else{
		if(d.minutes>0) { cost += hourly; }
		cost += d.hours * hourly;
		if(cost>daily2) {cost = daily2;}
		cost += (d.days-7) * daily2;
		cost += firstWeek;
	}	
	
	return cost;
}

//-------------------------------------------------------
function Duration()
{
	this.startDate;
	this.endDate;
	this.l=0;
	
	this.totalMillis;
	this.days;
	this.hours;
	this.minutes;
	this.seconds;
}

/* smallest value of startDate and endDate should be second. */
Duration.prototype.fromTo = function(startDate, endDate)
{
	this.startDate = startDate;
	this.endDate = endDate;
	
	if(this.endDate.getFullYear()!=2011){this.l=1;}
	
	// get the difference in milliseconds
	var ms = this.endDate.getTime() - this.startDate.getTime();
	this.totalMillis = ms;
	
	// get the difference in days
	oneDay = 60 * 60 * 24 * 1000;
	var d = ms / oneDay;
	//var dr = d * oneDay;
	//var dd = Math.round(dr) / oneDay;
	
	// num. of days
	this.days = Math.floor(d);
	
	// num. of hours
	h = (d - this.days) * 24;
	this.hours = Math.floor(h);
	
	// num. of minutes
	m = (h - this.hours) * 60;
	this.minutes = Math.floor(m);
	
	// seconds (value smaller than second will be omitted)
	s = (m - this.minutes) * 60;
	this.seconds = Math.round(s);
	
	if(this.seconds == 60){
		this.seconds = 0;
		this.minutes++;
	}
	if(this.minutes == 60){
		this.minutes= 0;
		this.hours++;
	}
	if(this.hours== 24){
		this.hours= 0;
		this.days++;
	}
}
