var Myselect = null;

function Editinit(mode)
{
	var fn = document.writeform;

		if(mode == 'write') var Mysource = '<style>P {margin-top:2px;margin-bottom:2px;}</style>'
		else if(mode == 'modify') {
			fn.content.value=fn.content.value.replace(/&amp;/g,'&');
			fn.content.value=fn.content.value.replace(/&gt;/g,'>');
			fn.content.value=fn.content.value.replace(/&lt;/g,'<');
			fn.content.value=fn.content.value.replace(/&nbsp;/g,' ');
			//fn.content.value=fn.content.value.replace(/\r\n/g,'<P>');
			var Mysource = '<style>P {margin-top:2px;margin-bottom:2px;}</style>'+fn.content.value;
			fn.content.value = "";
		}
		
		HTMLEDITOR.document.designMode='On'
		HTMLEDITOR.document.open('text/html');
		HTMLEDITOR.document.write(Mysource);
		HTMLEDITOR.document.close();
		HTMLEDITOR.document.body.style.fontSize = '13px';
		HTMLEDITOR.document.body.style.fontFamily = '±¼¸²';
		HTMLEDITOR.document.oncontextmenu = new Function('return false;');
}
	
function SetFont(ch){
			
	if (ch.options[ch.selectedIndex].value != '')
		HTMLEDITOR.document.execCommand("FontName", null, ch.options[ch.selectedIndex].value);
	ch.selectedIndex = 0;   
}

function SetFontSize(ch){
	if (ch.options[ch.selectedIndex].value != '')
		HTMLEDITOR.document.execCommand("FontSize", null, ch.options[ch.selectedIndex].value);
	ch.selectedIndex = 0;
}

function menu_click(obj){
	Myselect = HTMLEDITOR.document.selection.createRange();
	if (Myselect!=null) Myselect.select();


	if (obj.id == "id_Bold") HTMLEDITOR.document.execCommand("Bold");
	else if (obj.id == "id_Italic") HTMLEDITOR.document.execCommand("Italic");
	else if (obj.id == "id_Underline") HTMLEDITOR.document.execCommand("Underline");
	else if (obj.id == "id_fontcolor") Preview_Color('visible',1,obj);
	else if (obj.id == "id_bgcolor") Preview_Color('visible',2,obj);
	else if (obj.id == "id_backcolor") Preview_Color('visible',3,obj);
	else if (obj.id == "id_alignLeft") HTMLEDITOR.document.execCommand("JustifyLeft");
	else if (obj.id == "id_alignCenter") HTMLEDITOR.document.execCommand("JustifyCenter");
	else if (obj.id == "id_alignRight") HTMLEDITOR.document.execCommand("JustifyRight");
	else if (obj.id == "id_link") Set_link();

}

function Set_link(){
	var mylink = prompt("¸µÅ© ÁÖ¼Ò¸¦ ÀÔ·ÂÇØ ÁÖ¼¼¿ä.","http://");
	Myselect = HTMLEDITOR.document.selection.createRange();
	if (Myselect!=null){
    		Myselect.pasteHTML("<a href=\"" + mylink + "\" target=\"_blank\">" + Myselect.text + "</a> ");
	}
}


function Preview_Color(mode,type,obj){
	if(mode == 'visible'){
		if(type == 1) leftV = 130;
		else if(type == 2) leftV = 150;
		else if(type == 3) leftV = 170;

		Preview_ColorLF.type.value = type;
		Preview_ColorLF.color.value='Á÷Á¢ÀÔ·Â';
		//document.all('Preview_ColorL').style.top = eval(document.body.scrollTop + 330);
		//document.all('Preview_ColorL').style.left = document.all('main').offsetLeft + 290 + leftV;
		document.all('Preview_ColorL').style.top = getAbsoluteTop(obj)+obj.offsetHeight;
		document.all('Preview_ColorL').style.left = getAbsoluteLeft(obj)-obj.offsetWidth;
		document.all('Preview_ColorL').style.zIndex = 1;
		document.all('Preview_ColorL').style.visibility = 'visible';
	}
	else{
		document.all('Preview_ColorL').style.top = -1000000;
		document.all('Preview_ColorL').style.left = 0;
		document.all('Preview_ColorL').style.zIndex = -1;
		document.all('Preview_ColorL').style.visibility = 'hidden';
	}
}

function Preview_Color_Select(color){
	if(Preview_ColorLF.type.value == 1) Set_fontcolor(color);
	else if(Preview_ColorLF.type.value == 2) Set_bgcolor(color);
	else if(Preview_ColorLF.type.value == 3) Set_backcolor(color);
	Preview_Color('hidden','','');
}

function Set_fontcolor(color){
	if(Myselect!=null) Myselect.select();
	HTMLEDITOR.document.execCommand("forecolor", null, color);
	HTMLEDITOR.document.selection.empty();
}

function Set_bgcolor(color){
	if(Myselect!=null) Myselect.select();
	HTMLEDITOR.document.execCommand( "BackColor", null, color );
	HTMLEDITOR.document.selection.empty();
}

function Set_backcolor(color){
	var obj = HTMLEDITOR.document.all["backcolor"];
	if (obj != null)
		HTMLEDITOR.document.all["backcolor"].style.backgroundColor = color;
	else
		HTMLEDITOR.document.body.style.backgroundColor = color;
}

function GetSource(){
	var backcolor = HTMLEDITOR.document.body.style.backgroundColor;
	var obj = HTMLEDITOR.document.all["backcolor"];
	//var HTMLSOURCE = '';
	var HTMLSOURCE = HTMLEDITOR.document.body.innerHTML;
	/*
	if (HTMLEDITOR.document.body.innerHTML == ''){
		alert('³»¿ëÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä');
		HTMLEDITOR.focus();
		return;
	}
	*/
	if (backcolor.length == 0 || obj != null)
	{
		HTMLSOURCE = "<STYLE>P {MARGIN-TOP: 2px; MARGIN-BOTTOM: 2px}</STYLE>\n" + HTMLEDITOR.document.body.innerHTML;
	}
	else
	{
		HTMLSOURCE = "<div id='backcolor' style='background-color:" + backcolor + "'><STYLE>P {MARGIN-TOP: 2px; MARGIN-BOTTOM: 2px}</STYLE>\n" + HTMLEDITOR.document.body.innerHTML + "</div>";
	}
	return HTMLSOURCE;

}


function calculate()
{
	var fn = document.writeform;

	if(fn.edittype.value=='normal'){
		fn.content.value = GetSource();
	}
	else if(fn.edittype.value=='txt'){
		fn.content.value=fn.content.value.replace(/&/g,'&amp;');
		fn.content.value=fn.content.value.replace(/>/g,'&gt;');
		fn.content.value=fn.content.value.replace(/</g,'&lt;');
		fn.content.value=fn.content.value.replace(/\r\n/g,'<BR>');
	}
}
function editOption(type)
{
	var fn = document.writeform;

	if(type=='txt'){
		if(fn.edittype.value=='html')
			fn.content.value=stringreplace(fn.content.value,'txt');
		else
			fn.content.value=stringreplace(HTMLEDITOR.document.body.innerHTML,'txt');
			document.all.HTMLEDITOR.style.display='none';
			document.all.edittitle.style.display='none';
			fn.content.style.display = '';		
			fn.content.focus();			
	}
	else if(type=='html'){
		if(fn.edittype.value=='txt')
			fn.content.value=stringreplace(fn.content.value,'html');
		else
			fn.content.value=stringreplace(HTMLEDITOR.document.body.innerHTML,'html');
			document.all.HTMLEDITOR.style.display='none';
			document.all.edittitle.style.display='none';
			fn.content.style.display = '';
			fn.content.focus();
	}
	else if(type=='normal'){
			//HTMLEDITOR.document.body.innerHTML = stringreplace(fn.content.value,'normal');
			HTMLEDITOR.document.body.innerHTML = fn.content.value;
			document.all.HTMLEDITOR.style.display='';
			document.all.edittitle.style.display='';
			fn.content.style.display = 'none';
			HTMLEDITOR.focus();
	}
	else if(type=='set'){
			type='normal';
			fn.content.style.display = 'none';
			HTMLEDITOR.focus();
			}
	fn.edittype.value=type;
}
function stringreplace(txt,type)
{
	var fn = document.writeform;

	var returnvalue='';
	if(type=='normal'){
		returnvalue=txt.replace(/\r\n/g,'<BR>');
	}
	else if(type=='html'){
		txt=txt.replace(/&amp;/g,'&');
		txt=txt.replace(/&gt;/g,'>');
		txt=txt.replace(/&lt;/g,'<');
		txt=txt.replace(/&nbsp;/g,' ');
		returnvalue=txt.replace(/<BR>/g,'\r\n');
	}
	else if(type=='txt'){
		txt=txt.replace(/&amp;/g,'&');
		txt=txt.replace(/&gt;/g,'>');
		txt=txt.replace(/&lt;/g,'<');
		txt=txt.replace(/&nbsp;/g,' ');
		txt=txt.replace(/<BR>/g,'\r\n');
		txt=txt.replace(/<P>/g,'');
		returnvalue=txt.replace(/<\/P>/g,'');
	}
	return  returnvalue;

}

// ÀÌ¹ÌÁö Ã·ºÎ
function boardIU()
{
	window.open('?view=imgUpload','','width=350,height=150');
}

function insertPreImgEditor(url,more)
{
	try{
		var sText = HTMLEDITOR.document.selection.createRange();
		if (sText.parentElement().all.HTMLEDITOR == null){
		sText.pasteHTML("<img src='" + url + "' "+more+"><BR>");
				sText.select();
			}
		else{
			HTMLEDITOR.document.body.innerHTML =  "<img src='" + url + "' "+more+"><BR>" + HTMLEDITOR.document.body.innerHTML
			HTMLEDITOR.focus();
		}
	}
	catch(e){
		alert('ÀÌ¹ÌÁö¸¦ »ðÀÔÇÒ ¼ö ¾ø½À´Ï´Ù')
	}
}

function getAbsoluteTop(obj)
{
	var curNode = obj;
	var topVal = 0;

	while(curNode.tagName != 'BODY')
	{
		topVal += curNode.offsetTop - curNode.scrollTop;
		curNode = curNode.offsetParent;
	}

	return topVal;
}

function getAbsoluteLeft(obj)
{
	var curNode = obj;
	var leftVal = curNode.offsetWidth;

	while(curNode.tagName != 'BODY')
	{
		leftVal += curNode.offsetLeft;
		curNode = curNode.offsetParent;
	}

	return leftVal;
}


//ÆÄÀÏµî·Ï ÆÄÀÏÅÇ Ãß°¡»ý¼º ÇÔ¼ö
var FileArray = new Array();
var fileCnt = 0;
function AddFile()
{
	if (fileCnt < 10)
	{
		FileArray[FileArray.length] = FileArray.length;
		var index = FileArray[FileArray.length - 1];
		var tobj = document.all["tblAttFiles"];
		var objRow = tobj.insertRow();
		var objCell = objRow.insertCell();
		objCell.innerHTML = "<input type='file' name=AttFile"+index+" style='WIDTH: 90%; HEIGHT: 18px; border:1px solid black;border-color:#B9B9BA;font-size:9pt; background-color:#FFFFFF' value='' border='0' class='board_read_text4'> <input type='button' name='rb_AttFile_" + index + "' onClick='removeFile(this);' value='Ãë¼Ò' style='HEIGHT: 18px; border:1px solid black;border-color:#B9B9BA;font-size:9pt; background-color:#FFFFFF' border='0' class='board_read_text4'>";

		document.recalc(); //javascriptÈ£Ãâ½Ã ¸ðµç ÀÛ¾÷À» ¿Ï·áÈÄ È­¸é¿¡ º¸¿©ÁÖ´Â ÇÔ¼ö
		fileCnt++;
	}
	else
	{
		alert('10°³±îÁö ÀÔ´Ï´Ù.');
		return false;
	}

}

//ÆÄÀÏµî·Ï ÆÄÀÏÅÇ Á¦°Å ÇÔ¼ö
function removeFile(tobj)
{
	var objName = tobj.name.substring(11);
	if (fileCnt > 1)
	{
		document.all["tblAttFiles"].rows[objName].deleteCell();
		fileCnt--;
	}
	else
	{
		alert('ÇÑ°³°¡ ±âº»ÀÔ´Ï´Ù.');
		return false;
	}
}
