
jQuery.noConflict();
jQuery(document).ready(function($)
{	
	$("a[href='#']").click( function()
	{
		return false;
	});	
		
	$('form').submit(function(event) 
	{
		var input = $(this).find("input[name='email']");
		if(input)
		{
			if(input.val().indexOf('@')==-1)
			{
				event.preventDefault();
				alert('Please provide a valid email address');
				return false;
			}
		}
		return true;
	});
	
	$('input.expense_meals, input.expense_lodging, input.expense_transportation, input.contribution_hours, input.contribution_lodging, input.contribution_other').keydown(function(event)
	{
		var key = event.charCode || event.keyCode || 0;
        // allow backspace, tab, delete, period, arrows, numbers and keypad numbers ONLY
        if( key == 8 || 
			key == 9 || 
			key == 46 || 
			key == 190 ||
			(key >= 37 && key <= 40) ||
			(key >= 48 && key <= 57) ||
			(key >= 96 && key <= 105))
		{
			return true;	
		}
		else
		{
			event.preventDefault();
			return false;
		}
	});
	
	// EXPENSES
	$('input.expense_meals, input.expense_lodging, input.expense_transportation').change(function(event)
	{
		var row = $(this).closest('tr');
		
		var meals = row.find('input.expense_meals').val();
		
		var lodging = row.closest('tr').find('input.expense_lodging').val();
		lodging = Number(lodging)*1.5;
		row.closest('tr').find('input.expense_lodging_adjusted').val(lodging);
			
		var transportation = row.find('input.expense_transportation').val();
		
		// row sum
		var sum = Number(meals)+Number(lodging)+Number(transportation);
		row.find('input.expense_total').val(sum);
		
		// Meals column
		sum = 0;
		$('input.expense_meals').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#expense_total_meals').val(sum.toFixed(2));
		
		// Lodging column
		sum = 0;
		$('input.expense_lodging_adjusted').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#expense_total_lodging').val(sum.toFixed(2));
		
		// Transportation column
		sum = 0;
		$('input.expense_transportation').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#expense_total_transportation').val(sum.toFixed(2));
		
		// TOTAL
		sum = 0;
		$('input.expense_total').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#expenses_total').val("$"+sum.toFixed(2));
	});
	
	// CONTRIBUTIONS
	$('input.contribution_hours, input.contribution_lodging, input.contribution_other').change(function(event)
	{
		var row = $(this).closest('tr');
		
		var hours = row.find('input.contribution_hours').val();
		hours = Number(hours)*62.06;
		row.closest('tr').find('input.contribution_rate').val(hours);
		
		var lodging = row.find('input.contribution_lodging').val();
		
		var other = row.find('input.contribution_other').val();
		
		// row sum
		var sum = Number(hours)+Number(lodging)+Number(other);
		row.find('input.contribution_total').val(sum.toFixed(2));
		
		// Rate column
		sum = 0;
		$('input.contribution_rate').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#contribution_total_rate').val(sum.toFixed(2));
		
		// Lodging column
		sum = 0;
		$('input.contribution_lodging').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#contribution_total_lodging').val(sum.toFixed(2));
		
		// Other column
		sum = 0;
		$('input.contribution_other').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#contribution_total_other').val(sum.toFixed(2));
		
		// TOTAL
		sum = 0;
		$('input.contribution_total').each(function()
		{
			sum = sum + Number($(this).val());
		});
		$('input#contributions_total').val("$"+sum.toFixed(2));
	});
	
	
});
