/**
  * Toggle Destination Other
  *
  * Toggles whether or not to enable the "other" input
  * field to indicate a name for the other destination.
  * 
  * @author Matt James <matt@sephone.com>
  * @param  select element
  * @return void
  */
function toggleDestinationOther(selectElement){
	selectElement = $(selectElement);

	if ($F(selectElement) == "other") {
		if ($('container_'+selectElement.id).style.display == 'none') {
			selectElement.selectedIndex = 0;
		}
		Effect.toggle('container_'+selectElement.id, 'appear', {duration: 0.30});
		Effect.toggle('other_container_'+selectElement.id, 'appear', {duration: 0.30});
	}
	
	if (selectElement.selectedIndex == 0) {
		// We want the ship_to_self flag set because this item is going to the same
		// address that will be populated by the Shopper Info.
		$('ship_to_self_'+selectElement.id).value = 1;
	} else {
		// We do not want the ship_to_self flag set because we aren't shipping to self.
		$('ship_to_self_'+selectElement.id).value = 0;
	}
}

/**
  * Remove Destination Row
  *
  * Removes the row indicated by the given increment from the list.
  * 
  * @author Matt James <matt@sephone.com>
  * @param  increment number
  * @return void
  */
function removeDestinationRow(increment){
	$('row_'+increment).remove();
}


/**
  * Add Destination Row
  *
  * Adds a new row to specify a quantity and destination.
  * 
  * @author Matt James <matt@sephone.com>
  * @param  void
  * @return void
  */
function addDestinationRow(){		
	var newRow = $$('#destination_rows .row')[0].cloneNode(true);		
	var destinationRows = $('destination_rows');
	
	var totalRows = $$('#destination_rows .row').length;
	var increment = parseInt($$('#destination_rows .row')[totalRows-1].id.split('_')[1]) + 1;
	newRow.id = "row_"+increment;
	destinationRows.appendChild(newRow);
	
	var removeImage = $$('#'+newRow.id+' img')[0];
	removeImage.id = "remove_"+increment;
	removeImage.removeClassName("hidden");
	$$('#'+newRow.id+' img')[1].addClassName("hidden");
	
	var quantity = $$('#'+newRow.id+' input.quantity')[0];
	quantity.id = "quantity"+increment;
	quantity.name = "items["+increment+"][qty]";
	quantity.value = 1;
	
	var shipToSelf = $$('#'+newRow.id+' input.ship_to_self')[0];
	shipToSelf.id = "ship_to_self_destination"+increment;
	shipToSelf.name = "items["+increment+"][shipToSelf]";
	shipToSelf.value = 1;
	
	var productId = $$('#'+newRow.id+' input.product_id')[0];
	productId.id = "product_id_destination"+increment;
	productId.name = "items["+increment+"][product_id]";
	
	var destinationContainer = $$('#'+newRow.id+' div.container_destination')[0];
	destinationContainer.id = "container_destination"+increment;
	Element.setStyle(destinationContainer, {display: ''});
	
	var otherDestinationContainer = $$('#'+newRow.id+' div.other_container_destination')[0];
	otherDestinationContainer.id = "other_container_destination"+increment;
	Element.setStyle(otherDestinationContainer, {display: 'none'});
	
	var destinationSelect = $$('#'+newRow.id+' select')[0];
	destinationSelect.id = "destination"+increment;
	destinationSelect.name = "items["+increment+"][address_id]";
	destinationSelect.selectedIndex = 0;
	
	var otherDestination = $$('#'+newRow.id+' input.other_destination')[0];
	otherDestination.id = "other_destination"+increment;
	otherDestination.name = "items["+increment+"][name]";
	otherDestination.value = '';
	
	var toggleLink = $$('#'+newRow.id+' div.other_container_destination a')[0];
	toggleLink.id = "link_toggle_destination"+increment;
}