function lookup(txt){
	if(txt.charAt(0)=="&" && txt.indexOf("&amp;")==-1)
		txt="&amp;"+txt.substr(1);
	txt=txt.replace(/&amp;lpar;/gi,"&#x00028;");
	txt=txt.replace(/&amp;rpar;/gi,"&#x00029;");
	txt=txt.replace(/&amp;lbrack;/gi,"&#x0005B;");
	txt=txt.replace(/&amp;rbrack;/gi,"&#x0005D;");
	txt=txt.replace(/&amp;pm;/gi,"&#x000B1;");
	txt=txt.replace(/&amp;equals;/gi,"&#x0003D;");
	txt=txt.replace(/&amp;plus;/gi,"&#x0002B;");
	txt=txt.replace(/&amp;minus;/gi,"&#x02212;");
	txt=txt.replace(/&amp;compfn;/gi,"&#x02218;");
	txt=txt.replace(/&amp;mp;/gi,"&#x02213;");
	txt=txt.replace(/&amp;comma;/gi,",");
	txt=txt.replace(/&amp;verbar;/gi,"|");

	return txt;
}
	
function convertMath(node) {// for Gecko
	if (node.nodeType==1) {
		var newnode = 
			document.createElementNS("http://www.w3.org/1998/Math/MathML",
				node.nodeName.toLowerCase());
		for(var i=0; i < node.attributes.length; i++)
			newnode.setAttribute(node.attributes[i].nodeName,
				node.attributes[i].nodeValue);
		for (var i=0; i<node.childNodes.length; i++) {
			var st = node.childNodes[i].nodeValue;
			if (st==null || st.slice(0,1)!=" " && st.slice(0,1)!="\n") 
				newnode.appendChild(convertMath(node.childNodes[i]));
		}
		return newnode;
	}
	else
		return node;
}

function convert() {
	var mnodes = document.getElementsByTagName("math");
	var st,str,node,newnode;
	for (var i=0; i<mnodes.length; i++)
		if (document.createElementNS!=null){
			mnodes[i].innerHTML=lookup(mnodes[i].innerHTML);
			mnodes[i].parentNode.replaceChild(convertMath(mnodes[i]),mnodes[i]);
		}else { // convert for IE
			str = "";
			node = mnodes[i];
			while (node.nodeName!="/MATH") {
				st = node.nodeName.toLowerCase();
				if (st=="#text")
					str += lookup(node.nodeValue);
				else {
					if (st.slice(0,1)=="/"){
						str += "</m:"+st.slice(1);
					}else{
						str += "<m:"+st;					
						for(var j=0; j < node.attributes.length; j++){
							var attValue=node.attributes[j].nodeValue;
							if(	attValue!="italic" && attValue!="" &&
									attValue!="inherit" && attValue!=undefined)
								str += " "+node.attributes[j].nodeName+"="+
									"\""+attValue+"\"";
						}
					}
					str += ">";
				}
				node = node.nextSibling;
				if(node==null)break;
				node.parentNode.removeChild(node.previousSibling);
			}
			str += "</m:math>";
			newnode = document.createElement("span");
			if(node!=null)node.parentNode.replaceChild(newnode,node);
			newnode.innerHTML = str;
		}
}
