// ############################################
// Name: BPM Calculator
// Author: Blaine Booher
// Date: 9/6/05
// Calculates BPM via clicking
// ############################################

var time_obj;
var time_obj2
var time_start;
var time_prev;
var time_current;
var bpm_ave;
var bpm_delta;
var bpm_peaks;

function initialize()
{
	bpm_ave = 0;
	bpm_delta = 0;
	bpm_peaks = 0;
	time_start = 0;
	time_prev = 0;
	time_current = 0;
	document.bpm.bpm_average.value = "";
	document.bpm.bpm_delta.value = "";
	document.bpm.bpm_peaks.value = "";
}

function calc_bpm()
{
	time_obj = new Date();

	if (bpm_peaks > 0)
	{
		time_prev = time_current;
		time_current = time_obj.getTime();

		// *** Calculate average and peak values ***
		document.bpm.bpm_delta.value = (60)/((time_current - time_prev) / 1000);
		document.bpm.bpm_average.value = (60 * bpm_peaks) / ((time_current - time_start) / 1000);

	} else {
		initialize();
		time_start = time_prev = time_current = time_obj.getTime();
	}

	document.bpm.bpm_peaks.value=bpm_peaks;
	bpm_peaks++;
}