// (c)2001 XDE,Inc.  Feel free to improve and return to XDE!
// Only for use with XDE products, no other use licensed
// This copyright header must be shown in top of current file code.
//
// Version 1.12
//
var mainwindowloaded=false;
//update the below to reflect the installation location of the spellWindow
var SpellServerHref="SC_process_htmlOnly.asp";
var useActiveEdit=false; //For Active Edit Support set to true
useOWA2000=false; //For OWA Support set to true
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){
	var SpellingWindowURL="/xdespellchecker/noapplet/SpellWindow.htm";
	var spellingWindow;
	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,"width=450,height=300,toolbar=no,resizable=no");
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)+'">');
	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 + ' is not correct';

}

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=0;
	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="javascript:CheckWord(\'' + newtmpval+ '\');">' + tmpval + '</a></font>';
				}else{
					ReplaceWith='<a name=\'word' + TotalMispellings + '\' class=SpellError href="javascript:CheckWord(\'' + newtmpval+ '\');">' + tmpval + '</a>';
					currentError=TotalMispellings; //save for focus
				}
				TotalMispellings++;
				
				//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 //disabled if(TempDisplay.indexOf('<')==-1)
	 TempDisplay=replaceString(0,TempDisplay,'\n','<br>',true);

 if (isNS) {
	 with(parent.topframe.document){
	 	TempDisplay1='<script src="SpellCheckNoApplet.js"></script>';
		 TempDisplay2 = '<SPAN STYLE="position: absolute; border: 0px red solid; width: 100%">' + TempDisplay + '</SPAN>';
		 open();
		 write(TempDisplay1);
		 write(TempDisplay2);
		 close();
	  }
  
 }else{
	parent.topframe.foundSoFar.innerHTML=TempDisplay;
if(isMSIE&!isMac){ //assign focus to current word
	if(parent.topframe.document.links.length>0)
		parent.topframe.document.links[0].focus();
}		
 }

 if(TotalMispellings==0){
	parent.document.title='Spell Check Completed';    
	parent.mainframe.document.spellcheckform.suggestionlist.options.length=1;
	parent.mainframe.document.spellcheckform.tchangeto.focus();
	alert('Spell Check Completed');
 }
}

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 OriginalControl=eval('parent.opener.document.'+textobjectcheck);
	if(useActiveEdit){ //CKH added
		
		OriginalControl.DOM.body.innerHTML=self.document.spellcheckform.fixedtext.value;
		OriginalControl.Refresh();
	}else if(useOWA2000){
		OriginalControl.DocumentHTML=self.document.spellcheckform.fixedtext.value;
//Lets see if we can go without refresh		OriginalControl.Refresh();
	}else{
		OriginalControl.value=self.document.spellcheckform.fixedtext.value;
	}
	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="";
	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){
			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.
	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
	var TempMatched, fSubstring, sSubstring;
	var AlphaCharacters='abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ¡µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ';
	if(doall==true)changeall=true;
	TempMatched=0;
	while(TempMatched>=0){
	   	TempMatched=SearchString.indexOf(FindText,StartPos);
		if(TempMatched<0){
			return SearchString;
		}

		StartPos=TempMatched+FindText.length;
		if(TempMatched>-1){
			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,doall){
	//be sure to reset LastStartPosition between functions.
	var TempMatched=0;
	var fSubstring, sSubstring;
	var changeall=false
	if(doall==true)
		changeall=true;

	while(TempMatched>-1){
		TempMatched=SearchString.indexOf(FindText,StartPos);
		if(TempMatched==-1)return SearchString;
		StartPos=TempMatched+ReplaceWithText.length;
		fSubstring=SearchString.substring(0,TempMatched);
		sSubstring=SearchString.substring(TempMatched+FindText.length,SearchString.length);
		SearchString=fSubstring + ReplaceWithText + sSubstring;
		if(!changeall)
			break;
	}
	
	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){
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;
}

