// <script>

var g_oExcelWindow = null;
var g_oPDFWindow = null;
var g_oExcelForm;
var g_sPDFPath;
var g_sPDFTarget;

//
//  The sDocumentPath should be a root relative path to the PDF document
//  (e.g. "/MSDS/msds1.pdf").  This is necessary, since if the PDF document is loaded from
//	a META refresh tag, it will need the absolute address (determined by a Session
//	variable prepened to the path).  However, if the PDF document is loaded right away into
//	the window by the function below, a relative address is needed and created by prepending
//  a ".." to the root relative path. 
//
//	If the "bOpenQuickly" flag is set to TRUE, then a javascript function will determine
//	if the window is open and can receive the PDF file.  This is quick, but on some browsers, the
//  DownloadPDFFileInProgress window fails to fully load before it begins opening the PDF file.
//
//  If the "bOpenQuickly" flag is set to FALSE, the window will be opened normally
//	and the PDF document will be loaded into it when the DownloadPDFFileInProgress page 
//  refreshes from a dynamically generated META tag.
//
function loadPDFFile (sDocumentPath,sTarget,bOpenQuickly) {
	g_sPDFPath = sDocumentPath;
	g_sPDFTarget = sTarget;
	if (bOpenQuickly) {
		g_oPDFWindow = window.open("../Login/DownloadPDFFileInProgress.asp?sURL=None",g_sPDFTarget);
		checkIfPDFWindowReadyForFile();
	} else {
		g_oPDFWindow = window.open("../Login/DownloadPDFFileInProgress.asp?sURL=" + escape(sDocumentPath),g_sPDFTarget);
	}
}

function checkIfPDFWindowReadyForFile() {	
	var sLocationURL;
	var iIndexCheck;
	if (g_oPDFWindow && g_oPDFWindow.open) {
		sLocationURL = g_oPDFWindow.location.href;
		iIndexCheck = sLocationURL.lastIndexOf("DownloadPDFFileInProgress.asp", sLocationURL.length - 1);
//		If the DownloadPDFFileInProgress.asp page has NOT been loaded yet, keep checking...
		if (-1 == iIndexCheck) {
			setTimeout("checkIfPDFWindowReadyForFile()",500,"Javascript");
			return;
		} else {
//		...otherwise, go ahead and load the PDF file in the window.
//		Note that the root relative address is changed to a relative address
//		for the client-side redirection to occur properly.		
			g_oPDFWindow.location.href = ".." + g_sPDFPath;
		}
	}
}


//
//	Most of the pages loading Excel files use forms to submit information,
//	so this function takes a FORM tag as a variable, extracts the info it needs
//	to open the browser window, then submits the form.
//
function loadExcelFile(form){
	g_oExcelForm = form;
	g_oExcelForm.submit();
//	g_oExcelWindow = window.open("../Login/DownloadExcelFileInProgress.asp",form.target);
//	checkIfExcelWindowReadyForFile();
}

function checkIfExcelWindowReadyForFile() {	
	var sLocationURL;
	var iIndexCheck;
	if (g_oExcelWindow && g_oExcelWindow.open) {
		sLocationURL = g_oExcelWindow.location.href;
		iIndexCheck = sLocationURL.lastIndexOf("DownloadExcelFileInProgress.asp", sLocationURL.length - 1);
//		If the DownloadExcelFileInProgress.asp page has NOT been loaded yet, keep checking...
		if (-1 == iIndexCheck) {
			setTimeout("checkIfExcelWindowReadyForFile()",500,"Javascript");
			return;
		} else {
//		...otherwise, go ahead and load the Excel file in the window		
			g_oExcelForm.submit();		
		}
	}
}
