var numFromPlateRows; var numFromPlateCols;
var numOutputPlateRows; var numOutputPlateCols;
var intervalClearPlate;
mouseIsInToPlate=false;
isDragging=false;
fromStartHighlightRow=0;
fromStartHighlightCol=0;
fromEndHighlightRow=0;
fromEndHighlightCol=0; 
toStartHighlightRow=0;
toStartHighlightCol=0;
toEndHighlightRow=0;
toEndHighlightCol=0; 
well_color_unhighlighted="#fff";
well_color_highlighted="#99f";

function initCopyFromPlate() {
//  highlightWells('toplate');
//  highlightWells('fromplate')
//}


//function copyFromPlateInit() {
  numFromPlateRows=fromPlateRows.length;
  numFromPlateCols=fromPlateCols.length;
  numOutputPlateRows=outputPlateRows.length;
  numOutputPlateCols=outputPlateCols.length;
//  selectAllWells("fromplate");
  startCheckingIfInToPlate();
}

function isFromPlateWellActive(row,col) {
  if (fromPlate[row][col]["outputSampleHook"]!=null){return true;}
  return false;
}

function isToPlateWellActive(row,col) {
  if (outputPlate[row][col]["inputSampleHook"]!=null){return true;}
  return false;
}

function fromPlateHandleWellMouseDown(row,col) {

  if (isFromPlateWellActive(row,col)) {
    isDragging=true;
    fromStartHighlightRow=row;
    fromStartHighlightCol=col;
    fromEndHighlightRow=row;
    fromEndHighlightCol=col;
    highlightWells("fromplate");
    selectSameInToPlate();
  }
}

function fromPlateHandleWellMouseUp(row,col) {
  if(isDragging) {
    isDragging=false;
    if((row!=0 && row=="") || (col!=0&&col=="")) { return false; }
  }
	fromStartHighlightRow=1*fromStartHighlightRow;
	fromStartHighlightCol=1*fromStartHighlightCol;
	fromEndHighlightRow=1*fromEndHighlightRow;
	fromEndHighlightCol=1*fromEndHighlightCol;
	if(fromEndHighlightRow<fromStartHighlightRow){
		temp=fromStartHighlightRow;
		fromStartHighlightRow=fromEndHighlightRow;
		fromEndHighlightRow=temp;
	}
	if(fromEndHighlightCol<fromStartHighlightCol){
		temp=fromStartHighlightCol;
		fromStartHighlightCol=fromEndHighlightCol;
		fromEndHighlightCol=temp;
	}
    highlightWells("fromplate");
    selectSameInToPlate();
    validate();
}

function fromPlateHandleWellMouseOver(row,col) {
  document.onmousemove=fromPlateHandleWellMouseMove;
  if (document.captureEvents) document.captureEvents(Event.MOUSEMOVE);
  if(!isDragging) {
    //normal mouseover stuff
  } else {
    if(document.selection) { document.selection.empty(); } //kill text highlight in IE
//    fromEndHighlightRow=row;
//    fromEndHighlightCol=col;
    numToRows=outputPlateRows.length;
    numToCols=outputPlateCols.length;
    rowDiff=Math.abs(row-fromStartHighlightRow);
    if(rowDiff>numToRows-1) rowDiff=numToRows-1;
    colDiff=Math.abs(col-fromStartHighlightCol);
    if(colDiff>numToCols-1) colDiff=numToCols-1;
    if(row<fromStartHighlightRow) {
      fromEndHighlightRow=(fromStartHighlightRow-rowDiff);
    } else {
      fromEndHighlightRow=(fromStartHighlightRow+rowDiff);
    } 
    if(col<fromStartHighlightCol) {
      fromEndHighlightCol=(fromStartHighlightCol-colDiff);
    } else {
      fromEndHighlightCol=(fromStartHighlightCol+colDiff);
    }
    
    highlightWells("fromplate");
    selectSameInToPlate();
  }
}



function fromPlateHandleWellMouseMove(event) {
/*
x=mouseX(event);
	y=mouseY(event);
	if(grid[tooltipRow] && grid[tooltipRow][tooltipCol]) {
		well=grid[tooltipRow][tooltipCol];
		showTooltip(well,x,y);
	}
*/
}

function fromPlateHandleWellMouseOut(row,col) {
  document.onmousemove=null;
  if (document.captureEvents) document.captureEvents(null);
//hideTooltip();
  return true;
}

function toPlateHandleWellMouseOver(row,col) {
//  startCheckingIfInToPlate();
  //if (isFromPlateWellActive(row,col)) {
    showIntendedSelection(row,col);
    mouseIsInToPlate=true;
  //}
}

function toPlateHandleWellMouseOut(row,col) {
  mouseIsInToPlate=false;
}

function toPlateHandleWellClick(row,col) {
  setSelection(row,col);
  validate();
}

function setSelection(row,col) {
  fromPlateSelectHeight=fromEndHighlightRow-fromStartHighlightRow;
  fromPlateSelectWidth =fromEndHighlightCol-fromStartHighlightCol;
  intendedSelectLeft=col; intendedSelectRight =col+fromPlateSelectWidth;
  intendedSelectTop=row;  intendedSelectBottom=row+fromPlateSelectHeight;

  canMakeSelection=true;
  if(intendedSelectRight>=outputPlateCols.length || intendedSelectBottom>=outputPlateRows.length) {
    canMakeSelection=false;
  }
  if(!canMakeSelection) { return false; }

  toStartHighlightRow = row; toEndHighlightRow=row+fromPlateSelectHeight;
  toStartHighlightCol = col; toEndHighlightCol=col+fromPlateSelectWidth;
  highlightWells('toplate');
}

function showIntendedSelection(row,col) {
  fromPlateSelectHeight=fromEndHighlightRow-fromStartHighlightRow;
  fromPlateSelectWidth =fromEndHighlightCol-fromStartHighlightCol;
  intendedSelectLeft=col; intendedSelectRight =col+fromPlateSelectWidth;
  intendedSelectTop=row;  intendedSelectBottom=row+fromPlateSelectHeight;
  canMakeSelection=true;
  
  if(intendedSelectRight>=outputPlateCols.length || intendedSelectBottom>=outputPlateRows.length) {
    canMakeSelection=false;
  }
  iconToUse=canMakeSelection ? "welldot.gif" : "wellerror.gif";

  for(r=0;r<outputPlateRows.length;r++) { 
  for(c=0;c<outputPlateCols.length;c++) { 
    isInIntendedSelect=false;
    currentWell=document.getElementById("toplate_"+r+"_"+c);
    if(r>=intendedSelectTop && r<=intendedSelectBottom && 
          c>=intendedSelectLeft && c<=intendedSelectRight) {
      isInIntendedSelect=true;  
    }

    currentWell.style.backgroundImage=isInIntendedSelect? "url('"+pathToIcons+"copybetweenplates/"+iconToUse+"')" : "";
  }
  }
}

function clearIntendedSelection() {
//  if(!mouseIsInToPlate) { return false; }
  for(r=0;r<outputPlateRows.length;r++) { 
  for(c=0;c<outputPlateCols.length;c++) { 
    currentWell=document.getElementById("toplate_"+r+"_"+c);
    currentWell.style.backgroundImage="";
  }
  }
}


function startCheckingIfInToPlate() {
  intervalClearPlate=setInterval("cleanUpAfterLeavingToPlate()",100);
}

function cleanUpAfterLeavingToPlate() {
  if(mouseIsInToPlate) return false;
//  clearInterval(intervalClearPlate);
  clearIntendedSelection();
}


function selectRow(plate,row) {
  numCols=cols.length;
  if(plate=="fromplate"){
    fromStartHighlightRow=row;
    fromStartHighlightCol=0;
    fromEndHighlightRow=row;
    fromEndHighlightCol=numCols-1; 
    highlightWells(plate);
    selectSameInToPlate();
    validate();
  } else if(plate=="toplate"){
    toStartHighlightRow=row;
    toStartHighlightCol=0;
    toEndHighlightRow=row;
    toEndHighlightCol=numCols-1; 
    highlightWells(plate);
    validate();
  }
}

function selectColumn(plate,col) {
  numRows=rows.length;
  if(plate=="fromplate"){
    fromStartHighlightRow=0;
    fromStartHighlightCol=col;
    fromEndHighlightRow=numRows-1;
    fromEndHighlightCol=col; 
    highlightWells(plate);
    selectSameInToPlate();
    validate();
  } else if(plate=="toplate"){
    toStartHighlightRow=0;
    toStartHighlightCol=col;
    toEndHighlightRow=numRows-1;
    toEndHighlightCol=col; 
    highlightWells(plate);
    validate();
  }
}

function selectAllWells(plate) {
  if(plate=="fromplate"){
    fromStartHighlightRow=0;
    fromStartHighlightCol=0;
    fromEndHighlightRow=7;
    fromEndHighlightCol=11; 
    highlightWells(plate);
    selectSameInToPlate();
    validate();
  } else if(plate=="toplate"){
    toStartHighlightRow=0;
    toStartHighlightCol=0;
    toEndHighlightRow=7;
    toEndHighlightCol=11; 
    highlightWells(plate);
    validate();
  }
}

function selectLeftHalf(plate) {
  if(plate=="fromplate"){
    fromStartHighlightRow=0;
    fromStartHighlightCol=0;
    fromEndHighlightRow=fromPlateRows.length-1;
    fromEndHighlightCol=fromPlateCols.length/2-1; 
    highlightWells(plate);
    selectSameInToPlate();
    validate();
  } else if(plate=="toplate"){
    toStartHighlightRow=0;
    toStartHighlightCol=0;
    toEndHighlightRow=outputPlateRows.length;
    toEndHighlightCol=outputPlateCols.length/2-1; 
    highlightWells(plate);
    validate();
  }
}

function selectRightHalf(plate) {
  if(plate=="fromplate"){
    fromStartHighlightRow=0;
    fromStartHighlightCol=fromPlateCols.length/2;
    fromEndHighlightRow=fromPlateRows.length-1;
    fromEndHighlightCol=fromPlateCols.length-1; 
    highlightWells(plate);
    selectSameInToPlate();
    validate();
  } else if(plate=="toplate"){
    toStartHighlightRow=0;
    toStartHighlightCol=outputPlateRows.length/2;
    toEndHighlightRow=outputPlateRows.length-1;
    toEndHighlightCol=outputPlateCols.length-1; 
    highlightWells(plate);
    validate();
  }
}

function selectSameInToPlate() {
  toStartHighlightRow=fromStartHighlightRow;
  toEndHighlightRow=fromEndHighlightRow;	
    if(toEndHighlightRow*1<toStartHighlightRow*1){
    temp=toStartHighlightRow;
    toStartHighlightRow=toEndHighlightRow;
    toEndHighlightRow=temp;
  }
  while(toEndHighlightRow>outputPlateRows.length-1){
    toEndHighlightRow--;
    toStartHighlightRow--;
  }
  toStartHighlightCol=fromStartHighlightCol;	
  toEndHighlightCol=fromEndHighlightCol;	
  if(toEndHighlightCol*1<toStartHighlightCol*1){
    temp=toStartHighlightCol;
    toStartHighlightCol=toEndHighlightCol;
    toEndHighlightCol=temp;
  }
  while(toEndHighlightCol>outputPlateCols.length-1){
    toEndHighlightCol--;
    toStartHighlightCol--;
  }
  highlightWells("toplate");
}

function validate() {
  canSubmit=true;
  if(!compareSelections()) { canSubmit=false; }
  if(canSubmit) {
    document.getElementById("savebutton").disabled="";
    clearError();  
  } else {
    document.getElementById("savebutton").disabled="disabled";
  }
}

function compareSelections() {
  fromNumRows=fromEndHighlightRow-fromStartHighlightRow;
  toNumRows  =toEndHighlightRow-toStartHighlightRow;
  fromNumCols=fromEndHighlightCol-fromStartHighlightCol;
  toNumCols  =toEndHighlightCol-toStartHighlightCol;

  if(fromNumRows==toNumRows && fromNumCols==toNumCols) {
    return true;
  } else { 
    showError("Selection must be the same size and shape in both plates");
    return false;
  }
}

function clearError() {
  showError("&nbsp;");	
}

function showError(msg) {
  document.getElementById("errormessages").innerHTML=msg;
}

function highlightWells(plate) {
  //highlight from start row/col to end row/col
  if(plate=="fromplate"){
    numRows=fromPlateRows.length; numCols=fromPlateCols.length;
    beginRow=fromStartHighlightRow;
    endRow=fromEndHighlightRow;	
    beginCol=fromStartHighlightCol;	
    endCol=fromEndHighlightCol;
  } else if(plate=="toplate"){
    numRows=outputPlateRows.length; numCols=outputPlateCols.length;
    beginRow=toStartHighlightRow;
    endRow=toEndHighlightRow;	
    beginCol=toStartHighlightCol;	
    endCol=toEndHighlightCol;
  }
  //swap start and end points if end above or left of start
  //for comparison multiply by 1 - otherwise Javascript thinks they're strings
  if(endRow*1<beginRow*1){
    temp=beginRow;
    beginRow=endRow;
    endRow=temp;
  }
  if(endCol*1<beginCol*1){
    temp=beginCol;
    beginCol=endCol;
    endCol=temp;
  }

  for(i=0;i<numRows;i++) {
    for(j=0;j<numCols;j++) {
      highlightColor=well_color_unhighlighted;
      if(i>=beginRow && i<=endRow && j>=beginCol && j<=endCol) {
        highlightColor=well_color_highlighted;
			}
      document.getElementById(plate+"_"+i+"_"+j).style.backgroundColor=highlightColor;
    }
  }
}

function validateVolume(vol) {
  if(!isNumeric(vol)) {
    alert("Volume must be a number.");
    return false;
  }
  if(vol<0) {
    alert("Volume must be zero or more.");
    return false;
  }
  return true;
}

function doSave() {
  if(""!=document.getElementById("volume").value) {
    if(!validateVolume(document.getElementById("volume").value)) {
      return false;
    }
  }
  xdiff=toStartHighlightCol-fromStartHighlightCol;
  ydiff=toStartHighlightRow-fromStartHighlightRow;
  for(r=toStartHighlightRow;r<=toEndHighlightRow;r++) {
    for(c=toStartHighlightCol;c<=toEndHighlightCol;c++) {
	  key=outputPlate[r][c]["inputSampleHook"];  
	  value=fromPlate[r-ydiff][c-xdiff]["outputSampleHook"]; 
	  if (null!=key) {
	    window.parent.addToChangedFields(key+":sample",value);
	    if(""!=document.getElementById("volume").value) {
	  	  vol=document.getElementById("volume").value;  
	  	  //vol=vol/1000000; //convert to microlitres
	  	  //window.parent.addToChangedFields(key+":amount",vol+"L");
	  	  window.parent.addToChangedFields(key+":amount",vol+"uL");
	    }
	  }
    }
  }
  window.parent.closeCopyPlatesAndSubmit();
}

