// JavaScript function library

// Handle click on "Close this window" button

function closeWindow() {
	window.close();
}

// Extract the file name from a path or URI

function path_to_filename(path) {
	var last_slash = path.lastIndexOf("/");
	return path.substring(last_slash+1);
}

// Remove extension from a filename

function filename_no_ext(fn) {
	var dot = fn.lastIndexOf(".");
	return (dot >= 0) ? fn.substring(0,dot) : fn;
}

function random_number(lower_bound,upper_bound) {
	var initial_random = (upper_bound-lower_bound+1)*Math.random()+lower_bound;
	return Math.floor(initial_random);
}

// Handle click on "Update quantity" button on product pages with order form

function submit_to(thePage,theTarget) {
	with (document.forms.orderForm) {
		action = thePage;
		target = theTarget;
		submit();
	}
}

// Make fixed-position popup window for articles

function aw(link) {
	var w = 670, h = 400, winl = (screen.width-w)/2 + 20, wint = (screen.height-h)/2 + 20;
	var	winprops = 'scrollbars,resizable,width=' +w+ ',height=' +h+ ',left=' +winl+ ',top=' + wint;
	var win = window.open("",link.target,winprops);
	win.focus();	// for Netscape
	return true;
}

// Make fixed-position mini-order window for products
// Called from: Product Information button at bottom of article pages

function ow(link) {
	var w = 670, h = 400, winl = (screen.width-w)/2 + 20, wint = (screen.height-h)/2 + 20;
	var	winprops = 'scrollbars,resizable,width=' +w+ ',height=' +h+ ',left=' +winl+ ',top=' + wint;
	var win = window.open("",link.target,winprops);
	win.focus();	// for Netscape
}

function make_order_window(link) {ow(link)};

// Create window for Order Summary and make it the target of the order form
// Called from: All pages linking to the Order Summary page
// Returns the name of the popup window for use by sb()

function sw(dest) {
	var winname = 'OrderSummary';
	var w = 600, h = 300, winl = (screen.width-w)/2, wint = (screen.height-h)/2;
	var winprops = 'scrollbars,resizable,width=' +w+ ',height=' +h+ ',left=' +winl+ ',top=' + wint;
	var win = window.open(dest,winname,winprops);
	win.focus();	// for Netscape
	return win ? winname : '_self';
}

// Set target of form
// Called from: Submit buttons on Order Form and Specific Ordering Page,
//				Checkout button on Order Summary page
// Returns true to allow submit to proceed

function st(frm,targ) {
	frm.target = targ;
	return true;
}

// Post product quantities and navigate to a different page
// Called from: Nav buttons and links on Order Form
// Returns false to prevent hyperlink activation

function sb(dest,targ) {
	with (document.forms.orderForm) {
		destination.value = dest;
		target = targ;
		submit();
	}
	return false;
}

// Make randomly-staggered windows for product info

function pw(link) {
	var w = 670, h = 300, rand = random_number(-2, 2);
	winl = (screen.width-w)/2 + 10 * rand;
	wint = (screen.height-h)/2 + 20 * rand;
	var	winprops = "scrollbars,resizable,width=" +w+ ",height=" +h+ ",left=" +winl+ ",top=" + wint;
// New...
	with (link) {
		var fn = path_to_filename(href);		// Build a unique window name
		var abbr = filename_no_ext(fn);			//	for each product
		target = abbr.replace(/\W/g,""); 		// IE doesn't like hyphens in a target
		var win = window.open("",target,winprops);			// Supply href as first arg if necessary
		win.focus();	// for Netscape
	}
	return true;
}

// The onClick handler for substance/usage hyperlinks. Opens a popup window and generates the hit list. FOR INDEX

function rw(link) {
	var w = 400, h = 350, winl = 40, wint = 40;
	var winprops = 'status,scrollbars,resizable,copyhistory,width=' +w+ ',height=' +h+ ',left=' +winl+ ',top=' + wint;
	var	pListWindow = window.open("",link.target,winprops);
		pListWindow.focus();	// for Netscape

	// Get the list of products from the search portion of the link the user clicked. (format: '?products=i,j,k&term=foo')
	var query = link.search.substring(1);	// Drop question mark
	var termText = link.innerHTML;
	var nvList = query.split("&");
	var nvItem, products, term;
	for (var i=0; i<nvList.length; i++) {
		nvItem = nvList[i].split("=");
		if (nvItem[0] == "products") { products = unescape(nvItem[1]) };	// 'i,j,k'
		if (nvItem[0] == "term") { term = unescape(nvItem[1].replace(/\+/g,' ')) };	// most browsers will use termText instead
		}
	var prodArray = products.split(",");
	if (!termText) termText = term;		// NN4, Opera don't understand innerHTML?
	
	// Start the search results document like the product index starts.
	var	stylesheet = '<'+'style>\nbody {font-family: Verdana,Arial,Helvetica,sans-serif; background-color: #ffd;}\np.instructions {font-size: 10pt; color: black;}\nspan.term {font-size: 12pt; font-weight: 600; color: #CD5C5C;}\np.fileList {font-size: 10pt; color: teal;}\n<\/style>';
	
	with (pListWindow.document) {
		write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">\n');
		write('<html lang="en"><head>\n');
		write('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">\n');
		write('<title>Products to choose from<\/title>\n');
		write('<meta http-equiv="Content-Style-Type" content="text/css">\n');
		write('<meta http-equiv="Content-Script-Type" content="text/javascript">\n');
		write('<base href="' + self.location + '">\n');
		write('<'+'script type="text/javascript" src="js-files/functions.js" defer><\/script>\n');
		write(stylesheet+'\n');
		write('<\/head>\n');
		
		// Format the term and product information.
		write('<body>\n');
		write('<p class="instructions">The following product(s) are relevant to the term<br><span class="term">&#8220;'+termText+'&#8221;<\/span>&nbsp;:<\/p>\n');
	
		var path,index;
		for (var i=0; i<prodArray.length; i++) {
			index = parseInt(prodArray[i],10) - 1;
			path = 'ProductPages/' + fn[index] + '.asp';
			write('<p class="fileList"><a target="_blank" onClick="return pw(this)" href="' + path + '">' + pn[index] + '<\/a><\/p>\n');
		}

		write('<\/body><\/html>\n');
		close();
		return false;
	}
}

