// update this when you make edits!
var version_scripts_spell = 2

var oldAction = "";
var spellAreaName = "";

function SpellCheck(name)
{
	if (isSpellChecking) {
		StopSpellCheck();
		return
	}
	Working();
	isSpellChecking = true;
	//if (spellAreaName == "") 
	spellAreaName = name;
	var v = document.getElementById(spellAreaName);	// actual textarea
	var w = v.offsetWidth;
	var h = v.offsetHeight;
	var txt = v.value
	document.spellcheck.txt.value = txt;
	document.spellcheck.submit();
	
	/*
	var oldAction = document.themessage.action;
	document.themessage.action = "worker_spell.asp?action=check&formname="+spellAreaName;
	document.themessage.target = "worker";
	document.themessage.submit();
	*/
	var u = document.getElementById('spellwnd');	// spellchecking div
	if (v.nextSibling)
		v.parentNode.insertBefore(u, v.nextSibling);
	else
		what.parentNode.appendChild(u);
		
	v.style.display = "none";
	u.style.display = "block";
	u.style.width = w;
	u.style.height = h;
	u.innerHTML = v.value.replace(new RegExp("\n", "g"), "<br>");
	
	var t = document.getElementById('doit');
	if (t != null)
		t.style.display = "none";
	t = document.getElementById('viewtext');
	if (t != null)
		t.style.display = "inline";
	//document.themessage.action = oldAction;
	//document.themessage.target = "";
}

function InnerText(what)
{
	var val = what.innerHTML;
	val = val.replace(new RegExp("<br>", "ig"), "\n");
	val = val.replace(/<[^>]+>/g,"");
	return val;
}

function StopSpellCheck()
{
	isSpellChecking = false;
	if (spellAreaName == "") return;
	var v = document.getElementById(spellAreaName);
	var u = document.getElementById('spellwnd');
	if (v.style.display != "none") return;
	u.style.display = "none";
	v.style.display = "block";
	v.value = InnerText(u);
	
	var t = document.getElementById('doit');
	t.style.display = "inline";
	t = document.getElementById('viewtext');
	t.style.display = "none";
	
	u.innerHTML = "<font color=green>Spell Checking. Please be patient...</font>";
	
}

function UpdateMousePos(e)
{
	var x = 0; var y = 0;
	if (e != null && e != 1) {
		x = e.pageX;
		y = e.pageY;
		pX = x; pY = y;
	} else if (e == 1) {
		x = pX; y = pY;
	} else {
		x = (document.all) ? window.event.x + document.body.scrollLeft : pX;
		y = (document.all) ? window.event.y + document.body.scrollTop  : pY;
		pX = x; pY = y;
	}
}

function SpellAddResponse()
{
	if (servResponse == '-1') {
		alert("Unable to add word. The Admins should hear about this!");
		return;
	} else {
		SpellIgnore();
	}
}

function SubmitOnReturn(what)
{
	if (window.event == null) return;
	var kv = window.event.keyCode;
	if (kv == 13 && !window.event.shiftKey) {
		PickSuggest();
	}
}

function SuggestResponse()
{
	clearTimeout(ttHideTimer);
	if (servResponse == '-1') {
		HideGlobalTooltip();
		alert("Unable to get a sane response from server...");
		return;
	} else {
		var t = document.getElementById('g_tt');
		t.style.display = "block";
		t.style.width = "120px";
		var res = unescape(servResponse)
		var arr = res.split(',');
		var txt = '<tr style="padding:0px;"><td><input type=text class=scinput value="'+suggestWhat.innerHTML+'" size=8 id="spellpick" name="spellpick" OnKeyDown="SubmitOnReturn(this);">&nbsp;<span onClick="PickSuggest();"><img class=curs src="images/ic_checkbutt.gif" align=absmiddle title="Insert changes"></a></td></tr>'
		var cl = "or";
		for (i = 0; i < arr.length; i++) {
			cl = "or";
			if (i % 2.0 == 1.0) cl = "or";
			if (arr[i].length >= 1) {
				//txt = txt + '<tr class="sc '+cl+'" OnMouseover="hL2(this);" OnMouseOut="dL2(this);"><td><nobr><a href="http://thesaurus.reference.com/search?q='+arr[i]+'" target="_new" title="View entry entry in thesaurus"><img width=11 src="images/ic_t.gif" align="absmiddle"></a><a href="http://dictionary.reference.com/search?q='+arr[i]+'" target="_new"><img width=11 title="View entry entry in dictionary" src="images/ic_d.gif" align="absmiddle"></a>&nbsp;<span class=smalltext onClick="PickSuggest(\'' + arr[i] + '\');">' + arr[i] + '</span></nobr></td></tr>'
				txt = txt + '<tr class="sc '+cl+'" OnMouseover="hL2(this);" OnMouseOut="dL2(this);" onClick="PickSuggest(\'' + arr[i] + '\');"><td><nobr><a href="http://www.answers.com/'+arr[i]+'" target="_new" title="View definition"><img width=12 src="images/ic_d.gif"></a>&nbsp;<span class=smalltext onClick="PickSuggest(\'' + arr[i] + '\');">' + arr[i] + '</span></nobr></td></tr>'
			}
		}
		var cl = "or";
		if (res == "" && 0)
			txt = txt + '<tr class='+cl+'><td><nobr><span class=smalltext style="color: #777777;">Currently Unavailable...</span></nobr></td></tr>'
		cl = "er";
		var bottom = '';
		cl = "er";
		bottom += '<tr><td height=5></td></tr>';
		bottom += '<tr class='+cl+'><td class=sc OnMouseover="hL2(this);" OnMouseOut="dL2(this);" onClick="SpellAdd();" title="Add this word to your personal dictionary">Add</td></tr>';
		bottom += '<tr class='+cl+'><td class=sc OnMouseover="hL2(this);" OnMouseOut="dL2(this);" onClick="SpellIgnore();" title="Ignore all instances of this word in the text">Ignore</td></tr>';
		bottom += '<tr class='+cl+'><td class=sc OnMouseover="hL2(this);" OnMouseOut="dL2(this);" onClick="GotoNew(\'mypage.asp?action=dict\');" title="Go to the CCSpell Prefs page">CCSpell</td></tr>';
		
		var txt = '<table cellpadding=2 cellspacing=0 width="120" border=0 class="colbdr or">' + txt + bottom + '</table>';
		t.innerHTML = txt;
		//ttHideTimer = setTimeout('HideGlobalTooltip()', 5000);
	}
}

var suggestWhat = null;
function Suggest(what)
{
	clearTimeout(ttHideTimer);
	var word = InnerText(what);
	suggestWhat = what;
	var t = document.getElementById('g_tt');
	if (t == null) return;
	t.innerHTML = "...";
	t.style.left = pX - 10 + "px";
	t.style.top = pY + 5 + "px";
	t.style.display = "block";
	t.style.width = "100px";
	pollServer('worker_spell.asp?action=suggest&word='+word, 'SuggestResponse');
}

function PickSuggest(what)
{
	var word;
	if (what == null)
		word = document.getElementById('spellpick').value
	else
		word = what;
	suggestWhat.innerHTML = word;
	suggestWhat.className = "misDone";
	HideGlobalTooltip();
	var v = document.getElementById(spellAreaName);
	var u = document.getElementById('spellwnd');
	v.value = InnerText(u);
}


function SpellIgnore()
{
	HideGlobalTooltip();
	var parent = suggestWhat.parentNode;
	var word = suggestWhat.innerHTML;
	for (i = 0; i < parent.childNodes.length; i++) {
		var c = parent.childNodes[i];
		if (c.className == 'mis' && c.innerHTML == word)
			c.className = "misDone";
	}
}

function SpellAdd()
{
	var word = suggestWhat.innerHTML;
	pollServer('worker_spell.asp?action=add&word='+word, 'SpellAddResponse');
}

function SpellEdit()
{
	HideGlobalTooltip();
	var word = suggestWhat.innerHTML;
	var dud = prompt('Edit Word', word);
	if (dud != null) {
		suggestWhat.innerHTML = dud;
		suggestWhat.className = "misDone";
	}
}