var savedSelection, savedType;
var trte;
var post_allowed;
var img_allowed = false;
var browser_error= "vaadake üle browseri settingud"
var imgTimeout;


function browser_setting_error() {
  alert(browser_error);
}

function wr_trte_onload() {
//alert(post_allowed);

  trte = wr_trte1.document;
  if (post_allowed)
    getIFrameDocument("wr_trte1").designMode = "On";
    
  trte.open("text/html","replace");
  if (typeof(wr_trte_init)!="undefined") wr_trte_init();
  trte.close();
  if (document.getElementById("wr_trte1").contentDocument) img_allowed = true;
  document.getElementById("wr_contra").value = "260" + "205";
}

function activate() {
  img_allowed = true;
  document.getElementById("wr_contra").value = "260" + "205";
}

function deactivate() {
  if (!(document.getElementById("wr_trte1").contentDocument)) img_allowed = false;
}

function getIFrameDocument(aID){
  // if contentDocument exists, W3C compliant (Mozilla)
  if (document.getElementById(aID).contentDocument){
    return document.getElementById(aID).contentDocument;
  } else {
    // IE
    return document.frames[aID].document;
  }
}

function saveSelection() {
  savedSelection = trte.selection.createRange();
  savedType = trte.selection.type;
}
function getSelection() {
  var oSel = savedSelection;
  if (!oSel) {
    oSel = trte.selection.createRange();
    //oSel.type = trte.selection.type;
  }
  return oSel
}
function doCmd(cmd) {
  if (!post_allowed) return false;
  getIFrameDocument('wr_trte1').execCommand(cmd,false);
}

function doRichEditCommand(aName, aArg){
  if (!post_allowed) return false;
  getIFrameDocument('wr_trte1').execCommand(aName,false, aArg);
  document.getElementById('wr_trte1').contentWindow.focus();
}

function wf_onclick_cut() {
  if (document.getElementById("wr_trte1").contentDocument){
    browser_setting_error();
    doRichEditCommand("cut");
  } else {
    // IE
    doCmd("cut");
  }
}
function wf_onclick_copy() { 
  if (document.getElementById("wr_trte1").contentDocument){
    browser_setting_error();
    doRichEditCommand("copy");
  } else {
    // IE
    doCmd("copy");
  }
}
function wf_onclick_paste() { 
  if (document.getElementById("wr_trte1").contentDocument){
    browser_setting_error();
    doRichEditCommand("paste");
  } else {
    // IE
    doCmd("paste");
  }
}
function wf_onclick_bold() {
  if (document.getElementById("wr_trte1").contentDocument){
    doRichEditCommand("bold");
  } else {
    // IE
    doCmd("bold");
  }
}
function wf_onclick_italic() { 
  if (document.getElementById("wr_trte1").contentDocument){
    doRichEditCommand("italic");
  } else {
    // IE
    doCmd("italic");
  }
}
function wf_onclick_underline() { 
  if (document.getElementById("wr_trte1").contentDocument){
    doRichEditCommand("underline");
  } else {
    // IE
    doCmd("underline");
  }
}
function wf_onclick_undo() { 
  if (document.getElementById("wr_trte1").contentDocument){
    doRichEditCommand("undo");
  } else {
    // IE
    doCmd("undo");
  }
}

function wf_onclick_redo() { 
  if (document.getElementById("wr_trte1").contentDocument){
    doRichEditCommand("redo");
  } else {
    // IE
    doCmd("redo");
  }
}

function wf_onclick_hyperlink() { 
  if (!post_allowed) return;
  //FF
  if (document.getElementById("wr_trte1").contentDocument){
    var url = prompt("URL:", "http://");
    if (url) {
      getIFrameDocument('wr_trte1').execCommand("createlink", false, url);
      document.getElementById('wr_trte1').contentWindow.focus() 
    }
  } else {
  //IE
    trte.body.focus();
    trte.execCommand("CreateLink", true, "");
    trte.body.focus(); 
  }
}
function wf_addImage(img) 
{
  clearTimeout(imgTimeout);
  trte.body.focus();
  if (!(post_allowed && img_allowed)) {  
    return;
  }
  if (document.getElementById("wr_trte1").contentDocument){
    getIFrameDocument('wr_trte1').execCommand("InsertImage", "", femdir + img);
    document.getElementById('wr_trte1').contentWindow.focus() 
  } else {
  //IE
    var target = getSelection();
    target.pasteHTML("<IMG SRC=\"" + femdir + img + "\"  BORDER=\"0\">");
    trte.body.focus(); 
  }
}

function wa_trte_submit() 
{
  var frm = document.forms.wf_form;
  thtml = trte.body.innerHTML;
  //fixme: only pass when text or <img ...> is inserted
  if (thtml>"" && thtml!="<DIV></DIV>" && thtml!="<DIV>&nbsp;</DIV>" && thtml!="<P>&nbsp;</P>" && thtml!="<br>") 
    document.getElementById("trte_text").value = thtml;
  else 
    return false;
  //fixme: move to webform
  if (frm.elements.f_topic_id88name) 
  { 
    if (frm.elements.f_topic_id88name.value=="") 
    {
      alert("Pange teemale nimi!")
      return false;
    }
  }
  return true;
  if (typeof(wf_webform)=="boolean") return wf_onsubmit();
}

