//=======================================================================================
/*
function WSEnrichmentEngine()
{
	this.sContent = "this is a try out."; (note: here, try out is a phrase)
	// The vToken and vEnrichment are syncronized, the only difference is in the extra info that vEnrichment holds.
	// vToken is used first to tokenize the string and vEnrichment holds the enrichments info.
	this.vEnrichment =
	[
		{sWord: this, nPos: 0, nLength: 4, nLangType:5, s:[], g:["This"], t:[], e:[], d:[]},
		{sWord: is, nPos: 5, nLength: 2, nLangType:2, s:[], g:[], t:[], e:[], d:[]},
		{sWord: a, nPos: 8, nLength: 1, nLangType:2, s:[], g:[], t:[], e:[], d:[]},
		{sWord: try out, nPos: 10, nLength: 7, nLangType:5, s:[], g:[], t:[], e:["empirical","theoretical"], t:["trial","assignment","experiment"], d:["..."]},
		{sWord: ., nPos: 15, nLength: 1, nLangType:2, s:[], g:[], t:[], e:[], d:[]}
	]
	this.vToken =
	[
		{nPos: 0, nLength: 4, sWord: "this"},
		{nPos: 5, nLength: 2, sWord: "is"},
		{nPos: 8, nLength: 1, sWord: "a"},
		{nPos: 10, nLength: 4, sWord: "try"},
		{nPos: 14, nLength: 4, sWord: "out"},
		{nPos: 15, nLength: 1, sWord: "."}
	]
}
*/
//=======================================================================================
// Defining object: WSEnrichmentEngine
//=======================================================================================
function WSEnrichmentEngine(sUserID,oWSEditor,sContent,sMode,sFormat,sCurrentProfile,bTrial)
{
	//this.bDebug = false;
	this.bUsesIframe = null;
	this.sUserID = (sUserID ? sUserID : "");
	this.bShowErrors = false;
	this.wsEditor = oWSEditor;
	this.stResultElementId = {suggestions: "wsSuggestionsResult", dictionary: "wsDictionaryResult", progress: "wsProgress"};
	this.bTrial = (bTrial? bTrial : false);
	this.sServerURL = "http://enrichment.whitesmoke.com/clients_v2/server.php";
	this.sAJAXExURL = "http://enrichment.whitesmoke.com/clients_v2/xmlhttp.php";
	this.sMode = (sMode && sMode.length>0 ? sMode : "full");
	this.sFormat = (sFormat && sFormat.length>0 ? sFormat : "html");
   this.sProfile = (sCurrentProfile && sCurrentProfile.length>0 ? sCurrentProfile : "998");
	this.nMaxSuggestions = (sMode=="min" ? 4 : 6);
	this.sContent = (sContent? sContent : "");
	this.nMaxLength = 10000;
	this.vTokens = new Array();
	this.vSentenceInfo = new Array();
	this.sOriginalContent = sContent;
	this.vEnrichment = new Array();
	this.vAddedWords = new Array();
	this.sParent = "parent";
	this.oParent = parent;
	this.nSelectedIndex = -1;
	this.stColor = {general: "black", grammar: "limegreen", spelling: "red", enrichment: "blue"};
	this.stLangType = {unknown: 0, verb: 4, noun: 5, adjective: 6, adverb: 7};
	this.state = "";
	this.timerID = null;
	this.vDictionaryCache = new Array();
	this.undoInfo = new Array();
	this.redoInfo = new Array();
	this.stLastPointedInfo = {e: null, oMousePos: null, sWord: "", nLangTYpe: 0, nIndex: 0, oElement: null, vDictionary: new Array()};
	this.stPrevElementStyle = {}; //{font: null, textDecoration: null, borderBottom: null};
	this.vTemplatesMenu = new Array();
	this.sRSFrameId = "WSRSIFrame"; //WhiteSmoke RemoteScript IFrame ID
}
//=======================================================================================
// WSEnrichmentEngine Functions Definition:
//=======================================================================================
WSEnrichmentEngine.prototype.free = function()
{
	this.removeEventHandler();
	this.sContent = null;
	this.vTokens = null;
	this.vEnrichment = null;
	this.vDictionaryCache = null;
	this.setEditorContent("");
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.setEditorContent = function(sContent)
{
	this.wsEditor.setEditorContent(sContent);
	this.createWSElements();
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.setEditorCSS = function()
{
	return this.wsEditor.setEditorCSS();
}	
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEditorContent = function()
{
	return this.wsEditor.getEditorContent();
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEditorFrame = function()
{
	if (this.wsEditor.getEditorFrame)
	{
		return this.wsEditor.getEditorFrame();
	}
	else 
	{
		return this.wsEditor.getEditorWindow();
	}
	
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEditorDocument = function()
{
	var oDocument = this.wsEditor.getEditorFrame();
	if (oDocument && oDocument.document)
	{
		return oDocument.document; 
	}
	return null;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEditorElement = function()
{
	return this.wsEditor.getEditorElement();
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.isUsingIframe = function()
{
	if (this.bUsesIframe == null)
	{
		this.bUsesIframe = false;
		var oEditorElement = this.getEditorElement();
		if (oEditorElement)
		{
			this.bUsesIframe = (oEditorElement.tagName.toUpperCase().indexOf('IFRAME') != -1); 
		}
	}	
	return this.bUsesIframe;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getElementById = function(sId)
{
	var oEditorFrame = window;
	if (this.isUsingIframe())
	{
		oEditorFrame = this.getEditorFrame();
	}
	if (oEditorFrame)
	{
		return oEditorFrame.document.getElementById(sId);
	}
	return null;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getElementByIndex = function(nIndex)
{
	var sId = "wsWord" + nIndex;
	return this.getElementById(sId);
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getElementPosition = function(oElement)
{
	var sId = oElement.id;
	var sContent = this.getEditorContent();
	var nPos = sContent.indexOf(sId);
	if (nPos >= 0)
	{
		nPos = sContent.indexOf(">",nPos);
		if (nPos >= 0)
		{
			++nPos;
		}	
	}
	return nPos;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.removeWSElements = function()
{
	var oEditorFrame = this.getEditorFrame();
	for (var i=0; i<30; ++i)
	{
		if (oEditorFrame && oEditorFrame.document && oEditorFrame.document.body)
		{
			for (var sTagID in this.stResultElementId)
			{
				var oElement = oEditorFrame.document.getElementById(this.stResultElementId[sTagID]);
				if (oElement)
				{
					oEditorFrame.document.body.removeChild(oElement);
				}
			}
			return true;
		}
	}
	return false;

	/*
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		var suggestionElement = oEditorFrame.document.getElementById(this.stResultElementId.suggestions);
		if (suggestionElement)
		{
			oEditorFrame.document.body.removeChild(suggestionElement);
		}
		var progressElement = oEditorFrame.document.getElementById(this.stResultElementId.progress);
		if (progressElement)
		{
			oEditorFrame.document.body.removeChild(progressElement);
		}
		var sigElement = oEditorFrame.document.getElementById(this.stResultElementId.signature);
		if (sigElement)
		{
			oEditorFrame.document.body.removeChild(sigElement);
		}
	}
	*/	
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.createWSElements = function()
{
	var oEditorFrame = this.getEditorFrame();
	for (var i=0; i<30; ++i)
	{
		if (oEditorFrame && oEditorFrame.document && oEditorFrame.document.body)
		{
			this.removeWSElements();
			for (var sTagID in this.stResultElementId)
			{
				var	oElement = oEditorFrame.document.createElement('DIV');
				oElement.setAttribute('id',this.stResultElementId[sTagID]);
				oElement.setAttribute('name',this.stResultElementId[sTagID]);
				oElement.style.position='absolute';
				oElement.style.zIndex='200';
				oElement.style.display='none';
				oEditorFrame.document.body.appendChild(oElement);
			}
			return true;
		}
	}
	return false;

	/*var oEditorFrame = this.getEditorFrame();
	for (var i=0; i<30; ++i)
	{
		if (oEditorFrame && oEditorFrame.document && oEditorFrame.document.body)
		{
			this.removeWSElements();
	
			var	suggestionElement = oEditorFrame.document.createElement('DIV');
			suggestionElement.setAttribute('id',this.stResultElementId.suggestions);
			suggestionElement.setAttribute('name',this.stResultElementId.suggestions);
			suggestionElement.style.position='absolute';
			suggestionElement.style.zIndex='200';
			suggestionElement.style.display='none';
			oEditorFrame.document.body.appendChild(suggestionElement);
					
			//var progressElement = oEditorFrame.document.getElementById(this.stResultElementId.progress);
			var progressElement = oEditorFrame.document.createElement('DIV');
			progressElement.setAttribute('id',this.stResultElementId.progress);
			progressElement.setAttribute('name',this.stResultElementId.progress);
			progressElement.style.position='absolute';
			progressElement.style.zIndex='100';
			progressElement.style.display='none';
			oEditorFrame.document.body.appendChild(progressElement);

			break;
			return true;
		}
	}	
	return false;
	*/	
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getSuggestionsElement = function()
{
	var oElement = this.getElementById(this.stResultElementId.suggestions);
	return oElement;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getDictionaryElement = function()
{
	var oElement = this.getElementById(this.stResultElementId.dictionary);
	return oElement;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getProgressElement = function()
{
	var oElement = this.getElementById(this.stResultElementId.progress);
	return oElement;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getElementTagPosition = function(oElement)
{
	var sId = oElement.id;
	var sContent = this.getEditorContent();
	var nPos = sContent.indexOf(sId);
	if (nPos >= 0)
	{
		while (nPos>=0 && sContent.charAt(nPos) != "<")
		{
			--nPos;
		}
	}
	return nPos;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getSelectedProfile = function()
{
	return this.sProfile;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.initContent = function()
{
	var sContent = this.getEnrichedContent();
	this.displayContent(sContent);
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.displayContent = function(sContent)
{
	this.setEditorContent(sContent);
	setTimeout("onSetWSEventHandler()", 70);
	//this.setEventHandler();
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.buildSentenceContainer = function(vTokens)
{
	for (var i=0, j=0; i< vTokens.length; ++i)
	{
		if (vTokens[i].sWord == ".")
		{
			var nPos = vTokens[j].nPos;
			var nLength = vTokens[i].nPos + vTokens[i].nLength - nPos;
			this.vSentenceInfo[this.vSentenceInfo.length] = {nPos: nPos, nLength: nLength};
			j = i + 1;
		}
		else if (i == vTokens.length - 1)
		{
			var nPos = vTokens[j].nPos;
			var nLength = vTokens[i].nPos + this.vTokens[i].nLength - nPos;
			this.vSentenceInfo[this.vSentenceInfo.length] = {nPos: nPos, nLength: nLength};
			j = i + 1;
		}
	}

	/*
	alert("this.vSentenceInfo.length = " + this.vSentenceInfo.length);
	var sOut = "";
	for (var i=0; i < this.vSentenceInfo.length; ++i)
	{
		var nPos = this.vSentenceInfo[i].nPos;
		var nLength = this.vSentenceInfo[i].nLength;
		var sSentence = this.sContent.substr(nPos,nLength);
		sOut += "this.vSentenceInfo[" + i + "]:  sSentence=[" + sSentence + "], pos=[" + nPos + "], nLength=[" + nLength + "]\n";
	}
	alert("sOut = \n" + sOut);
	*/
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.tokenize = function(sContent)
{
	var htmlParser = new WSHTMLParser();
	this.vTokens = htmlParser.parseHTML(sContent);
	
	/*
	if (this.isHTMLMode())
	{
		this.vTokens = htmlParser.parseHTML(sContent);
	}
	else //plain text mode
	{
		this.vTokens = htmlParser.parseText(sContent);
	}
	*/
	
	//buildSentenceContainer(this.vTokens);

	/*
	var sOut = "";
	for (var i=0; i< this.vTokens.length; ++i)
	{
		sOut += "token [" + i + "]:  word=[" + this.vTokens[i].sWord + "] pos=[" + this.vTokens[i].nPos + "]\n";
	}
	alert(sOut);
	*/
	
	return this.vTokens;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.reTokenize = function(sContent)
{
	var htmlParser = new WSHTMLParser();
	var vTokens = new Array();
	if (this.isHTMLMode())
	{
		vTokens = htmlParser.parseHTML(sContent);
	}
	else //plain text mode
	{
		vTokens = htmlParser.parseText(sContent);
	}

	return this.vTokens;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.isHTMLMode = function()
{
	return (this.sFormat != "text");
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.isAlpha = function(sWord)
{
	//var regex=/[A-Za-z]*/;
	//return regex.test(sWord);

	for (var i=0; i<sWord.length; ++i)
	{
		var ch = sWord.charCodeAt(i);
		if ((ch<65 || ch>90) && (ch<97 || ch>122))
		{
			return false;
		}
	}
	return true;
}
//--------------------------------------------------------------------------------------
/*
WSEnrichmentEngine.prototype.cleanWSContentOriginal = function(sContent)
{
	var sNewContent = "";
	for (var i=0, nIndex=0; i<sContent.length; ++i, ++nIndex)
	{
		var sId = "wsWord" + nIndex.toString();
		var j = sContent.indexOf(sId,i);
		if (j >= 0)
		{
			while (sContent.charAt(j) != "<")
			{
				--j;
			}
			if (j >= 0)
			{
				sNewContent += sContent.substr(i,j-i);
				var nStart = sContent.indexOf(">",j);
				if (nStart >= 0)
				{
					++nStart;
					var nEnd = sContent.indexOf("</",nStart);
					if (nEnd >= 0)
					{
						//alert("start=" + nStart + ", end=" + nEnd + ", token=[" + sContent.substr(nStart,nEnd-nStart) + "]");
						sNewContent	+= sContent.substr(nStart,nEnd-nStart);
						i = nEnd + String("</span>").length - 1;
					}
					else
					{
						//alert("error! nEnd=" + nEnd);
					}
				}
				else
				{
					//alert("error! nStart=" + nStart);
				}
			}
		}
		else
		{
			sNewContent += sContent.substr(i);
			break;
		}
	}
	return sNewContent;
}
*/
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.cleanWSContentById = function(sContent,sIdPrefix,sTagName)
{
	var sNewContent = "";
	var nPos = 0;
	for ( ; nPos<sContent.length; ++nPos)
	{
		var j = sContent.indexOf(sIdPrefix,nPos);
		if (j >= 2 && (sContent.charAt(j-1) == "=" || sContent.charAt(j-2) == "=")) //looking for <span ... id=wsWord ..>
		{
			while (sContent.charAt(j) != "<")
			{
				--j;
			}
			if (j >= 0)
			{
				sNewContent	+= sContent.substr(nPos,j-nPos);
				var nStart = sContent.indexOf(">",j);
				if (nStart >= 0)
				{
					++nStart;
					var nEnd = sContent.indexOf("</",nStart);
					if (nEnd >= 0)
					{
						//alert("start=" + nStart + ", end=" + nEnd + ", token=[" + sContent.substr(nStart,nEnd-nStart) + "]");
						sNewContent	+= sContent.substr(nStart,nEnd-nStart);
						var tag = "</" + sTagName + ">";
						nPos = nEnd + tag.length - 1;
					}
					else
					{
						//alert("error! nEnd=" + nEnd);
					}
				}
				else
				{
					//alert("error! nStart=" + nStart);
				}
			}
		}
		else
		{
			sNewContent	+= sContent.substr(nPos);
			break;
		}
	}
	return sNewContent;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.cleanWSContent = function(sContent)
{
	var sNewContent = this.cleanWSContentById(sContent,"wsWord","span");
	//sNewContent = this.cleanWSContentById(sNewContent,this.stResultElementId.signature,"span");
	return sNewContent;
}
//--------------------------------------------------------------------------------------
/*
WSEnrichmentEngine.prototype.cleanWSContent = function(sContent)
{
	var sNewContent = "";
	var nPos = 0;
	for ( ; nPos<sContent.length; ++nPos)
	{
		var sIdPrefix = "wsWord";
		var j = sContent.indexOf(sIdPrefix,nPos);
		if (j >= 2 && (sContent.charAt(j-1) == "=" || sContent.charAt(j-2) == "=")) //looking for <span ... id=wsWord ..>
		{
			while (sContent.charAt(j) != "<")
			{
				--j;
			}
			if (j >= 0)
			{
				sNewContent	+= sContent.substr(nPos,j-nPos);
				var nStart = sContent.indexOf(">",j);
				if (nStart >= 0)
				{
					++nStart;
					var nEnd = sContent.indexOf("</",nStart);
					if (nEnd >= 0)
					{
						//alert("start=" + nStart + ", end=" + nEnd + ", token=[" + sContent.substr(nStart,nEnd-nStart) + "]");
						sNewContent	+= sContent.substr(nStart,nEnd-nStart);
						nPos = nEnd + String("</span>").length - 1;
					}
					else
					{
						//alert("error! nEnd=" + nEnd);
					}
				}
				else
				{
					//alert("error! nStart=" + nStart);
				}
			}
		}
		else
		{
			sNewContent	+= sContent.substr(nPos);
			break;
		}
	}
	return sNewContent;
}
*/
//--------------------------------------------------------------------------------------
/*
WSEnrichmentEngine.prototype.cleanWSElement = function(sContent,nPos,sId)
{
	var sNewContent = "";

	var j = sContent.indexOf(sId,nPos);
	if (j >= 0)
	{
		while (sContent.charAt(j) != "<")
		{
			--j;
		}
		if (j >= 0)
		{
			sNewContent	+= sContent.substr(nPos,j-nPos);
			var nStart = sContent.indexOf(">",j);
			if (nStart >= 0)
			{
				++nStart;
				var nEnd = sContent.indexOf("</",nStart);
				if (nEnd >= 0)
				{
					//alert("start=" + nStart + ", end=" + nEnd + ", token=[" + sContent.substr(nStart,nEnd-nStart) + "]");
					sNewContent	+= sContent.substr(nStart,nEnd-nStart);
					nPos = nEnd + String("</span>").length - 1;
				}
				else
				{
					alert("error! nEnd=" + nEnd);
				}
			}
			else
			{
				alert("error! nStart=" + nStart);
			}
		}
	}
	else
	{
		sNewContent	+= sContent.substr(nPos);
		break;
	}
	return sNewContent;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.cleanWSContent = function(sContent)
{
	var sNewContent = "";
	for (var nPos=0, nIndex=0; nPos<sContent.length; ++nPos, ++nIndex)
	{
		var sId = "wsWordAdd" + nIndex.toString();
		sNewContent += this.cleanWSElement(sContent,nPos,sId);
		nPos += 
		var sId = "wsWord" + nIndex.toString();
		sNewContent += this.cleanWSElement(sContent,nPos,sId);
	}
	return sNewContent;
}
*/
/*
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.cleanWSContent = function(sContent)
{
	var sNewContent = "";
	var nPrevPos = 0;
	for (var i=0; i<this.vEnrichment.length; ++i)
	{
		var enrichment = this.vEnrichment[i];
		var nPos = enrichment.nPos;
		var nLength = enrichment.nLength;
		
		var sId = "wsWord" + i.toString();

		var j = sContent.indexOf(sId,i);
		if (j >= 0)
		{
			while (sContent.charAt(j) != "<")
			{
				--j;
			}
			if (j >= 0)
			{
				sNewContent	+= sContent.substr(nPrevPos,j-nPrevPOs);
				var nStart = sContent.indexOf(">",j);
				if (nStart >= 0)
				{
					++nStart;
					var nEnd = sContent.indexOf("</",nStart);
					if (nEnd >= 0)
					{
						//alert("start=" + nStart + ", end=" + nEnd + ", token=[" + sContent.substr(nStart,nEnd-nStart) + "]");
						sNewContent	+= sContent.substr(nStart,nEnd-nStart);
						i = nEnd + String("</span>").length - 1;
					}
					else
					{
						alert("error! nEnd=" + nEnd);
					}
				}
				else
				{
					alert("error! nStart=" + nStart);
				}
			}
		}
		else
		{
			sNewContent	+= sContent.substr(i);
			break;
		}
	}
	alert("Before = \n" + sContent);
	alert("After = \n" + sNewContent);
	return sNewContent;
}
*/
//--------------------------------------------------------------------------------------
/*
WSEnrichmentEngine.prototype.getFormatedWord = function(sWord, nIndex, nLangType)
{
	var sEscapedWord = sWord.replace(/\'/g,"\\'");
	var sContent = "";
	var enrichment = this.vEnrichment[nIndex];
	if (enrichment)
	{
		var sColor = "";
		var sFunc = "";
		if (enrichment.s && enrichment.s.length > 0) //spelling
		{
			sColor = this.stColor.spelling;
			sFunc  = "onShowSpellingSuggestions";
		}
		else if (enrichment.g && enrichment.g.length > 0) //grammar
		{
			sColor = this.stColor.grammar;
			sFunc  = "onShowGrammarSuggestions";
		}
		else if ((enrichment.t && enrichment.t.length > 0)  || //thesaurus
					(enrichment.e && enrichment.e.length > 0)) //enrichment
		{
			sColor = this.stColor.enrichment;
			sFunc  = "onShowEnrichmentSuggestions";
		}
		if (sFunc.length > 0)
		{
			var sBorderStyle = "dotted";
			if (sWord.length == 1)
			{
				sBorderStyle = "solid";
			}
			//var nLangType = this.vEnrichment[nIndex].nLangType;
			sContent += '<span id="wsWord' + nIndex.toString() + '" ';
			sContent += 'style="position:static; cursor:pointer; cursor:hand; border-bottom: 2px ' + sBorderStyle + ' ' + sColor + ';" ';
			sContent += 'onMouseOver="javascript:' + this.sParent + '.onStartDictionaryTimer(\'' + sEscapedWord + '\',' + nIndex.toString() + ',this,' + nLangType + ');" ';
			sContent += 'onMouseOut="javascript:' + this.sParent + '.onStopDictionaryTimer();" ';
			sContent += 'onClick="javascript:' + this.sParent + '.onStopDictionaryTimer(); ';
			sContent += this.sParent + "." + sFunc + '(' + nIndex.toString() + ');">';
			sContent += sWord;
			sContent += '</span>';
		}
		else if (sWord.length >= 2 && this.isAlpha(sWord.charAt(0)))
		{
			sContent += '<span id="wsWord' + nIndex.toString() + '" ';
			sContent += 'style="position:static; cursor:pointer; cursor:hand;" ';
			sContent += 'onMouseOver="javascript:' + this.sParent + '.onStartDictionaryTimer(\'' + sEscapedWord + '\',' + nIndex.toString() + ',this,' + nLangType + ');" ';
			sContent += 'onMouseOut="javascript:' + this.sParent + '.onStopDictionaryTimer();">';
			sContent += sWord;
			sContent += '</span>';
		}
		else
		{
			sContent += '<span id="wsWord' + nIndex.toString() + '">';
			sContent += sWord;
			sContent += '</span>';
		}
	}
	else
	{
		sContent += '<span id="wsWord' + nIndex.toString() + '" ';
		sContent += 'style="position:static; cursor:pointer; cursor:hand;" ';
		sContent += 'onMouseOver="javascript:' + this.sParent + '.onStartDictionaryTimer(\'' + sEscapedWord + '\',' + nIndex.toString() + ',this,' + nLangType + ');" ';
		sContent += 'onMouseOut="javascript:' + this.sParent + '.onStopDictionaryTimer();">';
		sContent += sWord;
		sContent += '</span>';
	}
	return sContent;
}
*/
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getClassName = function(enrichment)
{
	var sClassName = "";
	if (enrichment)
	{
		if (enrichment.s && enrichment.s.length > 0) //spelling
		{
			sClassName = "wsSpelling";
		}
		else if (enrichment.g && enrichment.g.length > 0) //grammar
		{
			sClassName = "wsGrammar";
		}
		else if ((enrichment.t && enrichment.t.length > 0)  || //thesaurus
				 (enrichment.e && enrichment.e.length > 0)) //enrichment
		{
			sClassName = "wsEnrichment";
		}
	}	
	return sClassName;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getSuggestionsLength = function(enrichment)
{
	var nLength = 0;
	if (enrichment)
	{
		if (enrichment.s && enrichment.s.length > 0) //spelling
		{
			nLength = enrichment.s.length;
		}
		else if (enrichment.g && enrichment.g.length > 0) //grammar
		{
			nLength = enrichment.g.length;
		}
		else if ((enrichment.t && enrichment.t.length > 0)  || //thesaurus
				 (enrichment.e && enrichment.e.length > 0)) //enrichment
		{
			nLength = Math.max(enrichment.t.length,enrichment.e.length);
		}
	}	
	return Math.min(nLength,this.nMaxSuggestions);
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getVectorWidth = function(vSuggestions)
{
	var nMaxLength=0;
	if (vSuggestions)
	{
		for (var i=0; i<vSuggestions.length; ++i)
		{
			if (nMaxLength < vSuggestions[i].length)
			{
				nMaxLength = vSuggestions[i].length;
			}
		}
	}	
	return nMaxLength;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getSuggestionsWidth = function(enrichment)
{
	var nMaxLength=0;
	if (enrichment)
	{
		if (enrichment.s && enrichment.s.length > 0) //spelling
		{
			nMaxLength = Math.max(8,this.getVectorWidth(enrichment.s)) + 3;
		}
		else if (enrichment.g && enrichment.g.length > 0) //grammar
		{
			nMaxLength = Math.max(8,this.getVectorWidth(enrichment.g)) + 3;
		}
		else if ((enrichment.t && enrichment.t.length > 0)  || //thesaurus
				 (enrichment.e && enrichment.e.length > 0)) //enrichment
		{
			var nMax1=0, nMax2=0; 
			if (enrichment.t && enrichment.t.length > 0)
			{
				nMax1 = Math.max(10,this.getVectorWidth(enrichment.t)) + 3;
			}
			if (enrichment.e && enrichment.e.length > 0)
			{
				nMax2 = Math.max(10,this.getVectorWidth(enrichment.e)) + 3;
			}
			nMaxLength = nMax1 + nMax2;
		}
	}
	var pxWidth = nMaxLength * 8;
	//alert("nMaxLength = " + nMaxLength);
	return pxWidth;
}
//--------------------------------------------------------------------------------------
/*
WSEnrichmentEngine.prototype.getSuggestionsWidth = function(enrichment)
{
	var nColumn = 0;
	if (enrichment)
	{
		if (enrichment.s && enrichment.s.length > 0) //spelling
		{
			nColumn = 1;
		}
		else if (enrichment.g && enrichment.g.length > 0) //grammar
		{
			nColumn = 1;
		}
		else if ((enrichment.t && enrichment.t.length > 0)  || //thesaurus
					(enrichment.e && enrichment.e.length > 0)) //enrichment
		{
			nColumn = 1;
			if ((enrichment.t && enrichment.t.length > 0)  && (enrichment.e && enrichment.e.length > 0))
			{
				nColumn = 2;
			}
		}
	}
	var pxWidth = nColumn * 105;
	return pxWidth;
}
*/
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getFormatedWord = function(sWord, nIndex, nLangType)
{
	var sContent = "";
	sContent += '<span id="wsWordAdd' + nIndex.toString() + '"></span>';
	var enrichment = this.vEnrichment[nIndex];
	
	if (enrichment)
	{
		var sClassName = this.getClassName(enrichment);
		if (sClassName.length > 0)
		{
			sContent += '<span id="wsWord' + nIndex.toString() + '" ';
			sContent += 'class="' +  sClassName + '">';
			sContent += sWord;
			sContent += '</span>';
			//this.setEventArgs(sId,{sWord: sWord, nIndex: nIndex, sFunc: sFunc ...});
		}
		else if (sWord.length >= 2 && this.isAlpha(sWord)) //this.isAlpha(sWord.charAt(0)))
		{
			sContent += '<span id="wsWord' + nIndex.toString() + '" ';
			sContent += 'class="ws">';
			sContent += sWord;
			sContent += '</span>';
		}
		else //a single letter word or not an alphabet word (contains digits etc).
		{
			sContent += '<span id="wsWord' + nIndex.toString() + '">';
			sContent += sWord;
			sContent += '</span>';
		}
	}
	else
	{
		sContent += '<span id="wsWord' + nIndex.toString() + '" ';
		sContent += 'class="ws">';
		sContent += sWord;
		sContent += '</span>';
	}
	return sContent;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.addEvent = function(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false);
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else
	{
		return false;
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.removeEvent = function(obj, evType, fn)
{
	if (obj.removeEventListener)
	{
		obj.removeEventListener(evType, fn, false);
		return true;
	}
	else if (obj.dispatchEvent)
	{
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	}
	else
	{
		return false;
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.setEventHandler = function()
{
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		this.addEvent(oEditorFrame.document, 'keyup', onHandleKeyUp);
		this.addEvent(oEditorFrame.document, 'mousedown', onHandleMouseUp);
		this.addEvent(oEditorFrame.document, 'mousedown', onMouseClickWord);
		//this.addEvent(oEditorFrame.document, 'contextmenu', onMouseClickWord); //right click for popup menu
	}
	////this.addEvent(oEditorFrame.window, 'mousedown', onMouseClickWord);
	
	for (var nIndex=0; nIndex<this.vEnrichment.length; ++nIndex)
	{
		var oElement = this.getElementByIndex(nIndex);
		if (oElement)
		{
			this.addEvent(oElement, "mouseup", onMouseClickWord); //add by reference
			this.addEvent(oElement, "mouseover", onMouseOverWord); //function added by reference
			this.addEvent(oElement, "mouseout", onMouseOutWord); //function added by reference
		}
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.removeEventHandler = function()
{
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		this.removeEvent(oEditorFrame.document, 'keyup', onHandleKeyUp);
		this.removeEvent(oEditorFrame.document, 'mousedown', onHandleMouseUp);
		this.removeEvent(oEditorFrame.document, 'mousedown', onMouseClickWord);
		//this.removeEvent(oEditorFrame.document, 'contextmenu', onMouseClickWord);
	}	
	
	for (var nIndex=0; nIndex<this.vEnrichment.length; ++nIndex)
	{
		//var sId = "wsWord" + nIndex;
		//var oElement = oEditorFrame.document.getElementById(sId);
		var oElement = this.getElementByIndex(nIndex);
		if (oElement)
		{
			this.removeEvent(oElement, "mouseup", onMouseClickWord); //add by reference
			this.removeEvent(oElement, "mouseover", onMouseOverWord); //function added by reference
			this.removeEvent(oElement, "mouseout", onMouseOutWord); //function added by reference
		}
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEnrichedContent = function()
{
	var sContent = "";
	var nPrevPos = 0;
	var bBodyExist = false;
	
	//alert("getEnrichedContent, sContent = [\n" + this.sContent  + "\n]");	

	var nBodyPos = this.sContent.indexOf("<body");
	if (nBodyPos < 0)
	{
		nBodyPos = this.sContent.indexOf("<BODY");
	}
	if (nBodyPos >= 0)
	{
		bBodyExist = true;
		nBodyPos += 5;
		sContent += this.sContent.substr(0,nBodyPos);
		var nEndTagPos = this.sContent.indexOf(">",nBodyPos);
		if (nEndTagPos && nEndTagPos>=0)
		{
			++nEndTagPos;
			sContent += this.sContent.substr(nBodyPos,nEndTagPos-nBodyPos);
			nPrevPos = nEndTagPos;
		}
	}
	/*
	if (bBodyExist == false)
	{
		sContent += '<html><head></head><body scroll="auto" width="100%">';
	}
	*/
	for (var i = 0 ; i < this.vEnrichment.length ; ++i)
	{
		var enrichment = this.vEnrichment[i];
		var nPos = enrichment.nPos;
		var nLength = enrichment.nLength;
		var nLangType = enrichment.nLangType;
		var sWord = this.sContent.substr(nPos,nLength);

		for (var j=0; j<this.vAddedWords.length; ++j)
		{
			var nTmpPos    = this.vAddedWords[j].nPos;
			var nTmpLength = this.vAddedWords[j].nLength;
			var nTmpIndex  = this.vAddedWords[j].nIndex;
			var nTmpLangType  = this.vAddedWords[j].nLangType;

			//alert("nPrevPos=" + nPrevPos + ", nPos=" + nPos + ", nTmpPos=" + nTmpPos + ", nTmpLength=" + nTmpLength);
			if (nTmpPos > nPrevPos && nTmpPos < nPos)
			{
				sContent += this.sContent.substr(nPrevPos, nTmpPos-nPrevPos);
				var sEnrichedWord = this.sContent.substr(nTmpPos,nTmpLength);
				sContent += this.getFormatedWord(sEnrichedWord, nTmpIndex, nTmpLangType);
				nPrevPos = nTmpPos + nTmpLength;
			}
		}

		sContent += this.sContent.substr(nPrevPos, nPos-nPrevPos); //.replace(/ /g,"&nbsp;");
		nPrevPos = nPos + nLength;
		sContent += this.getFormatedWord(sWord,i,nLangType);
		if (i == this.vEnrichment.length-1 && this.sContent.indexOf("&nbsp;",nPrevPos) < 0)
		{
			sContent += "&nbsp;";
		}
	}
	sContent += this.sContent.substr(nPrevPos);

	if (bBodyExist == false)
	{
		//sContent += '</body></html>';
		if (! this.isHTMLMode())
		{
			sContent = sContent.replace(/\r\n|\n/g,"<br />");
		}
	}
	return sContent;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.updateWordStyle = function(nIndex, enrichment)
{
	var sClassName = this.getClassName(enrichment);
	var oElement = this.getElementByIndex(nIndex);
	oElement.className = sClassName;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.setEnrichmentInfo = function(oEnrichmentInfo)
{
	this.vEnrichment = oEnrichmentInfo;
	if (this.vEnrichment.length == 0)
	{
		this.nSelectedIndex = -1;
	}
	else if (this.nSelectedIndex >= this.vEnrichment.length)
	{
		this.nSelectedIndex = 0;
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.mergeEnrichmentInfo = function(oEnrichmentInfo)
{
	if (oEnrichmentInfo && oEnrichmentInfo.length > 0)
	{
		oEnrichmentInfo[0].sWord = this.vEnrichment[this.nSelectedIndex].sWord;
		oEnrichmentInfo[0].nPos = this.vEnrichment[this.nSelectedIndex].nPos;
		oEnrichmentInfo[0].nLength = this.vEnrichment[this.nSelectedIndex].nLength;
		this.vEnrichment[this.nSelectedIndex] = oEnrichmentInfo[0];

		this.updateWordStyle(this.nSelectedIndex,oEnrichmentInfo[0]);
		this.nSelectedIndex = -1;
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.setLastPointedInfo = function(e,oMousePos,sWord,nLastLangType,nLastPointedIndex,oLastPointedElement)
{
	this.stLastPointedInfo.e = e;
	this.stLastPointedInfo.oMousePos = oMousePos;
	this.stLastPointedInfo.sWord = sWord;
	this.stLastPointedInfo.nLangType = nLastLangType;
	this.stLastPointedInfo.nIndex = nLastPointedIndex;
	this.stLastPointedInfo.oElement = oLastPointedElement;
	this.stLastPointedInfo.vDictionary = new Array();
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.setDictionaryInfo = function(oEnrichmentInfo)
{
	this.stLastPointedInfo.vDictionary = new Array();
	if (oEnrichmentInfo && oEnrichmentInfo.length > 0)
	{
		this.stLastPointedInfo.vDictionary = oEnrichmentInfo[0].d;
	}
	var sWord = this.stLastPointedInfo.sWord;
	var nLangType = this.stLastPointedInfo.nLangType;
	if (! this.vDictionaryCache[nLangType])
	{
		this.vDictionaryCache[nLangType] = new Array(sWord);
	}
	this.vDictionaryCache[nLangType][sWord] = this.stLastPointedInfo.vDictionary;
	//alert("Dic[" + nLangType  + "," + sWord + "] := [" + this.vDictionaryCache[nLangType][sWord] + "]");
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getDictionaryInfo = function(sWord,nLangType)
{
	this.stLastPointedInfo.vDictionary = new Array();
	if (this.vDictionaryCache[nLangType] && this.vDictionaryCache[nLangType][sWord])
	{
		//alert("Dic[" + nLangType  + "," + sWord + "] == [" + this.vDictionaryCache[nLangType][sWord] + "]");
		this.stLastPointedInfo.vDictionary = this.vDictionaryCache[nLangType][sWord];
		return this.stLastPointedInfo.vDictionary;
	}
	//alert("Dic[" + nLangType  + "," + sWord + "] == null");
	return null;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.hideElement = function(oElement)
{
	if (oElement)
	{
		oElement.style.display = "none";
		oElement.style.visibility = "hidden";
		oElement.innerHTML = "";
	}
	return oElement;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.hideDictionary = function()
{
	onStopDictionaryTimer();
	var oElement = this.getDictionaryElement();
	if (oElement)
	{
		this.hideElement(oElement);
	}	
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.hideSuggestions = function()
{
	this.hideDictionary();
	var oElement = this.getSuggestionsElement();
	if (oElement)
	{
		this.hideElement(oElement);
	}	
	this.markWord(-1);
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getMousePos = function(e)
{
	var nMouseX = 0;
	var nMouseY = 0;
	
	try
	{
		if (!e)
		{
			var e = window.event;
		}
		if (!e)
		{
			var oMousePos = this.stLastPointedInfo.oMousePos;
			nMouseX = oMousePos.X;
			nMouseY = oMousePos.Y;
		}
		else if (e.clientX || e.clientY)
		{
			nMouseX = parseInt(e.clientX); //- parseInt(document.body.scrollLeft);
			nMouseY = parseInt(e.clientY); //- parseInt(document.body.scrollTop);
		}
		else if (e.pageX || e.pageY)
		{
			nMouseX = parseInt(e.pageX);
			nMouseY = parseInt(e.pageY);
		}
		else
		{
			var oMousePos = this.stLastPointedInfo.oMousePos;
			nMouseX = oMousePos.X;
			nMouseY = oMousePos.Y;
		}
	}
	catch (exception)
	{
		var oMousePos = this.stLastPointedInfo.oMousePos;
		nMouseX = oMousePos.X;
		nMouseY = oMousePos.Y;
	}
	// posx and posy contain the mouse position relative to the document
	//alert("X,Y = [" + nMouseX + ", " + nMouseY+ " ]");
	return {X: nMouseX, Y: nMouseY};
}
//--------------------------------------------------------------------------------------
/*
// get the position by the element, not by the mouse. not used becuase of a bug in ie. 
WSEnrichmentEngine.prototype.displaySuggestionsByElement = function(e,oDisplayElement,nSelectedIndex)
{
	var oEditorFrame = this.getEditorFrame();
	var oSpan = oEditorFrame.document.getElementById("wsWord" + nSelectedIndex.toString());
	if (oSpan)
	{
		var oEditorElement = this.getEditorElement();
		var framePosX = findPosX(oEditorElement);
		var framePosY = findPosY(oEditorElement);
		var spanPosX = parseInt(oSpan.offsetLeft) - parseInt(oEditorFrame.document.body.scrollLeft);
		var spanPosY = parseInt(oSpan.offsetTop)  - parseInt(oEditorFrame.document.body.scrollTop);

		oDisplayElement.style.left = spanPosX + framePosX;
		if (this.vEnrichment[nSelectedIndex].e.length > 0 && this.vEnrichment[nSelectedIndex].t.length > 0)
		{
			// display the thesaurus under the word (shift 100 pix to the left if possible)
			if (parseInt(oDisplayElement.style.left) >= 100)
			{
				oDisplayElement.style.left = parseInt(oDisplayElement.style.left) - 100;
			}
		}
		oDisplayElement.style.top  = spanPosY + framePosY + 20;
		oDisplayElement.style.display = "block";
		oDisplayElement.style.visibility = "visible";
		//oDisplayElement.style.zIndex = 100;
		return true;
	}
  	return false;
}
*/
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEditorViewport = function()
{
	var x=0 ,y=0;
	var oEditorFrame = this.getEditorFrame();
	if (! oEditorFrame)
	{
		return {width: x, height: y}; 
	}
	var oWindow = oEditorFrame;
	if (oEditorFrame.window)
	{
		oWindow = oEditorFrame.window;
	}

	if (oWindow.innerHeight) // all except Explorer
	{
		x = oWindow.innerWidth;
		y = oWindow.innerHeight;
	}
	else if (oWindow.document.documentElement && oWindow.document.documentElement.clientHeight)  //Explorer 6 Strict Mode
	{
		x = oWindow.document.documentElement.clientWidth;
		y = oWindow.document.documentElement.clientHeight;
	}
	else if (oWindow.document.body) // other Explorers
	{
		x = oWindow.document.body.clientWidth;
		y = oWindow.document.body.clientHeight;
	}
	return {width: x, height: y};
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEditorPosition = function()
{
	if (! this.isUsingIframe())
	{
		var oEditorElement = this.getEditorElement();
		if (oEditorElement)
		{
			var nFramePosX = parseInt(findPosX(oEditorElement)); // + parseInt(document.body.scrollLeft);
			var nFramePosY = parseInt(findPosY(oEditorElement)); // + parseInt(document.body.scrollTop);
			return {X: nFramePosX, Y: nFramePosY};
		}
	}	
	return {X: 0, Y: 0};
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.calcSuggestionsPosition = function(e,nSelectedIndex)
{
	var oReturnPos = {X: 0, Y: 0};

	var oMousePos = this.getMousePos(e);
	var nMouseX = parseInt(oMousePos.X);
	var nMouseY = parseInt(oMousePos.Y);
	
	//var oEditorPos = this.getEditorPosition();
	var nFramePosX  = 0; //oEditorPos.X;
	var nFramePosY = 0; //oEditorPos.Y;

	var nScrollLeft = 0;
	var nScrollTop = 0;
	
	if (this.isUsingIframe())
	{
		var oEditorFrame = this.getEditorFrame();
		if (oEditorFrame)
		{
			nScrollLeft = parseInt(oEditorFrame.document.body.scrollLeft); 
			nScrollTop  = parseInt(oEditorFrame.document.body.scrollTop);
		}
	}
	else
	{
		nFramePosX  = parseInt(document.body.scrollLeft);
		nFramePosY = parseInt(document.body.scrollTop)
	}
	
	var oViewport = this.getEditorViewport();
	var nFrameWidth  = parseInt(oViewport.width);
	var nFrameHeight = parseInt(oViewport.height);
	
	oReturnPos.X = nMouseX + nFramePosX + nScrollLeft - 9;
	oReturnPos.Y = nMouseY + nFramePosY + nScrollTop; // spanPosY + framePosY + 24;
	
	var enrichment = this.vEnrichment[nSelectedIndex];
	var pxWidth = parseInt(this.getSuggestionsWidth(enrichment));
	if (pxWidth >= 200)
	{
		// display the thesaurus under the word (shift 100 pix to the left if possible)
		if (parseInt(oReturnPos.X) >= 100)
		{
			oReturnPos.X = parseInt(oReturnPos.X) - 100;
		}
	}

	// fix the position so it wont be out of the frame.
	var nRightBorder = parseInt(parseInt(nFramePosX) + parseInt(nFrameWidth) - parseInt(pxWidth));
	var nLeftBorder = parseInt(nFramePosX);
	oReturnPos.X = Math.min(oReturnPos.X,nRightBorder);
	oReturnPos.X = Math.max(oReturnPos.X,nLeftBorder);

	if (this.isUsingIframe())
	{
		var pxLength = (this.getSuggestionsLength(enrichment)) * 16 + 55;
		if (oReturnPos.Y >= nFramePosY + nScrollTop + nFrameHeight - pxLength)		
		{
			if (oReturnPos.Y - pxLength > nFramePosY + nScrollTop)
			{
				oReturnPos.Y -= pxLength;
			}
		}
	}

	//fix the Y position so it will always points in a constant position under the the bottom border.
	oReturnPos.Y = oReturnPos.Y - (oReturnPos.Y % 14) + 20;

	//if (this.sMode == "min")
	//{
	//	oReturnPos.X = nRightBorder;
	//	oReturnPos.Y = 20;
	//}
	return oReturnPos;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.displaySuggestions = function(e,oDisplayElement,nSelectedIndex)
{
	var oCalcPosition = this.calcSuggestionsPosition(e,nSelectedIndex);
	oDisplayElement.style.left = oCalcPosition.X; 
	oDisplayElement.style.top = oCalcPosition.Y;

	oDisplayElement.style.display = "block";
	oDisplayElement.style.visibility = "visible";
		
	//alert("oDisplayElement.style: left=[" + oDisplayElement.style.left + "] top=[" + oDisplayElement.style.left + "]");
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showProgressDialog = function(bShow)
{
	var oElement = this.getProgressElement();
	if (oElement)
	{
		if (bShow)
		{
			this.hideSuggestions();
			this.hideDictionary();
		
			var oEditorViewport = this.getEditorViewport();
			var nDialogWidth = Math.max(190,parseInt(oEditorViewport.width/4));
			var nDialogHeight = Math.max(50,parseInt(oEditorViewport.height/8));
						
			if (this.isUsingIframe())
			{
				var oEditor = this.getEditorFrame();
				oElement.style.left = parseInt(parseInt(oEditorViewport.width)/2 - parseInt(nDialogWidth/2) + 
											   parseInt(oEditor.document.body.scrollLeft));
				oElement.style.top = parseInt(parseInt(oEditorViewport.height)/2 - parseInt(nDialogHeight/2) +
											  parseInt(oEditor.document.body.scrollTop));
			}
			else
			{
				var oEditorPos = this.getEditorPosition();
				oElement.style.left = parseInt(oEditorPos.X);
				oElement.style.top = parseInt(oEditorPos.Y);
			}
			//sContent = "<table bgcolor='#c3d9ff'><tr><td style='font-weight: bold; border: solid 1px navy;' align='center' width='270px' height='80px'>Processing data, please wait ...</td></tr></table>";
			//sContent = "<table style='background-color: #c3d9ff; border: groove 2px navy;'><tr><td style='font-weight: bold;' align='center' width='270px' height='80px'>Processing data, please wait ...</td></tr></table>";
			//sContent = "<table class='wsProgressBar'><tr><td class='wsProgressBar'>Processing data, please wait ...</td></tr></table>";
			sContent = "<table border='1px' bordercolordark='black' bordercolorlight='gray' bgcolor='#c3d9ff' CELLPADDING='0' CELLSPACING='0'><tr><td align='center' style='font-size: 60%; bold; font-weight: bold; width: " + nDialogWidth + "px; height: " + nDialogHeight + "px;'>Processing data, please wait ...</td></tr></table>";
			oElement.innerHTML = sContent;
			
			oElement.style.display = "block";
		}
		else
		{
			oElement.innerHTML = "";
			oElement.style.display = "none";
		}
		return true;
	}
	else
	{
		//alert("element not found: " + oElement);
		return false;
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.displayDictionary = function(oDisplayElement)
{
	oDisplayElement.className = "wsDictionaryOn";
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.setSuggestionsEventHandler = function(oDisplayElement,sId,oFunction)
{
	var oElements = oDisplayElement.getElementsByTagName("td");
	for (var i=0; i<oElements.length; ++i)
	{
		if (oElements[i].id == sId)
		{
			//alert("Found!, innerHTML = " + oElements[i].innerHTML);
			//this.addEvent(oElements[i], "click", oFunction); //function added by reference
			this.addEvent(oElements[i], "mousedown", oFunction); //function added by reference
			this.addEvent(oElements[i], "mouseover", onMouseOverSuggestion); //function added by reference
			this.addEvent(oElements[i], "mouseout", onMouseOutSuggestion); //function added by reference
		}
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getEnrichmentLangType = function(nLangType)
{
	var nEnrichLangType = 0 ;
	if (nLangType == this.stLangType.noun)
	{
		nEnrichLangType = this.stLangType.adjective;
	}
	else if (nLangType == this.stLangType.verb)
	{
		nEnrichLangType = this.stLangType.adverb;
	}
	return nEnrichLangType;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getSuggestionsHTML = function(vSuggestions,sId)
{
	var sContent = "";
	var size = vSuggestions.length;
	for (var i = 0; i<size && i<this.nMaxSuggestions; ++i)
	{
		sContent += '<tr>';
		//sContent += '<td style="cursor:default; color:black; padding: 0px 5px 0px 5px;" ';
		//sContent += sOnClick + ' ' + sOnMouseOver + ' ' + sOnMouseOut + '>';
		sContent += '<td id="' + sId + '" class="wsSuggestion">';
		sContent += vSuggestions[i];
		sContent += '</td>';
		sContent += '</tr>';
	}
	return sContent;
}
//--------------------------------------------------------------------------------------
//WSEnrichmentEngine.prototype.showSuggestions = function(vSuggestions,sHeader,sColor)
WSEnrichmentEngine.prototype.showSuggestions = function(e,vSuggestions,sHeader,sColor)
{
	var oDisplayElement = this.getSuggestionsElement();
	if (! oDisplayElement)
	{
		//alert("showSuggestions: no suggestions element");
		return false;
	}
	var sSuggestionId = "wsSuggestion";
	
	var sContent = '<table CELLPADDING=0 CELLSPACING=0>';
	sContent	+= '<tr valign="top">';
	sContent	+= '<td>';
	sContent	+= '<table wrap class="wsSuggestions">';
	sContent	+= '<tr>';
	if (vSuggestions.length > 0)
	{
		sContent	+= '<td id="wsReplace" valign="top" class="wsSuggestionsColumn">';
		sContent	+= '<table>';
		sContent	+= '<th align="left" class="wsSuggestionsHeader" style="color:' + sColor + ';">' + sHeader + '</th>';
		sContent 	+= this.getSuggestionsHTML(vSuggestions,sSuggestionId);
		sContent	+= '</table>';
		sContent	+= '</td>';
	}
	sContent	+= '</tr>';
	sContent	+= '</table>';
	sContent	+= '</td>';
	sContent	+= '<td width="200px" class="wsDictionaryOff" id="' + this.stResultElementId.dictionary + '" name="' + this.stResultElementId.dictionary + '">';
	sContent	+= '</td>';
	sContent	+= '</tr>';
	sContent	+= '</table>';

  	oDisplayElement.innerHTML = sContent;
  	this.setSuggestionsEventHandler(oDisplayElement,sSuggestionId,onReplaceWord);
  	this.displaySuggestions(e,oDisplayElement,this.nSelectedIndex);
  	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.show2Suggestions = function(e,vESuggestions,vTSuggestions)
{
	//var nMaxSuggestions = max(vESuggestions.length,vTSuggestions.length);
	if (vESuggestions.length == 0 && vTSuggestions.length == 0)
	{
		return false;
	}
	var oDisplayElement = this.getSuggestionsElement();
	if (! oDisplayElement)
	{
		//alert("show2Suggestions: no suggestions element");
		return false;
	}
	//var nLangType = this.vEnrichment[this.nSelectedIndex].nLangType;
	//var nEnrichLangType = this.getEnrichmentLangType(nLangType);
	
	var sEnrichmentId = "wsEnrichment";
	var sThesaurusId = "wsThesaurus";
	
	var sContent = '<table CELLPADDING=0 CELLSPACING=0>';
	sContent	 += '<tr valign="top">';
	sContent	 += '<td>';
	sContent	 += '<table wrap class="wsSuggestions">';
	sContent	 += '<tr>';
	if (vESuggestions.length > 0)
	{
		sContent	+= '<td id="wsAdd" valign="top" class="wsSuggestionsColumn">';
		sContent	+= '<table>';
		sContent	+= '<th align="left" class="wsSuggestionsHeader" style="color:blue;">Enrichments</th>';
		//sContent 	+= this.getSuggestionsHTML(vESuggestions,"onAddWord",nEnrichLangType);
		sContent 	+= this.getSuggestionsHTML(vESuggestions,sEnrichmentId);
		sContent	+= '</table></td>';
	}
	if (vTSuggestions.length > 0)
	{
		sContent	+= '<td id="wsReplace" valign="top" class="wsSuggestionsColumn">';
		sContent	+= '<table>';
		sContent	+= '<th align="left" class="wsSuggestionsHeader" style="color:blue;">Thesaurus</th>';
		sContent 	+= this.getSuggestionsHTML(vTSuggestions,sThesaurusId);
		sContent	+= '</table></td>';
	}
	sContent += '</tr>';
	sContent += '</table>';
	sContent += '</td>';
	// set column for the dictionary information
	sContent += '<td width="200px" class="wsDictionaryOff" id="' + this.stResultElementId.dictionary + '" name="' + this.stResultElementId.dictionary + '">';
	sContent += '</td>';
	sContent += '</tr>';
	sContent += '</table>';

	oDisplayElement.innerHTML = sContent;
	this.setSuggestionsEventHandler(oDisplayElement,sEnrichmentId,onAddWord);
	this.setSuggestionsEventHandler(oDisplayElement,sThesaurusId,onReplaceWord);
	this.displaySuggestions(e,oDisplayElement,this.nSelectedIndex);
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showTrialMessage = function(e)
{
	alert("This version available to the full version only.\nTo register go to -> https://buy.whitesmoke.com/buynew/");
	return true;	
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showLeftButtonMenu = function(e)
{
	var nIndex = getEventIndex(e);
	if (nIndex < 0)
	{
		var oLBMenu = window.document.getElementById("wsLeftButtonMenu");
		if (oLBMenu)
		{
			var oPos = this.getMousePos(e);
			oLBMenu.style.left = oPos.X;
			oLBMenu.style.top = oPos.Y;
			oLBMenu.style.display = "block";
			oLBMenu.style.visibility = "visible";
			return true;
		}
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.hideLeftButtonMenu = function()
{
	var oLBMenu = window.document.getElementById("wsLeftButtonMenu");
	if (oLBMenu)
	{
		oLBMenu.style.display = "none";
		oLBMenu.style.visibility = "hidden";
		return true;
	}
	return false;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showWSLegendMessage = function()
{
	var oDisplayElement = this.getDictionaryElement();
	if (! oDisplayElement)
	{
		return false;
	}

	var sContent = "";
	sContent += '<table class="wsLegend">';
	sContent += '<tr>';
	sContent += '<td valign="top">';
	sContent += '<span style="font-weight:bold; color:black">WhiteSmoke Legend: </span>\n';
	sContent += '<span style="font-weight:bold; color:' + this.stColor.spelling + '">Spelling</span>\n';
	sContent += '<span style="font-weight:bold; color:' + this.stColor.grammar + '">Grammar</span>\n';
	sContent += '<span style="font-weight:bold; color:' + this.stColor.enrichment + '">Enrichment</span>\n';
	sContent += '</td></tr></table>';

	oDisplayElement.innerHTML = sContent;
	this.displayDictionary(oDisplayElement);
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showDictionaryResult = function()
{
	//alert("showDictionaryResult");
	var bSuggestionsPresent = true;
	
	var oDisplayElement = this.getDictionaryElement();
	if (! oDisplayElement)
	{
		bSuggestionsPresent = false;
		oDisplayElement = this.getSuggestionsElement();
		if (! oDisplayElement)
		{
			return false;
		}
		
	}

	var vSuggestions = this.stLastPointedInfo.vDictionary;
	//alert("vSuggestions.length = " + vSuggestions.length);
	if (vSuggestions.length <= 0)
	{
		//this.hideDictionary();
		//return;
	}
	//var sWord = (this.stLastPointedInfo.oElement ? this.stLastPointedInfo.oElement.innerHTML : "");
	var sWord = this.stLastPointedInfo.sWord;

	var sContent = "";
	//sContent += '<table bgcolor="#c3d9ff" style="border: 2px solid; border-color: #e8f1ff #9daecd #9daecd #e8f1ff;">';
	//sContent += '<table bgcolor="#e8f1ff" style="border: 1px solid black;">';
	sContent += '<table class="wsDictionary">';
	//sContent += '<tr style="Height: 1px"><td align="right" valign="top" >close</td></tr>';
	//sContent += '<tr style="Height: 1px"><td style="color: red" align="right" valign="top">close</td></tr>';
	sContent += '<tr>';
	sContent += '<td valign="top">';
	sContent += '<table>';
	sContent += '<th align="left" class="wsDictionaryHeader">Dictionary</th>';
	sContent += '<tr><td align="left" class="wsDictionaryValue">' + sWord + ':</td></tr>';
	if (vSuggestions.length <= 0)
	{
		sContent += '<tr><td style="font-style:italic;" class="wsDictionaryValue">no suggestions</td></tr>';
	}
	for (var i = 0; i < vSuggestions.length; ++i)
	{
		sContent += '<tr>';
		sContent += '<td class="wsDictionarySuggestion">';
		sContent += vSuggestions[i];
		sContent += '</td>';
		sContent += '</tr>';
	}
	sContent += '</table></td></tr></table>';

	oDisplayElement.innerHTML = sContent;
	var e = this.stLastPointedInfo.e;
	if (bSuggestionsPresent == false)
	{
		this.displaySuggestions(e,oDisplayElement,this.stLastPointedInfo.nIndex);
	}
	else
	{
		this.displayDictionary(oDisplayElement);
	}
	return true;	
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showLookupResult = function()
{
	var vSuggestions = this.stLastPointedInfo.vDictionary;
	var sWord = this.stLastPointedInfo.sWord;

	var sContent = "";
	sContent += '<html><head></head>';
	sContent += '<body onMouseUp="javascript:' + this.sParent + '.onHideSuggestions();" width="100%" bgcolor="#e8f1ff" marginwidth=4 marginheight=4 topmargin=4 leftmargin=4>';
	sContent += '<table bgcolor="#e8f1ff" width="100%">';
	sContent += '<tr>';
	sContent += '<td valign="top">';
	sContent += '<table style="font-size: 80%">';
	sContent += '<th align="left" style="color:blue;font-size: 110%; font-weight: bold;">' + sWord + ':</th>';
	if (vSuggestions.length <= 0)
	{
		sContent += '<tr><td style="font-style:italic;">no suggestions</td></tr>';
	}
	for (var i = 0; i < vSuggestions.length; ++i)
	{
		sContent += '<tr>';
		sContent += '<td style="cursor:default; color:black; padding: 0px 5px 0px 5px;">';
		sContent += vSuggestions[i];
		sContent += '</td>';
		sContent += '</tr>';
	}
	sContent += '</table></td></tr></table>';
	sContent += '</body></html>';

	this.setEditorContent(sContent);
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showTemplates = function(vTemplates)
{
	this.vTemplatesMenu = vTemplates;

	var sContent = "";
	sContent += '<html><head>';
	sContent += '<frameset cols="29%, 71%" frameborder="yes" border="1" framespacing="1" frameborder="yes">';
	sContent += '<frame id="menu" name="menu" src="template_menu.html" scrolling="auto" frameborder="no">';
	sContent += '<frame id="content" name="content" scrolling="auto" frameborder="no">';
	sContent += '</frameset>';
	sContent += '</head>';
	sContent += '<body width="100%" bgcolor="#e8f1ff" marginwidth=4 marginheight=4 topmargin=4 leftmargin=4>';
	sContent += '</body></html>';

	this.setEditorContent(sContent);
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.markWord = function(nNewIndex)
{
	//var oEditorFrame = this.getEditorFrame();

	// First we need to remove the mark from the previous word.
	var nPrevIndex = this.nSelectedIndex;
	//alert("nNewIndex = " + nNewIndex + ", nPrevIndex = " + nPrevIndex);
	if (nPrevIndex >= 0)
	{
		//var sPrevId = "wsWord" + nPrevIndex.toString();
		//var oPrevElement = oEditorFrame.document.getElementById(sPrevId);
		var oPrevElement = this.getElementByIndex(nPrevIndex);
		if (oPrevElement && oPrevElement.style)
		{
			if (this.stPrevElementStyle != null)
			{
				//oPrevElement.className = this.stPrevElementStyle.className;
				oPrevElement.style.textDecoration = this.stPrevElementStyle.textDecoration; //"none"
				oPrevElement.style.borderBottomStyle = this.stPrevElementStyle.borderBottomStyle;
			}
		}
	}
	// Now, we need to mark the current word
	if (nNewIndex >= 0)
	{
		//var sId = "wsWord" + nNewIndex.toString();
		//var oElement = oEditorFrame.document.getElementById(sId);
		var oElement = this.getElementByIndex(nNewIndex);
		if (oElement && oElement.style)
		{
			//this.stPrevElementStyle.className = oElement.className;
			this.stPrevElementStyle.textDecoration = oElement.style.textDecoration;
			this.stPrevElementStyle.borderBottomStyle = oElement.style.borderBottomStyle;

			oElement.style.borderBottomStyle = "solid";
			//oElement.style.borderBottomWidth = "medium";
			//oElement.style.fontWeight = "bold";
			//oElement.style.fontSize = "85%";
		}
		else
		{
			//alert("cant find element with id=" + sId);
		}
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showSpellingSuggestions = function(e,nIndex)
{
	this.markWord(nIndex);
	this.state = "s";
	this.nSelectedIndex = nIndex;
	var enrichment = this.vEnrichment[this.nSelectedIndex];
	//if (enrichment && enrichment.s)
	{
		return this.showSuggestions(e,enrichment.s,"Spelling",this.stColor.spelling);
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showGrammarSuggestions = function(e,nIndex)
{
   if (this.bTrial == true)
   {
      this.showTrialMessage();
   }
   else
   {
      this.markWord(nIndex);
      this.state = "g";
      this.nSelectedIndex = nIndex;
      var enrichment = this.vEnrichment[this.nSelectedIndex];
      //if (enrichment && enrichment.g)
      {
	 return this.showSuggestions(e,enrichment.g,"Grammar","green");
      }
   }	
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showEnrichmentSuggestions = function(e,nIndex)
{
	this.markWord(nIndex);
	this.state = "e";
	this.nSelectedIndex = nIndex;
	var enrichment = this.vEnrichment[this.nSelectedIndex];
	//if (enrichment && (enrichment.e || enrichment.t))
	{
		return this.show2Suggestions(e,enrichment.e,enrichment.t);
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.showFirstSuggestions = function()
{
	/*
	if (this.vEnrichment.length > 0)
	{
		if (this.nSelectedIndex < 0)
		{
			this.nSelectedIndex = 0;
		}
		var vEnrichment = this.vEnrichment[this.nSelectedIndex];
		if (! vEnrichment)
		{
			return false;
		}
		if (vEnrichment.s && vEnrichment.s.length > 0)
		{
			this.showSpellingSuggestions(this.nSelectedIndex);
		}
		else if (vEnrichment.g && vEnrichment.g.length > 0)
		{
			this.showGrammarSuggestions(this.nSelectedIndex);
		}
		else if ((vEnrichment.e && vEnrichment.e.length > 0) ||
					(vEnrichment.t && vEnrichment.t.length > 0))
		{
			this.showEnrichmentSuggestions(this.nSelectedIndex);
		}
	}
	return true;
	*/
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.addUndoInfo = function(sFuncName,vArgs)
{
	//alert("addundoinfo : sFuncName=["  + sFuncName + "], vArgs[0]=[" + vArgs[0] + "]");
	this.undoInfo[this.undoInfo.length] = {nIndex: this.nSelectedIndex, sFuncName: sFuncName, vArgs: vArgs};
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getFilteredEditorContent = function()
{
	this.removeEventHandler();
	this.removeWSElements();
	var sContent = this.wsEditor.getEditorContent();
	if (! this.isHTMLMode())
	{
		sContent = html2Text(sContent);
	}
	var sNewContent = this.cleanWSContent(sContent);
	if (sNewContent.substr(sNewContent.length-6,6) == "&nbsp;")
	{
		sNewContent = sNewContent.substr(0,sNewContent.length-6);
	}
	return sNewContent;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getFilteredTemplateContent = function()
{
	var sContent = "";
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		var oContentFrame = oEditorFrame.frames['content'];
		sContent = oContentFrame.document.body.innerHTML;
	}	

	return sContent;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.updateContent = function(sProfileCode)
{
	if (sProfileCode && sProfileCode.length>0)
	{
		this.sProfile = sProfileCode;
	}
	var sNewContent = this.getFilteredEditorContent();
	this.setEditorContent(sNewContent);
	this.querySentence(sNewContent);
	return sNewContent;
	//this.reTokenize(sNewContent);
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.replaceElementText = function(oElement,oldText, newText)
{
	var sContent = oElement.innerHTML;
	var sNewContent = sContent.replace(oldText,newText);
	oElement.innerHTML = sNewContent;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.replaceWord = function(newWord,nLangType)
{
	this.hideSuggestions();

	var enrichment = this.vEnrichment[this.nSelectedIndex];
	var oldWord = enrichment.sWord;
	enrichment.sWord = newWord;
	
	var oElement = this.getElementByIndex(this.nSelectedIndex);
	this.replaceElementText(oElement,oldWord,newWord);

	var bIsThesaurus = false;
	for (var i=0; i< enrichment.t.length; ++i)
	{
		if (enrichment.t[i] == newWord)
		{
			enrichment.t[i] = oldWord;
			bIsThesaurus = true;
		}
	}
	//alert("bIsThesaurus = " +  bIsThesaurus + ", newWord = " + newWord + ", nLangType = " +  nLangType);
	
	this.addUndoInfo("replaceWord",["\"" + oldWord + "\"",nLangType]);
	if (! bIsThesaurus)
	{
		this.queryWord(newWord,nLangType);
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.addWord = function(newWord,nLangType)
{
	this.hideSuggestions();

	var sId = "wsWordAdd" + this.nSelectedIndex;
	var oElement = this.getElementById(sId);
	oElement.innerHTML += newWord + " ";
	
	this.addUndoInfo("deleteWord",["\"" + sId + "\"", "\"" +  newWord + "\"", nLangType]);
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.deleteWord = function(sElementId, sWord, nLangType)
{
	this.hideSuggestions();

	var oElement = this.getElementById(sElementId);
	this.replaceElementText(oElement,sWord,"");
	//oElement.innerHTML = "";
	
	this.addUndoInfo("addWord",["\"" + sWord + "\"",nLangType]);
}
//--------------------------------------------------------------------------------------
/*
WSEnrichmentEngine.prototype.adjustOffsets = function(nPos,nDelta)
{
	for (var j=0; j<this.vAddedWords.length; ++j)
	{
		if (this.vAddedWords[j].nPos > nPos)
		{
			this.vAddedWords[j].nPos += nDelta;
		}
	}

	for (var i=0; i<this.vEnrichment.length; ++i)
	{
		if (this.vEnrichment[i].nPos >= nPos)
		{
			this.vEnrichment[i].nPos += nDelta;
		}
	}
}
*/
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.sendToServer = function(sCallBack,sQueryType,nLangType,sMessage)
{
	//return this.sendToServerAJAX(sCallBack,sQueryType,nLangType,sMessage);
	return this.sendToServerAJAXExtended(sCallBack,sQueryType,nLangType,sMessage);
}
//--------------------------------------------------------------------------------------
/*
WSEnrichmentEngine.prototype.sendToServerIFRAME = function(sCallBack,sQueryType,nLangType,sMessage)
{
	if (sMessage.length <= this .nMaxLength)
	{
		sMessage = sMessage.replace(/\"/g,"&quot;");
	
		var oRSFrame = createIFrame(this.sRSFrameId);
		if (oRSFrame != null)
		{
			alert("this.sServerURL = " + this.sServerURL);
			
			var sContent = '<html><head></head><body>';
			//sContent += '<form accept="text/html" enctype="text/html" action="server.php" method="post" target="_self">';
			sContent += '<form name="wsForm" method="POST" target="" action="' + this.sServerURL + '">';
			sContent += '<input type="hidden" name="user_id" value="' + this.sUserID + '">';
			sContent += '<input type="hidden" name="callback" value="' + this.sParent + "." + sCallBack + '">';
			sContent += '<input type="hidden" name="query_type" value="' + sQueryType + '">';
			var sProfile = this.getSelectedProfile();
			sContent += '<input type="hidden" name="profile" value="' + sProfile + '">';
			sContent += '<input type="hidden" name="lang_type" value="' + nLangType + '">';
			sContent += '<input type="hidden" name="message" value="' + sMessage + '">'; //encodeURI(sMessage)
			sContent += '</form>';
			sContent += '</body></html>';
			
			//alert("sContent = " + sContent);
			
			oRSFrame.document.open();
			oRSFrame.document.write(sContent);
			oRSFrame.document.close();
			oRSFrame.document.forms['wsForm'].submit();
		}
	}
	else
	{
		alert("WhiteSmoke doesn't allow to enrich more then " + this.nMaxLength + " characters.");
		//this.initContent();
		//alert("Your Message is:\n" + sMessage);
	}
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.sendToServerAJAX = function(sCallBack,sQueryType,nLangType,sMessage)
{
	if (sMessage.length <= this.nMaxLength)
	{
		sMessage = sMessage.replace(/\"/g,"&quot;");

		var xmlhttp = new XMLHttpRequest();
		
		xmlhttp.onerror = function(description)	{
			alert('Error: ' + description);
		}
		
		xmlhttp.onreadystatechange = function()	{
			if(xmlhttp.readyState == 4)
			{
				//var sMsg = "";
				//sMsg += 'These are the response headers:\r\n\r\n' + xmlhttp.getAllResponseHeaders();
				//sMsg += 'These is the response:\r\n\r\n' + xmlhttp.responseText;
				//alert(sMsg);
				eval(xmlhttp.responseText);
			}
		}

		var sProfile = this.getSelectedProfile();		
		var sURL = this.sServerURL + "?user_id=" + this.sUserID + "&callback=" + sCallBack + "&query_type=" + sQueryType + "&profile=" + sProfile + "&lang_type=" + nLangType + "&message=" + encodeURI(sMessage);
		//var sURL = this.sServerURL + "?user_id=0&callback=" + this.sParent + "." + sCallBack + "&query_type=sentence&profile=" + sProfile + "&lang_type=" + nLangType + "&message=test";
	
		xmlhttp.open('GET', sURL);
		xmlhttp.send(null);
		//alert(Sarissa.serialize(xmlhttp.responseXML));
	}
	else
	{
		alert("WhiteSmoke doesn't allow to enrich more then " + this.nMaxLength + " characters.");
		return false;
	}
	return true;
}
*/
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.sendToServerAJAXExtended = function(sCallBack,sQueryType,nLangType,sMessage)
{
	if (sMessage.length <= this.nMaxLength)
	{
		//sMessage = sMessage.replace(/\"/g,"&quot;");

		var xmlhttp = new XMLHTTP(this.sAJAXExURL);
		
		xmlhttp.onerror = function(description)	{
			this.onHandleError(description);
			//alert('Error: ' + description);
		}
		
		xmlhttp.onreadystatechange = function()	{
			if (xmlhttp.readyState == 4)
			{
				//var sMsg = "";
				//sMsg += 'These are the response headers:\r\n\r\n' + xmlhttp.getAllResponseHeaders();
				//sMsg += 'Total length of data received: ' + xmlhttp.responseText.length + '\r\n';
				//sMsg += 'These is the response:\r\n\r\n' + xmlhttp.responseText;
				//var sFixedResponse = xmlhttp.responseText.replace(/\r\n|\n/g,"\\n");
				//alert("sFixedResponse = " + sFixedResponse);
				//alert("xmlhttp.responseText = " + xmlhttp.responseText);
				var nStart = xmlhttp.responseText.indexOf("<script");
				if (nStart >= 0)
				{
					//alert(xmlhttp.responseText);
					nStart = xmlhttp.responseText.indexOf(">",nStart) + 1;
					var nFinish = xmlhttp.responseText.indexOf("</script>",nStart);
					if (nFinish >= 0)
					{
						var sCode = xmlhttp.responseText.substr(nStart,nFinish-nStart);
						//alert(sCode);
						eval(sCode);
					}
				}	
				
				/*
				if (xmlhttp.responseXML.hasChildNodes())
				{
					var oNodes = xmlhttp.responseXML.childNodes;
					if (oNodes.length > 0)
					{
						if (oNodes[0].getElementsByTagName)
						{
						 	var oScripts = oNodes[0].getElementsByTagName("script");
						 	if (oScripts.length > 0)
						 	{
							 	for (var i=0; i<oScripts.length; ++i)
							 	{
							 		var sCode = oScripts[i].textContent;
							 		eval(sCode);
								}
								return true;
							}
						}
						var oWSEE = g_stWSEEngine.enrichment;
						if (oWSEE)
						{
							oWSEE.showProgressDialog(false);
						}	
					}
				}
				*/
			}
		}
		var sProfile = this.getSelectedProfile();
		var sURL = this.sServerURL + "?user_id=" + this.sUserID + "&callback=" + sCallBack + "&error_callback=onHandleError&query_type=" + sQueryType + "&profile=" + sProfile + "&lang_type=" + nLangType + "&message=" + encodeURI(sMessage);
		//var sURL = this.sServerURL + "?user_id=0&callback=" + sCallBack + "&query_type=sentence&profile=" + sProfile + "&lang_type=" + nLangType + "&message=test";
	
		xmlhttp.open('POST', sURL); //encodeURI(sURL)
		xmlhttp.send(null);
	}
	else
	{
		alert("WhiteSmoke doesn't allow to enrich more then " + this.nMaxLength + " characters.");
		return false;
	}
	return true;
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.querySentence = function(sSentence)
{
	//alert("on querySentence, sentence = [\n" + sSentence + "\n]");
	//sSentence = sSentence.replace(/\"/g,"&quot;");
	this.sContent = sSentence;	

	var vTokens = this.tokenize(sSentence);

	var sMessage = "";
	for (var i=0; i < vTokens.length; ++i)
	{
		//sMessage += encodeURI(vTokens[i].sWord) + " ";
		sMessage += vTokens[i].sWord + " ";
	}
	this.showProgressDialog(true);
	return this.sendToServer("handleQuerySentenceResponse","sentence",0,sMessage);
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.queryWord = function(sWord, nLangType)
{
	this.showProgressDialog(true);
	return this.sendToServer("handleQueryWordResponse","word",nLangType,sWord);
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.queryDictionary = function(sWord, nLangType)
{
	if (this.getDictionaryInfo(sWord,nLangType) != null)
	{
		this.showDictionaryResult();
		return true;
	}
	else
	{
		return this.sendToServer("handleQueryDictionaryResponse","dictionary",nLangType,sWord);
	}
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.lookupDictionary = function(sWord, nLangType)
{
	this.setLastPointedInfo(null,null,sWord,nLangType,0,null);
	if (this.getDictionaryInfo(sWord,nLangType) != null)
	{
		this.showLookupResult();
		return true;
	}
	else
	{
		return this.sendToServer("handleLookupDictionaryResponse","dictionary",nLangType,sWord);
	}
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.queryTemplateList = function()
{
	return this.sendToServer("handleQueryTemplateListResponse","templateList",0,"");
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.displayTemplateContent = function(sResponseData)
{
	var oEditorFrame = this.getEditorFrame();
	if (oEditorFrame)
	{
		var oContentFrame = oEditorFrame.frames['content'];
	
		oContentFrame.document.open();
		oContentFrame.document.write(sResponseData);
		oContentFrame.document.close();
		return true;
	}
	return false;
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.queryTemplate = function(sName)
{
	return this.sendToServer("handleQueryTemplateResponse","template",0,sName);
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.undo = function()
{
	if (this.undoInfo.length > 0)
	{
		var stUndo = this.undoInfo.pop(); //[this.undoInfo.length-1];
		if (stUndo)
		{
			var sUndoCode = "this." + stUndo.sFuncName + "(";
			for (var i=0; i < stUndo.vArgs.length; ++i)
			{
				if (i >= 1)
				{
					sUndoCode += ",";
				}
				sUndoCode += stUndo.vArgs[i];
			}
			sUndoCode += ");";
			this.nSelectedIndex = stUndo.nIndex;
			//alert("undo string = [" + sUndoCode + "]");
			eval(sUndoCode);
			this.redoInfo[this.redoInfo.length] = this.undoInfo.pop(); //to remove the undo information from the eval operation.
		}
	}
}
//--------------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.redo = function()
{
	if (this.redoInfo.length > 0)
	{
		var stRedo = this.redoInfo.pop(); //[this.redoInfo.length-1];
		if (stRedo)
		{
			var sRedoCode = "this." + stRedo.sFuncName + "(";
			for (var i=0; i < stRedo.vArgs.length; ++i)
			{
				if (i >= 1)
				{
					sRedoCode += ",";
				}
				sRedoCode += stRedo.vArgs[i];
			}
			sRedoCode += ");";
			this.nSelectedIndex = stRedo.nIndex;
			eval(sRedoCode);
		}
	}
}
//--------------------------------------------------------------------------------------
WSEnrichmentEngine.prototype.getURL = function(sURL)
{
	var sIFrameId = this.sRSFrameId;
	var wsIFrameObj = getFrameObj(sIFrameId);
	if (wsIFrameObj != null)
	{
		var IFrameDoc;
		if (wsIFrameObj.contentDocument) // For NS6
		{
			IFrameDoc = wsIFrameObj.contentDocument;
		}
		else if (wsIFrameObj.contentWindow) // For IE5.5 and IE6
		{
			IFrameDoc = wsIFrameObj.contentWindow.document;
		}
		else if (wsIFrameObj.document) // For IE5
		{
			IFrameDoc = wsIFrameObj.document;
		}
		else
		{
			return false;
		}
		IFrameDoc.location.replace(sURL);
		return true;
	}
	else
	{
		//alert("WhiteSmoke could not find the iframe");
	}

	return false;
}
//======================================================================================
// Global Functions Definition:
//======================================================================================
var g_stWSEEngine = {enrichment: null, lookup: null, template: null};
//--------------------------------------------------------------------------------------
function WSEnrichmentObject(sUserID,sClient)
{
	this.sUserID = sUserID;
	this.sClient = sClient;
	if (sClient == "shvoong")
	{
		this.sUserID = "762505472";
	}
	else if (sClient == "articlesbase")
	{
		this.sUserID = "188439296";
	}
}
//----------------------------------------------------------------------------
WSEnrichmentObject.prototype = {
	initEx : function(oWSEditor,sContent,sMode,sFormat,sCurrentProfile,bTrial)
	{ 
		if (!sContent)
		{
			sContent = new String();
		}
		var oWSEE = new WSEnrichmentEngine(this.sUserID,oWSEditor,sContent,sMode,sFormat,sCurrentProfile,bTrial);
		g_stWSEEngine.enrichment = oWSEE;
		if (oWSEE)
		{
			oWSEE.removeWSElements();
			//oWSEE.showWSLegendMessage();
			//alert("before oWSEE.setEditorCSS()");
			oWSEE.setEditorCSS();
			oWSEE.createWSElements();
			var sContent = oWSEE.cleanWSContent(sContent);
			//alert("sContent = " + sContent);
			if (sContent.length > 0 && sContent.length < 15000)
			{
				var bResult = oWSEE.querySentence(sContent);
				//oWSEE.showFirstSuggestions();
				return bResult;
			}
			return true;
		}
		else
		{
			return false;
		}
	},
	//----------------------------------------------------------------------------
	init : function(oWSEditor,sContent)
	{
		this.initEx(oWSEditor,sContent,null,null,null,null,null);
	},
	//----------------------------------------------------------------------------	
	initLookupDictionary : function(oWSEditor,sWord,sMode,sFormat)
	{
		if (sWord && sWord.length > 0 && sWord.length < 100)
		{
			var oWSEE = new WSEnrichmentEngine(this.sUserID,oWSEditor,sWord,sMode,sFormat);
			g_stWSEEngine.lookup = oWSEE;
			if (oWSEE)
			{
				oWSEE.tokenize(sWord);
				oWSEE.lookupDictionary(sWord,0);
			}
			return true;
		}
		else
		{
			//alert("There is no word to lookup");
		}
		return false;
	},
	//----------------------------------------------------------------------------
	initTemplates : function(oWSEditor,sMode,sFormat)
	{
		g_stWSEEngine.template = new WSEnrichmentEngine(this.sUserID,oWSEditor,"",sMode,sFormat);
		if (g_stWSEEngine.template)
		{
			g_stWSEEngine.template.queryTemplateList();
			return true;
		}
		return false;
	},
	//----------------------------------------------------------------------------
	updateContent : function(sProfile)
	{
		var sContent = "";
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{	
			sContent = oWSEE.updateContent(sProfile);
		}
		return sContent;
	},
	//----------------------------------------------------------------------------
	refresh : function()
	{
		return this.updateContent();
	},
	//----------------------------------------------------------------------------
	getFilteredEditorContent : function()
	{
		var sContent = "";
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			sContent = oWSEE.getFilteredEditorContent();
		}
		return sContent;
	},
	//----------------------------------------------------------------------------
	getFilteredTemplateContent : function()
	{
		var sContent = "";
		
		var oWSEE = g_stWSEEngine.template;
		if (oWSEE)
		{
			sContent = oWSEE.getFilteredTemplateContent();
			if (sContent && ! oWSEE.isHTMLMode())
			{
				sContent = html2Text(sContent);
			}
		}
		return sContent;
	},
	//----------------------------------------------------------------------------
	finishTemplateMode : function()
	{
		onHideSuggestions();
		onStopDictionaryTimer();
		
		var sContent = "";
		
		var oWSEE = g_stWSEEngine.template;
		if (oWSEE)
		{
			sContent = oWSEE.getFilteredTemplateContent();
			/*
			if (sContent && ! oWSEE.isHTMLMode())
			{
				sContent = html2Text(sContent);
			}
			*/
			oWSEE.free();
		}
		g_stWSEEngine.template = null;
		return sContent;
	},
	//----------------------------------------------------------------------------
	addSignatureDOM : function()
	{
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			var oEditorFrame = oWSEE.getEditorFrame();
			if (oEditorFrame && oEditorFrame.document && oEditorFrame.document.body)
			{
				var sTagID = "wsSignature";
				var	oElement = oEditorFrame.document.createElement('SPAN');
				oElement.setAttribute('id',sTagID);
				oElement.setAttribute('name',sTagID);
				oElement.innerHTML =  "<br><br>This abstract was checked by WhiteSmoke Solution. ";
				oElement.innerHTML += "<a rel='nofollow' href='http://www.tkqlhce.com/click-2081611-10423655?SID=Abstract' target='_blank'>Learn More.</a>";
				oElement.innerHTML += "<img src='http://www.awltovhc.com/image-2081611-10423655' width='1' height='1' border='0'/>";innerHTML += "<img src='http://www.awltovhc.com/image-2081611-10423655' width='1' height='1' border='0'/>";
				var oElement = oEditorFrame.document.body.appendChild(oElement);
				/*
				var	oScript = oEditorFrame.document.createElement('SCRIPT');
				var src = 'http://www.tkqlhce.com/placeholder-506690?target=_blank&text=%3Clink%3ELearn+More%3C%2Flink%3E&mouseover=N';
				oScript.setAttribute('type','text/javascript');
				oScript.setAttribute('src',src);				
			    if (navigator.userAgent.indexOf('Safari'))
			    {
			    	oScript.setAttribute('charset','utf-8'); // Safari bug
			    }
			    oScript = oEditorFrame.document.getElementsByTagName('head')[0].appendChild(oScript)
				//oScript = oChild.appendChild(oScript);
				*/
				return true;
			}
		}
		return false;	
	},
	//----------------------------------------------------------------------------
	addSignature : function()
	{
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			var sContent = oWSEE.getEditorContent();
			var sSignature = "";
			
			var pos = sContent.indexOf("</html>");
			if (pos <= 0)
			{
				pos = sContent.indexOf("</HTML>");
				if (pos <= 0)
				{
					pos = sContent.length;
				}
			}
			if (this.sClient == "shvoong")
			{
				sSignature  = "<span id='wsSignature' name='wsSignature'>";  
				sSignature += "<br><br>This abstract was checked by WhiteSmoke Solution. ";
				sSignature += "<a rel='nofollow' href='http://www.tkqlhce.com/click-2081611-10423655?SID=Abstract' target='_blank'>Learn More.</a>";
				sSignature += "<img src='http://www.awltovhc.com/image-2081611-10423655' width='1' height='1' border='0'/>";
				sSignature += "</span>";
			}
			if (sSignature.length > 0)
			{
				var sNewContent = sContent.substr(0,pos);
				sNewContent += sSignature;
				sNewContent += sContent.substr(pos);
				oWSEE.setEditorContent(sNewContent);
			}
			return true;
		}
		return false;
	},
	//----------------------------------------------------------------------------
	finishEnrichmentMode : function(sSignature)
	{
		onHideSuggestions();
		onStopDictionaryTimer();
		
		this.addSignature();
		
		var sContent = new String();
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			sContent = oWSEE.getFilteredEditorContent();
			oWSEE.setEditorContent(sContent);
		}
		return sContent;
	},
	//----------------------------------------------------------------------------
	cancelEnrichmentMode : function()
	{
		onHideSuggestions();
		onStopDictionaryTimer();
		
		var sOriginalContent = new String();
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			sOriginalContent = oWSEE.sOriginalContent;
			oWSEE.setEditorContent(sOriginalContent);
		}
		return sOriginalContent;
	},
	//----------------------------------------------------------------------------
	undo : function()
	{
		onHideSuggestions();
		onStopDictionaryTimer();
		
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			oWSEE.undo();
			return true;
		}
		return false;
	},
	//----------------------------------------------------------------------------
	redo : function()
	{
		onHideSuggestions();
		onStopDictionaryTimer();
		
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			oWSEE.redo();
			return true;
		}
		return false;
	}
	//----------------------------------------------------------------------------
};	
//==========================================================================================================
//--------------------------------------------------------------------------------------
/*
function mouseDown(e)
{
	if (parseInt(navigator.appVersion)>3)
	{
		var clickType=1;
		if (navigator.appName=="Netscape")
		{
			clickType=e.which;
		}	
		else
		{
			clickType=event.button;
		}
		if (clickType==1)
		{
			self.status='Left button!';
		}	
		if (clickType!=1)
		{
			self.status='Right button!';
		}	
	}
	return true;
}
*/
//--------------------------------------------------------------------------------------
function onSetWSEventHandler()
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.setEventHandler();
	}	
}
//--------------------------------------------------------------------------------------
function getElementIndex(oElement)
{
	nIndex = -1;
	if (oElement)
	{
		//alert("oElement = " + oElement + ", oElement.id = " + oElement.id + ", oElement.tagName = " + oElement.tagName);
		var sId = oElement.id;
		if (sId)
		{
			var nStart = sId.indexOf("wsWord");
			if (nStart < 0)
			{
				return nStart;
			}
			nStart += String("wsWord").length;
			var sIndex = sId.substr(nStart);
			nIndex = parseInt(sIndex);
			//alert("getElementIndex = " + nIndex);
		}	
	}
	return nIndex;
}
//--------------------------------------------------------------------------------------
function getEventIndex(e)
{
	var oElement = getEventElement(e);
	if (oElement)
	{
		return getElementIndex(oElement);
	}
	return -1;
}
//--------------------------------------------------------------------------------------
function getEventElement(e)
{
	var targ = null;
	if (! e)
	{
		e = window.event;
		if (!e)
		{
			return null;
		}
	}
	if (e.target)
	{
		targ = e.target;
	}
	else if (e.srcElement)
	{
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) // defeat Safari bug
	{
		targ = targ.offsetParent;
	}
	return targ;	
}
//--------------------------------------------------------------------------------------
function isRightClick(e)
{
	if (!e) e = window.event;
	var bRightClick = false;
	if (e.which)
	{
		bRightClick = (e.which==2 || e.which==3);
	}
	else if (e.button)
	{
		bRightClick = (e.button == 2)
	}
	//alert("bRightClick = " + bRightClick);
	return (bRightClick);
	//return false;
}
//--------------------------------------------------------------------------------------
function onGetTemplatesMenu()
{
	var oWSEE = g_stWSEEngine.template;
	if (oWSEE)
	{
		return oWSEE.vTemplatesMenu;
	}
	return null;
}
//======================================================================================
function getTemplateContent(sMessage)
{
	var sContent = "";
	var oWSEE = g_stWSEEngine.template;
	if (oWSEE)
	{
	}
	return sContent;
}
//======================================================================================
// Event Functions Definition:
//======================================================================================
var g_nPrevKeyCode = null;
function onHandleKeyUp(e)
{
	var nKeyCode = e.keyCode;
	if (nKeyCode == 27) //Esc character
	{
		onHideSuggestions();
		onHideLeftButtonMenu();
	}
	if (nKeyCode != g_nPrevKeyCode)
	{
		var sKeyValue = String.fromCharCode(nKeyCode);
		//alert("char[" + nKeyCode + "] = " + sKeyValue);
		
		if (nKeyCode == 190 || nKeyCode == 46 || sKeyValue==".") //190=='.' ; 13==enter character
		{
			g_nPrevKeyCode = nKeyCode;
			if (g_stWSEEngine && g_stWSEEngine.enrichment)
			{
				g_stWSEEngine.enrichment.updateContent();
			}
		}
		if ((nKeyCode >= 65 && nKeyCode <= 90) || (nKeyCode >= 97 && nKeyCode <= 122))
		{
			g_nPrevKeyCode = nKeyCode;
		}
		return true;
	}
	return true;
}
//--------------------------------------------------------------------------------------
function onHandleMouseUp(e)
{
	onHideSuggestions();
	//onShowLeftButtonMenu(e);
	//onLeftButtonMenu(e);
	return false;
}
//--------------------------------------------------------------------------------------
function onShowSpellingSuggestions(e,nIndex)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE) // && index>=0 && index<oWSEE.vEnrichment.length)
	{
		oWSEE.showSpellingSuggestions(e,nIndex);
	}
}
//--------------------------------------------------------------------------------------
function onShowGrammarSuggestions(e,nIndex)
{
	var oWSEE = g_stWSEEngine.enrichment;
	//if (oWSEE && index>=0 && index<oWSEE.vEnrichment.length)
	if (oWSEE)
	{
		//oWSEE.showGrammarSuggestions(index);
		oWSEE.showGrammarSuggestions(e,nIndex);
	}
}
//--------------------------------------------------------------------------------------
function onShowEnrichmentSuggestions(e,nIndex)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE) // && index>=0 && index<oWSEE.vEnrichment.length)
	{
		oWSEE.showEnrichmentSuggestions(e,nIndex);
	}
}
//--------------------------------------------------------------------------------------
function onReplaceWord(e)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		var oElement = getEventElement(e);
		if (oElement)
		{
			var sNewWord = oElement.innerHTML;
			var nLangType= oWSEE.vEnrichment[oWSEE.nSelectedIndex].nLangType;
			oWSEE.replaceWord(sNewWord,nLangType);
		}
		return true;			
	}
	return false;
}
//--------------------------------------------------------------------------------------
function onAddWord(e)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		var oElement = getEventElement(e);
		if (oElement)
		{
			var sNewWord = oElement.innerHTML;
			var nLangType = oWSEE.vEnrichment[oWSEE.nSelectedIndex].nLangType;
			var nEnrichmentLangType = oWSEE.getEnrichmentLangType(nLangType);
			oWSEE.addWord(sNewWord,nEnrichmentLangType);
		}
	}
}
//--------------------------------------------------------------------------------------
/*
function onReplaceWord(sNewWord,nLangType)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.replaceWord(sNewWord,nLangType);
	}
}
*/
//--------------------------------------------------------------------------------------
/*
function onAddWord(sNewWord,nLangType)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.addWord(sNewWord,nLangType);
	}
}
*/
//--------------------------------------------------------------------------------------
function onHideSuggestions(e)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.hideSuggestions();
	}
}
//--------------------------------------------------------------------------------------
var g_bLastButton = 0;
function onLeftButtonMenu(e)
{
	if (g_bLastButton == 0)
	{
		onShowLeftButtonMenu(e);
		g_bLastButton = 1;
	}
	else
	{
		onHideLeftButtonMenu();
		g_bLastButton = 0;
	}
}
//--------------------------------------------------------------------------------------
function onHideLeftButtonMenu()
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.hideLeftButtonMenu();
	}
}
//--------------------------------------------------------------------------------------
function onShowLeftButtonMenu(e)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.showLeftButtonMenu(e);
	}
}
//--------------------------------------------------------------------------------------
function onQueryDictionary(sWord, nLangType)
{
	var oWSEE = g_stWSEEngine.enrichment;
	onStopDictionaryTimer();
	if (oWSEE)
	{
		oWSEE.queryDictionary(sWord, nLangType);
	}
}
//--------------------------------------------------------------------------------------------
function onQueryTemplate(sName)
{
	var oWSEE = g_stWSEEngine.template;
	if (oWSEE)
	{
		oWSEE.queryTemplate(sName);
	}
}
//--------------------------------------------------------------------------------------
function onUndo()
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.undo();
	}
}
//--------------------------------------------------------------------------------------
function onRedo()
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.redo();
	}
}
//--------------------------------------------------------------------------------------------
function onHandleError(sError)
{
	onHideSuggestions();
	onStopDictionaryTimer();

	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.showProgressDialog(false);
	}
	if (this.bShowErrors)
	{
		if (!sError || sError.length==0)
		{
			sError = "Error!";
		}
		sError += "\n\nIf this problem persists, let us know by emailing us at: support@whitesmoke.com";
		alert(sError);
	}
}
//--------------------------------------------------------------------------------------------
function handleQuerySentenceResponse(sResponseData)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.showProgressDialog(false);
		if (sResponseData)
		{
			var oEnrichmentInfo = wsGetEnrichmentInfo(sResponseData,oWSEE.vTokens);
			oWSEE.setEnrichmentInfo(oEnrichmentInfo);
		}
		oWSEE.initContent();
	}
}
//--------------------------------------------------------------------------------------
function handleQueryWordResponse(sResponseData)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		oWSEE.showProgressDialog(false);
		if (sResponseData)
		{
			//alert(sResponseData);
			var oEnrichmentInfo = wsGetEnrichmentInfo(sResponseData,oWSEE.vTokens);
			oWSEE.mergeEnrichmentInfo(oEnrichmentInfo);
		}
		//oWSEE.updateContent();
	}
}
//--------------------------------------------------------------------------------------
function handleQueryDictionaryResponse(sResponseData)
{
	var oWSEE = g_stWSEEngine.enrichment;
	if (oWSEE)
	{
		if (sResponseData)
		{
			//alert("sResponseData:\n" + sResponseData);
			var oEnrichmentInfo = wsGetEnrichmentInfo(sResponseData,oWSEE.vTokens);
			//alert("Dictionary\n" + oEnrichmentInfo[0].d[0]);
			oWSEE.setDictionaryInfo(oEnrichmentInfo);
		}
		oWSEE.showDictionaryResult();
	}
}
//--------------------------------------------------------------------------------------
function handleLookupDictionaryResponse(sResponseData)
{
	var oWSEE = g_stWSEEngine.lookup;
	if (oWSEE)
	{
		if (sResponseData)
		{
			var oEnrichmentInfo = wsGetEnrichmentInfo(sResponseData,oWSEE.vTokens);
			oWSEE.setDictionaryInfo(oEnrichmentInfo);
		}
		oWSEE.showLookupResult();
	}
}
//--------------------------------------------------------------------------------------
function handleQueryTemplateListResponse(sResponseData)
{
	var oWSEE = g_stWSEEngine.template;
	if (oWSEE)
	{
		if (sResponseData)
		{
			//alert(sResponseData);
			var vTemplates = eval(sResponseData);
			oWSEE.showTemplates(vTemplates);
		}
	}
}
//--------------------------------------------------------------------------------------
function handleQueryTemplateResponse(sResponseData)
{
	var oWSEE = g_stWSEEngine.template;
	if (oWSEE)
	{
		oWSEE.sContent = "";
		if (sResponseData)
		{
			//oWSEE.sContent = sResponseData;
			oWSEE.displayTemplateContent(sResponseData);
			//oWSEE.setEditorContent(sResponseData);
		}
	}
}
//--------------------------------------------------------------------------------------
function html2Text(sHTML)
{
	var sText = "";
	var nPos = sHTML.indexOf("<body");
	if (!nPos || nPos<0)
	{
		nPos = sHTML.indexOf("<BODY");
		if (!nPos || nPos<0)
		{
			nPos = 0;
		}
	}
	sText = sHTML.substr(nPos);
	sText = sText.replace(/\r\n|\n/g,"");
	sText = sText.replace(/<p>|<P>/g,"\r\n\r\n");
	sText = sText.replace(/<br>|<BR>|<div>|<DIV>|<\/p>|<\/P>/g,"\r\n");
	sText = sText.replace(/\&amp;/g,"&");
	sText = sText.replace(/\&quot;/g,"\"");
	sText = sText.replace(/\&nbsp;/g," ");
	//removes all tags
	sText = sText.replace(/<[\w\/g]+[^<>]*>/g, ""); //replace(/<&#91;^>&#93;*>/g, "");
	//alert("after removing all tags\n" + sText);
	//alert("in html2Text:\n\nsHTML=[" + sHTML + "]\n\nsText=[" + sText + "]");
	return sText;
}
//======================================================================================
// Remote Script Code:
//======================================================================================
/*
//--------------------------------------------------------------------------------------
function getMouseXY(e) // works on IE6,FF,Moz,Opera7
{
	if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the event)
	if (e)
	{
		if (e.pageX || e.pageY)
		{ // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			mousex = e.pageX;
			mousey = e.pageY;
			algor = '[e.pageX]';
			if (e.clientX || e.clientY) algor += ' [e.clientX] '
		}
		else if (e.clientX || e.clientY)
		{ // works on IE6,FF,Moz,Opera7
			mousex = e.clientX + document.body.scrollLeft;
			mousey = e.clientY + document.body.scrollTop;
			algor = '[e.clientX]';
			if (e.pageX || e.pageY) algor += ' [e.pageX] '
		}
	}
}
*/
//--------------------------------------------------------------------------------------
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}
//--------------------------------------------------------------------------------------
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
//--------------------------------------------------------------------------------------
function getFrameObj(sFrameId)
{
	var wsFrameObj = window.frames[sFrameId];
	return (wsFrameObj? wsFrameObj : null);
}
//--------------------------------------------------------------------------------------
function createIFrame(sIFrameId)
{
	var wsIFrameObj = getFrameObj(sIFrameId);
	if (wsIFrameObj != null)
	{
		return wsIFrameObj;
	}
	else if (! document.createElement)
	{
		return null;
	}
	else if (!wsIFrameObj && document.createElement)
	{
		// Create the IFrame and assign a reference to the object to our global variable wsIFrameObj.
		// This will only happen the first time createIFrame() is called
		try
		{
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id',sIFrameId);
			tempIFrame.setAttribute('name',sIFrameId);
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			wsIFrameObj = document.body.appendChild(tempIFrame);

			if (document.frames)
			{
				// This is for IE5 Mac, because it will only allow access to the document object
				// of the IFrame if we access it through the document.frames array
				wsIFrameObj = document.frames[sIFrameId];
			}
		}
		catch(exception)
		{
			// This is for IE5 PC, which does not allow dynamic creation and manipulation
			// of an iframe object. Instead, we'll fake it up by creating our own objects.
			iframeHTML='<iframe id="' + sIFrameId + '" name="' + sIFrameId + '" "style="border:0px;width:0px;height:0px;"></iframe>';
			document.body.innerHTML += iframeHTML;
			wsIFrameObj = new Object();
			wsIFrameObj.document = new Object();
			wsIFrameObj.document.location = new Object();
			wsIFrameObj.document.location.iframe = document.getElementById(sIFrameId);
			wsIFrameObj.document.location.replace = function(location) {this.iframe.src = location;}
		}
	}
	else if (navigator.userAgent.indexOf('Gecko') !=-1 && !wsIFrameObj.contentDocument)
	{
		// we have to give NS6 a fraction of a second to recognize the new IFrame
		var sFunc = "createIFrame(" + this.sRSFrameId + ")";
		setTimeout(sFunc,10);
	}
	return wsIFrameObj;
}
//======================================================================================
// Dictionary Timer
//======================================================================================
var timerID = null;
//--------------------------------------------------------------------------------------
function getEscapedWord(sWord)
{
	return sWord.replace(/\'/g,"\\'");
}
//--------------------------------------------------------------------------------------
function onStartDictionaryTimer(e)
{
	//alert("onStartDictionaryTimer");
	if (timerID == null)
	{
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			if (oWSEE.sMode == "min")
			{
				return false;
			}

			var oElement = getEventElement(e);
			if (! oElement)
			{
				return false;
			}
			//alert("oElement.id = " + oElement.id + ", oElement.innerHTML = " + oElement.innerHTML);
			var sWord = getEscapedWord(oElement.innerHTML);
			if (sWord.length < 2 && ! oWSEE.isAlpha(sWord))
			{
				return true;
			}
			var nIndex = getElementIndex(oElement);
			if (nIndex < 0)
			{
				nIndex = oWSEE.nSelectedIndex;
			}
			var nLangType = 0;
			if (nIndex >= 0)
			{
				var nLangType = oWSEE.vEnrichment[nIndex].nLangType;
			}
			//alert("nLangType = " + nLangType);
			
			//oWSEE.setLastPointedInfo(sWord,nLastLangType,nLastPointedIndex,oLastPointedElement);
			var oMousePos = oWSEE.getMousePos(e);
			//alert("onStartDictionaryTimer, pos = " + oMousePos.X + "," + oMousePos.Y);
			oWSEE.setLastPointedInfo(e,oMousePos,sWord,nLangType,nIndex,oElement);
			var sFunc = "onQueryDictionary(\"" + sWord + "\"," + nLangType + ")";
			timerID = setTimeout(sFunc,2000);
		}
	}
}
//--------------------------------------------------------------------------------------
/*
function onStartDictionaryTimer(sWord,nLastPointedIndex,oLastPointedElement,nLastLangType)
{
	alert("onStartDictionaryTimer");
	if (timerID == null)
	{
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			oWSEE.setLastPointedInfo(sWord,nLastLangType,nLastPointedIndex,oLastPointedElement);
			//var sFunc = oWSEE.sParent + ".onQueryDictionary(\"" + sWord + "\"," + nLastLangType + ")";
			var sFunc = "onQueryDictionary(\"" + sWord + "\"," + nLastLangType + ")";
			timerID = setTimeout(sFunc,2000);
		}
	}
}
*/
//--------------------------------------------------------------------------------------
function onStopDictionaryTimer()
{
	if (timerID)
	{
		clearTimeout(timerID);
	}
	timerID = null;
}
//======================================================================================
function onShowMessage(sMessage)
{
	//alert(sMessage);
}
//======================================================================================
function onMouseClickWord(e)
{
	onStopDictionaryTimer();
	onHideLeftButtonMenu();
	
	if (e && ! isRightClick(e))
	{
		var oWSEE = g_stWSEEngine.enrichment;
		if (oWSEE)
		{
			var oElement = getEventElement(e);
			if (! oElement)
			{
				return false;
			}	
			var sWord = oElement.innerHTML;
			var nSelectedIndex = getElementIndex(oElement);
			while (nSelectedIndex < 0 && oElement.parentNode)
			{
				oElement = oElement.parentNode;
				nSelectedIndex = getElementIndex(oElement);
			}
		
			var enrichment = oWSEE.vEnrichment[nSelectedIndex];
			if (enrichment)
			{
				if (enrichment.s && enrichment.s.length > 0) //spelling
				{
					onShowSpellingSuggestions(e,nSelectedIndex);
				}
				else if (enrichment.g && enrichment.g.length > 0) //grammar
				{
					onShowGrammarSuggestions(e,nSelectedIndex);
				}
				else if ((enrichment.t && enrichment.t.length > 0)  || //thesaurus
							(enrichment.e && enrichment.e.length > 0)) //enrichment
				{
					onShowEnrichmentSuggestions(e,nSelectedIndex);
				}
			}
		}
	}	
	return false; //return false to disable right click;
}
//--------------------------------------------------------------------------------------
function onMouseOverWord(e)
{
	onStartDictionaryTimer(e);
}
//--------------------------------------------------------------------------------------
function onMouseOutWord(e)
{
	onStopDictionaryTimer();
}
//--------------------------------------------------------------------------------------
function onMouseOverSuggestion(e)
{
	onStartDictionaryTimer(e);
	var oElement = getEventElement(e);
	if (oElement)
	{
		oElement.className = "wsSuggestionOver";
	}
}
//--------------------------------------------------------------------------------------
function onMouseOutSuggestion(e)
{
	onStopDictionaryTimer();
	var oElement = getEventElement(e);
	if (oElement)
	{
		oElement.className = "wsSuggestion";
	}
}
//--------------------------------------------------------------------------------------

