

function AlcoholCalculator(mode) {
	this.mode = mode;	//advance, basic
	this.sex;		//male, female
	this.age;		//int
	this.height; 		//cm
	this.weight;   		//kg
	this.waterVolume; 	//mL
	this.OUNCE_ALCOHOL = 23.36 //g
	this.concOunce;		// g/ounce
	this.alcohol;
	this.limits = { level0 : 0, level1 : 0.01, level2 : .03, level3: 0.06, level4: 0.08, level5: 0.09, level6: 0.1 };
	
	this.message = { level0 : 'Sober', level1 : 'Possible impairment', level2 : 'buzzed', level3: 'impaired', level4: 'illegal in some places', level5: 'extremely intoxicated', level6: 'possibly fatal' };
	this.drinks = new Array();
	this.metabolism = 'moderate'; 	//moderate, normal, heavy
	this.metaValues = { moderate : 0.012, average : 0.017, heavy : 0.02 };
	this.minutes;		//minutes;
	this.BAC; 		
}

AlcoholCalculator.prototype.reset = function() {
	this.sex = 0;		//male, female
	this.age = 0;		//int
	this.height = 0;	//cm
	this.weight = 0;	//kg
	this.waterVolume = 0; 	//mL
	this.concOunce = 0;	// g/ounce
	this.alcohol = 0;
	this.drinks = new Array();
	this.metabolism = 'moderate'; 	//moderate, normal, heavy
	this.minutes = 0;		//minutes;
	this.BAC = 0; 	
}

AlcoholCalculator.prototype.getMessage = function(type) {
	return this.message[type];
}

AlcoholCalculator.prototype.getLimit = function() {
	var type = 'level0';
	var newType;
	for(newType in this.limits) {
		if(this.BAC >= this.limits[newType]) {
			type = newType;
		}
	}
	if(type == 'level0' && this.drinks.length > 0) type = 'level1';
	return type;
}

AlcoholCalculator.prototype.setMode = function(val) {
	this.mode = val;
}
AlcoholCalculator.prototype.setSex = function(val) {
	this.sex = val;
}
AlcoholCalculator.prototype.setAge = function(val) {
	this.age = val;
}
AlcoholCalculator.prototype.setMinutes = function(val) {
	this.minutes = val;
}
AlcoholCalculator.prototype.setWeight = function(val) {
	this.weight = val;
}
AlcoholCalculator.prototype.setHeight = function(val) {
	this.height = val;
}
AlcoholCalculator.prototype.setMetabolism = function(val) {
	this.metabolism = val;
}
AlcoholCalculator.prototype.addDrink = function(unit, val, alcohol) {
	var drink = new Drink(unit, val, alcohol);
	this.drinks.push(drink);
}

AlcoholCalculator.prototype.addTab = function(drinks) {
	for(var i=0; i < drinks.length; i++) {
		this.drinks.push(drinks[i]);
	}
}

AlcoholCalculator.prototype.removeDrink = function(index) {
	var drinks = this.drinks;
	var newDrinks = new Array();
	for(var i=0; i < drinks.length; i++) {
		if(i != index) {
			newDrinks.push(drinks[i]);
		}
	}
	this.drinks = newDrinks;
}

AlcoholCalculator.prototype.getTime = function(unit) {
	if(unit == 'hours') {
		return this.minutes/60;
	}
	else if (unit == 'minutes') {
		return this.minutes;
	}
}


AlcoholCalculator.prototype._calcWater = function() {
	if(this.mode == 'basic') {
		var waterVolume = { female : .49, male : .58 }
		this.waterVolume = this.weight * waterVolume[this.sex];
	}
	else {
		if(this.sex == 'male') {
			this.waterVolume = 2.447 - (0.09156 * this.age) + (0.1074 * this.height) + (0.3362 * this.weight);
		}
		else if (this.sex == 'female') {
			this.waterVolume = -2.097 + (0.1069 * this.height) + (0.2466 * this.weight);
		}
	}
	this.waterVolume = this.waterVolume*1000;
}

AlcoholCalculator.prototype._calcConcentration = function() {
	this.concOunce = ((this.OUNCE_ALCOHOL/this.waterVolume) * 0.806)*100;
}

AlcoholCalculator.prototype._calcDrinks = function() {
	var alcohol = 0;
	for(var i=0; i < this.drinks.length; i++) {
		alcohol += this.drinks[i].getAlcoholAmount('oz');
	}
	this.alcohol = alcohol;
}

AlcoholCalculator.prototype.calcBAC = function() {
	this._calcWater();
	this._calcConcentration();
	this._calcDrinks();
	this.BAC = (this.alcohol * this.concOunce) - (this.getTime('hours') * this.metaValues[this.metabolism]);
	this.BAC = round(4, this.BAC);	
	
	/*
	if( (this.BAC < 0.0001) && (this.BAC != 0) ) {
		 this.BAC = 0.0001;
	}
	*/
	if( (this.BAC < 0.001) && (this.drinks.length > 0) ) {
		this.BAC = 0.001;
	}
	else if (this.BAC < 0.001) {
		this.BAC = 0;
	}
	
	return this.BAC;
}


function Drink(unit, val, alcohol) {
	this.unit = unit;
	this.volume = val;
	this.percentage = alcohol;
	this.name = '';
}

Drink.prototype.setName = function(name) {
	this.name = name;
}

Drink.prototype.getAlcoholAmount = function(unit) {
	var val = convertUnits(this.unit, unit, this.volume);
	return (val * (this.percentage/100));
}

function convertUnits(from, to, value) {
	var decimal = { kg : 0, lbs : 0, inches : 2, cm : 1, oz : 1, ml : 0, pint : 1 };
	var acceptable = new Array('kg_lbs', 'lbs_kg', 'kg_kg', 'lbs_lbs', 'inches_cm', 'cm_inches', 'inches_inches', 'cm_cm', 'oz_ml', 'oz_pint', 'ml_oz', 'pint_oz', 'ml_pint', 'pint_ml', 'oz_oz', 'ml_ml', 'pint_pint');
	var key = from+'_'+to;
	if(!in_array(key, acceptable)) {
		return value;
	}
	if( from == to ) return round(decimal[to], value);
	
	if(key == 'kg_lbs') {
		return round(decimal[to], (value/0.4536));	
	}
	else if(key == 'lbs_kg') {
		return round(decimal[to], (value*0.4536));	
	}
	
	else if(key == 'inches_cm') {
		return round(decimal[to], (value*2.54));	
	}
	else if(key == 'cm_inches') {
		return round(decimal[to], (value/2.54));	
	}
	
	else if(key == 'oz_ml') {
		return round(decimal[to], (value*29.57));	
	}
	else if(key == 'ml_oz') {
		return round(decimal[to], (value/29.57));	
	}

}

function round(places, value) {
	 var offset = Math.pow(10, places);
	 return (Math.round(value*offset)/offset);
}

function in_array(needle, haystack) {
	for(var i=0; i < haystack.length; i++) {
		if(haystack[i] == needle) return true;
	}
	return false;
}

