/*
 * Component: cases
 *
 * Description:
 *   JavaScript function to be used on EP web pages
 *
 * Last Updated     : $Author: tnemeth $
 * Update Date Time : $Date: 2007/12/18 14:35:54 $
 * RCS File Name    : $RCSfile: TableScripts.js,v $
 * RCS Revision     : $Revision: 1.39.4.6 $
 * Locked by        : $Locker:  $
 * Symbolic name    : $Name: PATCH-5-2-1 $
 * State            : $State: Exp $
 */

var suffix = ""; // image name-part which distincts big and small images
var mul = 1; // image size multiplier
var ratio = 1; // window size multiplier

var trick_inputs = new Array(); // array to store the names of hidden inputs used to emulate buttons (see getInputButton())

/**
* this function checks for high resolution display and sets variables as needed
*/
function isHiRes() { 
   if (self.screen) {
      if (screen.width*screen.height > 5000000) {
         mul = 2;
         suffix = ".big";
         ratio = screen.width/1280;
         return true;
      }
   }
   return false;
}

var hires = isHiRes(); // boolean indicating if the display used is a high resolution one

/**
* this writes into the document an image tag
* @param img			the image name(with path)
* @param width		width of the image (discarded if less than zero)
* @param height		height of the image (discarded if less than zero)
* @param params	string containing any extra parameters for the image
*/
function getImageTag(img, width, height, params) {
	if (!params) {
		params = "";
	}
	var widthStr = "";
	if (width >=0) {
		widthStr = " width="+width;
	}
	var heightStr = "";
	if (height >=0) {
		heightStr = " height="+height;
	}
	return "<img SRC=\""+img+"\""+widthStr+heightStr+imageBorderStr+params+">";
}

/**
* this writes into the document an image tag
* @param img			the image name(with path)
* @param width		width of the image (discarded if less than zero)
* @param height		height of the image (discarded if less than zero)
* @param params	string containing any extra parameters for the image
*/
function writeImage(img, width, height, params) {
	document.writeln(getImageTag(img, width, height, params));
}

/**
* this writes Image type input field after adjusting parameters depending on "hires"
* important restrictions: 
* 	- the containing html page mustn't contain another control element with this name
* 	- value mustn't be "null" (the string whose length is 4, 2nd letter is 'u', etc... :) )
* @param img		the image name(with path defaulting to /art/b/)
* @param name	name of the input field
* @param value	value of the input field
*/
function getInputButton(img, name, value) {
	var p = img.indexOf(".gif");
	var fname = img;
	if (p>=0) {
		fname = img.substring(0,p);
	}
	fname = fname + suffix + ".gif";
	p = img.indexOf("/");
	if (p<0) {
		fname = "/art/b/" + fname;
	}
	if (name.length == 0) {// no parameter to pass by this button
		var html = "<input type=\"image\" src=\""+fname+"\"";
		document.write(html+imageBorderStr+">");
	}
	else {
		trick_inputs[trick_inputs.length] = name;
		var html = "<input type=\"hidden\" name=\""+name+"\" value=\""+value+"\">"+
			"<a href=\"javascript: submitTricky(\'"+name+"\', \'"+value+"\');\">"+
			"<img src=\""+fname+"\""+imageBorderStr+"></a>";
		document.write(html);
	}
}

function submitTricky(selected, value) {
	var form2Submit;
	var debugStr = "clearTricks:\n";
	debugStr += "  searching forms:"+document.forms.length+"\n";
	for (var i=0; i<document.forms.length; i++) {
		var form = document.forms[i]
		debugStr += "  form: "+form+"\n";
		debugStr += "    searching elements:"+form.elements+"\n";
		for (var j=0; j<form.elements.length; j++) {
			var element = form.elements[j];
			if (element.name == selected) {
				element.disabled = false;
				element.value = value;
				debugStr += "    element: "+element.name;
				debugStr +=" -> turning on.\n";
				form2Submit = form;
			}
			else {
				for (var k=0; k<trick_inputs.length; k++) {
					if (trick_inputs[k] == element.name) {
						element.disabled=true;
						element.value = "null";
						debugStr += "    element: "+element.name;
						debugStr +=" -> turning off.\n";
					}
				}
			}
		}
	}
	debugStr += "END.";
//	alert(debugStr);
	if (form2Submit) {
		form2Submit.submit();
	}
}

/**
* this calles writeImage after adjusting parameters depending on "hires"
* @param img			the image name(with path defaulting to /cases/art/)
* @param width		width of the image (discarded if less than zero)
* @param height		height of the image (discarded if less than zero)
* @param params	string containing any extra parameters for the image
*/
function getImage(img, width, height, params) {
	var p = img.indexOf(".gif");
	var fname = img;
	if (p>=0) {
		fname = img.substring(0,p);
	}
	fname = fname + suffix + ".gif";
	p = img.indexOf("/");
	if (p<0) {
		fname = "/cases/art/" + fname;
	}
	if (width) {
		var width2 = width*mul;
		var height2 = height*mul;
		writeImage(fname, width2, height2, params);
	}
	else {
		writeImage(fname);
	}
}

function getButtonTag(img, width, height, params) {
	var width2 = 91*mul;
	var height2 = 18*mul;
	var p = img.indexOf(".gif");
	var fname = img;
	if (p>=0) {
		fname = img.substring(0,p);
	}
	fname = fname + suffix + ".gif";
	p = img.indexOf("/");
	if (p<0) {
		fname = "/art/b/" + fname;
	}
	return getImageTag(fname, width2, height2, params);
}

/**
* this calles writeImage after adjusting parameters depending on "hires"
* @param img			the image name(with path defaulting to /art/b/)
* @param params	string containing any extra parameters for the image
*/
function getButton(img, params) {
	document.write(getButtonTag(img, params));
}

function redirPage_b(url) {
    url2 = url+backParam;
    top.location = url2;
}

function redirPage_prior(par1) {
    url2 = '../prior/index.jsp?cStudy='+par1+relevParam+backParam;
    top.location = url2;
}

function redirPage_prior2(par1) {
    redirPage_prior(par1+'&cOpenSingle=true');
}

function redirPage_prior3(par1) {
    redirPage_prior(par1+'&cLocalOnly=true'+'&cOpenSingle=true');
}

function redirPage_eQV(url) {
    url2 = 'index.jsp?cQV='+url+eQVParam;
    top.location = url2;
}

function redirPage_edit(par1,par2) {
    redirPage_b('/cases/edit.jsp?cStudy='+par1+'&cIMLockID='+par2);
}

function redirPage_editOnOwner(par0,par1,par2,par3,par4) {
    redirPage_b(par0+'/cases/edit.jsp?cStudy='+par1+'&cIMLockID='+par2+'&ParentSession='+par3+'&RefererHost='+escape(par4));
}

function redirPage_oper(par1,par2) {
    redirPage_b('oper.jsp?cInstr='+par1+'&cGr='+par2);
}

function redirPage_eP(url) {
    url2 = url+"&cContext="+pageContext;//ePParam;
    top.location = url2;
}

function redirPage_eSP(url) {
    url2 = sortParams+url;
    top.location = url2;
}

function redirPage_PBS(url) {
    url2 = 'getpbs.jsp?cGr='+url+PBSParam;
    top.location = url2;
}

function redirPage_MHUB(par1) {
	redirPage_b('/dotcom/parseStudyData.jsp?cStudy='+par1);
}

function ws(name) {
    window.status = name;
}

function wsf(name) {
    window.status = 'Filter on '+name;
}

function wsfc(name) {
    window.status = 'Change filter on '+name;
}

function wso(name) {
    window.status = 'Order on '+name;
}

function wsro() {
    window.status = 'Reverse sort order';
}

/**
* this function writes action buttons depending on rights (used by ComboAction in Worklist)
* @param rights		String containing characters representing rights:
*								{ 'o' : Open, 'q' : quickView, 'e' : Edit, 'd' : Delete, 'f' : forward, 'p' : Prior}
* @param dispnam	patient's name in a displayable format
* @param syui			the StudY's Unique Identifier
* @param nimg		number of images in this study
* @param nSrc           number of sources (for handling the case when study exists on multiple HUB servers)
* @param deletable	boolean indicating if this study is deletable
*/
function comboWrite(rights, dispnam, syui, nimg, nSrc, deletable, editable, isLockKnown, locker, isDbOnly, redirectHost, serverName, sid, isChanged, ownlock, folder, isLink ) { 
	var folderTag = '';
	if (rights.indexOf("o") > -1 || rights.indexOf("e") > -1) {
		if (editable && isLockKnown) {
			if (locker) { // is locked
				if (ownlock) { // locked by self
					getImage("semOwn", 10, 20, " alt=\"Locked by you\" title=\"Locked by you\"");
				}
				else { // locked by someone else
					getImage("semLocked", 10, 20, " alt=\"Locked by "+locker+"\" title=\"Locked by "+locker+"\"");
				}
			}
			else { // not locked
				getImage("semUnlocked", 10, 20, " alt=\"Not locked\" title=\"Not locked\"");
			}
		}
		else { // not lockable
			getImage("semNotLockable", 10, 20, "");
		}
	}
	if (folder) {
		folderTag = '&cFolder='+folder;
		if (isLink) {
			document.write("<A href= \'JavaScript: redirPage_oper(\"FixateLink\",\""+syui+folderTag+"\")\' onMouseOver=\'ws(\"Make a private copy of "+dispnam+"\"); return true;\'>");
			getImage("shortcut", 18, 20);
			document.write("</a>");
		}
		else {
			getImage("spacer", 17, 20);
		}
	}
	if (rights.indexOf("o") > -1) {
		if (nimg > 0) {
			document.write("<A href= \'JavaScript: redirPage_PBS(\""+syui+folderTag+"\")\' onMouseOver=\'ws(\"Open "+dispnam+"\"); return true;\'>");
			if (isChanged) {
				getImage("open.r", 21, 20, " alt=\"Open "+dispnam+"\"");
			}
			else {
				getImage("open", 21, 20, " alt=\"Open "+dispnam+"\"");
			}
			document.write("</a>");
		}
		else {
			getImage("spacer", 21, 20);
		}
	}
	if (rights.indexOf("q") > -1) {
		document.write("<A href= \'JavaScript: redirPage_eQV(\""+syui+"\")\' onMouseOver=\'ws(\"Quick View "+dispnam+"\"); return true;\'>");
		getImage("qview", 18, 20, " alt=\"Quick View "+dispnam+"\"");
		document.write("</a>");
	}
	if (rights.indexOf("e") > -1) {
		if (editable) {
			if (! isDbOnly){
				document.write("<A href= \'JavaScript: redirPage_edit(\""+syui+folderTag+"\", \""+lockid+"\")\' onMouseOver=\'ws(\"Edit "+dispnam+"\"); return true;\'>");
				getImage("edit", 18, 20, " alt=\"Edit "+dispnam+"\"");
				document.write("</a>");
			}
			else {
				 document.write("<A href= \'JavaScript: redirPage_editOnOwner(\""+redirectHost+"\", \""+syui+folderTag+"\", \""+lockid+"\", \""+sid+"\", \""+serverName+"\")\' onMouseOver=\'ws(\"Edit on the owner device "+dispnam+"\"); return true;\'>");
                                getImage("edit", 18, 20, " alt=\"Edit "+dispnam+"\"");
                                document.write("</a>");
			}
		}
		else {
			getImage("spacer", 18, 20);
		}
	}
	if (rights.indexOf("d") > -1) {
		if (deletable) {
			document.write("<A href= \'JavaScript: redirPage_oper(\"Del\",\""+syui+folderTag+"\")\' onMouseOver=\'ws(\"Delete "+dispnam+"\"); return true;\'>");
			getImage("delete", 17, 20, " alt=\"Delete "+dispnam+"\"");
			document.write("</a>");
		}
		else {
			getImage("spacer", 17, 20);
		}
	}
	if (rights.indexOf("f") > -1) {
		if (nimg > 0) {
			document.write("<A href= \'JavaScript: redirPage_oper(\"Fwd\",\""+syui+folderTag+"\")\' onMouseOver=\'ws(\"Forward/Export "+dispnam+"\"); return true;\'>");
			getImage("forward", 16, 20, " alt=\"Forward/Export "+dispnam+"\"");
			document.write("</a>");
		}
		else {
			getImage("spacer", 16, 20);
		}
	}
	if (rights.indexOf("p") > -1) {
		document.write("<A href= \'JavaScript: redirPage_prior(\""+syui+"\")\' onMouseOver=\'ws(\"Prior studies for "+dispnam+"\"); return true;\'>");
		getImage("prior", 17, 20, " alt=\"Prior studies for "+dispnam+"\"");
		document.write("</a>");
	}
	if (nSrc > 1) {
		// Study exists on multiple HUB server
		if (rights.indexOf("a") > -1) {
//			// Admin users will have a link to a correction page
			document.write("<A href= \'JavaScript: redirPage_MHUB(\""+syui+"\")\' onMouseOver=\'ws(\"Study resides on multiple HUB servers\"); return true;\'>");
			getImage("warning", 18, 20, " alt=\"Study exists on multiple HUB servers\"");
			document.write("</a>");
		}
		else {
			getImage("warning", 18, 20, " alt=\"Study exists on multiple HUB servers\"");
		}
	}
}

/**
* this function writes action buttons depending on rights (used by ComboAction in "Other  lists" list)
* @param rights		String containing characters representing rights:
*								{ 'd' : Delete, 'r' : rename}
* @param listUser		owner of the current list
* @param listName	name of the current list
* @param deletable	boolean indicating if this study is deletable
*/
function comboWriteList(rights, listUser, listName, listNameEnc, deletable) { 
   if (rights.indexOf("d") > -1) {
      if (deletable) {
         document.write("<A href= \'JavaScript: Oper_(\""+listUser+"\",\""+listNameEnc+"\",\"Delete\")\' onMouseOver=\'ws(\"Delete "+listName+"\"); return true;\'>");
         getImage("delete", 17, 20, " alt=\"Delete "+listName+"\"");
         document.write("</a>");
      }
      else {
         getImage("spacer", 17, 20);
      }
   }
   if (rights.indexOf("r") > -1) {
      document.write("<A href= \'JavaScript: Oper_(\""+listUser+"\",\""+listNameEnc+"\",\"Rename\")\' onMouseOver=\'ws(\"Rename "+listName+"\"); return true;\'>");
      getImage("rename", 17, 20, " alt=\"Rename "+listName+"\"");
      document.write("</a>");
   }
}

/**
* this function writes action buttons depending on rights (used by ComboAction in "Folders" list)
* @param rights		String containing characters representing rights:
*								{ 'd' : Delete, 'r' : rename}
* @param folderUser		owner of the current folder
* @param folderName		name of the current folder
*/
function comboWriteFolder(rights, folderUser, folderName) { 
   if (rights.indexOf("d") > -1) {
      document.write("<A href= \'JavaScript: Oper_(\""+folderUser+"/"+folderName+"\",\"Delete\")\' onMouseOver=\'ws(\"Delete "+folderName+"\"); return true;\'>");
      getImage("delete", 17, 20, " alt=\"Delete "+folderName+"\"");
      document.write("</a>");
   }
   if (rights.indexOf("r") > -1) {
      document.write("<A href= \'JavaScript: Oper_(\""+folderUser+"/"+folderName+"\",\"Rename\")\' onMouseOver=\'ws(\"Rename "+folderName+"\"); return true;\'>");
      getImage("rename", 17, 20, " alt=\"Rename "+folderName+"\"");
      document.write("</a>");
   }
}

/**
* this function writes action buttons depending on rights (used by ComboAction in "Tasks" list)
* @param rights		String containing characters representing rights:
*								{'e' : edit, 'r' : reschedule, 'd' : delete, 's' : suspend}
* @param filename		filename identifying the task
*/
function comboWriteTask(rights, filename) { 
	if (rights.indexOf("e") > -1) {
		document.write("<A href= \'JavaScript: Oper_(\""+filename+"\",\"Edit\")\' onMouseOver=\'ws(\"Edit "+filename+"\"); return true;\'>");
		getImage("edit", 18, 20, " alt=\"Edit "+filename+"\"");
		document.write("</a>");
	}
   if (rights.indexOf("r") > -1) {
      document.write("<A href= \'JavaScript: Oper_(\""+filename+"\",\"Reschedule\")\' onMouseOver=\'ws(\"Reschedule "+filename+"\"); return true;\'>");
      getImage("addtoqueue", 21, 20, " alt=\"Reschedule "+filename+"\"");
      document.write("</a>");
   }
   if (rights.indexOf("d") > -1) {
      document.write("<A href= \'JavaScript: Oper_(\""+filename+"\",\"Delete\")\' onMouseOver=\'ws(\"Delete "+filename+"\"); return true;\'>");
      getImage("delete", 17, 20, " alt=\"Delete "+filename+"\"");
      document.write("</a>");
   }
	if (rights.indexOf("s") > -1) {
		document.write("<A href= \'JavaScript: Oper_(\""+filename+"\",\"Suspend\")\' onMouseOver=\'ws(\"Suspend "+filename+"\"); return true;\'>");
		getImage("suspend", 18, 20, " alt=\"Suspend "+filename+"\"");
		document.write("</a>");
	}
}

/**
* this function writes action buttons depending on rights (used by ComboAction in "QR" list)
* @param rights			String containing characters representing rights:
*											{ 'r' : Retrieve, 'm' : Move (send)}
* @param study			String containing the studyUID
* @param parHost		String containing target host parameter
* @param isPresent	"true" means that the study is present locally, so should not be retrieved.
* @param coming			"true" means that the study is already retrieving
*/
function comboWriteQR(rights, parHost, study, PANM, isPresent, coming, isAdmin) { 
   if (rights.indexOf("r") > -1) {
   		if (isPresent) {
        	getImage("spacer", 16, 20);
   		}
   		else {
   			if (coming) {
   				if (isAdmin) {
      			document.write("<A href= \'JavaScript:forceRetrieve(\""+parHost+"\", \""+study+"\")\' onMouseOver=\'ws(\"Get "+PANM+" again\"); return true;\'>");
      		}
      		getImage("get.d", 16, 20, " alt=\""+PANM+" is retrieving\"  onMouseOver=\'ws(\""+PANM+" is retrieving\"); return true;\'");
   				if (isAdmin) {
	      		document.write("</a>");
	      	}
   			}
   			else {
      		document.write("<A href= \'/archive/get.jsp?cDevStudy="+parHost+"+"+study+"\' onMouseOver=\'ws(\"Get "+PANM+"\"); return true;\'>");
      		getImage("get", 16, 20, " alt=\"Get "+PANM+"\"");
      		document.write("</a>");
      	}
      }
   }
   if (rights.indexOf("m") > -1) {
     	document.write("<A href= \'/qr/retrieve.jsp?cDevStudy="+parHost+"+"+study+"&cOp=Move\' onMouseOver=\'ws(\"Move "+PANM+"\"); return true;\'>");
     	getImage("forward", 16, 20, " alt=\"Move "+PANM+"\"");
     	document.write("</a>");
   }
}

/**
* this function writes action buttons depending on rights (used by ComboAction in "Archive/Media" list)
* @param rights			String containing characters representing rights:
*											{ 'r' : Retrieve }
* @param study			String containing the studyUID
* @param isPresent	"true" means that the study is present locally, so should not be retrieved.
*/
function comboWriteMedia(rights, study, PANM, isPresent) { 
	if (rights.indexOf("r") > -1) {
   		if (isPresent) {
        	getImage("spacer", 16, 20);
   		}
   		else {
      		document.write("<A href= \'JavaScript:this.document.getForm.cGr.value=\""+study+"\";this.document.getForm.submit()\' onMouseOver=\'ws(\"Get "+PANM+"\"); return true;\'>");
      		getImage("get", 16, 20, " alt=\"Get "+PANM+"\"");
      		document.write("</a>");
      	}
	}
}

function comboWriteOrderGrouping(pstyiuid, cBack) {
	if (pstyiuid) {
   		document.write("<A href= \'/cases/orderGroupSplit.jsp?cBack="+cBack+"&cPSTYIUID="+pstyiuid+"\' onMouseOver=\'ws(\"View elements of Group\"); return true;\'>");
   		getImage("more", 19, 17, " alt=\"View elements of Group\"");
   		document.write("</a>");
	}
	else {
       	getImage("spacer", 19, 17);
	}
}

/**
* this function writes Batch action buttons depending on rights (used by ComboAction in Worklist)
* @param rights		String containing characters representing rights:
*								{ 'O' : Open, 'D' : Delete, 'F' : forward, 'M' : merge}
*/
function comboWriteBatch(rights) { // script writing Batch buttons
   if (rights.indexOf("O") > -1) {
      document.write("<A href= \'JavaScript: Batch_(\"Open\")\' onMouseOver=\'ws(\"Open selected studies\"); return true;\'>");
      getImage("open", 21, 20, " alt=\"Open selected studies\"");
      document.write("</a>");
   }
   if (rights.indexOf("D") > -1) {
      document.write("<A href= \'JavaScript: Batch_(\"Del\")\' onMouseOver=\'ws(\"Delete selected studies\"); return true;\'>");
      getImage("delete", 17, 20, " alt=\"Delete selected studies\"");
      document.write("</a>");
   }
   if (rights.indexOf("F") > -1) {
      document.write("<A href= \'JavaScript: Batch_(\"Fwd\")\' onMouseOver=\'ws(\"Forward/Export selected studies\"); return true;\'>");
      getImage("forward", 16, 20, " alt=\"Forward/Export selected studies\"");
      document.write("</a>");
   }
   if (rights.indexOf("M") > -1) {
      document.write("<A href= \'JavaScript: Batch_(\"Mrg\")\' onMouseOver=\'ws(\"Merge selected studies\"); return true;\'>");
      getImage("merge", 19, 17, " alt=\"Merge selected studies\"");
      document.write("</a>");
   }
   if (rights.indexOf("R") > -1) {
      document.write("<A href= \'JavaScript: Batch_(\"bAp\")\' onMouseOver=\'ws(\"Batch approve selected studies\"); return true;\'>");
      getImage("batchApprove", 18, 20, " alt=\"Batch approve selected studies\"");
      document.write("</a>");
   }
}

/**
* this function writes Batch action buttons depending on rights (used by ComboAction in "Other lists" list)
* @param rights		String containing characters representing rights:
*								{ 'D' : Delete, 'C' : compound}
*/
function comboWriteListBatch(rights) { // script writing Batch buttons
   if (rights.indexOf("D") > -1) {
      document.write("<A href= \'JavaScript: Batch_(\"BatchDel\")\' onMouseOver=\'ws(\"Delete selected lists\"); return true;\'>");
      getImage("delete", 17, 20, " alt=\"Delete selected lists\"");
      document.write("</a>");
   }
   if (rights.indexOf("C") > -1) {
      document.write("<A href= \'JavaScript: Batch_(\"Compound\")\' onMouseOver=\'ws(\"Merge selected lists\"); return true;\'>");
      getImage("merge", 19, 17, " alt=\"Merge selected lists\"");
      document.write("</a>");
   }
}

/**
* this function writes Batch action buttons depending on rights (used by ComboAction in "Task lists" list)
* @param rights		String containing characters representing rights:
*								{ 'E' : edit, 'R' : Reschedule, 'D' : delete, 'S' : suspend}
*/
function comboWriteTaskBatch(rights) { // script writing Batch buttons
	if (rights.indexOf("E") > -1) {
		document.write("<A href= \'JavaScript: Batch_(\"Edit\")\' onMouseOver=\'ws(\"Edit selected tasks\"); return true;\'>");
		getImage("edit", 18, 20, " alt=\"Edit selected tasks\"");
		document.write("</a>");
	}
	if (rights.indexOf("R") > -1) {
		document.write("<A href= \'JavaScript: Batch_(\"Reschedule\")\' onMouseOver=\'ws(\"Reschedule selected tasks\"); return true;\'>");
		getImage("addtoqueue", 21, 20, " alt=\"Reschedule selected tasks\"");
		document.write("</a>");
	}
	if (rights.indexOf("D") > -1) {
		document.write("<A href= \'JavaScript: Batch_(\"Delete\")\' onMouseOver=\'ws(\"Delete selected tasks\"); return true;\'>");
		getImage("delete", 17, 20, " alt=\"Delete selected tasks\"");
		document.write("</a>");
	}
	if (rights.indexOf("S") > -1) {
		document.write("<A href= \'JavaScript: Batch_(\"Suspend\")\' onMouseOver=\'ws(\"Suspend selected tasks\"); return true;\'>");
		getImage("suspend", 18, 20, " alt=\"Suspend selected tasks\"");
		document.write("</a>");
	}
}

