$(document).ready( function() {
	$("#payment-add-car").click( function() {
		var i;

		if ( $(".payment-car-header").length == 4 ) {
			$("#payment-add-car").closest("table").css( "visibility", "hidden" );
		} else {
			$("#payment-add-car").closest("table").css( "visibility", "visible" );
		}

		$("#payment-cars tr.cl").each( function() {
			$(this).find("td.cl:last-child").clone( true )
				.find(".tips_first_name")
					.find("div").hide().end()
				.end()
				.find(".tips_last_name")
					.find("div").hide().end()
				.end()
				.find(".tips_id_number")
					.find("div").hide().end()
				.end()
				.find(".tips_year")
					.find("div").hide().end()
				.end()
				.find(".tips_make")
					.find("div").hide().end()
				.end()
				.find(".tips_model")
					.find("div").hide().end()
				.end()
				.find(".tips_vin")
					.find("div").hide().end()
				.end()
				.find(".tips_car_price")
					.find("div").hide().end()
				.end()
				.find(".tips_insurance")
					.find("div").hide().end()
				.end()
				.find(".tips_transportation_price")
					.find("div").hide().end()
				.end()
				.find(".tips_shipping_price")
					.find("div").hide().end()
				.end()
				.find(".tips_dealers_fees")
					.find("div").hide().end()
				.end()
				.find("input, select").each(function(){
					this.name = this.name.replace(/\[[0-9]+\]/, "[9999]");
					if (this.nodeName == "INPUT" && this.type == "radio") {
						this.checked = false;
					}
				})
				.end()
				.appendTo( $(this) );

			$(this).find("td.cl:last-child")
				.find("input[type=text], select")
				//.not(".first_name, .last_name, .id_number")
				.val("");

			$(this).find("td.cl:last-child")
				.find(".car_price").val("0").end()
				.find(".transportation_price").val("0").end()
				.find(".shipping_price").val("0").end()
				.find(".dealers_fees").val("0").end()
				.find(".total").val("0.00").end()
		});

		$("#payment-cars tr.cl").each( function() {
			$(this).find("td.cl").each(function( index, self ){
				if (index > 0) {
					$(self).find("input, select").each(function(){
						this.name = this.name.replace(/\[[0-9]+\]/, "[" + (index - 1) + "]");
						if (this.nodeName == "INPUT" && this.type == "radio") {
							this.id = this.id.replace(/[0-9]+/, (index - 1));
						}
					});
					$(self).find("label").each(function(){
						$(this).attr("for", $(this).attr("for").replace(/[0-9]+/, (index - 1)));
					});
				}
			});
		});

		payment_update();

		i = 1;

		$(".payment-car-header span").each( function() {
			$(this).html( "Car " + i );
			i++;
		});

		$(".payment-delete-car").show();
	});

	$("#payment-add-vin").click( function() {
		var i;

		if ( $(".payment-container-header").length == 9 ) {
			$("#payment-add-vin").closest("table").css( "visibility", "hidden" );
		} else {
			$("#payment-add-vin").closest("table").css( "visibility", "visible" );
		}

		$("#payment-vins tr").each( function() {
			$(this).find("td:last-child").clone( true )
				.find(".tips_vin")
					.find("div").hide().end()
				.end()
				.find(".tips_shipping_price")
					.find("div").hide().end()
				.end()
				.appendTo( $(this) );

			$(this).find("td:last-child")
				.find("input, select")
				.val("");

			$(this).find("td:last-child")
				.find(".shipping_price").val("0").end()
				.find(".total").val("0.00").end()
		});

		container_update();

		i = 1;

		$(".payment-container-header span").each( function() {
			$(this).html( "Car " + i );
			i++;
		});

		$(".payment-delete-vin").show();
	});

	$(".payment-delete-car").click( function() {
		var i = $(".payment-delete-car").index( this ) + 1;

		$("#payment-cars tr.cl").each( function() {
			$(this).find( "td.cl:eq(" + i + ")" ).remove();
		});

		$("#payment-cars tr.cl").each( function() {
			$(this).find("td.cl").each(function( index, self ){
				if (index > 0) {
					$(self).find("input, select").each(function(){
						this.name = this.name.replace(/\[[0-9]+\]/, "[" + (index - 1) + "]");
						if (this.nodeName == "INPUT" && this.type == "radio") {
							this.id = this.id.replace(/[0-9]+/, (index - 1));
						}
					});
					$(self).find("label").each(function(){
						$(this).attr("for", $(this).attr("for").replace(/[0-9]+/, (index - 1)));
					});
				}
			});
		});

		payment_update();

		i = 1;

		$(".payment-car-header span").each( function() {
			$(this).html( "Car " + i );
			i++;
		});

		$("#payment-add-car").closest("table").css( "visibility", "visible" );

		if ( $(".payment-car-header").length == 1 ) {
			$(".payment-delete-car").hide();
		} else {
			$(".payment-delete-car").show();
		}

		updateTotal();
	});

	$(".payment-delete-vin").click( function() {
		var i = $(".payment-delete-vin").index( this ) + 1;

		$("#payment-vins tr").each( function() {
			$(this).find( "td:eq(" + i + ")" ).remove();
		});

		container_update();

		i = 1;

		$(".payment-container-header span").each( function() {
			$(this).html( "Car " + i );
			i++;
		});

		$("#payment-add-vin").closest("table").css( "visibility", "visible" );

		if ( $(".payment-container-header").length == 1 ) {
			$(".payment-delete-vin").hide();
		} else {
			$(".payment-delete-vin").show();
		}

		updateTotal();
	});

	$("#payments-submit").click( function() {
		$("#orderCarForm").submit();
	});

	$("#orderCarForm").submit( function() {
		$("#payments-submit").attr( "disabled", "disabled" );

		$("#orderCarForm").find("input, select, textarea").blur();
		updateTotal();
		$.ajax({
			async: false,
			type: "post",
			url: "/AJAX/payments_reg.php",
			data: $("#orderCarForm").serialize(),
			dataType: "json",
			success: function( data ) {
				if ( typeof data.errors != "undefined" ) {
					alert( "Please correct the errors." );
				} else if ( data == 0 ) {
					// $("#request_window").hide();
					// $("#orderCarForm").hide();
					$("#payment-sent-message").show();
				} else {
					alert( "Error occured." );
				}
			}
		});

		return false;
	});

	$(".payment-registration-section").find("input, select").change( function() {
		$(this).closest(".payment-registration-section").data( "changed", true )
		$(this).closest(".payment-registration-section").find(".payment-registration-section-expandcollapse-button").hide();
		$(this).closest(".payment-registration-section").find(".payment-registration-section-register-button").hide();
	});

	$(".payment-registration-section").find("input, select").keypress( function() {
		$(this).change();
	});

	$(".payment-registration-section-title").click( function() {
		if ( $(this).find(".visibility").val() == "1" ) {
			if ( $(this).find(".payment-registration-section-expandcollapse-button").css("display") == "none" ) {
				return;
			}

			if ( $(this).closest(".payment-registration-section").data("changed") === true ) {
				if ( !confirm( "Contents of collapsed sections won't be sent.\nAre you sure you want to collapse the section?" ) ) {
					return;
				}
			}

			$(this).next().hide();
			$(this).closest(".payment-registration-section").addClass("payment-registration-section-collapsed");
			$(this).find(".payment-registration-section-expandcollapse-button").html("▼");
			$(this).closest(".payment-registration-section").find(".payment-registration-section-register-button").val("Register");
			$(this).find(".visibility").val("0");
		} else {
			$(this).next().show();
			$(this).closest(".payment-registration-section").removeClass("payment-registration-section-collapsed");
			$(this).find(".payment-registration-section-expandcollapse-button").html("▲");
			$(this).closest(".payment-registration-section").find(".payment-registration-section-register-button").val("Close");
			$(this).find(".visibility").val("1");
		}
	});

	$(".payment-registration-section-title").mousedown( function() {
		return false;
	});
});

function payment_update() {
	for ( var i = 1; i <= 5; i++ ) {
		$("#payment-cars tr").find( "td:eq(" + i + ")" )
			.find(".first_name").attr( "id", "first_name" + i ).end()
			.find(".tips_first_name").attr( "id", "tips_first_name" + i ).end()
			.find(".last_name").attr( "id", "last_name" + i ).end()
			.find(".tips_last_name").attr( "id", "tips_last_name" + i ).end()
			.find(".id_number").attr( "id", "id_number" + i ).end()
			.find(".tips_id_number").attr( "id", "tips_id_number" + i ).end()
			.find(".year").attr( "id", "year" + i ).end()
			.find(".tips_year").attr( "id", "tips_year" + i ).end()
			.find(".make").attr( "id", "make" + i ).end()
			.find(".tips_make").attr( "id", "tips_make" + i ).end()
			.find(".model").attr( "id", "model" + i ).end()
			.find(".tips_model").attr( "id", "tips_model" + i ).end()
			.find(".vin").attr( "id", "vin" + i ).end()
			.find(".tips_vin").attr( "id", "tips_vin" + i ).end()
			.find(".car_price").attr( "id", "car_price" + i ).end()
			.find(".tips_car_price").attr( "id", "tips_car_price" + i ).end()
			.find(".transportation_price").attr( "id", "transportation_price" + i ).end()
			.find(".tips_transportation_price").attr( "id", "tips_transportation_price" + i ).end()
			.find(".shipping_price").attr( "id", "shipping_price" + i ).end()
			.find(".tips_shipping_price").attr( "id", "tips_shipping_price" + i ).end()
			.find(".dealers_fees").attr( "id", "dealers_fees" + i ).end()
			.find(".tips_dealers_fees").attr( "id", "tips_dealers_fees" + i ).end()
			.find(".total").attr( "id", "total" + i ).end();
	}
}

function container_update() {
	for ( var i = 1; i <= 10; i++ ) {
		$("#payment-vins tr").find( "td:eq(" + i + ")" )
			.find(".cvin").attr( "id", "cvin" + i ).end()
			.find(".tips_vin").attr( "id", "tips_cvin" + i ).end()
			.find(".shipping_price").attr( "id", "shipping_cprice" + i ).end()
			.find(".tips_shipping_price").attr( "id", "tips_shipping_cprice" + i ).end();
	}
}

// Bought cars editing

bought_animation = false;

	function getCaretPositionA(elem)
	{
		var CaretPos = 0;
		// IE Support
		if (document.selection)
		{
			//elem.focus ();
			var Sel = document.selection.createRange ();

			Sel.moveStart ('character', -elem.value.length);

			CaretPos = Sel.text.length;
		}
		// Firefox support
		else if (elem.selectionStart || elem.selectionStart == '0')
			CaretPos = elem.selectionStart;
		return (CaretPos);
	}

	function price_fieldKeyPress( b ) {
		var C= /[0-9\x25\x27\x24\x23\x2E]/;
		var a = b.which;

		var pos = getCaretPositionA(this);

		if ((this.value.indexOf('.') < 0) && (this.value.length == 6)) {
			C= /[\x25\x27\x24\x23\x2E]/;
		}
		if ((this.value.indexOf('.') > 0) || ((pos == 0) && (a == 46)) || ((pos < this.value.length - 2) && (a == 46)))
		{
			C= /[0-9\x25\x27\x24\x23]/;
		}
		if ((this.value.indexOf('.') > 0) && (this.value.length == this.value.indexOf('.') + 3))
		{
			C= /[\x25\x27\x24\x23]/;
		}
		var c = String.fromCharCode(a);
		if ( this.oncallback ) {
			this.oncallback( b );
		}
		return !!(a==0||a==8||a==9||a==13||c.match(C));
	}

	function price_fieldBlur( b ) {
		this.value = parseFloat( this.value ) || "";
		if (this.value.indexOf('.') > 0)
		{
			if (this.value.indexOf('.') == this.value.length - 1)
			{
				this.value += '00';
			} else if (this.value.indexOf('.') == this.value.length - 2) {
				this.value += '0';
			}
		} else if (this.value.length > 0) {
			this.value += '.00';
		}
	}

$(document).ready( function() {
	$(".bought_row").find(".shipper_release_to, .shipper_id_number, .shipper_release_phone").change( function() {
		var tr = $(this).closest("tr");

		var data = {
			action: "update",
			txn_id: tr.find(".txn_id").val(),
			release_to: tr.find(".shipper_release_to").val(),
			id_number: tr.find(".shipper_id_number").val(),
			release_phone: tr.find(".shipper_release_phone").val()
		};

		$.post( "/AJAX/bought.php", data, function( data ) {
			if ( data == "0" ) {
				if ( bought_animation === false ) {
					bought_animation = true;

					var bgcolor = tr.css( "background-color" );

					tr.animate( { backgroundColor: "#c4e4ee" }, 100, function() {
						tr.animate( { backgroundColor: bgcolor }, 600 );
					});

					setTimeout( 'bought_animation = false', 600 );
				}
			} else {
				alert( "Error occured" );
			}
		});
	});

	jQuery(".price_field").each(function(){
		jQuery(this).keypress( price_fieldKeyPress ).blur( price_fieldBlur );
	});

});

