//First lets initiate some variables

var isEditable= false;
var isIE;
var isMoz;
var isGecko;
var isSafari;
var isKonqueror;

//function killReturn() {
//  alert("ok");
//  key = event.keyCode;
//  if (key == 13) {
//    document.content.contentWindow.document.execCommand('paste', false, unescape("%0A"));
//    return false;
//  }
//}

function initiateEditor() {
 //check what browser is in use
 var browser = navigator.userAgent.toLowerCase();
 isIE = ((browser .indexOf( "msie" ) != -1) && (browser .indexOf( "opera" ) == -1) && (browser .indexOf( "webtv" ) == -1));
 isMoz = (browser .indexOf( "firefox" ) != -1);
 isOpe = (browser .indexOf( "opera" ) != -1);
 isGecko = (browser .indexOf( "gecko" ) != -1);
 isSafari = (browser .indexOf( "safari" ) != -1);
 isKonqueror = (browser.indexOf( "konqueror" ) != -1);
 
 //enable designMode if the browser is not safari or konqueror.
 if (document.getElementById && document.designMode && !isSafari && !isKonqueror) {
   isEditable= true;
 }
}
//Javascript function dislpayEditor will create the textarea.

function displayEditor(editor, html, width, height) {
   while(html.indexOf('<39>') >= 0) html = html.replace("<39>", "'");
   while(html.indexOf('<34>') >= 0) html = html.replace("<34>", '"');
   while(html.indexOf('<38>') >= 0) html = html.replace("<38>", "&");
   if(isEditable){
       document.writeln('<iframe class="txtpole" id="' + editor + '" name="' + editor + '" width="' + width + 'px" height="' + height + 'px"></iframe>');
//create a hidden field that will hold everything that is typed in the textarea
       document.writeln('<input type="hidden" id="hidden' + editor + '" name="hidden' + editor + '" value=""/>');
//assign html (textarea value) to hiddeneditor
      document.getElementById('hidden' + editor).value = html;
//call function designer
      designer(editor, html);
   }else{
     document.writeln('<textarea class="txtpole" name="' + editor + '" id="' + editor + '" cols="39" rows="10">' + html + '</textarea>');
   }
}

//this is designer function that enables designMode and writes defalut text to the text area
function designer(editor, html) {
    var browser = navigator.userAgent.toLowerCase();
    isIE = ((browser .indexOf( "msie" ) != -1) && (browser .indexOf( "opera" ) == -1) &&    (browser .indexOf( "webtv" ) == -1));
     var mainContent= "<html id=" + editor + "><head></head><body>" + html + "</body></html>" ;
//assign the frame(textarea) to the edit variable using that frames i
     var edit = document.getElementById(editor).contentWindow.document;
//write the content to the textarea
      edit.write(mainContent);
//enable the designMode
      edit.designMode =  "On" ;
//enable the designMode for Mozilla
    if(!isIE){
     document.getElementById(content).contentDocument.designMode = "on" ;
    }
}

//To execute command we will use javascript function execCommand. 
function editorCommand(editor, command, option) {
	if (isMoz && (command == "backcolor")) command = "hilitecolor";
	if (isOpe && (command == "backcolor")) command = "hilitecolor";
	if (command == "createlink") {
		option = document.getElementById('inplink').value;
		if (option.indexOf("http://") == -1) {
      option = "http://" + option;
		}
		option = "javascript:openLink('" + option + "')";
    if (option.indexOf("@") != -1) {
      option = "mailto:" + document.getElementById('inplink').value;
		  //option = "javascript:openLink('" + option + "')";
		}
	}
	if (command == "createlinkfile") {
		option = document.getElementById('inpfile').value;
		command = "createlink";
		//option = "mailto:" + option;
	}
	if (command == "forecolor") {
	  if (option == "") option = "purple";
	}
	if (command == "backcolor") {
	  if (option == "") option = "yellow";
	}
	//if (command == "createlinkfile") {
	//	command = "createlink";
	//}
  if ((command == "insertimage") && (option == "usr/..")) {option = ""; command = "";}
  //if (edit.smile.value == "img/emousm.gif") option = "img/emousm.gif";
	//option = "usr/favicon.ico" + '"' + " border=1";
  //option = "img/image.gif" + '"'+ "border = 1";
	/*if (command == "createlink") option = window.prompt("Zadejte adresu:", "http://");
	if (command == "forecolor") option = window.prompt("Zadejte barvu:", "#");*/
	// first we assign the content of the textarea to the variable mainField
	 var mainField;
          mainField = document.getElementById(editor).contentWindow;
 // then we will use execCommand to execute the option on the textarea making sure the textarea stays in focus
   try {
          mainField.focus();
          mainField.document.execCommand(command, false, option);
					if (command == "selectall") mainField.document.execCommand("delete", false, option);
          mainField.focus();
   } catch (e) { }
}

function updateEditor(editor) {
 if (!isEditable) return;
//assign the value of the textarea to the hidden field.
 var hiddenField = document.getElementById('hidden' + editor);
 if (hiddenField.value == null) hiddenField.value = "";
  hiddenField.value = document.getElementById(editor).contentWindow.document.body.innerHTML;
}

function colorChange(id) {
	optColor = id.options[id.selectedIndex].value;
}

function bgColorChange(id) {
	optBgColor = id.options[id.selectedIndex].value;
}

function sizeChange(id) {
	optSize = id.options[id.selectedIndex].value;
}

function imageChange(id) {
	optImage = id.options[id.selectedIndex].value;
}

function smileChange(id) {
	optImage = id.options[id.selectedIndex].value;
}

function linkFileChange(id) {
	optLinkFile = id.options[id.selectedIndex].value;
}

function fileChange(id) {
	optFile = id.options[id.selectedIndex].value;
	//alert(optFile);
}

function folderChange(id) {
	optFolder = id.options[id.selectedIndex].value;
	//alert(optFile);
}

function sizeImgChange(id) {
	optSizeImg = id.options[id.selectedIndex].value;
	//alert(optFile);
}

function disableTime() {
  if (document.edit.textakt.checked == true) {
    document.edit.textdat.disabled = true;
    document.edit.textcas.disabled = true;
  } else {
    document.edit.textdat.disabled = false;
    document.edit.textcas.disabled = false;
  }
}

function disableAuthor() {
  if (document.edit.radaut.checked == true) {
    document.edit.txtaut.disabled = false;
  } else {
    document.edit.txtaut.disabled = true;
  }
}

function decodeEntities(text){
//get the input
  t = text;
//remove carriage returns
  t = t.replace(new RegExp("\\=\r\n","gim"), "");
//replace urlencoded values
  t = unescape(t);
//replacement values
  var thechrs = new Array(
    ' ','!','"','#','$','%','&',"'",'(',')','*','+',',','-','.','/','0','1','2','3',
    '4','5','6','7','8','9',':',';','<','=','>','?','@','A','B','C','D','E','F','G', 
    'H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[', 
    '\\',']','^','_','`','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
    'p','q','r','s','t','u','v','w','x','y','z','{','|','}','~',' ','c','u','e','a',
    'a','a','a','c','e','e','e','i','i','i','a','a','e','ae','ae','o','o','o','u','u',
    'y','u','u','o','l','0','x','f','a','i','o','u','n','n','*','O','?','r','_','?',
    '?','!','<','>','_','_','_','|','|','a','a','a','c','|','|','+','+','c','y','+',
    '+','-','-','+','-','+','a','a','+','+','-','-','|','-','+','.','.','d','e','e',
    'e','i','i','i','i','+','+','_','_','|','i','_','o','o','o','o','o','o','u','p',
    'p','u','u','u','y','y','_',"'",'-','?','_','?','?','S','÷','¸','°','¨','·','¹',
    '³','²','_',' ');
//replace js numeric array values
  var l = 32;
  for (l = 32; l < 256; l++){
    t = t.replace(new RegExp("\\[" + l + ",","gim"),"[" + thechrs[l-32] + ",");
    t = t.replace(new RegExp("\\[" + l + ",","gim"),"[" + thechrs[l-32] + ",");
  }
  for (l = 32; l < 256; l++){
    t = t.replace(new RegExp("," + l + "\\]","gim"),"," + thechrs[l-32] + "]");
    t = t.replace(new RegExp("," + l + "\\]","gim"),"," + thechrs[l-32] + "]");
  }
  for (l = 32; l < 256; l++){
    t = t.replace(new RegExp("," + l + ",","gim"),"," + thechrs[l-32] + ",");
    t = t.replace(new RegExp("," + l + ",","gim"),"," + thechrs[l-32] + ",");
  }
  ta = t;
  t = t.replace(new RegExp("(\\[[^\\]]+)\\,([^\\]]+\\])","gim"), "$1$2");
  while(ta !== t){
    ta = t;
    t = t.replace(new RegExp("(\\[[^\\]]+)\\,([^\\]]+\\])","gim"), "$1$2");
  }
//replace entities
  var l = 32;
  for (l = 32; l < 100; l++){
    t = t.replace(new RegExp("&#" + l + ";","gim"),thechrs[l-32]);
    t = t.replace(new RegExp("&#0" + l + ";","gim"),thechrs[l-32]);
    t = t.replace(new RegExp("&#00" + l + ";","gim"),thechrs[l-32]);
  }
  var l = 100;
  for (l = 100; l < 256; l++){
    t = t.replace(new RegExp("&#" + l + ";","gim"),thechrs[l-32]);
    t = t.replace(new RegExp("&#0" + l + ";","gim"),thechrs[l-32]);
  }
//return results
  return t;
}		