
var bLoad=false
var pureText=true
var bTextMode=false
//**************************************************************//
//When you create the public_description object, the constructor//
//function that you assign to it can have any name, as long as  //
//the corresponding function appears somewhere in the scriptlet.//
//Within the constructor function, you declare the properties   //
//and methods that you want to expose within the scriptlet in   //
//the following ways                                            //
//**************************************************************//
public_description=new Editor

function Editor() { //constructor function
   this.put_text=SetText;
   this.EdFocus=GetEdFocus;
   this.save_text=Save;
   this.closewin = Close;
}

function GetEdFocus() {
   EditHtml.focus();
}
function initEditor() {
	EditHtml.document.designMode="on";
	EditHtml.document.open("text/html","replace")
	EditHtml.document.write();
	EditHtml.document.close();
}

function SetText() {
	var text = document.Edit.Msg.value;	
   EditHtml.document.designMode='on';
   EditHtml.document.open();
   EditHtml.document.clear();
   EditHtml.document.write(text);
   EditHtml.document.close();
}
function Save(p_tag) {
   if(!checkMode()) return;
   var fm = document.Edit.Fm.value;
   var fd = document.Edit.Fd.value;
   var sr = eval("window.opener.document."+fm+"."+fd);
   if(p_tag)
      sr.value = EditHtml.document.documentElement.outerHTML;
   else
      sr.value = EditHtml.document.body.innerHTML;
   window.close();
}
function Close(p_tag) {
   if(!p_tag){
      if(!confirm(msg_close)) 
         return;
   }
   window.close();
}
function Preview() {
   if(switchMode.checked){
      alert(msg_preview);
      return;
   }
   var charset = document.Edit.Fchar.value; 
   var winWidth = window.screen.width-150;
   var winHeight = window.screen.availHeight-200;
   var wintop = parseInt(100 / 2);
   var winleft = parseInt(150 / 2);
   prevWnd=window.open("","","top="+wintop+",left="+winleft+",scrollbars=yes,resizable=yes,menubar=yes,status=yes,width="+winWidth+",height="+winHeight);
   prevWnd.document.open();
   htmltag = "<html><title>Html Editor -Preview</title>";
   prevWnd.document.writeln(htmltag);
   htmltag = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset="+charset+"\">";
   prevWnd.document.writeln(htmltag);
   prevWnd.document.writeln(EditHtml.document.documentElement.innerHTML);
   htmltag = "</html>";
   prevWnd.document.writeln(htmltag);
   prevWnd.document.close();
}
// Check if toolbar is being used when in text mode
function checkMode() {
   if (!bTextMode) return true;
   alert(msg_mode);
   EditHtml.focus();
   return false;
}

//the commands to the buttons and menu in the Html Editor.
function commands(sCommands,bUserInterface,vValue) {
   if(!checkMode()) return;
   if(sCommands == null) return;
   if(vValue == null){ 
      if(bUserInterface == null)
         EditHtml.document.execCommand(sCommands);
      else
         EditHtml.document.execCommand(sCommands,bUserInterface);
 	}
   else{ 
      if(bUserInterface == null)
         EditHtml.document.execCommand(sCommands,'',vValue);
      else
         EditHtml.document.execCommand(sCommands,bUserInterface,vValue);
   }
   pureText = false;
   EditHtml.focus();
}

//Switches between text and html mode.
function changeMode(newMode) {
   bTextMode = newMode;
   var cont;
   if(bTextMode) {
      cont=EditHtml.document.documentElement.outerHTML;
      EditHtml.document.body.innerText=cont;
   } 
   else {
      cont=EditHtml.document.body.innerText;
      EditHtml.document.body.innerHTML=cont;
   }
   EditHtml.focus();
}
function clearHtml(){
   var fonts = EditHtml.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 refreshall(){
   if(!checkMode()) return;
   changeMode(true);
   changeMode(false);
}
//cancel link
function unLink(){
	var url = "";
   if(!checkMode()) return;
   // check the selection is none or not
	var sType = EditHtml.document.selection.type;
   if(sType == "None") return;
  	commands('Unlink',false); 
}
//insert link
function createLink() {
   EditHtml.focus();
   if(!checkMode()) return;

 	var param; 
	var height;
	var szParam = szHttp = "";
	var selPart;
	var len;
	var url = szURL = szTarget = szProtocol = "";
	var g_lang = document.Edit.Lang.value;
	
	selPart = getSelection();
	var sType = selPart.type;

	if(window.screen.width <= 800)
		height = 100;
	else
		height = 130;
   param = "help=no;status=no;scrollbar=no;dialogWidth:520px;dialogHeight:"+height+"px"; 

	var HttpParam = getHttpParam();
	if(HttpParam){
		szURL = HttpParam.href;
		url = szURL;	
		szTarget = HttpParam.target;
		len = szURL.indexOf(":");
		szProtocol = szURL.substring(0,len);
		len = len +1;
		if(szProtocol != "mailto" && szProtocol != "news")
			len = szURL.indexOf("//") + 2;
		szURL = szURL.substring(len);
		szParam = "&URL="+escape(szURL)+"&Target="+szTarget+"&Protocol="+szProtocol;
	}

  	var htmlvar = showModalDialog("./sub/insertLink.phtml?Lang="+g_lang+szParam,"",param);  
	if(htmlvar == "" || htmlvar == null) return;

	if(HttpParam) commands("Unlink",false,url);

	if(sType == "Text"){
		var sel = selPart.createRange();
		html = htmlvar + sel.htmlText + "</a>";
		sel.text = "";
		sel.pasteHTML(html);
		sel.select();
	}
	else if(sType == "Control"){
		var sel = selPart.createRange();
		var tempHTML = sel.item(0).outerHTML;
		var html = htmlvar + tempHTML + "</a>";
		sel.item(0).outerHTML = html;
	}
}
function getHttpParam(){
	var d = eval("EditHtml.document");
	var sel = d.selection.createRange();
	var Elem;
	if(sel.parentElement){
		Elem = getElement(sel.parentElement(),"A");
	}
	else{
		Elem = getElement(sel.item(0),"A")
	}
	if(Elem)
		return Elem;
}
function getElement(p_Element,p_Tag){
	while (p_Element != null && p_Element.tagName!=p_Tag)
	p_Element = p_Element.parentElement
	return p_Element;
}
function getSelection(){
	var oSel = EditHtml.document.selection;
	if(!oSel){
		oSel = EditHtml.document.selection.createRange();
		oSel.type = EditHtml.document.selection.type;
	}
	return oSel;
}
//Insert the Image
function insertImages() {
   var param;
   var height;
	var g_lang = document.Edit.Lang.value;
   EditHtml.focus();
   if(!checkMode()) return;
   if(window.screen.width <= 800)
      height = 260;
   else
      height = 320;

   param = "help=no;status=no;scrollbar=no;dialogWidth:570px;dialogHeight:"+height+"px"; 
	var sUrl = "./sub/insertImage.phtml?Lang="+g_lang;
   var htmlvar = showModalDialog(sUrl,"",param);  
   if(htmlvar !=null){
      if(EditHtml.document.selection.type=="None"){
         var sel=EditHtml.document.selection.createRange();
         sel.pasteHTML(htmlvar);
         sel.select();
      } 
      else EditHtml.focus();
   }
   else EditHtml.focus();
}

//Insert Table
function insertTable(){
   var param;
	var g_lang = document.Edit.Lang.value;
   EditHtml.focus();
   if(!checkMode()) return;
   var height;
   var width;
   if(window.screen.width <= 800){
      height = 360;
      width = 500;
   }
   else{
      height = 390;
      width = 550;
   }
   param = "help=no;status=no;scrollbar=no;dialogWidth:"+width+"px;dialogHeight:"+height+"px"; 
   var tbvar = showModalDialog("./sub/insertTable.phtml?Lang="+g_lang,"",param);  
   if(tbvar !=null){
      if(EditHtml.document.selection.type=="None"){
         var sel=EditHtml.document.selection.createRange();
         sel.pasteHTML(tbvar);
         sel.select();
      } 
      else EditHtml.focus();
   }
   else EditHtml.focus();
}

//Sets the text color.
function setFontColor() {
   if (! checkMode()) return;
   var param;
   var height;
   var width;
	var g_lang = document.Edit.Lang.value;
   if(window.screen.width <= 800){
      width=300;
      height = 400;
   }
   else{
      width=340;
      height = 420;
   }
   param = "help=no;status=no;scrollbar=no;dialogWidth:"+width+"px;dialogHeight:"+height+"px"; 
   var arr = showModalDialog("./sub/colorselect.phtml?ColTitle="+msg_fontcolor+"&Lang="+g_lang, "", param);
   if (arr != "") commands('forecolor',"",arr);
   else EditHtml.focus();
}

//Sets the background color.
function setBgColor() {
   if (!checkMode()) return;
   var param;
   var height;
   var width;
	var g_lang = document.Edit.Lang.value;
   if(window.screen.width <= 800){
      width=300;
      height = 400;
   }
   else{
      width=340;
      height = 420;
   }
   param = "help=no;status=no;scrollbar=no;dialogWidth:"+width+"px;dialogHeight:"+height+"px"; 
   var arr = showModalDialog("./sub/colorselect.phtml?ColTitle="+msg_bgcolor+"&Lang="+g_lang, "", param);
   if (arr != null) commands('backcolor',"",arr);
   else EditHtml.focus()
}
function insertMarquee(){
   EditHtml.focus();
   if(!checkMode()) return;
   var param;
	var g_lang = document.Edit.Lang.value;
   param = "help=no;status=no;scrollbar=no;dialogWidth:500px;dialogHeight:300px"; 
	
	//var mqParam = getMarqueeParam();
   var htmlvar = showModalDialog("./sub/insertMarquee.phtml?Lang="+g_lang,"",param);  
   if(htmlvar !=null){
      if(EditHtml.document.selection.type=="None"){
         var sel=EditHtml.document.selection.createRange();
         sel.pasteHTML(htmlvar);
         sel.select();
      } 
      else EditHtml.focus();
   }
   else EditHtml.focus();
}
function getMarqueeParam(){
	var d = eval("EditHtml.document");
	var sel = d.selection.createRange();
	var Elem;
	if(sel.parentElement){
		Elem = getElement(sel.parentElement(),"MARQUEE");
	}
	else{
		Elem = getElement(sel.item(0),"MARQUEE")
	}
	if(Elem)
		return Elem;
}
function insertFlash() {
   var param;
   var height;
   var width;
	var g_lang = document.Edit.Lang.value;
   EditHtml.focus();
   if(!checkMode()) return;
   if(window.screen.width <= 800){
      width = 470;
      height = 150;
   }
   else{
      width = 510;
      height = 180;
   }
   param = "help:no;status:no;scrollbar:no;dialogWidth:"+width+"px;dialogHeight:"+height+"px";
   var htmlvar = showModalDialog("./sub/insertFlash.phtml?Lang="+g_lang,"",param);
   if(htmlvar !=null){
      if(EditHtml.document.selection.type=="None"){
         var sel=EditHtml.document.selection.createRange();
         sel.pasteHTML(htmlvar);
         sel.select();
      }
      else EditHtml.focus();
   }
   else EditHtml.focus();
}
function insertAudio(){
   var param;
   var height;
	var g_lang = document.Edit.Lang.value;
   EditHtml.focus();
   if(!checkMode()) return;
   if(window.screen.width <= 800){
      width = 450;
      height = 150;
   }
   else{
      width = 510;
      height = 180;
   }
   param = "help=no;status=no;scrollbar=no;dialogWidth:"+width+"px;dialogHeight:"+height+"px"; 
   var htmlvar = showModalDialog("./sub/insertAudio.phtml?Lang="+g_lang,"",param);  
   if(htmlvar !=null){
      if(EditHtml.document.selection.type=="None"){
         var sel=EditHtml.document.selection.createRange();
         sel.pasteHTML(htmlvar);
         sel.select();
      } 
      else EditHtml.focus();
   }
   else EditHtml.focus();
}
function helpme(p_lang){
   var param;  
   EditHtml.focus();
   param = "top=0,left=0,toolbar=yes,menubar=yes,status=yes,resizable=yes,height=400,width=500";
   window.open("./help/"+p_lang+"/index.html",null,param);
}
function aboutme(){
   var param; 
   var height;
	var g_lang = document.Edit.Lang.value;
   EditHtml.focus();
   if(window.screen.width <= 800)
      height = 290;
   else 
      height = 330;
   param = "help:no;status:no;scrollbar:no;dialogWidth:460px;dialogHeight:"+height+"px";
   showModalDialog("./sub/aboutus.phtml?Lang="+g_lang,"",param);
}
//2002-01-18 Last Modified Date
//Author Alexson
//Copyright reserved by EC-SERVER.COM

