//Copyright © 2005-2010 John Goodman - www.unverse.net  *date 100312
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

var btn=[]; //object containing button strip information 
btn._w=16; btn._h=16; btn._f="/js/icons2.png"; //set to path to toolbar image
var a, oW, sel, rng, papa, trail, ppw, wn=window, msIE=("Microsoft Internet Explorer"==navigator.appName); 
var buts=''; 
var dobut=new Array();
var whizzies=new Array();
var taOrigSize=new Array();
var language = new Array();
language['Bold'] = "Fetstil (ctrl+B)";
language['Italic']  = "Kursiv (ctrl+I)";
language['Underline']  = "Understruken (ctrl+U)";
language['Left']  = "Vänsterjustera";
language['Center']  = "Centrera";
language['Right']  = "Högerjustera";
language['Justify']  = "Justera";
language['Number']  = "Numrerad lista";
language['Bullet']  = "Punktlista";
language['Indent']  = "Indragning";
language['Outdent']  = "Drag tillbaka";
function makeWhizzyWig(ta,ifw,ad){ // make a WhizzyWig from the textarea
 a=ta;
 whizzies[whizzies.length]=a;
 if (!document.designMode) {
  alert("Whizzywig "+t("editor not available for your browser"));
  return;
 }
 var taContent=o(a).defaultValue ? o(a).defaultValue : o(a).innerHTML ? o(a).innerHTML: ''; //anything in the textarea?
 if (!o(a).rows < 5) o(a).rows='15';//IE won't use % from style
 taWidth=o(a).style.width ? o(a).style.width : o(a).cols + "ex";  //grab the width and...
 taHeight=o(a).style.height ? o(a).style.height : o(a).rows + "em";  //...height from the textarea
 taOrigSize[a] = {w:taWidth,h:taHeight};
 //Create whizzy container
 var wC = document.createElement('div');
 wC.id = 'CONTAINER'+a+'';
 wC.style.width = taWidth;
 var taObject = o(a);
 o(a).parentNode.replaceChild(wC, o(a));
 o("CONTAINER"+a).appendChild(taObject);
 //End whizzy container
 if (o(a).nodeName=="TEXTAREA"){
 o(a).style.color='#060';
 o(a).style.zIndex='2';
 } else w('<input type="hidden" id="wzhid_'+a+'" name="'+a+'" />');
 h(a);
 var frm=o(a).parentNode;
 while (frm.nodeName != 'FORM') frm=frm.parentNode;//if not form, keep trying
 addEvt(frm,"submit",syncTextarea);
 w('<style type="text/css">button {vertical-align:middle;padding:0;margin:1px 0} button img{vertical-align:middle;margin:-1px} select{vertical-align:middle;margin:1px}  .wzCtrl {background:ButtonFace; border:1px solid black; padding:5px;} #sourceTa{color:#060;font-family:mono;}</style>');
 var dbuts=' bold italic underline | left center right justify | number bullet indent outdent | color hilite rule | link image smiley | clean ';
 if(ad) dbuts+="html ";
 btns=(dbuts).split(' ');
 for (var i=0,pos=0;i<btns.length;i++) {
  if(btns[i] && btns[i]!='|') {btn[btns[i]]=btn._w*pos++};
 }
 var ctrl=buts + dbuts;
 w('<div onmouseover="c(\''+a+'\')"><div id="CONTROLS'+a+'" class="wzCtrl" unselectable="on">');
 gizmos=ctrl.split(' ');
 for (var i=0; i < gizmos.length; i++) {
  if (gizmos[i]){ //make buttons and selects for toolbar, in order requested
   if (gizmos[i] == '|') w('&nbsp;&nbsp;');
   else if ((dbuts+buts).indexOf(gizmos[i]) != -1) makeButton(gizmos[i]);
  }
 }
 //w('<a href="http://www.unverse.net" style="color:buttonface" title="61 compact">.</a> ');
 w(fGo('LINK'));
 w('Webbadress (URL): <input type="text" id="lf_url'+a+'" size="60"><br>'+fNo(t("Lägg till länk"),"insertLink()"));//LINK_FORM end
 w(fGo('IMAGE'));
 w(t('Bildadress (URL)')+': <input type="text" id="if_url'+a+'" size="50"> <label title='+t("to display if image unavailable")+'><br>'+t("Align")+':<select id="if_side'+a+'"><option value="none">_&hearts;_ '+t("normal")+'</option><option value="left">&hearts;= &nbsp;'+t("left")+'</option><option value="right">=&hearts; &nbsp;'+t("right")+'</option></select> '+t("Border")+':<input type="text" id="if_border'+a+'" size="20" value="0" title="'+t("number or CSS e.g. 3px maroon outset")+'"> '+t("Margin")+':<input type="text" id="if_margin'+a+'" size="20" value="0" title="'+t("number or CSS e.g. 5px 1em")+'">'+fNo("Lägg till bild","insertImage()"));//IMAGE_FORM end
 w(fGo('SMILEY'));
 w(as('s6',16,16)+as('s11',16,16)+as('s10',16,16)+as('s13',16,16)+as('s28',16,16)+as('s26',16,16)+as('s18',16,16)+as('s12',16,16)+as('s17',16,16)+as('s15',15,15)+as('s16',15,15)+as('s7',15,22)+as('s4',30,18)+as('s8',25,23)+as('s21',34,15)+as('s23',16,16)+as('s29',38,23)+as('s30',29,20)+as('s31',35,20)+as('s32',40,18)+as('s39',38,20)+as('s3',18,27)+as('s2',20,20)+as('s34',20,25)+as('s36',19,24)+as('s22',23,25)+as('s40',25,25)+as('s25',26,28)+as('s19',18,22)+as('s1',20,20)+as('s5',20,30)+as('s20',27,28)+as('s37',26,25)+as('s14',31,25)+as('s33',26,18)+as('s35',28,23)+as('s27',34,36)+as('s38',31,31)+as('s9',32,32)+as('s24',33,35)+'</div>\n');
 w(fGo('COLOR')+'<input type="hidden" id="cf_cmd'+a+'"><div style="background:#000;padding:1px;height:22px;width:125px;float:left"><div id="cPrvw'+a+'" style="background-color:red; height:100%; width:100%"></div></div> <input type=text id="cf_color'+a+'" value="red" size=17 onpaste=vC(value) onblur=vC(value)> <input type="button" onmouseover=vC() onclick=sC() value="'+t("OK")+'">  <input type="button" onclick="hideDialogs();" value="Avbryt"><br> '+t("click below or enter a")+' <a href="http://www.unverse.net/colortable.htm" target="_blank">'+t("color name")+'</a><br clear=all> <table border=0 cellspacing=1 cellpadding=0 width=480 bgcolor="#000000">'+"\n");
 var wC=new Array("00","33","66","99","CC","FF")  //color table
 for (i=0; i<wC.length; i++){
  w("<tr>");
  for (j=0; j<wC.length; j++){
   for (k=0; k<wC.length; k++){
    var clr=wC[i]+wC[j]+wC[k];
    w(' <td style="background:#'+clr+';height:12px;width:12px" onmouseover=vC("#'+clr+'") onclick=sC("#'+clr+'")></td>'+"\n");
   }
  }
  w('</tr>');
 }
 w("</table></div>\n"); //end color table,COLOR_FORM
 w("</div>\n");
 w('<div class="wzCtrl" id="showWYSIWYG'+a+'" style="display:none"><input type="button" onclick="showDesign();" value="'+t("Göm HTML")+'">');
 w('</div>'+"\n");
 w('<iframe style="border:1px solid black;width:'+ifw+'px;height:'+taHeight+'" frameborder="0" src="javascript:;" id="whizzy'+a+'"></iframe><div align="center" style="border:1px solid black;background:ButtonFace;font-family:verdana; font-size:9px;padding:2px;" >This editor is based on <a href="http://unverse.net/">Whizzywig</a> and rewritten to suit &#68;&#105;&#110;&#83;&#116;&#97;&#114;&#116;&#115;&#105;&#100;&#97;</div></div>'+"\n", true); //finally write content to whizzy container
 oW=o("whizzy"+a).contentWindow;
 var d=oW.document;
 try{d.designMode="on";} catch(e){ setTimeout('oW.designMode="on";', 100);}
 d.open(); d.write('<html>\n<head>\n</head>\n<body id="'+a+'" style="background-image:none">\n'+tidyD(taContent)+'</body>\n</html>'); d.close();
 if (oW.addEventListener) oW.addEventListener("keypress", kb_handler, true); //keyboard shortcuts for Moz
 else {d.body.attachEvent("onpaste",function(){setTimeout('cleanUp()',10);});}
 addEvt(d,"mouseup", whereAmI); addEvt(d,"keyup", whereAmI); addEvt(d,"dblclick", doDbl);
 //move textarea so html menu appears on top
 var taObject = o(a);
 o("CONTAINER"+a).removeChild(o(a));
 o("CONTAINER"+a).appendChild(taObject);
 //end move
 a=null;
} //end makeWhizzyWig
function as(b,w,h){return '<img src="/i/'+b+'.gif" width="'+w+'" height="'+h+'" style="cursor:pointer;" onclick="insSm(\''+b+'\')"> ';}
function addEvt(o,e,f){if(wn.addEventListener) o.addEventListener(e, f, false); else o.attachEvent("on"+e,f);}
function doDbl(){if (papa.nodeName == 'IMG') doImage(); else if (papa.nodeName == 'A') doLink();}
function makeButton(b){  // assemble the button requested
 var ucBut=b.substring(0,1).toUpperCase();
 ucBut += b.substring(1);
 ucBut=t(ucBut.replace(/_/g,' '));
 if (o(a).nodeName!="TEXTAREA" && b=="html") return; 
 w('<button title="'+ucBut+'" type="button" style="border:1px solid black;background-color:#eee;width:24px;height:25px;" onClick="makeSo(\''+b+'\')"><div style="margin: 0 auto;width:'+btn._w+'px;height:'+btn._h+'px;background-image:url('+btn._f+');background-position:-'+btn[b]+'px 0px"></div></button>\n');
}
function fGo(id){return '<div id="'+id+'_FORM'+a+'" align="center" unselectable="on" style="display:none" onkeypress="if(event.keyCode==13) {return false;}"><hr>'+"\n"; }//new form
function fNo(t,g){return '<div style="margin-top:5px;"><input type="button" onclick="'+g+'" value="'+t+'"> <input type="button" onclick="hideDialogs();" value="Avbryt"></div></div>\n';}
function xC(c,o){return oW.document.execCommand(c,false,o);}
function makeSo(cm,op) {  //format selected text or line in the whizzy
 hideDialogs();
 oW.focus();
 if(dobut[cm]) {insHTML(dobut[cm]); return;}
 if (/Firefox/.test(navigator.userAgent)) {xC("styleWithCSS",cm=="hilite")} //no spans for bold, italic, ok hilite
 if (cm == "justify") cm = "full";
 if ("leftrightcenterfull".indexOf(cm) !=-1) cm="justify" + cm;
 else if (cm == "number") cm="insertorderedlist";
 else if (cm == "bullet") cm="insertunorderedlist";
 else if (cm == "rule") cm="inserthorizontalrule";
 switch (cm) {
  case "color": o('cf_cmd'+a).value="forecolor"; if (textSel()) s('COLOR_FORM'+a); break;
  case "hilite" : o('cf_cmd'+a).value=cm; if (textSel()) s('COLOR_FORM'+a); break;
  case "image" : doImage(); break;
  case "smiley" : s('SMILEY_FORM'+a); break;
  case "link" : doLink(); break;
  case "html" : showHTML(); break;
  case "clean" : cleanUp(); break;
  default: xC(cm,op);
 }
 oW.focus();
}
function vC(c) { // view Color
 if (!c) c=o('cf_color'+a).value;
 o('cPrvw'+a).style.backgroundColor=c;
 o('cf_color'+a).value=c;
}
function sC(c) {  //set Color 
 hideDialogs();
 var cmd=o('cf_cmd'+a).value;
 if  (!c) c=o('cf_color'+a).value;
 if (rng) rng.select();
 if (cmd=="hilite") try {xC("hilitecolor",c);} catch(e){xC("backcolor",c);}
 else {xC(cmd,c);}
 oW.focus();
}
function doLink(){
 if (textSel()) {
  if (papa.nodeName == 'A') o("lf_url"+a).value=papa.href 
  else o("lf_url"+a).value='http://';
  s('LINK_FORM'+a);
 }
}
function insertLink(url) {
 if (rng) rng.select();
 URL=url ? url : o("lf_url"+a).value; 
 if (URL.replace(/ /g,"") == "") xC('Unlink',null);
 else xC('CreateLink',URL);
 hideDialogs();
}

function doImage(){
 if (papa && papa.nodeName == 'IMG'){
  o("if_url"+a).value=papa.src;
  var position = papa.style.cssFloat?papa.style.cssFloat:papa.style.styleFloat;
  o("if_side"+a).selectedIndex=(position=="left")?1:(position=="right")?2:0; 
  o("if_border"+a).value=papa.style.border?papa.style.border:papa.border>0?papa.border:0;
  o("if_margin"+a).value=papa.style.margin?papa.style.margin:papa.hspace>0?papa.hspace:0;
 }
 s('IMAGE_FORM'+a);
}
function insertImage(URL, side, border, margin) { // insert image as specified
 hideDialogs();
 if (!URL) URL=o("if_url"+a).value;
 if (URL) {
  img='<img src="' + URL +'" ';
  if (!side) side=o("if_side"+a).value;
  if ((side == "left") || (side == "right")) align = 'float:' + side + ';'; else align = '';
  if (!border)  border=o("if_border"+a).value;
  if (border.match(/^\d+$/)) border+='px solid';
  if (!margin) margin=o("if_margin"+a).value;
  if (margin.match(/^\d+$/)) margin+='px';
  if (border || margin) img += ' style="border:' + border + ';margin:' + margin + ';' + align + '"';
  img += '/>';
  insHTML(img);
 }
}
function hideDialogs() { h('LINK_FORM'+a); h('IMAGE_FORM'+a); h('COLOR_FORM'+a); h('SMILEY_FORM'+a); }
function showDesign() {
 oW.document.body.innerHTML=tidyD(o(a).value);
 h(a); h('showWYSIWYG'+a); s('CONTROLS'+a); s('whizzy'+a);
 if(o("whizzy"+a).contentDocument) o("whizzy"+a).contentDocument.designMode="on"; //FF loses it on hide
 oW.focus();
}
function showHTML() {
 o(a).value=tidyH(oW.document);
 h('CONTROLS'+a); h('whizzy'+a); 
 s(a); 
 s('showWYSIWYG'+a);
 o(a).style.position="relative";
 o(a).style.width=taOrigSize[a].w;
 o(a).style.height=taOrigSize[a].h;
 o(a).style.borderWidth="1px";
 o(a).focus();
}

function syncTextarea() { //tidy up before we go-go
 for (var i=0;i<whizzies.length;i++){
  var t=whizzies[i];
  var d=o("whizzy"+t).contentWindow.document;
  if (o(t).style.display == 'block') d.body.innerHTML=o(t).value;
  var ret=(o(t).nodeName!="TEXTAREA") ? o('wzhid_'+o(t).id) : o(t);
  ret.value=tidyH(d);
 }
}
function clearText(){oW.document.body.innerHTML='';}
function clearWord(){
ih=d.body.innerHTML;
ih=ih.replace(/<o:p>\s*<\/o:p>/g, "")
.replace(/<o:p>.*?<\/o:p>/g, "&nbsp;")
.replace( /\s*mso-[^:]+:[^;"]+;?/gi, "" )
.replace( /\s*MARGIN: 0cm 0cm 0pt\s*;/gi, "" )
.replace( /\s*MARGIN: 0cm 0cm 0pt\s*"/gi, "\"" )
.replace( /\s*TEXT-INDENT: 0cm\s*;/gi, "" )
.replace( /\s*TEXT-INDENT: 0cm\s*"/gi, "\"" )
.replace( /\s*TEXT-ALIGN: [^\s;]+;?"/gi, "\"" )
.replace( /\s*PAGE-BREAK-BEFORE: [^\s;]+;?"/gi, "\"" )
.replace( /\s*FONT-VARIANT: [^\s;]+;?"/gi, "\"" )
.replace( /\s*tab-stops:[^;"]*;?/gi, "" )
.replace( /\s*tab-stops:[^"]*/gi, "" )
.replace( /\s*face="[^"]*"/gi, "" )
.replace( /\s*face=[^ >]*/gi, "" )
.replace( /\s*FONT-FAMILY:[^;"]*;?/gi, "" )
.replace(/<(\w[^>]*) class=([^ |>]*)([^>]*)/gi, "<$1$3")
.replace( /<(\w[^>]*) style="([^\"]*)"([^>]*)/gi, "<$1$3" )
.replace( /\s*style="\s*"/gi, '' )
.replace( /<SPAN\s*[^>]*>\s*&nbsp;\s*<\/SPAN>/gi, '&nbsp;' )
.replace( /<SPAN\s*[^>]*><\/SPAN>/gi, '' )
.replace(/<(\w[^>]*) lang=([^ |>]*)([^>]*)/gi, "<$1$3")
.replace( /<SPAN\s*>(.*?)<\/SPAN>/gi, '$1' )
.replace( /<FONT\s*>(.*?)<\/FONT>/gi, '$1' )
.replace(/<\\?\?xml[^>]*>/gi, "")
.replace(/<\/?\w+:[^>]*>/gi, "")
.replace( /<H\d>\s*<\/H\d>/gi, '' )
.replace( /<H1([^>]*)>/gi, '' )
.replace( /<H2([^>]*)>/gi, '' )
.replace( /<H3([^>]*)>/gi, '' )
.replace( /<H4([^>]*)>/gi, '' )
.replace( /<H5([^>]*)>/gi, '' )
.replace( /<H6([^>]*)>/gi, '' )
.replace( /<\/H\d>/gi, '<br>' )
.replace( /<(U|I|STRIKE)>&nbsp;<\/\1>/g, '&nbsp;' )
.replace( /<(B|b)>&nbsp;<\/\b|B>/g, '' )
.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' )
.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' )
.replace( /<([^\s>]+)[^>]*>\s*<\/\1>/g, '' );

//some RegEx code for the picky browsers
var re = new RegExp("(<P)([^>]*>.*?)(<\/P>)","gi") ;
str = str.replace( re, "<div$2</div>" ) ;
var re2 = new RegExp("(<font|<FONT)([^*>]*>.*?)(<\/FONT>|<\/font>)","gi") ; 
str = str.replace( re2, "<div$2</div>") ;
str = str.replace( /size|SIZE = ([\d]{1})/g, '' ) ;
}

function cleanUp(){xC("removeformat",null); tidyH(oW.document);}
function tidyD(h){ //FF designmode likes <B>,<I>...
 h=h.replace(/<(\/?)strong([^>]*)>/gi, "<$1B$2>").replace(/<(\/?)em>/gi, "<$1I>");
 return h;
}
function tidyH(d){ //attempt valid xhtml
 function lc(str){return str.toLowerCase()}
 function qa(str){return str.replace(/(\s+\w+=)([^"][^>\s]*)/gi,'$1"$2"');}
 function sa(str){return str.replace(/("|;)\s*[A-Z-]+\s*:/g,lc);}
 var sz=new Array('medium','xx-small','x-small','small','medium','large','x-large','xx-large');
 fs=d.getElementsByTagName("FONT");
 for (var i=0;i<fs.length;i++){
  if (fs[i].face) {fs[i].style.fontFamily = fs[i].face; fs[i].removeAttribute('face');}
  if (fs[i].size) {fs[i].style.fontSize = sz[fs[i].size]; fs[i].removeAttribute('size');} 
  if (fs[i].color) {fs[i].style.color = fs[i].color; fs[i].removeAttribute('color');}
 }
 ih=d.body.innerHTML;
 ih=ih.replace(/(<\/?)FONT([^>]*)/gi,"$1span$2") 
 .replace(/(<\/?)[B](\s+[^>]*)?>/gi, "$1strong$2>")
 .replace(/(<\/?)[I](\s+[^>]*)?>/gi, "$1em$2>")
 .replace(/<\/?(COL|XML|ST1|SHAPE|V:|O:|F:|F |PATH|LOCK|IMAGEDATA|STROKE|FORMULAS)[^>]*>/gi, "")
 .replace(/\bCLASS="?(MSOw*|Apple-style-span)"?/gi,"")
 .replace(/<[^>]+=[^>]+>/g,qa) //quote all atts
 .replace(/[–]/g,'-') //long –
 .replace(/[‘’]/g, "'") //single smartquotes ‘’ 
 .replace(/[“”]/g, '"') //double smartquotes “”
 .replace(/<(TABLE|TD|TH|COL)(.*)(WIDTH|HEIGHT)=["'0-9A-Z]*/gi, "<$1$2") //no fixed size tables (%OK) [^A-Za-z>]
 .replace(/<([^>]+)>\s*<\/\1>/g, "") //empty tag
 .replace(/><(H|P|D|T|BLO|FOR|IN|SE|OP|UL|OL|LI|SC)/gi,">\n<$1") //newline adjacent blocks
 .replace(/(<BR ?\/?>)([^\r\n])/gi,"$1\n$2") //newline on BR
 .replace(/([^\n])<(P|DIV|TAB|FOR)/gi,"$1\n\n<$2") //add white space
 .replace(/([^\n])<\/(UL|OL|DL|DIV|TAB|FOR)/gi,"$1\n</$2") //end block
 .replace(/([^\n])(<\/TR)/gi,"$1\n $2") //end row
 .replace(/\n<(BLO|LI|OP|TR|IN|DT)/gi,"\n <$1") //indent..
 .replace(/\n<(TD|TH|DD)/gi,"\n  <$1") //..more
 .replace(window.location.href+'#','#') //IE anchor bug
 .replace(/<(IMG|INPUT|BR|HR|LINK|META)([^>]*)>/gi,"<$1$2 />")  //self-close tags
 .replace(/(<\/?[A-Z]*)/g,lc) //lowercase tags...
 .replace(/STYLE="[^"]*"/gi,sa); //lc style atts
 return ih; 
}
function kb_handler(e) { // keyboard controls for Moz
 var cmd=false, prm=false;
 if (e && (e.ctrlKey && e.keyCode == e.DOM_VK_V)||(e.shiftKey && e.keyCode == e.DOM_VK_INSERT))
  {setTimeout('cleanUp()',10);}
 else if (e && e.keyCode==13 && !e.shiftKey &&papa.nodeName=="BODY") {cmd="formatblock";prm="<p>";}
 else if (e && e.ctrlKey) {
  var k=String.fromCharCode(e.charCode).toLowerCase();
  var cmd=(k=='b')?'bold':(k=='i')?'italic':(k=='l')?'link':(k=='m')?'image':false;
 }
 if (cmd) {
  makeSo(cmd, prm);
  e.preventDefault();  // stop the event bubble
  e.stopPropagation();
 }
}
function doTag(html) { // insert HTML into text area
 var url;
 if (!html) html=prompt("Enter some HTML or text to insert:", "");
 o(a).focus();
 if (html == '<a>') {
  url=prompt("Länkadressen:","http://"); 
  html='<a href="'+url+'">';
 }
 if (html == '<img>') {
  url=prompt("Adressen till bilden:","http://"); 
  var alt=prompt("Description of image");
  html ='<img src="'+url+'" alt="'+alt+'">';
 }
 var close='';
 if (html.indexOf('<') == 0 && html.indexOf('br') != 1 && html.indexOf('img') != 1)
  close=html.replace(/<([a-z0-6]+).*/,"<\/$1>");
 if (html != '<strong>' && html != '<em>') close += '\n';
 if (document.selection) {
  sel=document.selection.createRange();
  sel.text=html+sel.text+close;
 } else {
   before=o(a).value.slice(0,o(a).selectionStart);
   sel=o(a).value.slice(o(a).selectionStart,o(a).selectionEnd);
   after=o(a).value.slice(o(a).selectionEnd);
   o(a).value =before+html+sel+close+after;
 }
 o(a).focus(); 
}
function insSm(i) {insHTML('<img src="/i/'+i+'.gif" align="absmiddle">');}
function insHTML(html) { //insert HTML at current selection
 if (!html) html=prompt(t("Ange text eller HTML-kod för insättning:"), "");
 if (html.indexOf('js:') == 0) {
  try{eval(html.replace(/^js:/,''))} catch(e){}; return;
 }
 whereAmI();
 try {if (sel.type && sel.type!="Text") sel=""; xC("inserthtml",html+sel);}
 catch (e) {if (document.selection) {
  if (papa && papa.nodeName == 'IMG') {papa.outerHTML=html;}
  else if(rng) {rng.select(); rng.pasteHTML(html+rng.htmlText);}
 } }
}
function whereAmI(e){
 if (!e) var e=wn.event;
 var mu=e&&e.type=='mouseup';
  if (msIE){//Issue 11
  oW.document.getElementsByTagName("body")[0].focus(); 
  sel=oW.document.selection;
  rng=sel.createRange();
  papa=mu?e.srcElement:(sel.type == "Control")?rng.item(0):rng.parentElement();
 }else{
  sel=oW.getSelection();sn=sel.anchorNode;
  papa=mu?e.target:(sn.nodeName == '#text')?sn.parentNode:sn;
 }
 var pa=papa;
 trail=papa.nodeName; 
 while (!pa.nodeName.match(/^(HTML|BODY)/) && pa.className!="wzCtrl") {
  pa=pa.parentNode;
  trail=pa.nodeName + '>' + trail;
 }
 if (pa.className=="wzCtrl") trail=sel=rng=null;
 var id=pa.nodeName=="HTML" ? pa.getElementsByTagName("BODY")[0].id : pa.id.replace("CONTROL","");
 c(id); 
 wn.status=id+":"+trail;
}
function c(id) {//set current whizzy
 if (id=="" || whizzies.join().indexOf(id)=='-1') return;
 if (id!=a){
  a=id;
  try {oW=o("whizzy"+id).contentWindow;} catch(e){alert('set current: '+id);}
  if (oW) {if(oW.focus)oW.focus();wn.status=oW.document.body.id; }
 }
} 
function w(str, finalize) { //write to whizzy container
 if(!w.temp) w.temp="";
 w.temp += str;
 if(finalize){
  o("CONTAINER"+a).innerHTML += w.temp;
  w.temp = "";
 }
} 
function textSel() {if (sel  && sel != "" && sel.type != "None") return true;  else {alert("Du måste markera en text först!"); return false;}}
function s(id) {o(id).style.display='block';} //show element
function h(id) {o(id).style.display='none';} //hide element
function o(id) {return document.getElementById(id); } //get element by ID
function t(key) {return (wn.language && language[key]) ? language[key] :  key;} //translation
