var comment_tr = null;

$(document).ready( function() {
	$("#shipper_pages a").mousedown( function() {
		return false;
	});

	shipper_onload();
});

function shipper_onload() {
	
	$(".delete_button_now").click(function(){
		var a = 0;
		if ( confirm( "Are you sure?" ) ) {
			$(".shipper_row").each(function(){

				var tr = $(this),
					chk = tr.find(".shipper_delete_checkbox");

				if (chk.length > 0) {

					if (chk[0].checked)
					{
						if ( !tr.hasClass("shipper_additional_row") ) {
							tr.find(".shipper_buttons").hide();
							tr.find(".shipper_message").html("Deleting…");
							tr.find(".shipper_message").show();

							$.ajax({
								type: "post",
								url: "/AJAX/shipper.php",
								data: { action: "delete", txn_id: tr.find(".txn_id").val() },
								tr_row: tr,
								success: function( data ) {
									if ( data == "0" ) {
										remove_classes( this.tr_row );
										this.tr_row.addClass( "shipper_deleted_row" );
									} else {
										ajax_error( this.tr_row );
									}
								}
							});
						} else {
							tr.find(":text").val("");
							tr.find(".shipper_name").change();

							if ( tr.hasClass( "shipper_allowed_row" ) || tr.hasClass( "shipper_denied_row" ) ) {
								tr.find(".shipper_buttons").hide();
								tr.find(".shipper_message").html("Busy…");
								tr.find(".shipper_message").show();


								$.ajax({
									type: "post",
									url: "/AJAX/shipper.php",
									data: { action: "deliver", txn_id: tr.find(".txn_id").val() },
									tr_row: tr,
									success: function( data ) {
										if ( data == "0" ) {
											remove_classes( this.tr_row );
											this.tr_row.addClass( "shipper_delivered_row" );
											this.tr_row.find(".shipper_message").hide();
											this.tr_row.find(".shipper_buttons").show();
										} else {
											ajax_error( this.tr_row );
										}
									}
								});
							}
						}
					}

					chk[0].checked = false;
				}
			});
		}
	});

	$(".shipper_row").each( function() {
		( function( tr ) {
			var container = tr.find(".container_number");

			if ( container.find("a").length ) {
				container = container.find("a");
			}

			tr.data( "container", container.html() );

			tr.mouseover( function() {
				$(this).addClass( "shipper_over_row" );
			});
			tr.mouseout( function() {
				$(this).removeClass( "shipper_over_row" );
			});

			tr.find(".shipper_delete").click( function() {
				if ( confirm( "Are you sure?" ) ) {
					if ( !tr.hasClass("shipper_additional_row") ) {
						tr.find(".shipper_buttons").hide();
						tr.find(".shipper_message").html("Deleting…");
						tr.find(".shipper_message").show();

						$.post( "/AJAX/shipper.php", { action: "delete", txn_id: tr.find(".txn_id").val() }, function( data ) {
							if ( data == "0" ) {
								remove_classes( tr );
								tr.addClass( "shipper_deleted_row" );
							} else {
								ajax_error( tr );
							}
						});
					} else {
						tr.find(":text").val("");
						tr.find(".shipper_name").change();

						if ( tr.hasClass( "shipper_allowed_row" ) || tr.hasClass( "shipper_denied_row" ) ) {
							tr.find(".shipper_buttons").hide();
							tr.find(".shipper_message").html("Busy…");
							tr.find(".shipper_message").show();

							$.post( "/AJAX/shipper.php", { action: "deliver", txn_id: tr.find(".txn_id").val() }, function( data ) {
								if ( data == "0" ) {
									remove_classes( tr );
									tr.addClass( "shipper_delivered_row" );
									tr.find(".shipper_message").hide();
									tr.find(".shipper_buttons").show();
								} else {
									ajax_error( tr );
								}
							});
						}
					}
				}
				return false;
			});

			tr.find(".shipper_deliver").click( function() {
				$( ".container_" + tr.data("container") ).each( function() {
					( function( tr ) {
						tr.find(".shipper_buttons").hide();
						tr.find(".shipper_message").html("Busy…");
						tr.find(".shipper_message").show();

						$.post( "/AJAX/shipper.php", { action: "deliver", txn_id: tr.find(".txn_id").val() }, function( data ) {
							if ( data == "0" ) {
								remove_classes( tr );
								tr.addClass( "shipper_delivered_row" );
								tr.find(".shipper_message").hide();
								tr.find(".shipper_buttons").show();
							} else {
								ajax_error( tr );
							}
						});
					})( $(this) );
				});

				return false;
			});

			tr.find(".shipper_allow").click( function() {
				tr.find(".shipper_buttons").hide();
				tr.find(".shipper_message").html("Busy…");
				tr.find(".shipper_message").show();

				$.post( "/AJAX/shipper.php", { action: "allow", txn_id: tr.find(".txn_id").val() }, function( data ) {
					if ( data == "0" ) {
						remove_classes( tr );
						tr.addClass( "shipper_allowed_row" );
						tr.find(".shipper_message").hide();
						tr.find(".shipper_buttons").show();
					} else {
						ajax_error( tr );
					}
				});

				return false;
			});

			tr.find(".shipper_deny").click( function() {
				tr.find(".shipper_buttons").hide();
				tr.find(".shipper_message").html("Busy…");
				tr.find(".shipper_message").show();

				$.post( "/AJAX/shipper.php", { action: "deny", txn_id: tr.find(".txn_id").val() }, function( data ) {
					if ( data == "0" ) {
						remove_classes( tr );
						tr.addClass( "shipper_denied_row" );
						tr.find(".shipper_message").hide();
						tr.find(".shipper_buttons").show();
					} else {
						ajax_error( tr );
					}
				});

				return false;
			});

			tr.find(".shipper_comment_button").click( function() {
				$("#shipper_comment_text").val( tr.find(".comment").val() );
				$("#shipper_comment").show();

				comment_tr = tr;

				return false;
			});

			tr.find(".shipper_year, .shipper_make, .shipper_model, .shipper_vin, .shipper_name, .shipper_release_to, " +
				".shipper_release_phone, .shipper_date_poti_fee, .shipper_id_number").change( function() {
				update_car( tr, "" );
			});

			tr.find(".shipper_correct, .shipper_release, .shipper_done").click( function() {
				update_car( tr, tr.data("container") );
			});

			tr.find(".shipper_deliver").click( function() {
				$(this).parent("tr");
				return false;
			});
		})( $(this) );
	});

	$("#shipper_comment_save").click( function() {
		$("#shipper_comment").hide();
		comment_tr.find(".comment").val( $("#shipper_comment_text").val() );
		update_car( comment_tr, comment_tr.data("container") );
	});

	$("#shipper_comment_cancel").click( function() {
		$("#shipper_comment").hide();
	});

	$("#shipper_table").show();
	$("#shipper_loading").hide();
}

function update_car( tr, container ) {
	tr.find(".shipper_buttons").hide();
	tr.find(".shipper_message").html("Busy…");
	tr.find(".shipper_message").show();

	var data = {
		action: "update",
		txn_id: tr.find(".txn_id").val(),
		year: tr.find(".shipper_year").val(),
		make: tr.find(".shipper_make").val(),
		model: tr.find(".shipper_model").val(),
		vin: tr.find(".shipper_vin").val(),
		name: tr.find(".shipper_name").val(),
		release_to: tr.find(".shipper_release_to").val(),
		release_phone: tr.find(".shipper_release_phone").val(),
		date_poti_fee: tr.find(".shipper_date_poti_fee").val(),
		id_number: tr.find(".shipper_id_number").val(),
		correct: tr.find(".shipper_correct").is(":checked"),
		release: tr.find(".shipper_release").is(":checked"),
		done: tr.find(".shipper_done").is(":checked"),
		comment: tr.find(".comment").val(),
		container: container
	};

	$.post( "/AJAX/shipper.php", data, function( data ) {
		if ( data == "0" ) {
			tr.find(".shipper_message").hide();
			tr.find(".shipper_buttons").show();

			if ( tr.find(".comment").val() != "" ) {
				tr.find(".shipper_comment_button").addClass("shipper_comment_button_present");
			} else {
				tr.find(".shipper_comment_button").removeClass("shipper_comment_button_present");
			}

			comment_tr = null;
		} else {
			ajax_error( tr );
		}
	});
}

function remove_classes( tr ) {
	tr.removeClass( "shipper_pending_row" );
	tr.removeClass( "shipper_delivered_row" );
	tr.removeClass( "shipper_allowed_row" );
	tr.removeClass( "shipper_denied_row" );
}

function ajax_error( tr ) {
	alert( "Error occured" );
	tr.find(".shipper_message").hide();
	tr.find(".shipper_buttons").show();
}

