// Allows for adding and removing extra line 
// items from the dealer order form

$(document).ready(function() {
	// get the integer value of the hidden input
	var items = parseInt($("input#line-item-count").val());
	
	$("#add-line-item").click(function() {
		// set the zero index for the next <tr>
		var items_index = items;
		
		// update the count for the line-items variable
		items += 1;
		$("input#line-item-count").val(items);
		
		// get the full <tr> for the last line item, and add it to
		// the bottom of the table
		var line_item_row = $("#line-items tr:nth-child(2)").clone();		
		$("#line-items tbody").append(line_item_row);
		
		// set the correct names and ids for the new <select>s and
		// <input>s
		var putter_id = "id_" + items_index + "-putter"
		var putter_name = items_index + "-putter"
		var dexterity_id = "id_" + items_index + "-dexterity"
		var dexterity_name = items_index + "-dexterity"
		var shaft_id = "id_" + items_index + "-shaft"
		var shaft_name = items_index + "-shaft"
		var length_id = "id_" + items_index + "-length"
		var length_name = items_index + "-length"
		var lie_id = "id_" + items_index + "-lie"
		var lie_name = items_index + "-lie"
		var grip_id = "id_" + items_index + "-grip"
		var grip_name = items_index + "-grip"
		var head_cover_id = "id_" + items_index + "-head_cover"
		var head_cover_name = items_index + "-head_cover"
		var price_id = "id_" + items_index + "-price"
		var price_name = items_index + "-price"
		var quantity_id = "id_" + items_index + "-quantity"
		var quantity_name = items_index + "-quantity"
		var comments_id = "id_" + items_index + "-comments"
		var comments_name = items_index + "-comments"
		
		$("#line-items tr:last-child #id_0-putter").attr("name", putter_name);
		$("#line-items tr:last-child #id_0-putter").attr("id", putter_id);
		$("#line-items tr:last-child #id_0-dexterity").attr("name", dexterity_name);
		$("#line-items tr:last-child #id_0-dexterity").attr("id", dexterity_id);
		$("#line-items tr:last-child #id_0-shaft").attr("name", shaft_name);
		$("#line-items tr:last-child #id_0-shaft").attr("id", shaft_id);
		$("#line-items tr:last-child #id_0-length").attr("name", length_name);
		$("#line-items tr:last-child #id_0-length").attr("id", length_id);
		$("#line-items tr:last-child #id_0-lie").attr("name", lie_name);
		$("#line-items tr:last-child #id_0-lie").attr("id", lie_id);
		$("#line-items tr:last-child #id_0-grip").attr("name", grip_name);
		$("#line-items tr:last-child #id_0-grip").attr("id", grip_id);
		$("#line-items tr:last-child #id_0-head_cover").attr("name", head_cover_name);
		$("#line-items tr:last-child #id_0-head_cover").attr("id", head_cover_id);
		$("#line-items tr:last-child #id_0-price").attr("name", price_name);
		$("#line-items tr:last-child #id_0-price").attr("id", price_id);
		$("#line-items tr:last-child #id_0-quantity").attr("name", quantity_name);
		$("#line-items tr:last-child #id_0-quantity").attr("id", quantity_id);
		$("#line-items tr:last-child #id_0-comments").attr("name", comments_name);
		$("#line-items tr:last-child #id_0-comments").attr("id", comments_id);
		
		// add the "Remove Item" button
		$("#line-items tr:last-child td.remove_button_holder").append("<span class='remove_line_item'><img src='/static/images/dealers/icon_delete_item.png' title='Remove this line item' /></span>");
	});
	
	$(".remove_line_item").live("click", function() {
		items -= 1;
		// update the count for the line-items variable
		$("input#line-item-count").val(items);
		// remove the entire tr for this row
		$(this).parent().parent().remove();
	});
	
	// Copy the values from the Account Contact field to the Accounts Payable
	// field if the appropriate checkbox is clicked
	$("#same-person").bind('click', function() {
		// get the values from the "Account Contact" fields
		contact_first = $("#id_contact_first").val();
		contact_last = $("#id_contact_last").val();
		contact_phone = $("#id_contact_phone").val();
		contact_fax = $("#id_contact_fax").val();
		contact_email = $("#id_contact_email").val();
		
		// a map of the target field ids
		var contact_map = {
			"#id_ap_first": contact_first, 
			"#id_ap_last": contact_last, 
			"#id_ap_phone": contact_phone,
			"#id_ap_fax": contact_phone,
			"#id_ap_email": contact_email
		};
		
		// for each field in the "Accounts Payable" field, check if 
		// if it's already readonly. If not, copy the value from the 
		// "Account Contact" field and set the input as readonly
		$.each(contact_map, function(target, source) {
			if ($(target).attr("readonly") == true) {
				$(target).removeAttr("readonly").removeAttr("style");
			} else {
				$(target).val(source);
				$(target).attr("readonly", true).attr("style", "color:#666666");
			}
		});
		
	});
	
	// Same as above, for the addresses
	$("#same-address").bind('click', function() {
		// get the values from the "Billing Address" fields
		bill_address = $("#id_billing_address").val();
		
		// a map of the target field ids
		var address_map = {
			"#id_shipping_address": bill_address, 
		};
		
		// for each field in the "Accounts Payable" field, check if 
		// if it's already readonly. If not, copy the value from the 
		// "Account Contact" field and set the input as readonly
		$.each(address_map, function(target, source) {
			if ($(target).get(0).tagName == "SELECT") {
				
				if ($(target).attr("disabled") == true) {
					$(target).removeAttr("disabled");
					$("#added-state-value").remove();
				} else {
					$(target).val(source);
					$("#shipping-address").append("<input id='added-state-value' type='hidden' value='"+ source +"' name='"+ $(target).attr("name") +"' />");//.attr("type", "hidden").attr("id", $(target).attr("id")).attr("name", $(target).attr("name")).attr("value", $(target).val());
					$(target).attr("disabled", true);
				}
				
			} else {
				
				if ($(target).attr("readonly") == true) {
					$(target).removeAttr("readonly").removeAttr("style");
				} else {
					$(target).val(source);
					$(target).attr("readonly", true).attr("style", "color:#666666");
				}
				
			}
			
		});
		
	});
	
});
