/*------------------------------------------------------------------------*/
/*  SHOWS THE RIGHT CALCULATOR BASED ON THE DROP DOWNS / HIDES ALL OTHERS
/*------------------------------------------------------------------------*/
$('.select-option').change(function() {
	$('input.numeric').val('');
	$('input.total').val('');
	
	var fuelQuantityOpt = $('#select-fuel-quantity').val(); /* get the value of the fuel quantity drop down */
	var fuelAdditiveOpt = $('#select-additive-rate').val(); /* get the value of the fuel additive drop down */
	
	$('.calc').removeClass('show-option');
	$('.calc').addClass('hide-option'); /* Hides all of the calculator options ready for one to be chosen and shown */
	
	$('#ptb-unit-choice').removeClass('show-option');
	$('#ptb-unit-choice').addClass('hide-option'); /* Hides the PTB unit choice option in case it is not needed */
	
	$("#select-unit-ptb option[value='BBLS']").attr('selected', 'selected'); /* resets the PTB unit select to barrels */
	
	/*--------------------------------------------------------------------------*/
	/* Based on the value of the selected options, show the relevant calculator */
	/*--------------------------------------------------------------------------*/
	if (fuelQuantityOpt == 'Mertic Tonnes' && fuelAdditiveOpt == 'PPMV') {
		$('#calc1').removeClass('hide-option');
		$('#calc1').addClass('show-option');
	}
	else if (fuelQuantityOpt == 'Metres Cubed' && fuelAdditiveOpt == 'PPMV') {
		$('#calc2').removeClass('hide-option');
		$('#calc2').addClass('show-option');
	}
	else if (fuelQuantityOpt == 'Mertic Tonnes' && fuelAdditiveOpt == 'PPMW') {
		$('#calc3').removeClass('hide-option');
		$('#calc3').addClass('show-option');
	}
	else if (fuelQuantityOpt == 'Metres Cubed' && fuelAdditiveOpt == 'PPMW') {
		$('#calc4').removeClass('hide-option');
		$('#calc4').addClass('show-option');
	}
	else if (fuelQuantityOpt == 'Barrels' && fuelAdditiveOpt == 'PPMV') {
		$('#calc5').removeClass('hide-option');
		$('#calc5').addClass('show-option');
	}
	else if (fuelQuantityOpt == 'USG' && fuelAdditiveOpt == 'PPMV') {
		$('#calc8').removeClass('hide-option');
		$('#calc8').addClass('show-option');
	}
	else if (fuelQuantityOpt == 'Barrels' && fuelAdditiveOpt == 'PPMW') {
		$('#calc9').removeClass('hide-option');
		$('#calc9').addClass('show-option');
	}
	else if (fuelQuantityOpt == 'USG' && fuelAdditiveOpt == 'PPMW') {
		$('#calc11').removeClass('hide-option');
		$('#calc11').addClass('show-option');
	}
	/* BLANK OPTIONS */
	else if (fuelQuantityOpt == 'Mertic Tonnes' && fuelAdditiveOpt == 'PTB') {
		$('#calc1').removeClass('hide-option');
		$('#calc1').addClass('show-option');
		$("#select-fuel-quantity option[value='Mertic Tonnes']").attr('selected', 'selected');
		$("#select-additive-rate option[value='PPMV']").attr('selected', 'selected');
	}
	else if (fuelQuantityOpt == 'Metres Cubed' && fuelAdditiveOpt == 'PTB') {
		$('#calc1').removeClass('hide-option');
		$('#calc1').addClass('show-option');
		$("#select-fuel-quantity option[value='Mertic Tonnes']").attr('selected', 'selected');
		$("#select-additive-rate option[value='PPMV']").attr('selected', 'selected');
	}
	
	/*  PTB OPTIONS  */
	if (fuelQuantityOpt == 'Barrels' && fuelAdditiveOpt == 'PTB') {
		$('#calc10').removeClass('hide-option');
		$('#calc10').addClass('show-option'); /* Show the default option without additive density */
		
		$('#ptb-unit-choice').removeClass('hide-option');
		$('#ptb-unit-choice').addClass('show-option'); /* Show the unit choice for the PTB option */
	}
	else if (fuelQuantityOpt == 'USG' && fuelAdditiveOpt == 'PTB') {
		$('#calc12').removeClass('hide-option');
		$('#calc12').addClass('show-option'); /* Show the default option without additive density */
		
		$('#ptb-unit-choice').removeClass('hide-option');
		$('#ptb-unit-choice').addClass('show-option'); /* Show the unit choice for the PTB option */
	}
});

$('#select-unit-ptb').change(function() {
	var fuelQuantityOpt = $('#select-fuel-quantity').val();
	var fuelAdditiveOpt = $('#select-additive-rate').val();
	var unitChoice = $(this).val();
	
	if (fuelQuantityOpt == 'Barrels' && fuelAdditiveOpt == 'PTB') {
		if (unitChoice == 'USG') {
			$('#calc10').removeClass('show-option');
			$('#calc10').addClass('hide-option');
			
			$('#calc6').removeClass('hide-option');
			$('#calc6').addClass('show-option');
		}
		else if (unitChoice == 'LBS') {
			$('#calc6').removeClass('show-option');
			$('#calc6').addClass('hide-option');
			
			$('#calc10').removeClass('hide-option');
			$('#calc10').addClass('show-option');
		}
	}	
	else if (fuelQuantityOpt == 'USG' && fuelAdditiveOpt == 'PTB') {
		if (unitChoice == 'USG') {
			$('#calc12').removeClass('show-option');
			$('#calc12').addClass('hide-option');
			
			$('#calc7').removeClass('hide-option');
			$('#calc7').addClass('show-option');
		}
		else if (unitChoice == 'LBS') {
			$('#calc7').removeClass('show-option');
			$('#calc7').addClass('hide-option');
			
			$('#calc12').removeClass('hide-option');
			$('#calc12').addClass('show-option');
		}
	}
});

/*------------------------------------------------------------------------*/
/*  REMOVES / ADDS THE PTB OPTION DEPENDING ON FUEL QUANTITY CHOSEN
/*------------------------------------------------------------------------*/
$('#select-fuel-quantity').change(function() {
	if ($(this).val() == 'Barrels' || $(this).val() == 'USG')
	{
		if($("#select-additive-rate option[value='PTB']").length)
		{
			//DO NOT ADD THE PTB OPTION
		}
		else
		{
			$('#select-additive-rate').append(
				$('<option></option>').
				attr('value','PTB').
				text('PTB')
			);
		}
	}
	else
	{
		$("#select-additive-rate option[value='PTB']").remove();
	}
});

/*------------------------------------------------------------------------*/
/*  STOPS THE USER FROM TYPING ANYTHING INTO THE TOTAL
/*------------------------------------------------------------------------*/
$('input.total').keyup(function() {
	this.value = '';
});

/*------------------------------------------------------------------------*/
/*  FORCES ALL FIELDS TO BE NUMERIC ONLY
/*------------------------------------------------------------------------*/
$('input.numeric').numeric(null, numberInvalid);

function numberInvalid()
{
	alert("'" + $(this).val() + "' is not a valid number");
	$(this).val('');
}

/*------------------------------------------------------------------------*/
/*  CLEARS ALL FIELDS
/*------------------------------------------------------------------------*/
$('a.clear-calc').click(function() {
	$('input').val('');
});

/*------------------------------------------------------------------------*/
/*  CLEARS ALL FIELDS
/*------------------------------------------------------------------------*/
$('input.numeric').keyup(function() {
	$('input.total').val('');
});

/*--------------------------------------------------------------*/
/*  CALCULATOR 1
/*--------------------------------------------------------------*/

$('#submit1').click(function() {
	var fuelQuantity = $('#fuel-quantity1').val();
	var ppmv = $('#ppmv1').val();
	var fuelDensity = $('#fuel-density1').val();
	
	if (fuelQuantity == '' || ppmv == '' || fuelDensity == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * ppmv) / (fuelDensity * 1000));
		$('#total1').val(total);
	}
});

/*--------------------------------------------------------------*/
/*  CALCULATOR 2  */
/*--------------------------------------------------------------*/

$('#submit2').click(function() {
	var fuelQuantity = $('#fuel-quantity2').val();
	var ppmv = $('#ppmv2').val();
	
	if (fuelQuantity == '' || ppmv == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * ppmv) / 1000);
		$('#total2').val(total);
	}
});

/*--------------------------------------------------------------*/
/*  CALCULATOR 3  */
/*--------------------------------------------------------------*/

$('#submit3').click(function() {
	var fuelQuantity = $('#fuel-quantity3').val();
	var ppmw = $('#ppmw3').val();
	
	if (fuelQuantity == '' || ppmw == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * ppmw) / 1000);
		$('#total3').val(total);
	}
});

/*--------------------------------------------------------------*/
/*  CALCULATOR 4  */
/*--------------------------------------------------------------*/

$('#submit4').click(function() {
	var fuelQuantity = $('#fuel-quantity4').val();
	var ppmw = $('#ppmw4').val();
	var fuelDensity = $('#fuel-density4').val();
	
	if (fuelQuantity == '' || ppmw == '' || fuelDensity == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * ppmw * fuelDensity) / 1000);
		$('#total4').val(total);
	}
});

/*--------------------------------------------------------------*/
/*  CALCULATOR 5  */
/*--------------------------------------------------------------*/

$('#submit5').click(function() {
	var fuelQuantity = $('#fuel-quantity5').val();
	var ppmv = $('#ppmv5').val();
	
	if (fuelQuantity == '' || ppmv == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * 159 * ppmv) / (1000000 * 3.78));
		$('#total5').val(total);
	}
});


/*--------------------------------------------------------------*/
/*  CALCULATOR 6  */
/*--------------------------------------------------------------*/

$('#submit6').click(function() {
	var fuelQuantity = $('#fuel-quantity6').val();
	var ptb = $('#ptb6').val();
	var additiveDensity = $('#additive-density6').val();
	
	if (fuelQuantity == '' || ptb == '' || additiveDensity == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * ptb) / (1000 * 2.2 * additiveDensity * 3.78));
		$('#total6').val(total);
	}
});


/*--------------------------------------------------------------*/
/*  CALCULATOR 7  */
/*--------------------------------------------------------------*/

$('#submit7').click(function() {
	var fuelQuantity = $('#fuel-quantity7').val();
	var ptb = $('#ptb7').val();
	var additiveDensity = $('#additive-density7').val();
	
	if (fuelQuantity == '' || ptb == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * 0.0238 * ptb) / (1000 * 2.2 * additiveDensity * 3.78));
		$('#total7').val(total);
	}
});


/*--------------------------------------------------------------*/
/*  CALCULATOR 8  */
/*--------------------------------------------------------------*/

$('#submit8').click(function() {
	var fuelQuantity = $('#fuel-quantity8').val();
	var ppmv = $('#ppmv8').val();
	
	if (fuelQuantity == '' || ppmv == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * ppmv) / 1000000);
		$('#total8').val(total);
	}
});

/*--------------------------------------------------------------*/
/*  CALCULATOR 9  */
/*--------------------------------------------------------------*/

$('#submit9').click(function() {
	var fuelQuantity = $('#fuel-quantity9').val();
	var ppmw = $('#ppmw9').val();
	var fuelDensity = $('#fuel-density9').val();
	
	if (fuelQuantity == '' || ppmw == '' || fuelDensity == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * 2.2 * 159 * fuelDensity * ppmw) / 1000000);
		$('#total9').val(total);
	}
});


/*--------------------------------------------------------------*/
/*  CALCULATOR 10  */
/*--------------------------------------------------------------*/

$('#submit10').click(function() {
	var fuelQuantity = $('#fuel-quantity10').val();
	var ptb = $('#ptb10').val();
	
	if (fuelQuantity == '' || ptb == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * ptb) / 1000);
		$('#total10').val(total);
	}
});


/*--------------------------------------------------------------*/
/*  CALCULATOR 11  */
/*--------------------------------------------------------------*/

$('#submit11').click(function() {
	var fuelQuantity = $('#fuel-quantity11').val();
	var ppmw = $('#ppmw11').val();
	var fuelDensity = $('#fuel-density11').val();
	
	if (fuelQuantity == '' || ppmw == '' || fuelDensity == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * 2.2 * 3.78 * fuelDensity * ppmw) / 1000000);
		$('#total11').val(total);
	}
});


/*--------------------------------------------------------------*/
/*  CALCULATOR 12  */
/*--------------------------------------------------------------*/

$('#submit12').click(function() {
	var fuelQuantity = $('#fuel-quantity12').val();
	var ptb = $('#ptb12').val();
	
	if (fuelQuantity == '' || ptb == '') {
		alert('Please fill in all of the above fields');
	}
	else {
		var total = Math.ceil((fuelQuantity * 0.0238 * ptb) / 1000);
		$('#total12').val(total);
	}
});



$(document).ready(function(e) {
	$("a.submitHome").each(
		function(index,element){
			$( this ).bind (
				"click",
				function(){
					$.post("hitCount.php", {'button':'btnHome'}, function(data){
				})
		})
	})
})

$(document).ready(function(e) {
	$("a.submitOther").each(
		function(index,element){
			$( this ).bind (
				"click",
				function(){
					$.post("hitCount.php", {'button':'btnOther'}, function(data){
				})
		})
	})
})
