function setfocus()
{
document.forms[0].tx.select();
document.forms[0].tx.focus();
}

function format_sel(v) {
  var str = document.selection.createRange().text;
  document.saver.tx.focus();
  var sel = document.selection.createRange();
  sel.text = '<' + v + '>' + str + '</' + v + '>';
  return;
}

function copycb() {
var sel= document.selection.createRange();
var str = sel.text;
if (str == "") {
  setfocus();
  textRange = document.saver.tx.createTextRange();
  textRange.execCommand("RemoveFormat");
  textRange.execCommand("Copy");
  if (aCopy == 'checked')
    alert("The whole text has been copied to your clipboard.\nUse Ctrl-V to paste it in any other envionment.");
}
else {
  sel.execCommand("Copy");
}  
}

function new_doc() {
if (aNew == 'checked'){
if(confirm('You will loose any text currently \nin the editor. Continue?'))   document.saver.tx.value="";
  document.saver.tx.focus();
} else {
  document.saver.tx.focus();
}
  return;
}

function save_doc(stext){
  
  stext = stext.replace(/\n/g, '<br>');
  SaveFrame.document.open("text/html","replace");
  SaveFrame.document.write(stext);
  SaveFrame.document.close();
  SaveFrame.focus();
  SaveFrame.document.execCommand('SaveAs', false, 'lampoditor.txt');
}

function insert_link2() { 
var sel = document.selection.createRange();
var str = sel.text
if ( str!=""){
  SaveFrame.document.open("text/html","replace");
  SaveFrame.document.write(sel.text);
  SaveFrame.document.close();
  SaveFrame.focus();
  iText = SaveFrame.document.selection.createRange();
    //Create link
     SaveFrame.document.execCommand("CreateLink");
     //Replace text with URL
     alert( iText.parentElement().tagName);
     //if (iText.parentElement().tagName == "A"){
       alert(iText.parentElement().href);
       sel.text = '<a href=\"' + iText.parentElement().href + '\">' + str + '</a>'; 
     //}    
  }
else{
    alert("Please select some blue text!");
  }   

}


function insert_link() {
  var str = document.selection.createRange().text;
  document.saver.tx.focus();
  var my_link = prompt('Enter URL:','http://');
  if (my_link != null) {
    var sel = document.selection.createRange();
	sel.text = '<a href=\"' + my_link + '\">' + str + '</a>';
  }
  return;
}

function insert_pic() {
  document.saver.tx.focus();
  var my_pic = prompt('Enter URL for picture:','http://');
  if (my_pic != null) {
    var sel = document.selection.createRange();
	sel.text = '<img src="' + my_pic + '">';
  }
  return;
}

function mouseover(el) {
  el.className = 'raised';
}

function mouseout(el) {
  el.className = 'tlbbutton';
}

function mousedown(el) {
  el.className = 'pressed';
}

function mouseup(el) {
  el.className = 'raised';
}

function clear_textbox(popuptext)
{
if (document.saver.tx.value == popuptext)
document.saver.tx.value = "";
} 


// following code is from http://www.intelimen.com.br/lib/editor/index.php
// edited a little bit by me for Lampoditor

function clean_HTML(code) {
// removes all Class attributes on a tag eg. '<p class=asdasd>xxx</p>' returns '<p>xxx</p>'
   code = code.replace(/<([\w]+) class=([^ |>]*)([^>]*)/gi, "<$1$3"); 
// removes all style attributes eg. '<tag style="[some style]" [other attribs]>' returns '<tag [other attribs]>'
   code = code.replace(/<([\w]+) style="([^"]*)"([^>]*)/gi, "<$1$3"); 
// removes all xml stuff... <xml>,<\xml>,<xml> or <\?xml>
   code = code.replace(/<\\?\??xml[^>]>/gi, ""); 
// removes all ugly colon tags <a:b> or </a:b>
   code = code.replace(/<\/?\w+:[^>]*>/gi, ""); 
// removes all empty <p> tags
   code = code.replace(/<p([^>])*>(&nbsp;)*\s*<\/p>/gi,""); 
// removes all empty span tags
   code = code.replace(/<span([^>])*>(&nbsp;)*\s*<\/span>/gi,""); 
   return code
}

function replace(direction)
{
	var code = document.saver.tx.value;
	var ar = /ي/gi;
	var fa = /ی/gi;
	if (direction == 'a2f')
	     code = code.replace(ar, "ی");
	else if (direction == 'f2a')
 	     code = code.replace(fa, "ي")
	else
	    alert ( "Wrong direction" );

	document.saver.tx.value = code;
	return;
}


