var EditorLoadingMsg = "Àá½Ã¸¸ ±â´Ù·Á ÁÖ¼¼¿ä.";
/******************************
 * EDITOR
 ******************************/
function Editor(editorid, contentid)
{
    this.editorid = editorid;
    this.contentid = contentid;

    this.frame;
    this.iframe;
    this.doc;
    this.selection;
    this.range;
		this.LINK = "";
    this.fontstyle = "";
    this.bodystyle = "";
		this.prefontmessage = "";

    this.init = initEditor;
    this.refreshEditor = refreshEditor;
    this.setSize = setSize;
    this.focus = focusEditor;
		this.setLink = setLink;
    this.setFontStyle = setFontStyle;
		this.setPrefontmessage = setPrefontmessage;
    this.getEditorSource = getEditorSource;
    this.getFrame = getFrame;
    this.getContent = getContent;
		this.setContent = setContent;   
    this.getSelection = getSelection;
    this.createRange = createRange;
    this.setFontColorBoxPosition = setFontColorBoxPosition;
    this.setBGColorBoxPosition = setBGColorBoxPosition;
}

function initEditor(width, height)
{
    this.frame = this.getFrame(width, height);   
    this.doc = this.frame.document;
    this.doc.designMode = "on";
    this.doc.open("text/html");
    this.doc.write(this.getEditorSource());
    this.doc.close();
    
	// remove <br>
	if(!isIE())this.doc.body.innerHTML = "";

	
}

function refreshEditor()
{
    this.frame = this.getFrame(width, height);   
    this.doc = this.frame.document;
		this.doc.designMode = "on";
    
		this.doc.open("text/html");
    this.doc.write(this.getEditorSource());
    this.doc.close();
	
}

function setSize(width, height)
{
    if(isIE())
    {
      document.getElementById(this.editorid).style.width = width+"px";
      document.getElementById(this.editorid).style.height = height+"px";
    }
    else
    {
      this.iframe.style.width = width+"px";
      this.iframe.style.height = height+"px";
    }
}

function focusEditor()
{
    this.frame.focus();
}

function setLink(LINK)
{
		this.LINK = LINK;
}

function setFontStyle(fontstyle, bodystyle)
{
    this.fontstyle = fontstyle;
    this.bodystyle = bodystyle;
}

function setPrefontmessage(prefontmessage)
{
	this.prefontmessage = prefontmessage;
}

function getEditorSource()
{
    var source = "";
    var style = "";
    var body = "";
	
    style = "<style>P {margin-top:2px;margin-bottom:2px;}table {border:1 solid C6C3C6} " + this.fontstyle + "</style>";
		script = "<script>function resizeImage(num){ var width = eval('document.all.userImg'+num+'.width');if( width > 550 )  {   eval('document.all.userImg'+num+'.width = 550');}}<\/script>";
    body = "<body " + this.bodystyle + ">" + this.prefontmessage + "</body>";
   
    source = "<html><head>" + this.LINK + style + script + "</head>" + body + "</html>";

    return source;
}

function setFontColorBoxPosition(top, left)
{
    document.getElementById("TableFontColor").style.top = top;
    document.getElementById("TableFontColor").style.left = left;
}

function setBGColorBoxPosition(top, left)
{
    document.getElementById("TableBGColor").style.top = top;
    document.getElementById("TableBGColor").style.left = left;
}

function getFrame(width, height)
{		
    if (isIE())
    {
        document.getElementById(this.contentid).style.display = "none";
        this.frame = eval(this.editorid);
    }
    else
    {
        var textarea = document.getElementById(this.contentid);
        var htmlarea = document.createElement("div");
        textarea.parentNode.insertBefore(htmlarea, textarea);
        textarea.style.display = "none";
        document.getElementById(this.editorid).style.display = "none";

        this.iframe = document.createElement("iframe");
        htmlarea.appendChild(this.iframe);
                
        this.iframe.style.width = width + "px";
        this.iframe.style.height = height + "px";
        this.iframe.className = document.getElementById(this.editorid).className;
        this.frame = this.iframe.contentWindow;
    }

    return this.frame;
}


function getContent()
{
    return this.doc.body.innerHTML;
}

function getContents(mainfont, mainfonttype)
{
    if (mainfonttype == '0')
    {
        return "<font style ='FONT-FAMILY: "+ mainfont+"_0;'>" + this.doc.body.innerHTML + "</FONT>";
    }
    else
    {
   	    return "<FONT style ='FONT-SIZE: "+ mainfonttype +"pt; FONT-FAMILY: "+ mainfont+"_"+ mainfonttype +";'>" + this.doc.body.innerHTML + "</FONT>";
    }
}

function setContent(html)
{
    this.doc.body.innerHTML = html;
}

function SetContents()
{
	editor1.doc.body.innerHTML = ContentsDecode(document.all.before_contents.innerHTML);
}

function ContentsDecode(str)
{
    str = str.replace(/&lt;/gi,"<");
    str = str.replace(/&gt;/gi,">");
    str = str.replace(/&amp;/gi,"&");
	return str;
}

function getSelection()
{
    if (isIE()) 
        return this.doc.selection;
    else
        return this.frame.getSelection();
}

function createRange(sel) 
{
    if (isIE()) 
    {
        return sel.createRange();
    } 
    else 
    {
        this.focus();
        if (typeof sel != "undefined") 
        {
            try {
                return sel.getRangeAt(0);
            } catch(e) {
                return this.doc.createRange();
            }
        } 
        else 
        {
            return this.doc.createRange();
        }
    }
}

var oldpubsvc =0;
var oldpubcategory =0;
var LeverageSSobj = null;

function EditRun(){
	
	editor1 = new Editor("WEBEDITOR","CONTENT");
	editor1.setFontStyle("", "STYLE='font-size:9pt;font-family:µ¸¿ò;' class='view'");
	editor1.setPrefontmessage("");	
	editor1.init(568,370);

  if(!isIE())
  {
		editor1.doc.addEventListener("focus",changeEditorNotIE,true);
  }

	editor1.focus()
}

function changeEditorIE()
{
	editor = editor1;  
}

function onEditFocus()
{
	if(typeof(editor)!="object"){alert(EditorLoadingMsg);return false;}	// ·ÎµùÀü¿¡ È£ÃâµÇ¸é ¿¡·¯¹ß»ý.

	eobj = WEBEDITOR;
	var htmlstring = editor.doc.body.innerHTML;
	if (htmlstring  == "Æ÷½ºÆ® ¾ÕºÎºÐÀ» ½áÁÖ¼¼¿ä."||
		( htmlstring.length == 166 && htmlstring.toLowerCase() == "-±Û²ÃÀÌ³ª ±ÛÀÚ Å©±â¸¦ ¹Ù²Ù°í ½ÍÀ¸½Ç ¶§´Â ÇØ´ç ±ÛÀÚ¸¦ ºí·ÏÀ¸·Î ÁöÁ¤ÇÑ µÚ º¯°æÇÏ½Ç ¼ö ÀÖ½À´Ï´Ù.<br>-À¥ÆùÆ®ÀÇ Å©±â´Â ÆùÆ®¸í¿¡ ÀÖ´Â Å©±â·Î¸¸ »ç¿ë°¡´ÉÇÏ¸ç ±×¿Ü Å©±â·Î ÁöÁ¤½Ã µ¸¿òÀ¸·Î º¯°æµË´Ï´Ù.<br>-ÀÌ¹Ì ÀÛ¼ºÇÑ ±ÛÀ» ´Ù¸¥ ÆùÆ®·Î ¼öÁ¤ÇÏ½Ã¸é ÀÌÀü¿¡ Àû¿ëµÈ ±ÛÀÚ »ö»ó°ú º¼µå Àû¿ëÀÌ ÇØÁ¦µË´Ï´Ù.")
		)
	{
		editor.doc.body.innerHTML = "";
	}
	
}

function isIE()
{
    var ua = navigator.userAgent.toLowerCase();
    return (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1);
}

// edit wyswyg html 
function editContent(act)
{
	if(typeof(editor)!="object"){alert(EditorLoadingMsg);return false;}	// ·ÎµùÀü¿¡ È£ÃâµÇ¸é ¿¡·¯¹ß»ý.

    var selection = editor.getSelection();
    var range = editor.createRange(selection);
    editor.range = range;

    switch (act)
    {        
				case "Bold" :
        case "Italic" :
        case "Underline" :
        case "StrikeThrough" :
        case "JustifyLeft" :
        case "JustifyCenter" :
        case "JustifyRight" :
            editor.doc.execCommand(act, false, null);
            break;
        case "fontcolor" :
        case "bgcolor" :
            showFontColorBox(act);
            break;
        case "link" :
            showLinkBox();
    }		
}

function setFont(oSelect)
{
	if(typeof(editor)!="object"){alert(EditorLoadingMsg);return false;}	// ·ÎµùÀü¿¡ È£ÃâµÇ¸é ¿¡·¯¹ß»ý.
	
	if (oSelect.options[oSelect.selectedIndex].value == '')
	{
			oSelect.selectedIndex = 0;
			return;
	}

	if (!isIE())
	{
			execSelect("FontName", oSelect);
	}
	else
	{
			var fontvalue = oSelect.options[oSelect.selectedIndex].value;

	if(!(fontvalue == 'µ¸¿ò' || fontvalue == '±¼¸²' || 
		 fontvalue == '¹ÙÅÁ' || fontvalue == '±Ã¼­' ||
		 fontvalue == 'verdana' || fontvalue == 'times'))
	{
		editor.doc.execCommand("RemoveFormat", null, null);
		
		var htmltextval = editor.doc.selection.createRange().htmlText;
		var textval = editor.doc.selection.createRange().text;			
		
		if (textval != '')
		{				
			var ReplaceString = "<FONT style='"+fontvalue+"'>"+htmltextval+"</FONT>";
			editor.doc.selection.createRange().pasteHTML(ReplaceString);
			
		}
		else
		{
			var ReplaceString = "<FONT style='"+fontvalue+"'><p>&nbsp;"+htmltextval+"</p></FONT>";
								
				var alltext = editor.doc.body.innerHTML;
				alltext = alltext + ReplaceString;
				editor.doc.body.innerHTML = alltext;
		}
	}
	else
	{
		execSelect("FontName", oSelect);
	}
	}
}

function setFontSize(oSelect)
{
	if(typeof(editor)!="object"){alert(EditorLoadingMsg);return false;}	// ·ÎµùÀü¿¡ È£ÃâµÇ¸é ¿¡·¯¹ß»ý.
	
    if (!isIE())
    {
        execSelect("FontSize", oSelect);
    }
    else
    {
		var sText = editor.createRange(editor.getSelection());				
		var textvalue = sText.text;
		var facevalue = sText.parentElement().face;
		var sizevalue = oSelect.options[oSelect.selectedIndex].value;
		
		if(typeof(facevalue)!="object" || 
			facevalue == 'µ¸¿ò' || facevalue == '±¼¸²' || 
			facevalue == '¹ÙÅÁ' || facevalue == '±Ã¼­' ||
			facevalue == 'verdana' || facevalue == 'times')
		{
			execSelect("FontSize", oSelect);
		}
	    else
	    {
	    	editor.doc.execCommand("RemoveFormat", null, null);
	    	var htmltextvalue = sText.htmlText;
	    	    	
	    	var ReplaceString = "<FONT face='µ¸¿ò'; size='"+sizevalue+"'>"+htmltextvalue+"</FONT>";
	    	sText.pasteHTML(ReplaceString);
		}	
    }
	oSelect.selectedIndex = 0;    
}

function execSelect(act, el)
{
	if(typeof(editor)!="object"){alert(EditorLoadingMsg);return false;}	// ·ÎµùÀü¿¡ È£ÃâµÇ¸é ¿¡·¯¹ß»ý.

    if (el.options[el.selectedIndex].value != '')
        editor.doc.execCommand(act, null, el.options[el.selectedIndex].value);

    el.selectedIndex = 0;
}

function showFontColorBox(act)
{
    if (act == "fontcolor")
    {
        displayFontColorBox(true);
        displayBGColorBox(false);
    }
    if (act == "bgcolor")
    {
        displayFontColorBox(false);
        displayBGColorBox(true);
    }
}

function displayFontColorBox(flag)
{
    if (flag)
        document.getElementById("TableFontColor").style.display = "";
    else
        document.getElementById("TableFontColor").style.display = "none";
}

function displayBGColorBox(flag)
{
    if (flag)
        document.getElementById("TableBGColor").style.display = "";
    else
        document.getElementById("TableBGColor").style.display = "none";
}

function setFontColor(color)
{
    if (editor.range != null && isIE())
    {
        editor.range.select();
    }
    editor.doc.execCommand("ForeColor", false, color);

    displayFontColorBox(false);
}

function setBGColor(color, ftcolor)
{
    if (editor.range != null && isIE())
    {
        editor.range.select();
    }

	if(isIE()){
		editor.doc.execCommand( "BackColor", null, color );
	}else{
		editor.doc.execCommand( "hilitecolor", null, color );
	}
    
    if (ftcolor != "") editor.doc.execCommand("ForeColor", null, ftcolor);

    displayBGColorBox(false);
}

function showLinkBox()
{
	if(typeof(editor)!="object"){alert(EditorLoadingMsg);return false;}	// ·ÎµùÀü¿¡ È£ÃâµÇ¸é ¿¡·¯¹ß»ý.
	
    editor.focus();
	if( isIE()){
		editor.doc.execCommand("CreateLink", true, null);
	}else{
		var URL = prompt("URLÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä:", "http://");
		var textsel = null;
		var selection = editor.getSelection();
		var range = editor.createRange(selection);
		textsel = range.toString();
		
		if ((URL != null) && (URL != "")) {
			if (textsel == '') { textsel = prompt("±ÛÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä:"); }
			if ((textsel != null) && (textsel != "")) {
				var html = '<a href="' + URL + '" target="_blank">' + textsel + '</a>';
				editor.doc.execCommand('insertHTML', false, html);		
			}
		}
	}	
}

function AddImg(dir_name)
{
	var h = 238;
  var w = 304;
	var left = (screen.width-w)/2;
  var top = (screen.height-h)/2;
	var tmpl = "/helpdesk/pop_addimg.php?dir_name=" + dir_name;

	window.open(tmpl, "new_window", "toolbar=no,menubar=no,personalbar=no,height="+h+",width="+w+",left="+left+",top="+top+",scroolbars=yes,resizable=no");
}

function FileListToString()
{
	var obj = document.getElementById("filelist");
	var objlen = obj.length;
  var text = "";

	try{
		for(i=1;i<objlen;i++){
			/*if (i==(objlen - 1))
			{
				text = text + obj[i].value;
			}
			else{*/
			text = text + obj[i].value + "|@|";
			/*}*/
		}
	}
	catch(e){
	}
	//alert(text);
  return text;
}

function SetValueImg(EnFileNm, filesize, type, fileurl, fileNm, imgWidth, imgHeight)
{
	try {
		if(EnFileNm.length <= 3 || filesize.length <= 0 || fileNm.length <= 3)
		{
			alert("ÁË¼ÛÇÕ´Ï´Ù! ÀÏ½ÃÀûÀ¸·Î ÀÌ¹ÌÁö ¾÷·Îµå°¡ ¾ÈµÇ°í ÀÖ½À´Ï´Ù. Àá½ÃÈÄ¿¡ ´Ù½Ã ¿Ã·ÁÁÖ¼¼¿ä.");
			return false;
		}
	} catch(e){
		alert("ÁË¼ÛÇÕ´Ï´Ù! ÀÏ½ÃÀûÀ¸·Î ÀÌ¹ÌÁö ¾÷·Îµå°¡ ¾ÈµÇ°í ÀÖ½À´Ï´Ù. Àá½ÃÈÄ¿¡ ´Ù½Ã ¿Ã·ÁÁÖ¼¼¿ä.");
		return false;	
	}
	//(linktext,comment,type,chk)
    addlink(EnFileNm, '', type, "1", fileurl);
		//alert('00');
		//AddFileList(fileNm + "   " + parseInt(parseInt(filesize)/1024) + "KB",fileurl + "/" + fileNm + "|" + filesize+ "@nhn@1@nhn@" + imgWidth + "@nhn@" + imgHeight);
		AddFileList(fileNm + "   " + parseInt(parseInt(filesize)/1024) + "KB",fileurl + "/" + fileNm + "|" + filesize + "|" + imgWidth + "|" + imgHeight);
    FileSizeCalc(filesize);
	//try{ Setok(); } catch(e){ }
}

function addlink(linktext,comment,type,chk,fileurl)
{
	//alert('1');

	try
	{
		if(linktext.indexOf("<") >= 0){
			alert("Çã¿ëÇÏÁö ¾Ê´Â ¸µÅ©ÁÖ¼ÒÀÔ´Ï´Ù");
			return;
		}

		var contents = "";
		var ext = linktext.slice(linktext.lastIndexOf(".")+1).toLowerCase();

		var imgid = parseInt((Math.random()*10000000));
		linktext = "http://board.aruon.com/gBoard" + fileurl + "/" + linktext;
		//alert('2')
		//alert(linktext)
		//alert('3')
		if  (ext == "gif" || ext == "jpg" || ext == "png"){
			linktext = linktext.replace(/'/g, "\'");

			if (type=='top' || type == 'bottom')
			{   
				contents = "<center><img src=\"" + linktext + "\"  id='userImg" + imgid + "' onload='setTimeout(\"resizeImage(" + imgid + ")\",200)' style='cursor:hand' onclick='popview(this.src)' border='0'>" + comment + "</center>" ;	 
			}
			else{
				contents = "<img src=\"" + linktext + "\" align='" + type + "' id='userImg" + imgid + "' onload='setTimeout(\"resizeImage(" + imgid + ")\",200)' style='cursor:hand' onclick='popview(this.src)' border='0'>" + comment  ;
			}
		}
		
		//alert(contents);
		if (type=='top')
		{
			var myCon = editor.doc.body.innerHTML;
			editor.doc.body.innerHTML =  contents+ "<br>" + myCon ;
		}
		else if (type=='bottom')
		{
			var myCon = editor.doc.body.innerHTML;
			editor.doc.body.innerHTML =  myCon+ "<br>" + contents;
		}
		else
		{
			if(isIE())
			{
				var sText = eobj.document.selection.createRange();
				
				if (sText.parentElement().all.POSTEDITOR == null					
					&& sText.parentElement().all.papermain == null)
				{
					if (sText.parentElement().outerHTML.indexOf('<BODY') > 0)
					{
						sText.parentElement().innerHTML = "<P>" + contents + sText.parentElement().innerHTML  +  "</P>"
					}
					else
					{
						sText.parentElement().innerHTML = contents + sText.parentElement().innerHTML  
					}
				}
				else
				{
					var myCon = eobj.document.body.innerHTML;
					eobj.document.body.innerHTML = "<P>" + contents + myCon + "</P>";
				}
			}else{
				var myCon = editor.doc.body.innerHTML;
				editor.doc.body.innerHTML =  myCon+ "<br>" + contents + "<br>";
			}
		}
	}
	catch(e)
	{           
		//alert('ÀÌ¹ÌÁö »ðÀÔÀ» ÇÒ ¼ö ¾ø½À´Ï´Ù'+e);
		var myCon = editor.doc.body.innerHTML;
		editor.doc.body.innerHTML =  myCon+ "<br>" + contents + "<br>";
	}

	editor.focus();
}

function AddFileList(text,value)
{
   var obj = document.getElementById("filelist");
   obj[obj.length] = new Option(text, value);
}

function FileSizeCalc(filesize){	
	var attachAllrealsize = document.all["attachAllrealsize"].value;	
	attachAllrealsize = parseInt(attachAllrealsize) + parseInt(filesize);
	document.all["attachAllrealsize"].value = attachAllrealsize;
	document.all["attachAllsize"].value = parseInt((attachAllrealsize)/1024);
}