// All material contained within this page is the property of 4thorder(TM) 
// Copyright © 2004.  All rights reserved.
//
// Author: Michael Falatine
// Authors email: scripts@4thorder.us
//
// USAGE:
// You may use this script for commercial or personal use, however, the copyright is retained
// by 4thorder (TM).


// Set event handlers ------------
document.ondblclick=onDBLClickHandler;
document.onmouseover=onMouseOverHandler;
document.onmouseout=onMouseOutHandler;

document.onkeypress = 
function esc(e)
	{	
	var baseLocation=document.all.mTABLE
	document.all.rowId.value="disabled";
	
	if(typeof(e) == "undefined")
	{
	e=event;
	}
	if (e.keyCode == 27)
	{
		for (i=0; i < baseLocation.rows.length; i++)
		{
		baseLocation.rows(i).className='rowOnMouseOut';
		}
	}	
	}
// END ------------

// handler functions ------------
function onDBLClickHandler()
{
	var baseLocation=document.all.mTABLE;
	var currentElement=event.srcElement.parentElement;
	var previousID=document.all.rowId.value;
	var presentID=currentElement.getAttribute("id");
	
			//reset rowID and background for clicking outside of table row 
			if(currentElement.tagName == "TR" && presentID == "")
				{
				document.all.rowId.value="disabled";
					for (i=0; i < baseLocation.rows.length; i++)
						{
						baseLocation.rows(i).setAttribute("id",i+"r")
						baseLocation.rows(i).className='rowOnMouseOut';
						}
				}
			else
				{
					if(currentElement.tagName == "TR")
					{
						if(previousID == "disabled")
							{
							// set rowID : set background to "on click" color
							document.all.rowId.value=presentID;
							currentElement.className='rowOnDBLClick';
							}
						else
							{
							//set source row id : strip "r" from id
							fromID=parseInt(previousID,10)
							
							//set target row id : strip "r" from id				
							toID=parseInt(presentID,10)
							
							// move row : clear background : clear rowID from container
							baseLocation.moveRow(fromID,toID);
							initTableRows();				
							}
					}
				}
}

function onMouseOverHandler()
{
	var baseLocation=document.all.mTABLE;	
	var currentElement=event.srcElement.parentElement;
	var presentID=currentElement.getAttribute("id");
	
			if(currentElement.tagName == "TR" && presentID != "")
			{
			baseLocation.rows(presentID).className='rowOnMouseOver';
			}
}

function onMouseOutHandler()
{
	var baseLocation=document.all.mTABLE;	
	var currentElement=event.srcElement.parentElement;
	var previousID=document.all.rowId.value;
	var presentID=currentElement.getAttribute("id");
	
			if(currentElement.tagName == "TR" && presentID != "")
			{
				if(presentID != previousID)
				{
				baseLocation.rows(presentID).className='rowOnMouseOut';
				}
				else
				{
				baseLocation.rows(presentID).className='rowOnDBLClick';
				}
			}
}

// END ------------

// set ID's for each row(id=#r) : intialize row styles : row ID container value ------------
function initTableRows()
{
	var baseLocation=document.all.mTABLE;
	document.all.rowId.value="disabled";
		for (i=0; i < baseLocation.cells.length; i++)
		{
		baseLocation.cells(i).unselectable = "On";
		}
		for (i=0; i < baseLocation.rows.length; i++)
		{
		baseLocation.rows(i).setAttribute("id",i+"r")
		baseLocation.rows(i).className='rowOnMouseOut';
		}
}