var fDebug = false;					/* Debug string */


function DebugMsg(s, f)
{
    if (fDebug || (f != null && f)) {			/* If debug messages wanted */
	fDebug = confirm(s.toString());			/* Display the message */
    }
}


function ParseString(sCookie, sName, vDefault)
{
    var i;						/* String index */
    var j;						/* String index */
    var re;						/* Regular expression */

    sName = new String("&".concat(sName, "="));		/* Form search string */
    if (sCookie.slice(0, 1) == "?") {			/* If starts with "?" */
	sCookie = "&".concat(sCookie.slice(1));		/* Replace "?" with "&" */
    }
    i = sCookie.indexOf(sName);				/* Look for game count */
    if (i >= 0) {					/* If we have a count */
	i = i + sName.length;				/* Skip the name */
	j = sCookie.indexOf(";", i);			/* Find end of count */
	if (j < 0) {					/* If no ending delimiter */
	    j = sCookie.indexOf("&", i);		/* Look for alternate delimiter */
	    if (j < 0) {				/* If no ending delimiter */
		j = sCookie.length;			/* Use rest of string */
	    }
	}
	re = new RegExp("%20", "g");			/* Build expression */
	return sCookie.substr(i, j - i).replace(re, " ");
							/* Get the value */
    }
    return vDefault;					/* Return default value */
}


function GetQueryString(sName, vDefault)
{
    return ParseString(window.location.search, sName, vDefault);
							/* Get string from query */
}


function GetUserAgent()
{
    return LookupAgent(window.navigator.userAgent);	/* Return the browser version */
}


function LookupAgent(sAgent)
{
    /*
     *  We check "Opera" first because it impersonates IE (without being
     *  fully compatible, of course).
     */
    if (sAgent.indexOf("Opera") >= 0) {			/* If Opera */
	sAgent = "Opera";				/* Say it's Opera */
    }
    else if (sAgent.indexOf("Firefox") >= 0) {		/* If Firefox */
	sAgent = "Firefox";				/* Say it's Firefox */
    }
    else if (sAgent.indexOf("Yahoo!") >= 0 || sAgent.indexOf("WebIndexer") >= 0) {
							/* If Yahoo! */
	sAgent = "*Yahoo!";				/* Say it's Yahoo!'s indexer */
    }
    else if (sAgent.indexOf("aipbot/") >= 0) {		/* If aipbot */
	sAgent = "*aipbot";				/* Say it's aipbot (whatever that is) */
    }
    else if (sAgent.indexOf("appie ") >= 0) {		/* If appie */
	sAgent = "*Walhello";				/* Say it's Walhello's robot */
    }
    else if (sAgent.indexOf("curl/") >= 0) {		/* If Linux curl */
	sAgent = "curl";				/* Say it's curl */
    }
    else if (sAgent.indexOf("Googlebot/") >= 0) {	/* If Googlebot */
	sAgent = "*Google";				/* Say it's Google's indexer */
    }
    else if (sAgent.indexOf("msnbot/") >= 0) {		/* If msnbot */
	sAgent = "*MSN";				/* Say it's MSN's indexer */
    }
    else if (sAgent.indexOf("Jeeves") >= 0) {		/* If Jeeves */
	sAgent = "*Ask.com";				/* Say it's Ask.com's indexer */
    }
    else if (sAgent.indexOf("Safari/") >= 0) {		/* If Safari, a Mac browser */
	sAgent = "Safari";				/* Say it's Safari */
    }
    else if (sAgent.indexOf("Quest") >= 0) {		/* If Quest, some kind of crawler??? */
	sAgent = "*Quest";				/* Say it's Quest */
    }
    else if (sAgent.indexOf("WISEnutbot") >= 0) {	/* If WiseNut's indexer */
	sAgent = "*WiseNut";				/* Say it's WiseNut */
    }
    else if (sAgent.indexOf("TurnitinBot/") >= 0) {	/* If Turnitin's indexer */
	sAgent = "*TurnItIn";				/* Say it's TurnItIn */
    }
    else if (sAgent.indexOf("psbot/") >= 0 || sAgent.indexOf("picsearch.com") >= 0) {
							/* If Picsearch's indexer */
	sAgent = "*PicSearch";				/* Say it's PicSearch */
    }
    else if (sAgent.indexOf("W3CRobot/") >= 0) {	/* If W3C's indexer */
	sAgent = "*W3CRobot";				/* Say it's W3CRobot */
    }
    else if (sAgent.indexOf("ia_archiver") >= 0) {	/* If Alexa's indexer */
	sAgent = "*Alexa";				/* Say it's Alexa */
    }
    else if (sAgent.indexOf("LinkWalker") >= 0) {	/* If LinkWalker */
	sAgent = "*LinkWalker";				/* Say it's LinkWalker */
    }
    else if (sAgent.indexOf("Gigabot") >= 0) {		/* If Gigabot */
	sAgent = "*Gigablast";				/* Say it's Gigablast's indexer */
    }
    else if (sAgent.indexOf("Gaisbot") >= 0) {		/* If Gais' crawler */
	sAgent = "*Gais";				/* Say it's Gais's indexer */
    }
    else if (sAgent.indexOf("sherlock/") >= 0) {	/* If "sherlock" */
	sAgent = "*sherlock";				/* Say it's sherlock */
    }
    else if (sAgent.indexOf("Konqueror/") >= 0) {	/* If Konqueror */
	sAgent = "Konqueror";				/* Say it's Konqueror, a Linux browser */
    }
    else if (sAgent.indexOf("NextGenSearchBot") >= 0) { /* If NextGenSearchBot */
	sAgent = "*ZoomInfo";				/* Say it's ZoomInfo's indexer */
    }
    else if (sAgent.indexOf("AtlocalBot/") >= 0) {	/* If AtlocalBot */
	sAgent = "*@local";				/* Say it's @local's indexer */
    }
    else if (sAgent.indexOf("ConveraCrawler/") >= 0) {	/* If Convera's crawler */
	sAgent = "*convera";				/* Say it's convera */
    }
    else if (sAgent.match(/almaden.*crawler/) != null) {/* IF IBM Almaden crawler */
	sAgent = "*IBM";				/* Say it's IBM */
    }
    else if (sAgent.indexOf(" MSIE ") >= 0) {		/* Else if Internet Explorer */
	if (sAgent.indexOf(" Mac") >= 0) {		/* If Macintosh platform */
	    sAgent = "IEMac";				/* IE on the Mac */
	}
	else sAgent = "IE";				/* Else say it's IE */
    }
    else if (sAgent.indexOf(" Gecko/") >= 0) {		/* Else if Mozilla */
	if (sAgent.indexOf(" Netscape/") >= 0) {	/* If Netscape */
	    sAgent = "Netscape";			/* Say it's Netscape's browser */
	}
	else sAgent = "Mozilla";			/* Else say it's from Mozilla.org */
    }
    else if (sAgent.toLowerCase().indexOf("bot/") >= 0) {
							/* Else if probably a robot */
	sAgent = "*unknown";				/* Say it's an unknown robot */
    }
    else sAgent = "unknown";				/* Else we don't know */
    return sAgent;					/* Return agent string */
}


function ParseNumber(sCookie, sName, vDefault)
{
    return Number(ParseString(sCookie, sName, vDefault));
    							/* Get the number */
}


function UpdateCookie(sName, sValue)
{
    sName = new String("&".concat(sName, "=", sValue, "; expires=Thu, 31 Dec 2099 23:59:59 GMT;"));
    							/* Form cookie string */
    window.document.cookie = sName;			/* Set item and expiration date */
}


function StringToDate(s)
{
    var d = null;					/* Date */

    if (arguments.length >= 2) {			/* If reference date given */
	d = arguments[1];				/* Use it */
    }
    d = new MyDate(s, d);				/* Parse date */
    if (d.Year != -1) {					/* If valid date */
	return new Date(d.Year, d.Month, d.Day);	/* Return date object */
    }
    return null;					/* Not a valid date string */
}


function Today()
{
    var d;						/* Date */
    var s;						/* String */

    if ((s = GetQueryString("today", null)) == null || (d = StringToDate(s, new Date())) == null) {
							/* If no well-formed date given */
	d = new Date();					/* Use real date */
    }
    return d;						/* Return "today" */
}


function MyDate(sDate)
{
    var d;						/* Reference date */
    var i;						/* Integer */
    var re = /^(\d+)\D(\d+)\D(\d+)$/;			/* Match date string */
    var re2 = /^(\d+)\D(\d+)$/;				/* Match month/day string */

    if (arguments.length >= 2 && arguments[1] != null) {/* If we have an explicit reference date */
	d = arguments[1];				/* Use it */
    }
    else d = Today();					/* Else get time now */
    if (sDate.match(re) != null) {			/* If match found */
	this.Month = parseInt(RegExp.$1, 10);		/* Assume month first */
	this.Day = parseInt(RegExp.$2, 10);		/* Assume day second */
	this.Year = parseInt(RegExp.$3, 10);		/* Assume year third */
	if (this.Month > 31 && this.Day <= 12 && this.Year <= 31) {
							/* If [YY]YY/MM/DD */
	    i = this.Month;				/* Get the year */
	    this.Month = this.Day;			/* Set the month */
	    this.Day = this.Year;			/* Set the day */
	    this.Year = i;				/* Set the year */
	}
	else if (this.Month > 12 && this.Day <= 12) {	/* Else if DD/MM/[YY]YY */
	    i = this.Month;				/* Get the day */
	    this.Month = this.Day;			/* Set the month */
	    this.Day = i;				/* Set the day */
	}
	if ((i = this.Year) < 100) {			/* If two-digit year */
	    i += Math.floor(d.getFullYear()/100)*100;	/* Add current century */
	    if (i > d.getFullYear() + 60) {		/* If more than 60 years from now */
		i -= 100;				/* Assume last century */
	    }
	    this.Year = i;				/* Update year */
	}
	--this.Month;					/* Make 0 = January, etc. */
    }
    else if (sDate.match(re2) != null) {		/* Else if match found */
	this.Year = d.getFullYear();			/* Use this year */
	this.Month = parseInt(RegExp.$1, 10);		/* Assume month first */
	this.Day = parseInt(RegExp.$2, 10);		/* Assume day second */
	if (this.Month > 12 && this.Day <= 12) {	/* If DD/MM */
	    i = this.Month;				/* Get the day */
	    this.Month = this.Day;			/* Set the month */
	    this.Day = i;				/* Set the day */
	}
	--this.Month;					/* Make 0 = January, etc. */
    }
    else {						/* Else bad date string */
	this.Year = -1;					/* Invalid year */
	this.Month = -1;				/* Invalid month */
	this.Day = -1;					/* Invalid day */
    }
}


function MapToDate(sPage)
{
    var d = Today();					/* Get the current date */
    var ms;						/* Milliseconds */

    ms = d.getTime() - d.getDay()*86400000;		/* Back up to Sunday */
    do {						/* Dummy loop */
	if (sPage == "current") {			/* Current week */
	    break;					/* No further adjustment needed */
	}
	if (sPage == "next") {				/* Next week */
	    ms += 7*86400000;				/* Next Sunday */
	    break;					/* Exit loop */
	}
	if (sPage == "previous") {			/* Previous week */
	    ms -= 7*86400000;				/* Last Sunday */
	    break;					/* Exit loop */
	}
	return sPage;					/* Don't know how to map it */
    }
    while (false);					/* End dummy loop */
    d.setTime(ms);					/* Set the time */
    return d.getFullYear().toString().concat("0".concat((d.getMonth() + 1).toString()).slice(-2), "0".concat(d.getDate().toString()).slice(-2));
						  	/* Return "yyyymmdd" */
}


function MakeURL(s)
{
    var i;						/* Index */
    var sFile = "";					/* File to request */
    var sBookmark = "";					/* Bookmark */
    var sOpts = "";					/* Options */

    if ((i = s.indexOf("#")) >= 0) {			/* If bookmark specified */
	sBookmark = s.slice(i);				/* Grab it */
	s = s.slice(0, i);				/* Remove it from main string */
    }
    sFile = window.location.href;			/* Get current invocation */
    if (sFile.match(/[&?](log=\w+)/) != null) {		/* If "log" given */
	sOpts = RegExp.$1;				/* Get its value */
    }
    if (sFile.match(/[&?](debug=\w+)/) != null) {		/* If "debug" given */
	if (sOpts.length > 0) {				/* If not empty */
	    sOpts = sOpts.concat("&");			/* Add ampersand */
	}
	sOpts = sOpts.concat(RegExp.$1);		/* Get its value */
    }
    if (s.length > 0 && sOpts.length > 0) {		/* If something to append */
	sOpts = "&".concat(sOpts);			/* Prepend with ampersand */
    }
    s = s.concat(sOpts);				/* Add options if any */
    if ((i = sFile.indexOf("?")) >= 0) {			/* If question mark given */
	sFile = sFile.slice(0, i);			/* Trim everything after it */
    }
    if (s.length > 0) {					/* If we have arguments */
    	s = "?".concat(s);				/* Prepend question mark */
    }
    return sFile.concat(s, sBookmark);			/* Return reference */
}


function GetObjectReference(sOb)
{
    var colob;						/* Object collection */
    var ob;						/* Object */

    if ((ob = window.document.getElementById(sOb)) != null) {
							/* If object found */
	return ob;					/* Return reference to object */
    }
    colob = window.document.getElementsByName(sOb);	/* Get all elements named sOb */
    if (colob.length > 0) {				/* If there are any */
	return colob.item(0);				/* Take the first one */
    }
    if (arguments.length < 2 || !arguments[1]) {	/* If not suppressing error message */
	alert("Couldn't find \"".concat(sOb, "\""));	/* Error message */
    }
    return null;					/* No such object */
}


function IsBlank(s)
{
    return s.match(/^\s*$/) != null;			/* Return true if blank string */
}


function Trim(s)
{
    s = s.replace(/&nbsp;/g, " ");			/* Replace non-breaking spaces with spaces */
    s = s.replace(/\s+/g, " ");				/* Replace multiple spaces with single space */
    s = s.replace(/^\s+(.*)/, "$1");			/* Trim leading blanks */
    return s.replace(/(.*)\s+$/, "$1");			/* Trim trailing blanks */
}


function nbsp(s)
{
    if (IsBlank(s)) {					/* If blank string */
	s = "&nbsp;";					/* Use non-breaking space */
    }
    return s;						/* Return the string */
}


function IsCrawler()
{
    var sAgent;						/* User agent */

    if (arguments.length > 0) {				/* If we have an argument */
	sAgent = arguments[0];				/* Use it */
    }
    else sAgent = GetUserAgent();			/* Else get current user agent */
    return sAgent.slice(0, 1) == "*";			/* Not a true browser */
}


function WindowHeight()
{
    if (window.innerHeight != null) {			/* If Gecko/Mozilla */
	return window.innerHeight;
    }
    if (window.document.documentElement != null) {	/* If IE */
	return window.document.documentElement.offsetHeight;
    }
    return window.document.body.offsetHeight;
}


function WindowWidth()
{
    if (window.innerWidth != null) {
	return window.innerWidth;
    }
    if (window.document.documentElement != null) {
	return window.document.documentElement.offsetWidth;
    }
    return window.document.body.offsetWidth;
}


function GetTop(ob)
{
    var t = 0;						/* Top */

    while (ob != null) {				/* While we have an object */
	t += ob.offsetTop;				/* Add top relative to parent */
	ob = ob.offsetParent;				/* Check parent next */
    }
    return t;
}


function __FixAnchors()
{
    var col;
    var i;
    var j;
    var s;
    var sFriendly;
    var s2;

    col = window.document.getElementsByTagName("a");
    for (i = 0; i < col.length; ++i) {
	if (col.item(i).href.length == 0 && col.item(i).name.length == 0) {
	    s = col.item(i).innerHTML;
	    if (s.indexOf("@") > 0) {
		s2 = ""
		for (j = s.length - 1; j >= 0; j -= 1) {
		    s2 = s2.concat(s.slice(j, j + 1));
		}
		if ((sFriendly = col.item(i).title) == null || sFriendly.length == 0) {
		    sFriendly = s2;
		}
		col.item(i).innerHTML = sFriendly;
		col.item(i).href = "mailto:".concat(s2);
	    }
	}
    }
}


function MyOpen(sDoc)
{
    var sExt;

    sExt = sDoc;
    if ((i = sExt.indexOf("?")) >= 0) {
	sExt = sExt.slice(0, i);
    }
    sExt = sExt.slice(-4).toLowerCase();
    if (sExt != ".asp" && sExt != ".htm" && sExt != ".pdf") {
	window.location.href = "default.asp?source=cofclc0&page=DocLink&doc=".concat(sDoc);
    }
    else window.open(sDoc);
}
