	var iCount = 0;  // This is for debugging only.

	window.onload=function()
	{
		getAndSetEquationsConvertedCountAjax();
	}


	// This function tells us whether the user has already embedded his/her
	// LaTeX (which is converted to MathML) within the template we make available.
	//
	// This returns 1 (true) or 0 (false).
	function isMathMLTemplateAlreadyPresentInThisString(sDoc)
	{
		var iIndex1 = sDoc.indexOf("?xml version=");
		var iIndex2 = sDoc.indexOf("!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN\"");
		//alert("iIndex1: " + iIndex1 + ", iIndex2: " + iIndex2);
		//stringObject.toLowerCase()

		// Note: The leading less-than sign (at index 0) has been removed from index1 search,
		// which is why we demand that the index1 is equal to 1 not 0.
		if( (iIndex1 != -1) && (iIndex1 == 1) &&
		    (iIndex2 != -1) )
		{	return 1;
		}
		else
		{	return 0;
		}
	}

	// This can be used to set MathML or to clear the iframe by setting sMathML=="".
	// * NOTE: The MathML characters MUST BE IN UNICODE (rather than MathML
	// characters like &alpha;) for this to work.
	function setTheMathMLInIFrameTo(sMathML)
	{
		// Set the sMathML in the the iframe 'iframe_display_mathml'.

		////sMathML = sMathML.replace("&alpha;", "j");  // This eliminates the problem?


		// Has the user placed his/her LaTeX in the xml template.
		var ib = isMathMLTemplateAlreadyPresentInThisString(sMathML); // Returns 0 (false), or 1 (true).
		//alert('ib: ' + ib);

		var objDiv = window.frames['iframe_display_mathml'].document.getElementById('src_mathml_div');
		//alert('objDiv: ' + objDiv);
		
		if(objDiv)
		{
			// This clears the <div id='src_mathml_div'>...</div> tags in the iframe xml doc.
			objDiv.innerHTML = '';  // Probably better ways to do this using XML DOM, but this is easiest for now.

			if(ib == 1) // Yes, the template is already in use.
			{
				//--
				// Next, we extract all the xml (including all MathML) between
				// the <body>..</body> tags of the user's document. We can then insert this
				// into the iframe below (between the <div id="src_mathml_div"></div>, as
				// we do for code not in the template.

				var parser = new DOMParser();
				var xmlDoc=parser.parseFromString(sMathML, "text/xml");
				//alert('xmlDoc: ' + xmlDoc);

				// See http://www.devarticles.com/c/a/JavaScript/Manipulating-XML-Data-with-JavaScript/
				// which explains documentElement (which is the 'body' of the document).
				objDiv.appendChild(xmlDoc.documentElement);
				//--

			}
			else  //if(ib == 0) // No, the template is NOT already in use.
			{
				// Note, in this case, 'src_mathml_div' is between the body tags of
				// the src document (called iframe_src_with_xhtml_template.xml) for the
				// iframe (with name 'iframe_display_mathml').
				//
				// To reference an element with id="src_mathml_div" in a frame with
				// name="iframe_display_mathml", one would use the following syntax:

				//alert(sMathML);

				// PROBLEM CALL IS HERE. MathML characters like &alpha; do not work
				// when inserted via innerHTML.  However, if we change MathML chars
				// (e.g. &alpha;) to Unicode char (e.g. &#x003B2;), it works.

				objDiv.innerHTML = sMathML;
			}
		}

		return;
	}




	//<div id="iframe_display_mathml_div" style="float: left; padding-left: 4em; padding-top: 1em;">
	//	<iframe id="iframe_display_mathml" name="iframe_display_mathml" src="http://www.pwhunter.com/test/mwapplet/iframe_src_with_xhtml_template.xml" style="border: 1px solid #e0e0e0; width: 250px; height: 140px;" scrolling="auto">Your browser is incapable of viewing iframe elements.</iframe>
	//</div>
	//
	// This is called from the applet (see MathToWeb.java).
	// It renders (displays as MathML) generated from the user's LaTeX in the box
	// (the iframe) to the right (at the top) of the MathToWeb applet.
	// It then calls the Ajax function that increments the Equations Converte counter.
	//
	// * NOTE: The MathML characters in sMathML (the argument) MUST BE IN UNICODE
	// (rather than MathML characters like &alpha;) for this to work. The applet
	// takes care of this by always creating a Unicode version of the converted
	// MathML and passing it to this function.
	//
	// See chArrayML_Unicode[] and
	// replaceMathMLCharactersWithUnicodeOnesAndPlaceResultIn_chArrayML_Unicode()
	// in MathToWeb.java.
	//
	function renderMathMLAndIncrementEquationCounter(sNumberOfEquationsConverted, sMathML)
	{
		//alert('sNumberOfEquationsConverted: ' + sNumberOfEquationsConverted + ", sMathML: " + sMathML);

		setTheMathMLInIFrameTo(sMathML);  // PROBLEM HERE with &alpha; MathML characters. I solved it by having the applet send Unicode to this function always.
		//alert('hit2');

		// This increments the total number of equations
		// converted - at the server database.
		incrementEquationsConvertedCountAtServerAjax(sNumberOfEquationsConverted);
	}

	// This is called from the applet (see SelectFileToConvertJPanel.java) when the
	// 'Clear' button is pressed.
	// We need to clear the iframe that contains the rendered MathML.
	function clearMathML()
	{
		//alert('Hit clearMathML()');

		setTheMathMLInIFrameTo("");

		return;
	}



	//--
	// This Ajax takes a number from the applet (sNumberOfEquationsConverted, representing
	// the number of equations converted from
	// LaTeX to MathML of the most recent 'Convert') and posts it to the server where
	// the total number of conversions is incemented and then sent back to the hander below
	// which incements the total number on the webpage the user is viewing and working with.

	var xmlHttpObjectIncrementCount = null;

	function incrementEquationsConvertedCountAtServerAjax(sNumberOfEquationsConverted)
	{
		//alert("hit incrementEquationsConvertedCountAtServerAjax(), sNumberOfEquationsConverted: " + sNumberOfEquationsConverted);

		var sSendData = "dw=itnec"+"&n="+sNumberOfEquationsConverted;  // 'dw' == 'do_what', 'itnec' == 'increment total number (of) equations converted'

		xmlHttpObjectIncrementCount = getXmlHttpObject();
		//alert("xmlHttpObjectIncrementCount: " + xmlHttpObjectIncrementCount + ", sSendData: " + sSendData); // keep

		if(xmlHttpObjectIncrementCount)
		{
			xmlHttpObjectIncrementCount.onreadystatechange = handlerFor_incrementEquationsConvertedCountAtServerAjax;
			xmlHttpObjectIncrementCount.open("POST", "http://www.mathtoweb.com/cgi-bin/mathtoweb_equations_converted.pl", true);
			xmlHttpObjectIncrementCount.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttpObjectIncrementCount.send(sSendData);
			//alert("(itnex) sSendData: " + sSendData);
		}

		return;

	}


	function handlerFor_incrementEquationsConvertedCountAtServerAjax()
	{
		//alert('hit handlerFor_incrementEquationsConvertedCountAtServerAjax');

		//alert('hit, xmlHttpObjectIncrementCount.readyState' + xmlHttpObjectIncrementCount.readyState);
		//alert('hit, xmlHttpObjectIncrementCount.status' + xmlHttpObjectIncrementCount.status);
		//alert('hit, xmlHttpObjectIncrementCount.responseText' + xmlHttpObjectIncrementCount.responseText);


		if( (xmlHttpObjectIncrementCount) && (xmlHttpObjectIncrementCount.readyState == 4) )
		{
			if(xmlHttpObjectIncrementCount.status == 200)  // 200 = OK
			{
				if(xmlHttpObjectIncrementCount.responseText)   //xmlHttpObjectIncrementCount.readyState==4
				{
					//alert('hit, xmlHttpObjectIncrementCount.responseText' + xmlHttpObjectIncrementCount.responseText);
					var sResponseText = xmlHttpObjectIncrementCount.responseText;
					var sArrayParts = sResponseText.split("|");

					var sStringReturn = sArrayParts[0];  	//'itnec'
					var sNewTotalNumberEquationsConverted = sArrayParts[1];  	// A positive int as String representing the total number of equations converted by MathToWeb [Online].

					//alert("sStringReturn: " + sStringReturn + ", sNewTotalNumberEquationsConverted: " + sNewTotalNumberEquationsConverted);
					
					if(sStringReturn == 'itnec')  // 'itnec' == 'increment total number (of) equations converted'.
					{
						if(sNewTotalNumberEquationsConverted != '')
						{
							setEquationsConvertedCountToThisStringInteger(sNewTotalNumberEquationsConverted); // This sets the html in the webpage.
						}

					}
				}
			}
			else
			{	//alert("Problem retrieving XML data"); // We will do nothing and just keep the present number in place.
			}
		}

		return;
	}
	//--

	//--
	// This Ajax is called every time the present page loads (see the body onload call).
	// It queries the server to get the latest total number of equations converted and
	// sets this number in the page html.

	var xmlHttpObjectGetAndSetEquationsConvertedCount = null;

	function getAndSetEquationsConvertedCountAjax()
	{
		//alert("hit getAndSetEquationsConvertedCountAjax()");

		var sSendData = "dw=gtnec"+"&n=0";  // The second part '&n=' is just a dummy in this instance. // 'dw' == 'do_what', 'gtnec' == 'get total number (of) equations converted'

		xmlHttpObjectGetAndSetEquationsConvertedCount = getXmlHttpObject();
		//alert("xmlHttpObjectGetAndSetEquationsConvertedCount: " + xmlHttpObjectGetAndSetEquationsConvertedCount + ", sSendData: " + sSendData); // keep

		if(xmlHttpObjectGetAndSetEquationsConvertedCount)
		{
			xmlHttpObjectGetAndSetEquationsConvertedCount.onreadystatechange = handlerFor_getAndSetEquationsConvertedCountAjax;
			xmlHttpObjectGetAndSetEquationsConvertedCount.open("POST", "http://www.mathtoweb.com/cgi-bin/mathtoweb_equations_converted.pl", true);
			xmlHttpObjectGetAndSetEquationsConvertedCount.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttpObjectGetAndSetEquationsConvertedCount.send(sSendData);
			//alert("sSendData2: " + sSendData);
		}

		return;

	}

	function handlerFor_getAndSetEquationsConvertedCountAjax()
	{
		//alert('hit handlerFor_getAndSetEquationsConvertedCountAjax()');

		//alert('hit, xmlHttpObjectGetAndSetEquationsConvertedCount.readyState' + xmlHttpObjectGetAndSetEquationsConvertedCount.readyState);
		//alert('hit, xmlHttpObjectGetAndSetEquationsConvertedCount.status' + xmlHttpObjectGetAndSetEquationsConvertedCount.status);
		//alert('hit, xmlHttpObjectGetAndSetEquationsConvertedCount.responseText' + xmlHttpObjectGetAndSetEquationsConvertedCount.responseText);

		if( (xmlHttpObjectGetAndSetEquationsConvertedCount) && (xmlHttpObjectGetAndSetEquationsConvertedCount.readyState == 4) )
		{
			if(xmlHttpObjectGetAndSetEquationsConvertedCount.status == 200)  // 200 = OK
			{
				// *NOT GETTING STATUS 200 - BECAUSE WE ARE NOT RUNNING THIS FILE FROM mathtoweb.com/cgi-bin/

				if(xmlHttpObjectGetAndSetEquationsConvertedCount.responseText)   //xmlHttpObjectGetAndSetEquationsConvertedCount.readyState==4
				{
					//alert('hit, xmlHttpObjectGetAndSetEquationsConvertedCount.responseText' + xmlHttpObjectGetAndSetEquationsConvertedCount.responseText);
					var sResponseText = xmlHttpObjectGetAndSetEquationsConvertedCount.responseText;
					var sArrayParts = sResponseText.split("|");

					var sStringReturn = sArrayParts[0];  	// 'gtnec'
					var sTotalNumberEquationsConverted = sArrayParts[1];  	// A positive int as String representing the total number of equations converted by MathToWeb [Online].

					//alert("sStringReturn: " + sStringReturn + ", sTotalNumberEquationsConverted: " + sTotalNumberEquationsConverted);
					if(sStringReturn == 'gtnec')  // 'gtnec' == 'get total number (of) equations converted'.
					{
						if(sTotalNumberEquationsConverted != '')
						{
							setEquationsConvertedCountToThisStringInteger(sTotalNumberEquationsConverted); // This sets the html in the webpage.
						}

					}
				}
			}
			else
			{	//alert("Problem retrieving XML data"); // We will do nothing and just keep the present number in place.
			}
		}

		return;
	}
	//--


	// This sets the html 'Equations converted count'. Note that the positive integer
	// comes in as an String (this is so we can use BIGINT in database if required).
	//
	function setEquationsConvertedCountToThisStringInteger(sEquationsConvertedCount)
	{
		if(sEquationsConvertedCount == '') return;

		var obj = document.getElementById('equations_converted_count');

		if(obj)
		{
			obj.innerHTML = sEquationsConvertedCount;
		}
		return;

	}

	function getXmlHttpObject()
	{
		var xmlHttp=null;
		try
		{	  // Firefox, Opera 8.0+, Safari
			  xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{
			  // Internet Explorer
			  try
			  {    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			  }
			  catch(e)
			  {    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			  }
		}

		if(xmlHttp == null)
		{	alert("Your browser does not support AJAX.");
			return false;
		}

		return xmlHttp;
	}
	
	
	function showExample(e)
	{
		//alert('e: ' + e);
		//--
		// Stop the mouse event from propagating.
		if (!e) {e = window.event;}
		e.cancelBubble = true;
		if (e.stopPropagation) {e.stopPropagation();}
		//--

		var obj = document.getElementById('example_box_span');

		if(obj)
		{
			var sHTML = "";

			sHTML += "<div style='background-color: #f7ffce; border:1px solid #e0e0e0; padding: 8px;'>";
			sHTML += "Type (or copy/paste) the following LaTeX expression<br/>";
			sHTML += "into the top editor and press the 'Convert' button.<br/>";
			sHTML += "<code>";
			sHTML += "<pre style='font-size: 1.5em;'>$\\int_{\\alpha}^{\\beta} f(x) \\, dx$</pre>";
			sHTML += "</code><br/>";
			sHTML += "<a href='' onclick='hideExample(event); return false;'>Click here to <u>hide</u> this example.</a>";
			sHTML += "</div>";

			obj.innerHTML = sHTML;
		}
	}
	
	function hideExample(e)
	{
		//--
		// Stop the mouse event from propagating.
		if (!e) {e = window.event;}
		e.cancelBubble = true;
		if (e.stopPropagation) {e.stopPropagation();}
		//--

		var obj = document.getElementById('example_box_span');

		if(obj)
		{
			var sHTML = "<a href='' onclick='showExample(event); return false;'>Click here to see an example.</a>";
			obj.innerHTML = sHTML;
		}
	}
	
	
	