if (typeof console == "undefined") {
this.console = {log: function() {}};
}
function ESpace() {
this.faderBackgroundColor = '#fff';
this.faderBoxShadow = '5px 5px 5px #444';
this.faderBoxShadowIE = 'color=#444444,direction=135';
}
var espace = new ESpace();
function check_usphone(field) {
var element = document.getElementById(field)
var number = element.value
if (number == '') {
return true
}
if (!number.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ x0-9]*$/)) {
element.focus()
alert('Phone number format is (www) xxx-yyyy zzz')
return false
}
else {
return true
}
} 
function check_url(field) {
var element = document.getElementById(field)
var url = element.value
if (url == '') {
return true
}
if ((url.substring(0, 7) != 'http://') &&
(url.substring(0, 8) != 'https://') &&
(url.substring(0, 1) != '/')) {
element.focus()
alert('The web address must start with http://, https:// or just /')
return false
}
else {
return true
}
}
function _toggle(id, separator, force_hidden) {
var el = $(id);
var ieframe = $('ie' + id)
if ((force_hidden === true) || (el.style.visibility == 'visible')) {
el.style.visibility = 'hidden'
ieframe.style.display = 'none'
}
else {
var input = $(id + '_values')
var coords = Position.cumulativeOffset(input)
var fleft = coords[0] - 28
var ftop = coords[1] - 24
el.style.top=(ftop) + 'px'
el.style.left=fleft + 'px'
el.style.visibility = 'visible'
ieframe.style.width = el.offsetWidth
ieframe.style.height = el.offsetHeight
ieframe.style.top = el.style.top
ieframe.style.left = el.style.left
ieframe.style.zIndex = el.style.zIndex - 1
ieframe.style.display = 'block'
}
updateList(id, separator)
return(false)
}
function checkem(name, bool) {
var count = 0
var id = name + count
var el = $(id)
while (el != null) {
el.checked = bool
count++
id = name + count
el = $(id)
}
return(false)
}
function updateList(name, separator) {
var count = 0
var values = ''
var string = ''
var id = name + '0'
var el = $(id)
while (el != null) {
if (el.checked) {
string += (string != '' ? separator : '') + el.getAttribute('xstring')
}
count++
id = name + count
el = $(id)
}
var input_id = name + '_values'
el = $(input_id)
el.value = string
el.title = string
}
var ua = navigator.userAgent.toLowerCase();
var isSafari = (ua.indexOf('safari') != - 1);
var isMac = (ua.indexOf('maci') != -1);
var isIE = (ua.indexOf('msie') != - 1);
var isIE7 = (ua.indexOf('msie 7.') != - 1);
var isMacIE = isIE && isMac;
function getRealTop(el) {
var yPos = el.offsetTop - (el.scrollTop ? el.scrollTop : 0) + (isSafari || isMacIE ? 0 : getScrollTop())
tempEl = el.offsetParent
while (tempEl != null) {
yPos += tempEl.offsetTop - (tempEl.parentNode.scrollTop == null ? 0 : tempEl.parentNode.scrollTop)
tempEl = tempEl.offsetParent
}
return(yPos)
}
function getRealLeft(el) {
var xPos = el.offsetLeft
tempEl = el.offsetParent
while (tempEl != null) {
xPos += tempEl.offsetLeft
tempEl = tempEl.offsetParent
}
return(xPos)
}
function getScrollTop() {
var y
if (self.pageYOffset) // all except Explorer
{
y = self.pageYOffset;
}
else if (document.documentElement && document.documentElement.scrollTop)
{
y = document.documentElement.scrollTop;
}
else if (document.body) // all other Explorers
{
y = document.body.scrollTop;
}
return(y)
}
/* Cookie functions from www.netspade.com/articles/2005/11/16/javascript-cookies/ */
/*
* Sets a Cookie with the given name and value.
*
* name Name of the cookie
* value Value of the cookie
* [expires] Days to expire the cookie (default: end of current session)
* [path] Path where the cookie is valid (default: path of calling document)
* [domain] Domain where the cookie is valid
* (default: domain of calling document)
* [secure] Boolean value indicating if the cookie transmission requires a
* secure transmission
*/
function setCookie(name, value, expires, path, domain, secure) {
var today = new Date();
today.setTime(today.getTime());
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
var expires_date = new Date(today.getTime() + expires);
var str = expires_date.toGMTString();
var expires_str = str.substr(0, 7) + '-' + str.substr(8, 3) + '-' + str.substr(12);
}
cookie = name + "=" + escape(value) +
((expires_date) ? ";expires=" + expires_str : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
document.cookie = cookie;
}
/*
* Gets the value of the specified cookie.
*
* name Name of the desired cookie.
*
* Returns a string containing value of specified cookie,
* or null if cookie does not exist.
*/
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
/*
* Deletes the specified cookie.
*
* name name of the cookie
* [path] path of the cookie (must be same as path used to create cookie)
* [domain] domain of the cookie (must be same as domain used to create cookie)
*/
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
// Display a message if not and return false
function cknum(id, min, max, name) {
var field = document.getElementById(id)
var val = field.value
if (val == '') {
val = 0
}
if (!isInt(val)) {
alert(name + ' must be a whole number between ' + min + ' and ' + max)
field.focus()
return(true)
}
if (val < min) {
alert(name + ' must be at least ' + min)
field.focus()
return(true)
}
if (val > max) {
alert(name + ' must be less than or equal to ' + max)
field.focus()
return(true)
}
return(false)
}
function isInt(str) {
var i = parseInt(str)
if (isNaN(i)) {
return(false)
}
i = i.toString ()
if (i != str)
return(false)
return(true)
}
// Display a message if not and return false
function ckdec(id, min, max, name) {
var field = document.getElementById(id)
var num = field.value.replace('$', '').replace(/,/g, '');
if (num == '') {
num = 0;
}
if (!isFloat(num)) {
alert(name + ' must be a number between ' + min + ' and ' + max)
field.focus()
return(true)
}
if (num < min) {
alert(name + ' must be at least ' + min)
field.focus()
return(true)
}
if (num > max) {
alert(name + ' must be less than or equal to ' + max)
field.focus()
return(true)
}
return(false)
}
// Can include a leading dollar sign
function isFloat(str) {
return(str == String(str).match(/[+-]{0,1}[$]{0,1}\d*\.{0,1}\d*/)[0]);
}
// DD, MMxDD, MMxDDxYY, MMxDDxYYYY, YYYYxMMxDD, TOD(AY), 
// where x is any non-numeric, printable character
// otherwise an empty field is OK
function checkDate(field, required) {
return(checkDateLocale(field, required, 'middle', '/'));
}
// separator: '.' or '/' or '-' or...
function checkDateLocale(field, required, endian, separator) {
err = '';
val = field.value.toUpperCase();
today = new Date();
if ((val == '') && (required != true)) {
return true;
}
if ((val.length >= 3) && (val.substring(0, 3) == 'TOD')) {
day = today.getDate();
month = today.getMonth() + 1;
year = today.getYear();
if (year < 1900) {
year += 1900;
}
}
else if ((val.length >= 3) && (val.substring(0, 3) == 'TOM')) {
date = new Date(today.getTime() + 86400000);
day = date.getDate();
month = date.getMonth() + 1;
year = date.getYear();
if (year < 1900) {
year += 1900;
}
}
else if (val.substring(0, 1) == 'Y') {
date = new Date(today.getTime() - 86400000);
day = date.getDate();
month = date.getMonth() + 1;
year = date.getYear();
if (year < 1900) {
year += 1900;
}
}
else {
pos = 0;
a = '';
ch = val.substring(pos, pos+1);
while ((ch >= '0') & (ch <= '9')) {
a = a + ch;
pos++;
ch = val.substring(pos, pos+1);
}
pos++;
b = '';
ch = val.substring(pos, pos+1);
while ((ch >= '0') & (ch <= '9')) {
b = b + ch;
pos++;
ch = val.substring(pos, pos+1);
}
pos++;
c = '';
ch = val.substring(pos, pos+1);
while ((ch >= '0') & (ch <= '9')) {
c = c + ch;
pos++;
ch = val.substring(pos, pos+1)
}
if ((a != '') && (b == '') && (c == '')) {
year = -1;
day = parseInt(a, 10);
month = today.getMonth() + 1;
}
else if ((a != '') && (b != '') && (c == '')) {
year = -1;
if (endian == 'little') {
day = parseInt(a, 10);
month = parseInt(b, 10);
}
else {
month = parseInt(a, 10);
day = parseInt(b, 10);
}
}
else if ((a != '') && (b != '') && (c != '')) {
if (endian == 'little') {
day = parseInt(a, 10);
month = parseInt(b, 10);
year = parseInt(c, 10);
}
else if (endian == 'middle') {
month = parseInt(a, 10);
day = parseInt(b, 10);
year = parseInt(c, 10);
}
else {
year = parseInt(a, 10);
month = parseInt(b, 10);
day = parseInt(c, 10);
}
}
}
if (year == -1) {
year = today.getYear();
if (year < 1900) {
year += 1900;
}
}
if (year < 100) {
if (year < 50) {
year = 2000 + year;
}
else {
year = 1900 + year;
}
}
if (month < 1 || month > 12)
err = 'Bad month';
if (day < 1 || day > 31)
err = 'Bad day';
if (year < 0)
err = 'Bad year';
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day == 31)
err = 'Bad day';
}
if (month == 2) {
var g = parseInt(year / 4);
if (isNaN(g)) {
err = 'Bad year';
}
if (day > 29)
err = 'Bad day';
if (day == 29 && ((year / 4) != parseInt(year / 4)))
err = 'Bad day';
}
if (year < 999)
err = 'Bad year';
if (year > 9999)
err = 'Bad year';
if (err != '') {
if (val == '')
alert('A date is required')
else
alert('Invalid date: ' + err)
return false
}
else {
if (month < 10) {
month = '0' + month;
}
if (day < 10) {
day = '0' + day;
}
if (endian == 'middle')
field.value = month + separator + day + separator + year;
else if (endian == 'big')
field.value = year + separator + month + separator + day;
else
field.value = day + separator + month + separator + year;
return true
}
}
// H H:M H:Ma H:Mp Noon Midnight NOW
// otherwise an empty field is OK
function checkTime(field, required) {
err = ''
val = field.value.toLowerCase()
if ((val == '') && (required != true))
return true
if (val.substr(0, 3) == 'now') {
d = new Date()
min = d.getMinutes()
if (min < 10) {
min = '0' + min
}
hour = d.getHours()
val = hour + ':' + min
if (hour > 11)
val = val + " pm"
else
val = val + " am"
}
if (val.substring(0, 1) == 'm') {
hour = '12'
minute = '00'
aorp = 'a'
}
else if (val.substring(0, 1) == 'n') {
hour = '12'
minute = '00'
aorp = 'p'
} else {
pos = 0 
hour = ''
ch = val.substring(pos, pos+1)
while ((ch >= '0') & (ch <= '9')) {
hour = hour + ch
pos += 1 
ch = val.substring(pos, pos+1)
}
if ((ch != 'a') && (ch != 'p'))
pos += 1
minute = ''
ch = val.substring(pos, pos+1)
while ((ch >= '0') & (ch <= '9')) {
minute = minute + ch
pos += 1 
ch = val.substring(pos, pos+1)
}
if (minute == '')
minute = '00'
if (minute.length == 1)
minute = minute + '0'
aorp = ''
ch = val.substring(pos, pos+1)
while ((pos <= val.length) && (aorp == '')) {
pos += 1
if ((ch == 'a') || (ch == 'p'))
aorp = ch
ch = val.substring(pos, pos+1)
}
if (aorp == '') {
if ((hour >= 7) && (hour <= 11))
aorp = 'a'
else
aorp = 'p'
}
}
if (hour == 24) {
hour = 12
aorp = 'a'
}
if (hour > 12) {
hour = hour - 12
aorp = 'p'
}
if (hour < 1 || hour > 24)
err = 'Bad hour'
if (minute < 0 || minute > 59)
err = 'Bad minute'
if (err != '') {
if (val == '')
alert('Time Required')
else {
if (confirm('Invalid Time.\n\nClick OK for help\nor Cancel to fix the date.'))
alert('Times may be entered in any of the following formats:\n\nHH, HH:MM, HH:MMa, HH:MMp, Noon, Midnight, NOW')
}
field.focus()
return false
}
else {
field.value = hour + ':' + minute + ' ' + aorp + 'm'
return true
}
}
function tasize(field, max) {
if (field.value.length > max) {
field.value = field.value.substr(0, max)
return false }
else
return true
}
// This checks to make sure the setTimeout() calls don't cascade (why do they?!?)
var next_keep_alive = 0
function keepalive() {
var now = Date.parse(Date())
if (now > next_keep_alive) {
var offset = 1 * 60 * 1000
setTimeout('keepalive()', offset)
next_keep_alive = now + (offset * 0.95)
document.getElementById('keepalive').src = '/jslib/keepalive.php?' + next_keep_alive
}
}
function get_select_value(select) {
return(select.options[select.selectedIndex].value)
}
// Select the first one if the value was not found
function set_select_value(select, value) {
var options = select.options
var set = false
for (var i=options.length-1; i>=0; i--) {
if (options[i].value == value) {
options[i].selected = true
set = true
}
}
if (!set) {
options[0].selected = true
}
}
// http://gmurphey.com/
// http://creativecommons.org/licenses/by-sa/2.5/
function align(e, position) {
e = $(e);
var container = Try.these(
function () { return e.parentNode },
function () { return e.parentElement },
function () { return false });
if (container != false) {
var dimensions = Element.getDimensions(e);
// e.removeAttribute('style');
Element.setStyle(container, { position: 'relative' });
for (var name in position) {
if (name == 'hAlign') {
switch (position[name]) {
case 'left':Element.setStyle(e, { 
position: 'absolute', 
left: 0 
});
break;
case 'center':Element.setStyle(e, { 
position: 'absolute', 
left: '50%', 
marginLeft: ((dimensions.width / 2) * -1) + 'px' 
});
break;
case 'middle':Element.setStyle(e, { 
position: 'absolute', 
left: '50%', 
marginLeft: ((dimensions.width / 2) * -1) + 'px' 
});
break;
case 'right':Element.setStyle(e, { 
position: 'absolute', 
right: 0 
});
break;
}
} else if (name == 'vAlign') {
switch (position[name]) {
case 'top':Element.setStyle(e, { 
position: 'absolute', 
top: 0 
});
break;
case 'center':Element.setStyle(e, { 
position: 'absolute', 
top: '50%', 
marginTop: ((dimensions.height / 2) * -1) + 'px' 
});
break;
case 'middle':Element.setStyle(e, { 
position: 'absolute', 
top: '50%', 
marginTop: ((dimensions.height / 2) * -1) + 'px' 
});
break;
case 'bottom':Element.setStyle(e, { 
position: 'absolute', 
bottom: 0 
});
break;
}
}
}
}
return e;
}
function exportTable(id, header) {
document.getElementById('table_html').value = document.getElementById(id).innerHTML
if (header) {
document.getElementById('header_html').value = document.getElementById(header).innerHTML
}
document.getElementById('myForm').submit()
}
function show_popup(id) {
var el = document.getElementById(id);
/* Advanced styles */
el.style.MozBorderRadius = '8px';
el.style.borderRadius = '8px';
el.style.webkitBorderRadius = '8px';
el.style.MozBoxShadow = espace.faderBoxShadow; // '5px 5px 5px #444';
el.style.webkitBoxShadow = espace.faderBoxShadow; // '5px 5px 5px #444';
el.style.boxShadow = espace.faderBoxShadow; // '5px 5px 5px #444';
el.style.filter = 'progid:DXImageTransform.Microsoft.Shadow(' + espace.faderBoxShadowIE + ')'; // color=#444444,direction=135)';
el.style.zIndex = 100;
/* Title... if it exists */
if (el.childNodes[1]) {
el.childNodes[1].style.borderRadius = '7px 7px 0 0';
el.childNodes[1].style.MozBorderRadius = '7px 7px 0 0';
el.childNodes[1].style.webkitBorderRadius = '7px 7px 0 0';
}
var width = 0;
if (typeof(window.innerWidth) == 'number') {
width = window.innerWidth;
}
else if (document.documentElement && (document.documentElement.clientWidth)) {
width = document.documentElement.clientWidth;
}
else if (document.body && (document.body.clientWidth)) {
width = document.body.clientWidth;
}
el.style.display = 'block';
var w = 0;
if (el.style.pixelWidth) {
w = el.style.pixelWidth;
}
else {
w = el.offsetWidth;
}
el.style.display = 'none';
var left = (width - w) / 2;
if ((el.style.width != '80%') && (left < 50) && (width > 400)) {
el.style.width = '80%';
return(show_popup(id));
}
// On Macs, if the popup has Flash, the Flash itself is draggable (you can't click in it?!?)
if (!isMac) {
try {new Draggable(id) } catch (err) { }
}
el.style.left = left + 'px';
fade_page();
setTimeout("var inputs = $$('#" + id + " input'); if (inputs.length > 0) { try { inputs[0].focus() } catch(err) {} }", 250);
$(id).fire('espace:aboutToOpen');
try {
Effect.Appear(el, {duration: 0.2});
}
catch (err) {
el.style.display = 'block';
}
setTimeout("document.getElementById('" + id + "').style.display = 'block'", 200);
return(false);
}
function window_size() {
var size = new Object();
if (typeof(window.innerWidth) == 'number') {
size.width = window.innerWidth;
size.height = window.innerHeight;
}
else if (document.documentElement && (document.documentElement.clientWidth)) {
size.width = document.documentElement.clientWidth;
size.height = document.documentElement.clientHeight;
}
else if (document.body && (document.body.clientWidth)) {
size.width = document.body.clientWidth;
size.height = document.body.clientHeight;
}
return(size);
}
// The DIV should have CLASS=POPUP (which sets the z-index, position, display, etc.
function hide_popup(id) {
$(id).fire('espace:aboutToClose');
document.getElementById(id).style.display = 'none';
unfade_page();
}
function fade_page() {
var fader = document.getElementById('x_fade_page');
if (!fader) {
fader = document.createElement('div');
fader.id = 'x_fade_page';
fader.style.display = 'none';
document.body.appendChild(fader);
}
fader.style.display = 'block';
fader.style.zIndex = 99
fader.style.position = 'fixed';
fader.style.top = 0;
fader.style.left = 0;
fader.style.height = '100%';
fader.style.width = '100%';
fader.style.backgroundColor = espace.faderBackgroundColor; // '#fff';
fader.style.opacity = '0.80';
fader.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity=80)";
return(false);
}
function unfade_page() {
var fader = document.getElementById('x_fade_page');
fader.style.display = 'none';
return(false);
}
function getScrollXY() {
var scrOfX = 0, scrOfY = 0;
if (typeof(window.pageYOffset) == 'number') {
scrOfY = window.pageYOffset;
scrOfX = window.pageXOffset;
}
else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
scrOfY = document.body.scrollTop;
scrOfX = document.body.scrollLeft;
} 
else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
scrOfY = document.documentElement.scrollTop;
scrOfX = document.documentElement.scrollLeft;
}
return(new Array(scrOfX, scrOfY));
}
function enterToTab() {
$$('input').each(function(input) {
Event.observe(input, 'keydown', enter2Tab);
} )
$$('select').each(function(input) {
Event.observe(input, 'keydown', enter2Tab);
} )
}
function enter2Tab(e) {
if (e.keyCode == 13) {
Event.stop(e);
var all = Form.getElements('myForm');
var found = false;
for (var i=0; i<all.length; i++) {
if (all[i].id == this.id) {
found = true;
}
if (found) {
if (all[i+1].type != 'hidden') {
all[i+1].focus();
all[i+1].select();
return(false);
}
}
}
for (var i=0; i<all.length; i++) {
if (all[i].type != 'hidden') {
all[i].focus();
all[i].select();
return(false);
}
}
return(false);
}
}
function autosize_ta(ta, min) {
var t = document.getElementById(ta);
var s = t.value;
if (s) {
var h = 1;
var l = 0;
while (l > -1) {
l = s.indexOf("\n", l+1);
h++;
}
var o = Math.round(s.length / (t.cols-1));
var c = (h > o ? h : o);
if (c < min) {
c = min;
}
t.rows = c;
}
}
function toggle_display(id, if_blank) {
var el = document.getElementById(id);
var disp = el.style.display;
if (disp == '') {
el.style.display = (if_blank ? if_blank : 'block');
}
else {
el.style.display = (disp == 'none' ? 'block' : 'none');
}
return(false);
}
function twistee(triangle, content) {
if ($(content).style.display == 'none') {
$(content).style.display = 'block';
triangle.innerHTML = '&#9660;';
}
else {
$(content).style.display = 'none';
triangle.innerHTML = '&#9658;';
}
return(false);
}
var last_selected = new Array();
function master_changed(php, ajax, master, slave, selected, direction) {
$(slave + '_busy').show();
if (!selected) {
selected = last_selected[slave];
}
else {
last_selected[slave] = selected;
}
var parms = new Object();
parms['id'] = $F(master);
parms['_ajax'] = ajax;
parms['all'] = Form.serialize('myForm');
if (direction == 'next') {
parms['d'] = 'next';
}
else if (direction == 'prev') {
parms['d'] = 'prev';
}
new Ajax.Request(php, {parameters: parms,
onSuccess: function(resp, values) {
var select = $(slave);
select.length = 0;
for(index in values) {
opt = new Option(values[index], index);
if (index == '-prev-') {
opt.style.fontStyle = 'italic';
opt.style.color = '#888';
}
else if (index == '-next-') {
opt.style.fontStyle = 'italic';
opt.style.color = '#888';
}
select.options[select.options.length] = opt;
if (selected && (index == selected)) {
opt.selected = true;
}
}
$(slave + '_busy').hide();
Event.fire(slave, 'my:change');
},
onFailure: function() {
alert('A problem occurred trying to load the "' + slave + '" drop-down list');
$(slave + '_busy').hide();
}
} );
}
function validate_email(id) {
var field = document.getElementById(id)
var val = field.value
var v=true
var l=val.length
if (l==0) return(true)
if (l<6) v=false
if (val.lastIndexOf(' ')>0) v=false
var dot=val.lastIndexOf('.')
if (dot==-1 || l-dot>5) v=false
var at=val.lastIndexOf('@')
if (at==-1 || l-at<5) v=false
if (!v) {
alert('A valid e-mail address is required.\nFor example, jdoe@acme.com')
field.focus()
}
return(v);
};
function addSpellIcon(id, url) {
var el = $(id);
var t = getRealTop(el);
var l = getRealLeft(el);
var h = el.getHeight();
var w = el.getWidth();
var icon = document.createElement('img');
icon.src = '/jslib/images/spellcheck.gif';
icon.title = 'Spell check';
icon.style.position = 'absolute';
icon.style.display = 'block';
icon.style.width = '16px';
icon.style.height = '16px';
icon.style.top = (t + h - 17) + 'px';
icon.style.left = (l + w - 17) + 'px';
icon.style.backgroundColor = '#fff';
icon.onclick = function() { spellCheck(this, id, url) }
document.body.appendChild(icon);
}
function spellCheck(icon, id, url) {
var original_source = icon.src;
icon.src = '/jslib/images/small_roller.gif';
var el = $(id);
el.setAttribute('spellcheck', false);
var parms = new Object();
parms['html_content'] = el.value;
new Ajax.Request(url, {
method: 'post',
parameters: parms,
onFailure: function(t) { alert('Something went wrong...\n' + t.status + ': ' + t.statusText) },
onSuccess: function(t, words) {
if (words.length == 0) {
info('No misspellings were found');
}
else {
fixWords(el, words, url);
}
},
onComplete: function() { icon.src = original_source }
});
return(false);
}
function fixWords(el, words, url) {
if ($('speller-list')) {
closeSpeller(el);
}
var id = el.getAttribute('id');
var pos = el.cumulativeOffset();
var t = pos.top;
var l = pos.left;
var dims = el.getDimensions();
var h = dims.height;
var w = dims.width;
var speller = document.createElement('div');
speller.id = 'speller-list';
speller.style.backgroundColor = '#ffffff';
speller.style.width = (w-1) + 'px';
speller.style.height = '68px';
speller.style.overflow = 'scroll';
speller.style.top = (t + h) + 'px';
speller.style.left = l + 'px';
speller.style.border = '1px solid #aaa';
speller.style.fontSize = '11px';
speller.style.position = 'absolute';
document.body.appendChild(speller);
var cancel = document.createElement('div');
cancel.id = 'speller-list-cancel';
cancel.onclick = function() { closeSpeller(el) }
cancel.style.height = '11px';
cancel.style.width = '12px';
cancel.style.backgroundImage = 'url(/jslib/images/cancel.gif)';
cancel.style.position = 'absolute';
cancel.style.left = (w-14+l) + 'px';
cancel.style.top = (68-13+t+h) + 'px';
cancel.style.cursor = 'pointer';
cancel.title = 'Close';
document.body.appendChild(cancel);
var html = '';
var list = new Array();
for(var misspelled in words) {
if (html == '') {
selectWord(el, misspelled);
}
html += '<div id="fix-' + misspelled + '" onmouseover="return selectWord(\'' + id + '\', \'' + misspelled + '\')">' + 
'<b><a href="" onclick="return replaceWord(this, \'' + id + '\', \'' + misspelled + '\', \'' + url + '\')">' + misspelled + '</a></b>:';
list[list.length] = misspelled;
words[misspelled].split(',').each(function(w) {
html += ' <a href="" onclick="return replaceWord(this, \'' + id + '\', \'' + misspelled + '\')">' + w + '</a>';
});
html += '</div>';
}
speller.innerHTML = html;
el.observe('keyup', function() {
list.each(function(word) {
if (el.value.indexOf(word) < 0) {
var div = $('fix-' + word);
if (div) {
div.remove();
var remaining = $('speller-list').childElements().toArray();
if (remaining.length == 0) {
closeSpeller(el);
}
}
}
});
});
}
function replaceWord(link, id, bad, url) {
var el = $(id);
var text = el.value;
var good = link.innerHTML;
el.value = text.replace(new RegExp(bad, 'g'), good);
$('fix-' + bad).remove();
if (url && (good == bad)) {
var parms = new Object();
parms['add'] = good;
new Ajax.Request(url, {
method: 'post',
parameters: parms,
onFailure: function(t) { alert('Something went wrong...\n' + t.status + ': ' + t.statusText) },
onSuccess: function(t) { info(good + ' was added to your dictionary') }
});
}
var remaining = $('speller-list').childElements().toArray();
if (remaining.length == 0) {
closeSpeller(el);
return(false);
}
var next = remaining[0].getAttribute('id').substr(4);
selectWord(el, next);
return(false);
}
function selectWord(textArea, word) {
var string = $(textArea).value.toLowerCase();
var start = string.indexOf(word.toLowerCase());
if (start > -1) {
selectRange(textArea, start, start+word.length);
}
}
function selectRange(textArea, startCharNo, endCharNo) {
var element = $(textArea);
element.focus();
if (element.setSelectionRange) {
element.setSelectionRange(startCharNo, endCharNo);
}
else {
var r = element.createTextRange();
r.collapse(true);
r.moveEnd('character', endCharNo);
r.moveStart('character', startCharNo);
r.select(); 
}
}
function closeSpeller(el) {
$('speller-list').remove();
$('speller-list-cancel').remove();
el.stopObserving('keyup');
}

