
var onEditMode = true;

function editorinit() {

var source = "<html><head><style>P {margin-top:2px;margin-bottom:2px;} table {border:1 solid C6C3C6}</style><script>function resizeImage(num){}</script></head><body></body></html>"
POSTEDITOR.document.designMode="On"
POSTEDITOR.document.open("text/html");
POSTEDITOR.document.write(source);
POSTEDITOR.document.close();
POSTEDITOR.document.body.style.fontSize = "9pt";
//POSTEDITOR.document.body.style.fontFamily = "µ¸¿ò";
//POSTEDITOR.document.oncontextmenu = new Function("return false;");
POSTEDITOR.document.onmouseup = checkpos;
POSTEDITOR.document.onkeyup = checkposkey;

}
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);

}

var Selection = null;
			 
function SetFont(ch){
		
if (ch.options[ch.selectedIndex].value != '')
       POSTEDITOR.document.execCommand("FontName", null, ch.options[ch.selectedIndex].value);
       			       
//ch.selectedIndex = 0;   

			
}
function SetFontSize(ch){
			
if (ch.options[ch.selectedIndex].value != '')
          POSTEDITOR.document.execCommand("FontSize", null, ch.options[ch.selectedIndex].value);
			          
//ch.selectedIndex = 0;

}
function PostContents(){
/*try{
	if (document.all["HtmlYn"].checked == false)
	{
		HTMLSOURCE = POSTEDITOR.document.body.innerHTML;
	}
	else{
		HTMLSOURCE = document.all.contents.value
	}
}
catch(e){*/
var HTMLSOURCE = "";
try{
   HTMLSOURCE = POSTEDITOR.document.body.innerHTML;
}
catch(e){
}
return HTMLSOURCE;
			
}
			
function checkpos(){

	 try{
   	var sText = POSTEDITOR.document.selection.createRange();
	if (sText.parentElement().all.POSTEDITOR == 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 = 0;
				 }
             }
				 else{
					 document.all.fontsize.selectedIndex = 0;
				 }

			 }
			 catch(e){}

		
		 }else{
			 try{
             document.all.fontface.selectedIndex = 1;
             document.all.fontsize.selectedIndex = 0;
			 }
			 catch(e){}

		 }
	}
}
			 catch(e){}
}

function checkposkey(){

	if (POSTEDITOR.event.keyCode >= 37 && POSTEDITOR.event.keyCode <= 40)
	{
			var sText = POSTEDITOR.document.selection.createRange();
		if (sText.parentElement().all.POSTEDITOR == 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 = 0;
				 }
             }
				 else{
					 document.all.fontsize.selectedIndex = 0;
				 }
			 }
			 catch(e){}

			 }else{
			 try{
             document.all.fontface.selectedIndex = 1;
             document.all.fontsize.selectedIndex = 0;
			 }
			 catch(e){}

		 }
		}

	}

}
	
function action(order){

if (!onEditMode)
{
	alert("HTML ¸ðµå¿¡¼­´Â ¿¡µðÅÍ¸¦ »ç¿ëÇÒ¼ö ¾ø½À´Ï´Ù.")
	return;
}						

Selection = POSTEDITOR.document.selection.createRange();
if (Selection!=null)Selection.select();

	if (order == "Bold"){
	  POSTEDITOR.document.execCommand("Bold");
	}
	else if (order == "Italic"){
	  POSTEDITOR.document.execCommand("Italic");
	}
	else if (order == "Underline"){
	  POSTEDITOR.document.execCommand("Underline");
	}
	else if (order == "fontcolor") {
	  FontColor('1');
	}
	else if (order == "bgcolor") {
	  FontColor('2');
	}
	else if (order == "alignLeft") {
	  POSTEDITOR.document.execCommand("JustifyLeft");
	}
	else if (order == "alignCenter") {
	  POSTEDITOR.document.execCommand("JustifyCenter");
	}
	else if (order == "alignRight")   {
	  POSTEDITOR.document.execCommand("JustifyRight");  
	}
	else if (order == "link")   {
	   POSTEDITOR.focus();
	   POSTEDITOR.document.execCommand("CreateLink",1);  // Open_link();	
	}
	else if (order == "Strike")   {
	  POSTEDITOR.document.execCommand("StrikeThrough");
	}

	else if (order == "html")
	{
		//alert(PostContents());
		view_but.innerHTML = "<a href=\"javascript:action('editer');\"><img src=\"/Images/Editor/butt_img24_editer.gif\" width=\"65\" height=\"17\" border=\"0\"></a>";
			
		EditView.style.display = "none";
		HTMLView.style.display = "";
		
		document.all.HTMLSource.value = PostContents();
	}
	else if (order == "editer")
	{
		view_but.innerHTML = "<a href=\"javascript:action('html');\"><img src=\"/Images/Editor/butt_img24_html.gif\" width=\"65\" height=\"17\" border=\"0\"></a>";
		
		var edit_chk;
		edit_chk = document.all.HTMLSource.editchk;
		
		var edit_str = "";

		edit_str = document.all.HTMLSource.value;
		
		EditView.style.display = "";
		HTMLView.style.display = "none";
		
		if(edit_chk == "Y")
			POSTEDITOR.document.body.innerHTML = edit_str;
	}
}

function hidemenu(){
		document.all["TableFontColor"].style.display = "none";
		document.all["TableLink"].style.display = "none";
		document.all["TableFontBackColor"].style.display = "none";
}

function FontColor(ch)
{
	if (navigator.userAgent.indexOf("MSIE 5.0") > 0)
	{
	     //alert("´ç½ÅÀÇ ºê¶ó¿ìÁ®´Â 5.0 ÀÌ±º¿ä!");
		 document.all["TableFontColor"].style.top = "65";
	     document.all["TableFontBackColor"].style.top = "65";
	}

	if (ch=="1")
	{
		ModalessDialog('/IKY_INC/HtmlEditor/select_color.asp?mode=font', window, 238, 187 );
		//document.all["TableFontColor"].style.display = "";
		//document.all["TableLink"].style.display = "none";
		//document.all["TableFontBackColor"].style.display = "none";


	}
	if (ch=="2")
	{
		ModalessDialog('/IKY_INC/HtmlEditor/select_color.asp?mode=bgcolor', window, 238, 187 )
		//document.all["TableLink"].style.display = "none";
		//document.all["TableFontColor"].style.display = "none";
		//document.all["TableFontBackColor"].style.display = "";

	}
}


function Set_fontcolor(color){

    if (Selection!=null)Selection.select();
    POSTEDITOR.document.execCommand("forecolor", null, color);
    //document.all["TableFontColor"].style.display = "none";
	POSTEDITOR.document.selection.empty()
	
}

function Set_bgcolor(color,color2){
    
if (Selection!=null)Selection.select();
	POSTEDITOR.document.execCommand( "BackColor", null, color );
	if (color2 != "")
	{
          POSTEDITOR.document.execCommand("forecolor", null, color2);
	}
    //document.all["TableFontBackColor"].style.display = "none";
	POSTEDITOR.document.selection.empty()

}

function Set_backcolor(color){

	POSTEDITOR.document.body.style.backgroundColor = color;

}

function Open_link(){
document.all["TableFontColor"].style.display = "none";
document.all["TableFontBackColor"].style.display = "none";
document.all["TableLink"].style.display = "";


}

function Set_link(mylink){
if (Selection!=null)Selection.select();
	//var mylink = prompt("¸µÅ©¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.","http://");
	POSTEDITOR.document.execCommand("CreateLink", null, mylink);
	document.all["TableLink"].style.display = "none";
	document.all["linkbox"].value = 'http://';

}

function addcatepop(paperId){

	window.open("/post/add_category.asp?blogId=" + paperId,"addcate","width=360,height=250,scrollbars=no");

}
function addsketchpop(paperId){

	window.open("/post/sketch/add_sketch.asp?blogId=" + paperId,"addsketch","width=600,height=520,scrollbars=no");

}

function addlinkpop(paperId){

	window.open("/post/add_link.asp?blogId=" + paperId,"addlink","width=330,height=230,scrollbars=no");

}

function addfilepop(paperId){

	window.open("/post/add_file.asp?blogId=" + paperId,"addfile","width=330,height=170,scrollbars=no");

}

function addimagepop(paperId,pos){
		if (pos=="0")
		{
			window.open("/post/add_image.asp?blogId=" + paperId,"addimage","width=330,height=270,scrollbars=no");
		}
	  else if(pos=="noencode"){
	    window.open("/post/add_image.asp?blogId=" + paperId + "&Sposition=1&noencode=ok","addimage","width=330,height=270,scrollbars=no");
	  }
		else{
			
			window.open("/post/add_image.asp?blogId=" + paperId + "&Sposition=1","addimage","width=330,height=270,scrollbars=no");
		}

}

function addlink(linktext,comment,type,chk){
	try{
		var contents = "";
		var ext = linktext.slice(linktext.lastIndexOf(".")+1).toLowerCase();
		
		var imgid = parseInt((Math.random()*10000000));
		if (chk=="1")
		{
			 //linktext = "/blog/userdir" + linktext;
		}

		if  (ext == "gif" || ext == "jpg" || ext == "png" || ext == "bmp")
		{
			linktext = linktext.replace(/'/g, "\'");
			
			if (type=='top' || type == 'bottom')
			{   
				contents = "<center><img src='" + (linktext) + "'  id='userImg'>" + comment + "</center>" ;	 
				//alert(escape(linktext));
			}
			else{
				contents = "<img src='" + (linktext) + "' align='" + type + "' id='userImg'>" + comment  ;
			}
		}
		else {//if (ext == "wma" || ext == "wmv" || ext == "asf" || ext == "asx" || ext == "swf" || ext == "mp3"){
			linktext = linktext.replace(/'/g, "\'");
			contents = "<embed autostart='false' src='" + linktext + "' wmode='transparent'>" + comment;
		}

		//alert(contents);
		if (type=='top')
		{
			var myCon = POSTEDITOR.document.body.innerHTML;
			POSTEDITOR.document.body.innerHTML =  contents+ "<br>" + myCon ;
		}
		else if (type=='bottom')
		{
			var myCon = POSTEDITOR.document.body.innerHTML;
			POSTEDITOR.document.body.innerHTML =  myCon+ "<br>" + contents ;
		}
		else
		{
			var sText = POSTEDITOR.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 = POSTEDITOR.document.body.innerHTML;
				POSTEDITOR.document.body.innerHTML = "<P>" + contents + myCon + "</P>";
			}
			 //alert(POSTEDITOR.document.body.innerHTML);
		}
		
		//alert(POSTEDITOR.document.body.innerHTML);
	}
	catch(e)
	{           
	// alert('ÀÌ¹ÌÁö »ðÀÔÀ» ÇÒ ¼ö ¾ø½À´Ï´Ù')
		var myCon = POSTEDITOR.document.body.innerHTML;
		POSTEDITOR.document.body.innerHTML = "<P>" + contents + myCon + "</P>";
	}
	POSTEDITOR.focus();
}

function SetValue(EnFileNm,filesize,type,fileurl,fileNm){
    
    addlink(EnFileNm,'',type,"1");
	AddFileList(fileNm + "   " + parseInt(parseInt(filesize)/1024) + "KB",fileurl + fileNm + "@izy@" + filesize+ "@izy@1");
    FileSizeCalc(filesize);

	try{
		Setok();
	}
	catch(e){
	}

}

function AddFileList(text,value){
   
   var obj = document.all.filelist;
   //alert(text);
   //alert(value);
   obj[obj.length] = new Option(text, value);
}

function DelFileList(blog_id,b_id,addDate){
	try{
		var idx = document.all.filelist.options.selectedIndex;
		
		if (idx == 0)
		{
			alert("»èÁ¦ÇÏ½Ç ÆÄÀÏÀ» ¼±ÅÃÇØ ÁÖ¼¼¿ä.")
			return;
		}

		var objvalue = document.all.filelist.options[idx].value;
		
		actionframe.location="/IKY_INC/HtmlEditor/delete_file_action.asp?uid="+blog_id+"&b_id="+b_id+"&idx=" + idx + "&filename=" + objvalue;
	}
	catch(e){
		alert("»èÁ¦ÇÏ½Ç ÆÄÀÏÀ» ¼±ÅÃÇØ ÁÖ¼¼¿ä.")
	}
}

function DelFileListGuest(paperId,logNo,addDate){
	try{
		var idx = document.all.filelist.options.selectedIndex;
		if (idx == 0)
		{
			alert("»èÁ¦ÇÏ½Ç ÆÄÀÏÀ» ¼±ÅÃÇØ ÁÖ¼¼¿ä.")
			return;
		}

		var objvalue = document.all.filelist.options[idx].value;
		
		window.open("/guestbook/fileDelete.asp?blogId=" + paperId + "&idx=" + idx + "&logNo=" + logNo + "&filename=" + objvalue+"&addDate="+addDate,"filedel","scrollbars=no,width=100,height=100");

	}
	catch(e){
		alert("»èÁ¦ÇÏ½Ç ÆÄÀÏÀ» ¼±ÅÃÇØ ÁÖ¼¼¿ä.")
	}
}

// ÆÄÀÏ»èÁ¦½Ã ÆÄÀÏ¸®½ºÆ®¿¡¼­ »èÁ¦, ÆÄÀÏÀüÃ¼»çÀÌÁî Á¶Á¤
function DelFileConfirm(idx,filesize){
    var obj = document.all.filelist;
	obj.options[idx] = null;
    FileSizeCalcM(filesize)
}

// ÆÄÀÏ¸®½ºÆ®¸¦ ½ºÆ®¸µÀ¸·Î ¸¸µé¾î¼­ hidden °ª¿¡ ³Ö´Â´Ù.
function FileListToString(){
    var obj = document.all.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 + "<izy>";
        }
	}
}
catch(e){
		alert(e.message)
}
    return text;
}


// ÆÄÀÏ ¾÷·Îµå ÈÄ ÆÄÀÏÀ» ¼¿·ºÆ®¹Ú½º¿¡ »Ñ·ÁÁÖ±â
function SetValueFile(filename,filesize,filetail,fileMode){

	/*var fileText = "";


	fileText = '<table border="0" cellpadding="0" cellspacing="0"><tr><td colspan=2 height=5></td></tr>';		
	fileText = fileText + '<tr><td width="80"><b>Ã·ºÎÆÄÀÏ</b></td>';
	fileText = fileText + '<td width="440"><img src="/imgs/ico_disk.gif" hspace="3" align="absmiddle"> <a href="' + filename + '" target="_blank">' + filetail + '</a> <a href="javascript:DelAttach(\'1\');"><img src="/imgs/btn_del01.gif" align="absmiddle" hspace="3"></a></td>';
	fileText = fileText + '</tr></table>';
*/
	//document.all["fileYn"].innerHTML = fileText;
	//document.all["fileYn"].style.display = "";
	//document.all["attach"].value = filename;
	//document.all["attachsize"].value = filesize;
//	alert(filename + "   " + parseInt(parseInt(filesize)/1024) + "KB",filename + "@izy@" + filesize + "@izy@" + filetail + "@izy@" + fileMode);
    AddFileList(filename + "   " + parseInt(parseInt(filesize)/1024) + "KB",filename + "@izy@" + filesize + "@izy@" + filetail + "@izy@" + fileMode);
	FileSizeCalc(filesize);
  //  paperInit();
	try{
		Setok();
	}
	catch(e){
	}

}

//ÆÄÀÏ»çÀÌÁî °è»ê
function FileSizeCalc(filesize){

	var attachAllrealsize = document.all["attachAllrealsize"].value;
	attachAllrealsize = parseInt(attachAllrealsize > 0 ? attachAllrealsize : 0) + parseInt(filesize);
    document.all["attachAllrealsize"].value = attachAllrealsize;
    document.all["attachAllsize"].value = parseInt(attachAllrealsize/1024);

}

//ÆÄÀÏ»çÀÌÁî °è»ê
function FileSizeCalcM(filesize){

	var attachAllrealsize = document.all["attachAllrealsize"].value;
	attachAllrealsize = parseInt(attachAllrealsize) - parseInt(filesize);
    document.all["attachAllrealsize"].value = attachAllrealsize;
    document.all["attachAllsize"].value = parseInt(attachAllrealsize/1024);


}


function ShowHtml(){

	if (document.WriteFrm.HtmlYn.checked == true)
	{
		document.WriteFrm.contents.value = POSTEDITOR.document.body.innerHTML;
        document.all.POSTEDITOR.style.display = "none";
		document.WriteFrm.contents.style.display = "";

		onEditMode = false;
	}
	else{

        editorinit();
		POSTEDITOR.document.body.innerHTML = document.WriteFrm.contents.value;
		document.WriteFrm.contents.style.display = "none";
		document.all.POSTEDITOR.style.display = "";

        onEditMode = true;
	}
}

function DelAttach(ch){

	var tempsize = document.all["attachsize"].value;
	document.all["fileYn"].style.display = "none";
	document.all["attach"].value = "";
	document.all["attachsize"].value = 0;

	if (ch=="1")
	{
	    FileSizeCalcM(tempsize);
	}
}


function pop_editor_insert(umode,uid)
{
	var num=document.all["filenum"].value;
	
	if (parseInt(num)>=100)
	{
		alert("ÆÄÀÏÃ·ºÎ´Â 100°³±îÁö¸¸ °¡´ÉÇÕ´Ï´Ù");
	}
	else
	{
		url="/IKY_INC/HtmlEditor/pop_editor_insert.asp?uid="+uid+"&umode="+umode;
		var win = window.open(url,umode,"width=358,height=317");
		win.focus();
	}
}

function plusFileNum()
{
	document.all["filenum"].value=parseInt(document.all["filenum"].value)+1;
}

/**
	parameter values 
 		url :	 »ó´ë °æ·Î È¤Àº Àý´ë °æ·Î (¿¹: ./target.asp )
		args 	: ¸®ÅÏ¹ÞÀ» °ªÀ» À§ÇÑ °´Ã¼ - Modal/Modaless Dialog´Â Á÷Á¢ ¸®ÅÏ¸øÇÔ 
			(¿¹ : window or document.form )
		width 	: »ý¼ºÇÒ ´ÙÀÌ¾ó·Î±×ÀÇ °¡·Î ±æÀÌ (pixcel)
		height 	: »ý¼ºÇÒ ´ÙÀÌ¾ó·Î±×ÀÇ ¼¼·Î ±æÀÌ (pixcel)

	return values
		args	: °´Ã¼ÀÇ Æ÷ÀÎÅÍ (document.form °ú °°Àº °´Ã¼ µî)
*/


// Modal Dialog¸¦ À§ÇÑ Wrapper MethodÀÔ´Ï´Ù.
function ModalDialog( url, args, width, height )
{
	var env_options = "dialogHeight: " + height + "px; dialogWidth: " + width + "px;  edge: Raised; center: Yes; help: No; scroll: No; resizable: No; status: No;";
	window.showModalDialog( url, args, env_options);
}

// Modeless Dialog¸¦ À§ÇÑ Wrapper MethodÀÔ´Ï´Ù.
function ModalessDialog( url, args, width, height )
{
	var env_options = "dialogHeight: " + height + "px; dialogWidth: " + width + "px;  edge: Raised; center: Yes; help: No; scroll: No; resizable: No; status: No;";
	window.showModelessDialog( url, args, env_options);
}

function inputUrl(url)
{
	document.winfrm.url.value=url;
}


function downloadFile(file)
{
	actionframe.location.href="/IKY_INC/HtmlEditor/file_download.asp?file="+file;
}


function resizeImage(Maxwidth)
{	
	var f_obj = document.getElementById('pageContents'); 
	
	if(!f_obj){
		f_obj = document;
	}
	var imgs = f_obj.getElementsByTagName('IMG');
	
	if (imgs == null) return;

	var state = false;

	for(i=0;i<imgs.length;i++)
	{
		if(imgs[i].id != "resize_none")
		{
					
			width = parseInt(imgs[i].width);
		
			height = parseInt(imgs[i].height);

			//alert(width);
		
			if (width <= 1 && height <= 1)
			{
				state=!state;
			}		
					
			if (!state && (width > Maxwidth)) // ÀÌ¹ÌÁö°¡  Maxwidth º¸´Ù Å©¸é ¸®»çÀÌÂ¡ 	
			{
				imgs[i].style.width = Maxwidth;
				imgs[i].style.height = (Maxwidth/width) * height;
			}
		}
	}

	
}

function startEmbed()
{
	var imgs=document.all.tags('embed');
	if (imgs==null) return;

	for(i=0;i<imgs.length;i++)
	{
		if (i==0) 
		{
			imgs[i].play();
		}
		else
		{
			imgs[i].stop();
		}
	}
}
