$(document).ready(function(){

	// Quick Entry Interface
	$('.btn_quick_entry').click(function() {
		var vehicle = $(this).parent().siblings('.txtVehicleKey').attr('rel');
		var date = $(this).parent().siblings().children('.txtDate').val();
		var odometer = $(this).parent().siblings().children('.txtOdometer').val();
		var gallons = $(this).parent().siblings().children('.txtGallons').val();
		var price = $(this).parent().siblings().children('.txtPrice').val();

		$.post('blank.php', { page: 'quick_entry', vehicle: vehicle, date: date, odometer: odometer, gallons: gallons, price: price },
		function(data) {
			alert(data);
		});

		return false;
	});
	
	// Check for numeric value
	$('.numeric').blur(function() {
		var tempNumber = $(this).val();
		if (isNaN(tempNumber)) {
			alert('This needs to be a number.');
		}
	});
	
	// Live maintenance cost
	$('.maintenance_cost').change(function() {
		var totalQuantity = 0;
		$('.maintenance_cost').each(function() {
			var quantity = parseFloat(this.value);
			quantity = isNaN(quantity) ? 0 : quantity;
			totalQuantity += quantity;
		});
		$('#txtMaintenanceTotal').val(String(totalQuantity));
	});
	
	// Activate login panel on successful sign up
	if ($('.success:contains("Congratulations")').length > 0) {
		$('.signIn').slideToggle('slow');
	}
	
	// Clock picker
	$(".time").clockpick({military: true});
	
	// Add date inputs
	$(".date").datepicker({ dateFormat: 'mm-dd-yy' });
	
	// Activate tabs
	$('#tab-list').tabs();
	
	// Don't style inputs of type image
	$('input[type=image]').addClass('dontStyleMe');
	
	// Animate special messages
	$('.success, .error').slideDown('fast');
	
	// Insert clear floats after form inputs
	//$(':input').after('<div class="clear"></div>');

	// Check for required fields
	$('.required').after('<img class="exclamation" style="display: none;" src="shared/exclamation.png" />');
	$('.required').blur(function() {
		if ($(this).val() == "") {
			$(this).addClass('invalid');
			$('.required ~ img').hide();
			$('.invalid ~ img').show();
		}
		else {
			$(this).removeClass('invalid');
			$('.required ~ img').hide();
			$('.invalid ~ img').show();
		}
	});
	
	// Table striping
	function stripe_table_rows() {
		var classNames = {
			0: 'first',
			1: 'second',
			2: 'third'};
		
		$('table.striped tbody tr').not('[th]').each(function(index) {
			$(this).addClass(classNames[index % 3]);
		});
	}
	stripe_table_rows();
	
	// Menu effects
	/*$('.menuitem a').hover(
		function() {
			$(this).animate( { paddingRight:"20px" }, "fast" );
		},
		function() {
			$(this).animate( { paddingRight:"10px" }, "fast" );
		}
	);*/

	// Show history detail
	$('.imgExpandHistory').click(function() {
		$(this).next('div').slideToggle('fast');
	});

	// Verify a purposeful delete
	$('.btnRemove').click(function() {
		var answer = confirm('Are you sure?');
		if (answer) {
			// Go ahead
		}
		else {
			return false;
		}
	});
	
	// towFleet site; Show / Hide content for tabs
	$('.tab1').click(function() {
		$('.featureItem').hide();
		$('.desc1').show();
	});
	$('.tab2').click(function() {
		$('.featureItem').hide();
		$('.desc2').show();
	});
	$('.tab3').click(function() {
		$('.featureItem').hide();
		$('.desc3').show();
	});
	$('.tab4').click(function() {
		$('.featureItem').hide();
		$('.desc4').show();
	});
	$('.tab5').click(function() {
		$('.featureItem').hide();
		$('.desc5').show();
	});
	$('.tab6').click(function() {
		$('.featureItem').hide();
		$('.desc6').show();
	});
	
	// Show the Sign In box
	var trigger_login = $('.trigger-login').length;
	if ( trigger_login > 0 ) {
		$('.signIn').slideToggle('slow');
	}
	
	$('a:contains("Log In")').click(function() {
		$('.contact').hide();
		$('.signIn').slideToggle('slow');
		return false;
	});

	// Show Contact Form
	$('a:contains("Contact")').click(function() {
		$('.signIn').hide();
		$('.contact').slideToggle('slow');
		return false;
	});
	
	// Clear text box
	$('.clearMe').click(function() {
		$(this).val('');
	});
	
	// Grab towPartner Data and populate towFleet registration form
	/*$('#btnCopy').click(function() {
		var memberNum = $('#txtTPMemberNumber').val();
		var memberPass = $('#txtTPPassword').val();
		$('#towPartnerInfo').load('site/retrieve_towpartner_data.php?txtTPMemberNumber='+memberNum+'&txtTPPassword='+memberPass, function() {
			$('#txtCompanyName').val($('#companyName').text());
			$('#txtCompanyPhone').val($('#phone').text());
			$('#txtAddress').val($('#address').text());
			$('#txtCity').val($('#city').text());
			$('#sltState').val($('#state').text());
			$('#txtZip').val($('#zip').text());
		})
		return false;
	});*/
	
	// Check for already used username
	$('#txtId').blur(function() {
		var accountId = $('#txtId').val();
		$.ajax({
			type: "POST",
			url: "site/check_for_existing_user.php",
			data: "accountId=" + accountId,
			success: function(msg) {
				if (msg == 'ID Already Taken') {
					$('#txtId').val(msg);
					$('#txtId').addClass('invalid');
				}
			}
		});
	});
	
	// Hijack the Contact form with AJAX
	$('#btnSendMessage').click(function() {
		var name = $('#txtMailName').val();
		var email = $('#txtMailEmail').val();
		var message = $('#txtMailMessage').val();
		$('#emailMessage').html('<img src="shared/loader_dkbrown.gif" />');
		$.ajax({
			type: "POST",
			url: "site/send_email.php",
			data: "txtMailName=" + name + "&txtMailEmail=" + email + "&txtMailMessage=" + message,
			success: function(msg) {
				$('#emailMessage').text(msg);
				$('.contact').slideUp('slow');
			}
		})
		return false;
	});
	
	// Compare passwords
	$('#txtPassword2').blur(function() {
		var firstPass = $('#txtPassword1').val();
		var secondPass = $('#txtPassword2').val();
		if (firstPass != secondPass) {
			alert('Password does not match.');
		}
		else {
			// Move along - nothing to see here
		}
	});
	
	// Maintenance Costs Report
	$('#btn_maintenance').click(function() {
		var costBeginDate = $('#txtBeginCostDate').val();
		var costEndDate = $('#txtEndCostDate').val();
		var criteria = $('#sltMaintenance').val();
		$('#costResult').load('reports/maintenance_cost.php?begindate='+costBeginDate+'&enddate='+costEndDate+'&criteria='+criteria, function() {})
		return false;
	});
	
	// Downtime Report
	$('#btn_downtime').click(function() {
		var costBeginDate = $('#txtBeginDownTimeDate').val();
		var costEndDate = $('#txtEndDownTimeDate').val();
		var criteria = $('#sltDowntime').val();
		$('#downTimeResult').load('reports/downtime.php?begindate='+costBeginDate+'&enddate='+costEndDate+'&criteria='+criteria, function() {})
		return false;
	});

	// Mileage Report
	$('#btn_mileage').click(function() {
		var costBeginDate = $('#txtBeginMileageDate').val();
		var costEndDate = $('#txtEndMileageDate').val();
		var criteria = $('#sltMileage').val();
		$('#mileageResult').load('reports/mileage.php?begindate='+costBeginDate+'&enddate='+costEndDate+'&criteria='+criteria, function() {})
		return false;
	});
});