/**
  * Toggle New Destination
  *
  * When the shopper has selected to input a "New Destination"
  * from the list, we need to show a form by which to input that
  * information.  If they chose a recipient nickname from the
  * dropdown instead, we shouldn't show the form.  This function
  * will do that.
  * 
  * @author Matt James <matt@sephone.com>
  * @param  select selectElement
  * @return void
  */
function toggleNewDestination(selectElement)
{
	var orderItemDestinationId = selectElement.id.split("_")[1];
	if ($F(selectElement) == "new") {
		// Show the form.
		Element.removeClassName("new_destination_"+orderItemDestinationId, 'hidden');
	} else {
		// Hide the form.
		Element.addClassName("new_destination_"+orderItemDestinationId, 'hidden');
	}
}

/**
  * Add Destination
  *
  * When the shopper clicks "Add Another" on the editItem page
  * this function is run.  Essentially clones the first row (which
  * should always be present), changes some names and ids, and then
  * adds it to the bottom of the list so the shopper can choose a
  * new quantity for a new destination.
  * 
  * @author Matt James <matt@sephone.com>
  * @param  void
  * @return void
  */
function addDestination(){
	// Clone the first one so we can attach a variant to the end of the list.
	var destinationSelectionRow = $('destination_1').cloneNode(true);
	var newDestinationRow = $('new_destination_1').cloneNode(true);
	
	// Update the id
	var num = ($$('#destination_table tr.destination_row').length / 2) + 1;
	destId = 'destination_'+num;
	newDestId = 'new_destination_'+num;
	destinationSelectionRow.id = destId;
	newDestinationRow.id = newDestId;
	
	$('destination_table').appendChild(destinationSelectionRow);
	$('destination_table').appendChild(newDestinationRow);
	
	// Change a few things about the selection row to put it in default state.
	var destinationQuantity = $$('#'+destId+' input.number')[0];
	destinationQuantity.value = 1;
	destinationQuantity.name = "destinations["+num+"][quantity]";
	var destinationSelector = $$('#'+destId+' select')[0];
	destinationSelector.selectedIndex = 0;
	destinationSelector.id = "dropdown_"+num;
	destinationSelector.name = "destinations["+num+"][address_id]";
	
	// Change a few things about the new destination row to put it in default state.
	$$('#'+newDestId+' input')[0].name.replace('[1]', '['+num+']');
	$$('#'+newDestId+' input')[0].value = '';
}