
/* Moves a row from one table section to another.  The tables sections can be
 * in different tables or the same table.  Making changes can have unintended
 * results if you are not careful as the index of the row could be changed and
 * the incorrect row may be delted.
 */
function FRL_move_table_row(row_id, source_table_section_id, destination_table_section_id, destination_index){
	var src = document.getElementById(source_table_section_id);
	var dst = document.getElementById(destination_table_section_id);
	//get the index for the row in the source table
	row_index = null;
	for(i = 0; i < src.rows.length; i++){
		if(src.rows[i].id == row_id){
			row_index = i;
			i = 1 + src.rows.length;
		}
	}
	if(row_index == null) {
		alert("Error: Move failed because the row was not found in specified table");
		return false;
	}
	//create the new destination row
	if(destination_index >= 0){
		var ntr = dst.insertRow(destination_index);
	}
	else {
		var ntr = dst.insertRow(dst.rows.length);
	}
	for(i = 0; i < src.rows[row_index].cells.length; i++){
		ntr.cells[i] = src.rows[row_index].cells[i];
	}
	//var tmp = src.rows[row_index].innerHTML;
	//ntr.innerHTML = tmp;
	tmp = src.rows[row_index].id;
	ntr.id = tmp;
	src.deleteRow(row_index);
}

/* Removes a row from a table using its id */
function FRL_remove_table_row(row_id, source_table_section_id){
	var src = document.getElementById(source_table_section_id);
	//get the index for the row in the source table
	row_index = null;
	for(i = 0; i < src.rows.length; i++){
		if(src.rows[i].id == row_id){
			row_index = i;
			i = 1 + src.rows.length;
		}
	}
	if(row_index == null) {
		alert("Error: Delete failed because the row was not found in specified table");
		return false;
	}
	src.deleteRow(row_index);
}


/* Move a row from one table to another
 *  - src_table_section: the table section that currently contains the
 *         row to be moved
 *  - dest_table_section: the table section that will contains the
 *         row once it is moved
 *  - orig_index: the index of the row to move in the source table
 *  - dest_index: the index of the row to insert the row before; if
 *         not specified the row will be appended to the end of the
 *         destination table.
 */
function FRL_move_tr(src_table_section, dest_table_section, orig_index, dest_index) {
	var src = document.getElementById(src_table_section);
	var dest = document.getElementById(dest_table_section);

// validate and set default values
	if(!src){
		Error("Unrecgnized Source Table Section ID in call to FRL_shift_tr().")
		return false;
	}
	if(!dest){
		Error("Unrecgnized Source Table Section ID in call to FRL_shift_tr().")
		return false;
	}
	// orig_index must be in the valid range of src
	orig_index = parseInt(orig_index);
	if((!orig_index && orig_index != 0) || orig_index > src.rows.length || orig_index < 0) {
		alert("Row index out of range in call to FRL_shift_tr()");
		return false;
	}
	// orig_index must be in the valid range of src
	dest_index = parseInt(dest_index);
	if((!dest_index && dest_index != 0) || dest_index > src.rows.length) {
		dest_index = dest.rows.length;
	}
	else if (orig_index < 0) {
		dest_index = 0;
	}
	dest.insertRow(dest_index);
	dest.replaceChild(src.rows[orig_index], dest.rows[dest_index]);
}


/* Shift the position of a row within a table.
 *  - table_section: the id of the tbody, tfoot, thead containing the
 *         row to be shifted
 *  - orig_indes: the index of the row to move
 *  - direction: Negative (or 0) to move to the top of the table or  
 *         positive to move tow the bottom of the table
 *  - steps: the number of rows to shift by; if shifting the specified
 *         steps would cause the row to be moved outside the bounds of
 *         of the table section 0 (if direction is negative) or the 
 *				 src.rows.length (if the direction is positive) is unsed.
 */
function FRL_shift_tr(table_section, orig_index, direction, steps) {
	var src = document.getElementById(table_section);

// validate and set default values
	if(!src){
		Error("Unrecgnized Table Section ID in call to FRL_shift_tr().")
		return false;
	}
	// orig_index must be in the valid range of src
	orig_index = parseInt(orig_index);
	if((!orig_index && orig_index != 0) || orig_index > src.rows.length || orig_index < 0) {
		alert("Row index out of range in call to FRL_shift_tr()");
		return false;
	}

	steps = parseInt(steps);
	if (!steps || steps < 1) {
		steps = 1;
	}
	
	direction = parseInt(direction);
	//simplify the direction to either 1 or -1
	if (!direction || direction <= 0) {
		direction = -1;
	}
	else {
		direction  = 1;
	}

	// calculate the destiantion index
	var dest_index = orig_index + direction * steps;
	
	// clone the row to be moved so it is not lost
	var original = src.rows[orig_index].cloneNode(true);

	// delete the old copy of the row
	src.deleteRow(orig_index);

	/* The destination index must be within the bounds of the table AFTER
	 * the old copy has been removed.
	 */
	if (dest_index < 0) {
		dest_index = 0;
	}
	else if(dest_index > src.rows.length) {
		dest_index = src.rows.length;
	}

	// insert a blank row at the new location and replace it with the clone
	src.insertRow(dest_index);
	src.replaceChild(original, src.rows[dest_index]);
}
