﻿/*
 changes: 
	url (163)
	src="../Spell/close.gif" (40)
	background-color: #6CC0DC; (40)



*/

var txtSpellerName = "E-tikrintuvas";
var txtBtnReplace = "Pakeisti";
var txtBtnReplaceAll = "Pakeisti visus";
var txtBtnIgnore = "Ignoruoti";
var txtBtnIgnoreAll = "Ignoruoti visus";
var txtReplaceWith = "Pakeisti į:";
var txtSuggestions = "Pasiūlymai:";
var txtNoErrors = "Klaidų nerasta";


function spellerdlgMouseDown(e) {
	return speller.dlgMouseDown (e)
}


var speller = {
	currentInput : 0,
	currentSuggestion : 0,
	currentOffs : 0,
	fldseparator : "qwoeycgqweouysepalsdfjhqwo",
	spcDlg : null,
	xmlHttp : null,
	suggestions : [],
	inputsToCheck : [],
	ignorelist : [],
	replacelist : [],
	draginitx : 0,
	draginity : 0,
	bDown : false,

	html : '<table ID="spcDlg" STYLE="border: 2px solid black; display: none; background: #eeeeee; top:100px; left:100px; width: 300px; height: 220px; position: absolute;"><tr ID="spellerCaption" style="padding: 1px; text-indent: 3px; font: bold 14px Arial; background-color: #6CC0DC; color: white; cursor: move;" onmousedown="spellerdlgMouseDown()" onmouseup="speller.dlgMouseUp()"><td>' + txtSpellerName + '<img onmousedown="speller.dlgClose()" src="../Spell/close.gif" style="position: absolute;right: 4px;top: 4px;cursor: hand;cursor: pointer;" width="16" height="14"></td></tr><tr><td colspan="2"> <table align="center"><tr><td><table STYLE="background: #eeeeee;"><tr><td><span ID="ftnspellContext" STYLE="width: 160px; font: 10pt Verdana, Arial, Sans-Serif;"></span><br>&nbsp;<span ID="ftnspellErrWord" STYLE="color: red; width: 160px; font: 14pt Verdana, Arial, Sans-Serif;">negerai</span></td></tr><tr><td><span ID="ftnspellErrActionLabel" STYLE="font: 8pt verdana, arial, sans-serif;"> ' + txtReplaceWith + '</span><br>&nbsp;<input TYPE="text" ID="ftnspellErrActionValue" STYLE="width: 160px; font: Verdana, Arial, Sans-Serif;" size="20"></td></tr><tr><td><span ID="ftnspellSuggLabel" STYLE="font: 8pt verdana, arial, sans-serif;"> ' + txtSuggestions + '</span><br>&nbsp;<select ID="ftnspellSuggList" SIZE="5" STYLE="width: 160px; font: Verdana, Arial, Sans-Serif;" onclick="speller.SuggestionClick()" ondblclick="speller.SuggestionDblclick()"></select></td></tr></table></td><td ALIGN="center" VALIGN="middle"><!--<button STYLE="background: #cccccc; width: 100px; font: 8pt verdana, arial, sans-serif;" ID="ftnspell_options" CLASS="spellerButton"> Options</button>--><p><button STYLE="background: #cccccc; width: 100px; font: 8pt verdana, arial, sans-serif;" ID="ftnspell_replace" CLASS="spellerButton" onclick="speller.Replace ()"> ' + txtBtnReplace + '</button><br><button STYLE="background: #cccccc; width: 100px; font: 8pt verdana, arial, sans-serif;" ID="ftnspell_ignore" CLASS="spellerButton" onclick="speller.Ignore()"> ' + txtBtnIgnore + '</button><br> <!--<button STYLE="background: #cccccc; width: 100px; font: 8pt verdana, arial, sans-serif;" ID="ftnspell_learn" CLASS="spellerButton"> Learn</button>--></p><p><button STYLE="background: #cccccc; width: 100px; font: 8pt verdana, arial, sans-serif;" ID="ftnspell_replaceAll" CLASS="spellerButton" onclick="speller.ReplaceAll ()"> ' + txtBtnReplaceAll + '</button><br><button STYLE="background: #cccccc; width: 100px; font: 8pt verdana, arial, sans-serif;" ID="ftnspell_ignoreAll" CLASS="spellerButton" onclick="speller.IgnoreAll ()"> ' + txtBtnIgnoreAll + '</button></p></td></tr></table></td></tr></table>',


	init : function () {
		return
		var e = document.createElement ("TABLE");
		e.outerHTML = this.html
		this.showobj ("e",e)
//		document.appendChild (e)
//		alert (this.html)
//		this.showobj ("spcdlg", document.getElementById ("spcDlg"))
	
	},
	showobj : function (name,obj) {
		var msg="";
		for (i in obj) {
			var value = obj[i].toString ()
		
//			if (i.indexOf ("on") != 0 && i.indexOf ("html") == -1) msg += name+"."+i+"="+obj[i]+"\n";
			if (i.indexOf ("on") != 0 && i.indexOf ("html") == -1 && value.indexOf ("function") == -1) msg += name+"."+i+"="+value+"\n";
		}
		alert (msg)
	},
	dlgMouseUp : function () {
		document.onmousemove=null
		document.onmouseup=null
		speller.Mark ()
	},
	dlgClose : function () {
		speller.bDown = false
		var spcDlg = document.getElementById ("spcDlg")
		spcDlg.style.display="none"
		var b = document.getElementById ("spellerDlgBlind")
		b.style.display="none"
			document.onmousemove=null
			document.onmouseup=null
	},
	dlgShow : function () {
		speller.bDown = false
		var spcDlg = document.getElementById ("spcDlg")
		spcDlg.style.display="block"
		spcDlg.style.zIndex=1000

		var b = document.getElementById ("spellerDlgBlind")
		b.style.width=document.body.clientWidth
		b.style.height=document.body.clientHeight
		b.style.display="block"
		b.style.zIndex=200
	},
	dlgMouseDown : function (e) {
		if (!e) e = event
//showobj ("event",e)
		var spcDlg = document.getElementById ("spcDlg")
		this.draginitx=spcDlg.offsetLeft-e.clientX
		this.draginity=spcDlg.offsetTop-e.clientY

		speller.bDown = true
		document.onmousemove = function (e) {
			if (!e) e = event
			if (speller.bDown) {
				var spcDlg = document.getElementById ("spcDlg")
				spcDlg.style.left=e.clientX+speller.draginitx+"px"
				spcDlg.style.top=e.clientY+speller.draginity+"px"
			}
			return false
		}
		document.ommouseup = function () {
			document.onmousemove=null
			document.onmouseup=null
		}
	},
	start : function () {
		var caption = document.getElementById ("spellerCaption")
		caption.onmousedown=spellerdlgMouseDown
		this.inputsToCheck = new Array;
		var t, i, text;
		text = "";

		t=document.getElementsByTagName("TEXTAREA");
		for (i = 0; i < t.length; i++) {
			if (t[i].id.indexOf ('ftnspell') != 0) {
				this.inputsToCheck [this.inputsToCheck.length] = t[i];
			}
		}
		t=document.getElementsByTagName("INPUT");
		for (i = 0; i < t.length; i++) {
			if ((t[i].id.indexOf ('ftnspell') != 0) && (t[i].type == "text")) {
				this.inputsToCheck [this.inputsToCheck.length] = t[i];

			}
		}

		this.inputsToCheck.sort (function (a,b) { return a.sourceIndex - b.sourceIndex } )
		for (i = 0; i < this.inputsToCheck.length; i++) {
			if (this.inputsToCheck[i].tagName == "TEXTAREA") {
//				showobj ("a", this.inputsToCheck[i].value)
				if (this.inputsToCheck[i].innerText)
					text += this.inputsToCheck[i].innerText;
				else 
					text += this.inputsToCheck[i].value;
			} else if (this.inputsToCheck[i].tagName == "INPUT") {
				text += this.inputsToCheck[i].value;
			} else {
				alert ("Unknown tagName "+inputsToCheck[i].tagName);
			}
			text += " "+this.fldseparator+" ";
		}
		this.currentInput = 0
		this.currentSuggestion = 0
		this.currentOffs = 0

		if (window.XMLHttpRequest) this.xmlHttp = new XMLHttpRequest();
		else this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		if (this.xmlHttp == null) {
			alert("Your browser does not support XMLHTTP.")
			return
		}

		this.xmlHttp.onreadystatechange=this.stateChanged;
/*		var t2 = text;
		t2.replace (new RegExp("\r\n","g"), "\n");
		alert ("text.length= "+text.length */
		var url = "http://"+document.location.host+"/Spell/spell_isapi.dll"

		url += "?Check"
		text = encodeURIComponent (text)
		text = "text="+text

		this.xmlHttp.open("POST",url, true);
		this.xmlHttp.send(text)
	},
	stateChanged : function () {
		if (speller.xmlHttp.readyState==4) { 
			if (speller.xmlHttp.status == 200) {
				speller.dlgShow ()

				var r = speller.xmlHttp.responseText;
//				alert(r);
				r=speller.xmlHttp.responseText.replace (new RegExp("\r\n","g"), "\n");
				speller.suggestions = speller.xmlHttp.responseText.split ("\n");
			
//			showobj ("sug", speller.suggestions)
				speller.currentInput = 0
				speller.currentSuggestion = 0
				speller.spellSetupItem ()
			} else {
				alert ("Status: "+speller.xmlHttp.statusText);
			}
		}
	},
	Mark : function () {
		var s = this.suggestions[this.currentSuggestion].split (",");
		var spellcontext = ""
		if (this.inputsToCheck[this.currentInput].createTextRange) {
			var tr = this.inputsToCheck[this.currentInput].createTextRange()
			tr.move ('character', parseInt(s[1])+parseInt(this.currentOffs))
			tr.moveEnd ('character', parseInt(s[2]))
			tr.select ()
			tr.scrollIntoView (true)
			tr.moveStart ('word', -2)
			tr.moveEnd ('word', 2)
			spellcontext = tr.text
		} else {
			// NonIE
			var i = this.inputsToCheck[this.currentInput];
			i.selectionStart = parseInt(s[1])+parseInt(this.currentOffs)
			i.selectionEnd = i.selectionStart+parseInt(s[2])

		}

		var ph = document.getElementById ("ftnspellContext");
		if (ph) {
			ph.innerText = ph.textContent = spellcontext
		}
	},
	spellSetupItem : function () {
		if (!this.spellItemOk ()) {
			this.dlgClose ()
			alert (txtNoErrors)
			return
		}

		document.getElementById ("ftnspellErrActionValue").value = ""
		this.inputsToCheck[this.currentInput].scrollIntoView (true)
		var ew = document.getElementById ("ftnspellErrWord");
		var s = this.suggestions[this.currentSuggestion].split (",");
		ew.innerText = ew.textContent = s[0]

		this.Mark ();
		if (s[3]) {
			document.getElementById ("ftnspellErrActionValue").value = s[3];
		}

		var sl = document.getElementById ("ftnspellSuggList").options;
		var i;
		sl.length = 0
		for (i = 3; i < s.length; i++) {
			sl [sl.length] = new Option (s[i],s[i],false,false)
		}

	},
	spellItemOk : function () {
		for (; this.currentSuggestion < this.suggestions.length; this.currentSuggestion++) {
			var s = this.suggestions[this.currentSuggestion].split (",");
			if (this.suggestions[this.currentSuggestion].indexOf (this.fldseparator) != -1) { // Next input field
				// Clean selection for FF
				var i = this.inputsToCheck[this.currentInput];
				i.selectionStart = 0
				i.selectionEnd = undefined

				if (this.currentInput < this.inputsToCheck.length-1) {
					this.currentInput++
					var s = this.suggestions[this.currentSuggestion].split (",");
					this.currentOffs = -(parseInt(s[1])+s[0].length + 1);
				}
				else return false
			} else { // check if ignore/replace all
				if (this.ignorelist[s[0]]) {
				} else if (this.replacelist[s[0]]) {
					this.ReplaceText (s[1], s[2], this.replacelist[s[0]])
				} else {
					return true;
				}
			}
		}
		return false;
			
		
		if (this.currentSuggestion >= this.suggestions.length-1) {
			this.dlgClose ()
		} else {
			if (this.suggestions[this.currentSuggestion].indexOf (this.fldseparator) != -1) { // Next input field
				this.currentSuggestion++				
				this.currentInput++
			}
			if (this.currentSuggestion >= this.suggestions.length-1) {
				this.dlgClose ()
			} else {
				return true;
			}
		}
		return false;
	},
	MoveNext : function () {
		this.currentSuggestion++
		return this.spellItemOk ()
	},
	Next : function () {
		if (this.MoveNext ()) { this.spellSetupItem () }
		else this.dlgClose ()
	},
	Ignore : function () {
		this.Next ()
	},
	SuggestionClick : function (e) {	
//		if (!e) e = event
		var re = document.getElementById ("ftnspellErrActionValue");
		var s = document.getElementById ("ftnspellSuggList");
//		alert (s.options[s.selectedIndex].value)
//		re.value = e.srcElement.innerText;
		re.value = s.options[s.selectedIndex].value
	},
	ReplaceText : function (posStart, len, replacewith) {
		posStart = parseInt(posStart)+parseInt(this.currentOffs)
		if (this.inputsToCheck[this.currentInput].createTextRange) {
			var tr = this.inputsToCheck[this.currentInput].createTextRange()
			tr.move ('character', parseInt(posStart))
			tr.moveEnd ('character', len)
			tr.text = replacewith;
		} else {
			// NonIE
			var v = this.inputsToCheck[this.currentInput].value
			var s1 = v.substring(parseInt(0),parseInt(posStart))
			var s2 = v.substring(parseInt(posStart)+parseInt(len), v.length)
//			alert ("Replace: at " +posStart+" len "+len+" with "+replacewith+" length "+v.length+"\n"+s1+")\n("+s2)
			this.inputsToCheck[this.currentInput].value = s1 + replacewith + s2
				
/*
			var startPos = myField.selectionStart;
			var endPos = myField.selectionEnd;
			myField.value = myField.value.substring(0, startPos) + myValue + 
			myField.value.substring(endPos, myField.value.length);
			myField.focus();
			myField.selectionStart = startPos + myValue.length;
			myField.selectionEnd = startPos + myValue.length;
*/
		}
		this.currentOffs += replacewith.length-len;
	},
	SuggestionDblclick : function () {
		var s = speller.suggestions[speller.currentSuggestion].split (",");
		var sl = document.getElementById ("ftnspellSuggList");
		
		var r = sl.options[sl.selectedIndex].value
//		r.replace (/[\n\r]+/i,'')
		if (r.charCodeAt(r.length-1) == 13) r = r.substring (0, r.length-1)
		speller.ReplaceText (s[1], s[2], r)
//		alert (r.charCodeAt(r.length-1))
//		speller.ReplaceText (s[1], s[2], event.srcElement.innerText)
		speller.Next ()
	},
	Replace : function () {
		var s = this.suggestions[this.currentSuggestion].split (",");
		this.ReplaceText (s[1], s[2], document.getElementById ("ftnspellErrActionValue").value)
		this.Next ()
	},
	IgnoreAll : function () {
		var s = this.suggestions[this.currentSuggestion].split (",");
		this.ignorelist[s[0]] = 'y'
		this.Next ()
	},
	ReplaceAll : function () {
		var s = this.suggestions[this.currentSuggestion].split (",");
		var r = document.getElementById ("ftnspellErrActionValue").value
		this.replacelist[s[0]] = r
		this.ReplaceText (s[1], s[2], r)
		this.Next ()
	}
};


function showobj (name,obj,nameonly) {
		var msg="",i;
		for (i in obj) {
			if (i.indexOf ("on") != 0 && i.indexOf ("html") == -1 ) {
				if (nameonly || typeof obj[i] == "function") msg += name+"."+i +"\n";
				else msg += name+"."+i+"="+obj[i]+"\n";
			}
//			msg+= "i ("+(typeof obj[i]) +")\n";
		
		}
		alert (msg)

	}

document.write (speller.html)
document.write ('<div ID="spellerDlgBlind" onmouseup="speller.Mark()" STYLE="display: none; background:#eeeeee; top:0px; left:0px; position: absolute; filter:progid:DXImageTransform.Microsoft.Alpha(opacity=50)"></div>')
