﻿/**
* 이 script는 HTML Editor를 사용하기 위한 script입니다.
*/


var sHeader="<style>P {margin-top:10px; margin-bottom:10px}</style>\n<body  topmargin=\"10\" marginwidth=\"0\" marginheight=\"10\" leftmargin=\"1\" rightmargin=\"0\"  style=\"font-size:9pt; font-family:Arial,굴림\">"
var sFooter="</body>\n</html>";

//작성, 답변을 위한 init
function init(contentType){ 
	bltn_body.document.designMode = "On";
	bltn_body.document.open();
	bltn_body.document.write(sHeader);
	bltn_body.document.close();
	bltn_body.document.execCommand("2D-Position", 1);
	bltn_body.document.execCommand("LiveResize", 1);
	type_change(contentType)
	//document.form.title.focus();	
}

//수정을 위한 init
function modifyInit(contentType){
	bltn_body.document.designMode = "On";
	bltn_body.document.open();
	bltn_body.document.write(sHeader);
	bltn_body.document.write(document.aform.body.value);
	bltn_body.document.write(sFooter);
	bltn_body.document.close();
	bltn_body.document.execCommand("2D-Position", 1);
	bltn_body.document.execCommand("LiveResize", 1);
	type_change(contentType);
	//document.form.title.focus();	
	//bltn_body.focus();
}
    
function popUp(menuName, on){
	if(gotlayers){
		if(on){
			moveLayers(menuName);
			if(DOM){
				document.getElementById(menuName).style.visibility = "visible";
			}else{
				document.all[menuName].style.visibility = "visible";
			}
		}else{
			if(DOM){
				document.getElementById(menuName).style.visibility = "hidden";
			}else{
				document.all[menuName].style.visibility = "hidden";
			}
		}

		popWin(document.getElementById(menuName), menuName, '');
	}
}

//called by popUp
//htmltable is DIV id

var gotlayers = 1;
IE4 = (document.all) ? 1 : 0;
DOM = (document.getElementById) ? 1 : 0;

function moveLayers(menuName){
	if (gotlayers){
		if(menuName == 'htmltable')
		{
			if(DOM){
				screenWidth = (document.body.clientWidth ? document.body.clientWidth : window.innerWidth) + 18;
				document.getElementById(menuName).style.left = 290;
				document.getElementById(menuName).style.top = 130;
			}else{
				screenWidth = document.body.clientWidth + 18;
				document.all[menuName].style.pixelLeft = screenWidth-350;
				document.all[menuName].style.pixelTop = 36;
			}
		}else if(menuName == 'htmlmovie')
		{
			if(DOM){
				screenWidth = (document.body.clientWidth ? document.body.clientWidth : window.innerWidth) + 18;
				document.getElementById(menuName).style.left = 290;
				document.getElementById(menuName).style.top = 200;
			}else{
				screenWidth = document.body.clientWidth + 18;
				document.all[menuName].style.pixelLeft = screenWidth-350;
				document.all[menuName].style.pixelTop = 36;
			}
		}else if(menuName == 'htmlImage')
		{
			if(DOM){
				screenWidth = (document.body.clientWidth ? document.body.clientWidth : window.innerWidth) + 18;
				document.getElementById(menuName).style.left = 290;
				document.getElementById(menuName).style.top = 200;
			}else{
				screenWidth = document.body.clientWidth + 18;
				document.all[menuName].style.pixelLeft = screenWidth-350;
				document.all[menuName].style.pixelTop = 36;
			}
		}else if(menuName == 'htmlflush')
		{
			if(DOM){
				screenWidth = (document.body.clientWidth ? document.body.clientWidth : window.innerWidth) + 18;
				document.getElementById(menuName).style.left = 290;
				document.getElementById(menuName).style.top = 200;
			}else{
				screenWidth = document.body.clientWidth + 18;
				document.all[menuName].style.pixelLeft = screenWidth-350;
				document.all[menuName].style.pixelTop = 36;
			}
		}
	}
}

//createRange()
function insTable(){
	var divObj = document.getElementById('htmltable');
	var row = divObj.document.getElementById('rows').value;
	var col = divObj.document.getElementById('cols').value;
	var width = divObj.document.getElementById('width').value;
	var height = divObj.document.getElementById('height').value;
	var cellpadding = divObj.document.getElementById('cellpadding').value;
	var cellspacing = divObj.document.getElementById('cellspacing').value;
	var border = divObj.document.getElementById('border').value;
	var tablealign = divObj.document.getElementById('tablealign').value;
	var align = divObj.document.getElementById('align').value;
	var valign = divObj.document.getElementById('valign').value;
	var bdcolor = divObj.document.getElementById('bdcolor').value;
	var bgcolor = divObj.document.getElementById('bgcolor').value;

	var str = "<table style='width:" + width + "; height:" + height + "' border='" + border + "' bordercolor='" + bdcolor + "' align='" + tablealign + "' cellpadding='" + cellpadding + "' cellspacing='" + cellspacing + "' bgcolor='" + bgcolor + "'>";
	for(var i=1; i<=row; i++){
		str = str + "<tr>";
		for(var j=1; j<=col; j++){
			str = str + "<td valign='" + valign + "' align='" + align + "'></td>";
		}
		str = str + "</tr>";
	}
	str = str + "</table>";

	bltn_body.focus();
	bltn_body.document.selection.createRange().pasteHTML(str);
	popUp('htmltable', false);
}

function insImage(){
	var divObj = document.getElementById('htmlImage');
	var imageUrl = divObj.document.getElementById('imageUrl').value;

	if(imageUrl != '')
	{
		var str = "<IMG src='" + imageUrl + "' >";

		bltn_body.focus();
		bltn_body.document.selection.createRange().pasteHTML(str);
	}
	popUp('htmlImage', false);
}

function insFlash(){
	var divObj = document.getElementById('htmlflash');
	var flashUrl = divObj.document.getElementById('flashUrl').value;

	if(flashUrl != '')
	{
		var str = "<embed src='" + flashUrl + "' quality='high' pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='200' height='200' wmode='transparent'></embed>";

		bltn_body.focus();
		bltn_body.document.selection.createRange().pasteHTML(str);
	}
	popUp('htmlflash', false);
}

function insMovie(){
	var divObj = document.getElementById('htmlmovie');
	var movieUrl = divObj.document.getElementById('movieUrl').value;

	if(movieUrl != '')
	{
		var str = "<EMBED src='" + movieUrl + "' quality='high' width='200' height='200' wmode='transparent'></EMBED>";

		bltn_body.focus();
		bltn_body.document.selection.createRange().pasteHTML(str);
	}
	popUp('htmlmovie', false);
}

//  edRange
function EditExec(cmd){
	if(document.all['bltn_body'].style.display == "block"){
		bltn_body.focus();
		edRange = bltn_body.document.selection.createRange();
		edRange.execCommand(cmd);
	}
}

function EditExec2(cmd, value){
	if(document.all['bltn_body'].style.display == "block"){
		bltn_body.focus();
		edRange = bltn_body.document.selection.createRange();
		edRange.execCommand(cmd, false, value);
	}
}
  
function EditExec3(cmd, value){
	var index = value.selectedIndex;
	var argv2 = value.options[index].value;
	EditExec2(cmd, argv2);
}


//text창의 'text/html'에 의한 화면 변경
function type_change(value) {
    if(value == "TEXT") {
		//document.form.body.value = bltn_body.document.body.innerHTML;
        document.aform.body.style.display = 'block';
        document.all['bltn_body'].style.display = "none";
        document.all['buttontable'].style.visibility = 'hidden';
        //document.all['buttontable'].style.position = 'absolute';
        //alert("bltn_body.document.body.innerHTML:" + bltn_body.document.body.innerHTML);
		document.aform.body.value = bltn_body.document.body.innerHTML;
		document.aform.body.style.visibility = 'visible';
		document.aform.body.focus();
     } else {
     	//alert("document.aform.body.value:" + document.aform.body.value);
        bltn_body.document.body.innerHTML = document.aform.body.value;
        bltn_body.focus();
		document.aform.body.style.display = 'none';
		document.aform.body.style.visibility = 'hidden';
        document.all['bltn_body'].style.display = "block";
        document.all['buttontable'].style.position = 'relative';
        document.all['buttontable'].style.visibility = 'visible';

		//document.form.title.focus();
     }
}
