// JavaScript Document:  listreorder.js

//Global var denoting current AJAX operation
var ajaxaction= "";

//Global vars for moving objects up and down
var up_down= '';
var item_position= '';
var item_source= '';
var item_target= '';
var item_count= 0;
var item_type= '';
var moved_item= '';

function jsAJAXStart(action,element) {
	//The action is recorded to the global variable so jsAJAXEnd knows what to do with the AJAX_response XML
	
	ajaxaction= action;

	
	//Determine if element is a string (the value itself) or an object you need to retrieve the value from
	if (typeof(element)== 'object')
		{
			var url_parameter= element.value;
		}	
	else
		{
			var url_parameter= element;
		}
	
	//Action also determines the URL the request is geared towards
	switch (action)
		{
			
			case "items":
				jsAJAXLoad('http://www.swartzfager.org/blog/ajax_listreorder.cfm?switch_ids=' + url_parameter);
				break;
				
		} //end of switch
	
} //end of function


function jsAJAXLoad(url) {
	// branch for native XMLHttpRequest object
	if ( window.XMLHttpRequest ) 
		{
			req = new XMLHttpRequest();
			//the call to jsAJAXEnd CANNOT have parans ()
			req.onreadystatechange = jsAJAXEnd;
			req.open("GET", url, true);
			req.send(null);
		// branch for IE/Windows ActiveX version
		} 
	else if ( window.ActiveXObject ) 
		{
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if ( req ) 
				{
					//the call to jsAJAXEnd CANNOT have parans ()
					req.onreadystatechange = jsAJAXEnd;
					req.open("GET", url, true);
					req.send();
				}
		}
} //end of function

function jsAJAXEnd() {
		
	if (req.readyState == 4 ) 
		{		
			if (req.status == 200) 
				{		
					//Get XML-formatted response from the server
					var response = req.responseXML;
					switch (ajaxaction)
						{
							
							case "items":
								//Preserve value of global var up_down to current_move
								current_move= up_down;
								//Reset up_down
								up_down= '';
								
								//Retrieve the result of the AJAX call (indicating either success or failure)
								answer_array= response.getElementsByTagName("result");
								if (answer_array[0].firstChild.data== 'Success')
									{
										if (current_move== 'up')
											{
												ajaxFinishMoveUp();
											}
										else if (current_move== 'down')
											{
												ajaxFinishMoveDown();
											}
									}
								
								break;
								
						} //end of switch
				}  //end of req.status= 200
			
		}  //end of req.readyState= 4
	
} //end of function



/* ***********  Move Up/Down AJAX Functions ***************** */

function ajaxStartMoveUp(position,id,previous,itemtype) {
	/*
	USAGE:  Starting function for moving an item up one position in a list of items, using AJAX to update the item order in the database table/data file at the same time.
	PARAMETERS:
	position:  the position of the item in the list of items.   Starts at 1 for the first item in the list
	id:  the unique identifier for the item in the database table/data file.
	previous:  the unique identifier for the item in the database table/data file that PRECEDES the item selected to move up in the item list (the "source" item).
	itemtype:  a string or number used to identify what items are being moved and therefore indicate what functions should be executed when the AJAX call has completed (allows you to use this functionality for different lists of items in your application)
	*/
	
	//First, make sure that any other positional movements have completed by checking to see if the global var "up_down" has been reset
	if (up_down== '')
		{
			//Save parameters to global vars
			item_position= position;
			item_source= id;
			item_target= previous;
			item_type= itemtype;
			
			//Set up_down global var.  This will now prevent any other movement actions from occuring until this current one has completed.
			up_down= 'up';
			
			//Create a compound value that will be sent to jsAJAXStart and from there to the server-side script that will switch the order values of the two items.
			compound= id + "_" + previous;
			
			//Use the itemtype to determine the action parameter sent to jsAJAXStart (see the comments in the jsAJAXStart function for what role the action parameter plays).
			if (itemtype== 'items')
				{
					jsAJAXStart('items',compound);
				}
			
		}
			
 }  //end of function


function ajaxStartMoveDown(position,id,next,itemtype) {
	/*
	USAGE:  Starting function for moving an item down one position in a list of items, using AJAX to update the item order in the database table/data file at the same time.
	PARAMETERS:
	position:  the position of the item in the list of items.   Starts at 1 for the first item in the list
	id:  the unique identifier for the item in the database table/data file.
	next:  the unique identifier for the item in the database table/data file that FOLLOWS the item selected to move down in the item list (the "source" item).
	itemtype:  a string or number used to identify what items are being moved and therefore indicate what functions should be executed when the AJAX call has completed (allows you to use this functionality for different lists of items in your application)
	*/
	
	//First, make sure that any other positional movements have completed by checking to see if the global var "up_down" has been reset
	if (up_down== '')
		{
			//Save parameters to global vars
			item_position= position;
			item_source= id;
			item_target= next;
			item_type= itemtype;
			
			//Set up_down global var.  This will now prevent any other movement actions from occuring until this current one has completed.
			up_down= 'down';
			
			//Create a compound value that will be sent to jsAJAXStart and from there to the server-side script that will switch the order values of the two items.
			compound= id + "_" + next;
			
			//Use the itemtype to determine the action parameter sent to jsAJAXStart (see the comments in the jsAJAXStart function for what role the action parameter plays).
			if (itemtype== 'items')
				{
					jsAJAXStart('items',compound);
				}
			
		}
			
 }  //end of function




function ajaxFinishMoveUp() {
	 /*
	USAGE:  Function that completes the process of moving an item up one position in a list of items.  This process involves making control changes (changes to the up/down controls), visible changes (to elements that users expect to visibly change as a result of the move, like the name of the item) and invisible changes (like id variables in hyperlink URLs).
	*/
	 
	 //Retrieve values from global vars
	 position= item_position;
	 source_item_id= item_source;
	 target_item_id= item_target;
	 itemcount= item_count;
	 
	 //Call function to update controls
	 updateMoveUpControls(position,source_item_id,target_item_id,itemcount,'ajax');
	 
	 //Call function to update item title.  Pass the position and the direction of the movement of the selected item
	 updateItemTitles(position,'up');
	 
	 //Call function to update other visible changes (if there are any)
	 moveSwitchTextNodes(position,'item_type_','up');
	 
	 //Call function to update invisible changes (if there are any)
	 moveSwitchHyperlinkHrefs(position,'item_answer_','up');
	 
	  /*To make it easier for the user to see that the item has shifted up, remove the highlight background color of the previously-moved item (if there is one), then highlight the moved one.  If you change the syntax/name of the element id in your display page, be sure to update it here as well.
	 Note:  you could highlight or draw attention to the entire "row" that makes up the item, but note that if your items are contained in a <table>, you cannot change the border or background color of a row dynamically in IE or Netscape.
	 */
	 
	 if (moved_item != '')
	 	{
			past_row_id= 'item_title_' + moved_item;
			jsChangeNodeClass(past_row_id,'bgcolor_white');
		}
	moved_item_id= 'item_title_' + (parseInt(position)-1);
	jsChangeNodeClass(moved_item_id,'bgcolor_yellow');
	moved_item= (parseInt(position)-1);
	
} //end of function


function ajaxFinishMoveDown() {
	 /*
	USAGE:  Function that completes the process of moving an item down one position in a list of items.  This process involves making control changes (changes to the up/down controls), visible changes (to elements that users expect to visibly change as a result of the move, like the name of the item) and invisible changes (like id variables in hyperlink URLs).
	*/
	 
	 //Retrieve values from global vars
	 position= item_position;
	 source_item_id= item_source;
	 target_item_id= item_target;
	 itemcount= item_count;
	 
	 //Call function to update controls
	 updateMoveDownControls(position,source_item_id,target_item_id,itemcount,'ajax');
	 
	 //Call function to update item title.  Pass the position and the direction of the movement of the selected item
	 updateItemTitles(position,'down');
	 
	  //Call function to update other visible changes (if there are any)
	 moveSwitchTextNodes(position,'item_type_','down');
	
	 //Call function to update invisible changes (if there are any)
	 moveSwitchHyperlinkHrefs(position,'item_answer_','down');
	 	 
	 /*To make it easier for the user to see that the item has shifted down, remove the highlight background color of the previously-moved item (if there is one), then highlight the moved one.  If you change the syntax/name of the element id in your display page, be sure to update it here as well.
	 Note:  you could highlight or draw attention to the entire "row" that makes up the item, but note that if your items are contained in a <table>, you cannot change the border or background color of a row dynamically in IE or Netscape.
	 */
	 
	 if (moved_item != '')
	 	{
			past_row_id= 'item_title_' + moved_item;
			jsChangeNodeClass(past_row_id,"bgcolor_white");
		}
	moved_item_id= 'item_title_' + (parseInt(position)+1);
	jsChangeNodeClass(moved_item_id,'bgcolor_yellow');
	moved_item= (parseInt(position)+1);	 
	
} //end of function


/* ***********  Move Up/Down Non-AJAX Functions ***************** */

function nonAjaxMoveUp(position,id,previous,itemtype) {
	/*
	USAGE:  Starting function for moving an item up one position in a list of items, using a hidden text field to preserve the order change for a form submission.
	PARAMETERS:
	position:  the position of the item in the list of items.   Starts at 1 for the first item in the list
	id:  the unique identifier for the item in the database table/data file.
	previous:  the unique identifier for the item in the database table/data file that PRECEDES the item selected to move up in the item list (the "source" item).
	itemtype:  a string or number used to identify what items are being moved and therefore indicate what functions should be executed (allows you to use this functionality for different lists of items in your application).
	*/
	
	if (up_down== '')
		{
			
			//Set up_down global var
			up_down= 'up';
			
			//Call the function that records the change of order to the hidden text box.
			reorderItemList(previous,id);
			
			//Retrieve item count value from global var
	 		itemcount= item_count;
	 
	 		//Call function to update controls
	 		updateMoveUpControls(position,id,previous,itemcount,'form');
	 
	 		//Call function to update item title.  Pass the position and the direction of the movement of the selected item
	 		updateItemTitles(position,'up');
	 
	  		//Call function to update other visible changes (if there are any)
			moveSwitchTextNodes(position,'item_type_','up');
	
	 		//Call function to update invisible changes (if there are any)
	 		moveSwitchHyperlinkHrefs(position,'item_answer_','up');
	 	 
		 /*To make it easier for the user to see that the item has shifted down, remove the highlight background color of the previously-moved item (if there is one), then highlight the moved one.  If you change the syntax/name of the element id in your display page, be sure to update it here as well.
	 	Note:  you could highlight or draw attention to the entire "row" that makes up the item, but note that if your items are contained in a <table>, you cannot change the border or background color of a row dynamically in IE or Netscape.
	 */
	 
	 	if (moved_item != '')
	 		{
				past_row_id= 'item_title_' + moved_item;
				jsChangeNodeClass(past_row_id,'bgcolor_white');
			}
		moved_item_id= 'item_title_' + (parseInt(position)-1);
		jsChangeNodeClass(moved_item_id,'bgcolor_yellow');
		moved_item= (parseInt(position)-1);
	
		//Reset up_down
		up_down= '';
		
	}  
	
}  //end of function


function nonAjaxMoveDown(position,id,previous,itemtype) {
	/*
	USAGE:  Starting function for moving an item up one position in a list of items, using a hidden text field to preserve the order change for a form submission.
	PARAMETERS:
	position:  the position of the item in the list of items.   Starts at 1 for the first item in the list
	id:  the unique identifier for the item in the database table/data file.
	previous:  the unique identifier for the item in the database table/data file that PRECEDES the item selected to move up in the item list (the "source" item).
	itemtype:  a string or number used to identify what items are being moved and therefore indicate what functions should be executed (allows you to use this functionality for different lists of items in your application).
	*/
	
	if (up_down== '')
		{
			
			//Set up_down global var
			up_down= 'down';
			
			//Call the function that records the change of order to the hidden text box.
			reorderItemList(id,previous);
			
			//Retrieve item count value from global var
	 		itemcount= item_count;
	 
	 		//Call function to update controls
	 		updateMoveDownControls(position,id,previous,itemcount,'form');
	 
	 		//Call function to update item title.  Pass the position and the direction of the movement of the selected item
	 		updateItemTitles(position,'down');
	 
	  		//Call function to update other visible changes (if there are any)
			moveSwitchTextNodes(position,'item_type_','down');
	
	 		//Call function to update invisible changes (if there are any)
	 		moveSwitchHyperlinkHrefs(position,'item_answer_','down');
	 	 
		 /*To make it easier for the user to see that the item has shifted down, remove the highlight background color of the previously-moved item (if there is one), then highlight the moved one.  If you change the syntax/name of the element id in your display page, be sure to update it here as well.
	 	Note:  you could highlight or draw attention to the entire "row" that makes up the item, but note that if your items are contained in a <table>, you cannot change the border or background color of a row dynamically in IE or Netscape.
	 */
	 
	 	if (moved_item != '')
	 		{
				past_row_id= 'item_title_' + moved_item;
				jsChangeNodeClass(past_row_id,"bgcolor_white");
			}
		moved_item_id= 'item_title_' + (parseInt(position)+1);
		jsChangeNodeClass(moved_item_id,'bgcolor_yellow');
		moved_item= (parseInt(position)+1);	 
	
		//Reset up_down
		up_down= '';
		
	}  
	
}  //end of function



function reorderItemList(upper,lower) {
	/*
	USAGE:  Used when AJAX cannot be used to update the data, this function updates a comma-delimited ordered list of item IDs in a hidden text field to reflect the change in position of two items
	PARAMETERS:
	upper:  the ID of the two items being reordered that is closest to the start of the list.
	lower:  the ID of the two items being reordered that is closest to the end of the list.
	*/
	
	//Get hidden text field containing new order of item IDs
	reorder_obj= document.getElementById('new_order');
	reorder_list= reorder_obj.value;
	
	//Convert the comma-delimited list to an array
	reorder_array= reorder_list.split(',');
	
	//Loop through array.  When the loop encounters the upper item's ID, it will replace it with the lower item's ID, and vice-versa.  The end result is that the two IDs swap positions in the array.
	for (i= 0; i < reorder_array.length; i++)
		{
			if (reorder_array[i]== upper)
				{
					reorder_array[i]= lower;
				}
			else if (reorder_array[i]== lower)
				{
					reorder_array[i]= upper;	
				}
		}
	//Convert the array back to a comma-delimted list
	new_list= reorder_array.join();
	//Write the new list back to the text field
	reorder_obj.value= new_list;
	
 }
 

/* *************************************** The Heavy Lifters ********************************************* */

function updateMoveUpControls(position,source_item_id,target_item_id,last_position,updatetype) {
	/*
	USAGE:  Changes the CSS class for the selected item and the item directly above it, and changes the JavaScript hyperlinks for 6 movement controls:
		--the Up and Down controls of the selected item
		--the Up and Down controls item directly above the selected item
		--the Up control of the item directly below the selected item
		--the Down control of the item above the item directly above the selected item (if there is one)
	PARAMETERS:
	position:  the position of the selected item in the list of items.
	source_item_id:  the unique identifier for the selected item in the database table/data file.
	target_item_id:  the unique identifier for the item above the selected item in the database table/data file.
	*/
	
	//Determine how many elements need to be changed based on the position of the source and target items in the list.
	source_position= position;
	target_position= parseInt(position)-1;
	
	//If the source item is not the last item in the list, then we need to get the position number of the element below the source item.
	if (source_position < last_position)
		{
			bottommost_position= parseInt(position)+1;
		}
		
	//If the target item is not the first item in the list, then we need to get the postion number of the element above the target items (2 items about the source item)
	if (target_position > 1)
		{
			uppermost_position= parseInt(position)-2;
		}
	
	//Run the getMovementParameters function to parse the javascript link and get the first 3 parameters (position,source,target) of the needed JavaScript hyperlinks.  
	source_parameters_Up= getMovementParameters(source_position,'up');
	source_parameters_Down= getMovementParameters(source_position,'down');
	target_parameters_Up= getMovementParameters(target_position,'up');
	target_parameters_Down= getMovementParameters(target_position,'down');
	if (typeof(bottommost_position) != 'undefined')
		{
			bottommost_parameters_Up= getMovementParameters(bottommost_position,'up');
		}
	if (typeof(uppermost_position) != 'undefined')
		{
			uppermost_parameters_Down= getMovementParameters(uppermost_position,'down');
		}
			
			
	//Now start switching the parameters to indicate the new up and down targets of the source item and the target item.  Array map:  [0]= position, [1]= item id, [2]= previous or next item id (depending on if the control is an Up or Down control)
	new_source_parameters_Up= [source_parameters_Up[0],target_parameters_Up[1],source_parameters_Up[1]];
	new_source_parameters_Down= [source_parameters_Up[0],target_parameters_Up[1],source_parameters_Down[2]];
	new_target_parameters_Up= [target_parameters_Up[0],source_parameters_Up[1],target_parameters_Up[2]];
	new_target_parameters_Down= [target_parameters_Up[0],source_parameters_Up[1],target_parameters_Down[1]];

	//Now switch the parameters to indicate the new Up target on the bottommost item and the Down target on the uppermost item (if they exist)
	if (typeof(bottommost_position) != 'undefined')
		{
			new_bottommost_parameters_Up= [bottommost_parameters_Up[0],bottommost_parameters_Up[1],target_parameters_Up[1]];
		}
	if (typeof(uppermost_position) != 'undefined')
		{
			new_uppermost_parameters_Down= [uppermost_parameters_Down[0],uppermost_parameters_Down[1],source_parameters_Down[1]];
		}
		
	//Now write them back to each of the hyperlinks
	rewriteMovementCall(source_position,'up',new_source_parameters_Up,updatetype);
	rewriteMovementCall(source_position,'down',new_source_parameters_Down,updatetype);
	rewriteMovementCall(target_position,'up',new_target_parameters_Up,updatetype);
	rewriteMovementCall(target_position,'down',new_target_parameters_Down,updatetype);
	if (typeof(bottommost_position) != 'undefined')
		{
			rewriteMovementCall(bottommost_position,'up',new_bottommost_parameters_Up,updatetype);
		}
	if (typeof(uppermost_position) != 'undefined')
		{
			rewriteMovementCall(uppermost_position,'down',new_uppermost_parameters_Down,updatetype);
		}
			
} //end of function


function updateMoveDownControls(position,source_item_id,target_item_id,last_position,updatetype) {
	/*
	USAGE:  Changes the CSS class for the selected item and the item directly below it, and changes the JavaScript hyperlinks for 6 movement controls:
		--the Up and Down controls of the selected item
		--the Up and Down controls item directly below the selected item
		--the Up control of the item directly below the item directly below the selected item (if there is one)
		--the Down control of the item above the selected item directly above the selected item
	PARAMETERS:
	position:  the position of the selected item in the list of items.
	source_item_id:  the unique identifier for the selected item in the database table/data file.
	target_item_id:  the unique identifier for the item above the selected item in the database table/data file.
	last_position:  the position number that belongs to the last item in the list.
	*/
	
	//Determine how many elements need to be changed based on the position of the source and target items in the list.
	source_position= position;
	target_position= parseInt(position)+1;
	
	//If the source item is not the first item in the list, then we need to get the position number of the element above the source item.
	if (source_position > 1)
		{
			uppermost_position= parseInt(position)-1;
		}
	
	//If the target item is not the last item in the list, then we need to get the position number of the element below the target item (2 items down from the source item)
	if (target_position < last_position)
		{
			bottommost_position= parseInt(position)+2;
		}
	
	//Run the getMovementParameters function to parse the javascript link and get the first 3 parameters (position,source,target) of the needed JavaScript hyperlinks.  
	source_parameters_Up= getMovementParameters(source_position,'up');
	source_parameters_Down= getMovementParameters(source_position,'down');
	target_parameters_Up= getMovementParameters(target_position,'up');
	target_parameters_Down= getMovementParameters(target_position,'down');
	if (typeof(bottommost_position) != 'undefined')
		{
			bottommost_parameters_Up= getMovementParameters(bottommost_position,'up');
		}
	if (typeof(uppermost_position) != 'undefined')
		{
			uppermost_parameters_Down= getMovementParameters(uppermost_position,'down');
		}
		
	//Now start switching the parameters to indicate the new up and down targets of the source item and the target item.  Array map:  [0]= position, [1]= item id, [2]= previous or next item id (depending on if the control is an Up or Down control)
	new_source_parameters_Up= [source_parameters_Up[0],target_parameters_Up[1],source_parameters_Up[2]];
	new_source_parameters_Down= [source_parameters_Up[0],target_parameters_Up[1],source_parameters_Up[1]];
	new_target_parameters_Up= [target_parameters_Up[0],source_parameters_Up[1],target_parameters_Up[1]];
	new_target_parameters_Down= [target_parameters_Up[0],source_parameters_Up[1],target_parameters_Down[2]];
	
	//Now switch the parameters to indicate the new Up target on the bottommost item and the Down target on the uppermost item (if they exist)
	if (typeof(bottommost_position) != 'undefined')
		{
			new_bottommost_parameters_Up= [bottommost_parameters_Up[0],bottommost_parameters_Up[1],source_parameters_Up[1]];
		}
	if (typeof(uppermost_position) != 'undefined')
		{
			new_uppermost_parameters_Down= [uppermost_parameters_Down[0],uppermost_parameters_Down[1],target_parameters_Up[1]];
		}
			
	//Now write them back to each of the hyperlinks
	rewriteMovementCall(source_position,'up',new_source_parameters_Up,updatetype);
	rewriteMovementCall(source_position,'down',new_source_parameters_Down,updatetype);
	rewriteMovementCall(target_position,'up',new_target_parameters_Up,updatetype);
	rewriteMovementCall(target_position,'down',new_target_parameters_Down,updatetype);
	if (typeof(bottommost_position) != 'undefined')
		{
			rewriteMovementCall(bottommost_position,'up',new_bottommost_parameters_Up,updatetype);
		}
	if (typeof(uppermost_position) != 'undefined')
		{
			rewriteMovementCall(uppermost_position,'down',new_uppermost_parameters_Down,updatetype);
		}
			
		
} //end of function



function getMovementParameters(position_number,direction_type) {
	/*
	USAGE:  Parses the hyperlink of the movement control identified by the position_number and the direction_type and returns an array
	PARAMETERS:
	position_number:  the position number part of the id of the hyperlink element.  Identifies movement control along with direction_type.
	direction_type:  the direction part of the id of the hyperlink element.  Identifies movement control along with position_number
	*/
	
	//If you change the syntax/name of the element id in your display page, be sure to update it here as well
	link_id= 'item_' + direction_type + '_' + position_number;
	
	//Get the hyperlink object
	link_obj= document.getElementById(link_id);
	
	//Get the href
	link_href= link_obj.href;
	
	//Start parsing the href.  The syntax is in the form "javascript:{function name}({position},{id},{next or previous item's id},{itemtype});", so use two split functions to break up the href into the needed pieces
	comma_division= link_href.split(',');  
	functioncall_and_first_parameter= comma_division[0].split('(');  //gives us 'javascript' and '2'
	
	//Create the array of parameters
	params= new Array(functioncall_and_first_parameter[1],comma_division[1],comma_division[2]);
	
	//Return the array
	return params
	
} //end of function

function rewriteMovementCall(position_number,direction_type,param_array,updatetype) {
	/*
	USAGE:  Creates the hyperlink of the movement control identified by the position_number and the direction_type using the passed array
	PARAMETERS:
	position_number:  the position number part of the id of the hyperlink element.  Identifies movement control along with direction_type.
	direction_type:  the direction part of the id of the hyperlink element.  Identifies movement control along with position_number
	param_array:  the array of parameters to use in the string
	*/
	
	//If you change the syntax/name of the element id in your display page, be sure to update it here as well
	link_id= 'item_' + direction_type + '_' + position_number;
	
	//Create the link based on the direction_type and the global var item_type
	if (direction_type== 'up')
		{
			if (updatetype== 'ajax')
				{
					new_link_href= "javascript:ajaxStartMoveUp(" + param_array[0] + "," + param_array[1] + "," + param_array[2] + ",'" + item_type + "');";
				}
			else
				{
					new_link_href= "javascript:nonAjaxMoveUp(" + param_array[0] + "," + param_array[1] + "," + param_array[2] + ",'" + item_type + "');";
				}
		}
	else
		{
			if (updatetype== 'ajax')
				{
					new_link_href= "javascript:ajaxStartMoveDown(" + param_array[0] + "," + param_array[1] + "," + param_array[2] + ",'" + item_type + "');";
				}
			else
				{
					new_link_href= "javascript:nonAjaxMoveDown(" + param_array[0] + "," + param_array[1] + "," + param_array[2] + ",'" + item_type + "');";
				}
		}
	
	//Rewrite the href value of the object
	jsChangeHref(link_id,new_link_href);
	
} //end of function


function updateItemTitles(position,direction) {
	/*
	USAGE:  Swaps the nodes contained in the selected item's "title" node with the nodes in the target item's "title" node
	PARAMETERS:
	position_number:  the position number part of the id of the item's "title" element.
	direction:  'up' or 'down', indicating whether the target is one position lower/above or one position higher/below the selected item.
	*/
	
	//If you change the syntax/name of the element's title id in your display page, be sure to update it here as well
	source_title_id= 'item_title_' + position;
	
	if (direction== 'up')
		{
			target_position= parseInt(position)-1;
		}
	else 
		{
			target_position= parseInt(position)+1;
		}
		
	target_title_id= 'item_title_' + target_position;
	
	source_obj= document.getElementById(source_title_id);
	target_obj= document.getElementById(target_title_id);

	clone1= source_obj.childNodes[0].cloneNode(true);
	clone2= target_obj.childNodes[0].cloneNode(true);
		
	source_obj.removeChild(source_obj.childNodes[0]);
	target_obj.removeChild(target_obj.childNodes[0]);
	target_obj.appendChild(clone1);
	source_obj.appendChild(clone2);
	
	
} //end of function
	
function moveSwitchTextNodes(source_position,static_id,direction) {
	/*
	USAGE:  Swaps the text in the selected nodes of the source and target items
	PARAMETERS:
	source_position:  the position number part of the id of the source/selected item's elements.
	static_id:  the textual component of the id, the part that is the same for all instances of the element in the list
	direction:  'up' or 'down', indicating whether the target is one position lower/above or one position higher/below the selected item.
	*/
	
	//First, get the position number of the target element depending on the direction
	if (direction== 'up')
		{
			target_position= parseInt(source_position)-1;
		}
	else
		{
			target_position= parseInt(source_position)+1;
		}
		
	//Now create the ids of the elements containing text nodes that should be switched.  If the static_id part of the id is not the prefix of the id, remember to alter the statments just below accordingly.
	source_id= static_id + source_position;
	target_id= static_id + target_position;
	
	
	//Now get the corresponding elements
	source_obj= document.getElementById(source_id);
	target_obj= document.getElementById(target_id);
	
	//Use childNodes reference to update the text (the text node is the first child element within the selected page element).  Works in IE6, IE7, and FireFox.
	
	//Store the source text in an intermediate variable
	source_text= source_obj.childNodes[0].nodeValue
	//Now give the source object the text of the target object
	source_obj.childNodes[0].nodeValue= target_obj.childNodes[0].nodeValue;
	//Now give the target object the text stored in the intermediate variable
	target_obj.childNodes[0].nodeValue= source_text;
			
	
}  //end of function 

function moveSwitchHyperlinkHrefs(source_position,static_id,direction) {
	/*
	USAGE:  Swaps the href attribute values in the selected <a> nodes of the source and target items
	PARAMETERS:
	source_position:  the position number part of the id of the source/selected item's elements.
	static_id:  the textual component of the id, the part that is the same for all instances of the element in the list
	direction:  'up' or 'down', indicating whether the target is one position lower/above or one position higher/below the selected item.
	*/
	
	//First, get the position number of the target element depending on the direction
	if (direction== 'up')
		{
			target_position= parseInt(source_position)-1;
		}
	else
		{
			target_position= parseInt(source_position)+1;
		}
	
	//Now create the ids of the elements containing text nodes that should be switched.  If the static_id part of the id is not the prefix of the id, remember to alter the statments just below accordingly.
	source_id= static_id + source_position;
	target_id= static_id + target_position;
	
	//Now get the corresponding elements
	source_obj= document.getElementById(source_id);
	target_obj= document.getElementById(target_id);
	
	//Store the source href in an intermediate variable
	source_href= source_obj.href;
	//Now give the source object the href of the target object
	source_obj.href= target_obj.href;
	//Now give the target object the href stored in the intermediate variable
	target_obj.href= source_href;
	
}

