/**
 * Copyright 2010 eliquidcalculator.com - All rights reserved
 */

var jQT = $.jQTouch( {
	icon : 'logo.png',
	statusBar : 'black'
});
var nicotineDensity = 1.01;
var isBaseCalculator = true;
$(function() {
	$('#readybases').bind('change', function(event) {
		setReadyBase()
	});
	$('#readybases').bind('blur', function(event) {
		setReadyBase()
	});

	$('#calculate').bind('click', function(event) {
		calculateRecipe()
	});
	$('#calculate2').bind('click', function(event) {
		calculateRecipe()
	});

	$('#setstartingrecipelink').bind('click', function(event) {
		onSetStartingRecipeClick()
	});
	$('#setstartingrecipelink').bind('click', function(event) {
		onStartingRecipeChanged()
	});
	$('#setstartingbaselink').bind('click', function(event) {
		onStartingBaseClick()
	});
	$('#setstartingbaselink').bind('click', function(event) {
		onBaseChanged()
	});

	$('#basetosetrecipebutton').bind('click', function(event) {
		onRecipeChanged()
	});
	$('#basetosetrecipearrow').bind('click', function(event) {
		onRecipeChanged()
	});
	$('#startrecipetosetbasebutton').bind('click', function(event) {
		onBaseChanged()
	});
	$('#startrecipetosetbasearrow').bind('click', function(event) {
		onBaseChanged()
	});

	$('#bNicotine').bind('keyup', function(event) {
		onBaseChanged()
	});
	$('#bPg').bind('keyup', function(event) {
		onBaseChanged()
	});
	$('#bVg').bind('keyup', function(event) {
		onBaseChanged()
	});
	$('#bWater').bind('keyup', function(event) {
		onBaseChanged()
	});

	$('#rNicotine').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rQuantity').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rPg').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rVg').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rWater').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rAlcohol').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rFlavour1').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rFlavour2').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rFlavour3').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rFlavour4').bind('keyup', function(event) {
		onRecipeChanged()
	});
	$('#rFlavour5').bind('keyup', function(event) {
		onRecipeChanged()
	});

	$('#srNicotine').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srQuantity').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srPg').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srVg').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srWater').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srAlcohol').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srFlavour1').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srFlavour2').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srFlavour3').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srFlavour4').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	$('#srFlavour5').bind('keyup', function(event) {
		onStartingRecipeChanged()
	});
	// $('#bPg').bind('change',function(event){checkBase()});
});
function onSetStartingRecipeClick() {
	isBaseCalculator = false;
	jQuery('#abasebackbutton').remove();
	jQuery('#basebackbutton').remove();
	jQuery(
			'<span id="basebackbutton"><a id="abasebackbutton" class="button back" href="#setstartingrecipe">St. Recipe</a></span>')
			.insertBefore('#basetosetrecipebutton');
}
function onStartingBaseClick() {
	isBaseCalculator = true;
	jQuery('#abasebackbutton').remove();
	jQuery('#basebackbutton').remove();
	jQuery(
			'<span id="basebackbutton"><a id="abasebackbutton" class="button back" href="#">Home</a></span>')
			.insertBefore('#basetosetrecipebutton');
}
function setReadyBase() {
	$readyBase = $('#readybases').val();
	if ($readyBase == 'traditional') {
		$('#bPg').val('50.0');
		$('#bVg').val('40.0');
		$('#bWater').val('10.0');
	} else if ($readyBase == 'velvetcloud') {
		$('#bPg').val('0.0');
		$('#bVg').val('80.0');
		$('#bWater').val('20.0');
	} else if ($readyBase == 'iceblade') {
		$('#bPg').val('95.0');
		$('#bVg').val('0.0');
		$('#bWater').val('5.0');
	} else if ($readyBase == 'lfpg') {
		$('#bPg').val('100.0');
		$('#bVg').val('0.0');
		$('#bWater').val('0.0');
	} else if ($readyBase == 'lfvg') {
		$('#bPg').val('0.0');
		$('#bVg').val('80.0');
		$('#bWater').val('20.0');
	}

	/*
	 * if ($readyBase == 'custom') { //setBaseFieldsReadOnly(false); } else {
	 * //setBaseFieldsReadOnly(true); }
	 */

}
function setBaseFieldsReadOnly($isReadOnly) {
	/*
	 * if ($isReadOnly) { jQuery('#bPg').attr('readonly', 'true');
	 * jQuery('#bVg').attr('readonly', 'true');
	 * jQuery('#bWater').attr('readonly', 'true'); } else {
	 * jQuery('#bPg').attr('readonly', 'false'); jQuery('#bVg').attr('readonly',
	 * 'false'); jQuery('#bWater').attr('readonly', 'false'); }
	 */
}
function onBaseChanged() {
	// Check starting base
	$isBaseError = false;
	$bNicotine = parseFloat($('#bNicotine').val());
	if ($bNicotine > nicotineDensity * 1000) {
		$('#bNicotineError').text(
				'cannot be more than ' + (nicotineDensity * 1000) + ' mg/ml');
		$isBaseError = true;
		$isError = true;
	} else if ($bNicotine <= 0) {
		$('#bNicotineError').text('must be positive');
		$isBaseError = true;
		$isError = true;
	} else if (isNaN($bNicotine)) {
		$('#bNicotineError').text('is not a number');
		$isBaseError = true;
		$isError = true;
	} else {
		$('#bNicotineError').text('');
	}

	$bPg = parseFloat($('#bPg').val());
	if ($bPg < 0) {
		$('#bPgError').text('cannot be negative');
		$isBaseError = true;
		$isError = true;
	} else if ($bPg > 100) {
		$('#bPgError').text('can never be more than 100%');
		$isBaseError = true;
		$isError = true;
	} else if (isNaN($bPg)) {
		$('#bPgError').text('is not a number');
		$isBaseError = true;
		$isError = true;
	} else {
		$('#bPgError').text('');
	}
	$bVg = parseFloat($('#bVg').val());
	if ($bVg < 0) {
		$('#bVgError').text('cannot be negative');
		$isBaseError = true;
		$isError = true;
	} else if ($bVg > 100) {
		$('#bVgError').text('can never be more than 100%');
		$isBaseError = true;
		$isError = true;
	} else if (isNaN($bVg)) {
		$('#bVgError').text('is not a number');
		$isBaseError = true;
		$isError = true;
	} else {
		$('#bVgError').text('');
	}
	$bWater = parseFloat($('#bWater').val());
	if ($bWater < 0) {
		$('#bWaterError').text('cannot be negative');
		$isBaseError = true;
		$isError = true;
	} else if ($bWater > 100) {
		$('#bWaterError').text('can never be more than 100%');
		$isBaseError = true;
		$isError = true;
	} else if (isNaN($bWater)) {
		$('#bWaterError').text('is not a number');
		$isBaseError = true;
		$isError = true;
	} else {
		$('#bWaterError').text('');
	}
	$bTotal = $bWater + $bVg + $bPg;
	if ($bTotal > 100 || $bTotal < 100) {
		$('#bError')
				.text(
						'PG + VG + Water is ' + $bTotal.toFixed(2) + '! It must be 100.00...');
		$isBaseError = true;
		$isError = true;
	}
	if (!$isBaseError) {
		$('#bError').text('');
		jQuery('#basetosetrecipearrow').remove();
		jQuery('#basetosetrecipebutton').remove();
		jQuery(
				'<span id="basetosetrecipebutton"><a class="button slideup" href="#setrecipe">Tg. Recipe</a></span>')
				.appendTo('#setbasetoolbar');
		jQuery(
				'<ul id="basetosetrecipearrow" class="edgetoedge"><li class="arrow"><a href="#setrecipe">Set Target Recipe</a></li></ul>')
				.appendTo('#setbase');
		$('#basetosetrecipebutton').bind('click', function(event) {
			onRecipeChanged()
		});
		$('#basetosetrecipearrow').bind('click', function(event) {
			onRecipeChanged()
		});
	} else {
		jQuery('#basetosetrecipearrow').remove();
		jQuery('#basetosetrecipebutton').remove();
	}
}
function onRecipeChanged() {
	$bNicotine = parseFloat($('#bNicotine').val());
	$bPg = parseFloat($('#bPg').val());
	$bVg = parseFloat($('#bVg').val());
	$bWater = parseFloat($('#bWater').val());
	$rNicotine = parseFloat($('#rNicotine').val());
	$rQuantity = parseFloat($('#rQuantity').val());
	$rPg = parseFloat($('#rPg').val());
	$rVg = parseFloat($('#rVg').val());
	$rWater = parseFloat($('#rWater').val());
	$rAlcohol = parseFloat($('#rAlcohol').val());
	$rFlavour1 = parseFloat($('#rFlavour1').val());
	$rFlavour2 = parseFloat($('#rFlavour2').val());
	$rFlavour3 = parseFloat($('#rFlavour3').val());
	$rFlavour4 = parseFloat($('#rFlavour4').val());
	$rFlavour5 = parseFloat($('#rFlavour5').val());
	$rTotal = $rVg + $rPg + $rWater + $rAlcohol + $rFlavour1 + $rFlavour2
			+ $rFlavour3 + $rFlavour4 + $rFlavour5;
	$isRecipeError = false;
	$isError = false;

	// Check recipe
	if (isBaseCalculator) {
		if ($rNicotine > $bNicotine) {
			$('#rNicotineError').text(
					'cannot be more than ' + $bNicotine + ' mg/ml');
			$isRecipeError = true;
			$isError = true;
		} else if ($rNicotine == 0 && $bNicotine > 0) {
			$('#rNicotineError').text('cannot be 0 mg/ml');
			$isRecipeError = true;
			$isError = true;
		} else if ($rNicotine < 0) {
			$('#rNicotineError').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rNicotine)) {
			$('#rNicotineError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rNicotineError').text('');
		}

		$baseRatio = 1;
		$isBaseCompatible = true;
		if ($bNicotine != 0) {
			$baseRatio = $bNicotine / $rNicotine;
		} else {
			if ($rPg != 0 && $bPg / $rPg > 1)
				$baseRatio = $bPg / $rPg;
			else if ($rPg != 0 && $bPg / $rPg <= 1)
				$baseRatio = 1;
			else if ($rPg == 0 && $bPg > 0)
				$isBaseCompatible = false;
			if ($rVg != 0 && $bVg / $rVg > $baseRatio)
				$baseRatio = $bVg / $rVg;
			else if ($rVg == 0 && $bVg > 0)
				$isBaseCompatible = false;
			if ($rWater != 0 && $bWater / $rWater > $baseRatio)
				$baseRatio = $bWater / $rWater;
			else if ($rWater == 0 && $bWater > 0)
				$isBaseCompatible = false;
		}
		$rQuantity = parseFloat($('#rQuantity').val());
		if ($rQuantity <= 0) {
			$('#rQuantityError').text('must be positive');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rQuantity)) {
			$('#rQuantityError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rQuantityError').text('');
		}
		if ($bNicotine != 0 && $rPg < $bPg / $baseRatio) {
			$('#rPgError').text(
					'cannot be less than ' + ($bPg / $baseRatio) + '%');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rPg)) {
			$('#rPgError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rPgError').text('');
		}
		if ($bNicotine != 0 && $rVg < $bVg / $baseRatio) {
			$('#rVgError').text(
					'cannot be less than ' + ($bVg / $baseRatio) + '%');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rVg)) {
			$('#rVgError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rVgError').text('');
		}
		if ($bNicotine != 0 && $rWater < $bWater / $baseRatio) {
			$('#rWaterError').text(
					'cannot be less than ' + $bWater / $baseRatio + '%');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rWater)) {
			$('#rWaterError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rWaterError').text('');
		}
		$rAlcohol = parseFloat($('#rAlcohol').val());
		if ($rAlcohol < 0) {
			$('#rAlcoholError').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rAlcohol)) {
			$('#rAlcoholError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rAlcoholError').text('');
		}
		$rFlavour1 = parseFloat($('#rFlavour1').val());
		if ($rFlavour1 < 0) {
			$('#rFlavour1Error').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour1)) {
			$('#rFlavour1Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour1Error').text('');
		}
		$rFlavour2 = parseFloat($('#rFlavour2').val());
		if ($rFlavour2 < 0) {
			$('#rFlavour2Error').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour2)) {
			$('#rFlavour2Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour2Error').text('');
		}
		$rFlavour3 = parseFloat($('#rFlavour3').val());
		if ($rFlavour3 < 0) {
			$('#rFlavour3Error').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour3)) {
			$('#rFlavour3Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour3Error').text('');
		}
		$rFlavour4 = parseFloat($('#rFlavour4').val());
		if ($rFlavour4 < 0) {
			$('#rFlavour4Error').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour4)) {
			$('#rFlavour4Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour4Error').text('');
		}
		$rFlavour5 = parseFloat($('#rFlavour5').val());
		if ($rFlavour5 < 0) {
			$('#rFlavour5Error').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour5)) {
			$('#rFlavour5Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour5Error').text('');
		}
	} else {
		$srNicotine = parseFloat($('#srNicotine').val());
		$srVg = parseFloat($('#srVg').val());
		$srWater = parseFloat($('#srWater').val());
		$srQuantity = parseFloat($('#srQuantity').val());
		$srPg = parseFloat($('#srPg').val());
		$srAlcohol = parseFloat($('#srAlcohol').val());
		$srFlavour1 = parseFloat($('#srFlavour1').val());
		$srFlavour2 = parseFloat($('#srFlavour2').val());
		$srFlavour3 = parseFloat($('#srFlavour3').val());
		$srFlavour4 = parseFloat($('#srFlavour4').val());
		$srFlavour5 = parseFloat($('#srFlavour5').val());

		if (!($srNicotine < $bNicotine)
				&& $rNicotine > Math.max($bNicotine, $srNicotine)) {
			$('#rNicotineError')
					.text(
							'cannot be more than ' + Math.max($bNicotine,
									$srNicotine) + ' mg/ml');
			$isRecipeError = true;
			$isError = true;
		} else if ($rNicotine >= $bNicotine && $srNicotine < $bNicotine) {
			$('#rNicotineError').text(
					'must be less than ' + $bNicotine + ' mg/ml');
			$isRecipeError = true;
			$isError = true;
		} else if ($rNicotine == 0 && $bNicotine > 0 && $srNicotine > 0) {
			$('#rNicotineError').text('cannot be 0 mg/ml');
			$isRecipeError = true;
			$isError = true;
		} else if ($rNicotine < 0) {
			$('#rNicotineError').text('cannot be negative');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rNicotine)) {
			$('#rNicotineError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rNicotineError').text('');
		}
		$minQuantityForNic = $srQuantity;
		if (Math.max($bNicotine, $srNicotine) == 0)
			$minQuantityForNic = $srQuantity;
		else if ($rNicotine != 0 && $rNicotine <= $srNicotine)
			$minQuantityForNic = $srNicotine / $rNicotine * $srQuantity;
		else if ($rNicotine > $srNicotine)
			$minQuantityForNic = ((($srNicotine - $rNicotine) * $srQuantity) / ($rNicotine - $bNicotine))
					+ $srQuantity;
		// TODO: calculate minQuantity for other ingredients.

		/*
		 * $baseRatio = 1; $isBaseCompatible = true; if ($bNicotine != 0) {
		 * $baseRatio = $bNicotine / $rNicotine; } else { if ($rPg != 0 && $bPg /
		 * $rPg > 1) $baseRatio = $bPg / $rPg; else if ($rPg != 0 && $bPg / $rPg <=
		 * 1) $baseRatio = 1; else if ($rPg == 0 && $bPg > 0) $isBaseCompatible =
		 * false; if ($rVg != 0 && $bVg / $rVg > $baseRatio) $baseRatio = $bVg /
		 * $rVg; else if ($rVg == 0 && $bVg > 0) $isBaseCompatible = false; if
		 * ($rWater != 0 && $bWater / $rWater > $baseRatio) $baseRatio = $bWater /
		 * $rWater; else if ($rWater == 0 && $bWater > 0) $isBaseCompatible =
		 * false; }
		 */
		// TODO: $minQuantity = min($srQuantity, Math.abs(x));
		if ($rQuantity < $minQuantityForNic) {
			$('#rQuantityError').text(
					'cannot be less than ' + $minQuantityForNic + ' ml for the given target nicotine');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rQuantity)) {
			$('#rQuantityError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rQuantityError').text('');
		}
		$fBase = (($rNicotine * $rQuantity) - ($srNicotine * $srQuantity))
		/ $bNicotine;
		$rPgMin = (($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bPg) + ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srPg)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rPgMax = $rPgMin + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rPg < $rPgMin) {
			$('#rPgError').text(
					'must be min ' + $rPgMin + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rPg > $rPgMax) {
			$('#rPgError').text(
					'can be max ' + $rPgMax + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rPg)) {
			$('#rPgError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rPgError').text('');
		}
		$rVgMin = (($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bVg) + ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srVg)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rVgMax = $rVgMin + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rVg < $rVgMin) {
			$('#rVgError').text(
					'must be min ' + $rVgMin + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rVg > $rVgMax) {
			$('#rVgError').text(
					'can be max ' + $rVgMax + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rVg)) {
			$('#rVgError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rVgError').text('');
		}
		$rWaterMin = (($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bWater) + ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srWater)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rWaterMax = $rWaterMin + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rWater < $rWaterMin) {
			$('#rWaterError').text(
					'must be min ' + $rWaterMin + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rWater > $rWaterMax) {
			$('#rWaterError').text(
					'can be max ' + $rWaterMax + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rWater)) {
			$('#rWaterError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rWaterError').text('');
		}
		$rAlcoholMin = (($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srAlcohol)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rAlcoholMax = $rAlcoholMin + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rAlcohol < $rAlcoholMin) {
			$('#rAlcoholError').text(
					'must be min ' + $rAlcoholMin + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rAlcohol > $rAlcoholMax) {
			$('#rAlcoholError').text(
					'can be max ' + $rAlcoholMax + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rAlcohol)) {
			$('#rAlcoholError').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rAlcoholError').text('');
		}
		$rFlavour1Min = (($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour1)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rFlavour1Max = $rFlavour1Min + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rFlavour1 < $rFlavour1Min) {
			$('#rFlavour1Error').text(
					'must be min ' + $rFlavour1Min + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rFlavour1 > $rFlavour1Max) {
			$('#rFlavour1Error').text(
					'can be max ' + $rFlavour1Max + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour1)) {
			$('#rFlavour1Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour1Error').text('');
		}
		$rFlavour2Min = (($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour2)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rFlavour2Max = $rFlavour2Min + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rFlavour2 < $rFlavour2Min) {
			$('#rFlavour2Error').text(
					'must be min ' + $rFlavour2Min + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rFlavour2 > $rFlavour2Max) {
			$('#rFlavour2Error').text(
					'can be max ' + $rFlavour2Max + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour2)) {
			$('#rFlavour2Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour2Error').text('');
		}
		$rFlavour3Min = (($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour3)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rFlavour3Max = $rFlavour3Min + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rFlavour3 < $rFlavour3Min) {
			$('#rFlavour3Error').text(
					'must be min ' + $rFlavour3Min + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rFlavour3 > $rFlavour3Max) {
			$('#rFlavour3Error').text(
					'can be max ' + $rFlavour3Max + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour3)) {
			$('#rFlavour3Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour3Error').text('');
		}
		$rFlavour4Min = (($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour4)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rFlavour4Max = $rFlavour4Min + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rFlavour4 < $rFlavour4Min) {
			$('#rFlavour4Error').text(
					'must be min ' + $rFlavour4Min + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rFlavour4 > $rFlavour4Max) {
			$('#rFlavour4Error').text(
					'can be max ' + $rFlavour4Max + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour4)) {
			$('#rFlavour4Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour4Error').text('');
		}
		$rFlavour5Min = (($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour5)) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100));
		$rFlavour5Max = $rFlavour5Min + (($rQuantity - $srQuantity - $fBase) / ($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100)) * 100);
		if ($rFlavour5 < $rFlavour5Min) {
			$('#rFlavour5Error').text(
					'must be min ' + $rFlavour5Min + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if ($rFlavour5 > $rFlavour5Max) {
			$('#rFlavour5Error').text(
					'can be max ' + $rFlavour5Max + '% for the given target quantity');
			$isRecipeError = true;
			$isError = true;
		} else if (isNaN($rFlavour5)) {
			$('#rFlavour5Error').text('is not a number');
			$isRecipeError = true;
			$isError = true;
		} else {
			$('#rFlavour5Error').text('');
		}
	}
	if ($rTotal > 100 || $rTotal < 100) {
		$('#rError')
				.text(
						'PG + VG + Water + Alcohol + Flavours is ' + $rTotal
								.toFixed(2) + '! It must be 100.00...');
		$isRecipeError = true;
		$isError = true;
	}
	if (!$isRecipeError) {
		$('#rError').text('');
		jQuery('#recipetosetdropsarrow').remove();
		jQuery('#recipetosetdropsbutton').remove();
		jQuery(
				'<span id="recipetosetdropsbutton"><a class="button slideup" href="#setdrops">Drops</a></span>')
				.appendTo('#setrecipetoolbar');
		jQuery(
				'<ul id="recipetosetdropsarrow" class="edgetoedge"><li class="arrow"><a href="#setdrops">Set Drops</a></li></ul>')
				.appendTo('#setrecipe');
	} else {
		jQuery('#recipetosetdropsarrow').remove();
		jQuery('#recipetosetdropsbutton').remove();
	}
}
function onStartingRecipeChanged() {
	// Check recipe
	$isStartingRecipeError = false;
	$srNicotine = parseFloat($('#srNicotine').val());
	if ($srNicotine > nicotineDensity * 1000) {
		$('#srNicotineError').text(
				'cannot be more than ' + (nicotineDensity * 1000) + ' mg/ml');
		$isStartingRecipeError = true;
		$isError = true;
	} else if ($srNicotine < 0) {
		$('#srNicotineError').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srNicotine)) {
		$('#srNicotineError').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srNicotineError').text('');
	}

	$srVg = parseFloat($('#srVg').val());
	$srWater = parseFloat($('#srWater').val());
	$srQuantity = parseFloat($('#srQuantity').val());
	if ($srQuantity <= 0) {
		$('#srQuantityError').text('must be positive');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srQuantity)) {
		$('#srQuantityError').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srQuantityError').text('');
	}
	$srPg = parseFloat($('#srPg').val());
	if ($srPg < 0) {
		$('#srPgError').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srPg)) {
		$('#srPgError').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srPgError').text('');
	}
	if ($srVg < 0) {
		$('#srVgError').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srVg)) {
		$('#srVgError').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srVgError').text('');
	}
	if ($srWater < 0) {
		$('#srWaterError').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srWater)) {
		$('#srWaterError').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srWaterError').text('');
	}
	$srAlcohol = parseFloat($('#srAlcohol').val());
	if ($srAlcohol < 0) {
		$('#srAlcoholError').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srAlcohol)) {
		$('#srAlcoholError').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srAlcoholError').text('');
	}
	$srFlavour1 = parseFloat($('#srFlavour1').val());
	if ($srFlavour1 < 0) {
		$('#srFlavour1Error').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srFlavour1)) {
		$('#srFlavour1Error').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srFlavour1Error').text('');
	}
	$srFlavour2 = parseFloat($('#srFlavour2').val());
	if ($srFlavour2 < 0) {
		$('#srFlavour2Error').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srFlavour2)) {
		$('#srFlavour2Error').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srFlavour2Error').text('');
	}
	$srFlavour3 = parseFloat($('#srFlavour3').val());
	if ($srFlavour3 < 0) {
		$('#srFlavour3Error').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srFlavour3)) {
		$('#srFlavour3Error').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srFlavour3Error').text('');
	}
	$srFlavour4 = parseFloat($('#srFlavour4').val());
	if ($srFlavour4 < 0) {
		$('#srFlavour4Error').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srFlavour4)) {
		$('#srFlavour4Error').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srFlavour4Error').text('');
	}
	$srFlavour5 = parseFloat($('#srFlavour5').val());
	if ($srFlavour5 < 0) {
		$('#srFlavour5Error').text('cannot be negative');
		$isStartingRecipeError = true;
		$isError = true;
	} else if (isNaN($srFlavour5)) {
		$('#srFlavour5Error').text('is not a number');
		$isStartingRecipeError = true;
		$isError = true;
	} else {
		$('#srFlavour5Error').text('');
	}
	$srTotal = $srVg + $srPg + $srWater + $srAlcohol + $srFlavour1
			+ $srFlavour2 + $srFlavour3 + $srFlavour4 + $srFlavour5;
	if ($srTotal > 100 || $srTotal < 100) {
		$('#srError').text(
				'PG + VG + Water + Alcohol + Flavours is ' + $srTotal
						.toFixed(2) + '! It must be 100.00...');
		$isStartingRecipeError = true;
		$isError = true;
	}
	if (!$isStartingRecipeError) {
		$('#srError').text('');
		jQuery('#startrecipetosetbasearrow').remove();
		jQuery('#startrecipetosetbasebutton').remove();
		jQuery(
				'<span id="startrecipetosetbasebutton"><a class="button slideup" href="#setbase">Base</a></span>')
				.appendTo('#setstartingrecipetoolbar');
		jQuery(
				'<ul id="startrecipetosetbasearrow" class="edgetoedge"><li class="arrow"><a href="#setbase">Set Base</a></li></ul>')
				.appendTo('#setstartingrecipe');
		$('#startrecipetosetbasebutton').bind('click', function(event) {
			onBaseChanged()
		});
		$('#startrecipetosetbasearrow').bind('click', function(event) {
			onBaseChanged()
		});
	} else {
		jQuery('#startrecipetosetbasearrow').remove();
		jQuery('#startrecipetosetbasebutton').remove();
	}
}
function calculateRecipe() {
	$isError = false;

	// Get starting recipe
	if (!isBaseCalculator) {
		$srNicotine = parseFloat($('#srNicotine').val());
		$srVg = parseFloat($('#srVg').val());
		$srWater = parseFloat($('#srWater').val());
		$srQuantity = parseFloat($('#srQuantity').val());
		$srPg = parseFloat($('#srPg').val());
		$srAlcohol = parseFloat($('#srAlcohol').val());
		$srFlavour1 = parseFloat($('#srFlavour1').val());
		$srFlavour2 = parseFloat($('#srFlavour2').val());
		$srFlavour3 = parseFloat($('#srFlavour3').val());
		$srFlavour4 = parseFloat($('#srFlavour4').val());
		$srFlavour5 = parseFloat($('#srFlavour5').val());
	}

	// Get starting base
	$bNicotine = parseFloat($('#bNicotine').val());
	$bPg = parseFloat($('#bPg').val());
	$bVg = parseFloat($('#bVg').val());
	$bWater = parseFloat($('#bWater').val());

	// Get recipe
	$rNicotine = parseFloat($('#rNicotine').val());
	$rPg = parseFloat($('#rPg').val());
	$rVg = parseFloat($('#rVg').val());
	$rWater = parseFloat($('#rWater').val());
	$rQuantity = parseFloat($('#rQuantity').val());
	$rAlcohol = parseFloat($('#rAlcohol').val());
	$rFlavour1 = parseFloat($('#rFlavour1').val());
	$rFlavour2 = parseFloat($('#rFlavour2').val());
	$rFlavour3 = parseFloat($('#rFlavour3').val());
	$rFlavour4 = parseFloat($('#rFlavour4').val());
	$rFlavour5 = parseFloat($('#rFlavour5').val());
	$rTotal = $rVg + $rPg + $rWater + $rAlcohol + $rFlavour1 + $rFlavour2
			+ $rFlavour3 + $rFlavour4 + $rFlavour5;

	// Check drops
	$dPg = parseFloat($('#dPg').val());
	if ($dPg < 1) {
		$dPg = 1;
		$('#dPg').val($dPg.toFixed(0));
	}
	$dVg = parseFloat($('#dVg').val());
	if ($dVg < 1) {
		$dVg = 1;
		$('#dVg').val($dVg.toFixed(0));
	}
	$dWater = parseFloat($('#dWater').val());
	if ($dWater < 1) {
		$dWater = 1;
		$('#dWater').val($dWater.toFixed(0));
	}
	$dAlcohol = parseFloat($('#dAlcohol').val());
	if ($dAlcohol < 1) {
		$dAlcohol = 1;
		$('#dAlcohol').val($dAlcohol.toFixed(0));
	}
	$dFlavours = parseFloat($('#dFlavours').val());
	if ($dFlavours < 1) {
		$dFlavours = 1;
		$('#dFlavours').val($dFlavours.toFixed(0));
	}
	$roundDrops = parseInt($('#rounddrops').val());

	if (isBaseCalculator) {
		jQuery('#fsrQuantityListItem').remove();
		// Calculate final quantities
		$baseRatio = 1;
		$isBaseCompatible = true;
		if ($bNicotine != 0) {
			$baseRatio = $bNicotine / $rNicotine;
		} else {
			if ($rPg != 0 && $bPg / $rPg > 1)
				$baseRatio = $bPg / $rPg;
			else if ($rPg != 0 && $bPg / $rPg <= 1)
				$baseRatio = 1;
			else if ($rPg == 0 && $bPg > 0)
				$isBaseCompatible = false;
			if ($rVg != 0 && $bVg / $rVg > $baseRatio)
				$baseRatio = $bVg / $rVg;
			else if ($rVg == 0 && $bVg > 0)
				$isBaseCompatible = false;
			if ($rWater != 0 && $bWater / $rWater > $baseRatio)
				$baseRatio = $bWater / $rWater;
			else if ($rWater == 0 && $bWater > 0)
				$isBaseCompatible = false;
		}
		if ($isBaseCompatible)
			$fBase = $rQuantity / $baseRatio;
		else
			$fBase = 0;
		$fdBase = (($dPg * $bPg) + ($dVg * $bVg) + ($dWater * $bWater)) / 100
				* $fBase;
		$fPg = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rPg) - ($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bPg)) / 100;
		$fdPg = $dPg * $fPg;
		$fVg = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rVg) - ($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bVg)) / 100;
		$fdVg = $dVg * $fVg;
		$fWater = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rWater) - ($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bWater)) / 100;
		$fdWater = $dWater * $fWater;
		$fAlcohol = $rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rAlcohol / 100;
		$fdAlcohol = $dAlcohol * $fAlcohol;
		$fFlavour1 = $rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour1 / 100;
		$fdFlavour1 = $dFlavours * $fFlavour1;
		$fFlavour2 = $rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour2 / 100;
		$fdFlavour2 = $dFlavours * $fFlavour2;
		$fFlavour3 = $rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour3 / 100;
		$fdFlavour3 = $dFlavours * $fFlavour3;
		$fFlavour4 = $rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour4 / 100;
		$fdFlavour4 = $dFlavours * $fFlavour4;
		$fFlavour5 = $rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour5 / 100;
		$fdFlavour5 = $dFlavours * $fFlavour5;
	} else {
		jQuery('#fsrQuantityListItem').remove();
		jQuery('<li id="fsrQuantityListItem">Starting recipe<input type="text" placeholder="St. Recipe" name="fsrQuantity" id="fsrQuantity" autocapitalize="off" autocorrect="off" autocomplete="off" readonly="true" /></li>').insertBefore('#fBaseListItem');
		$fBase = (($rNicotine * $rQuantity) - ($srNicotine * $srQuantity))
					/ $bNicotine;
		$fdBase = (($dPg * $bPg) + ($dVg * $bVg) + ($dWater * $bWater)) / 100
				* $fBase;
		$fPg = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rPg) - ($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bPg) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srPg)) / 100;
		$fdPg = $dPg * $fPg;
		$fVg = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rVg) - ($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bVg) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srVg)) / 100;
		$fdVg = $dVg * $fVg;
		$fWater = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rWater) - ($fBase * ((100 - $bNicotine / nicotineDensity / 10) / 100) * $bWater) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srWater)) / 100;
		$fdWater = $dWater * $fWater;
		$fAlcohol = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rAlcohol) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srAlcohol)) / 100;
		$fdAlcohol = $dAlcohol * $fAlcohol;
		$fFlavour1 = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour1) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour1)) / 100;
		$fdFlavour1 = $dFlavours * $fFlavour1;
		$fFlavour2 = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour2) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour2)) / 100;
		$fdFlavour2 = $dFlavours * $fFlavour2;
		$fFlavour3 = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour3) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour3)) / 100;
		$fdFlavour3 = $dFlavours * $fFlavour3;
		$fFlavour4 = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour4) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour4)) / 100;
		$fdFlavour4 = $dFlavours * $fFlavour4;
		$fFlavour5 = (($rQuantity * ((100 - $rNicotine / nicotineDensity / 10) / 100) * $rFlavour5) - ($srQuantity * ((100 - $srNicotine / nicotineDensity / 10) / 100) * $srFlavour5)) / 100;
		$fdFlavour5 = $dFlavours * $fFlavour5;
		
		
		/*
		 * else if ($rNicotine != 0 && $rNicotine <= $srNicotine) $baseQuantity =
		 * $srNicotine / $rNicotine * $srQuantity; else if ($srNicotine != 0 &&
		 * $rNicotine > $srNicotine) $baseQuantity = ((($srNicotine -
		 * $rNicotine) * $srQuantity) / ($rNicotine - $bNicotine)) +
		 * $srQuantity;
		 */
	}
	// If there is any error, clear the previous results, show error message
	// after "calculate button" and return.
	// Else clear error message
	if ($isError) {
		if ($isBaseError) {
			$('#fBaseError').text('Invalid starting ');
			$('#fBaseErrorLink').text('base');
		} else if ($isRecipeError) {
			$('#fRecipeError').text('Invalid ');
			$('#fRecipeErrorLink').text('recipe');
		}
		$('#fBase').val('');
		$('#fPg').val('');
		$('#fVg').val('');
		$('#fWater').val('');
		$('#fAlcohol').val('');
		$('#fFlavour1').val('');
		$('#fFlavour2').val('');
		$('#fFlavour3').val('');
		$('#fFlavour4').val('');
		$('#fFlavour5').val('');
		return false;
	} else {
		$('#fBaseError').text('');
		$('#fBaseErrorLink').text('');
		$('#fRecipeError').text('');
		$('#fRecipeErrorLink').text('');
	}

	// Show results
	if (!isBaseCalculator) $('#fsrQuantity').val($srQuantity.toFixed(3) + ' ml');
	$('#fBase').val(
			$fBase.toFixed(3) + ' ml  -  ' + $fdBase.toFixed($roundDrops) + ' drops');
	$('#fPg').val($fPg.toFixed(3) + ' ml  -  ' + $fdPg.toFixed($roundDrops) + ' drops');
	$('#fVg').val($fVg.toFixed(3) + ' ml  -  ' + $fdVg.toFixed($roundDrops) + ' drops');
	$('#fWater').val(
			$fWater.toFixed(3) + ' ml  -  ' + $fdWater.toFixed($roundDrops) + ' drops');
	$('#fAlcohol').val(
			$fAlcohol.toFixed(3) + ' ml  -  ' + $fdAlcohol.toFixed($roundDrops)
					+ ' drops');
	$('#fFlavour1').val(
			$fFlavour1.toFixed(3) + ' ml  -  ' + $fdFlavour1.toFixed($roundDrops)
					+ ' drops');
	$('#fFlavour2').val(
			$fFlavour2.toFixed(3) + ' ml  -  ' + $fdFlavour2.toFixed($roundDrops)
					+ ' drops');
	$('#fFlavour3').val(
			$fFlavour3.toFixed(3) + ' ml  -  ' + $fdFlavour3.toFixed($roundDrops)
					+ ' drops');
	$('#fFlavour4').val(
			$fFlavour4.toFixed(3) + ' ml  -  ' + $fdFlavour4.toFixed($roundDrops)
					+ ' drops');
	$('#fFlavour5').val(
			$fFlavour5.toFixed(3) + ' ml  -  ' + $fdFlavour5.toFixed($roundDrops)
					+ ' drops');
	return false;
}

function checkBase() {

	// Check starting base
	$isBaseError = false;
	$bNicotine = parseFloat($('#bNicotine').val());
	if ($bNicotine > nicotineDensity * 1000) {
		$bNicotine = nicotineDensity * 1000;
		$('#bNicotine').val($bNicotine.toFixed(0));
	} else if ($bNicotine < 0) {
		$bNicotine = 0;
		$('#bNicotine').val($bNicotine.toFixed(0));
	}
	$bPg = parseFloat($('#bPg').val());
	if ($bPg < 0) {
		$bPg = 0;
		$('#bPg').val($bPg.toFixed(1));
	}
	$bVg = parseFloat($('#bVg').val());
	if ($bVg < 0) {
		$bVg = 0;
		$('#bVg').val($bVg.toFixed(1));
	}
	$bWater = parseFloat($('#bWater').val());
	if ($bWater < 0) {
		$bWater = 0;
		$('#bWater').val($bWater.toFixed(1));
	}
	$bTotal = $bWater + $bVg + $bPg;
	if ($bTotal > 100 || $bTotal < 100) {
		$('#bError')
				.text(
						'PG + VG + Water is ' + $bTotal.toFixed(2) + '! It must be 100.00...');
		$isBaseError = true;
		$isError = true;
	}
	if (!$isBaseError) {
		$('#bError').text('');
	}
}

