//var $j = jQuery.noConflict();
// JavaScript Document

// Formats field to display in US Currency
function formatCurrency(num) {
	var num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		var num = "0";
	var sign = (num == (num = Math.abs(num)));
	var num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	var num = Math.floor(num/100).toString();
	if(cents<10)
	var cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	var num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

// Sets the Energy Efficient Burn Hours field to the same value as the Existing Burn Hours field
function changeBurnHours() {
	jQuery('#burnHoursEfficiency').val( jQuery('#existBurnHours').val() );
}

// Sets the Energy Efficient KWHR field to the same value as the Existing KWHR field
function changeKWHR() {
	jQuery('#kwhrEfficiency').val( jQuery('#existKwhr').val() );
}

// Sets the Energy Efficient Fixture Quantity field to the same value as the Existing Fixture Quantity field
function changeQuantity() {
	jQuery('#efficiencyFixtureQuantity').val( jQuery('#existFixtureQuantity').val() );
}


jQuery(document).ready(function($) {
		   
	// Assigns same value for Annual Burn Hours and Average KWHR for Exising fields and Energy Efficient fields
	$('#burnHoursEfficiency').val( $('#existBurnHours').val() );
	$('#kwhrEfficiency').val( $('#existKwhr').val() );
			
	$('#energySubmit').click(function() {

		// Hide error messages
		$('.error1,.error2,.error3').css('display', 'none');

		//validation of form

		var existFixtureQuantity = $('input#existFixtureQuantity').val();
		existFixtureQuantity = parseFloat(existFixtureQuantity);

		//var num = /^\d+$/;

		// Checking to see if quantity text field is empty, if so, display error
		if ( !existFixtureQuantity ) {
			$('.error1').css("display", "block");
			$('input#existFixtureQuantity').css('border', '2px inset red');
			$('input#existFixtureQuantity').focus();
		} else {
			// Checking to see if that number is greater than 0, if not display error
			if (existFixtureQuantity <= 0) {	
				$('.error2').css("display", "block");
				$('input#existFixtureQuantity').css('border', '2px inset red');
				document.energyEfficiency.existFixtureQuantity.value = 1;
				document.energyEfficiency.efficiencyFixtureQuantity.value = 1;
				$('input#existFixtureQuantity').focus();
				// Only occurs if text field has been filled out correctly
			} else {
				$('input#existFixtureQuantity').css('border', '2px inset');

				var existLampWattage = parseFloat( $('#existLampWattage').val() );
				var existKwhr        = parseFloat( $('#existKwhr').val() );
				var existBurnHours   = parseFloat( $('#existBurnHours').val() );
				$('#existCostFixture').val(
					existLampWattage * existKwhr * existBurnHours / 1000
				);

				// Determining which Energy Efficient Lamp was selected
				var lampEfficiency       = parseFloat( $('#lampEfficiency').val() );
				var burnHoursEfficiency  = parseFloat( $('#burnHoursEfficiency').val() );
				var kwhrEfficiency       = parseFloat( $('#kwhrEfficiency').val() );
				$('#efficiencyCostFixture').val(
					lampEfficiency * burnHoursEfficiency * kwhrEfficiency / 1000
				);

				// Calculating Final Totals
				document.energyEfficiency.existAnnualCost.value = document.energyEfficiency.existFixtureQuantity.value * document.energyEfficiency.existCostFixture.value;	
				document.energyEfficiency.savingsCostFixture.value = document.energyEfficiency.existCostFixture.value - document.energyEfficiency.efficiencyCostFixture.value;				
				document.energyEfficiency.efficiencyAnnualCost.value = document.energyEfficiency.efficiencyCostFixture.value * document.energyEfficiency.efficiencyFixtureQuantity.value;
				document.energyEfficiency.savingsAnnualCost.value = document.energyEfficiency.existAnnualCost.value - document.energyEfficiency.efficiencyAnnualCost.value;

				// Formatting Currency Fields
				document.energyEfficiency.existCostFixture.value = formatCurrency(document.energyEfficiency.existCostFixture.value);
				document.energyEfficiency.efficiencyCostFixture.value = formatCurrency(document.energyEfficiency.efficiencyCostFixture.value);

				if (document.energyEfficiency.savingsCostFixture.value > 0) {
					$('#savingsCostFixture').css('color', '#060');
				} else {
					$('#savingsCostFixture').css('color', '#000');
				}

				document.energyEfficiency.savingsCostFixture.value = formatCurrency(document.energyEfficiency.savingsCostFixture.value);
				document.energyEfficiency.existAnnualCost.value = formatCurrency(document.energyEfficiency.existAnnualCost.value);
				document.energyEfficiency.efficiencyAnnualCost.value = formatCurrency(document.energyEfficiency.efficiencyAnnualCost.value);

				if (document.energyEfficiency.savingsAnnualCost.value > 0) {
					$('#savingsAnnualCost').css('color', '#060');
				} else {
					$('#savingsAnnualCost').css('color', '#000');
				}

				document.energyEfficiency.savingsAnnualCost.value = formatCurrency(document.energyEfficiency.savingsAnnualCost.value);
			}
		}

		// Stops the page from reloading
		return false;

	});
});

