function backColorSelect(id)
{
    showDialog(id, "BackColor", "ColorTable");
}

function foreColorSelect(id)
{
    showDialog(id, "ForeColor", "ColorTable");
}

function fontSelect(id)
{
    showDialogForCtrl(id, "FontName", "FontTable", document.getElementById(id + "FontNameLabel"));
}

function fontSizeSelect(id)
{
    showDialogForCtrl(id, "FontSize", "FontSizeTable", document.getElementById(id + "FontSizeLabel"));
}

var lastTextRange = null;

function showDialogForCtrl(id, cmd, dialogName, ctrl) 
{
  lastTextRange = document.selection.createRange();
  hideDialogs();
  var but = document.getElementById(id + cmd + "Cmd");
  var pan = document.getElementById(dialogName);
  pan.cmd = cmd;
  pan.callerId = but.id;
  pan.paramId = id;

  var pTop = ctrl.offsetTop + ctrl.offsetHeight;
  var pLeft = ctrl.offsetLeft;
  var lastTop = 0;
  var lastLeft;
  var elem = ctrl.offsetParent;

  while (elem != null) {
    pLeft += elem.offsetLeft != null ? elem.offsetLeft : 0;
    pTop += elem.offsetTop != null ? elem.offsetTop : 0;
    elem = elem.offsetParent;
  }

  pan.style.left = pLeft;
  pan.style.top = pTop;
  pan.style.display = "inline";
  but.className = "pressedToolbarLink";

  for (i = 0; i < pan.firstChild.rows.length; i++) {
    pan.firstChild.rows[i].firstChild.selected="false";
    pan.firstChild.rows[i].firstChild.style.backgroundColor = "white";
  }
  // must setup focus to edit conbtrol before asking what text style is
  var ctrl = document.getElementById(id + "InputField");
  ctrl.setActive();
//  ctrl.focus();
  
  // because 0 == "" next statement is false for black color (val != "")
  var val = new String(document.queryCommandValue(cmd));     

  if (val != null && val != "" && document.getElementById(val) != null) {
    document.getElementById(val).selected="true";
    document.getElementById(val).style.backgroundColor="gainsboro";
  }

  //checkCommand(id);
}

function showDialog(id, cmd, dialogName)
{
  var img = document.getElementById(id + cmd + "Img");
  showDialogForCtrl(id, cmd, dialogName, img);
}

function hideDialogs()
{
  hideDialog("ColorTable", "FontColor");
  hideDialog("FontTable", "FontName");
  hideDialog("FontSizeTable", "FontSize");
}

function cmd(id, val)
{
  var ctrl = document.getElementById(id + "InputField");
  ctrl.setActive();
  //ctrl.focus();
  document.execCommand(val);
  checkCommand(id);
}

function tableCmd(dialogName, cmd, val)
{
  var tab = document.getElementById(dialogName);

  var ctrl = document.getElementById(tab.paramId + "InputField");
  ctrl.setActive();
  //ctrl.focus();

  if (lastTextRange != null && lastTextRange.htmlText.length > 0) {
    lastTextRange.select();
  }

  document.execCommand(cmd, "false", val);
  checkCommand(tab.paramId);
  hideDialog(dialogName, cmd);
}

function hideDialog(dialogName, cmdName)
{
  document.getElementById(dialogName).style.display = "none";
  var cid = document.getElementById(dialogName).callerId;

  if (cid != null && cid != "") {
    var el = document.getElementById(cid);
    el.className = "toolbarLink";
  }
}

function checkCommand(id)
{
  markCommand(id, "Bold");
  markCommand(id, "Italic");
  markCommand(id, "Underline");
  markCommand(id, "JustifyLeft");
  markCommand(id, "JustifyRight");
  markCommand(id, "JustifyCenter");
  markCommand(id, "JustifyFull");
  markCommand(id, "InsertOrderedList");
  markCommand(id, "InsertOrderedList");
  markCommand(id, "InsertUnorderedList");
  
  markCommandLabeled(id, "FontName");
  markCommandLabeled(id, "FontSize");
  
  document.getElementById(id).value = document.getElementById(id + "InputField").innerHTML;
}

function markCommand(id, cmd)
{
  var but = document.getElementById(id + cmd + "Cmd");

  if (document.queryCommandValue(cmd))
    but.className = "pressedToolbarLink";
  else
    but.className = "toolbarLink";
}

function markCommandLabeled(id, cmd) {
  //if (window.event && window.event.srcElement.contentEditable != "true")  { return; }

  var label = document.getElementById(id + cmd + "Label");

  var ctrl = document.getElementById(id + "InputField");
  ctrl.setActive();
//  ctrl.focus();
  
  var val = new String(document.queryCommandValue(cmd));
  
  if (val != null && val != "" ) {
    label.innerText = val;
  }  
  else {
    //label.innerText = "Font Name";
  }
}

function we_onkeydown(ctrl)
{
    var maxLength = new Number(ctrl.maxLength);
    if(maxLength > 0 && ctrl.innerHTML.length >= maxLength) 
    {
        if (!window.event.ctrlKey || !window.event.altKey )
        {
            var pressedkey = /[a-zA-Z0-9\.\,\/]/
            if (pressedkey.test(String.fromCharCode(window.event.keyCode)))
            {
                window.event.returnValue = false;
            }
        }
    }    
}

function we_restrict(ctrl)
{
  var maxLength = new Number(ctrl.maxLength);
  if(maxLength > 0 && ctrl.innerHTML.length >= maxLength) 
  {
      ctrl.innerHTML = ctrl.innerHTML.substr(0, maxLength);
  }
}

function SpellSource(id)
{
	this.ctrlID = id + "InputField";
  
	this.get_text = function()
	{
		var ctrl = document.getElementById(this.ctrlID);
		return ctrl.innerHTML;
	}
	this.set_text = function(text)
	{
		var ctrl = document.getElementById(this.ctrlID);
		ctrl.innerHTML = text;
	}
}

function spellCheck(id)
{
	var spell = GetRadSpell(RadSpellControlID);
	
	spell.set_textSource(new SpellSource(id));
	spell.startSpellCheck();
}  


