//var bodyTag="<BODY MONOSPACE bgcolor=\"#FFFFCC\"  STYLE=\"FONT-SIZE: 10pt; FONT-FAMILY: Arial, Sans-Serif;\">"
var bodyTag="<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"css/index.css\"><BODY>"
var bTextMode=false;
var myEditor=new Editor;


function RunCom(what,opt) {
  if (!isRTextMode()) return;

  mytext.focus(); 
 
  if (opt=="removeFormat") {
    what=opt;
    opt=null;
  }

  if (opt==null) mytext.document.execCommand(what);
  else mytext.document.execCommand(what,"",opt);
  
  mytext.focus();
}

//Switches between text and html mode.
function setMode(newMode) {
  bTextMode = newMode;
  var cont;
  if (bTextMode) 
  {
    cleanHtml();
    cleanHtml();

    cont=mytext.document.body.innerHTML;
    mytext.document.body.innerText=cont;
  } 
  else 
  {
    cont=mytext.document.body.innerText;
    mytext.document.body.innerHTML=cont;
  }
  
  mytext.focus();
}


function InitBtn(btn) {
  btn.onmouseover = BtnMouseOver;
  btn.onmouseout = BtnMouseOut;
  btn.onmousedown = BtnMouseDown;
  btn.onmouseup = BtnMouseUp;
  btn.ondragstart = YCancelEvent;
  btn.onselectstart = YCancelEvent;
  btn.onselect = YCancelEvent;
  btn.YUSERONCLICK = btn.onclick;
  btn.onclick = YCancelEvent;
  btn.YINITIALIZED = true;
  return true;
}


function YCancelEvent() {
  event.returnValue=false;
  event.cancelBubble=true;
  return false;
}

// Toolbar button onmouseover handler
function BtnMouseOver() {
  if (event.srcElement.tagName != "IMG") return false;
  var image = event.srcElement;
  var element = image.parentElement;
  
  // Change button look based on current state of image.
  if (image.className == "Ico") element.className = "BtnMouseOverUp";
  else if (image.className == "IcoDown") element.className = "BtnMouseOverDown";

  event.cancelBubble = true;
}

// Toolbar button onmouseout handler
function BtnMouseOut() {
  if (event.srcElement.tagName != "IMG") {
    event.cancelBubble = true;
    return false;
  }

  var image = event.srcElement;
  var element = image.parentElement;
  yRaisedElement = null;
  
  element.className = "Btn";
  image.className = "Ico";

  event.cancelBubble = true;
}

// Toolbar button onmousedown handler
function BtnMouseDown() {
  if (event.srcElement.tagName != "IMG") {
    event.cancelBubble = true;
    event.returnValue=false;
    return false;
  }

  var image = event.srcElement;
  var element = image.parentElement;

  element.className = "BtnMouseOverDown";
  image.className = "IcoDown";

  event.cancelBubble = true;
  event.returnValue=false;
  return false;
}

// Toolbar button onmouseup handler
function BtnMouseUp() {
  if (event.srcElement.tagName != "IMG") {
    event.cancelBubble = true;
    return false;
  }

  var image = event.srcElement;
  var element = image.parentElement;

  if (element.YUSERONCLICK) eval(element.YUSERONCLICK + "anonymous()");

  element.className = "BtnMouseOverUp";
  image.className = "Ico";

  event.cancelBubble = true;
  return false;
}

function createLink() {
  //Is View Source is Checked!
  if (!isRTextMode()) return;
  
  mytext.focus();
  
  var isA = getEl("A",mytext.document.selection.createRange().parentElement());
       
  var str=prompt("Enter URL :", isA ? isA.href : "http:\/\/");
  
  //if str selection type is None!(If the user didn't block the string) then
  // get the string and add the <A HREF and paste it.  
  if ((str!=null) && (str!="http://")) {
    if (mytext.document.selection.type=="None") {    	
      var sel=mytext.document.selection.createRange();
      sel.pasteHTML("<A HREF=\""+str+"\" target=\"_blank\">"+str+"</A> ");
      sel.select();
    }
    else 
    //If user had selected/blocked the string  just pass this command.
    RunCom("CreateLink",str);
  }
  else 
  //If nothing entered just Focust in our IFRAME.
  mytext.focus();
}

// Check if toolbar is being used when in text mode
function isRTextMode() {
  if (! bTextMode) return true;
  alert("Please uncheck the \"View HTML source\" checkbox.");
  mytext.focus();
  return false;
}

//Finds and returns an element.
function getEl(sTag,start) {
 //Copy the selected character to "start" string while start!=NULL && tagName doesn't have 'A' 
  while ((start!=null) && (start.tagName!=sTag)) start = start.parentElement;
  return start;
}

function cleanHtml() {
  var fonts = mytext.document.body.all.tags("FONT");
  var curr;
  for (var i = fonts.length - 1; i >= 0; i--) {
    curr = fonts[i];
    if (curr.style.backgroundColor == "#ffffff") curr.outerHTML = curr.innerHTML;
  }
}






function Editor() {
  this.SetHtml=_SetHtml;
  this.GetHtml=_GetHtml;
  this.SetText=_SetText;
  this.GetText=_GetText;
  this.SetFocus=_SetFocus;
}

function _SetFocus() {
  mytext.focus();
}

function _GetText() {
  return mytext.document.body.innerText;
}

function _SetText(text) {
  text = text.replace(/\n/g, "<br>")
  mytext.document.body.innerHTML=text;
}

function _GetHtml() {
  if (bTextMode) 
    return mytext.document.body.innerText;
  else {
    cleanHtml();
    cleanHtml();
    return mytext.document.body.innerHTML;
  }
}

function _SetHtml(sVal) {
  if (bTextMode) mytext.document.body.innerText=sVal;
  else mytext.document.body.innerHTML=sVal;
}

function InitEditor()
{
var i, curr
for (i=0; i<document.body.all.length; i++) 
{
	curr=document.body.all[i];
	if (curr.className == "Btn") 
	{
		InitBtn(curr)
	}
}
mytext.document.clear();
mytext.document.open();
mytext.document.write(bodyTag);
mytext.document.close();
mytext.document.designMode="On";
}

