$(document).ready( function() {
	Date.firstDayOfWeek = 0;
	Date.format = "yyyy-mm-dd";

	$(".financed-date").datePicker({ startDate: "1980-01-01" })

	$(".financed-more").click( function() {
		$( ".financed-car-" + $(this).closest("tr").attr("alt") ).show();
		$(this).closest("tr").find(".financed-less").show();
		$(this).closest("tr").find(".financed-last-value").hide();
		$(this).closest("tr").find(".financed-car-value").show();
		$(this).hide();
	}).mousedown( function() {
		return false;
	});

	$(".financed-less").click( function() {
		$( ".financed-car-" + $(this).closest("tr").attr("alt") ).hide();
		$(this).closest("tr").find(".financed-more").show();
		$(this).closest("tr").find(".financed-car-value").hide();
		$(this).closest("tr").find(".financed-last-value").show();
		$(this).hide();
	}).mousedown( function() {
		return false;
	});

	$(".financed-delete").click( function() {
		return confirm( "Are you sure?" );
	});

	$("#financed-show-all").click( function() {
		window.location = "/my-account/financed/";
	});

	$("#payment-cars-show-all").click( function() {
		window.location = "/my-account/id/";
	});

	financed_animation = false;

	$("tr[alt] .financed-name, " +
	  "tr[alt] .financed-date, " +
	  "tr[alt] .financed-vin, " +
	  "tr[alt] .financed-amount, " +
	  "tr[alt] .financed-permonth").change( function() {
		var tr = $(this).closest("tr");
		var data = {
			action: "update",
			id: tr.attr("alt"),
			name: tr.find(".financed-name").val(),
			date: tr.find(".financed-date").val(),
			vin: tr.find(".financed-vin").val(),
			amount: tr.find(".financed-amount").val(),
			permonth: tr.find(".financed-permonth").val()
		};

		$.post( "/AJAX/financed.php", data, function( data ) {
			if ( data == "0" ) {
				if ( financed_animation === false ) {
					financed_animation = true;

					var bgcolor = tr.css( "background-color" );

					tr.animate( { backgroundColor: "#c4e4ee" }, 100, function() {
						tr.animate( { backgroundColor: bgcolor }, 600 );
					});

					setTimeout( 'financed_animation = false', 600 );
				}
			} else {
				ajax_error( tr );
			}
		});
	});

	/*$(".financed-vin").change( function() {
		var tr = $(this).closest("tr");

		if ( $(this).val() != "" ) {
			$.get( "/AJAX/name_for_vin.php", { vin: $(this).val() }, function( data ) {
				if ( data != "" ) {
					tr.find(".financed-name").val( data ).change();
				}
			});
		}
	});*/
});

function ajax_error( tr ) {
	alert( "Error occured" );
}

