var onEditMode = true;
var Selection = null;
var fontSize = 12;


function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_showHideLayers() {
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function encrypt(thetext) {
	output = new string;
	temp = new array();
	temp2 = new array();
	textsize = thetext.length;
	for (i = 0; i < textsize; i++) {
		rnd = math.round(math.random() * 122) + 68;
		temp[i] = thetext.charcodeat(i) + rnd;
		temp2[i] = rnd;
	}
	for (i = 0; i < textsize; i++) {
		output += string.fromcharcode(temp[i], temp2[i]);
	}
	return output;
}


function decrypt(thetext) {
	output = new string;
	temp = new array();
	temp2 = new array();
	textsize = thetext.length;
	for (i = 0; i < textsize; i++) {
		temp[i] = thetext.charcodeat(i);
		temp2[i] = thetext.charcodeat(i + 1);
	}
	for (i = 0; i < textsize; i = i+2) {
		output += string.fromcharcode(temp[i] - temp2[i]);
	}
	return output;
}


function GoTop() {
	window.scrollTo(0,0);
}


function FullWin(objform,tpnum,url) {
	objform.pnum.value = tpnum;
	window.open('','fullWin','Fullscreen=yes,type=Fullwindow,scrollbars=no');
	objform.target="fullWin"
	objform.action= url;
	objform.submit();
}


function setcookie (name, value, expires) {
	var cookievalue = encrypt(value);
	var settingcookie = name + "=" + escape (cookievalue) + "; path=/; expires=";
	if ( expires != "" ) {
		settingcookie += expires.togmtstring();
	}
	document.cookie = settingcookie;
}


function getcookie(name) {
	var nameofcookie = name + "=";
	var x = 0;
	while ( x <= document.cookie.length ) {
		var y = (x+nameofcookie.length);
		if ( document.cookie.substring( x, y ) == nameofcookie ) {
			if ( (endofcookie=document.cookie.indexof( ";", y )) == -1 )
				endofcookie = document.cookie.length;
				return decrypt(unescape( document.cookie.substring( y, endofcookie ) ));
		}
		x = document.cookie.indexof( " ", x ) + 1;
		if ( x == 0 )
			break;
	}
	return "";
}


function Win(url,name,w,h)
{
	var str = "height=" + h + ",innerHeight=" + h;
	str += ",width=" + w + ",innerWidth=" + w;

	if (window.screen) {
		var ah = screen.availHeight - 20;
		var aw = screen.availWidth - 10;

		var xc = (aw - w) / 2;
		var yc = (ah - h) / 2;

		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
	}
	window.open(url, name, str);
}

function Win2(url,name,w,h){
	var ah = screen.availHeight - 20;
	var aw = screen.availWidth - 10;
	var xc = (aw - w) / 2;
	var yc = (ah - h) / 2;

	window.open(url,name,"status=no,toolbar=no,resizable=no,scrollbars=yes, menubar=no,width="+w+",height="+h+",top="+yc+",left="+xc);
}

function modelWin(url,name,w,h) {
        var dialog = window.showModelessDialog(url, name, "dialogHeight:200px;dialogWidth:200px;center:yes;status:no;scroll:yes;unadorned:yes;help:no");
}

function reply() {
	document.form.submit();
}


function refreshImage(temp){}


function checkSearchValue()  {
	if (document.form.searchString.value=="") {
		alert("°Ë»ö¾î¸¦ ÀÔ·ÂÇÏ¼¼¿ä")
		document.form.searchString.focus();
		return;
	}
	document.form.submit();
}


function checkBasicForm() {
	if(onEditMode==true){
		document.form.content.value = postContent();
	}

	if(document.form.name.value=="") {
		alert("ÀÌ¸§À» ÀÔ·ÂÇÏ¼¼¿ä");
		document.form.name.focus();
	return;
	}
	if(document.form.email.value=="") {
		alert("ÀÌ¸ÞÀÏÀ» ÀÔ·ÂÇÏ¼¼¿ä");
		document.form.email.focus();
	return;
	}
	if(document.form.title.value=="") {
		alert("Á¦¸ñÀ» ÀÔ·ÂÇÏ¼¼¿ä");
		document.form.title.focus();
	return;
	}
	if(document.form.content.value=="") {
		alert("³»¿ëÀ» ÀÔ·ÂÇÏ¼¼¿ä");
	return;
	}
	if(document.form.pwd.value=="") {
		alert("ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇÏ¼¼¿ä");
		document.form.pwd.focus();
	return;
	}
	if(document.form.spam.value!="º¹Áö") {
		alert("º¹Áö¹æÁö¸¦ À§ÇØ 'º¹Áö' ÀÌ¶ó°í ÀÔ·ÂÇØ ÁÖ¼¼¿ä.");
		document.form.spam.focus();
	return;
	}
	return true;
}

function showImage(imageobj) { 
	var imgwin = window.open("",'imageWin','scrollbars=no,status=no,toolbar=no,resizable=1,location=no,menu=no,width=10,height=10');
	imgwin.focus(); 
	imgwin.document.open(); 
	imgwin.document.write("<html>\n"); 
	imgwin.document.write("<head>\n"); 
	imgwin.document.write("<title>»çÁø º¸±â</title>\n"); 
	imgwin.document.write("<sc"+"ript>\n"); 
	imgwin.document.write("function resize() {\n"); 
	imgwin.document.write("pic = document.il;\n"); 
	imgwin.document.write("if (eval(pic).height) { var name = navigator.appName\n"); 
	imgwin.document.write(" if (name == 'Microsoft Internet Explorer') { myHeight = eval(pic).height + 40; myWidth = eval(pic).width + 12;\n"); 
	imgwin.document.write(" } else { myHeight = eval(pic).height + 9; myWidth = eval(pic).width; }\n"); 
	imgwin.document.write(" clearTimeout();\n"); 
	imgwin.document.write(" var height = screen.height;\n"); 
	imgwin.document.write(" var width = screen.width;\n"); 
	imgwin.document.write(" var leftpos = width / 2 - myWidth / 2;\n"); 
	imgwin.document.write(" var toppos = height / 2 - myHeight / 2; \n"); 
	imgwin.document.write(" self.moveTo(leftpos, toppos);\n"); 
	imgwin.document.write(" self.resizeTo(myWidth, myHeight);\n"); 
	imgwin.document.write("}else setTimeOut(resize(), 100);}\n"); 
	imgwin.document.write("</sc"+"ript>\n"); 
	imgwin.document.write("</head>\n"); 
	imgwin.document.write('<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgcolor="#FFFFFF">\n'); 
	imgwin.document.write("<a href=# onclick=window.close()><img border=0 src="+imageobj+" xwidth=100 xheight=100 name=il onload='resize();'></a>\n"); 
	imgwin.document.write("</body>\n"); 
	imgwin.document.close(); 
}

function scaleFont(val) {
	var contentArea, lineHeight;
	contentArea = document.getElementById("contentArea");
	if (val > 0) {
			if (fontSize <= 18) {
			fontSize = fontSize + val;
			lineHeight = fontSize+Math.round(1.2*fontSize);
			contentArea.style.fontSize = fontSize + "px"; } }
	else {
		if (fontSize > 11) {
			fontSize = fontSize + val;
			lineHeight = fontSize+Math.round(1.2*fontSize);
			contentArea.style.fontSize = fontSize + "px";
		}
	}
}

function sendComment() {
	if(document.commentForm.name2.value==""){
		alert("ÀÛ¼ºÇÏ½ÅºÐÀÇ ÀÌ¸§À» ÀÔ·ÂÇØÁÖ¼¼¿ä");
		document.commentForm.name2.focus();
		return;
	}
	if(document.commentForm.ctxt2.value==""){
		alert("µ¡±Û ³»¿ëÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä");
		document.commentForm.ctxt2.focus();
		return;
	}
		if(document.commentForm.pw2.value==""){
		alert("ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä");
		document.commentForm.pw2.focus();
		return;
	}
	document.commentForm.submit();
}


function sendBasicForm() {
	if (checkBasicForm())
	{
//		decodeContent(EDITOR.document.body.innerHTML);
		document.form.submit();
	}
}


function sendFileForm() {
	if(checkBasicForm())
	{
		if(document.form.attachFilePath.value=="")  {
			document.form.submit();
		}
		else  
		{ 
			document.form.submit();
			strAppVersion = navigator.appVersion; 

			if (strAppVersion.indexOf('MSIE')!=-1 &&
				strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4) {
				winstyle = "dialogWidth=385px; dialogHeight:150px; center:yes";
				window.showModelessDialog("show_progress.asp?nav=ie", null, winstyle);
			}
			else {
				winpos = "left=" + ((window.screen.width-380)/2)+",top=" +
				((window.screen.height-110)/2); 
				winstyle = "width=380,height=110,status=no,toolbar=no,menubar=no," + 
				"location=no,resizable=no,scrollbars=no,copyhistory=no," + winpos; 
				window.open("show_progress.asp",null,winstyle); 
			}
			return true; 
		}
	}
}


function checkNull(oField, strMsg, blnFocus)
{
	var isNotNull = false;
	var fieldType = null;
	var mainField;
	
	if (typeof(oField.type) == 'undefined' && typeof(oField.length) != 'undefined')
		mainField = oField[0];
	else
		mainField = oField;
	fieldType = mainField.type;


	switch (fieldType)
	{
		case "text" :
		case "password" :
		case "file" :
			isNotNull = hasValue(oField);
			break;
		case "textarea" :
			isNotNull = hasEditerValue(oField);
			break;
		case "checkbox" :
		case "radio" :		
			isNotNull = isChecked(oField);
			break;		
		case "select-one" :
		case "select-multiple" :
			isNotNull = isNotSelected(oField);
			break;
	}
	
	if (!isNotNull)
	{
		alert(strMsg);
		if (blnFocus == true)
		{
			mainField.focus();
		}
	}
	
	return isNotNull;
}


function hasValue(oField)
{
	if (oField.value.replace(/(^\s*)|(\s*$)/g, "") == "")
		return false;
	else
		return true;
}

function hasEditerValue(oField)
{
	if (oField.value.replace(/&nbsp;/g, " ").replace(/(^\s*)|(\s*$)/g, "") == "")
		return false;
	else
		return true;
}

function isChecked(oField)
{	
	var checked = false;

	if (typeof(oField.length) != 'undefined')
	{
		for (var i=0; i<oField.length; i++)
			if (oField[i].checked)
				checked = true;
	}
	else
	{
		checked = oField.checked;
	}
	
	return checked;
}

function isNotSelected(oField)
{
	if (oField.selectedIndex == -1)
	{
		return false;
	}
	else
	{
		if (oField.value == -1)
			return false;
		else
			return true;
	}
}


function decodeContent(str)
{
	return str.replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&amp;/gi,"&");
}


function switchMode(flag)
{
	var f = document.form;
	if(flag=="Off"){								// Editor
		f.content.value = postContent();
		EDITOR.document.designMode="Off";
		onEditMode = false;
		document.all.contentBox.style.display = "";
		document.all.EDITOR.style.display = "none";
	}else{											// HTML Source
		initEditor(document.form.content.value);
		onEditMode = true;
		document.all.contentBox.style.display = "none";
		document.all.EDITOR.style.display = "";
	}
}


function initEditor(arg) 
{

	if(!arg){
		var source = "<html><head><style>.Background {background-color:#FFFFFF} P {margin-top:2px;margin-bottom:2px;} table {border:1 solid C6C3C6}</style><script>function refreshImage(target_img){}</script></head><body></body></html>";
	}else{
		var source = "<html><head><style>.Background {background-color:#FFFFFF} P {margin-top:2px;margin-bottom:2px;} table {border:1 solid C6C3C6}</style><script>function refreshImage(target_img){}</script></head><body>"+arg+"</body></html>";
	}
	EDITOR.document.designMode="on";
	EDITOR.document.open("text/html");
	EDITOR.document.write(source);
	EDITOR.document.close();
	
	EDITOR.document.body.style.fontSize = "9pt";
	EDITOR.document.body.style.fontFamily = "µ¸¿ò";
	EDITOR.document.body.contentEditable="True";
}


function setFont(oSelect)
{
	if (oSelect.options[oSelect.selectedIndex].value != '')
    	EDITOR.document.execCommand("FontName", null, oSelect.options[oSelect.selectedIndex].value);
			       
	oSelect.selectedIndex = 0;
}


function postContent()
{
	var txt="";
	txt=EDITOR.document.body.innerHTML;
	for(var i=0;i<txt.length;i++)
	{
		txt=txt.replace("</P>","<br>");
		txt=txt.replace("<P>","");
	}
	EDITOR.document.body.innerHTML=txt;

	HTMLSOURCE = EDITOR.document.body.innerHTML;
	return HTMLSOURCE;
}


function setFontSize(oSelect)
{
	if (oSelect.options[oSelect.selectedIndex].value != '')
    	EDITOR.document.execCommand("FontSize", null, oSelect.options[oSelect.selectedIndex].value);
	oSelect.selectedIndex = 0;
}

function Point(iX, iY){
	this.x = iX;
	this.y = iY;
}

function fGetXY(aTag){
  var oTmp = aTag;
  var pt = new Point(0,0);
  do {
  	pt.x += oTmp.offsetLeft;
  	pt.y += oTmp.offsetTop;
  	oTmp = oTmp.offsetParent;
  } while(oTmp.tagName!="BODY");
  return pt;
}


function editContent(act,obj)
{
	if (!onEditMode)
	{
		alert("HTML ¸ðµå¿¡¼­´Â ¿¡µðÅÍ¸¦ »ç¿ëÇÒ¼ö ¾ø½À´Ï´Ù.");
		return;
	}						

	Selection = EDITOR.document.selection.createRange();
	if (Selection != null) Selection.select();
	
	switch (act)
	{
		case "Bold" :
		case "Italic" :
		case "Underline" :
		case "StrikeThrough" :		
		case "JustifyLeft" :
		case "JustifyCenter" :
		case "JustifyRight" :

		case "InsertOrderedList" :
		case "InsertUnOrderedList" :
		case "Indent" :
		case "Outdent" :

			EDITOR.document.execCommand(act);
			break;
		case "fontcolor" :
			setFontColor(obj);
			break;
		case "bgcolor" :		
			setBGColor("BackColor",obj);
			break;
		case "link" :
			showLinkBox();
	}
}

function showLinkBox()
{
	EDITOR.focus();
	EDITOR.document.execCommand("CreateLink",true);
}

function setTable()
{
	Win("../include/insTable.asp", "insTable",375,275);
}

function setFontColor(color)
{
    if (Selection != null) 
		Selection.select();
    var newcolor = showModalDialog("../include/colorwindow.asp", 0, "resizable: no; help: no; status: no; scroll: no;");
    if (newcolor != null) { EDITOR.document.execCommand("ForeColor", false, "#"+newcolor); }
}

function setBGColor(color, ftcolor)
{
	if (Selection != null) 
		Selection.select();
    var newcolor = showModalDialog("../include/colorwindow.asp", 0, "resizable: no; help: no; status: no; scroll: no;");
    if (newcolor != null) { EDITOR.document.execCommand("BackColor", null, "#"+newcolor); }
	if (ftcolor != "")
          EDITOR.document.execCommand("ForeColor", null, ftcolor);
}

function setBackColor(color)
{
	EDITOR.document.body.style.backgroundColor = color;
}

function popImage()
{
	Win("../include/AttachImage.asp", "addimage", 330, 280);
}

function popFile()
{
	Win("../include/AttachFile.asp", "addfile", 330, 170);
}

function popLink()
{
	Win("../include/AttachLink.asp", "addlink", 330, 210);
}

function addTable(attachTag)
{
	var sText = EDITOR.document.selection.createRange();
	
	if (sText.parentElement().all.EDITOR == 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
	{
		EDITOR.document.body.innerHTML = "<P>" + attachTag + EDITOR.document.body.innerHTML + "</P>";
	}
	EDITOR.focus();
}

function addLink(attachURL, comment, alignPos, attachtype)
{
	try
	{
		var attachTag = getAttachTag(attachURL, comment, alignPos, attachtype);

		if (alignPos=='top')
		{
			EDITOR.document.body.innerHTML =  attachTag+ "<br>" + EDITOR.document.body.innerHTML;		
		}
		else if (alignPos=='bottom')
		{
			EDITOR.document.body.innerHTML =  EDITOR.document.body.innerHTML + "<br>" + attachTag ;
		}
		else
		{
			var sText = EDITOR.document.selection.createRange();
			
			if (sText.parentElement().all.EDITOR == 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
	   		{
				EDITOR.document.body.innerHTML = "<P>" + attachTag + EDITOR.document.body.innerHTML + "</P>";
			}
		}
		if(onEditMode==false)
				document.form.content.value = postContent();
	}
	catch(e)
	{
		EDITOR.document.body.innerHTML = "<P>" + attachTag + EDITOR.document.body.innerHTML + "</P>";
	}
	
	EDITOR.focus();
}

function getAttachTag(attachURL, comment, alignPos, attachtype)
{
	var attachTag = "";
		
	switch (attachtype)
	{
		case 1:			
			attachTag = "<a href='down.asp?file_name='" + attachURL+ "' target=_blank style='text-decoration:underline;color:blue'>"+comment+"</a><br>";
			break;
		case 2:
			if (alignPos == 'top' || alignPos == 'bottom'){
				attachTag = "<center><img src='../upload/pictures/" + attachURL + "' align=" + alignPos + " border=0 onLoad='JavaScript:refreshImage(this);' alt='" + comment + "></a><br></center>";
			}else{
				attachTag = "<img src='../upload/pictures/" + attachURL + "' align=" + alignPos + " border=0 onLoad='JavaScript:refreshImage(this);' alt='" + comment + "'></a><br>";
			}
			break;
		case 3:
			attachTag = "<embed autostart=\"false\" src=\"" + attachURL + "\" ><br>" + comment;
			break;
	}
	return attachTag;
}


function decodeContent(str)
{
	return str.replace(/&lt;/gi,"<").replace(/&gt;/gi,">").replace(/&amp;/gi,"&");
}


function setContent()
{
	EDITOR.document.body.innerHTML = document.all.DIVcontent.innerHTML;
}


function checkNull(oField, strMsg, blnFocus)
{
	var isNotNull = false;
	var fieldType = null;
	var mainField;
	
	if (typeof(oField.type) == 'undefined' && typeof(oField.length) != 'undefined')
		mainField = oField[0];
	else
		mainField = oField;
	fieldType = mainField.type;


	switch (fieldType)
	{
		case "text" :
		case "password" :
		case "file" :
			isNotNull = hasValue(oField);
			break;
		case "textarea" :
			isNotNull = hasEditerValue(oField);
			break;
		case "checkbox" :
		case "radio" :		
			isNotNull = isChecked(oField);
			break;		
		case "select-one" :
		case "select-multiple" :
			isNotNull = isNotSelected(oField);
			break;
	}
	
	if (!isNotNull)
	{
		alert(strMsg);
		if (blnFocus == true)
		{
			mainField.focus();
		}
	}
	
	return isNotNull;
}


function isChecked(oField)
{	
	var checked = false;

	if (typeof(oField.length) != 'undefined')
	{
		for (var i=0; i<oField.length; i++)
			if (oField[i].checked)
				checked = true;
	}
	else
	{
		checked = oField.checked;
	}
	
	return checked;
}


function isNotSelected(oField)
{
	if (oField.selectedIndex == -1)
	{
		return false;
	}
	else
	{
		if (oField.value == -1)
			return false;
		else
			return true;
	}
}

 
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='inp' value='http://' align='absmiddle'>"
	+ "<a href='javascript:setLink(document.all.linkbox.value);'><img src='../image/btn_ok01.gif' width='32' height='19' align='absmiddle' border='0'></a>"
	+ "</td>"
	+ "</tr>"
	+ "</table>"
	+ "</div>");