// --------------------------------------------------------
//  File: inet_js_code.js
// --------------------------------------------------------

var gContactUsOn = false;
var gShowPricesOn = false;
var gWeCanUseCookies = false;
var gWeAreInstalledLocallyOnAHardDisk = true;
var gOurCookies = "";
var gReferrer = "";
var gReferrer_Displayed_Name = "";
var gP4 = "";
var gP9 = "";
var gP9_SessionSummary = "";
var gContactUsOn_redirectTargetPage   = false;
var gWeAreOnMacintosh = false;
var gBrowserIs_Netscape  = false;
var gBrowserIs_IE = false;
var gBrowserIs_Firefox = false;
var gBrowserIs_Mozilla = false;
var gBrowserIs_Safari = false;
var gShowPricesOn_redirectTargetPage  = false;
var gP4_redirectTargetPage = "";
var gP9_redirectTargetPage = "";
var gWeAreRedirectingToPriceListPage = false;
var gDocReferrer = "";
var gFirstUrlOfSession = "";
var gRedirectToThisNewUrl = "";
var gNumTimesCreatedCookie = 0;
var sCookie_name    = 'instrunetcookie';
var sCookie_path    = '\/';
var sCookie_domain  = '';  // this would need to be www.instrunet.com
var gPrimaryInterest = "";
var gNeedToTest_readyState = true;

//	--------------------------------------------------------
//	enables debugging

var gDebuggingAlertsOn = false;
var gDebuggingCookies = false;
var gDonChangeSearchOnAll_A_Links = true;  // <<< this is buggy on local hd w/ # anchors and buggy on internet

//	--------------------------------------------------------
//	Process_A_Front_End_Redirection_HEAD()   				-- place in HEAD
//	Process_A_Front_End_Redirection_BODY()   				-- place in BODY
//	Process_A_Front_End_Redirection_onreadystatechange()	-- we attach this to the document & it runs after doc becomes ready
//
// 	The page that is being loaded is redirected to another page. Process_A_Front_End_Redirection_HEAD()
//	excutes in the head before the page is loaded. 
//	And Process_A_Front_End_Redirection_BODY()
//	executes in the body, when loading it.
function Process_A_Front_End_Redirection_HEAD( newUrl )
{
	// alert('Process_A_Front_End_Redirection_HEAD');

	//	----------------------------
	//	interrogate ?x=y params at end of new url, which is the target
	//	page that we are redirecting to

	gContactUsOn_redirectTargetPage   = (GetParameter_PassParamsToJavaScript(newUrl, 3) == 'c');
	gShowPricesOn_redirectTargetPage  = (GetParameter_PassParamsToJavaScript(newUrl, 8) == 'p');
	gP4_redirectTargetPage            =  GetParameter_PassParamsToJavaScript(newUrl, 4);
	gP9_redirectTargetPage            =  GetParameter_PassParamsToJavaScript(newUrl, 9);

	//	----------------------------
	//	CREATE COOKIE FOR SESSION
	//	
	//	We now run some routines before loading (e.g. printing body). In here,
	//	we will see who the referrer is and then CREATE A cookie based on that
	//	info (e.g. disti is refer).
	//
	//	CAREFUL:  BODY text may not have loaded yet, therefore we cannot operate
	//	operate ON object id's or things that have not yet been loaded.
	inet_interrogate_situation_before_loading(); 
  
	//	----------------------------
	//	remember the new url that we will jump to
	//  alert('new url = ' + newUrl);
	gRedirectToThisNewUrl = newUrl;

	//	----------------------------
	// run the Website tracker if we are on the internet
	Track_iNet_Page_If_On_Internet(document);

	//	----------------------------
	//	dataplain does not work with: when document activates, we will run this routine
	// 		window.onactivate = Process_A_Front_End_Redirection_onactivate();
	// 		window.document.onload = Process_A_Front_End_Redirection_onactivate();

	return;
}

function Process_A_Front_End_Redirection_BODY()
{
	//	we redirect after the document is loaded (i.e. ready = complete)
	//	because dataplain/search engines will not recognize this thing if it does
	//	not fully load to the point of becoming ready
	
	//	ref: http://dean.edwards.name/weblog/2005/02/order-of-events/
	//	alert('Process_A_Front_End_Redirection_BODY');

	//	onreadystatechange is supported by IE (and not may others)
	if (typeof document.onreadystatechange != 'undefined')	{

		// alert('onreadystatechange isr being added');
		gNeedToTest_readyState = true;
		document.onreadystatechange = Process_A_Front_End_Redirection_onreadystatechange;
	}
	else if (gBrowserIs_Mozilla || gBrowserIs_Firefox)	{

		// mozilla supports this DOMContentLoaded event which runs after the doc is loaded
		// alert('Mozilla or Firefox DOMContentLoaded');
		gNeedToTest_readyState = false;
		document.addEventListener("DOMContentLoaded", Process_A_Front_End_Redirection_onreadystatechange, null);
	}
	else	{

		//	We redirect immediately. Dataplain does
		//	not work well here, yet at least we can get the redirection running
		gNeedToTest_readyState = false;
		Process_A_Front_End_Redirection_onreadystatechange();
	}

	return;
}

function Process_A_Front_End_Redirection_onreadystatechange()
{
	//	NOTE: WAITING FOR THE READYSTATECHANGE THING INVOLVES A 2 to 4SEC DELAY !
	//	NOTE: WAITING FOR THE READYSTATECHANGE THING INVOLVES A 2 to 4SEC DELAY !
	//	NOTE: WAITING FOR THE READYSTATECHANGE THING INVOLVES A 2 to 4SEC DELAY !

	// alert('document.readyState = ' + document.readyState + ", gNeedToTest_readyState = " + gNeedToTest_readyState);

	//	if we are responding to a readstagechange event, we need to test
	//	the readystate flag to see if the document is loaded
	if (gNeedToTest_readyState)	{
	
		var docIsLoaded = true;
	
		//	Mac sends "interactive" msg and windows sends "complete" msg\
		//	We test below if readyState is supported by this browser
		if (typeof document.readyState != 'undefined') {
			docIsLoaded = (gWeAreOnMacintosh ? (document.readyState == 'interactive') : (document.readyState == 'complete'));
		}
	
		//	if the doc is not yet completely loaded, then exit
		if ( !docIsLoaded ) { return; }
	}

	// used for debugging
	if (gDebuggingCookies) { alert('Process_A_Front_End_Redirection_onreadystatechange()'); }
	if (gRedirectToThisNewUrl == "") { alert('ERROR www.instrunet.com Process_A_Front_End_Redirection_BODY'); }

	// replace this url w/ new url. This supports the BACK button.
	// same as: window.open(gRedirectToThisNewUrl, '_self', '', true); 
	Redirect_to_another_url( gRedirectToThisNewUrl );

	//	NOT USED: we will redirect in 300mSec, to give search engines/dataplain time
	//	to see this page
	// window.setTimeout('Process_A_Front_End_Redirection_AFTER_onreadystatechange()', 300); 

	return;
}

function Process_A_Front_End_Redirection_AFTER_onreadystatechange()
{
	//	not used
	return;
}

function Redirect_to_another_url( newUrl )
{
	//	this does not run on jscript 1.0 (1.1 is ok),
	//	yet ie >=4 has jscript >= 1.1. If one does not have
	//	jscript 1.1,
	window.location.replace( newUrl );
}

//	--------------------------------------------------------
//	print email link to browser
function Print_EmailTo_link(emailAddr, subjectText_wSpaces, bodyText_wSpaces, linkText)
{
	var bodyText = bodyText_wSpaces.replace(' ', '%20');

	var subjectText = subjectText_wSpaces.replace(' ', '%20');

	var t = ' <A CLASS=\"contact\" HREF=\"mailto:' + emailAddr;

	if (gWeAreOnMacintosh)  { 								// Macintosh
		t += '\?subject\=' + subjectText + '\?body\=' + bodyText; }
	else if (gBrowserIs_IE) { 								//	IE
		t += '\?subject\=' + subjectText + ' &amp; ' + 'body\=' + bodyText; }
	else { 
		t += '\?body\=' + bodyText; }

	t += '\">' + linkText + '<\/A>';

	// alert(t);
	document.write(t);
	return;
}

//	--------------------------------------------------------
//	inet_interrogate_situation_before_loading()
//
//	This is excuted in the head, before the page loads and before
//	the on load event. This is useful at calculating vars that
//	determine how to print the body (thats why we run this in the
//	head).  WHEN THIS CODE EXECUTES, THE BODY text may not have loaded
//	yet, therefore we cannot operate ON object id's or things that have
//	not yet been loaded.
//	
//	parameters tacked to the end of url (e.g. "index.html?1=x,2=y,3=z")
//  parameter 3    if this = "c,", we show "Contact Us" & EmailUs button; otherwise we don't
//  parameter 8    if this = "p, we show usa prices; otherwise we don't
//  parameter 4    if this != "", then we search this text for a distributor name
//  parameter 9    XYZZZ, if X=c contact is on, if Y = p prices are on, ZZZ = 1st 3 letters of disti
function inet_interrogate_situation_before_loading()
{ 
	var valueOfSavedCookie = "";

	// alert("begin inet_interrogate_situation_before_loading");

	//	if we are not loading this page, and are instead redirecting
	//	to another page, then don't get involved in initializing
	//	cookie state and other variables in here (we'll do that later)
	if (gWeAreRedirectingToPriceListPage) { return; }

	// get url to this page
	var href_url = "";
    if (location) { if (location.href)	{ href_url = location.href.toLowerCase(); } } 
	
    //	get name of browser
    var agt = navigator.userAgent.toLowerCase();

	//	determine if we are on a mac or a windows computer
	gWeAreOnMacintosh = (agt.indexOf("mac") != -1);

    // If you want to allow spoofing, take out the tests for opera and webtv.
	//
	//	netscape pc/mac
	//
	//	mozzila firefox mac/pc
	//		- Mozilla browsers don’t support onreadystatechange, so the results won’t mean much on that platform. 
	//
	//	apple safari mac
	//
	//	ref: http://1-800-volunteer.org/1800Vol/admin/js/util.js;

	if (agt.indexOf('safari') != -1) { gBrowserIs_Safari = true; }
    else if (agt.indexOf('netscape')!=-1) { gBrowserIs_Netscape  = true; }
	else if ((navigator.appName == 'Microsoft Internet Explorer') || ((agt.indexOf('msie') != -1) && (agt.indexOf('opera') == -1))) { gBrowserIs_IE  = true; }
	else if (agt.indexOf("firefox") != -1) { gBrowserIs_Firefox = true; }
	else if (navigator.product == 'Gecko' && navigator.appName == 'Netscape') { gBrowserIs_Mozilla = true; }	

	// Calc if we are running off a local hard disk or from the internet
	if (href_url != "") {
		if (href_url.substring(0, 4) == "http") {
     		gWeAreInstalledLocallyOnAHardDisk = false;
			if (gDebuggingAlertsOn) { alert("we are on internet"); }
		}
	}

	// calc if we can use cookies
	if ( window.navigator.cookieEnabled ) { gWeCanUseCookies = true; }

	if (gDebuggingCookies) { alert('document.cookie = ' + document.cookie); }

	// see if we should show the special buttons
	if (gWeCanUseCookies) {

		if (gDebuggingAlertsOn) { alert("we can use cookies"); }
		gOurCookies = get_cookie( sCookie_name );
		if (gDebuggingAlertsOn || gDebuggingCookies) { alert('Saved gOurCookies = ' + gOurCookies + ', this page href_url = ' + href_url ); }

		//	-------------------------------------------
		//	if the cookie is empty, assume we are just starting
		//	our browser session (we are on session page #1) and need to 
		//	look at url suffix & document referer (url of previous page
		//	before hitting our website) to see what we should show

		if ((gOurCookies == "") || (gOurCookies == null)) {

			//	init
			var gTestReferrerCode = false;
			var gPrintReferrerNameIntoWindowForTesting = false;
			var ref = "";
			var g3CharDistiNameInUrl = "";

			//	interogate ?x=y params at end of url
  			gContactUsOn  = (GetParameter_PassParamsToJavaScript(href_url, 3) == 'c');
   			gShowPricesOn = (GetParameter_PassParamsToJavaScript(href_url, 8) == 'p');
   			gP4           =  GetParameter_PassParamsToJavaScript(href_url, 4);
   			gP9           =  GetParameter_PassParamsToJavaScript(href_url, 9);

			//	if someone enterred the website w/o the ?... suffix params, and
			//	we are redirecting to another page; then use the ?...suffix params
			//	on the target redirection page if they exist
			if (gContactUsOn_redirectTargetPage)  { gContactUsOn = true; }
			if (gShowPricesOn_redirectTargetPage) { gShowPricesOn = true; }
			if ((gP4 == "") && (gP4_redirectTargetPage != "")) { gP4 = gP4_redirectTargetPage; }
			if ((gP9 == "") && (gP9_redirectTargetPage != "")) { gP9 = gP9_redirectTargetPage; }

			//	if we have a param #9 in the url...
			if (gP9 != "") {  
				//	if X in "?9=XYZZZ" tell us to turn on the contact us...
				if (gP9.substring(0, 1).toLowerCase() == 'c') { gContactUsOn = true; } 
				//	if Y in "?9=XYZZZ" tell us to turn on the prices..
				if (gP9.substring(1, 2).toLowerCase() == 'p') { gShowPricesOn = true; } 
				//	see if there is a 3 char disti name in the url (i.e. "?9=XYZZZ"
				//	where ZZZ is the 3char code); and if so, we look for that as well
				//	in our search for a disti
				g3CharDistiNameInUrl = gP9.substring(2, 5).toLowerCase(); 
			}

			//	if there is no 3char disti name in p9 then look for this in p4
			if ((g3CharDistiNameInUrl == "") && (gP4 != ""))	{
				g3CharDistiNameInUrl = gP4.substring(0, 3).toLowerCase(); 
				// alert('g3CharDistiNameInUrl = ' + g3CharDistiNameInUrl);
			}

			if (gDebuggingAlertsOn) { alert('gContactUsOn = ' + gContactUsOn + ', gShowPricesOn = ' + gShowPricesOn + ', gP4 = ' + gP4 + ', gP9 = ' + gP9); }

			//	get referring website (i.e. url of website before
			//	hitting this website)
			if (document.referrer) { if (document.referrer != "") { 
				ref = document.referrer.toLowerCase(); 
				gDocReferrer = ref;
			} }

			//	try to determine primary interest of viewer (e.g. thermocouple,
			//	strain gage, thermistor). We first look at referrer, and
			//	if we don't see it here, we look at the current url
			//	e.g. (thermocouple.html)
			gPrimaryInterest = determine_interest( gDocReferrer );
			if (gPrimaryInterest == "") {
				gPrimaryInterest = determine_interest( href_url );
			}

			//	get url of 1st page that is loaded for the session
			//	we remove preceeding stuff (e.g. http, file path) to
			//	make this easier to read
			if (href_url != "")	{
				var startChar_FirstUrlOfSession = 0;
				var http_index = 0;
				if (gWeAreInstalledLocallyOnAHardDisk)	{
					if ((http_index = href_url.indexOf('instrunet_website_local_org_files')) >= 0)	{
						startChar_FirstUrlOfSession = http_index; }
					else if ((http_index = href_url.indexOf('instrunet web site original')) >= 0)	{
						startChar_FirstUrlOfSession = http_index; }
					else if ((http_index = href_url.indexOf('instrunet%20web%20site%20original')) >= 0)	{
						startChar_FirstUrlOfSession = http_index; }
				}
				else {
					http_index = href_url.indexOf('http:\/\/');
					if (http_index >= 0)	{
						startChar_FirstUrlOfSession = 7;
					}
				}
				gFirstUrlOfSession = href_url.substring(startChar_FirstUrlOfSession, href_url.length);
				// alert('gFirstUrlOfSession = ' + gFirstUrlOfSession);
			}

			//	if there is no referring website and we have a p4 param, then
			//	scan the p4 param for a disti name
			if ((ref == "") && (gP4 != ""))  { ref = gP4.toLowerCase(); }
		
			//	see if referrer is a search engine or gwi website or "?4=gwi" or "?9=XYZZZ"
			if (    (ref.indexOf("search.yahoo")   >= 0)    ||
					(ref.indexOf(".google.")       >= 0)    ||
					(ref.indexOf(".altavista.")    >= 0)    ||
					(ref.indexOf(".dogpile.")      >= 0)    ||
					(ref.indexOf("dmoz.")          >= 0)    ||
					(ref.indexOf("search.msn")     >= 0)    ||
					(ref.indexOf("guide.apple")    >= 0)    ||
				    (ref.indexOf("gwinst")         >= 0)    || 
					(ref.indexOf("gw instruments") >= 0)    ||
					(g3CharDistiNameInUrl == "gwi")) {

				gReferrer = "gwi";
				gReferrer_Displayed_Name = "GW Instruments, Inc";
				gContactUsOn = true;  
			}

			//	see if referrer is a distributor
			else if ((ref.indexOf("omega") >= 0) || (g3CharDistiNameInUrl == "ome") || gTestReferrerCode) {
				gReferrer = "ome";
				gReferrer_Displayed_Name = "www.omega.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("cyberres") >= 0) || (g3CharDistiNameInUrl == "cyb")) {
				gReferrer = "cyb";
				gReferrer_Displayed_Name = "www.cyberresearch.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("peekel") >= 0) || (g3CharDistiNameInUrl == "pee")) {
				gReferrer = "pee";
				gReferrer_Displayed_Name = "www.peekel.nl";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("sanlien") >= 0) || (g3CharDistiNameInUrl == "san")) {
				gReferrer = "san";
				gReferrer_Displayed_Name = "www.sanlien.com.tw";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("ntt") >= 0) || (g3CharDistiNameInUrl == "ntt")) {
				gReferrer = "ntt";
				gReferrer_Displayed_Name = "www.ntt.dk";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("dycor") >= 0) || (g3CharDistiNameInUrl == "dyc")) {
				gReferrer = "dyc";
				gReferrer_Displayed_Name = "www.dycor.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("novotek") >= 0) || (g3CharDistiNameInUrl == "nov")) {
				gReferrer = "nov";
				gReferrer_Displayed_Name = "www.novotek.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("memstec") >= 0) || (g3CharDistiNameInUrl == "mem")) {
				gReferrer = "mem";
				gReferrer_Displayed_Name = "www.memstec.com.tw";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("instrumentation.it") >= 0) || (g3CharDistiNameInUrl == "inz")) {
				gReferrer = "inz";
				gReferrer_Displayed_Name = "www.instrumentation.it";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("dipsi") >= 0) || (g3CharDistiNameInUrl == "dip")) {
				gReferrer = "dip";
				gReferrer_Displayed_Name = "www.dipsi.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("analysa") >= 0) || (g3CharDistiNameInUrl == "ana")) {
				gReferrer = "ana";
				gReferrer_Displayed_Name = "www.analysa.it";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("algosys") >= 0) || (g3CharDistiNameInUrl == "alg")) {
				gReferrer = "alg";
				gReferrer_Displayed_Name = "www.algosys.fr";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("aimil") >= 0) || (g3CharDistiNameInUrl == "aim")) {
				gReferrer = "aim";
				gReferrer_Displayed_Name = "www.aimil.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("additive") >= 0) || (g3CharDistiNameInUrl == "add")) {
				gReferrer = "add";
				gReferrer_Displayed_Name = "www.additive-net.de";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("measx") >= 0) || (g3CharDistiNameInUrl == "mea")) {
				gReferrer = "mea";
				gReferrer_Displayed_Name = "www.measx.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}


			else if ((ref.indexOf("toyo") >= 0) || (g3CharDistiNameInUrl == "toy"))  {
				gReferrer = "toy";
				gReferrer_Displayed_Name = "www.toyo.co.jp";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("amplicon") >= 0) || (g3CharDistiNameInUrl == "amp")) {
				gReferrer = "amp";
				gReferrer_Displayed_Name = "www.amplicon.co.uk";
				gContactUsOn = false;  gShowPricesOn = false;
			}

			//	this is new as of 4/21/09
			else if ((ref.indexOf("measys") >= 0) || (ref.indexOf("measurementsystems") >= 0) || (gP4 == "msy") || (g3CharDistiNameInUrl == "msy")) { 
				gReferrer = "msy";				
				gReferrer_Displayed_Name = "www.measys.com";	// www.measurementsystems.co.uk  = www.measys.com
				gContactUsOn = false;  gShowPricesOn = false;
			}

/*
			else if ((ref.indexOf("additive-net") >= 0) || (g3CharDistiNameInUrl == "add")) {
				gReferrer = "add";
				gReferrer_Displayed_Name = "shop.additive-net.de";
				gContactUsOn = false;  gShowPricesOn = false;
			}
*/
			else if ((ref.indexOf("scitech") >= 0) || (g3CharDistiNameInUrl == "sci")) {
				gReferrer = "sci";
				gReferrer_Displayed_Name = "www.scitech.com.au";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("a-tech") >= 0) || (g3CharDistiNameInUrl == "ate")) {
				gReferrer = "ate";
				gReferrer_Displayed_Name = "www.a-tech.ch";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("savenhitech") >= 0) || (gP4 == "sav")) {
				gReferrer = "sav";
				gReferrer_Displayed_Name = "www.savenhitech.se";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("sanlien") >= 0) || (g3CharDistiNameInUrl == "san")) {
				gReferrer = "san";
				gReferrer_Displayed_Name = "www.sanlien.com.tw";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("koreatechmeas") >= 0) || (gP4 == "kor")) {
				gReferrer = "kor";
				gReferrer_Displayed_Name = "www.koreatechmeas.com";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("pimzos") >= 0) || (g3CharDistiNameInUrl == "pim")) {
				gReferrer = "pim";
				gReferrer_Displayed_Name = "www.pimzos.nl";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("geitmann") >= 0) || (g3CharDistiNameInUrl == "gei")) {
				gReferrer = "gei";
				gReferrer_Displayed_Name = "www.geitmann.de";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf("fieldworks.nl") >= 0) || (g3CharDistiNameInUrl == "fie")) {
				gReferrer = "fie";
				gReferrer_Displayed_Name = "www.fieldworks.nl";
				gContactUsOn = false;  gShowPricesOn = false;
			}
			else if ((ref.indexOf(".optocom.com") >= 0) || (g3CharDistiNameInUrl == "opt")) {
				gReferrer = "opt";
				gReferrer_Displayed_Name = "www.optocom.com.my";
				gContactUsOn = false;  gShowPricesOn = false;
			}

			if (gPrintReferrerNameIntoWindowForTesting) { gReferrer_Displayed_Name = 'REF=' + ref; }
	
			//	if this is a disti, make sure contactus and prices are off
			if ((gReferrer != "gwi") && (gReferrer != ""))	{
				gContactUsOn  = false;
				gShowPricesOn = false;
			}

			//	calculate cookie value
			var cookie_text = "";
			cookie_text += (gContactUsOn  ? 'c' : 'x');
			cookie_text += (gShowPricesOn ? 'p' : 'x');

			cookie_text += ('__1__' + gPrimaryInterest);
			cookie_text += ('__2__' + gReferrer_Displayed_Name);
			cookie_text += ('__3__' + gReferrer);
			cookie_text += ('__4__' + gDocReferrer);
			cookie_text += ('__5__' + gFirstUrlOfSession);

			//	convert '/' chars to '-' to make it easier for cookie processor
			{
				var cookie_text_cleaned = ReplaceText(cookie_text, '\/', '-');
				cookie_text = cookie_text_cleaned;
			}

			//	save the cookie; and tell it to expire when browser closes
			setCookie(sCookie_name, cookie_text, 0);

			gNumTimesCreatedCookie += 1;
			if (gDebuggingAlertsOn || gDebuggingCookies) { 
				valueOfSavedCookie = get_cookie( sCookie_name );
				alert('we now saved this cookie. valueOfSavedCookie =' + valueOfSavedCookie + ', document.cookie = ' + document.cookie + ',  gNumTimesCreatedCookie = ' + gNumTimesCreatedCookie); 
			}
	    }
		
		//	---------------------------------------------------------
		//	else we are on page # >=2 of our session and we look at the
		//	the cookie that was settup on page #1 of user's session, which
		//	contains info about how the user got here and what we should show

		else	{

			var lengthOf = gOurCookies.length;
			if (lengthOf > 0)	{

				gContactUsOn     = (gOurCookies.substring(0, 1) == 'c');
				gShowPricesOn    = (gOurCookies.substring(1, 2) == 'p');

				var one_offset   =  gOurCookies.indexOf('__1__', 0);
				var two_offset   =  gOurCookies.indexOf('__2__', one_offset);
				var three_offset =  gOurCookies.indexOf('__3__', two_offset);
				var four_offset  =  gOurCookies.indexOf('__4__', three_offset);
				var five_offset  =  gOurCookies.indexOf('__5__', four_offset);

				if (one_offset > 1)   {  gPrimaryInterest 			= gOurCookies.substring(one_offset + 5,   two_offset);   }
				if (two_offset > 1)	  {  gReferrer_Displayed_Name   = gOurCookies.substring(two_offset + 5,   three_offset); }
				if (three_offset > 1) {  gReferrer           	 	= gOurCookies.substring(three_offset + 5, four_offset);  }
				if (four_offset > 1)  {  gDocReferrer       		= gOurCookies.substring(four_offset + 5,  five_offset);  }
				if (five_offset > 1)  {  gFirstUrlOfSession      	= gOurCookies.substring(five_offset + 5,  lengthOf);     }
			}

			if (gDebuggingAlertsOn || gDebuggingCookies) { 
				valueOfSavedCookie = get_cookie(sCookie_name);
				alert('we just loaded cookie. valueOfSavedCookie =' + valueOfSavedCookie + ', document.cookie = ' + document.cookie + ',   gPrimaryInterest = ' + gPrimaryInterest + ',   gReferrer_Displayed_Name = ' + gReferrer_Displayed_Name + ',   gReferrer = ' + gReferrer + ',   gDocReferrer = ' + gDocReferrer + ',   gFirstUrlOfSession = ' + gFirstUrlOfSession); 
			}
		}
	}	

	//	--------------------------------------------------------
	//	if this is a disti, make sure contactus and prices are off

	if ((gReferrer != "gwi") && (gReferrer != ""))	{
    	gContactUsOn  = false;
		gShowPricesOn = false;
	}

	//	--------------------------------------------------------
	// alert('search = ' + document.location.search + ', hash = ' + document.location.hash);

	if (gDebuggingAlertsOn) { alert('gContactUsOn = ' + (gContactUsOn?"True":"False") + ', gShowPricesOn = ' + (gShowPricesOn?"True":"False")); }

	//	--------------------------------------------------------
	//	if contactus, prices or disti is on; then alc the param 9=XYZZZ and tack onto the end of
	//	the <a ... href= ...> links on this document
	if (gContactUsOn || gShowPricesOn || (gReferrer != "")) {
		var ref_3chars = "xxx";
		if (gReferrer.length >= 3) {  ref_3chars = gReferrer.substring(0, 3).toLowerCase();  }
		gP9_SessionSummary = '?9=' + (gContactUsOn ? 'c' : 'x') + (gShowPricesOn ? 'p' : 'x') + ref_3chars;
	}

	// alert('load, gP9_SessionSummary = ' + gP9_SessionSummary);

	return;
}

//	--------------------------------------------------------
//	inet_OnLoad()
//
//	Called by Onload event, which means BODY file has already been loaded 
//	(and printed via jscript document.write() code) and objects (e.g. list of
//	A links, items with ID's) exists at this time.
function inet_OnLoad()
{
	
	//	--------------------------------------------------------
	//	Go to ALL 'A' link objects (50 to 130 of them) and append
	//	"?9=XYZZZ" to them so that if someone copies a URL to
	//	they will get back the x/y/zz state (contactus/prices/disti)
	
	//	 This is buggy on local hd w/ # anchors and buggy on internet
	//	 This is buggy on local hd w/ # anchors and buggy on internet
	if (0)	{
	
		//	if changing all A link search fields is NOT disabled manually ...
		if ( ! gDonChangeSearchOnAll_A_Links )	{
			//	if we have '?9=XYZZZ' info...
			if (gP9_SessionSummary != "") {
				//	if this page has A links...
				if (document.links != null)	{
					//	Get list of all A link objects in this page.
					//	Since each menu item is an A link, this is 50 to 150 objects.
					var a_links = document.links;
					if (a_links != null) {
						// alert('coll.length = ' + coll.length);
						for (i=0; i < a_links.length; i++) 	{
							var href_target = a_links[i].href.toLowerCase();
							if (href_target != "")	{
	
								//	if this is a tag that connects to this web site
								if ((href_target.indexOf('javascript:') <= -1)	 &&
									(href_target.indexOf('http') <= -1)	 		 &&
									(href_target.indexOf('mailto') <= -1)	 	 &&	
									(href_target.indexOf('ftp:') <= -1)			 &&
									(href_target.substring(0, 1) != '#')		 &&
									(href_target != '#'))	{
		
									//	add '?9=XYZZZ' suffix if it does not already have a suffix
									if (a_links[i].search == "")	{
										a_links[i].search = gP9_SessionSummary;	
										//	this works in testing: ssoftware/iw/tutorial/index.html?9=xxome#eyw
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return
}

//	--------------------------------------------------------
// Get value of cookie
function get_cookie(NameOfCookie) {
  var arg = NameOfCookie + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  var null_string = "";
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null_string;
}

//	get the value of the cookie given the location within the cookie string
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

//	store the cookie
function setCookie(NameOfCookie, value, expire_mSecs) 
{
	var ExpireDate = new Date ();
	var expireDate_mSecs = (expire_mSecs == 0) ? 0 : (ExpireDate.getTime() + expire_mSecs);
	SetCookie_LongMethod(NameOfCookie, value, expireDate_mSecs, sCookie_path, sCookie_domain);
}

//	delete the cookie
function delCookie(NameOfCookie) {
	SetCookie_LongMethod(NameOfCookie, "", 1, sCookie_path, sCookie_domain);
}

//	save the cookie
function SetCookie_LongMethod(NameOfCookie, value, expires, path, domain )
{
  if ((expires!=null) && (expires!=0)) {  FixCookieDate(expires);  }

  var full_cookie_string = "";

  full_cookie_string = NameOfCookie + "=" + escape(value) +
    (  ((expires==null) || (expires==0)) ? "" : ("; expires=" + expires.toGMTString()) ) +
    (  ((path==null)    || (path==""))   ? "" : ("; path=" + path)		) +
    (  ((domain==null)  || (domain=="")) ? "" : ("; domain=" + domain)  );

  // alert('full_cookie_string = ' + full_cookie_string);

  document.cookie = full_cookie_string;
}

function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime();	 // dawn of (Unix) time - should be 0
  if (skew > 0) { 				// Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
 }
}

  /*
  **  Replace a token in a string
  **    s  string to be processed
  **    t  token to be found and removed
  **    u  token to be inserted
  **  returns new String
  */
function ReplaceText(s, t, u) 
{
  var i = s.indexOf(t);
  var r = "";
  if (i == -1) return s;
  r += s.substring(0,i) + u;
  if ( i + t.length < s.length) {
    r += ReplaceText(s.substring(i + t.length, s.length), t, u);
  }
  return r;
}

 

//	--------------------------------------------------------
// 	recovers parameters separated by commas, and prefixed with an ID number. e.g. (?1=x, 2=y)
function GetParameter_PassParamsToJavaScript(passParamsToJavaScript_string, index)
{
  passParamsToJavaScript_string += ', ';

  var searchString = String((String(index)+'='));

  var searchPrefixLength = Number(((Number(index) <= 9) ? 2 : 3));

  var start_index = Number(searchPrefixLength + Number(passParamsToJavaScript_string.indexOf(searchString)));	

  var stop_index  = Number(0                  + Number(passParamsToJavaScript_string.indexOf(",", start_index)));	

  if (start_index <= 1) { return ""; }

  if (stop_index < start_index)  { stop_index = passParamsToJavaScript_string.length; }

  parameterText = passParamsToJavaScript_string.substring(start_index, stop_index);

  return parameterText;
}

//	--------------------------------------------------------
// Runs the Website tracker if we are on the internet
function Track_iNet_Page_If_On_Internet(doc)
{
    // alert('doc.location.href = ' + doc.location.href);
	if (doc.location) { 
  	   if (doc.location.href) { 
			 var url_string = doc.location.href;
			 //	if we are on the internet ... 
			 if (url_string.toLowerCase().substring(0, 4) == "http") {
				t = "";
				t += '<script language=\"javascript\" src=\"http:\/\/www.dataplain.com\/stats-js.cgi?host=dp&user=gweinreb\"> <\/script> ';
				t += '<noscript> <img src=\"http:\/\/www.dataplain.com\/stats-image.cgi?host=dp&user=gweinreb\" height=\"1\" width=\"1\" border=\"0\"> <\/noscript>';
				if (gDebuggingAlertsOn) { alert(t); }
				//alert(t);
				doc.writeln(t);
			}
		}
	}
}

//	--------------------------------------------------------
//  reloads the window if Nav4 resized
function MM_reloadPage(init) {  
  	if (init==true) with (navigator) {
		if ((appName=="Netscape") && (parseInt(appVersion)==4)) {
    		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; 
		} 
	} else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) 
		location.reload();
}

//	--------------------------------------------------------
//  reloads the window if Nav4 resized
function print_price(priceText)
{
	if (gShowPricesOn) { document.write(priceText); }
}

//	--------------------------------------------------------
//  If suffix is #128 or #168, we redirect to another url
function redirect_to_another_url_if_suffix_tells_us_to_do_that()
{
   var searchNumber = 0;
	if (location.hash) {
	   if ((location.hash.length > 1) && (location.hash.length <= 5) && (location.search.length == 0)) {
            start = Number(location.hash.indexOf("#"));
            if (start == 0)  {
               searchNumber = Number(location.hash.substring(start + 1, location.hash.length));
            }
        }	
	}

	var internet_url = 'http:\/\/www.instrunet.com\/';
	var gTestingOnLocalHardDisk = false;
	if (gTestingOnLocalHardDisk) { internet_url = ""; }

	var redirect_url = "";

	if (searchNumber == 128) { redirect_url = internet_url + 'p\/index.html\?3=c,4=gwi,8=p'; }
	if (searchNumber == 168) { redirect_url = internet_url + 'download\/files\/inet_an168.pdf'; }

	//	if we are redirecting to another page (i.e. we are not to load this one)
	if (redirect_url != "")	{
		gWeAreRedirectingToPriceListPage = true;
		Redirect_to_another_url( redirect_url );
		// alert( 'redirect_url = ' + redirect_url);
	}

	return;
}

//	--------------------------------------------------------
//  determine the end user's primary interest

var interest_tc    = 'thermocouple';
var interest_therm = 'thermistor';
var interest_sg    = 'strain';
var interest_rtd   = 'rtd';
var interest_quadr = 'quadrature';
var interest_lv    = 'labview';
var interest_dasy  = 'dasylab';
var interest_loadc = 'load cell';

function determine_interest( desc_string )
{
	var interest = "";

	if (desc_string == null) { return interest; }

	if (desc_string == "") { return interest; }

	var desc_lc = desc_string.toLowerCase();

	var desc = ReplaceText(desc_lc, '-', ' ');

	if      (desc.indexOf(interest_tc) >= 0) 	{ interest = interest_tc; }
	else if (desc.indexOf(interest_therm) >= 0) { interest = interest_therm; }
	else if (desc.indexOf(interest_sg) >= 0) 	{ interest = interest_sg; }
	else if (desc.indexOf(interest_rtd) >= 0) 	{ interest = interest_rtd; }
	else if (desc.indexOf(interest_quadr) >= 0) { interest = interest_quadr; }
	else if (desc.indexOf(interest_lv) >= 0) 	{ interest = interest_lv; }
	else if (desc.indexOf(interest_dasy) >= 0) 	{ interest = interest_dasy; }
	else if ((desc.indexOf(interest_loadc) >= 0) || (desc.indexOf('loadcell') >= 0))  { interest = interest_loadc; }

	return interest;
}
