// Written By Clint Furse for Boxworks Technologies
// July 2006

// to run this script you need to include a few things in your page
// 1.	var imgpath - set to the path containing up/dn arrow images ***(should now be included in header)***
// 2.	hidden input fld named 'ords', this will save the order when the save button is pressed
// 3.   rows to order must include attributes: 
//		id = [1 - ?] (global index for all rows used to determine row color)
//		curord = [?] (will need to be a unique value and will passed in the form by the save function)
//		grpcnt = [1 - ?] (will help determine how far down the row can be moved)
//		grpid = [1 - ?] (the index for the current group)
// 4.   the table cells that contain the up/dn arrows, need to have id="up" or id="dn"
// 5.	up/dn images that call javascript functions rowDn/rowUp(rowid) with the id of the current row (<tr>)
// 6.	Save button that calls the javascript function save(table) with the current table as the paramter.

function rowUp(rowid){
  	var cur_row = document.getElementById(rowid);
  	var parent = cur_row.parentNode;  	
  	var prev_row = node_before(cur_row);
  	var cur_row = parent.removeChild(cur_row);
  	var id = cur_row.id;
  	var grpid = cur_row.getAttribute("grpid");
  	parent.insertBefore(cur_row,prev_row);
  	cur_row.id = prev_row.id;
  	cur_row.setAttribute("grpid", prev_row.getAttribute("grpid"));
  	prev_row.id = id;
  	prev_row.setAttribute("grpid", grpid);
  	
  	colorRows(cur_row);
  	
  	drawCells(cur_row);
  	drawCells(prev_row);
}
  
function rowDn(rowid){
  	var cur_row = document.getElementById(rowid);
  	var parent = cur_row.parentNode;
  	var next_row = node_after(cur_row);
  	var cur_row = parent.removeChild(cur_row);
  	var id = cur_row.id;
  	var grpid = cur_row.getAttribute("grpid");
  	parent.insertBefore(cur_row,node_after(next_row));
  	cur_row.id = next_row.id;
  	cur_row.setAttribute("grpid", next_row.getAttribute("grpid"));
  	next_row.id = id;
  	next_row.setAttribute("grpid", grpid);
  	colorRows(cur_row);
  	
  	drawCells(cur_row);
  	drawCells(next_row);
}
  
function colorRows(cur){
  	allrows = cur.parentNode.childNodes;
  	for (var i = 0; i < allrows.length; i++){
  		if (allrows[i].id){
  		  var id = parseInt(allrows[i].id);
		  	if ((id % 2) == 0){
		  		allrows[i].className = 'printable_even';
		  	}
		  	else{
		  		allrows[i].className = 'printable_odd';
		  	}
  		}
  	}
  	cur.className = 'row_highlight';
}
  
function drawCells(row){	
  	var allcells = row.childNodes;
  	var dncell;
  	var upcell;

  	for (var i = 0; i < allcells.length; i++){
  		if (allcells[i].id && allcells[i].id == 'dn'){
  			dncell = allcells[i];
  	  	}
  	  	if (allcells[i].id && allcells[i].id == 'up'){
  			upcell = allcells[i];
  	  	}
  	}

  	if (row.getAttribute("grpid") == '1'){
  		drawArrow(dncell);
  		drawBlank(upcell);
  	}else if (row.getAttribute("grpid") == row.getAttribute("grpcnt")){
  		drawBlank(dncell);
  		drawArrow(upcell);
  	}else{
  		drawArrow(dncell);
  		drawArrow(upcell);
  	}  	
}

function drawArrow(cell){
	try{
		if(customOrderFunc){};
		var use_custom = true;
	}catch(e){
		var use_custom = false;
	}
		
  	while (cell.hasChildNodes()){
  		cell.removeChild(cell.firstChild);
  	}
	var img = document.createElement('img');
    img.className = 'clickable';
    if(cell.id == 'dn'){
    	img.setAttribute('src', imgpath + 'dn.gif');
    	if(use_custom){
    		img.onclick= function() { customOrderFunc(this,'dn',this.parentNode.parentNode.id) };
    	}else{
    		img.onclick= function() { rowDn(this.parentNode.parentNode.id) };
    	}
  	}else{
  		img.setAttribute('src', imgpath + 'up.gif');
  		if(use_custom){
    		img.onclick= function() { customOrderFunc(this,'up',this.parentNode.parentNode.id) };
    	}else{
  			img.onclick= function() { rowUp(this.parentNode.parentNode.id) };
  		}
  	}

  	cell.appendChild(img);
}
  
function drawBlank(cell){
	while (cell && cell.hasChildNodes()){
  		cell.removeChild(cell.firstChild);
  	}
}
  
function save(table){
  	var rows = table.childNodes;
  	var str = '';
  	for (var i = 0; i < rows.length; i++){
  		var cur_row = rows[i];
  		if(is_all_ws(cur_row))
  		{
  			continue;
  		}
  		if (cur_row.getAttribute("curord")){
  			str = str + cur_row.getAttribute("curord") + ',';
  		}
  	}
  	var ordfld = document.getElementsByName('ords')[0];
  	ordfld.value = str;
  	document.forms[0].submit();
}

// helper functions needed for firefox to work correctly
// we need to skip blank/whitespace nodes when doing next/prev sibling
function is_all_ws( nod )
{
  return !(/[^\t\n\r ]/.test(nod.data));
}

function node_after( sib )
{
  while ((sib = sib.nextSibling)) {
    if (!is_all_ws(sib)) return sib;
  }
  return null;
}

function node_before( sib )
{
  while ((sib = sib.previousSibling)) {
    if (!is_all_ws(sib)) return sib;
  }
  return null;
}

function first_child( par )
{
  var res=par.firstChild;
  while (res) {
    if (!is_all_ws(res)) return res;
    res = res.nextSibling;
  }
  return null;
}

function removeEmptyNodes(parent)
{
  for (var i = 0; i < parent.childNodes.length; i++) {
    var node = parent.childNodes[i];
    if (node.nodeType == 3 && !/[^\t\n\r ]/.test(node.nodeValue)) {
      node.parentNode.removeChild(node);
    }
  }
}



