function getSel()
{
	if (window.getSelection)
	{
		return window.getSelection();
	}
	else if (document.getSelection)
	{
		return document.getSelection();
	}
	else if (document.selection)
	{
		return document.selection.createRange().text;
	}
	else return;
}

function setfocus()

{

document.forms[0].tx.select();

document.forms[0].tx.focus();

}



function format_sel(v) {
  var sel= getSel();
  //var sel = window.getSelection();
  var str = sel;
  document.saver.tx.focus();

  var sel = document.selection.createRange();

  sel.text = '<' + v + '>' + str + '</' + v + '>';

  return;

}



function copycb() {
/*
var sel= getSel();

var str = sel.text;
alert(str);
if (str == "") {
*/
  setfocus();
  textRange = document.saver.tx.createTextRange();
  textRange.execCommand("RemoveFormat");
  textRange.execCommand("Copy");
  alert("The whole text has been copied to your clipboard.\nUse Ctrl-V to paste it in another envionment.");

//}

//else {

//  sel.execCommand("Copy");


//}  

}



function new_doc() {

if(confirm('You will loose any text currently \nin the editor. Continue?'))   document.saver.tx.value="";

  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 insert_general() {

  var str = document.selection.createRange().text;

  document.saver.tx.focus();

  var my_tag = prompt('Enter the desired html tag such as SUP, or STRIKE. If you type DIV or SPAN,\nyou will be prompted to add an optional line of properties.','div');

  if (my_tag != null) {

    if (my_tag.toLowerCase() == 'div' || my_tag.toLowerCase() == 'span'){

		var my_prop = prompt('Enter the properties for your ' + my_tag.toUpperCase() + ' tag:', 'dir="rtl"');

  		if (my_prop != null) {

			var sel = document.selection.createRange();

			sel.text = '<' + my_tag + ' ' + my_prop + '>' + str + '</' + my_tag + '>';

		}

		else {

			var sel = document.selection.createRange();

			sel.text = '<' + my_tag + '>' + str + '</' + my_tag + '>';

		}

		

	}

	else {

		var sel = document.selection.createRange();

		sel.text = '<' + my_tag + '>' + str + '</' + my_tag + '>';

	}

  }

  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 = /J/gi;

	var fa = /Ì/gi;

	if (direction == 'a2f')

	     code = code.replace(ar, "Ì");

	else if (direction == 'f2a')

 	     code = code.replace(fa, "J")

	else

	    alert ( "Wrong direction" );



	document.saver.tx.value = code;

	return;

}






