// (c)2001 XDE,Inc.  
// Only for use with XDE products, no other use licensed
// This copyright header must be shown in top of current file code.
//
/* 
-------------------------------------------------------------
$Id: SpellCheckNoApplet.js,v 1.8 2003/08/24 00:46:01 jaxtrx Exp $
-------------------------------------------------------------
$RCSfile: SpellCheckNoApplet.js,v $
$Author: jaxtrx $
$Revision: 1.8 $
$Date: 2003/08/24 00:46:01 $
$Locker:  $
$Name:  $
------------------------------------------------------------
*/
var mainwindowloaded=false;
/*
Spell Server Processor Page

For JSP/Java Server JSP Support Use : SpellCheckNoApplet.jsp 
For JSP/Java Server Servlet Support Use : /xdespellchecker/servlet/xdeSpellNoApplet
For ASP COM Use : SC_process_htmlOnly.asp
For ASP COM Spell Proxy Use : sc_proxy.asp

*/
var SpellServerHref="SC_process_htmlOnly.asp";
var SpellingWindowURL="/xdespellchecker/noapplet/SpellWindow.htm"; //update to reflect the installation location of the spellWindow

var NoHtml=false;
var isMSIE=(navigator.appName=='Microsoft Internet Explorer');
var isNS=navigator.appName=='Netscape';
var isMac=navigator.appVersion.indexOf("Mac") > 0;

var LastStartPosition=0; //reset between calls to search and replace.
var TotalMispellings=0;
function doSpell(opener,Language,p_ctrl,reloadnow){
/* p_ctrl has several special parameters:
typectrl=[type of control]
exec=[JavaScript/VBScript method to call on parent window (yours)] e.g. save a form when finish has been selected.

*/

	
	var spellingWindow;
	var x = (screen.width - 450) / 2;
	var y = (screen.height - 300) / 2;
	
	x=100;
	y=100;
	opener.status='Spell Check Started';
	if(isMSIE&&(parseInt (navigator.appVersion)< 4)){
	  alert("This SpellChecker Only supports IE 4.0+ or NS 4.0+");
	  return;
	}
	spellingWindow=window.open(SpellingWindowURL+"?language="+Language+"&ctrl="+p_ctrl,null,"top="+x+",left="+y+",width=450,height=300,toolbar=no,resizable=no");
if(isNS)
	spellingWindow.window.focus();

if(!reloadnow)
	return spellingWindow;
}

function CheckWord(WordtoCheck){
	//Move to current word via h ref #
	//Load array
	//populate suggestions
	currentword=WordtoCheck;
	WordtoCheck=StdreplaceString(0,WordtoCheck,"|XDEapos|","'",true);
	parent.document.title='Looking up suggestions for '+WordtoCheck;
//CKH Added 04/22/2002 avoid mac resource crash, do not remove browser check.
if(isMSIE){
	parent.footerframe.document.close();
	parent.footerframe.document.open();
}
//end changes
	parent.footerframe.document.write('<html><body><FORM ENCTYPE="application/x-www-form-urlencoded" ACTION="'+SpellServerHref+'" NAME="SpellForm" target="_self" METHOD="POST">');
	parent.footerframe.document.write('<input type="text" name="texttocheck" value="'+escape(WordtoCheck)+'">');
	var language=parent.mainframe.document.spellcheckform.language.value;
	parent.footerframe.document.write('<input type="text" name="language" value="'+escape(language)+'">');
	parent.footerframe.document.write('<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">');
	parent.footerframe.document.write('</FORM></body></form>');
	
	parent.footerframe.document.SpellForm.texttocheck.value=escape(WordtoCheck);
	parent.footerframe.document.forms[0].submit();
	parent.mainframe.document.spellcheckform.currentword.value=WordtoCheck;
	
	parent.document.title=WordtoCheck + parent.mainframe.document.spellcheckform.misSpelledMessage.value;

}

function RefreshText(){
	LastStartPosition=0; //Note last start position is a global.
	var str=parent.mainframe.document.spellcheckform.MispelledWords.value;
	var fixedtext=parent.mainframe.document.spellcheckform.fixedtext.value;
	var TempDisplay=fixedtext;
	var oldstart=0;
	var FirstWord='';
	TotalMispellings=0;
	var currentError=-1;
	var arr = str.split("|XDE|");
	for(var i = 0; i < arr.length; i++){
		var tmpval=arr[i];
	 	if(tmpval.length>0&&tmpval!=''){
			if(TotalMispellings==0)
				FirstWord=tmpval;
			if(tmpval.indexOf('-ISOK')>-1){
				var tmpstr=tmpval.substr(0,tmpval.length-5);
				LastStartPosition=TempDisplay.indexOf(tmpstr,oldstart)+tmpstr.length;
					
			}else{
				newtmpval=StdreplaceString(0,tmpval,"'","|XDEapos|",true);
				var ReplaceWith='';
				if(parent.mainframe.document.spellcheckform.currentword.value==tmpval){
					ReplaceWith='<FONT SIZE="+1"><a name=\'word' + TotalMispellings + '\' class=SpellError href="java'+'script:CheckWord(\'' + newtmpval+ '\');">' + tmpval + '</a></font>';
					if(currentError==-1)
						currentError=TotalMispellings; //save for focus
				}else{
					ReplaceWith='<a name=\'word' + TotalMispellings + '\' class=SpellError href="java'+'script:CheckWord(\'' + newtmpval+ '\');">' + tmpval + '</a>';
					
				}
				TotalMispellings++;
				if(TotalMispellings>20)
					break; //to prevent script from running too slowly
				
				//Note last start position is a global.
				oldstart=LastStartPosition;

				TempDisplay=replaceString(LastStartPosition,TempDisplay,tmpval,ReplaceWith);
				fixedone=true;
			}
		}
	}

if(NoHtml) //Don't do if we have an html delimiter, should be user input later 
	 TempDisplay=replaceString(0,TempDisplay,'\n','<br>',true);

 if (isNS) {
	 with(parent.topframe.document){
//	 	TempDisplay1='<sc'+'ript src="SpellCheckNoApplet.js"></scr'+'ipt>';
		 TempDisplay2 = '<SP'+'AN STYLE="position: absolute; border: 0px red solid; width: 100%">' + TempDisplay + '</SPAN>';
		 open();
		 write(TempDisplay2);
		 close();
	  }
  
 }else{
	parent.topframe.foundSoFar.innerHTML=TempDisplay;
	if(isMSIE&!isMac){ //assign focus to current word
		for (i=0; i<parent.topframe.document.links.length; i++) {
			if(parent.topframe.document.links[i].name=='word'+currentError)
				parent.topframe.document.links[i].focus();
		}
	}			
 }

 if(TotalMispellings==0){
	parent.document.title=parent.mainframe.document.spellcheckform.Spellfinished.value;    
	parent.mainframe.document.spellcheckform.suggestionlist.options.length=1;
	parent.mainframe.document.spellcheckform.tchangeto.focus();
	if(confirm(parent.mainframe.document.spellcheckform.Spellfinished.value))
		finishSpellCheck()
	else
		CancelSpellCheck();
 }
//Add finishSpellCheck() function to the end of RefreshText() function to automatically close window
}

function SelectWord(){
	var listindex=self.document.spellcheckform.suggestionlist.selectedIndex;
	self.document.spellcheckform.tchangeto.value=self.document.spellcheckform.suggestionlist.options[listindex].text;
}

function CancelSpellCheck(){
	parent.close();
}

function finishSpellCheck(){
	//return text to calling form control.
	var textobjectcheck=getQueryParm(parent.document.location.toString(),"ctrl");
	var controlType=getQueryParm(parent.document.location.toString(),"typectrl");
	var executeWhenComplete=getQueryParm(parent.document.location.toString(),"exec");
//	var OriginalControl=eval('parent.opener.document.'+textobjectcheck);
	var OriginalControl=eval('parent.opener.document.'+textobjectcheck)||parent.opener.document.getElementById(textobjectcheck);
	setControlText(OriginalControl,self.document.spellcheckform.fixedtext.value,controlType);
	if(executeWhenComplete!="") //code to execute when finish selected.
		eval("parent.window.opener."+executeWhenComplete);
	parent.close();
}

function ServerSpellCompleted() {
	parent.document.title='Please correct highlighted text.';    
}
function CheckNextWord(LastWord){


//locate where we are in the buffer, skip to the next word...
	var str=self.document.spellcheckform.MispelledWords.value;
	parent.mainframe.document.spellcheckform.suggestionlist.options.length=0;
	parent.mainframe.document.spellcheckform.tchangeto.value='';
	var arr = str.split("|XDE|");
	var nextword="";
	var tryit=false;
	for(var i = 0; i < arr.length; i++){
		var tmpval=arr[i];
		var stripped=StdreplaceString(0,tmpval,'-ISOK','',true);
		if(tmpval.length > 0 && stripped == LastWord)
			tryit=true;
//				alert(arr[i+1]);
		if(tryit){
			if(i+1 < arr.length){
				if(arr[i+1]!='' && arr[i+1].indexOf('-ISOK')==-1){
					CheckWord(arr[i+1]);

				return;
				}
			}else{//no more
				break;
			}
		}
	}

	RefreshText();
}
function ChangeWord(){
	var WordtoChange=self.document.spellcheckform.currentword.value;
	var tmpchangeto=self.document.spellcheckform.tchangeto.value;
	var ReplaceWith=tmpchangeto;
	var fixedtext=self.document.spellcheckform.fixedtext.value;
	var TempDisplay=fixedtext;
	var str=self.document.spellcheckform.MispelledWords.value;
	var tlen=0;
	self.document.spellcheckform.MispelledWords.value='';
	var arr = str.split("|XDE|");
	var  MisspelledWordsLeft='';
	var  AddRemainingMispelledWords=false; //use later for change all
	//Rebuild the mis-spelled words buffer, only do once otherwsie second mispelling of same word will be ignored.

	MisspelledWordsLeft=StdreplaceString(0,str,WordtoChange+'|XDE|',ReplaceWith+'-ISOK|XDE|',false)

	self.document.spellcheckform.MispelledWords.value=MisspelledWordsLeft;
	TempDisplay= replaceString(tlen,TempDisplay,WordtoChange,ReplaceWith);
	self.document.spellcheckform.fixedtext.value=TempDisplay;
	CheckNextWord(ReplaceWith);
}

function ChangeAll(){
	var WordtoChange=self.document.spellcheckform.currentword.value;
	var tmpchangeto=self.document.spellcheckform.tchangeto.value;
	var ReplaceWith=tmpchangeto;
	var fixedtext=self.document.spellcheckform.fixedtext.value;
	var TempDisplay=fixedtext;
	var str=self.document.spellcheckform.MispelledWords.value;
	var tlen=0;
	self.document.spellcheckform.MispelledWords.value='';
	var arr = str.split("|XDE|");
	var  MisspelledWordsLeft='';
	var  AddRemainingMispelledWords=false; //use later for change all
	//Rebuild the mis-spelled words buffer.
	MisspelledWordsLeft=StdreplaceString(0,str,WordtoChange+'|XDE|',WordtoChange+'-ISOK|XDE|',true)
	self.document.spellcheckform.MispelledWords.value=MisspelledWordsLeft;
	TempDisplay= replaceString(tlen,TempDisplay,WordtoChange,ReplaceWith,true);
	self.document.spellcheckform.fixedtext.value=TempDisplay;
	CheckNextWord(WordtoChange);
}

function Ignore(){
	var WordtoChange=self.document.spellcheckform.currentword.value;
	var str=self.document.spellcheckform.MispelledWords.value;
	var  MisspelledWordsLeft='';
	MisspelledWordsLeft=StdreplaceString(0,str,WordtoChange+'|XDE|',WordtoChange+'-ISOK|XDE|',false)
	self.document.spellcheckform.MispelledWords.value=MisspelledWordsLeft;
	CheckNextWord(WordtoChange);
}

function IgnoreAll(){
	var WordtoChange=self.document.spellcheckform.currentword.value;
	var str=self.document.spellcheckform.MispelledWords.value;
	var  MisspelledWordsLeft='';
	MisspelledWordsLeft=StdreplaceString(0,str,WordtoChange+'|XDE|',WordtoChange+'-ISOK|XDE|',true)
	self.document.spellcheckform.MispelledWords.value=MisspelledWordsLeft;
	CheckNextWord(WordtoChange);
}

function AddWord(){
alert('Sorry access denied to server dictionary, server administrator modify the spellchecknoapplet.js file (addword function)');
return;
	var WordtoChange=self.document.spellcheckform.currentword.value;
	var str=self.document.spellcheckform.MispelledWords.value;
	var  MisspelledWordsLeft='';
	MisspelledWordsLeft=StdreplaceString(0,str,WordtoChange+'|XDE|',WordtoChange+'-ISOK|XDE|',true)
	self.document.spellcheckform.MispelledWords.value=MisspelledWordsLeft;
	parent.document.title='Adding '+ WordtoChange;
	parent.footerframe.document.write('<FORM ENCTYPE="application/x-www-form-urlencoded" ACTION="'+SpellServerHref+'" NAME="SpellForm" target="_self" METHOD="POST">');
	parent.footerframe.document.write('<TEXTAREA NAME="xdewordtoadd" ROWS="10" COLS="80"></TEXTAREA>');
	parent.footerframe.document.write('<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">');
	parent.footerframe.document.write('</FORM>');
	parent.footerframe.document.SpellForm.xdewordtoadd.value=escape(WordtoChange);
	parent.footerframe.document.forms[0].submit();

	var startindex=0;
	//Notify the user as to what is happening.
	//do not remove this alert, it also provides time for the request to be processed.
	alert('Adding "'+WordtoChange+'" to server dictionary'); 
	CheckNextWord(WordtoChange);
}

//General Functions
function replaceString(StartPos,SearchString,FindText,ReplaceWithText,doall){
	var changeall=false,intag=false;
	var TempMatched, fSubstring, sSubstring;
	var AlphaCharacters='abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ¡µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ';
	if(doall==true)changeall=true;
	TempMatched=SearchString.indexOf(FindText,StartPos);
	if(TempMatched==-1)return SearchString;
	for(var i = 0; i < SearchString.length; i++){
		if(i>TempMatched)
			TempMatched=SearchString.indexOf(FindText,i);		
		if(TempMatched==-1)return SearchString;
		StartPos=TempMatched+FindText.length;

		var tmpval=SearchString.charAt(i);

		if(tmpval=='<')intag=true;
		if(tmpval=='>'&&intag==true){
			intag=false; 
			continue;
		}
		if(intag)
			continue;
		if(TempMatched==i){ 
			if(TempMatched==0)
				PreviousChar=' ';
			else
				PreviousChar=SearchString.charAt(TempMatched-1);
			
			if(StartPos<SearchString.length)
				LastChar=SearchString.charAt(StartPos);
			else
				LastChar=' ';
			
			//Check if we're currently in the middle of another word.
			if(AlphaCharacters.indexOf(PreviousChar)==-1 && 
				AlphaCharacters.indexOf(LastChar)==-1 ){
				//Casual we have a whole word.  
				fSubstring=SearchString.substring(0,TempMatched);
				sSubstring=SearchString.substring(TempMatched+FindText.length,SearchString.length);
				SearchString=fSubstring + ReplaceWithText + sSubstring;
				//move forward to skip this partial word.
				LastStartPosition=fSubstring.length+ReplaceWithText.length;
				if(!changeall)break;
			}else{
				LastStartPosition=StartPos;//move forward to skip this partial word.
			}
			
		}
}

return SearchString; //Return changed text
}

function StdreplaceString(StartPos,SearchString,FindText,ReplaceWithText,changeall){
	//be sure to reset LastStartPosition between functions.
	var TempMatched=0,fSubstring, sSubstring,intag=false;
	TempMatched=SearchString.indexOf(FindText,StartPos);
	if(TempMatched==-1)return SearchString;
	for(var i = 0; i < SearchString.length; i++){
		if(i>TempMatched)
			TempMatched=SearchString.indexOf(FindText,i);		
		if(TempMatched==-1)return SearchString;
		var tmpval=SearchString.charAt(i);

		if(tmpval=='<')intag=true;
		if(tmpval=='>'&&intag==true){
			intag=false; 
			continue;
		}
		if(intag)
			continue;
		if(TempMatched==i){ 
	  		StartPos=TempMatched+ReplaceWithText.length;
			fSubstring=SearchString.substring(0,TempMatched);
			sSubstring=SearchString.substring(TempMatched+FindText.length,SearchString.length);
			SearchString=fSubstring + ReplaceWithText + sSubstring;
			if(!changeall)
				return SearchString; //Return changed text	break;
			else
				TempMatched=SearchString.indexOf(FindText,StartPos);	

		}		
	     
	}
	
	return SearchString; //Return changed text
}

function getQueryParm(pURL,ParmtoGet){
	var StartPos=pURL.indexOf(ParmtoGet);
	var ParmValue='';
	if(StartPos>-1){
		var lookfor='&';
		var NextPos=pURL.indexOf(lookfor,StartPos);
		if(NextPos==-1)NextPos=pURL.length;
		ParmValue=pURL.substring(StartPos+ParmtoGet.length+1,NextPos);
	}
return ParmValue;
}

function StripHTML(InString){
return InString;
/* now handled by components
var intag=false;
var cleanedbuffer="",addchar="";
InString=StdreplaceString(0,InString,"&nbsp;"," ",true);
	for(var i = 0; i < InString.length; i++){
		var tmpval=InString.charAt(i);
		if(tmpval=='<')intag=true;
		if(tmpval=='>'&&intag==true){
			intag=false; 
			addchar=" ";
			continue;

		}
		if(intag)continue;
		if(cleanedbuffer.length>0)
			cleanedbuffer+=addchar;
		cleanedbuffer+=	tmpval;
		addchar="";
	}
return cleanedbuffer;
*/
}

function CancelAllSpellCheck(){ //added by sothebys.com, used with multiText Box
	if (parent.opener.xdearr){
		parent.opener.xdearr.length=0;
	}
	CancelSpellCheck();
}

function isJSDefined(pobject) {
	var t = typeof pobject
	return t != "undefined" && (t != "object" || String(pobject) != "undefined")
}

function getControlText(OriginalControl,controlType){
	if(controlType=='xdeedit'){ //handle html editor
		if(!isNS)
			return OriginalControl.document.body.innerHTML;
		else
			return OriginalControl.frameWindow.document.body.innerHTML;
	}else if(controlType=='useActiveEdit'){
		return OriginalControl.DOM.body.innerHTML;
	}else if(controlType=='useOWA2000'){
		return OriginalControl.frameWindow.document.body.innerHTML;
	}else if(controlType=='divtag'){
		return OriginalControl.innerHTML;
	}else{
		return OriginalControl.value;
	}
}
function setControlText(OriginalControl,text,controlType){
	if(controlType=='xdeedit'){ //handle html editor
		if(!isNS)
			OriginalControl.document.body.innerHTML=text;
		else{
			OriginalControl.value=text;
			OriginalControl.frameWindow.document.body.innerHTML=text;
		}
	}else if(controlType=='useActiveEdit'){
		OriginalControl.DOM.body.innerHTML=text;
		OriginalControl.Refresh();
	}else if(controlType=='useOWA2000'){
		OriginalControl.frameWindow.document.body.innerHTML=text;
		OriginalControl.Refresh();
	}else if(controlType=='divtag'){
		OriginalControl.innerHTML=text;
	}else{
		OriginalControl.value=text;
	}
}
