function Individual() {
	var tons = 0;
	var carbonCredits = 0;
	var paymentOption = 1; // trees only
	var isForfait = false;

	this.init = function init() {
		this.carbonCredits = 0;
	};
	
	this.setPaymentOption = function setPaymentOption(paymentOption) {
		this.paymentOption = paymentOption;
	};
	
	this.home = function home(province, electricity, gaz, oil, wood) {
		var price = 0;
		var intensity = 0;

		switch(province) {
		case 'QC':
			price = 6.81;
			intensity = 6.0;
			break;
		case 'ON':
			price = 10.89;
			intensity = 180;
			break;
		case 'MAN':
			price = 6.44;
			intensity = 10;
			break;
		case 'SAS':
			price = 10.91;
			intensity = 810;
			break;
		case 'ALB':
			price = 13.45;
			intensity = 930;
			break;
		case 'CB':
			price = 6.98;
			intensity = 20;
			break;
		case 'TN':
			price = 10.43;
			intensity = 15;
			break;
		case 'IPE':
			price = 14.81;
			intensity = 192;
			break;
		case 'NE':
			price = 11.75;
			intensity = 549;
			break;
		case 'NB':
			price = 11.51;
			intensity = 366;
			break;
		} 
		
		this.tons = 0;
		
		if(electricity > 0) {
			this.tons += (electricity * 100.0 * intensity * 12.0) / price;
		}
		
		if(gaz > 0) {
			this.tons += (gaz * 100.0) * ((1891 * 12) / 49.65);
		}
		
		if(oil > 0) {
			this.tons += (oil * 100.0) * (2725 / 105.86);
		}
		
		if(wood > 0) {
			this.tons += wood * 1500 * 1430;
		}
		
		this.tons /= 1000000;
	};
	
	this.car = function car(km, averageLitter, carFuel) {
		this.tons = 0;
		var littersPerYear = (km * averageLitter) / 100;
		var co2PerLitter = 2.367;

		if(carFuel == 'Z') {
			co2PerLitter = 2.54;
		}
		
		var emissionsPerYear = littersPerYear * co2PerLitter;
		var emissionstons = emissionsPerYear / 1000;
		
		this.tons = emissionstons;
	};

	this.fly = function fly(departure, arrival, roudtrip) {
		km = 0.0;

		if(departure != "" && arrival != "") {
			var html = $.ajax({
				  url: "calculateairportsdistance?c1=" + departure + '&c2=' + arrival,
				  async: false
				 }).responseText;
			
			km = parseInt(html);
			
			if(km < 800) {
				this.tons = parseFloat(km) * 0.1753;
			} else if(km < 3700) {
				this.tons = parseFloat(km) * 0.0983;
			} else {
				this.tons = parseFloat(km) * 0.1106;
			}
			
			if(roudtrip == "true") this.tons *= 2;
			
			this.tons /= 1000.0;
		}
	};

	this.trainbus = function trainbus(trainKm, trainInterval, busKm, busInterval) {
		this.tons = 0;

		if(trainInterval == "true") {
			trainKm *= 52.0;
		}
		
		if(busInterval == "true") {
			busKm *= 52.0;
		}
		
		this.tons += (trainKm * 130.0);
		this.tons += (busKm * 76.0);
		
		
		
		this.tons /= 1000000;
	};

	this.event = function event(name, guests, cars, carsKm, flyLocal, flyInternationalShort, flyInternationalLong, hotel, rooms, night, hotelType) {
		this.tons = 0;
		
		// calculate cars
		this.tons += (cars *  carsKm * (10.9 / 100.0) * 2289.0 * 2.0) / 1000000;
		
		// calculate flights
		this.tons += (parseFloat((flyLocal * 175.3) +
				(flyInternationalShort * 98.3) +
				(flyInternationalLong * 110.6))) / 1000000;
		
		// this.tons += parseFloat((rooms * night * 49895.1607));
		
		// calculate hotels
		if(hotelType == 2) {
			this.tons += parseFloat((rooms * night * 0.011));
		} else {
			this.tons += parseFloat((rooms * night * 0.055));			
		}
		
		//this.tons /= 1000000;

	};
	
	this.gift = function gift(fromname, fromemail, toname, toemail, message, amount, tons) {
		this.tons = parseFloat(amount) / 120.0;
	};
	
	this.giftOnly = function giftOnly(fromname, fromemail, toname, toemail, message, amount, tons, trees) {
		if(tons > 0) {
			this.tons = tons;
		} else if(amount > 0) {
			this.tons = parseFloat(amount) / 120.0;
		} else {
			this.tons = parseFloat(trees) / 6.0;
		}
		
	};
	
	this.individ = function individ(adultes, kids, percent) {
		this.tons = 0;
		
		this.tons += parseFloat((adultes * 10.7 * percent) + (kids * 0.5 * 10.7 * percent));
		
		this.tons /= 100;
	};
	
	/** BUSINESS **/
	this.building = function building(province, electricity, gaz, oil) {
		var price = 0;
		var intensity = 0;

		switch(province) {
		case 'QC':
			price = 6.81;
			intensity = 6.0;
			break;
		case 'ON':
			price = 10.89;
			intensity = 180;
			break;
		case 'MAN':
			price = 6.44;
			intensity = 10;
			break;
		case 'SAS':
			price = 10.91;
			intensity = 810;
			break;
		case 'ALB':
			price = 13.45;
			intensity = 930;
			break;
		case 'CB':
			price = 6.98;
			intensity = 20;
			break;
		case 'TN':
			price = 10.43;
			intensity = 15;
			break;
		case 'IPE':
			price = 14.81;
			intensity = 192;
			break;
		case 'NE':
			price = 11.75;
			intensity = 549;
			break;
		case 'NB':
			price = 11.51;
			intensity = 366;
			break;
		} 
		
		this.tons = 0;
		
		if(electricity > 0) {
			this.tons += (electricity * 100.0 * intensity * 12.0) / price;
		}
		
		if(gaz > 0) {
			this.tons += (gaz * 100.0) * ((1891 * 12) / 49.65);
		}
		
		if(oil > 0) {
			this.tons += (oil * 100.0) * (2725 / 105.86);
		}
		
		this.tons /= 1000000;
	};
	
	this.cars = function cars(cars, km, trucks, tkm) {
		this.tons = 0;
		
		this.tons += parseFloat( (cars * km * 140.0) + (trucks * tkm * 235.0));
		
		this.tons /= 1000000;
	};
	
	this.trips = function trips(domesticFly, internationalFly, internationalLongFly, trainTotal, trainTotalKm, busTotal, busTotalKm, hotelTotalHigh, hotelTotal) {

		this.tons = 0;

		this.tons += parseFloat(domesticFly * 0.1753* 800) / 1000;
		this.tons += parseFloat(internationalFly * 0.0983* 2250) / 1000;
		this.tons += parseFloat(internationalLongFly * 0.1106* 3700) / 1000;

		this.tons += parseFloat(trainTotal * trainTotalKm * 0.13) / 1000;
		this.tons += parseFloat(busTotal * busTotalKm * 76) / 1000000;

		this.tons += parseFloat(hotelTotalHigh * 0.055) ;
		this.tons += parseFloat(hotelTotal * 0.011) ;
		
	}
	
	this.paperPercentCo2 = function paperPercentCo2(paperPercent) {
		if(paperPercent == 0) {
			return 2.941;
		} else if(paperPercent == 30) {
			return 2.572;
		} else {
			return 1.711;
		}
	}
	
	this.papers = function papers(paperPercent, paperQuantity) {

		this.tons = 0;
		
		this.tons += parseFloat(this.paperPercentCo2(paperPercent) * paperQuantity);
		
		this.tons /= 1000;
	}
	
	this.postal = function postal(postalMethod, postalNumber, postalWeight, postalKm) {

		this.tons = 0;
		
		var postalMethodEmission = 0.0;
		
		if(postalMethod == "R") {
			postalMethodEmission = 33;
		} else if(postalMethod == "M") {
			postalMethodEmission = 10;
		} else {
			postalMethodEmission = 17;
		}
		
		this.tons += parseFloat(postalMethodEmission * postalNumber * (postalWeight / 1000) * postalKm);
		
		this.tons /= 1000000;
	}
		
	this.setForfait = function setForfait(tons, trees, credits, price) {
		this.isForfait = true;
		this.tons = tons;
		this.carbonCredits = credits;
		
		if(credits > 0 && trees < 0) {
			this.paymentOption = 3;
		} else {
			this.paymentOption = 1;
		}
	};
		
	this.calculate = function calculate() {
		if(!this.isForfait) {
			if(this.paymentOption == 2) {
				this.carbonCredits = this.roundUp(this.tons / 2.0);
			} else if(this.paymentOption == 3) {
				this.carbonCredits = this.roundUp(this.tons);
			}
		}
		
	};
	
	this.roundUp = function roundUp(value) {
		return Math.round(value * 100) / 100;
	};

	this.getTons = function getTons() {
		return this.roundUp(this.tons);
	};

	this.setTons = function setTons(t) {
		this.tons = t;
	};

	this.getCost = function getCost() {
		cost = 0;
		
		if(this.paymentOption == 1) {
			cost = this.roundUp( (parseFloat(this.tons) * 120.00));
			
			if(cost < 20)
				cost = 20;

		} else if(this.paymentOption == 2) {
			
			half = this.roundUp(parseFloat(this.tons) / 2.0);
			
			cost = (half * 120.0) + (half * 20.0);
			
			if(cost < 35)
				cost = 35;

		} else {
			cost = this.roundUp( (parseFloat(this.tons) * 20.00));

			if(cost < 10)
				cost = 10;
		}

		return cost;
	};
	
	this.getCarbonCredits = function getCarbonCredits() {
		return this.carbonCredits;
	};
	
	this.getTrees = function getTrees() {
		var tree = 0;
		
		if(this.paymentOption != 3) {
			cost = this.getCost() - parseFloat(this.carbonCredits * 20);
			tree = Math.ceil(cost / 20.0) ;
		}	
		
		if(tree < 1 && !this.isForfait && this.paymentOption != 3) {
			tree = 1;
		}
		return tree;
	};
}


