﻿// JScript File
var command = "";
        function InitializeToolbar()
         {
            var DivElements = document.getElementsByTagName('DIV');
              for (var i=0; i < DivElements.length; i++) 
              {
                if (DivElements[i].className == "imagebutton") 
                {
                  DivElements[i].onmouseover = tbmouseover;
                  DivElements[i].onmouseout = tbmouseout;
                  DivElements[i].onmousedown = tbmousedown;
                  DivElements[i].onmouseup = tbmouseup;
                  DivElements[i].onclick = tbclick;
                }
             }
             fnSetRichTextContent();
         }
        function tbmousedown(e)
        {
             var EventX = e ? e : window.event; 

              //this.firstChild.style.left = 2;
              //this.firstChild.style.top = 2;
              this.style.border="inset 1px";
              if (EventX.returnValue) 
              {
                EventX.returnValue = false;
              } 
              else if (EventX.preventDefault)
              {
                EventX.preventDefault( );
              }
              else 
              {
                return false;
              }
        }
/*
        function tbmouseup()
        {
          this.firstChild.style.left = 1;
          this.firstChild.style.top = 1;
          this.style.border="outset 1px";
        }

        function tbmouseout()
        {
          this.style.border="solid 0px #dfefca";
        }

        function tbmouseover()
        {
          this.style.border="outset 1px #dfefca";
        }
*/
function tbmouseup()
        {
          //this.firstChild.style.left = 1;
          //this.firstChild.style.top = 1;
          //this.style.border="outset 1px";
          this.style.border="solid 1px #cce7af";
        }

        function tbmouseout()
        {
          
          //this.style.border="solid 0px #dfefca";
          this.style.border="solid 1px #cce7af";
        }

        function tbmouseover()
        {
          //this.style.border="outset 1px #dfefca";
           this.style.border="outset 1px #cce7af";
        }
  function insertNodeAtSelection(win, insertNode)
  {
      // get current selection
      var sel = win.getSelection();

      // get the first range of the selection
      // (there's almost always only one range)
      var range = sel.getRangeAt(0);

      // deselect everything
      sel.removeAllRanges();

      // remove content of current selection from document
      range.deleteContents();

      // get location of current selection
      var container = range.startContainer;
      var pos = range.startOffset;

      // make a new range for the new selection
      range=document.createRange();

      if (container.nodeType==3 && insertNode.nodeType==3) {

        // if we insert text in a textnode, do optimized insertion
        container.insertData(pos, insertNode.nodeValue);

        // put cursor after inserted text
        range.setEnd(container, pos+insertNode.length);
        range.setStart(container, pos+insertNode.length);

      } else {


        var afterNode;
        if (container.nodeType==3) {

          // when inserting into a textnode
          // we create 2 new textnodes
          // and put the insertNode in between

          var textNode = container;
          container = textNode.parentNode;
          var text = textNode.nodeValue;

          // text before the split
          var textBefore = text.substr(0,pos);
          // text after the split
          var textAfter = text.substr(pos);

          var beforeNode = document.createTextNode(textBefore);
          afterNode = document.createTextNode(textAfter);

          // insert the 3 new nodes before the old one
          container.insertBefore(afterNode, textNode);
          container.insertBefore(insertNode, afterNode);
          container.insertBefore(beforeNode, insertNode);

          // remove the old node
          container.removeChild(textNode);

        } else {

          // else simply insert the node
          afterNode = container.childNodes[pos];
          container.insertBefore(insertNode, afterNode);
        }

        range.setEnd(afterNode, 0);
        range.setStart(afterNode, 0);
      }

      sel.addRange(range);
  };

function getOffsetTop(elm) {

  var mOffsetTop = elm.offsetTop;
  var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetTop += mOffsetParent.offsetTop;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetTop;
}

function getOffsetLeft(elm) 
{
    var mOffsetLeft = elm.offsetLeft;
   var mOffsetParent = elm.offsetParent;

  while(mOffsetParent){
    mOffsetLeft += mOffsetParent.offsetLeft;
    mOffsetParent = mOffsetParent.offsetParent;
  }
 
  return mOffsetLeft;
}

function tbclick()
{

 if (this.id == "createlink") 
 {
    var szURL = prompt("Enter a URL:", "http://");
    if ((szURL != null) && (szURL != ""))
     {
      document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.execCommand("CreateLink",false,szURL);
     }
  } 
  else if (this.id == "removelink") 
  {
  //alert("hi");
    document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.execCommand("Unlink",false,"");
  }
  else if (this.id == "createimage")
  {
    imagePath = prompt('Enter Image URL:', 'http://');
    if ((imagePath != null) && (imagePath != "")) 
    {
      document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.execCommand('InsertImage', false, imagePath);
    }
  } 
  else if (this.id == "insertsmiley")
  {
    dlgColorPalette('insertsmiley');
  }
  else
   {
    document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.execCommand(this.id, false, null);
  }
}

function Select(selectname)
{
  var cursel = document.getElementById(selectname).selectedIndex;
  /* First one is always a label */
  if (cursel != 0)
   {
    var selected = document.getElementById(selectname).options[cursel].value;
    document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.execCommand(selectname, false, selected);
   }
  document.getElementById("ctl00_ContentPlaceHolder1_RichTextEditor").contentWindow.focus();
}

function dismisscolorpalette()
{
  //document.getElementById("colorpalette").style.visibility="hidden";
}

function Start() 
{   
   //alert("called")
    //alert(document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor'))
    if (document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor') !=null)
    {
       // alert(document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.designMode)
         document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.designMode = "on";
        //alert(document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.designMode)
      try
       {
        //document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.execCommand("undo", false, null);
            
          document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.focus();
        }
        catch (e) 
        {
//           alert(e.toString())
            alert("This functionality is not supported on your level of Mozilla.");
        }

      InitializeToolbar();
      if (document.addEventListener) 
      {
        document.addEventListener("mousedown", dismisscolorpalette, true);
        document.getElementById("ctl00_ContentPlaceHolder1_RichTextEditor").contentWindow.document.addEventListener("mousedown", dismisscolorpalette, true);
        document.addEventListener("keypress", dismisscolorpalette, true);
        document.getElementById("ctl00_ContentPlaceHolder1_RichTextEditor").contentWindow.document.addEventListener("keypress", dismisscolorpalette, true);
      }
      else if (document.attachEvent)
       {
        document.attachEvent("mousedown", dismisscolorpalette, true);
        document.getElementById("ctl00_ContentPlaceHolder1_RichTextEditor").contentWindow.document.attachEvent("mousedown", dismisscolorpalette, true);
        document.attachEvent("keypress", dismisscolorpalette, true);
        document.getElementById("ctl00_ContentPlaceHolder1_RichTextEditor").contentWindow.document.attachEvent("keypress", dismisscolorpalette, true);
       }
     }
}

function fnSetRichTextContent()
{
    
    if (navigator.appVersion.indexOf("MSIE")!=-1)
    {
        //if(document.getElementById("ctl00_ContentPlaceHolder1_RichTextEditor") !=null)
        //{
            window.frames['ctl00_ContentPlaceHolder1_RichTextEditor'].document.write("editing content"); 
        //}
      // Please do not delete or comment this line as it requires the frame to be focused (in write mode) to have the text displayed during page load.
       //In case if required to comment this (that is not necessary),please write another function which comment this line and call it in appropriate places.
       //alert(document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document)
       if ( document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.body != null)
       {
             //alert("hi")   
             document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.body.innerHTML = document.getElementById("ctl00_ContentPlaceHolder1_hidRichTextContent").value;
       }
            // alert(document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.body.innerHTML)
     }
     else
     {      
        if (document.getElementById("ctl00_ContentPlaceHolder1_hidRichTextContent").value !='')
        {        
          document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow.document.body.innerHTML = document.getElementById("ctl00_ContentPlaceHolder1_hidRichTextContent").value;
        }
     }
 }
 // edited by nitin on 17 feb
 function dlgColorPalette(command) {
	//function to display or hide color palettes
	
	//get dialog position
	var oDialog = document.getElementById('cprte1');
	
	var buttonElement = document.getElementById(command);
	//var buttonElement = document.getElementById( rte);
    //var iLeftPos = getOffsetLeft(buttonElement)+ getOffsetLeft( document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor'));
    	
	//var iLeftPos = getOffsetLeft(rte);
	//var iTopPos = getOffsetTop(buttonElement) + getOffsetTop( document.getElementById(rte)) + (buttonElement.offsetHeight + 4);
	var iTopPos = getOffsetTop( document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor')) ;
	
	
	oDialog.style.left =   "360px";
	oDialog.style.top = (iTopPos) + "px";

	//if (command == parent.command) {
	if (command == "insertsmiley") {
		//if current command dialog is currently open, close it

		if (oDialog.style.visibility == "hidden") {

			showHideElement(oDialog, 'show');
		}
		 else {
		  
			showHideElement(oDialog, 'hide');
		}
	}
	else {
		//if opening a new dialog, close all others
		  showHideElement('cprte1', 'hide');
		}
		
	setRange();
	
	//save current values
	parent.command = command;
	currentRTE = document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor');
	
	
}

function setRange() {
	//function to store range of current selection
	var oRTE;
	if (document.all) {
	
		oRTE = frames['ctl00_ContentPlaceHolder1_RichTextEditor'];
		var selection = oRTE.document.selection; 
		if (selection != null) rng = selection.createRange();
	} else {
		oRTE = document.getElementById('ctl00_ContentPlaceHolder1_RichTextEditor').contentWindow;
		var selection = oRTE.getSelection();
		rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
}

function getOffsetLeft(elm) {
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	var parents_up = 2;
	
	while(parents_up > 0) {
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
		parents_up--;
	}
	
	return mOffsetLeft;
}
function showHideElement(element, showHide) {
	//function to show or hide elements
	//element variable can be string or object
	if (document.getElementById(element)) {
		element = document.getElementById(element);
	}
	
	if (showHide == "show") {
		element.style.visibility = "visible";
	} else if (showHide == "hide") {
		element.style.visibility = "hidden";
	}
}
