/* this is generic finding function taken from
www.howtocreate.co.uk/tutorials/index.php?tut=0&part=15 */
function MWJ_findObj(oName, oFrame, oDoc) {
    if(!oDoc) {
	if(oFrame) {
	    oDoc = oFrame.document;
	} else {
	    oDoc = window.document;
	}
    }

    if(oDoc[oName]) {
	return oDoc[oName];
    }
    if(oDoc.all && oDoc.all[oName]) {
	return oDoc.all[oName];
    }
    if(oDoc.getElementById && oDoc.getElementById(oName)) {
	return oDoc.getElementById(oName);
    }
    
    for(var x = 0; x < oDoc.forms.length; x++) {
	if(oDoc.forms[x][oName]) {
	    return oDoc.forms[x][oName];
	}
    }
    for(var x = 0; x < oDoc.anchors.length; x++) {
	if(oDoc.anchors[x].name == oName) {
	    return oDoc.anchors[x];
	}
    }
    for(var x = 0; document.layers && x < oDoc.layers.length; x++) {
	var theOb = MWJ_findObj(oName, null, oDoc.layers[x].document);
	if( theOb ) {
	    return theOb;
	}
    }
    if(!oFrame && window[oName]) {
	return window[oName];
    }
    if(oFrame && oFrame[oName]) {
	return oFrame[oName];
    }
    for(var x = 0; oFrame && oFrame.frames && x < oFrame.frames.length; x++) {
	var theOb = MWJ_findObj(oName, oFrame.frames[x],
				oFrame.frames[x].document);
	if(theOb) {
	    return theOb;
	}
    }
    return null;
}

function replacetitle() {
    var titleobj;
    var tempobj;
    var tempobj2;
    titleobj = MWJ_findObj('title');
    titleobj = titleobj.parentNode; //the div
    titleobj.innerHTML = '<img src="images/chinq.gif" alt="Chinquapin"></img>'
}

function fixspace(mystring) {
    //  is the value of &ensp;
    //  is the value of &emsp;
    return(mystring.replace(/( )|(&ensp;)|(&emsp;)|( )/g, " "));
}

function walkandreplace(myobj) {
    if(myobj.nodeValue) {
	myobj.nodeValue = fixspace(myobj.nodeValue);
    }
    if(myobj.firstChild) {
	walkandreplace(myobj.firstChild);
    }
    if(myobj.nextSibling) {
	walkandreplace(myobj.nextSibling);
    }
}

function replacespaces () {
    if(document.body) {
	walkandreplace(document.body);
    } else if(document.documentElement) {
	walkandreplace(document.documentElement);
    } else if(document.childNodes) {
	walkandreplace(document.childNodes[0]);
    } else {
	//that sucks.  Oh well.
    }
}

function docompat () {
    if((navigator.appName == 'Netscape'
	&& (navigator.userAgent.indexOf('Macintosh') != -1
	    || parseFloat(navigator.appVersion) < 6))
       || navigator.userAgent.indexOf('Opera') != -1) {
	//replace ensp and emsp with a simple space.
	replacespaces();
    }
    if(navigator.appName == 'Microsoft Internet Explorer'
       && navigator.userAgent.indexOf('Mac') == -1) {
	//take out the object and use the underlying GIF
	replacetitle();
    }
    if(navigator.appName == 'Netscape'
       && navigator.userAgent.indexOf('Opera') == -1
       && parseFloat(navigator.appVersion) < 6
       && navigator.product != 'Gecko') {
	//this is too much for a poor javascript to handle
	var temp;
	temp = document.URL;
	if(temp.indexOf('compat.cgi') == -1) {
//	    temp = temp.replace(/http:\/\/[^\/]*\/~evan\/chinquapin\/*/, "");
//	    window.location = ("/~evan/chinquapin/bin/compat.cgi/" + temp);
	    temp = temp.replace(/http:\/\/[^\/]*\/*/, "");
	    window.location = ("/bin/compat.cgi/" + temp);
	}
    }
}

if(document.captureEvents && Event.ONLOAD) {
    document.captureEvents(Event.ONLOAD);
}
window.onload=docompat;
