//
//-:: Spell Checker ::-
//
//requires IE and Word on the client machine.

//
//takes a text string and runs a spell checker on it, returns corrected text
//
//required components: 
//	svcspell_checker.vbs
//	svcsc_results.htm	
//
//notes:
//	if svcsc_results.htm is not in the same folder as the calling script, 
//	change the variable svcpath from "" to reflect this
//
//params:
//	svcchkText		- the text string to check
//	
//platforms:
//	IE6+ PC			- works
//	IE5+ PC			- assumed to work
//	
//use:
//	with a textarea <textarea name="mytext"></textarea>
//	and a button <input type="button" onClick="mytext.value=svcspellCheck(mytext.value)">


// ### Change this is the svcsc_results.htm is not in the same place as the calling page
svcpath = ""

dim word , wordDocument , svcsuggestions , svcrange , svcchoice

function svcspellCheck(svcchkText)
	spellCheck = svcchkText
	on error resume next

	msgBoxError1 = 	"The spell check cannot be initialised." & chr(10) & chr(10)
	msgBoxError2 = "" // chr(10) & chr(10) & "Would you like to see Help regarding this issue?"


	// ### detect if we can run scripts
	set fso = CreateObject("scripting.filesystemobject")

	if err.number <> 0 then
		// ###  no we can't
		msgBoxErrorInner = "Your Internet security settings may be preventing the" & chr(10) & "spell checker from running."
		cont = msgbox( msgBoxError1 & msgBoxErrorInner & msgBoxError2 , vbOKOnly , "Error ..." )
		exit function
	end if
	
	// ### initialise word
	if not isObject(word) then set word = createobject("word.application")

	// ### set cleanUp function
	document.body.onunload = GetRef("svcsc_closeDown")

	// ### was Word initialised?
	if err.number<>0 then
		msgBoxErrorInner = "Word may not be correctly installed on this machine"
		cont = msgbox( msgBoxError1 & msgBoxErrorInner & msgBoxError2 , vbOKOnly , "Error ..." )
		svcsc_closeDown()
//			if cont <>6 then msgbox("Spell Check cancelled")
//			else newwin = open("help.htm" , "help" , "width=300,height=400,scrollbars,resizable")
//			end if
		exit function
	end if
	
	// ### create Document
	//if not isObject(wordDocument) then 
	set wordDocument = word.documents.add
	
	// ## put in text
	word.selection.text = svcchkText
	
	// ### process errors
	set objerrors = wordDocument.spellingErrors
	eCount = objerrors.count
	if eCount > 0 then
		for each strerror in objerrors
			pos = strerror.start

			svcsuggestions = ""
			svcrange = ""
			svcchoice = ""

			sPos = pos - 20 : if sPos < 1 then sPos = 1
			svcrange = mid(svcchkText,sPos,pos - sPos) 
			svcrange = svcrange & " <b style=""color:red"">" & strerror.text & "</b> " 
			svcrange = svcrange & mid( svcchkText , pos + 1 + len( strerror.text ) , 20 )

			with word.getspellingsuggestions(strerror.text)
				for X = 1 to .count
					if X > 1 then svcsuggestions = svcsuggestions + ","
					svcsuggestions = svcsuggestions + .item(X)
				next
				r = showModalDialog(svcpath & "svcsc_results.htm",window,"dialogWidth:400px;dialogHeight:300px;center:yes;status:no;scrolling:no")
				if r = "end" then svcspellCheck = svcchkText : svcsc_killDoc() : exit function
				selection = svcchoice
				if selection<>"" then
					if pos < 1 then pos = 1
					svcchkText = left(svcchkText,pos - 1) & replace(svcchkText,strerror.text,selection,pos,1)
				end if
			end with
		next
	end if
	if err.number<>0 then msgbox ("fatal error" & chr(10) & err.description & err.line)

	// ### kill document
	svcsc_killDoc()

	on error goto 0

	svcspellCheck = svcchkText
	msg = "No spelling mistakes found"
	if eCount > 0 then msg = "Spell check complete"
	call msgbox(msg,64)
	//svcsc_closeDown()
end function

sub svcsc_killDoc()
	// ### kill document
	wordDocument.close(0)
	set wordDocument = nothing
end sub

sub svcsc_closeDown
	// ### kill Word
	// if this doesn't run you're system will run really badly with lots of Word instances open :p
	on error resume next
	word.quit()
	set word = nothing
	on error goto 0
end sub
