$(document).ready(function(){
	
	function roundNumber(num, dec) {
		var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
		return result;
	}

	$('#form_energycalc').submit(function() {

		var bulbSize = $('#frm_bulb').val();
		var numBulbs = $('#frm_nobulbs').val();
		var timeOn 	 = $('#frm_timeon').val();
		var timeOcc  = $('#frm_occupied').val();
		var elecCost = $('#frm_eleccost').val();
		var numWDays = $('#frm_nodays').val();

		var valid = 1;

		if (bulbSize == '' && valid == 1) {
			$('div.error').show();
			$('div.error').html('Please enter the bulb size in Watts.');
			$('#frm_bulb').focus();
			valid = 0;
		}

		if (numBulbs == '' && valid == 1) {
			$('div.error').show();
			$('div.error').html('Please enter the number of bulbs in the room.');
			$('#frm_nobulbs').focus();
			valid = 0;
		}

		if (timeOn == '' && valid == 1) {
			$('div.error').show();
			$('div.error').html('Please enter the number of predicted hours the lights will be on per day.');
			$('#frm_timeon').focus();
			valid = 0;
		}

		if (timeOcc == '' && valid == 1) {
			$('div.error').show();
			$('div.error').html('Please enter the number of hours the room is occupied.');
			$('#frm_occupied').focus();
			valid = 0;
		}

		if (elecCost == '' && valid == 1) {
			$('div.error').show();
			$('div.error').html('Please enter the current electricy cost in £/kWh.');
			$('#frm_eleccost').focus();
			valid = 0;
		}

		if (numWDays == '' && valid == 1) {
			$('div.error').show();
			$('div.error').html('Please enter the number of annual working days.');
			$('#frm_nodays').focus();
			valid = 0;
		}

		if (valid == 1) {
			
			$('div.error').hide();
			
			var totalWattage = bulbSize * numBulbs
			$('#calc_totalwattage').html(totalWattage+' W');
			
			var roomCurrant = totalWattage / 240;
			$('#calc_current').html(roundNumber(roomCurrant, 2)+' A');
			
			var kwhUsed = (totalWattage * timeOn) / 1000;
			$('#calc_kwhused').html(kwhUsed+' kWh');
			
			var costDay = kwhUsed * elecCost;
			$('#calc_costday').html('£'+roundNumber(costDay, 2));
			
			var costYear = costDay * numWDays;
			$('#calc_costyear').html('£'+roundNumber(costYear, 2));
			
			var costEnergy = ((timeOcc * totalWattage) / 1000) * elecCost * numWDays;
			$('#calc_costenergy').html('<strong>£'+roundNumber(costEnergy, 2)+'</strong>');

			var costSaving = costYear - costEnergy;
			$('#calc_saving').html('<strong>£'+roundNumber(costSaving, 2)+'</strong>');
			
		}
		
		return false;
	});

});