var CTX_PATH = "";
var EDITOR_HEIGHT = 352;
var onEditMode = true;
// ÇöÀç¼±ÅÃµÈ ¿¡µðÅÍÃ¢

var eobj = null;

var Selection = null;

var DESC_WIDTH = 0;
var DESC_HEIGHT = 0;

function EditOn()
{

	resizeEditor(0);
	eobj = POSTEDITOR;
	POSTEDITOR.focus();
	
	//var initcontents = "<html><head><LINK REL='StyleSheet' HREF='/styless.css' type='text/css'>";
	var initcontents = "<html><head><LINK REL='StyleSheet' HREF='/css/style.css' type='text/css'>";
	initcontents = initcontents + "</head><body STYLE='font-size:10pt; font-family:±¼¸²;'>";
	initcontents = initcontents + "<p></p>";
	initcontents = initcontents + "</body></html>";

	POSTEDITOR.document.designMode="On"

	POSTEDITOR.document.open("text/html");
	POSTEDITOR.document.write(initcontents);
	POSTEDITOR.document.close();

	
	POSTEDITOR.document.onmouseup = checkpos;
	POSTEDITOR.document.onkeyup = checkposkey;

	//eobj.focus();	
}

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

function SetContents()
{
	POSTEDITOR.document.body.innerHTML = ContentsDecode(document.all.contentsDiv.innerHTML);
}

function SetFont(ch)
{
			
	if (ch.options[ch.selectedIndex].value != '')
	{
		eobj.document.execCommand("FontName", null, ch.options[ch.selectedIndex].value);
		POSTEDITOR.focus();
	}
			
}

function SetFontSize(ch)
{
	if (ch.options[ch.selectedIndex].value != '')
	{
		eobj.document.execCommand("FontSize", null, ch.options[ch.selectedIndex].value);
		POSTEDITOR.focus();
	}
}

function PostContents()
{
	var HTMLSOURCE = "";
	try
	{
	   	HTMLSOURCE = POSTEDITOR.document.body.innerHTML;
	}
	catch(e)
	{
	}
 
	return HTMLSOURCE;
		
}
			
function checkpos()
{
	
	try{
		var sText = eobj.document.selection.createRange();

		if (sText.parentElement().all.eobj == null && sText.parentElement().all.papermain == null)
		{
		
			var lineval = sText.parentElement().outerHTML;
			lineval = lineval.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 = 2;
						}
					}
					else
					{
						document.all.fontsize.selectedIndex = 2;
					}
				}
				catch(e)
				{
				}
			}
			else
			{
				try
				{
					document.all.fontface.selectedIndex = 1;
					document.all.fontsize.selectedIndex = 2;
				}
				catch(e)
				{
				}
			}
		}
	}
	catch(e)
	{
	}
}

function checkposkey()
{
	if (eobj.event.keyCode >= 37 && eobj.event.keyCode <= 40)
	{
		var sText = eobj.document.selection.createRange();
		
		if (sText.parentElement().all.eobj == null && sText.parentElement().all.papermain == null)
		{
			 var lineval = sText.parentElement().outerHTML;
			 lineval = lineval.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 = 2;
						 }
             		}
				 	else
				 	{
						document.all.fontsize.selectedIndex = 2;
				 	}
			 	}
			 	catch(e)
			 	{
			 	}
			}
			else
			{
				try
				{
		             document.all.fontface.selectedIndex = 1;
		             document.all.fontsize.selectedIndex = 2;
			 	}
			 	catch(e)
			 	{
			 	}
			}
		}
	}
}
	
function action(order)
{

	if (!onEditMode)
	{
		alert("HTML ¸ðµå¿¡¼­´Â ¿¡µðÅÍ¸¦ »ç¿ëÇÒ¼ö ¾ø½À´Ï´Ù.")
		return;
	}						

	Selection = eobj.document.selection.createRange();

	if (Selection!=null)Selection.select();

	if (Selection.text == '')
	{
		return;
	}	
	
	if (order == "Bold")
	{
	  eobj.document.execCommand("Bold");	 
	}
	else if (order == "Italic")
	{
	  eobj.document.execCommand("Italic");
	}
	else if (order == "Underline")
	{
	  eobj.document.execCommand("Underline");
	}
	else if (order == "alignLeft") 
	{
	  eobj.document.execCommand("JustifyLeft");
	}
	else if (order == "alignCenter") 
	{
	  eobj.document.execCommand("JustifyCenter");
	}
	else if (order == "alignRight")   
	{
	  eobj.document.execCommand("JustifyRight");  
	}
	else if (order == "link")   
	{
	   eobj.focus();
	   eobj.document.execCommand("CreateLink",1);
	}
	else if (order == "Strike")   
	{
	  eobj.document.execCommand("StrikeThrough");
	}

}

function validAttachImg(filename)
{
	if(!unescape(filename.toUpperCase()).match(/(.JPG|.JPEG|.GIF|.PNG)/))
	{
		alert("À¥ºê¶ó¿ìÀú°¡ Áö¿øÇÏ´Â ÀÌ¹ÌÁö ÆÄÀÏÀº jpg, jpeg, gif, png ÆÄÀÏÀÔ´Ï´Ù.\nÀÌ¹ÌÁö ÆÄÀÏÀ» ´Ù½Ã ¼±ÅÃÇØÁÖ½Ê½Ã¿ä.");
		return false;
	}
	return true;
}

function addlink(linktext,comment,type,chk,seq_no)
{
	try
	{
	
		//comment : ÇöÀç »ç¿ëÇÏÁö ¾Ê½À´Ï´Ù.
		
		if(linktext.indexOf("<") >= 0)
		{
			alert("Çã¿ëÇÏÁö ¾Ê´Â ¸µÅ©ÁÖ¼ÒÀÔ´Ï´Ù");
			return;
		}
		var contents1 = "";
		var contetns2 = "";
		var ext = linktext.slice(linktext.lastIndexOf(".")).toLowerCase();
		
		var imgid = seq_no;//parseInt((Math.random()*10000000));
		
		// º»¹®¿¡ Ãß°¡ÀÌ¸é
		if (chk==1)
		{
			if(unescape(ext.toUpperCase()).match(/(SWF|MP3|WAV|WMA|MID|WMV|AVI)/))
			{
				linktext = linktext.replace(/'/g, "\'");


				contents1 = "<P><embed autostart='false' src='" + linktext + "' >" + comment + "</P>";	
			}
			else
			{
				
				linktext = linktext.replace(/'/g, "\'");
			
				
				if (type=='top' || type == 'bottom')
				{   
					contents1 = "<P><center><img src='" + linktext + "'  id='userImg:" + imgid + ":userImg'>" + comment + "</center></P>";
				}
				else
				{
					//contents1 = "<P>";
					contents1 = "<table border='0' align='" + type + "'>";
					contents1 += "<tr>";
					contents1 += "<td class='editImg'>";

					contents1 += "<img src='" + linktext + "' id='userImg:" + imgid + ":userImg' onload='resizeDescriptImage(this);' border='0'>";
					
					contents2 = "<P align='" + type + "'><img src='" + linktext + "' id='userImg:" + imgid + ":userImg' onload='resizeDescriptImage(this);' border='0'></P>";
					
					contents1 += "</td>";
					contents1 += "</tr>";
					contents1 += "</table>";
					//contents1 += "</P>";
				}
			}
			

			if (type=='top')
			{
				var myCon = eobj.document.body.innerHTML;
				eobj.document.body.innerHTML =  contents+ "<br>" + myCon ;
			}
			else if (type=='bottom')
			{
				var myCon = eobj.document.body.innerHTML;
				eobj.document.body.innerHTML =  myCon+ "<br>" + contents ;
			}
			else
			{				
				var sText = eobj.document.selection.createRange();

				
				if (sText.parentElement().all.POSTEDITOR == null)
				{
					if(sText.parentElement().outerHTML.indexOf("userImg:") > 0)
					{// Ä¿¼­°¡ À§Ä¡ÇÑ°÷¿¡ ÀÌ¹ÌÁö°¡ ÀÖ´Ù¸é
						sText.parentElement().innerHTML = contents2 + sText.parentElement().innerHTML;
					}
					else
					{
						sText.parentElement().innerHTML = contents1 + sText.parentElement().innerHTML;
					}
				}				
				else
				{// ¹«Á¶°Ç ¾Õ¿¡ Ãß°¡ (focus °¡ POSTEDITOR¿¡ ¾øÀ»°æ¿ì)
					var myCon = eobj.document.body.innerHTML;
					eobj.document.body.innerHTML = myCon + contents1;
					//¹«Á¶°Ç µÚ¿¡ Ãß°¡
				}
			}
		}
	}
	catch(e)
	{           
		//alert('ÀÌ¹ÌÁö µî·ÏÀ» ÇÒ ¼ö ¾ø½À´Ï´Ù');
		var myCon = eobj.document.body.innerHTML;
		//eobj.document.body.innerHTML = "<P>" + contents + myCon + "</P>";
		eobj.document.body.innerHTML = myCon + contents1;
		//¹«Á¶°Ç µÚ¿¡ Ãß°¡
	}
	//eobj.focus();
}



function addlink2(linktext,comment,type,chk,seq_no)
{
	try
	{		
		if(linktext.indexOf("<") >= 0)
		{
			alert("Çã¿ëÇÏÁö ¾Ê´Â ¸µÅ©ÁÖ¼ÒÀÔ´Ï´Ù");
			return;
		}

		var contents1 = "";
		var contetns2 = "";
		var ext = linktext.slice(linktext.lastIndexOf(".")).toLowerCase();
		
		var imgid = seq_no;//parseInt((Math.random()*10000000));
		
		// º»¹®¿¡ Ãß°¡ÀÌ¸é
		if (chk==1)
		{
			if(unescape(ext.toUpperCase()).match(/(SWF|MP3|WAV|WMA|MID|WMV|AVI)/))
			{
				linktext = linktext.replace(/'/g, "\'");


				contents1 = "<P><embed autostart='false' src='" + linktext + "' >" + comment + "</P>";	
			}
			else
			{
				
				linktext = linktext.replace(/'/g, "\'");
			
				
				if (type=='top' || type == 'bottom')
				{   
					contents1 = "<P><center><img src='" + linktext + "'  id='userImg:" + imgid + ":userImg'>" + comment + "</center></P>";
				}
				else
				{
					//contents1 = "<P>";
					contents1 = "<table border='0' align='" + type + "'>";
					contents1 += "<tr>";
					contents1 += "<td class='editImg'>";

					contents1 += "<img src='" + linktext + "' id='userImg:" + imgid + ":userImg' onload='resizeDescriptImage(this);' border='0'>";
					
					contents2 = "<P align='" + type + "'><img src='" + linktext + "' id='userImg:" + imgid + ":userImg' onload='resizeDescriptImage(this);' border='0'></P>";
					
					contents1 += "</td>";
					contents1 += "</tr>";
					contents1 += "</table>";
					//contents1 += "</P>";
				}
			}
			

			if (type=='top')
			{
				var myCon = eobj.document.body.innerHTML;
				eobj.document.body.innerHTML =  contents+ "<br>" + myCon ;
			}
			else if (type=='bottom')
			{
				var myCon = eobj.document.body.innerHTML;
				eobj.document.body.innerHTML =  myCon+ "<br>" + contents ;
			}
			else
			{				
				var sText = eobj.document.selection.createRange();
				
				if (sText.parentElement().all.POSTEDITOR == null)
				{
					if(sText.parentElement().outerHTML.indexOf("userImg:") > 0)
					{
						sText.parentElement().innerHTML = contents2 + sText.parentElement().innerHTML;
					}
					else
					{
						sText.parentElement().innerHTML = contents1 + sText.parentElement().innerHTML;
					}
				}
				else
				{
					var myCon = eobj.document.body.innerHTML;
					eobj.document.body.innerHTML = myCon + contents1;
				}
			}
		}
	}
	catch(e)
	{           
		var myCon = eobj.document.body.innerHTML;
		eobj.document.body.innerHTML = myCon + contents1;
	}
}


function openFontBackColor() 
{
	openLayerMenu(CTX_PATH+"/common/layer_fontbackcolor.jsp", 150, 110, "no");	
}

function openFontColor() 
{
	openLayerMenu(CTX_PATH+"/common/layer_fontcolor.jsp", 150, 110, "no");	
}

function resizeEditor(dir) 
{
    if (dir == 0) document.all.POSTEDITOR.style.height = EDITOR_HEIGHT;
    if (dir == 1) document.all.POSTEDITOR.style.height = EDITOR_HEIGHT*2;
}

function resizeDescriptImage(resizeObj)
{
	try
	{
		if(resizeObj.width > DESC_WIDTH || resizeObj.offsetWidth > DESC_WIDTH)
		{
			resizeObj.width = DESC_WIDTH - 4;
		}
	}
	catch(ex)
	{
	}	
}

function filogLink()
{
    eobj.focus();

	//var contents = " <A href='http://www.eummoney.com/"+login_user+"'>http://www.eummoney.com/"+login_user+"</A> ";
	var contents = " <A href=\"javascript:openFilogHome('"+login_user+"')\">http://www.eummoney.com/"+login_user+"</A> ";

	try
	{
		var sText = eobj.document.selection.createRange();

		//sText.execCommand("CreateLink", false, contents);

		if (sText.parentElement().all.eobj == null)
		{
			sText.parentElement().innerHTML = contents + sText.parentElement().innerHTML;
		}
		else
		{
			var myCon = eobj.document.body.innerHTML;
			eobj.document.body.innerHTML = myCon + contents;
		}
	}
	catch(e)
	{           
		var myCon = eobj.document.body.innerHTML;
		eobj.document.body.innerHTML = myCon + contents;

	}
}
