
//include all js and css files used in this editor
document.write("<link rel='stylesheet' type='text/css' href='"+HTTP_PATH_GURU_RTE+"picker.css' />");
document.write("<script src='"+HTTP_PATH_GURU_RTE+"picker.js' type='text/javascript'></script>");



//define global variables
var editorUrl  
var fileUploadPage;
var cp;
var isIE;
var isGecko;
var isSafari;
var isKonqueror;
var isOpera;
var editMode = "";
var allRTEs = "";
var maxMode = false;
var defaultWidth,defaultHeight


/*
 * This function initialize the Rich text editor values.
 * For multiple instances of RTE this function should be called once in a page
*/
function initRTEs(rtePath,strPage)
{
  var browser = navigator.userAgent.toLowerCase();
  isIE = ((browser .indexOf( "msie" ) != -1) && (browser .indexOf( "opera" ) == -1) && (browser .indexOf( "webtv" ) == -1));
  isOpera = (browser .indexOf( "opera" ) != -1);
  isGecko = (browser .indexOf( "gecko" ) != -1);
  isSafari = (browser .indexOf( "safari" ) != -1);
  isKonqueror = (browser.indexOf( "konqueror" ) != -1);
   
  //set editor path
  editorUrl = rtePath;
  fileUploadPage = strPage;
   
  //required for color picker
  //cp = new ColorPicker();	 
  //cp.writeDiv();
   
}

/*
 *This function is created by cesariel to fix the problem in Bold image that is not showing
 */
function preload()
{
    var img = ["Bold.gif"];
    imgobj = new Image();
    var i = 0;
    for( i=0; i <= img.length; i++)
     {
         
         imgobj.src= editorUrl + "images/" + img[i];
     }
}

	 
/*
 * This is the main function to create editor. This set default toolbar , create ifrmaes , hiddens etc.
 *
*/
function createEditor(rteId,rtewidth,rteheight)
{
 defaultWidth = rtewidth;
 defaultHeight = rteheight;

var strToolBar="<span id=\""+rteId+"fontstyle\"><select id=\""+rteId+"fontSelector\" name=\""+rteId+"fontstyleList\" style=\"font-family:Verdana; font-size:11px;color:#000000; border:1px solid #999999; \" onchange=\"editorCommand('"+rteId+"','FontName', this.value);\" ><option selected=\"selected\" value=\"\">[Font]</option>  <option value=\"Arial\">Arial</option><option value=\"Courier New\">Courier New</option><option value=\"Georgia\">Georgia</option><option value=\"Times New Roman\">Times New Roman</option><option value=\"Verdana\">Verdana</option></select></span><span id=\""+rteId+"fontsize\"><select id=\""+rteId+"sizeSelector\" name=\""+rteId+"fontsizeList\" style=\"font-family:Verdana; font-size:11px;color:#000000; border:1px solid #999999; \" onchange=\"editorCommand('"+rteId+"','FontSize', this.value);\"><option selected=\"selected\" value=\"\">[Size]</option><option value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option></select></span><span id=\""+rteId+"width\" style=\"font-family:Verdana; font-size:11px;color:#000000;\">&nbsp; W &nbsp;<input type=\"text\" name=\""+rteId+"txtWidth\" id=\""+rteId+"txtWidth\" value=\""+rtewidth+"\" onchange=\"setDimension('"+rteId+"','width',this.value);\" size=\"5\" style=\"font-family:Verdana; font-size:11px;color:#000000; border:1px solid #999999; \" onKeyPress=\"return onKeyPressNumbers(event);\"  maxlength=\"4\" /></span><span id=\""+rteId+"height\" style=\"font-family:Verdana; font-size:11px;color:#000000;\">&nbsp; H &nbsp;<input type=\"text\" name=\""+rteId+"txtHeight\" id=\""+rteId+"txtHeight\" value=\""+rteheight+"\" onchange=\"setDimension('"+rteId+"','height',this.value);\" size=\"5\" style=\"font-family:Verdana; font-size:11px;color:#000000; border:1px solid #999999; \" onKeyPress=\"return onKeyPressNumbers(event);\" maxlength=\"4\" /></span><br /><span id=\""+rteId+"boldButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Bold')\"><img src=\""+editorUrl+"images/Bold.gif\" border=\"0\" alt=\"Bold\" title=\"Bold\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"italicButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Italic')\"><img src=\""+editorUrl+"images/Italic.gif\" border=\"0\" alt=\"Italic\" title=\"Italic\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"underlineButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Underline')\"><img src=\""+editorUrl+"images/under.gif\" border=\"0\" alt=\"Underline\" title=\"Underline\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"strikeButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','StrikeThrough')\"><img src=\""+editorUrl+"images/Strikethrough.gif\" alt=\"Strikethrough\" title=\"Strikethrough\" border=\"0\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"superscriptButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','SuperScript')\"><img src=\""+editorUrl+"images/Superscript.gif\" border=\"0\" alt=\"Superscript\" title=\"Superscript\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"subscriptButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','SubScript')\"><img src=\""+editorUrl+"images/Subscript.gif\" border=\"0\" alt=\"Subscript\" title=\"Subscript\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"leftButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','JustifyLeft')\"><img src=\""+editorUrl+"images/left.gif\" border=\"0\" alt=\"Left align\" title=\"Left align\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"centerButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','JustifyCenter')\"><img src=\""+editorUrl+"images/Center.gif\" border=\"0\" alt=\"Center align\" title=\"Center align\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"rightButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','JustifyRight')\"><img src=\""+editorUrl+"images/right.gif\" border=\"0\" alt=\"Right align\" title=\"Right align\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"justifyButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','JustifyFull')\"><img src=\""+editorUrl+"images/justifyfull.gif\" border=\"0\" alt=\"Justify\" title=\"Justify\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"horizontalRuleButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','InsertHorizontalRule')\"><img src=\""+editorUrl+"images/HR.gif\" border=\"0\" alt=\"Horizontal Rule\" title=\"Horizontal Rule\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"orderlistButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','InsertOrderedList')\"><img src=\""+editorUrl+"images/numlist.gif\" border=\"0\" alt=\"Ordered List\" title=\"Ordered List\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"unorderlistButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','InsertUnorderedList')\"><img src=\""+editorUrl+"images/bullist.gif\" border=\"0\" alt=\"UnOrdered List\" title=\"UnOrdered List\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"indentButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Indent')\"><img src=\""+editorUrl+"images/inindent.gif\" border=\"0\" alt=\"Indent\" title=\"Indent\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"outdentButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Outdent')\"><img src=\""+editorUrl+"images/outdent.gif\" border=\"0\" alt=\"Outdent\" title=\"Outdent\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"ForeColor\" onClick=\"openPicker('"+rteId+"ForeColor','"+rteId+"','ForeColor');\" style=\"cursor:pointer\"> <img src=\""+editorUrl+"images/textcolor.gif\" border=\"0\" alt=\"Text Color\" title=\"Text Color\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"BackColor\" onClick=\"openPicker('"+rteId+"BackColor','"+rteId+"','BackColor');\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/bgcolor.gif\" border=\"0\" alt=\"Background Color\" title=\"Background Color\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"linkButton\" style=\"cursor:pointer\" onclick=\"addLink('"+rteId+"')\"><img src=\""+editorUrl+"images/hyperlink.gif\" border=\"0\" alt=\"Hyperlink\" title=\"Hyperlink\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"unlinkButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Unlink')\"><img src=\""+editorUrl+"images/Link.gif\" border=\"0\" alt=\"Remove link\" title=\"Remove link\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"imageButton\" onclick=\"addImage('"+rteId+"','image')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/imageLocal.gif\" border=\"0\" alt=\"Upload Image\" title=\"Upload Image\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"mediaButton\" onclick=\"addImage('"+rteId+"','media')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/flashicon.jpg\" border=\"0\" alt=\"Upload Flash media\" title=\"Upload Flash media\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"objectButton\" onclick=\"addObject('"+rteId+"')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/embedobj.jpg\" border=\"0\" alt=\"Embed Object\" title=\"Embed Object\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"characterButton\" onclick=\"insertCharacters('"+rteId+"')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/special_chars.gif\" border=\"0\" alt=\"Special Characters\" title=\"Special Characters\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"tableButton\" onclick=\"dlgInsertTable('"+rteId+"')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/table.gif\" border=\"0\" alt=\"Table\" title=\"Table\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"cutButton\" onclick=\"editorClipboard('"+rteId+"','Cut')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/Cut.gif\" border=\"0\" alt=\"Cut\" title=\"Cut\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"copyButton\" onclick=\"editorClipboard('"+rteId+"','Copy')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/Copy.gif\" border=\"0\" alt=\"Copy\" title=\"Copy\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"pasteButton\" onclick=\"editorClipboard('"+rteId+"','Paste')\" style=\"cursor:pointer\"><img src=\""+editorUrl+"images/Paste.gif\" border=\"0\" alt=\"Paste\" title=\"Paste\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"undoButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Undo')\"><img src=\""+editorUrl+"images/Undo.gif\" border=\"0\" alt=\"Undo\" title=\"Undo\" /></span>&nbsp; &nbsp;<span id=\""+rteId+"redoButton\" style=\"cursor:pointer\" onclick=\"editCommand('"+rteId+"','Redo')\"><img src=\""+editorUrl+"images/redo.gif\" border=\"0\" alt=\"Redo\" title=\"Redo\" /></span><span style=\"font-family:Verdana; font-size:12px;\" id=\""+rteId+"sourceButton\"><input type=\"checkbox\" id=\""+rteId+"changeView\" value=\"1\" onclick=\"viewSource('"+rteId+"')\"/>HTML</span>&nbsp; &nbsp;&nbsp;<span style=\"font-family:Verdana; font-size:12px;\" id=\""+rteId+"previewButton\"><input type=\"checkbox\" id=\""+rteId+"chkPreview\" value=\"1\" onclick=\"preview('"+rteId+"')\"/>PREVIEW</span>&nbsp; &nbsp;<span style=\"font-family:Verdana; font-size:12px; cursor:pointer;\" id=\""+rteId+"browsersupport\" onclick=\"browserList()\"><img src=\""+editorUrl+"images/brosersupport.jpg\" border=\"0\" alt=\"Browser support\" title=\"Browser support\" /></span>&nbsp; &nbsp;<span style=\"font-family:Verdana; font-size:12px; cursor:pointer;\" id=\""+rteId+"maximize\" onclick=\"maximizeMe('"+rteId+"')\"><img id=\""+rteId+"imgMax\" src=\""+editorUrl+"images/maximize.gif\" border=\"0\" alt=\"Maximize\" title=\"Maximize\" /></span>";

//call this functions to preload images not get by the browser
preload();

//div which hold editor
var editorDiv  = document.getElementById(rteId+"Container");

/*//create hidden which hold editor data
var editHidden = document.createElement("input");
editHidden.setAttribute("type","hidden");
editHidden.setAttribute("name",rteId+"Hidden");
editHidden.setAttribute("id",rteId+"Hidden");
editorDiv.appendChild(editHidden);
//set hidden value
document.getElementById(rteId+"Hidden").value = strHTML;*/


//create toolbar div
var toolDiv =  document.createElement("div");
toolDiv.setAttribute("id","tool"+rteId);
toolDiv.style.width = rtewidth +"px";
toolDiv.style.backgroundColor = "#CCCCCC";
toolDiv.style.border = "1px solid #CCCCCC";
toolDiv.innerHTML = strToolBar;	
editorDiv.appendChild(toolDiv);

//create editor iframe
var editFrame = document.createElement("iframe");
editFrame.setAttribute("id",rteId);
editFrame.setAttribute("width",rtewidth+"px");
editFrame.setAttribute("height",rteheight+"px");
editFrame.frameBorder = 0;
editFrame.style.border = "1px solid #CCCCCC";
editorDiv.appendChild(editFrame);

//create preview iframe
var previewFrame = document.createElement("iframe");
previewFrame.setAttribute("id","preview"+rteId);
previewFrame.setAttribute("width",rtewidth+"px");
previewFrame.setAttribute("height",rteheight+"px");
previewFrame.frameBorder = 0;
//previewFrame.src = HTTP_PATH_GURU_RTE+"page.html";
previewFrame.style.display = "none";
previewFrame.style.border = "1px solid #CCCCCC";
editorDiv.appendChild(previewFrame);

//update allRTEs 
 if (allRTEs.length > 0) allRTEs += ":";
 allRTEs += rteId;
 
 //create main body of editor
 designEditor(rteId);


}
//end of function


/*
 * This function used to set needed editor buttons.
 *
*/
function setToolBar(rte,fStyle,fSize,bButton,iButton,uButton,stkButton,spButton,sbButton,lButton,cButton,rButton,jButton,hrButton,olButton,ulButton,inButton,odButton,fcButton,bcButton,lkButton,unlButton,imgButton,mdButton,objButton,chrButton,tblButton,cutButton,copyButton,pastButton,unButton,reButton,scButton,prButton,tWidth,tHeight)
{
    
  //you can show toolbar controls accroding your requirement
  
  document.getElementById(rte+"fontstyle").style.display = fStyle;    //font style
  document.getElementById(rte+"fontsize").style.display = fSize;     //font size
  document.getElementById(rte+"boldButton").style.display = bButton;   //Bold
  document.getElementById(rte+"italicButton").style.display = iButton; //Italic
  document.getElementById(rte+"underlineButton").style.display = uButton;  //underline
  document.getElementById(rte+"strikeButton").style.display = stkButton;  //strike through
  document.getElementById(rte+"superscriptButton").style.display = spButton;  //superscript
  document.getElementById(rte+"subscriptButton").style.display = sbButton;  //subscript
  document.getElementById(rte+"leftButton").style.display = lButton;  //left align
  document.getElementById(rte+"centerButton").style.display = cButton;  //center align
  document.getElementById(rte+"rightButton").style.display = rButton;  //right align
  document.getElementById(rte+"justifyButton").style.display = jButton;  //justify
  document.getElementById(rte+"horizontalRuleButton").style.display = hrButton;  //horizontal rule
  document.getElementById(rte+"orderlistButton").style.display = olButton;  //order list
  document.getElementById(rte+"unorderlistButton").style.display = ulButton;  //unorder list
  document.getElementById(rte+"indentButton").style.display = inButton;  //indent
  document.getElementById(rte+"outdentButton").style.display = odButton;  //outdent
  document.getElementById(rte+"ForeColor").style.display = fcButton;  //text color
  document.getElementById(rte+"BackColor").style.display = bcButton;  //text highlignt color
  document.getElementById(rte+"linkButton").style.display = lkButton;  //Link 
  document.getElementById(rte+"unlinkButton").style.display = unlButton;  //unlink
  document.getElementById(rte+"imageButton").style.display = imgButton;  //image
  document.getElementById(rte+"mediaButton").style.display = mdButton;  //flash object
  document.getElementById(rte+"objectButton").style.display = objButton;  //script object
  document.getElementById(rte+"characterButton").style.display = chrButton;  //add special character
  document.getElementById(rte+"tableButton").style.display = tblButton;  //table
  document.getElementById(rte+"cutButton").style.display = cutButton;  //cut
  document.getElementById(rte+"copyButton").style.display = copyButton;  //copy
  document.getElementById(rte+"pasteButton").style.display = pastButton;  //paste
  document.getElementById(rte+"undoButton").style.display = unButton;  //undo
  document.getElementById(rte+"redoButton").style.display = reButton;  //redo
  document.getElementById(rte+"sourceButton").style.display = scButton;  //view source	
  document.getElementById(rte+"previewButton").style.display = prButton;  //preview
  
  /*	apply for hight and width for spacific page 
  #change on 
  */
/*  if(!tWidth)
  { 
	  tWidth	=	'none';
  }
  if(!tHeight)
  {
	  tHeight	=	'none';
  }
*/  /* end of this  code*/
  
  document.getElementById(rte+"width").style.display = 'none'//tWidth;  //width
  document.getElementById(rte+"height").style.display = 'none'//tHeight;  //height
 
}

/*
 * This function create main body of the editor
 *
*/
function designEditor(rteId)
{
	//get value from hidden
	var strHTML = document.getElementById(rteId+"Hidden").value;	
		
	//create new divs to wrap object so that at design time user can see flash logo
	strHTML = strHTML.replace(/<object/gi,'<div id="scDiv"><object');
	strHTML = strHTML.replace(/<param name=\"movie\"/gi,'<param name="play" value="false"><param name="wmode" value="transparent"><param name="movie"');
	strHTML = strHTML.replace(/<\/object>/gi,'</object></div>');
	strHTML = strHTML.replace(/><\/embed><\/object>/gi,' wmode="transparent" play="false"></embed></object>'); 
	//end of div	
	
	//editor page
	var mainContent= "<html><head><style type='text/css'>p{margin:0px;} #scDiv{background-image:url("+editorUrl+"images/flash.png); width:250px; height:250px;background-repeat:no-repeat; }</style></head><body style=\"font-family:Verdana; font-size:12px;color:#000000;\">"+strHTML+"</body></html>";			
	
	var editWin;
	
	//get document
	if(isIE) editWin = document.getElementById(rteId).contentWindow.document ;	
	else editWin = document.getElementById(rteId).contentDocument ;	
	
	//made design mode on 
	editWin.designMode="on";
	
	
	//write html
	editWin.open();
	editWin.write(mainContent);
	editWin.close();
	
}

/*
 * This function set picked color either for forcolor or backcolor
 *
*/
function pickColor(color)
{	
	//set to edit mode
	if(editMode==targetRte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
	//set color	
	var editWin = document.getElementById(targetRte).contentWindow.document;
	
	if(isIE) IESelectionRange.execCommand(cmd, false, color);  
	else
	{
		if(cmd =="BackColor")editWin.execCommand("hilitecolor", false, color); 
		else editWin.execCommand(cmd, false, color); 
	}//end if	 
}

/*
 * This function set values of execcommand
 *
*/
function editorCommand(rte,cmd,value)
{
	 
	//set to edit mode
	if(editMode == rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
	//set color	
	var editWin = document.getElementById(rte).contentWindow.document;
		
	editWin.execCommand(cmd, false, value); 
}

/*
 * This function  call normal function of execcommand
 *
*/
function editCommand(rte,cmd)
{
	//set to edit mode
	if(editMode == rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
	//set color	
	var editWin = document.getElementById(rte).contentWindow.document;
	editWin.execCommand(cmd, false, null); 
}



/*
 * Call execcommand for clipboard functions
 *
*/
function editorClipboard(rte,cmd)
{
	//set to edit mode
	if(editMode == rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
	//set color	
	var editWin = document.getElementById(rte).contentWindow.document;
	editWin.execCommand(cmd); 
}


//open popup to get table data
function popUpWin (url, win, width, height, options) {
  var leftPos = (screen.availWidth - width) / 2;
  var topPos = (screen.availHeight - height) / 2;
  options += 'width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
  return window.open(url, win, options);
}

//this function insert html into iframe
function insertHTML(html)
{
  
 if(isIE) insertHTMLforIE (html);
 else insertHTMLForMozilla(html);
}

//insert table command
function dlgInsertTable(rte) {
	//set target rte
	targetRte = rte;
	
   //set to edit mode
	if(editMode == rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
  //function to open/close insert table dialog 
  var InsertTable = popUpWin(editorUrl+'insert_table.htm', 'InsertTable', 360, 180, '');
}


//this function add image
function addImage(rte,type)
{
    //set target rte
	targetRte = rte;
	
   //set to edit mode
	if(editMode == rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
   var imgPath = popUpWin(editorUrl+fileUploadPage+'?mediaType='+type+'&editorUrl='+editorUrl, 'InsertImage', 360, 180, '');
}

//add object
function addObject(rte)
{
	//set target rte
	targetRte = rte;
	
	//set to edit mode
	if(editMode == rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
	//var strObj = prompt("Please insert object code here");
	// insertHTML(strObj) ;
	 var InsertObject = popUpWin(editorUrl+'insert_object.htm', 'InsertObject', 360, 180, '');
	   
}

//add Link
function addLink(rte)
{
	//set to edit mode
	if(editMode==rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
	//editor
	var editWin = document.getElementById(rte).contentWindow.document;
	
	var url = window.prompt("Enter an URL:", "http://");
	if (url===null) return;
	if (url==="")
	{
	  editWin.execCommand("unlink", false, null); 
	}
	else
	{
	  editWin.execCommand("createLink", false, url); 
	}
}

//insert spacel characters
function insertCharacters(rte)
{
	//set target rte
	targetRte = rte;
	
   //set to edit mode
	if(editMode == rte)
	{
		alert("Sorry, you can't use this function in source mode\n Please change it to edit mode.");
		return;
	}
	
   var charValue = popUpWin(editorUrl+'insert_special_char.htm', 'InsertCharacter', 360, 180, '');

}

//view source
function viewSource(rte)
{
		
	var sTmp;  
	
	if(document.getElementById(rte+"changeView").checked == true)
	{
	
	  if(isIE)
	  {
		 sTmp = document.getElementById(rte).contentWindow.document.body.innerHTML ;
	     document.getElementById(rte).contentWindow.document.body.innerText  = sTmp ;
	  }
	  else
	  {
		 sTmp = document.createTextNode(document.getElementById(rte).contentWindow.document.body.innerHTML);	  
	     document.getElementById(rte).contentWindow.document.body.innerHTML = "";
	     document.getElementById(rte).contentWindow.document.body.appendChild(sTmp);
		 // Task: error with & in code
		 // Modified by: Cesariel
		 // Description: When the editor is in HTML mode the (&) changes to (&amp;) which is supposed to be just (&)
		 // Date: 12/04/2009
		 str = document.getElementById(rte).contentWindow.document.body.innerHTML;
		 str = str.replace(/&amp;amp;/gi, "&amp;");
		 document.getElementById(rte).contentWindow.document.body.innerHTML = str;
		 // End Modification: error with & in code
	  }	 
	  
	  //set mode
	  editMode = rte;
	  
	}
	else
	{
	  if(isIE)
	  {
		 sTmp = document.getElementById(rte).contentWindow.document.body.innerText ;
	     document.getElementById(rte).contentWindow.document.body.innerHTML  = sTmp ;
	  }
	  else
	  {
		  sTmp = document.getElementById(rte).contentWindow.document.createRange();
		  sTmp.selectNodeContents(document.getElementById(rte).contentWindow.document.body);
          document.getElementById(rte).contentWindow.document.body.innerHTML = sTmp.toString();
	  
	  }
	  
	  //set mode
	  editMode ="";
	  
	}
	
	//set focus
	document.getElementById(rte).contentWindow.focus();
}


//this function fix opera bugs
function fixOperaRangeSelectionBug()
{ 
   var brCollection = document.getElementById(targetRte).contentWindow.document.body.getElementsByTagName('br');   
   for (var num1 = 0;num1 < brCollection.length; num1++)
   {         
     var br = brCollection[num1];  
	 var prevSibling = br.previousSibling; 
		 
	 if (prevSibling != null) 
	 {            
	   var hasNbsp = isNbsp(prevSibling);  
	   if (!hasNbsp)
	   {  	     
	     // insert the invisible non-breaking-whitespace 
	     br.parentNode.insertBefore(document.getElementById(targetRte).contentWindow.document.createTextNode('\u00A0'), br); 
	   }       
	 }      
  } 
}
//end of opera bug fix function

 //chceck nbsp
 function isNbsp(textNode) 
 {     
     if (textNode.nodeType != 3)return false;    
   
	 var pat = /\u00A0/;   
	 return textNode.nodeValue.match(pat);
 }

//end of function
	
//original functions
if (!Array.prototype.map) {
	Array.prototype.map = function(fun) {
		var collect = [];
		for (var ix = 0; ix < this.length; ix++) { collect[ix] = fun(this[ix]); }
		return collect;
	}
}

function bindEvent(target, eventName, fun) {
	if (target.addEventListener) {
		target.addEventListener(eventName, fun, false);
	} else {
		target.attachEvent("on" + eventName, function(){ fun(event); });
	} 
}


//*****************************************************************************
//functions for insert html in IE and Firefox

function insertHTMLforIE (html){
   var win = document.getElementById(targetRte).contentWindow; 
   var doc = win.document; 
   win.focus(); 
   var range = doc.selection.createRange(); 
   try { 
      range.pasteHTML(html); 
	  range.collapse(false);
      range.select();
   } 
   catch (e){ 
    alert(e); 
    }
 }
 
 //insert html into firefox
 function insertHTMLForMozilla(html){
	
	//fix opera bug
	if(isOpera) fixOperaRangeSelectionBug();
	
    var win = document.getElementById(targetRte).contentWindow;  
    var doc = win.document;  
	win.focus(); 
    var fragment = doc.createDocumentFragment();  
    var div = doc.createElement("div");  
    div.innerHTML = html;  
    while (div.firstChild) {
      fragment.appendChild(div.firstChild);  
    }  
    var selection = win.getSelection();  
    range = selection.getRangeAt(0);
    range.deleteContents();  
    var container = range.startContainer;  
    var offset = range.startOffset;  
    switch (container.nodeType) {  
        case 1:    // Element node    
        container.insertBefore(fragment,container.childNodes[offset]);    
        break;  
        case 3:    // Text node    
        var node = container.splitText(offset);    
        node.parentNode.insertBefore(fragment, node);    
        break;  
    }
} 

//this function show preview of editor
function preview(rte)
{
	var previewData = document.getElementById(rte).contentWindow.document.body.innerHTML ;	
	previewData = filterData(previewData);
		
		
	if(document.getElementById(rte+"chkPreview").checked == true)
	{		
		document.getElementById(rte).style.display = "none";
		document.getElementById("preview"+rte).style.display = "";	
		
		//editor page
		var previewContent= "<html><head><style type='text/css'>p{margin:0px;} #scDiv{background-image:url("+editorUrl+"images/flash.png); width:250px; height:250px;background-repeat:no-repeat; }</style></head><body style=\"font-family:Verdana; font-size:12px;color:#000000;\">"+previewData+"</body></html>";			
		
		var editWin;
	
		//get document
		if(isIE) editWin = document.getElementById("preview"+rte).contentWindow.document ;	
		else editWin = document.getElementById("preview"+rte).contentDocument ;	
		
	  	
	    //write html
	    editWin.open();
	    editWin.write(previewContent);
	    editWin.close();
						
		//display content		
		//document.getElementById("preview"+rte).contentWindow.document.body.innerHTML  = previewData;
		
		
	}
	else
	{
		document.getElementById(rte).style.display = "";
		document.getElementById("preview"+rte).style.display = "none";
	}
}

//get all tags of document
function findObject(rte)
{
	var arrObj = document.getElementById(rte).contentWindow.document.all.tags("OBJECT");	
	if (arrObj!=null)
	{
      var rangeObj = arrObj[0].createTextRange();
      if (rangeObj != null) 
	  {
        alert(rangeObj.text);
      }
   }
}
//end function

//filter data 
function filterData(strHTML)
{
	strHTML = strHTML.replace(/"<a href=".*param">|"<a href='.*param'>/gi,'\"');
	strHTML = strHTML.replace(/&lt;param<\/a>/gi,'&lt;param');
	strHTML = strHTML.replace(/"<a href=".*">/gi,'\"');
	strHTML = strHTML.replace(/<\/a>"/gi,'\"');			
	strHTML = strHTML.replace(/&lt;/g,'<').replace(/&gt;/g,'>');
	
	//add div to you tube script	
	//----------------------------------------------------------------------------------------------------------------------------	
	//first clear all divs
	if(isIE) strHTML = strHTML.replace(/<div id=scDiv>/gi,'');	
	else strHTML = strHTML.replace(/<div id=\"scDiv\">/gi,'');	
	
	strHTML = strHTML.replace(/<param name=\"play\" value=\"false\"><param name=\"wmode\" value=\"transparent\">/gi,'');
	strHTML = strHTML.replace(/<\/object><\/div>/gi,'</object>');
	strHTML = strHTML.replace(/wmode=\"transparent\" play=\"false\"><\/embed><\/object>/gi,'></embed></object>');
			
	//return 
	return strHTML;
}

//show browserlist
function browserList()
{
	alert("This Rich text editor support these browsers:\n IE6,IE7,Firefox3.0.8,Opera9.64,Safari3.0.1 and Chrome");
}

//This function update Rtes contents
function updateRTEs()
{
	var arrRTEs = allRTEs.split(":");
	
	//run loop
	for(var iCnt=0; iCnt < arrRTEs.length; iCnt++)
	{
		//update contents of related editor hidden
		document.getElementById(arrRTEs[iCnt]+"Hidden").value = filterData(document.getElementById(arrRTEs[iCnt]).contentWindow.document.body.innerHTML);
		
	}
}
//end of function

//this function get range of selected text
function setRange(rte)
{   
	//function to store range of current selection 
	var oRTE;   
	if (document.all)
	{   
	  oRTE = frames[rte]; 
	  var selection = oRTE.document.selection; 
	  if (selection != null) rng = selection.createRange();  
	} 
	else 
	{   
	   oRTE = document.getElementById(rte).contentWindow; 
	   var selection = oRTE.getSelection(); 
	   rng = selection.getRangeAt(selection.rangeCount - 1).cloneRange();
	}
	//return
	return rng;
}

//This function set dimension of editor
function setDimension(rte,param,value)
{
		
	//check 
	if(param == "width")
	{
		//check for min width 150
		if((parseInt(value) < 150) || (value== "") )
		{
			alert("Minimum required Width is: 150 ");
			document.getElementById(rte+"txtWidth").value = 150;
			value = 150;			
		}
		else if(parseInt(value) > 1000)
		{
			alert("Maximum required Width is: 1000 ");
			document.getElementById(rte+"txtWidth").value = 1000;
			value = 1000;			
		}
				
	    //set width
		document.getElementById("tool"+rte).style.width = value +"px";
		document.getElementById(rte).style.width = value +"px";
		document.getElementById("preview"+rte).style.width = value +"px";
	}
	else
	{	
	    //check for min width 150
		if((parseInt(value) < 150) || (value==""))
		{
			alert("Minimum required Height is : 150");
			document.getElementById(rte+"txtHeight").value = 150;
			value = 150;	
		}
		else if(parseInt(value) > 1000)
		{
			alert("Maximum required Height is: 1000 ");
			document.getElementById(rte+"txtHeight").value = 1000;
			value = 1000;			
		}
		
		//set width
	    document.getElementById(rte).style.height = value +"px";	
	    document.getElementById("preview"+rte).style.height = value +"px";
	}
	
}

//check keypress event
function onKeyPressNumbers(e)
{
   var key = window.event ? e.keyCode : e.which;

   var keychar = String.fromCharCode(key);
     
   //check backspace
   if(parseInt(key) == 8) return true;
   
   reg = /\D/;  
   
   return !reg.test(keychar);
}


//this function call modal message
function maximizeMe(rte)
{
	//set width of editor	
	
	if(!maxMode)
	{	
	    document.getElementById(rte+"imgMax").src = editorUrl+"images/restore.gif";
		document.getElementById(rte+"imgMax").title = "Restore Down";
		document.documentElement.style.overflow = "hidden";			
		window.scrollTo(0,0);
		
		document.getElementById("tool"+rte).style.width = browserWindowSize("width")+"px";
		document.getElementById(rte).style.width = browserWindowSize("width")+"px";
		document.getElementById("preview"+rte).style.width = browserWindowSize("width")+"px";
		
		//set height of editor	
		document.getElementById(rte).style.height = parseInt(browserWindowSize("height"))-70+"px";
		document.getElementById("preview"+rte).style.height = parseInt(browserWindowSize("height"))-70+"px";		
		
		if(isIE) document.getElementById(rte+"Container").className = "divMaxSizeIE";	
		else document.getElementById(rte+"Container").className = "divMaxSizeNonIE";	
		
		
		
		//set true
		maxMode = true;		
		
	}
	else
	{
		document.getElementById(rte+"imgMax").src = editorUrl+"images/maximize.gif";
		document.getElementById(rte+"imgMax").title = "Maximize";
		document.documentElement.style.overflow = "scroll";
		window.scrollTo(0,0);
		
		//$("select").addClass("showCombo");		
		document.getElementById("tool"+rte).style.width =defaultWidth +"px";
		document.getElementById(rte).style.width = defaultWidth+"px";
		document.getElementById("preview"+rte).style.width = defaultWidth +"px";
		
		//set height of editor	
		document.getElementById(rte).style.height = defaultHeight +"px";
		document.getElementById("preview"+rte).style.height = defaultHeight+"px";
		
		
		document.getElementById(rte+"Container").className = "";		
		//set max mode false
		maxMode = false;
		
		
	}	
	
}



//retun browser window size
function browserWindowSize(type)
{
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  //return width or height
  if(type == "width") return myWidth;
  else return myHeight;
 
}

function getPosition(obj){
    var topValue= 0,leftValue= 0;
    while(obj){
	leftValue+= obj.offsetLeft;
	topValue+= obj.offsetTop;
	obj= obj.offsetParent;
    }
    finalvalue = leftValue + "," + topValue;
    return finalvalue;
}

