//**********************************************************************************************************************
/**
* DOCUMENT: /core/plugins/events/front/projects.back.js
* DEVELOPED BY: Ryan Stemkoski
* COMPANY: Zipline Interactive
* EMAIL: ryan@gozipline.com
* PHONE: 509-321-2849
* DATE: 4/28/2010
* DESCRIPTION: This document has all of the javascript functions required for the events  plugin.
*/
//***********************************************************************************************************************

//***********************************************************************************************************************
//ON DOCUMENT READY FUNCTIONS
//***********************************************************************************************************************

/**
 * 2012 Clinician/Observer/Referee Voucher Form JS
 * @uses This calculates the total cost of the voucher and inserts it into #total
 */
var total = 0;
var clinician_fee = observer_fee = 75;
var price_per_mile = 0.55;

/**
 * jQuery $(document).ready();
 */
$(function() {

	$('.event_dates').datepicker();
	
	//The input boxes
	$('.voucher_costs_text').keyup(function() {
		updateTotal();
	});
	
	//The checkboxes
	$('.voucher_costs_checkbox').change(function() {
		updateTotal();
	});
});

/**
 * updateTotal() - Calculate the total cost of a voucher form on /2012-voucher-officials-form/
 * @return void
 */
function updateTotal() {
	total = 0;
		
	//Determine if this is for a clinician
	var clinician = $('#clinician_fee').attr('checked');
	if (clinician == true) {
		total += clinician_fee;
	}
	
	//Determine if this is for an observer
	var observer = $('#observer_fee').attr('checked');
	if (observer == true) {
		total += observer_fee;
	}
	
	//Calculate Mileage (0.55 * mileage)
	var mileage = $('#mileage').val();
	if (isNumeric(mileage)) {
		if (observer == true && parseInt(mileage) > 50) {
			total += 15;
		}
		else {
			total += (mileage * price_per_mile);
		}
	}
	
	//Add in 'Other'
	var other = $('#other').val();
	if (isNumeric(other)) {
		total += parseFloat(other);
	}
	
	//Display the Total (This will also be recalculated in PHP)
	$('#total').attr('value', parseFloat(total).toFixed(2));
}

/**
 * isNumeric() - Determines if the value is numerical
 * @param mixed mixed_var The variable to determine whether or not it's numerical
 * @return boolean Returns true if the value is numerical, false if not
 */
function isNumeric (mixed_var) {
    return (typeof(mixed_var) === 'number' || typeof(mixed_var) === 'string') && mixed_var !== '' && !isNaN(mixed_var);
}
