var lastSelection = null;
var initBkColor = null;
var initColor = null;
var oldBackgroundColor = null;
var initZIndex = 0;
var dragapproved=false;
var startDragPoz;
var x;
var z = null; //current dragged object
var startDrag = false;
var arrCells;

function move(){
  mouseMove();
  var aux;
		if (event.button == 1 && dragapproved){
		  z.style.pixelLeft = startDragPoz+event.clientX-x;
		  for (i=1; i < arrCells.length;i++){
			aux = arrCells(i);
			if ((z.offsetLeft > aux.offsetLeft) && (z.offsetLeft < (aux.offsetLeft+aux.clientWidth))){
			  if  (lastSelection == null) {
			    lastSelection = aux;
			    selectCell(aux);          
			  }
			  else {
				if (aux != lastSelection) {
				  deselectCell(lastSelection);
				  selectCell(aux); 
			      lastSelection = aux;			  
				}
			  }
			}
		  }
		  window.event.cancelBubble = true;
		  return false;
		}
}

function drags(){
  if (event.srcElement.className == "tableheaderdrag"){
		arrCells = document.all.listingTbl.rows(0).cells		
    dragapproved = true;
    z = event.srcElement;
		oldBackgroundColor = z.runtimeStyle.backgroundColor
		z.runtimeStyle.backgroundColor = "#004F92";
		z.runtimeStyle.color = initColor;
		z.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=70)"
    initZIndex = z.style.zIndex;
    z.style.zIndex = 100;
    startDragPoz = z.style.pixelLeft;
    x = event.clientX;
  }
}

function dragsEnd(){
  var newFieldOrder = "";
  if (event.button == 1 && dragapproved){  
    z.style.pixelLeft = startDragPoz;
    z.style.zIndex = initZIndex;
    if (lastSelection != null) {
      deselectCell(lastSelection);
      swapColumns(z,noRows);
      lastSelection = null;  
      for (i=1; i < document.all.listingTbl.rows(0).cells.length;i++){
	    aux = document.all.listingTbl.rows(0).cells(i);      
        newFieldOrder = newFieldOrder + aux.id + ",";
      }
      //document.form1.newFieldOrder.value = newFieldOrder.substring(0,newFieldOrder.length-1);       
      //document.form1.orderHasChanged.value = "yes";      
	  var today = new Date();
	  var expires = new Date();
	  expires.setTime(today.getTime() + 1000*60*60*24*365);
	  setCookie(crtPrjID+"newFieldOrder",newFieldOrder.substring(0,newFieldOrder.length-1), expires);      
	  setCookie(crtPrjID+"orderHasChanged","yes", expires);      
    }
    z.runtimeStyle.backgroundColor = oldBackgroundColor;
    z.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=100)"
    z = null;
  }
  dragapproved = false;
}

function onclickCell(noRows)
{
  var e, c;
  
  e = window.event.srcElement;
  c = findCell(e);

  if (c != null) {
    if (lastSelection == null) {
      selectCell(c);
      lastSelection = c;
	}
	else {
	  if (c == lastSelection) {
	    deselectCell(lastSelection);
	  }
	  else {
            swapColumns(c,noRows);
            deselectCell(lastSelection);
	  }
	  lastSelection = null;
    }
  }
  window.event.cancelBubble = true;
} 

function swapColumns(c, noRows){
  var toColumn, fromColumn;
  var arrRows;
  arrRows = document.all.listingTbl.rows(0).cells;
  for (i=0; i < arrRows.length;i++){
    if (arrRows(i) == c) toColumn = i;
    else
      if (arrRows(i) == lastSelection) fromColumn = i;
  }
  for (i=0; i <= noRows; i++) {
    arrRows = document.all.listingTbl.rows(i);
    //arrRows).cells(toColumn).swapNode(arrRows.cells(fromColumn));
    arrRows.cells(toColumn).parentNode.insertBefore(arrRows.cells(toColumn),arrRows.cells(fromColumn));
  }
}

function findCell(e)
{
  if (e.tagName == "TD") {
    return e;
  }
  else if (e.tagName == "BODY") {
    return null;
  }
  else {
    return findCell(e.parentElement);
  }
}

function selectCell(c)
{
  initBkColor = c.runtimeStyle.backgroundColor;
  initColor = c.runtimeStyle.color;
  c.runtimeStyle.backgroundColor = "red";
  c.runtimeStyle.color = "white";
}

function cancelSelect()
{
  if (lastSelection != null) {
    deselectCell(lastSelection);
    lastSelection = null;
  }
}

function deselectCell(c)
{
  c.runtimeStyle.backgroundColor = initBkColor;
  c.runtimeStyle.color = initColor;
}
if (ie5 || ie6) {
	document.onmousedown=drags;
	document.onmouseup=dragsEnd;
	document.onmousemove = move;
}
