//INITIALIZATION
$(function() {

		//Assign event handlers
		
		//Radio buttons
		$("input[id^=optA]").click(UpdateWeightLabelsA);
		$("input[id^=optB]").click(UpdateWeightLabelsB);
		
		//Buttons
		$('input#btnCalculateA').click(CalculateA);
		$('input#btnCalculateB').click(CalculateB);
		$('input#btnResetA').click(FullResetA);
		$('input#btnResetB').click(FullResetB);
		
		//Text boxes
		$('input[id^=txtA]').keydown(PartialResetA);
		$('input[id^=txtB]').keydown(PartialResetB);
		$('input.FuelInput').keypress(function(e){ return AllowNumbersOnly(e, false)});
		
		//Initialize weight labels
		UpdateWeightLabelsA();
		UpdateWeightLabelsB();
		
		//Clear any previous calculations
		FullResetA();
		FullResetB();
		
		//Initialize help dialog
		//$("div#dialog1").jqm({ modal: true, trigger: 'img.helpIcon' });
		$('div#dialog1')
			.jqDrag('.jqDrag')
			.jqResize('.jqResize')
			.jqm({
				trigger:'img.helpIcon',
				overlay: 50,
				modal: true,
				onShow: function(h) {
					/* callback executed when a trigger click. Show notice */
					h.w.css('opacity',1).fadeIn(); 
					},
				onHide: function(h) {
					/* callback executed on window hide. Hide notice, overlay. */
					h.w.fadeOut("slow",function() { if(h.o) h.o.remove(); });
					} 
			});

	}
);



//SCREEN FUNCTIONS
function CalculateA(){
	var intF1 = parseInt($("input#txtA1").val());
	var intF2 = parseInt($("input#txtA2").val());
	var intF3 = parseInt($("input#txtA3").val());
	
	if ((isNaN(intF1)) || (isNaN(intF2)) || (isNaN(intF3))){
		ShowHideErrorMsg("A","Error: Invalid input in one or more textboxes.");
		return;
	}
	
	var intFuelConsumption = intF1 - intF2 + intF3;

	if (intF3 >= intF2){
		ShowHideErrorMsg("A","Error: Fuel uplift for next flight cannot exceed tank total.");
		return;
	}

	if (intFuelConsumption <= 0){
		ShowHideErrorMsg("A","Error: Fuel consumption must be greater than zero.");
		return;
	}
	
	//Weights are valid.  Convert to metric tonnes
	var numTonnes;
	if ($("input#optA_KG").attr("checked"))
	{
		numTonnes = intFuelConsumption * .001;
	}
	else
	{
		numTonnes = intFuelConsumption * 0.000453592
	}
	
	//Multiply by the emissions factor for Jet Kerosene (3.15) and round to nearest integer.
	var intCO2 = Math.round(numTonnes * 3.15);
	
	//Display the results
	numTonnes = Math.round(numTonnes * 100000) / 100000;
	$("div#divResultsA").css("display", "block");
	$("span#spanResultsA_fc").html(numTonnes.toString());
	$("span#spanResultsA_CO2").html(intCO2.toString());
}

function CalculateB(){
	var intF1 = parseInt($("input#txtB1").val());
	var intF2 = parseInt($("input#txtB2").val());
	var intF3 = parseInt($("input#txtB3").val());
	
	if ((isNaN(intF1)) || (isNaN(intF2)) || (isNaN(intF3))){
		ShowHideErrorMsg("B","Error: Invalid input in one or more textboxes.");
		return;
	}
	
	var intFuelConsumption = intF1 + intF2 - intF3;
	
	if (intFuelConsumption <= 0){
		ShowHideErrorMsg("B","Error: Fuel consumption must be greater than zero.");
		return;
	}
	
	//Weights are valid.  Convert to metric tonnes
	var numTonnes;
	if ($("input#optB_KG").attr("checked"))
	{
		numTonnes = intFuelConsumption * .001;
	}
	else
	{
		numTonnes = intFuelConsumption * 0.000453592
	}
	
	//Multiply by the emissions factor for Jet Kerosene (3.15) and round to nearest integer.
	var intCO2 = Math.round(numTonnes * 3.15);
	
	//Display the results
	numTonnes = Math.round(numTonnes * 100000) / 100000;
	$("div#divResultsB").css("display", "block");
	$("span#spanResultsB_fc").html(numTonnes.toString());
	$("span#spanResultsB_CO2").html(intCO2.toString());
}

function FullResetA(){
	$("input[id^=txtA]").val("");
	PartialResetA();
}

function PartialResetA(){
	$("span[id^=spanResultsA]").html("&nbsp;");
	$("div#divResultsA").css("display", "none");
	$("div#divErrorA").css("display", "none");
	ShowHideErrorMsg("A", "");

}

function FullResetB(){
	$("input[id^=txtB]").val("");
	PartialResetB();
}

function PartialResetB(){
	$("span[id^=spanResultsB]").html("&nbsp;");
	$("div#divResultsB").css("display", "none");
	$("div#divErrorB").css("display", "none");
	ShowHideErrorMsg("B", "");
}

function UpdateWeightLabelsA(){
	
	if ($("input#optA_KG").attr("checked"))
	{
		$("span.UnitsA").html("kg");
	}
	else
	{
		$("span.UnitsA").html("lbs");
	}
	
	PartialResetA();
}

function UpdateWeightLabelsB(){
	
	if ($("input#optB_KG").attr("checked"))
	{
		$("span.UnitsB").html("kg");
	}
	else
	{
		$("span.UnitsB").html("lbs");
	}
	
	PartialResetB();
}


//UTILITY FUNCTIONS
function AllowNumbersOnly(event, AllowExtraChars) {
  //alert("AllowNumbersOnly");
  var e = event || window.event;
  var code = e.charCode || e.keyCode;     //depending on what key was pressed

  //If it's a function key, don't filter it
  if (e.keyCode == 13) {                  //Trap Enter Key
    e.returnValue = false;
    e.cancelBubble = true;
    return false;
  }
  if (e.charCode == 0) return true;       //Function key -- Firefox only
  if (e.ctrlKey || e.altKey) return true; //Ctrl or Alt
  if (code < 32) return true;             //ASCII control character

  //If AllowExtraChars is true, then allow - , .
  var allowed;
  if (AllowExtraChars)
    allowed = "0123456789,.-";
  else
    allowed = "0123456789";

  //Retrieve character pressed
  var c = String.fromCharCode(code);

  //Check if key allowed
  if (allowed.indexOf(c) != -1) {
    return true;
  }
  else {
    e.returnValue = false;
    e.cancelBubble = true;
    return false;
  }
}

function ShowHideErrorMsg(section, msg){
	//Shows the specified message in the error div located in the specified section, or hides the div if msg = "".
	//Section must be A or B.
	var div = "div#divError" + section;
	
	if (msg != ""){
		$(div).html(msg).css("display", "block");
	} else {
		$(div).html("&nbsp;").css("display", "none");
	}
}