var onEditMode = true;
var Selection = null;

function initEditor(mode) 
{
	if(mode=="N"){
		
	    var source = "<html><head><style>";
	        source += ".Background {background-color:#FFFFFF} ";
	        source += "P {margin-top:2px;margin-bottom:2px;} ";
	        source += "BLOCKQUOTE {margin-top:2px;margin-bottom:2px;margin-left:9px;} "; 
	        source += "table {border:1 solid C6C3C6}";
	        source += ".itable {border:1 solid C6C3C6}</style>";
	        source += "<script>";
	        source += "function resizeImage(num){}";
	        source += "</script></head>";
	        source += "<body class=\"Background\"></body></html>";
	
	    EDITOR1.document.designMode="On";
	    EDITOR1.document.open("text/html");
	    EDITOR1.document.write(source);
	    EDITOR1.document.close();
	
	    EDITOR1.document.body.style.fontSize = "9pt";
	    EDITOR1.document.body.style.fontFamily = "돋움";
	} else {
		
	    var source = "<html><head><style>.Background {background-color:#FFFFFF; topmargin:0 leftmargin:0} P {margin-top:2px;margin-bottom:2px;} table {border:1 solid C6C3C6}</style><script>function resizeImage(num){}</script></head><body class=\"Background\"></body></html>";
		
	    EDITOR1.document.open("text/html");
	    EDITOR1.document.write(source);
		EDITOR1.document.close();
		EDITOR1.document.body.style.fontSize = "9pt";
	    EDITOR1.document.body.style.fontFamily = "돋움";
	    
	    setContent();
	    
	}
	EDITOR1.document.onmouseup = function() { EDITOR1_onmouseup(); }
}



function replaceIT(string, suchen, ersetzen){
 ausgabe= "" + string;
 while(ausgabe.indexOf(suchen)>-1){
  pos=ausgabe.indexOf(suchen);
  ausgabe = "" + (ausgabe.substring(0, pos) + ersetzen +
  ausgabe.substring((pos + suchen.length), ausgabe.length));
 }
 return ausgabe;
}

function setFont(oSelect)
{
    if (oSelect.options[oSelect.selectedIndex].value != '')
        EDITOR1.document.execCommand("FontName", null, oSelect.options[oSelect.selectedIndex].value);
                   
    oSelect.selectedIndex = 0;   
}

function add_indent(v)
{
	EDITOR1.focus();
	if (Selection != null) {
		Selection.select();
  }

	if(v>0) {
	  EDITOR1.document.execCommand("indent",false, null);
	} else if (v<0) {
	  EDITOR1.document.execCommand("outdent",false,null);
	}

}


function postContent()
{
  HTMLSOURCE = EDITOR1.document.body.innerHTML;

	EDITOR1.document.body.createTextRange().execCommand("copy"); 
	
    return HTMLSOURCE;
}

function setFontSize(oSelect)
{
	if (oSelect.options[oSelect.selectedIndex].value != '')
  	EDITOR1.document.execCommand("FontSize", null, oSelect.options[oSelect.selectedIndex].value);
	
	oSelect.selectedIndex = 0;
}

function editContent(act)
{
    if (!onEditMode)
    {
        alert("HTML 모드에서는 에디터를 사용할수 없습니다.");
        return;
    }                        

    Selection = EDITOR1.document.selection.createRange();
    if (Selection != null) Selection.select();
    if (Selection.text == '') return;

	switch (act)
    {
        case "Bold" :
        case "Italic" :
        case "Underline" :
        case "StrikeThrough" :        
        case "JustifyLeft" :
        case "JustifyCenter" :
        case "JustifyRight" :
            EDITOR1.document.execCommand(act);
            break;
        case "fontcolor" :
        case "bgcolor" :        
            showFontColorBox(act);
            break;
        case "link" :
            showLinkBox();
		 break;
		case "font_bold"           : Selection.text = '<strong>' + Selection.text + '</strong>' ;		break;
    }
}

function showFontColorBox(act)
{
    if (act == "fontcolor")
    {
        document.all["ShowFontColorBox"].style.display = "";
        document.all["ShowBGColorBox"].style.display = "none";
    }
    if (act == "bgcolor")
    {
        document.all["ShowFontColorBox"].style.display = "none";
        document.all["ShowBGColorBox"].style.display = "";
    }
}

function showLinkBox()
{
    EDITOR1.focus();
    EDITOR1.document.execCommand("CreateLink",true);
}


function setFontColor(color)
{
    if (Selection != null) 
        Selection.select();
    EDITOR1.document.execCommand("ForeColor", false, color);

    document.all["ShowFontColorBox"].style.display = "none";
    EDITOR1.document.selection.empty();
}

function setBGColor(color, ftcolor)
{
    if (Selection != null) 
        Selection.select();
        
    EDITOR1.document.execCommand("BackColor", null, color );
    
    if (ftcolor != "")
    	EDITOR1.document.execCommand("ForeColor", null, ftcolor);

    document.all["ShowBGColorBox"].style.display = "none";
    EDITOR1.document.selection.empty();
}

function setBackColor(color)
{
    EDITOR1.document.body.style.backgroundColor = color;
}

var sepFile = "||";
var setField = "|";
function popImage()
{
}

function popFile(cluburl)
{
}

function popLink()
{
}

function popSketch()
{
}

function popPoll()
{
}

function addLink(attachURL, comment, alignPos, chk)
{
    try
    {
        var attachTag = getAttachTag(attachURL, comment, alignPos, chk);

        if (alignPos=='top')
        {
            EDITOR1.document.body.innerHTML =  attachTag+ "<br>" + EDITOR1.document.body.innerHTML;
        }
        else if (alignPos=='bottom')
        {
            EDITOR1.document.body.innerHTML =  EDITOR1.document.body.innerHTML + "<br>" + attachTag ;
        }
        else
        {
            var sText = EDITOR1.document.selection.createRange();
            
            if (sText.parentElement().all.EDITOR1 == null)
            {
                if (sText.parentElement().outerHTML.indexOf('<BODY') > 0)
                {
                    sText.parentElement().innerHTML = "<P>" + attachTag + sText.parentElement().innerHTML  +  "</P>";
                }
                else
                {
                    sText.parentElement().innerHTML = attachTag + sText.parentElement().innerHTML;
                }
            }
               else
               {
                EDITOR1.document.body.innerHTML = "<P>" + attachTag + EDITOR1.document.body.innerHTML + "</P>";
            }
        }
    }
    catch(e)
    {
        EDITOR1.document.body.innerHTML = "<P>" + attachTag + EDITOR1.document.body.innerHTML + "</P>";
    }
    
    EDITOR1.focus();
}

function getAttachTag(attachURL, comment, alignPos, chk)
{
    var attachTag = "";
        
    if (chk == "1")
    {
         attachURL = "http://cafefiles.naver.net" + attachURL;
    }
        
    attachURL = attachURL.replace(/'/g, "\'");
            
    if  (attachURL.match(/.jpg|.jpeg|.gif|.png$/i))
    {
        var imgid = parseInt((Math.random()*10000000));    
        
        if (alignPos == 'top' || alignPos == 'bottom')
            attachTag = "<center><img src='" + attachURL + "' align='" + alignPos + "' id='userImg" + imgid + "' onload='setTimeout(\"resizeImage(" + imgid + ")\",200)' style='cursor:hand' onclick='popview(\"" + attachURL + "\")'>" + comment + "</center>" ;
        else
            attachTag = "<img src='" + attachURL + "' align='" + alignPos + "' id='userImg" + imgid + "' onload='setTimeout(\"resizeImage(" + imgid + ")\",200)' style='cursor:hand' onclick='popview(\"" + attachURL + "\")'>" + comment ;
    }
    else 
    {
        attachTag = "<embed autostart='false' src='" + attachURL + "' >" + comment;
    }
    
    return attachTag;
}

function addList(dirname, filename, filesize)
{
    if (filename == 'poll@nhn')
    {
        document.all.attachfilelist.add(new Option('폴이 첨부되었습니다' , filename));
    }
    else
    {
        if (filesize)
        {    
            calcFileSize(filesize, 1);
            document.all.attachfilelist.add(new Option(filename + "   " + parseInt(parseInt(filesize)/1024) + "KB", dirname +"/" + filename));
        }
        else
            document.all.attachfilelist.add(new Option(filename, dirname +"/" + filename));
    }
}

function calcFileSize(filesize, oper)
{
    if (oper == 1)
    {
        document.all.attachsizerealsum.value = parseInt(document.all.attachsizerealsum.value) + parseInt(filesize);
    }
    else
    {
        document.all.attachsizerealsum.value = parseInt(document.all.attachsizerealsum.value) - parseInt(filesize);
    }
    
    document.all.attachsizesum.value = parseInt(document.all.attachsizerealsum.value/1024);    
}

function addPoll(attachpolls)
{
    document.all.attachpollyn.value = "Y";
    document.all.attachpolls.value = attachpolls;
    document.all.imgPoll.src = "./images/btn_poll_edit.gif";
}

function removePoll()
{
    document.all.attachpollyn.value = "";
    document.all.attachpolls.value = "";
    document.all.imgPoll.src = "./images/btn_poll.gif";
}

function addFile(dirname, filename, filesize, filetype)
{
    if (filetype == "F") document.all.attachfileyn.value = "Y";
    if (filetype == "I") document.all.attachimageyn.value = "Y";
    document.all.attachfiles.value = document.all.attachfiles.value + dirname +"/" + filename + "|" + filetype + "||";
    document.all.attachsizes.value = document.all.attachsizes.value + filesize + "||";
}

function removeFile(index)
{
    arrAttachfile = document.all.attachfiles.value.split("||");
    arrAttachsize = document.all.attachsizes.value.split("||");

    calcFileSize(arrAttachsize[index], -1);    
    
    arrAttachfile.remove(index);
    arrAttachsize.remove(index);
    document.all.attachfiles.value = arrAttachfile.join("||");
    document.all.attachsizes.value = arrAttachsize.join("||");
    
    
    document.all.attachfileyn.value = "";
    document.all.attachimageyn.value = "";
    for (var i = 0; i<arrAttachfile.length; i++)
    {
        if (arrAttachfile[i].match(/F$/))
        {
            document.all.attachfileyn.value = "Y";
        }
        if (arrAttachfile[i].match(/I$/))
        {
            document.all.attachimageyn.value = "Y";
        }
    }
}

function removeAttach()
{
    var attachlist = document.all.attachfilelist;
    
    if (attachlist.selectedIndex <= 0)
        return;

    if (attachlist.options[attachlist.selectedIndex].value == "poll@nhn")
        removePoll();
    else
        removeFile(attachlist.selectedIndex-1);
    
    attachlist.remove(attachlist.selectedIndex);    
}

function decodeContent(str)
{
    return str.replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&amp;/gi,"&");
}

function setContent()
{	
	var str = document.all.DIVcontent.innerHTML;
    EDITOR1.document.body.innerHTML = str;
}

function checkpos()
{
    var sText = EDITOR1.document.selection.createRange();
    
    if (sText.parentElement().all.EDITOR1 == null && sText.parentElement().all.papermain == null)
    {
        var lineval = sText.parentElement().outerHTML.toLowerCae();

        var idx = lineval.indexOf("<font");
        if (idx > -1)
        {
            try 
            {
                if (sText.parentElement().face != null)
                {
                    if (sText.parentElement().face != "")
                        document.all.fontface.value = sText.parentElement().face;
                    else
                        document.all.fontface.selectedIndex = 1;
                }
                else
                {
                    document.all.fontface.selectedIndex = 1;
                }

                if (sText.parentElement().size != null)
                {
                    if (sText.parentElement().size != "")
                        document.all.fontsize.value = sText.parentElement().size;
                    else
                        document.all.fontsize.selectedIndex = 0;
                }
                else
                {
                    document.all.fontsize.selectedIndex = 0;
                }
            }
            catch(e) {}
        }
        else
        {
            try 
            {
                document.all.fontface.selectedIndex = 1;
                document.all.fontsize.selectedIndex = 0;
            }
            catch(e) {}

        }
    }
}

function checkposkey()
{
    if (EDITOR1.event.keyCode >= 37 && EDITOR1.event.keyCode <= 40)
    {
        var sText = EDITOR1.document.selection.createRange();
        
        if (sText.parentElement().all.EDITOR1 == null && sText.parentElement().all.papermain == null)
        {
            var lineval = sText.parentElement().outerHTML.toLowerCase();
            var idx = lineval.indexOf("<font");
            
            if (idx > -1)
            {
                try {
                    if (sText.parentElement().face != null)
                    {
                        if (sText.parentElement().face != "")
                            document.all.fontface.value = sText.parentElement().face;
                        else
                            document.all.fontface.selectedIndex = 1;
                    }
                    else
                    {
                        document.all.fontface.selectedIndex = 1;
                    }
                    
                    if (sText.parentElement().size != null)
                    {
                        if (sText.parentElement().size != "")
                            document.all.fontsize.value = sText.parentElement().size;
                        else
                            document.all.fontsize.selectedIndex = 0;
                    }
                    else
                    {
                        document.all.fontsize.selectedIndex = 0;
                    }
                }
                catch(e) {}
            }
            else
            {
                document.all.fontface.selectedIndex = 1;
                document.all.fontsize.selectedIndex = 0;
            }
        }
    }
}

document.write("" 
  + "<div id='TableFontColor' style='position:absolute;top:50;left:240;display:none;'>"
  + "<table width='200' border='0' cellspacing='1' cellpadding='0' bgcolor='#CCCCCC'>"
  + "<tr>" 
  + "<td align='center' bgcolor='#FFFFFF' width='85'>" 
  + "<table width='111' border='0' cellspacing='4' cellpadding='0' height='88' style='cursor:hand'>"
  + "<tr>" 
  + "<td bgcolor='#008000' onclick='setFontColor(\"#008000\");'>&nbsp;</td>"
  + "<td bgcolor='#009966' onclick='setFontColor(\"#009966\");'>&nbsp;</td>"
  + "<td bgcolor='#99CC66' onclick='setFontColor(\"#99CC66\");'>&nbsp;</td>"
  + "<td bgcolor='#999966' onclick='setFontColor(\"#999966\");'>&nbsp;</td>"
  + "<td bgcolor='#CC9900' onclick='setFontColor(\"#CC9900\");'>&nbsp;</td>"
  + "</tr>"
  + "<tr>" 
  + "<td bgcolor='#D41A01' onclick='setFontColor(\"#D41A01\");'>&nbsp;</td>"
  + "<td bgcolor='#FF0000' onclick='setFontColor(\"#FF0000\");'>&nbsp;</td>"
  + "<td bgcolor='#FF7635' onclick='setFontColor(\"#FF7635\");'>&nbsp;</td>"
  + "<td bgcolor='#FF9900' onclick='setFontColor(\"#FF9900\");'>&nbsp;</td>"
  + "<td bgcolor='#FF3399' onclick='setFontColor(\"#FF3399\");'>&nbsp;</td>"
  + "</tr>"
  + "<tr>" 
  + "<td bgcolor='#9B18C1' onclick='setFontColor(\"#9B18C1\");'>&nbsp;</td>"
  + "<td bgcolor='#993366' onclick='setFontColor(\"#993366\");'>&nbsp;</td>"
  + "<td bgcolor='#666699' onclick='setFontColor(\"#666699\");'>&nbsp;</td>"
  + "<td bgcolor='#0000FF' onclick='setFontColor(\"#0000FF\");'>&nbsp;</td>"
  + "<td bgcolor='#177FCD' onclick='setFontColor(\"#177FCD\");'>&nbsp;</td>"
  + "</tr>"
  + "<tr>" 
  + "<td bgcolor='#006699' onclick='setFontColor(\"#006699\");'>&nbsp;</td>"
  + "<td bgcolor='#003366' onclick='setFontColor(\"#003366\");'>&nbsp;</td>"
  + "<td bgcolor='#333333' onclick='setFontColor(\"#333333\");'>&nbsp;</td>"
  + "<td bgcolor='#8E8E8E' onclick='setFontColor(\"#8E8E8E\");'>&nbsp;</td>"
  + "<td bgcolor='#C1C1C1' onclick='setFontColor(\"#C1C1C1\");'>&nbsp;</td>"
  + "</tr>"
  + "</table>"
  + "</td>"
  + "<td align='center' bgcolor='#FFFFFF' width='162' valign='top'>"
  + "<br>"
  + "직접입력"
  + "<font face='Verdana' size='1'>" 
  + "<input type='text' name='FontBox' style='width:60px' maxlength='6' class='box' value=''>"
  + "</font>"
  + "<br>"
  + "<table width='5' border='0' cellspacing='0' cellpadding='0' height='10'>"
  + "<tr>" 
  + "<td>&nbsp;</td>"
  + "</tr>"
  + "</table>"
  + "<a href='javascript:setFontColor(document.all.FontBox.value);'><img src='./images/btn_ok01.gif' width='32' height='19' align='absmiddle'></a>"
  + "<br>"
  + "</td>"
  + "</tr>"
  + "</table>"
  + "</div>");

document.write(""
  + "<div id='TableBGColor' style='position:absolute;top:0;left:260;display:none;'>"
  + "<table width='200' border='0' cellspacing='1' cellpadding='0' bgcolor='#CCCCCC'>"
  + "  <tr>" 
  + "    <td align='center' bgcolor='#FFFFFF' width='85'>" 
  + "      <table width='111' border='0' cellspacing='5' cellpadding='0' height='88' style='cursor:hand'>"
  + "        <tr valign='top'>" 
  + "          <td bgcolor='#FFDAED' width='53' onclick='setBGColor(\"#FFDAED\",\"\");'>바탕색</td>"
  + "          <td bgcolor='#FF0000' width='53' onclick='setBGColor(\"#FF0000\",\"#FFFFFF\");'><font color='#FFFFFF'>바탕색</font></td>"
  + "        </tr>"
  + "        <tr valign='top'>" 
  + "          <td bgcolor='#99DCFF' width='53' onclick='setBGColor(\"#99DCFF\",\"\");'>바탕색</td>"
  + "          <td bgcolor='#0000FF' width='53' onclick='setBGColor(\"#0000FF\",\"#FFFFFF\");'><font color='#FFFFFF'>바탕색</font></td>"
  + "        </tr>"
  + "        <tr valign='top'>" 
  + "          <td bgcolor='#A6FF4D' width='53' onclick='setBGColor(\"#A6FF4D\",\"\");'>바탕색</td>"
  + "          <td bgcolor='#009966' width='53' onclick='setBGColor(\"#009966\",\"#FFFFFF\");'><font color='#FFFFFF'>바탕색</font></td>"
  + "        </tr>"
  + "        <tr valign='top'>" 
  + "          <td bgcolor='#E4FF75' width='53' onclick='setBGColor(\"#E4FF75\",\"\");'>바탕색</td>"
  + "          <td bgcolor='#E4E4E4' width='53' onclick='setBGColor(\"#E4E4E4\",\"\");'>바탕색</td>"
  + "        </tr>"
  + "        <tr valign='top'>" 
  + "          <td bgcolor='#333333' width='53' onclick='setBGColor(\"#333333\",\"#FFFFFF\");'><font color='#FFFFFF'>바탕색</font></td>"
  + "          <td bgcolor='#333333' width='53' onclick='setBGColor(\"#333333\",\"#FFFF00\");'><font color='#FFFF00'>바탕색</font></td>"
  + "        </tr>"
  + "        <tr valign='top'>" 
  + "          <td bgcolor='#8E8E8E' width='53' onclick='setBGColor(\"#8E8E8E\",\"#FFFFFF\");'><font color='#FFFFFF'>바탕색</font></td>"
  + "          <td bgcolor='#CDCDCD' width='53' onclick='setBGColor(\"#CDCDCD\",\"#FFFFFF\");'><font color='#FFFFFF'>바탕색</font></td>"
  + "        </tr>"
  + "        <tr valign='top'>" 
  + "          <td width='53' onclick='setBGColor(\"\",\"\");'>바탕색</td>"
  + "          <td width='53'>&nbsp;</td>"
  + "        </tr>"
  + "      </table>"
  + "    </td>"
  + "    <td align='center' bgcolor='#FFFFFF' width='162'>"
  + "      <br>"
  + "      직접입력"
  + "      <br>"
  + "      <input type='text' name='FontBox2' style='width:60px' maxlength='6' class='box' value='' align='absmiddle'>"
  + "      <br>"
  + "      <table width='5' border='0' cellspacing='0' cellpadding='0' height='10'>"
  + "        <tr>" 
  + "          <td>&nbsp;</td>"
  + "        </tr>"
  + "      </table>"
  + "      <a href='javascript:setBGColor(document.all.FontBox2.value,\"\");'><img src='./images/btn_ok01.gif' width='32' height='19' align='absmiddle'></a>"
  + "      <br>"
  + "      <br>"
  + "    </td>"
  + "  </tr>"
  + "</table>"
  + "</div>");
  
document.write(""
  + "<div id='TableLink' style='position:absolute;top:100;left:280;display:none;'>"
  + "<table width=260 border=0 cellspacing=1 cellpadding=3 bgcolor='#CCCCCC'>"
  + "<tr>"
  + "<td bgcolor='#FFFFFF'>"
  + "<input type='text' name='linkbox' style='width:215px' class='box' value='http://' align='absmiddle'>"
  + "<a href='javascript:setLink(document.all.linkbox.value);'><img src='./images/btn_ok01.gif' width='32' height='19' align='absmiddle'></a>"
  + "</td>"
  + "</tr>"
  + "</table>"
  + "</div>");
