function calculateHours(element, newValue, oldValue) {


	// OK break the times on the form into bits, and convert them to
	// a 24 hr decimal time to make calculation easy
	var start_time_string = new String($('timesheet[start_time]').value);
	var start_time_array = start_time_string.split(":");
	if (start_time_array.length != 2) return;
	var start_time_dec = parseFloat(start_time_array[0])+start_time_array[1]/60;

	var finish_time_string = new String($('timesheet[finish_time]').value);
	var finish_time_array = finish_time_string.split(":");
	if (finish_time_array.length != 2) return;
	var finish_time_dec = parseFloat(finish_time_array[0])+finish_time_array[1]/60;

	if (start_time_dec > finish_time_dec) return;
	else {
		var gross_hrs = finish_time_dec - start_time_dec;
		$('timesheet_gross_hrs').value = gross_hrs;
		calculateAmount(gross_hrs);
	}

};

function calculateAmount(gross_hrs) {
	var discount_hrs = parseFloat($('timesheet_discount_hrs').value);
	if (isNaN(discount_hrs)) discount_hrs = 0;
	var chargeable_hrs = gross_hrs - discount_hrs;
	var billable_amount = chargeable_hrs * parseFloat($('rate_per_hr').innerHTML);

	$('timesheet_gross_hrs').value = gross_hrs;
	$('chargeable_hrs').innerHTML = chargeable_hrs;
	$('billable_amount').innerHTML = formatCurrencyNoDollar(billable_amount);
}

function updateRatePerHr(element, newValue, oldValue) {
	new Ajax.Updater('rate_per_hr','/public.php/timesheets/getRatePerHr?charge_rate[id]='+newValue, {
		onComplete: function(transport) {
			calculateAmount($('timesheet_gross_hrs').value);
		}
	});
};

function updateChargeRates(element, newValue, oldValue) {
	//	new Ajax.Updater('rate_per_hr','/public.php/timesheets/getRatePerHr?charge_rate[id]='+newValue, {
	//		onComplete: function(transport) {
	//			 calculateHours($('timesheet[finish_time]'),0,1);
	//		}
	//	});
	console.log("should alter my charge rate drop down choices here");
};

function calculateTotalPaid() {
	var total = 0.00;
	
	$$('div.amount').each(function(d,index) {
		// add amount if corresponding checkbox is checked
		if ($("timesheet_"+d.id).checked) {
			total += parseFloat(d.innerHTML);
		}
	});

	$('totalPaidEx').innerHTML = formatCurrency(total)+" (ex)";
	$('totalPaidInc').innerHTML = formatCurrency(total*1.1)+" (inc)";
	
}

function setInvoicePaid ($class) {
	var val = true;
	$$('input.'+$class).each(function(i, index) {
		if(index == 0) {
			//decide if we are turning selection on or off
			if (i.checked) val = false;
		}
		
		i.checked = val;		
	});
	
	calculateTotalPaid();
}



