// script to replace <q> in a site's html
// by: eightize
// modified: aug 22 2006
// may be either in head or at the end of document body

// convert quote tags to quotation marks in appName	Microsoft Internet Explorer
var appType = "Microsoft Internet Explorer";
var appVers = 4.0;
if (navigator.appName == appType) {
	var splitChar = / /;
	var appNum = parseFloat(navigator.appVersion.split(splitChar)[0]);
	if (appNum <= appVers) { 
		// check if this is in the document head or at the end of the body
		if (document.body) {
			if (!window.firstRun) {
				window.firstRun = true; // only want to run this once!
				ieQuotes();
			}
		} else { // in head because body does not exist
			addLoadEvent(ieQuotes); 
		}
	}
}

// replace marks
function ieQuotes() {
	var qTags = Array("<q>","</q>");
	var qVals = Array("&#8220;","&#8221;");
	var docContents = document.body.innerHTML;
	for (var j=0; j<qTags.length; j++) {
		docContents = docContents.replace(REmake(qTags[j]),qVals[j])
	}
	// replace all html in page
	document.body.innerHTML=docContents;
}

// returns regular expression of string
function REmake(testStr,switchStr) {
	var badChars = Array('\\','^','$','*','+','?','.','(',')','{','}','|','[',']','/');
	var tempStr = testStr;
	for (var i=0; i<badChars.length; i++) {
		tempStr = tempStr.replace(badChars[i],'\\'+badChars[i]);
	}
	return new RegExp(tempStr,(switchStr)?switchStr:'gi');
}

// from http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
  var oldonload = window.onload;

  if (typeof(window.onload)!= 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
