var command = "";

function InitToolbarButtons() {
  var kids = document.getElementsByTagName('DIV');
  for (var i=0; i < kids.length; i++) {
    if (kids[i].className == "imagebutton") {
      kids[i].onmouseover = tbmouseover;
      kids[i].onmouseout = tbmouseout;
      kids[i].onmousedown = tbmousedown;
      kids[i].onmouseup = tbmouseup;
      kids[i].onclick = tbclick;
    }
  }
}

function tbmousedown(e)
{
  var evt = e ? e : window.event; 

  this.firstChild.style.left = 1;
  this.firstChild.style.top = 1;
  this.style.border="inset 1px";
  if (evt.returnValue) {
    evt.returnValue = false;
  } else if (evt.preventDefault) {
    evt.preventDefault( );
  } else {
    return false;
  }
}

function tbmouseup()
{
  this.firstChild.style.left = 1;
  this.firstChild.style.top = 1;
  this.style.border="outset 1px";
}

function tbmouseout()
{
  this.style.border="solid 1px";
  this.style.backgroundColor="";
}

function tbmouseover()
{
  this.style.border="outset 1px";
  this.style.backgroundColor="#F4FBFD";
}

  function insertNodeAtSelection(win, insertNode)
  {
      // get current selection
      var sel = win.getSelection();

      // get the first range of the selection
      // (there's almost always only one range)
      var range = sel.getRangeAt(0);

      // deselect everything
      sel.removeAllRanges();

      // remove content of current selection from document
      range.deleteContents();

      // get location of current selection
      var container = range.startContainer;
      var pos = range.startOffset;

      // make a new range for the new selection
      range=document.createRange();

      if (container.nodeType==3 && insertNode.nodeType==3) {

        // if we insert text in a textnode, do optimized insertion
        container.insertData(pos, insertNode.nodeValue);

        // put cursor after inserted text
        range.setEnd(container, pos+insertNode.length);
        range.setStart(container, pos+insertNode.length);

      } else {


        var afterNode;
        if (container.nodeType==3) {

          // when inserting into a textnode
          // we create 2 new textnodes
          // and put the insertNode in between

          var textNode = container;
          container = textNode.parentNode;
          var text = textNode.nodeValue;

          // text before the split
          var textBefore = text.substr(0,pos);
          // text after the split
          var textAfter = text.substr(pos);

          var beforeNode = document.createTextNode(textBefore);
          afterNode = document.createTextNode(textAfter);

          // insert the 3 new nodes before the old one
          container.insertBefore(afterNode, textNode);
          container.insertBefore(insertNode, afterNode);
          container.insertBefore(beforeNode, insertNode);

          // remove the old node
          container.removeChild(textNode);

        } else {

          // else simply insert the node
          afterNode = container.childNodes[pos];
          container.insertBefore(insertNode, afterNode);
        }

        range.setEnd(afterNode, 0);
        range.setStart(afterNode, 0);
      }

      sel.addRange(range);
  };

function doEmptyBookmark()
{
var genericName = "bookmarkName";
iView.document.execCommand('createbookmark', false, genericName);
}

function tbclick() {
  if (this.id == "createlink") {
    var szURL = prompt("Enter a URL:", "*http://");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('edit').contentWindow.document.execCommand("CreateLink",false,szURL);
    }
  } else if (this.id == "createlink_b") {
    var szURL = prompt("Enter a URL:", "");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('edit').contentWindow.document.execCommand("CreateLink",false,szURL);
    }
  } else if (this.id == "bookmark") {
	  nyit('konyvjelzo.php', '600', '500');
	 /* 
    var szURL = prompt("Enter a URL:", "#21");
    if ((szURL != null) && (szURL != "")) {
      document.getElementById('edit').contentWindow.document.execCommand("CreateLink",false,szURL);
    }*/
  } else {
    document.getElementById('edit').contentWindow.document.execCommand(this.id, false, null);
  }
}

function Select(selectname)
{
  var cursel = document.getElementById(selectname).selectedIndex;
  /* First one is always a label */
  if (cursel != 0) {
    var selected = document.getElementById(selectname).options[cursel].value;
    document.getElementById('edit').contentWindow.document.execCommand(selectname, false, selected);
    document.getElementById(selectname).selectedIndex = 0;
  }
  document.getElementById("edit").contentWindow.focus();
}


function Start() {
  document.getElementById('edit').contentWindow.document.designMode = "on";
  try {
    document.getElementById('edit').contentWindow.document.execCommand("undo", false, null);
  }  catch (e) {
    alert("This demo is not supported on your level of Mozilla.");
  }
  InitToolbarButtons();
}

var timer1 = "";
var done = false;

function StartWorking(){
	timer1 = window.setInterval("PasteText()", 100);
}

function PasteText(){
	if (done == true) {
		window.clearInterval(timer1);
		timer1 = "";
		x = document.getElementById("leiras").value;
		y = window.edit.document.body;
		y.innerHTML = x;
	}
}

function copyValue() {	
	var theHTML=window.edit.document.body.innerHTML;
	document.getElementById("leiras").value = theHTML;
}

var formazas_legyen = true;

function formazasTipusa(ob) {
	if ( ob.checked == false ) {
		document.getElementById('toolbar1').style.display='none';
		document.getElementById('edit').style.display='none';
		document.getElementById('leiras').style.display='block';
		window.clearInterval(masolTexterea);
		formazas_legyen = false;
	} else {
		document.getElementById('toolbar1').style.display='block';
		document.getElementById('leiras').style.display='none';	
		document.getElementById('edit').style.display='block';
		formazas_legyen = true;
		PasteText();
		masolTexterea = window.setInterval("copyValue()", 500);
	}	
}
function bookmark(id) {
	document.getElementById('edit').contentWindow.document.execCommand("CreateLink",false, "#" + id);
	zar();
}
function belsolink(neve) {
	document.getElementById('edit').contentWindow.document.execCommand("CreateLink",false, neve);
	zar();
}

function fileneve(id,neve) {
	document.getElementById(id).value=neve;
	zar();
}
/*
newWindow=null;

function zar() {

	if(newWindow && !newWindow.closed && newWindow.open) {
		newWindow.close();
		newWindow=null;
	}
}
*/
function nyit2(URL, Width, Height) {

	zar();

	var Left = (screen.width-Width)/2;
	var Top = (screen.height-Height)/2;
	
	newWindow = window.open(URL,"nagyKepAblak","width="+Width+",height="+Height+",left="+Left+",top="+Top+", scrollbars=1");
	newWindow.focus();
}




