var spellBase = "/lib/tag/xm/inc/spell/";
var isSpellChecking = false;
var spellDiv,mnuDiv;
function createRequestObject() { var ro; var browser = navigator.appName; if (browser == "Microsoft Internet Explorer") { ro = new ActiveXObject("Microsoft.XMLHTTP");} else { ro = new XMLHttpRequest();};return ro;}
var http = createRequestObject(); 

function checkSpelling(obj) { 
	var str = obj.value; 
	var splBtn = document.getElementById(obj.id+"SpellControl");
	if (!isSpellChecking && http) { 
		splBtn.src = spellBase+"indicator.gif";
		http.open('post', spellBase+'spellcheck.cfm'); 
		http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 
		http.onreadystatechange = function(){handleSpellingResponse(obj);}; 
		isSpellChecking = true; 
		http.send("textstring="+escape(str));
	};
}

var Position =(function(){function resolveObject(s){if(document.getElementById && document.getElementById(s)!=null){return document.getElementById(s);}else if(document.all && document.all[s]!=null){return document.all[s];}else if(document.anchors && document.anchors.length && document.anchors.length>0 && document.anchors[0].x){for(var i=0;i<document.anchors.length;i++){if(document.anchors[i].name==s){return document.anchors[i]}}}}var pos ={};
pos.set = function(o,left,top){if(typeof(o)=="string"){o = resolveObject(o);}if(o==null || !o.style){return false;}o.style.position = "absolute";if(typeof(left)=="object"){var pos = left;left = pos.left;top = pos.top;}o.style.left = left + "px";o.style.top = top + "px";return true;};
pos.get = function(o){var fixBrowserQuirks = true;if(typeof(o)=="string"){o = resolveObject(o);}if(o==null){return null;}var left = 0;var top = 0;var width = 0;var height = 0;var parentNode = null;var offsetParent = null;offsetParent = o.offsetParent;var originalObject = o;var el = o;while(el.parentNode!=null){el = el.parentNode;if(el.offsetParent==null){}else{var considerScroll = true;if(fixBrowserQuirks && window.opera){if(el==originalObject.parentNode || el.nodeName=="TR"){considerScroll = false;}}if(considerScroll){if(el.scrollTop && el.scrollTop>0){top -= el.scrollTop;}if(el.scrollLeft && el.scrollLeft>0){left -= el.scrollLeft;}}}if(el == offsetParent){left += o.offsetLeft;if(el.clientLeft && el.nodeName!="TABLE"){left += el.clientLeft;}top += o.offsetTop;if(el.clientTop && el.nodeName!="TABLE"){top += el.clientTop;}o = el;if(o.offsetParent==null){if(o.offsetLeft){left += o.offsetLeft;}if(o.offsetTop){top += o.offsetTop;}}offsetParent = o.offsetParent;}}if(originalObject.offsetWidth){width = originalObject.offsetWidth;}if(originalObject.offsetHeight){height = originalObject.offsetHeight;}return{'left':left, 'top':top, 'width':width, 'height':height};};
pos.getCenter = function(o){var c = this.get(o);if(c==null){return null;}c.left = c.left +(c.width/2);c.top = c.top +(c.height/2);return c;};return pos;})();


var suggStr = new Object();

function showSugg(splObj) {
	var key = splObj.innerHTML;
	var splMnu = new Array(1);
	for (var s=0; s<suggStr[key].length;s++) {
		splMnu[s] = new Array(suggStr[key][s]);
	}	
	showSpellMenu(splObj, splMnu);
}

function closeSpellCheck(obj) {
	var spellDiv = document.getElementById("splDivLayer"+obj.id);
	if (spellDiv) {
		obj.value = spellDiv.innerHTML.replace(/\<span[^>]*\>(.*?)\<\/span\>/ig,"$1");
		document.body.removeChild(spellDiv);
	}
	
	var splBtn = document.getElementById(obj.id+"SpellControl");
	splBtn.src = spellBase+"spellc.gif";
}

function showSpellMenu(splObj, s) {
	mnuDiv = document.getElementById("cIMMenu");
	if (mnuDiv) {
		document.body.removeChild(mnuDiv);
	}
	
	// create element and insert last into the body
	var d = document.createElement("DIV");
	d.id = "cIMMenu";
	d.className = "Menu";

	//str = "<table border=0>";
	for (i=0; i<s.length; i++) {
		var m = document.createElement("DIV");
		m.className = "MenuItem";
		m.innerHTML = s[i][0];
		m.onclick = function () {splObj.innerHTML = this.innerHTML;splObj.className="spellCorrected";document.body.removeChild(document.getElementById("cIMMenu"));};
		m.onmouseover = function(){this.className="MenuOver"}
		m.onmouseout  = function(){this.className="MenuItem"}
		d.appendChild(m);
	}
	document.body.appendChild(d);
	d.onmouseover = function() {onMenu = 1;if (d.focus) d.focus();};
	d.onmouseout = function() {onMenu = 0;};
	d.onblur = function() {if (onMenu == 0) document.body.removeChild(d);};	
	
	var objRealPos = Position.get(splObj);
	d.style.top = objRealPos["top"]+splObj.offsetHeight+"px";
	d.style.left = objRealPos["left"]+"px";
	if (d.focus) d.focus();
}	

function handleSpellingResponse(obj) {
	if(http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlObj = http.responseXML.documentElement;
			suggStr = new Object();

			//alert(xmlObj.xml);
			var new_string = obj.value;
			var string_length = new_string.length;
			var curr_index = string_length+1;
			var suggest_string = "";
			var start,length,end,suggestions,post,splText;

			var sNodes = xmlObj.childNodes;
			var node;
			for (var i = xmlObj.childNodes.length; i >0; i--) {
				node = xmlObj.childNodes[i-1];
				if (!node.firstChild) continue;
				suggestions = node.firstChild.nodeValue;
				start = parseFloat(node.getAttribute("o"));
				length = parseFloat(node.getAttribute("l"));
				end = start + length;
				post = "";
				splText = new_string.substring(start,end);
				
				var sugArr = suggestions.split("\t");
				suggStr[splText] = new Array(1);
				for (var s=0;s<sugArr.length;s++) {
					suggStr[splText][s] = sugArr[s];
				}
				//alert("Start: "+start+" End: "+end+" Length: "+length);
				if (end < curr_index) post = new_string.substring(end,curr_index);
				suggest_string = "<span class='spellErr' onclick='showSugg(this);'>"+splText+"</span>"+post+suggest_string;
				//alert(suggest_string);
				curr_index = start;
			}
			
			if (curr_index > 0) suggest_string = new_string.substring(0,curr_index)+suggest_string;
			
			//Create a div and place above the textarea
			spellDiv = document.getElementById("splDivLayer"+obj.id);
			if (!spellDiv) {
				spellDiv = document.createElement("div");
				spellDiv.id = "splDivLayer"+obj.id;
				spellDiv.className = "spellLayer";
				spellDiv.setAttribute("title","Click OK to resume editing");
			}

			var objRealPos = Position.get(obj);
			spellDiv.style.width=obj.offsetWidth;
			spellDiv.style.height=obj.offsetHeight;
			spellDiv.style.top=objRealPos["top"];
			spellDiv.style.left=objRealPos["left"];
			document.body.appendChild(spellDiv);
			spellDiv.innerHTML = suggest_string;
			var splBtn = document.getElementById(obj.id+"SpellControl");
			splBtn.src = spellBase+"ok.gif";
			
			if (xmlObj.childNodes.length == 0) {
				alert("No spelling errors found");
				closeSpellCheck(obj);
			}
			isSpellChecking = false;

		}
	}
}