// JavaScript Document

var count = 0;
var hopcount = 0;
var grainweight = 0;
var maximum = 0;
var sg = 0;
var plato = 0;
var newhops = 0;
var color = 1;

function addIngredientField() {
	if (count < 10) {
		for (var i = 1; i <= 10; i++) {
			var row = 'row' + i;
			if (document.getElementById(row).style.display == 'none' || document.getElementById(row).style.display == '') {
				document.getElementById(row).style.display = 'block';
				count++;
				break;
			}
		}
	}
}

function removeIngredientField(arg) {
	if (count >= 0) {
		count--;
		var row = 'row' + arg;
		var type = 'ingredient' + arg;
		var amt = 'pounds' + arg;
		document.getElementById(row).style.display = 'none';
		document.getElementById(type).selectedIndex = 0;
		document.getElementById(amt).value = 0;
		maltValues();
	}
}

function addHopField() {
	if (hopcount < 10) {
		for (var i = 1; i <= 10; i++) {
			var hoprow = 'hoprow' + i;
			if (document.getElementById(hoprow).style.display == 'none' || document.getElementById(hoprow).style.display == '') {
				document.getElementById(hoprow).style.display = 'block';
				hopcount++;
				break;
			}
		}
	}
}

function removeHopField(arg) {
	if (hopcount >= 0) {
		hopcount--;
		var hoprow = 'hoprow' + arg;
		var hops = 'hop' + arg;
		var mins = 'minutes' + arg;
		var oz = 'ounces' + arg;
		document.getElementById(hoprow).style.display = 'none';
		document.getElementById(hops).selectedIndex = 0;
		document.getElementById(mins).value = 1;
		document.getElementById(oz).value = 0;
		hopUtilization();
	}
}

function maltValues() {
	var total = 0;
	var mcu = 0;
	var srm = 0;
	grainweight = 0;
	for (var i=1; i <= 10; i++) {
		var type = 'ingredient' + i;
		var typevals = document.getElementById(type).value.split(':');
		var extract = typevals[0];
		var lovibond = typevals[1];
		var ppg = 46 * extract;
		var amt = 'pounds' + i;
		var pounds = document.getElementById(amt).value;
		
		total += ppg * pounds;
		mcu += lovibond * pounds;
		grainweight += 1*pounds;
	}
	
	if (grainweight != 0) {	
		var mash = (grainweight/4).toFixed(2);
		var step = (mash/2).toFixed(2)
		var sparge = (grainweight/2).toFixed(2);
		var absorbed = (grainweight/10).toFixed(2);
		var volume = document.getElementById('volume').value;
		var boil = ((1*volume) + (volume * .2)).toFixed(2);
		var added = (boil - ((1*mash) - (1*absorbed) + (1*sparge))).toFixed(2);
		var stepadded = ((1*added) - (1*step)).toFixed(2);
		var mashtype = document.getElementById('mashtype').value;
	
		var infusion = '<strong>Infusion:</strong><br><ul>' + mash + ' gallons of mash water (168-174&ordm;F)<br>' + sparge + ' gallons of sparge water (170&ordm;F)<br>' + added + ' gallons added to boil.</ul>';
	
		var step = '<strong>Step Mash:</strong><br><ul>' + mash + ' gallons of mash water (130&ordm;F)<br>' + step + ' gallons of step water (200&ordm;F)<br>' + sparge + ' gallons of sparge water (170&ordm;F)<br>' + stepadded + ' gallons added to boil.</ul>';
		if (volume != 0) {	
			total = total/volume;	
			srm = (1.4922 * Math.pow((mcu/volume), 0.6859)).toFixed(1);
			var colorIndex = Math.round(srm);
			var colorkey = 'color' + colorIndex;
			if (colorIndex <= 40) {
				color = document.getElementById(colorkey).value;
			} else {
				color = '#000';	
			}
		} else { 
			total = 0;
			srm = 0;
		}
	
		maximum = ((Math.round(total)/1000) + 1).toFixed(3);
		var maxplato = Math.round((-0.003 + (0.258587 * total) - (0.00022281 * Math.pow(total, 2)) + (0.000000135997 * Math.pow(total, 3))) * 10)/10;
		var brewhouse = document.getElementById('extract').value/100;
		var expected = (((Math.round(total) * brewhouse)/1000) + 1).toFixed(3);
		var explato = Math.round((-0.003 + (0.258587 * total*brewhouse) - (0.00022281 * Math.pow(total*brewhouse, 2)) + (0.000000135997 * Math.pow(total*brewhouse, 3))) * 10)/10;
		sg = expected;
		plato = explato;
		document.getElementById('og').value = expected;
		document.getElementById('totalbox').innerHTML = 'Your Maximum Potential Exact Target is: &nbsp; <strong>' + maximum + '</strong> &nbsp; (' + maxplato + '&ordm;)<br><hr> Your Approximate Expected Wort Gravity is: &nbsp; <strong>' + expected + '</strong> &nbsp; (' + explato + '&ordm;)<br><hr><div style=\"background-color: ' + color + '; float: right; width: 80px; height: 20px; margin-left: 10px;\">&nbsp;</div> Your Approximate Expected SRM is: &nbsp; <strong>' + srm + '</strong><br><hr>';
		document.getElementById('gravity').value = expected;
		document.getElementById('mashing').style.display = 'block';
		document.getElementById('finish').style.display = 'block';
		calculateAlcohol();
		if (mashtype == 'infusion') {
			document.getElementById('mashing').innerHTML  = infusion;
		} else {
			document.getElementById('mashing').innerHTML  = step;	
		}
	} else {
		document.getElementById('mashing').style.display = 'none';
		document.getElementById('totalbox').innerHTML = '';
		document.getElementById('finish').style.display = 'none';
	}
	if (hopcount > 0) {
		hopUtilization();
	}
	var bbl = document.getElementById('bbl').value;
	if (bbl != 0) {
		convert2BBL();
	}
}

function hopChange(index) { 
	newhops = index;	
	hopUtilization();
}

function hopUtilization() {
	var ibu = 0;
	var grav = document.getElementById('gravity').value;
	var wort = document.getElementById('volume').value;
	for (var i=1; i <= 10; i++) {
		var mins = 'minutes' + i;
		var minutes = document.getElementById(mins).value;
		var acid = 'aa' + i;
		if (newhops == i) {
			var alpha = 'hop' + i;
			var aa = document.getElementById(alpha).value;
			document.getElementById(acid).value = aa;
		} else {
			var aa = document.getElementById(acid).value;	
		}
		var oz = 'ounces' + i;
		var ounces = document.getElementById(oz).value;	
		var util = Math.round((1.65 * Math.pow(0.000125, grav - 1)) * ((1 - Math.pow(Math.E,(-0.04 * minutes)))/4.15) * 1000)/10;
		var bitterness = Math.round(util * (ounces * (aa/100) * 7490) / wort)/100;
		ibu += bitterness;
		if (count == 0 || document.getElementById('gravity').value == 1) {
			document.getElementById('ibubox').innerHTML = 'You should select your grains before calculating IBU';
		} else {
			document.getElementById('ibubox').innerHTML = 'Your Approximate Expected IBU is: &nbsp; <strong>' + ibu.toFixed(2) + '</strong>';
		}
	}
	newhops = 0;
	if (bbl != 0) {
		convert2BBL();
	}
}

function convert2BBL() {
	var bbl = document.getElementById('bbl').value;
	if (bbl != 0) {
		var weight = bbl * 31 * 8.3 * sg;
		var extractweight = weight * (plato/100); 
		var extperc = document.getElementById('extract').value/100;
		var grainbill = '<strong>Grain Bill</strong><hr>';
		var totalweight = 0;
		var volume = document.getElementById('volume').value;
		var multiplier = (bbl * 31)/volume;
		var hopbill = '<strong>Hop Bill</strong><hr>';
		
		for (var i=1; i <= 10; i++) {
			var amt = 'pounds' + i;
			var pounds = document.getElementById(amt).value;
			if (pounds != 0) {
				var type = 'ingredient' + i;
				var typevals = document.getElementById(type).value.split(':');
				var rate = typevals[0];
				var grain = document.getElementById(type).options[document.getElementById(type).selectedIndex].text;
				var grainperc = pounds/grainweight;
				var grainpounds = ((extractweight * grainperc) / (rate * extperc)).toFixed(1);
				grainbill += '<div class=\"billleft\">' + grain + '</div><div class=\"billright\"><strong>' + grainpounds + ' </strong>lbs.</div><div class=\"clear\">&nbsp;</div>';
				totalweight += 1*grainpounds;
			}
		}
		grainbill += '<hr><div class=\"billleft\">total weight</div><div class=\"billright\"><strong>' + totalweight.toFixed(1) + ' </strong>lbs.</div><div class=\"clear\">&nbsp;</div>';
		document.getElementById('totals').innerHTML = grainbill;
		for (var i=1; i <= 10; i++) {
			var oz = 'ounces' + i;
			var ounces = document.getElementById(oz).value;
			if (ounces != 0) {
				var type = 'hop' + i;
				var hoptype = document.getElementById(type).options[document.getElementById(type).selectedIndex].text;
				var mins = 'minutes' + i;
				var minutes = document.getElementById(mins).value;
				var lbhops = Math.floor((ounces * multiplier)/16);
				var ozhops = ((ounces * multiplier)%16).toFixed(2);
				
				hopbill += '<div class=\"billleft\">' + hoptype + ' &nbsp;(' + minutes +' minutes)</div><div class=\"billright\"><strong>' + lbhops + ' </strong>lbs. <strong>' + ozhops + '</strong> oz.</div><div class=\"clear\">&nbsp;</div>';
			}
		} 
		document.getElementById('hoptotals').innerHTML = hopbill;
		document.getElementById('hoptotals').style.display = 'block';
	} else {
		document.getElementById('totals').innerHTML = '';	
		document.getElementById('hoptotals').style.display = 'none';
	}
}

function calculateAlcohol() {
	var alcohol = 0;
	var factor = 135;
	var asg = document.getElementById('og').value;
	var fg = document.getElementById('fg').value;
	for (var i = 1; i <= 10; i++) {
		var alcoholfactor = 'factor' + i;
		var afvalues = document.getElementById(alcoholfactor).value.split(':');
		var og = afvalues[0];
		if (((1*asg) - (1*fg)) <= (1*og)) {
			factor = afvalues[1];
			break;
		}
	}
	alcohol = ((asg - fg) * factor).toFixed(2);
	eff = Math.round(((((asg-1))/((maximum-1)))*100));
	document.getElementById('alcohol').innerHTML = ' &nbsp; Alcohol: ' + alcohol + '% &nbsp; eff. ' + eff + '%';
}