function HasExtension(theFileName, theExtension) {
	if (theFileName.length > theExtension.length) {
		return ( (theFileName.length - theExtension.length) == theFileName.lastIndexOf(theExtension));
	} else {
		return false
	}
}

function MakeWindowName(theFileName) {
	var f = "";
	var c;
	for (var i=0; i<theFileName.length; i++) {
		// only use numbers and letters to construct window name
		c = theFileName.substr(i,1);
		if ((c >= '0') && (c <= '9')) { 
			f = f+c;
		} else if ((c >= 'a') && (c <= 'z')) { 
			f = f+c;
		} else if ((c >= 'A') && (c <= 'Z')) { 
			f = f+c;
		}
	}
	return (f);
}

function MakeWindowTitle(theFileName) {
	var f = "";
	var c;
	for (var i=0; i<theFileName.length; i++) {
		c = theFileName.substr(i,1);
		if ((c >= '0') && (c <= '9')) { 
			f = f+c;
		} else if ((c >= 'a') && (c <= 'z')) { 
			f = f+c;
		} else if ((c >= 'A') && (c <= 'Z')) { 
			f = f+c;
		} else if (c == '-') { 
			f = f+c;
		} else { 
			f = f+' ';
		}
	}
	return (f);
}

function newWindowURL(theURL,width,height,aFeature,aTarget) {
	/* were optional arguments provided ?
		a. 1 argument always means:  theURL
		b. 2 arguments always means: theURL,aFeature
		c. 3 arguments always means: theURL,width,height
		d. 4 arguments always means: theURL,width,height,aFeature
		e. 5 arguments always means: theURL,width,height,aFeature,aTarget
	*/
	var hasSize     = arguments.length >= 3;
	var hasFeature  = (arguments.length == 2) || (arguments.length >= 4);
	var hasTarget   = arguments.length == 5;
	var theFeatures = 'scrollbars,resizable,menubar,toolbar,status,';
	var isIE        = (navigator.appName.indexOf("Microsoft") != -1);
	var availHeight = screen.availHeight - 120;
	if (isIE) { availHeight = availHeight - 25; } // IE windows need space for menu at bottom
	var availWidth  = screen.availWidth  - 20;
	var xPos        = 0;
	var yPos        = 0;
	if (hasFeature) {
		var extraFeature = '';
		if (arguments.length == 2) {
			extraFeature = arguments[1];
		} else { 
			extraFeature = arguments[3];
		}
		if (extraFeature == "") { // don't change any features
		} else if (extraFeature == "all") { // add the features normally left out
			theFeatures = theFeatures+'location,directories,';
		} else if ( (extraFeature == "min") || (extraFeature == "off") ) { // reduce to minimum set
			theFeatures = 'scrollbars,resizable,'; 
			availHeight = screen.availHeight + 20;
		} else {
			theFeatures = theFeatures+extraFeature+',';
		}
	}
	
	// was optional width + height provided ?
	// if yes, they are always arguments[1] & arguments[2]
	if (hasSize) {
		var w = arguments[1]; var h = arguments[2];
		if (w == 0) w = screen.availWidth;
		if (h == 0) h = screen.availHeight - 50;
		if (extraFeature == "off") {
			xPos = screen.availWidth - w;
			if (xPos < 0) { xPos = 0;  }
			yPos = 25;
		}
		if (isIE) { var w = w+15; var h = h+15; } // IE windows seem to need more size
		if (availHeight < h) h = availHeight;
		if (availWidth  < w) w = availWidth;
		theFeatures = theFeatures+'width='+w+',height='+h;
	} else { // use window defaults
		if (availWidth > 700) availWidth = 700;
		theFeatures = theFeatures+'width='+availWidth+',height='+availHeight;
	}

	// set window location using Internet Explorer
	if (isIE) {
		theFeatures = theFeatures+',left='+xPos+',top='+yPos;
	// set window location assuming Netscape Communicator
	} else { // if (navigator.appName.indexOf("Netscape") != -1)
		theFeatures = theFeatures+',screenX='+xPos+',screenY='+yPos;
	}

	// get file name portion of URL:
	// 1. follows last "/"
	// 2. need to "unescape" result just incase it is escaped (done by IE6)
	var theFileName = unescape(theURL.substr(theURL.lastIndexOf("/")+1));

	// determine if file is a graphic file
	var isGraphicFile = false;
	var ext = new Array(".gif", ".jpg", ".jpeg")
	for (var i=0; i<ext.length; i++) { 
		if (HasExtension(theFileName, ext[i])) { 
			isGraphicFile=true; 
			break; 
		}
	}
	var theTarget;
	if (hasTarget) {
		theTarget = aTarget;
	} else {
		theTarget = MakeWindowName(theFileName);
	}
	if (isGraphicFile) { // create HTML to enclose graphic file
		// page title is file name without trailing extension
		var title;
		var i = theFileName.lastIndexOf('.');
		if (i>0) { title = MakeWindowTitle(theFileName.substr(0,i)); }
		else { title = MakeWindowTitle(theFileName); }
		var w = window.open('',theTarget,theFeatures);
		var d = w.document;
		d.writeln('<html>');
		d.writeln('<head>');
		d.writeln('<title>'+title+'</title>');
		d.writeln('</head>');
		d.writeln('<body bgcolor="white">');
		d.writeln('<center>');
		d.writeln('<h2 style=\'font-size: 14pt; font-style: normal; font-weight: bold; color: black; font-family: \"Times New Roman\", serif;\'>'+title+'</h2>');
		d.writeln('<img src="'+theURL+'" alt="'+theFileName+'" hspace=0 vspace=0 border=0 align=left>');
		d.writeln('</center>');
		d.writeln('</body>');
		d.writeln('</html>');
		d.close();
	} else { // non-graphic file (assume some text file) just open it
		var w = window.open(theURL,theTarget,theFeatures);
	}
	return false;
}
