var schk_active=0;var schk_button;var schk_button_text='  Spellcheck  ';var schk_orig_text='';var schk_orig_text_div;var schk_orig_display;var schk_text_div;var schk_data;var schk_words;var schk_menu;var schk_modified=0;var schk_error='';var schk_bubble;var schk_timeout_id;var schk_green='#009900';var schk_red='#ee0000';var schk_blue='#0000ee';var schk_edit_word_idx=-1;var prev_props=[];function spellcheck_do(el_button,el_name){if(schk_active){spellcheck_finish();return;}schk_button_text=el_button.value;if(spellcheck_init(el_name)){schk_button=el_button;el_button.value='Resume editing';schk_active=1;}else
if(!schk_error){spellcheck_temp_bubble(el_name,'No spelling errors found!');}}function spellcheck_remove_bubble(){if(schk_timeout_id){window.clearTimeout(schk_timeout_id);schk_timeout_id=0;}if(!schk_bubble)return;schk_bubble.parentNode.removeChild(schk_bubble);schk_bubble=undefined;}function spellcheck_temp_bubble(el_name,txt){spellcheck_remove_bubble();var elm=gk_get_el(el_name);if(!elm)return;var x=gk_get_absolute_pos_left(elm);var y=gk_get_absolute_pos_top(elm);schk_bubble=document.createElement('div');var ms=schk_bubble.style;spellcheck_copy_appearance(elm,schk_bubble);ms.position='absolute';ms.zIndex='100';ms.display='block';ms.border='1px solid #000000';ms.backgroundColor='#ffffdd';ms.padding='10px';ms.cursor='default';ms.color='#000000';ms.textDecoration='none';ms.left=x+'px';ms.top=y+'px';ms.visibility='hidden';schk_bubble.innerHTML=txt;document.body.appendChild(schk_bubble);x=x+Math.floor((elm.offsetWidth-schk_bubble.offsetWidth)/2);y=y+Math.floor((elm.offsetHeight-schk_bubble.offsetHeight)/2);schk_bubble.style.left=x+'px';schk_bubble.style.top=y+'px';ms.visibility='visible';schk_bubble.onmousemove=spellcheck_remove_bubble;gui_display_shadow(schk_bubble);schk_timeout_id=window.setTimeout('spellcheck_remove_bubble();',3000);}function spellcheck_save_word_edit(){if(schk_edit_word_idx<0)return 0;var ew=gk_get_el('spellcheck_word_edit');if(!ew)return;var o=schk_data[schk_edit_word_idx];if((typeof o)!='object')return;var new_word=ew.value;if(new_word!=''&&!new_word)return;if(new_word==spellcheck_get_word_shown(schk_edit_word_idx))return;if((typeof o.pw)!='string')o.pw=o.w;o.w=new_word;if(schk_words[o.w]){schk_words[o.w].ci=-1;}spellcheck_save_modified_text();}function spellcheck_finish_word_edit(){if(schk_edit_word_idx<0)return 0;spellcheck_save_word_edit();schk_edit_word_idx=-1;spellcheck_render();return 1;}function spellcheck_evt_click(evt){if(!evt)var evt=window.event;if(schk_menu){spellcheck_remove_menu();return;}if(spellcheck_finish_word_edit()){return;}var t='';t+='<span style="color: '+schk_blue+'; cursor: pointer; text-decoration: underline;" onClick="spellcheck_finish()">Resume editing&hellip;</span><br>';var x=gk_mouse_x(evt)+3;var y=gk_mouse_y(evt)-1;spellcheck_display_menu(x,y,t);}function spellcheck_copy_appearance(src,dst){var props=Array('fontSize','fontFamily');for(var ii=0;ii<props.length;ii++){var x=props[ii];var ss=gk_get_style(src,x);if(!ss)continue;dst.style[x]=ss;}}function spellcheck_init(el_name){var td=gk_get_el(el_name);if(!td)return 0;schk_orig_text_div=td;schk_orig_text=td.value;schk_modified=0;var parent_div=td.parentNode;if(!parent_div)return 0;if(!spellcheck_process_text())return 0;var txt_w=td.offsetWidth-4;var txt_h=td.offsetHeight-4;var nd=document.createElement('div');schk_text_div=nd;nd.onclick=spellcheck_evt_click;var props=Array('fontSize','fontFamily');for(var ii=0;ii<props.length;ii++){var x=props[ii];var ss=gk_get_style(td,x);if(!ss)continue;nd.style[x]=ss;}var nds=nd.style;nds.position='absolute';nds.padding='2px';nds.width=txt_w+'px';nds.height=txt_h+'px';nds.border='1px solid #000000';nds.backgroundColor='#ffffdd';nds.color='#000000';nds.overflow='auto';nds.whiteSpace='normal';nds.cursor='default';nds.textAlign='left';spellcheck_render();schk_orig_display=schk_orig_text_div.style.display;schk_orig_text_div.style.visibility='hidden';parent_div.insertBefore(schk_text_div,schk_orig_text_div);return 1;}function spellcheck_finish(){spellcheck_remove_menu();if(schk_button)schk_button.value=schk_button_text;schk_active=0;if(!schk_orig_text_div)return 0;if(!schk_text_div)return 0;schk_orig_text_div.style.visibility='visible';var parent_div=schk_text_div.parentNode;if(!parent_div)return 0;parent_div.removeChild(schk_text_div);schk_data=[];schk_words=[];schk_text_div=undefined;schk_orig_text_div=undefined;return 1;}function spellcheck_remove_menu(){spellcheck_finish_word_edit();if(!schk_menu)return;schk_menu.parentNode.removeChild(schk_menu);schk_menu=undefined;}function spellcheck_display_menu(x,y,t){spellcheck_remove_menu();if(!schk_text_div)return;schk_menu=document.createElement('div');var ms=schk_menu.style;spellcheck_copy_appearance(schk_text_div,schk_menu);ms.position='absolute';ms.zIndex='100';ms.display='block';ms.border='1px solid #000000';ms.backgroundColor='#ffffff';ms.padding='5px';ms.color='#000000';ms.textDecoration='none';ms.left=x+'px';ms.top=y+'px';ms.whiteSpace='nowrap';schk_menu.innerHTML=t;document.body.appendChild(schk_menu);gui_display_shadow(schk_menu);}function spellcheck_evt_menu_edit(evt,idx){if(!evt)var evt=window.event;evt.cancelBubble=true;if(evt.stopPropagation)evt.stopPropagation();if(!schk_text_div)return;if(idx<0||idx>=schk_data.length)return;var o=schk_data[idx];if((typeof o)=='string')return;spellcheck_remove_menu();schk_edit_word_idx=idx;spellcheck_render();var ew=gk_get_el('spellcheck_word_edit');if(ew){ew.focus();}}function spellcheck_get_word_shown(idx){var o=schk_data[idx];if((typeof o)=='string')return o;if((typeof o.pw)=='string')return o.w;var w=schk_words[o.w];if(!w)return o.w;if(w.mis&&w.ci>=0&&w.sug&&w.ci<w.sug.length){return w.sug[w.ci];}return o.w;}function spellcheck_evt_menu_ignore(evt,idx){if(!evt)var evt=window.event;evt.cancelBubble=true;if(evt.stopPropagation)evt.stopPropagation();if(!schk_text_div)return;if(idx<0||idx>=schk_data.length)return;var o=schk_data[idx];if((typeof o)=='string')return;schk_data[idx]=spellcheck_get_word_shown(idx);spellcheck_remove_menu();spellcheck_render();}function spellcheck_evt_menu_ignore_all(evt,idx){if(!evt)var evt=window.event;evt.cancelBubble=true;if(evt.stopPropagation)evt.stopPropagation();if(!schk_text_div)return;if(idx<0||idx>=schk_data.length)return;var o=schk_data[idx];if((typeof o)=='string')return;var cur_ww=spellcheck_get_word_shown(idx);for(var i=0;i<schk_data.length;i++){var ww=spellcheck_get_word_shown(i);if(ww!=cur_ww)continue;schk_data[i]=ww;}spellcheck_remove_menu();spellcheck_render();}function spellcheck_cancel_evt(evt){if(!evt)var evt=window.event;evt.cancelBubble=true;if(evt.stopPropagation)evt.stopPropagation();return 0;}function spellcheck_evt_menu_word(evt,idx,sug_idx){if(!evt)var evt=window.event;evt.cancelBubble=true;if(evt.stopPropagation)evt.stopPropagation();if(!schk_text_div)return;if(idx<0||idx>=schk_data.length)return;var o=schk_data[idx];if((typeof o)=='string')return;if((sug_idx<0)&&((typeof o.pw)=='string')){o.w=o.pw;o.pw=null;if(schk_words[o.w]){schk_words[o.w].ci=-1;}}else{var word=schk_words[o.w];if(!word){spellcheck_error('Misspelled word not found on menu click! ('+o.w+')');return;}if(sug_idx<-1||sug_idx>=word.sug.length){spellcheck_error('Correction index is out of range! ('+sug_idx+','+o.w+')');return;}word.ci=sug_idx;o.pw=null;}spellcheck_save_modified_text();spellcheck_remove_menu();spellcheck_render();}function spellcheck_save_modified_text(){schk_modified=1;if(!schk_orig_text_div)return;var t=spellcheck_get_text();if(!t)return;schk_orig_text_div.value=t;if((typeof schk_orig_text_div.onchange)=='function'){schk_orig_text_div.onchange();}}function spellcheck_evt_click_word(evt,idx){if(!evt)var evt=window.event;evt.cancelBubble=true;if(evt.stopPropagation)evt.stopPropagation();if(!schk_text_div)return;if(idx<0||idx>=schk_data.length)return;var o=schk_data[idx];if((typeof o)=='string')return;var word=schk_words[o.w];var span_style='cursor: pointer; text-decoration: underline;';var t='';if((typeof o.pw)=='string'){t+='&hellip; <span style="color: '+schk_red+'; '+span_style+'" onClick="spellcheck_evt_menu_word(event,'+idx+',-1)">'+gk_html_safe(o.pw)+'</span><br>';}if(word){if(word.ci>=0){t+='&hellip; <span style="color: '+schk_red+'; '+span_style+'" onClick="spellcheck_evt_menu_word(event,'+idx+',-1)">'+gk_html_safe(o.w)+'</span><br>';}if(!word.sug||!word.sug.length){if(word.mis){t+='&raquo; (no suggestions)<br>';}}else{for(var i=0;i<word.sug.length;i++){var s=gk_html_safe(word.sug[i]);t+='&raquo; <span style="color: '+schk_green+'; '+span_style+'" onClick="spellcheck_evt_menu_word(event,'+idx+','+i+')">'+s+'</span><br>';}}}t+='<span style="color: '+schk_blue+'; '+span_style+'" onClick="spellcheck_evt_menu_edit(event,'+idx+')">Edit...</span><br>';t+='<span style="color: '+schk_blue+'; '+span_style+'" onClick="spellcheck_evt_menu_ignore(event,'+idx+')">Ignore...</span><br>';t+='<span style="color: '+schk_blue+'; '+span_style+'" onClick="spellcheck_evt_menu_ignore_all(event,'+idx+')">Ignore all...</span><br>';var elm=null;if(evt.target)elm=evt.target;else if(evt.srcElement)elm=evt.srcElement;var x,y;if(elm){x=gk_get_absolute_pos_left(elm)-elm.parentNode.scrollLeft+1;y=gk_get_absolute_pos_top(elm)-elm.parentNode.scrollTop+elm.offsetHeight+2;}else{x=gk_mouse_x(evt)+3;y=gk_mouse_y(evt)-1;}spellcheck_display_menu(x,y,t);}function spellcheck_process_text(){schk_data=schk_orig_text.match(/http:\/\/.+?\s+|id\=[\w\-]+|bd\=\d+|\w+\'\w\w?\b|\W+|\w+/g);if(!schk_data||!schk_data.length)return 0;var all_test=schk_data.join('');if(schk_orig_text!=all_test){spellcheck_error('Error tokenizing text! (mismatch)');return 0;}schk_words=new Object;for(var i=0;i<schk_data.length;i++){var o=schk_data[i];if(!o.match(/\w+\'\w\w?/)&&o.match(/\W/))continue;if(o.match(/\d/))continue;if(o.length<2)continue;if(schk_words[o]==undefined){schk_words[o]={mis:0,sug:null,ci:-1};}schk_data[i]={w:o,pw:null};}if(!spellcheck_consult_server())return 0;return 1;}function spellcheck_get_text(){if(!schk_data)return '';if(!schk_words)return '';var txt='';for(var i=0;i<schk_data.length;i++){txt+=spellcheck_get_word_shown(i);}return txt;}function spellcheck_evt_word_edit_catch_enter(evt){if(!evt)var evt=window.event;evt.cancelBubble=true;if(evt.stopPropagation)evt.stopPropagation();var key=evt.keyCode||evt.which;if(key!=13)return true;spellcheck_finish_word_edit();return false;}function spellcheck_render(){if(!schk_text_div)return 0;var span_style='cursor: pointer; text-decoration: underline;';var txt='';for(var idx=0;idx<schk_data.length;idx++){var o=schk_data[idx];if((typeof o)=='string'){var t=gk_html_safe(o);t=t.replace(/\n/g,'<br>');txt+=t;continue;}if(idx==schk_edit_word_idx){var ww=spellcheck_get_word_shown(schk_edit_word_idx);var l=ww.length;if(!l||l<1)l=1;l+=2;txt+='<INPUT TYPE="text" MAXLENGTH="100" VALUE="'+ww+'" SIZE='+l+' id="spellcheck_word_edit" onClick="spellcheck_cancel_evt(event)" onMouseDown="spellcheck_cancel_evt(event)" onMouseUp="spellcheck_cancel_evt(event)" onKeyPress="return spellcheck_evt_word_edit_catch_enter(event);">';continue;}if((typeof o.pw)=='string'){var dw=gk_html_safe(o.w);if(!dw)dw='&hellip;';txt+='<span style="color: '+schk_green+'; '+span_style+'" onClick="spellcheck_evt_click_word(event,'+idx+');" title="Click for suggestions">'+dw+'</span>';continue;}var w=schk_words[o.w];if(w&&w.mis){if(w.ci>=0&&w.sug&&w.ci<w.sug.length){var new_w=w.sug[w.ci];txt+='<span style="color: '+schk_green+'; '+span_style+'" onClick="spellcheck_evt_click_word(event,'+idx+');" title="Click for suggestions">'+gk_html_safe(new_w)+'</span>';}else{txt+='<span style="color: '+schk_red+'; '+span_style+'" onClick="spellcheck_evt_click_word(event,'+idx+');" title="Click for suggestions">'+gk_html_safe(o.w)+'</span>';}continue;}txt+=gk_html_safe(o.w);}schk_text_div.innerHTML=txt;return 1;}function spellcheck_consult_server(){var xhttp=gk_get_http();if(!xhttp){alert('Your web browser is lacking the functionality required for spellchecker:\nXMLHTTP object not present!');return 0;}var url='/spellcheck.pl?rnd='+Math.random();var req='';for(var w in schk_words){if(req.length)req+='|';req+=w;}xhttp.open('POST',url,false);xhttp.send(req);var http_text='';try{http_text=xhttp.responseText;}catch(e){}if(http_text=='')return 0;var rr=http_text.split('|');for(var i=0;i<rr.length;i++){var sug=rr[i].split(',');var word=sug[0];var o=schk_words[word];if(!o){spellcheck_error('Misspelled word not found! ('+word+')');continue;}o.mis=1;o.sug=[];for(var j=1;j<sug.length;j++){o.sug.push(sug[j]);}}return 1;}function spellcheck_error(msg){gk_log_error(msg);schk_error=msg;}
