
var htmledit_suspend=false;

function htmledit_stripBase(text){
  var h=new String(document.location);
  var i=h.lastIndexOf("/");
  if(i>0)h=h.substr(0,i+1);
  
  return Replace(text,h,"");
}

function htmledit_htmlmode(control){
  var textEdit=control.document.body;
  return (textEdit.title=="")||(textEdit.title=="true");
}

function htmledit_execCommand(id,command) {
  if(!htmledit_htmlmode(id)){alert('Not in WYSIWYG mode');return ;}
  
  htmledit_suspend=true;
  id.focus();
  var edit = id.document.selection.createRange();
  if (arguments[2]==null) edit.execCommand(command);
  else edit.execCommand(command,false, arguments[2]);
  edit.select();
  htmledit_suspend=false;  
}

function htmledit_swapModes(control,imgname) {
  return ;
/*
  var img1="pics/UI_html.gif";
  var img2="pics/UI_txt.gif";
  var img=document.images[imgname];
  
  if(!img)return ;
  img.src=(img.src.indexOf(img1)>0)?img2:img1;
*/
  
  var textEdit=control.document.body;  
  
  if(textEdit.title=="")textEdit.title="false";
  else textEdit.title=eval("!"+textEdit.title);
   
  if(textEdit.innerText){ 
    if(htmledit_htmlmode(control)){
      textEdit.innerHTML = htmledit_stripBase(Replace(textEdit.innerText,"\n",""));
      textEdit.style.fontFamily = "";
      textEdit.style.fontSize ="";
      textEdit.style.textAlign = "";      
    }  
    else {
      textEdit.innerText = htmledit_stripBase(textEdit.innerHTML);
      textEdit.style.fontFamily = "monospace";
      textEdit.style.fontSize = "10pt";
      textEdit.style.textAlign = "left";
    }  
    var s = textEdit.createTextRange();
    s.collapse(false);
    s.select();    
  }
}  

function htmledit_addLink(id){
  var url=prompt("Geef URL:", "");  
  if(url)htmledit_execCommand(id,"CreateLink",url);
}

var htmledit_Image=null;

function _htmledit_addImage(image){
  htmledit_execCommand(htmledit_Image,"InsertImage",image);
}


function htmledit_Pop(url,awidth,aheight,aresize,amodal) {
  var D=new Date();
  var W=(awidth)?awidth:700;
  var H=(aheight)?aheight:500;
  var L=(screen.width)?(screen.width-W)/2:0;
  var T=(screen.height)?(screen.height-H)/2:0;
  var modal=(amodal)?(amodal==true):true; 
  
  url=QueryReplace(url,"popup","true");
  modal=false;
  CloseCurtain();
  if((modal)&&(window.showModalDialog))
    window.showModalDialog(url,document,"dialogWidth:"+W+"px;dialogHeight:"+H+"px;status:yes;resizable:"+aresize);
  else {
    var S="resizable=yes,directories=no,scrollbars=yes,modal=yes,toolbar=no,status=yes,menubar=no,left="+eval(L)+",top="+eval(T)+",width="+eval(W)+",height="+eval(H);
    popupWindow=window.open(url, "w"+eval(D.getTime()), S);
    if(parseInt(navigator.appVersion) >= 4)popupWindow.window.focus();
    
  }
  OpenCurtain();
}

function htmledit_addImage(id){
  htmledit_Image=id;
  htmledit_Pop("htmlbrowser.php?ID="+id.name);
}

function htmledit_setFontSize(id,sel){
  htmledit_execCommand(id,"FontSize",sel.selectedIndex+1);
}

function htmledit_setFont(id,sel){
  htmledit_execCommand(id,"FontName",sel[sel.selectedIndex].value);
}

function htmledit_SetBorder(id,aan){
  if(aan){
    id.runtimeStyle.borderTop="1px Solid #EEEEEE";
    id.runtimeStyle.borderBottom="1px Solid #777777";

    id.runtimeStyle.borderLeft=id.runtimeStyle.borderTop;
    id.runtimeStyle.borderRight=id.runtimeStyle.borderBottom;
  }
  else id.runtimeStyle.border="1px Solid #DDDDDD";
}

function htmledit_initEditor(id,tekst) {

  tekst=Replace(Replace(tekst,"&#34;",'"'),"\n",'');  

  id.document.designMode="On";
  id.document.open();
  id.document.write("<HTML><link rel=STYLESHEET type=text/css href=style.css><BODY style='margin: 0pt; padding: 0pt; border: none;' id='"+id.name+"' name='"+id.name+"' class=FormTekst>"+tekst+"</BODY></HTML>");
  id.document.close();

  if(id.attachEvent)id.attachEvent('onkeydown',htmledit_eventHandler);
  if(id.document.attachEvent)id.document.attachEvent('onkeydown',htmledit_eventHandler);
  
  if(id.attachEvent)id.attachEvent('onfocusout',htmledit_eventHandler);
  if(id.document.attachEvent)id.document.attachEvent('onfocusout',htmledit_eventHandler);

  id.focus();
}

function htmledit_insertlist(id,type){
	var s = id.document.selection.createRange();
//  s.pasteHTML("<ul><li>"+Replace(s.text,"\n","<li>")+"</ul>");

  if(s.text.indexOf("<UL>")>0)s.text=Replace(Replace(Replace(Replace(s.text,"<UL>",""),"<LI>",""),"</LI>",""),"</UL>","");
  else {
    s.pasteHTML("<ul><li>"+Replace(s.text,"\n","<li>")+"</ul>");
    htmledit_execCommand(id,"InsertUnorderedList");
  }  
	s.select();  
}

function htmledit_eventHandler(event){ 
  var id=event.srcElement;
  if(event.type=="keydown"){
  	if((event.shiftKey==false)&&(event.shiftLeft==false)&&(event.ctrlKey==false)&&(event.ctrlLeft==false)&&(event.altKey==false)&&(event.altLeft==false)){
  		if((event.keyCode==13)&&(htmledit_htmlmode(id))){
  			event.returnValue=false;
  			var s = id.document.selection.createRange();
  			s.text="\n";
  			s.select();
  		}
  	}
  }
  else if(event.type=="focusout"){
//    if(!htmledit_suspend){
      if(!htmledit_htmlmode(id))htmledit_swapModes(id,"");
      var s=new String(id.id);
      s=s.substring(0,s.length-4);
      document.all[s].value=htmledit_stripBase(id.document.body.innerHTML);
//    }
  }  
  else {  
    debug(event);  
  }
}
