﻿if (document.onclick) { var old_onclick = document.onclick; document.onclick = function(e) { checkClickLocation(e); old_onclick(e);}
} else { document.onclick = checkClickLocation;}
var objToCheck; var spellingResultsDiv = null; var spellingSuggestionsDiv = null; var spellUrl = 'SELF'; function setObjToCheck(id){ objToCheck = document.getElementById(id);}
function setSpellUrl(s) { spellUrl = s;}
function spellCheck_cb(new_data) { var isThereAMisspelling = new_data.charAt(0); new_data = new_data.substring(1); if (spellingResultsDiv) { spellingResultsDiv.parentNode.removeChild(spellingResultsDiv);}
spellingResultsDiv = document.createElement('DIV'); spellingResultsDiv.className = 'edit_box'; spellingResultsDiv.style.width = objToCheck.style.width; spellingResultsDiv.style.height = objToCheck.style.height; spellingResultsDiv.innerHTML = new_data; objToCheck.style.display = "none"; objToCheck.parentNode.insertBefore(spellingResultsDiv,objToCheck); document.getElementById("status").innerHTML = ""; document.getElementById("action").innerHTML = "<a class=\"resume_editing\" onClick=\"resumeEditing();\">Resume Editing</a>"; if(isThereAMisspelling != "1"){ document.getElementById("status").innerHTML = "No Misspellings Found"; objToCheck.disabled = false;}
}
function spellCheck() { var query; if (spellingResultsDiv) { spellingResultsDiv.parentNode.removeChild(spellingResultsDiv); spellingResultsDiv = null;}
document.getElementById("action").innerHTML = "<a class=\"check_spelling\">Check Spelling &amp; Preview</a>"; document.getElementById("status").innerHTML = "Checking..."; query = objToCheck.value; cpaint_call(spellUrl, 'POST', 'spellCheck', query, spellCheck_cb);}
function checkClickLocation(e){ if (spellingSuggestionsDiv) { if (spellingSuggestionsDiv.ignoreNextClick) { spellingSuggestionsDiv.ignoreNextClick = false;}
else { var theTarget = getTarget(e); if (theTarget != spellingSuggestionsDiv){ spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv); spellingSuggestionsDiv = null;}
}
}
return true;}
function getTarget(e){ var value; if (checkBrowser() == "ie"){ value = window.event.srcElement;}
else{ value = e.target;}
return value;}
function checkBrowser(){ var theAgent = navigator.userAgent.toLowerCase(); if(theAgent.indexOf("msie") != -1){ if(theAgent.indexOf("opera") != -1){ return "opera";}
else{ return "ie";}
}
else if(theAgent.indexOf("netscape") != -1){ return "netscape";}
else if(theAgent.indexOf("firefox") != -1){ return "firefox";}
else if(theAgent.indexOf("mozilla/5.0") != -1){ return "mozilla";}
else if(theAgent.indexOf("\/") != -1){ if (theAgent.substr(0,theAgent.indexOf('\/')) != 'mozilla'){ return navigator.userAgent.substr(0,theAgent.indexOf('\/'));}
else{ return "netscape";}
}
else if(theAgent.indexOf(' ') != -1){ return navigator.userAgent.substr(0,theAgent.indexOf(' '));}
else{ return navigator.userAgent;}
}
function showSuggestions_cb(new_data){ spellingSuggestionsDiv.innerHTML = new_data; spellingSuggestionsDiv.style.display = 'block'; document.getElementById("status").innerHTML = "";}
function showSuggestions(word, id) { document.getElementById("status").innerHTML = "Searching..."; var x = findPosX(id); var y = findPosY(id); var scrollPos = 0; if (checkBrowser() != "ie") { scrollPos = spellingResultsDiv.scrollTop;}
if (spellingSuggestionsDiv) { spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv);}
spellingSuggestionsDiv = document.createElement('DIV'); spellingSuggestionsDiv.style.display = "none"; spellingSuggestionsDiv.className = 'suggestion_box'; spellingSuggestionsDiv.style.position = 'absolute'; spellingSuggestionsDiv.style.left = x + 'px'; spellingSuggestionsDiv.style.top = (y+16-scrollPos) + 'px'; spellingSuggestionsDiv.ignoreNextClick = true; document.body.appendChild(spellingSuggestionsDiv); cpaint_call(spellUrl, 'POST', 'showSuggestions', word, id, showSuggestions_cb);}
function replaceWord(id, newWord){ document.getElementById(id).innerHTML = trim(newWord); if (spellingSuggestionsDiv) { spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv); spellingSuggestionsDiv = null;}
document.getElementById(id).style.color = "#005500";}
function switchText() { var text = spellingResultsDiv.innerHTML; text = text.replace(/<br *\/?>/gi, "~~~"); cpaint_call(spellUrl, 'POST', 'switchText', text, switchText_cb);}
function switchText_cb(new_string) { new_string = new_string.replace(/~~~/gi, "\n"); objToCheck.style.display = "none"; objToCheck.value = new_string; objToCheck.disabled = false; if (spellingResultsDiv) { spellingResultsDiv.parentNode.removeChild(spellingResultsDiv); spellingResultsDiv = null;}
objToCheck.style.display = "block"; resetAction();}
function resumeEditing() { document.getElementById("action").innerHTML = "<a class=\"resume_editing\">Resume Editing</a>"; document.getElementById("status").innerHTML = "Working..."; if (spellingSuggestionsDiv) { spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv); spellingSuggestionsDiv = null;}
switchText();}
function resetAction() { document.getElementById("action").innerHTML = "<a class=\"check_spelling\" onClick=\"spellCheck();\">Check Spelling &amp; Preview</a>"; document.getElementById("status").innerHTML = "";}
function resetSpellChecker() { resetAction(); objToCheck.value = ""; objToCheck.style.display = "block"; objToCheck.disabled = false; if (spellingResultsDiv) { spellingResultsDiv.parentNode.removeChild(spellingResultsDiv); spellingResultsDiv = null;}
if (spellingSuggestionsDiv) { spellingSuggestionsDiv.parentNode.removeChild(spellingSuggestionsDiv); spellingSuggestionsDiv = null;}
document.getElementById("status").style.display = "none";}
function findPosX(object){ var curleft = 0; var obj = document.getElementById(object); if (obj.offsetParent){ while (obj.offsetParent){ curleft += obj.offsetLeft - obj.scrollLeft; obj = obj.offsetParent;}
}
else if (obj.x){ curleft += obj.x;}
return curleft;}
function findPosY(object){ var curtop = 0;var curtop = 0; var obj = document.getElementById(object); if (obj.offsetParent){ while (obj.offsetParent){ curtop += obj.offsetTop - obj.scrollTop; obj = obj.offsetParent;}
}
else if (obj.y){ curtop += obj.y;}
return curtop;}
function trim(s) { while (s.substring(0,1) == ' ') { s = s.substring(1,s.length);}
while (s.substring(s.length-1,s.length) == ' ') { s = s.substring(0,s.length-1);}
return s;}
