// use this file to control VTJ by doing these things to your html document:

// 1. in your <script> block, you can define this variable;
//		var imagingURL = '/Acordex/viewimage.html';	URL to viewimage.html file ,If omitted, default will be /Acordex/viewimage.html

// 2. after your script block ends, add this line: 	<script src="[path to file]/display.js" type="text/javascript" language="javascript"></script>

// 3. Now you can call: DisplayImage(newDocName); anywhere in your html document, or more important, you can call
//		parent.DisplayImage(newDocName); from a frame when your html document is a frameset
//		opener.DisplayImage(newDocName); from a window that you open from your html document 
// 'newDocName' is the index name/image, (i.e. BillsofLadingImages/012345678912)
//
//
// This HTML link below will display and image, either by opening directly from the link if a pop-up
// blocker prevents opening a window from Javascript, or through javascript which can replace the display image
// with a new one without reloading the HTML (a slight time savings). Construct a link to display an image as follows:
//
//  <A href='viewimage.html#docname' target=ImageViewer onClick='return !DisplayImage("BillsofLadingImages/012345678912");' title='View Bill of Lading'
//		onMouseOver='self.status="View Bill of Lading"; return true;'
//		onMouseOut='self.status=""; return true;'>Original Bill of Lading Image</a>
// Tested on Windows for IE, Firefox, Netscape, Opera.
// Tested on Mac for IE, Firefox, Netscape, Opera, Camino
if ((typeof vtj) == "undefined") var vtj = null;
if (((typeof documentName)== "undefined") || documentName== null) var documentName = "";
if (((typeof imagingURL)  == "undefined") || imagingURL  == null) var imagingURL = "/Acordex/viewimage.html";

// IE uses different screen positioning (see below)
var isIE = (navigator.userAgent.indexOf("MSIE") >= 0);
var isOpera = (navigator.userAgent.indexOf("Opera") >= 0);

// used to detect a pop-up blocker that stops the Javascript
var popupBlocked = false;

// This function will return false if the window cannot be opened (pop-up blocker), this allows an HTML link to directly
// open the window if DisplayImage() called from 'onClick' function of the link and fails
function DisplayImage(newDocName) {
	// this variable is picked up by 'viewimage.html' and used to display the image:
	documentName=newDocName;
	if (isOpera) CheckClosed();   // workaround for opera bug that does not detect window closed
	if (vtj == null || vtj.closed) {	// if viewing in separate window, and window not opened yet or was closed...
		// some pop-up blockers will kill the javascript right at window.open, so we set a flag here so at least
		// on the next click we won't try and window will be opened directly from the link
		if (popupBlocked) return(false);
		popupBlocked = true;
		// These sizes (650 by 860) show an 8 by 11 page at full size
		bestWidth = 650;
		bestHeight = 860;
		winHeight = screen.availHeight - 50;	// leave room for windows start menu bar, and status line
		winWidth = screen.availWidth - 10;
		xpos = 0;
		ypos = 0;
		if (winWidth > bestWidth) {
			xpos = (winWidth - bestWidth) - 10;		// place window on right side
			winWidth = bestWidth;
			}
		if (winHeight > bestHeight) {
			ypos = (winHeight - bestHeight) / 2;	// center window vertically
			winHeight = bestHeight;
			}
		if (isIE) screenPosOption = "left="+xpos+",top="+ypos;
		else screenPosOption = "screenX="+xpos+",screenY="+ypos;
		// if 'viewimage.html' comes from same server as this javascript, you may eliminate ' + "#" + escape(documentName) ' from below to remove
		// image name from URL. unescape converts special characters to %NN notation
		vtj = window.open(imagingURL + "#" + escape(documentName) , "ImageViewer", "width="+winWidth+",height="+winHeight+"," + screenPosOption + ",status=yes,resizable=yes,address=yes");
		popupBlocked = false;
		if (vtj == null || vtj.closed) // pop-up blocker detected
			return false; // return false, we failed to display the image, the link will bring up image manually
	}
	else {	// window is open, just tell applet new document name
		fetchImage(newDocName);
	}
	return true;
}

// Certain browsers (e.g. IE on Macintosh) do not support calls from Javascript to Java, so we must reload the applet
// Some browsers cannot handle onerror either, so we have to detect whether the JavaScript to Java fails.
var saveOnError = window.onerror;
var errorOccured;
var noJS2J = false;

function reloadVTJ(msg, url, line) {
	noJS2J = true;
	errorOccured = true;
	window.onerror = saveOnError;
	// if 'viewimage.html' comes from same server as this javascript, you may use the line below instead to remove image
	// name from URL
	// vtj.location = vtj.location.href;
	vtj.location.replace(imagingURL + "#" + escape(documentName));
	return true;
}

function fetchImage(newDocName) {
	window.onerror = reloadVTJ;
	vtj.focus();
	if (noJS2J) {
		reloadVTJ("No JavaScript to Java", "self", "0");
	} else {
		errorOccured = false;
		noJS2J = true;
		if ((typeof vtj.document.viewer) == "undefined") {
			reloadVTJ("No JavaScript to Java", "self", "0");
			noJS2J = false;	// this is probably a user double-click, so don't give up on JS2J
			return;
		}
		vtj.document.viewer.setDocName(newDocName);
		noJS2J = errorOccured;
		window.onerror = saveOnError;
	}
}
	
// Work around for opera bug which does not set 'window.closed' variable when a browser window closes. So
// we must test if window was opened by attempting to focus window
function CheckClosed()
{
	if (vtj != null && !vtj.closed) {	// if we beliwe window is open
		try {
			vtj.focus();		// this fails if window is really closed
		} catch (err) {
			vtj = null;
		}
	}
}
