
function Show()
{
	var form = document.forms.write;
	form.mf_content_type[0].checked=true;
	if (form.mf_content_type_now.value == 'P')
	{
		document.getElementById('eEditor').style.display	= '' ;
		document.getElementById('eNoEditor').style.display	= 'none' ;

		if (nm_webeditor == 'GM')
		{
			gmFrame.document.open("text/html");
			gmFrame.document.write(str);
			gmFrame.document.write(form.mf_body_text.value.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>\n"));
			gmFrame.document.close();
			gmFrame.focus();
		} else if (nm_webeditor == 'OPEN')
		{
			document.getElementById('wysiwygmf_body_html').contentWindow.document.body.innerHTML = form.mf_body_text.value.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>\n");
		} else if (nm_webeditor == 'TINYMCE')
		{
			tinyMCE.execCommand('mceSetContent',false,form.mf_body_text.value.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>\n"));
		} else
		{
			var oEditor = FCKeditorAPI.GetInstance( 'mf_body_html' ) ;
			oEditor.SetHTML(form.mf_body_text.value.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br>\n"));

			// This is a hack for Gecko... it stops editing when the editor is hidden.
			if ( !document.all )
			{
				if (  oEditor.EditMode == FCK_EDITMODE_WYSIWYG )
				{
					oEditor.SwitchEditMode() ;
					oEditor.SwitchEditMode() ;
				}
			}
		}
		form.mf_content_type_now.value = 'H';
	}
}

function Hide()
{
	var form = document.forms.write;
	form.mf_content_type[1].checked=true;
	if (form.mf_content_type_now.value == 'H')
	{
		if (nm_webeditor == 'GM')
		{
			var mf_body_html_value = SubmitHTML().replace(/<br[ ]*[^>]*>/gi, "\n").replace(/<[^<>]*>/gi, "").replace(/\&nbsp\;/gi, " ").replace(/\&lt\;/gi, "<").replace(/\&gt\;/gi, ">");
		} else if (nm_webeditor == 'OPEN')
		{
			var mf_body_html_value = document.getElementById('wysiwygmf_body_html').contentWindow.document.body.innerHTML.replace(/<br[ ]*[^>]*>/gi, "\n").replace(/<[^<>]*>/gi, "").replace(/\&nbsp\;/gi, " ").replace(/\&lt\;/gi, "<").replace(/\&gt\;/gi, ">");
		} else if (nm_webeditor == 'TINYMCE')
		{
			var mf_body_html_value = tinyMCE.get('mf_body_html').getContent().replace(/<br[ ]*[^>]*>/gi, "\n").replace(/<[^<>]*>/gi, "").replace(/\&nbsp\;/gi, " ").replace(/\&lt\;/gi, "<").replace(/\&gt\;/gi, ">");
		} else
		{
			//alert(mf_body_html___Frame.window.frames['eEditorArea'].document.body.innerHTML);
			var oEditor = FCKeditorAPI.GetInstance( 'mf_body_html' ) ;
			var mf_body_html_value = oEditor.GetHTML(oEditor.Config['FormatOutput']).replace(/<br[ ]*[^>]*>/gi, "\n").replace(/\&nbsp\;/gi, " ").replace(/<[^<>]*>/gi, "").replace(/\&lt\;/gi, "<").replace(/\&gt\;/gi, ">");
		}		

		if (mf_body_html_value != "" && !confirm("[HTML]에서 [텍스트]모드로 변경할 경우\n\n적용된 태그가 모두 사라집니다.\n\n변경하시겠습니까?"))
		{
			form.mf_content_type[0].checked=true;
			return;
		}

		form.mf_body_text.value = mf_body_html_value;

		document.getElementById('eEditor').style.display	= 'none' ;
		document.getElementById('eNoEditor').style.display	= '' ;
		form.mf_content_type_now.value = 'P';
		ContentTypeCheck();
	}
}

function ContentTypeCheck()
{
	var form = document.forms.write;
	if ((form.is_rec_chk.checked == true)&&(form.mf_content_type[1].checked == true))
	{
		alert("본문 형식이 '텍스트'일때는 수신확인기능을 사용할 수 없습니다.");
		form.is_rec_chk.checked = false;
	}
}

function uploadExecute()
{
	var form = document.forms.write_upload;

	if (form.mf_attach_name.value == "")
	{
		alert("첨부할 파일을 먼저 선택하세요.");
		form.mf_attach_name.focus();
	} else
	{
		form.action = nmpath + "/mail_write_upload.php";
		form.method = "post";
		form.enctype = "multipart/form-data";		// [2011-01-07] 사파리 호환
		form.submit();
		form.mf_attach_name.disabled = true;
	}
}

function uploadSuccess(file_name, upload_tmp_name, files_size_h, files_size, file_size_h, file_size, err_msg)
{
	var form = document.forms.write_upload;
	var mf_attach_names = form_findElement(form, "mf_attach_names[]");

	file_name = file_name.replace("\\","");

	if (err_msg == null || err_msg == "")
	{
		var option = new Option(file_name + " (" + file_size_h + ")", file_name + "|" + upload_tmp_name + "|" + file_size);
		mf_attach_names.options[mf_attach_names.length] = option;
		form.mf_attachs_size.value = files_size;
		form.mf_attachs_size_h.value = files_size_h;
		// 크기자동조절
		if (mf_attach_names.length > mf_attach_names.size  )
		{
			mf_attach_names.size = mf_attach_names.length;
		}	// if()
	} else
	{
		alert(err_msg);
	}
	form.mf_attach_name.disabled = false;
}

function uploadDel()
{
	var form = document.forms.write_upload;
	var mf_attach_names = form_findElement(form, "mf_attach_names[]");

	var sel_cnt = 0;
	for(i=0; i < mf_attach_names.length; i++)
	{
		if (mf_attach_names[i].selected == true)
		{
			sel_cnt++;
			break;
		}
	}

	if (sel_cnt < 1)
	{
		alert("삭제할 첨부파일을 선택하세요");
	} else
	{
		form.btn_del.disabled = true;
		form.action = nmpath + "/mail_write_upload_del.php";
		form.method = "get";
		form.submit();
	}
}

function uploadDelSuccess(file_name, upload_tmp_name, files_size_h, files_size, file_size, err_msg)
{
	var form = document.forms.write_upload;
	var mf_attach_names = form_findElement(form, "mf_attach_names[]");

	file_name = file_name.replace("\\","");

	for(i=0; i < mf_attach_names.length ; i++)
	{
		if (mf_attach_names[i].value == file_name + "|" + upload_tmp_name + "|" + file_size)
		{
			mf_attach_names[i] = null;
			break;
		}
	}

	form.mf_attachs_size.value = files_size;
	form.mf_attachs_size_h.value = files_size_h;

	if (err_msg != null && err_msg != "")
	{
		alert(err_msg);
	}
	form.btn_del.disabled = false;
}

function addCc()
{
	// [2007-06-05] basic skin 에서 전체답장시 에러 방지
	if (document.all.show_cc)
	{
		document.all.show_cc.style.display = "none";
		document.all.show_cc1.style.display = "";
		document.all.show_cc2.style.display = "";
	}
}

function addBcc()
{
	// [2007-06-05] basic skin 에서 전체답장시 에러 방지
	if (document.all.show_bcc)
	{
		document.all.show_bcc.style.display = "none";
		document.all.show_bcc1.style.display = "";
		document.all.show_bcc2.style.display = "";
	}
}

function mail_write_check(form)
{
	// 구분자 통일.
	form.mf_to.value = trim(form.mf_to.value.replace(/\;/g, ","));
	form.mf_to.value = form.mf_to.value.replace(/\r\n/g, ",");

	form.mf_cc.value = trim(form.mf_cc.value.replace(/\;/g, ","));
	form.mf_cc.value = form.mf_cc.value.replace(/\r\n/g, ",");

	form.mf_bcc.value = trim(form.mf_bcc.value.replace(/\;/g, ","));
	form.mf_bcc.value = form.mf_bcc.value.replace(/\r\n/g, ",");

	if (form_chk_value(form.mf_to, "받는사람") == false) return false;
	if (form_chk_emails_confirm(form.mf_to, "받는사람") == false) return false;
	if (form_chk_emails_confirm(form.mf_cc, "참조") == false) return false;
	if (form_chk_emails_confirm(form.mf_bcc, "숨은참조") == false) return false;
	//if (form_chk_value(form.mf_subject, "제목") == false) return false;

	if (form.is_mailing && form.is_mailing.checked == false && form.mf_to.value.length >= 256)
	{
		if (confirm("안내)\n\n받는사람에 입력된 주소들은 메일을 받는 사람들에게 모두 보여지게 됩니다.\n\n불필요한 주소 노출을 방지 하기 위해 [개인별로 보내기]옵션을 사용하시겠습니까?"))
		{
			form.is_mailing.checked = true;
		}
	}

	if (form.mf_subject.value == "")
	{
		if (confirm("제목을 입력하세요.\n\n제목을 입력하지 않고 보내시려면 [취소]를 누르시기 바랍니다."))
			return false;
	}

	if (document.forms.write.mail_upload_type.value == '0')
	{
		// uploading check
		// [2010-07-09] iPhone, iPad file upload disabled
		//if (document.forms.write_upload.mf_attach_name.disabled == true)
		if (/iPhone|iPad/.test(window.navigator.userAgent) == false && document.forms.write_upload.mf_attach_name.disabled == true)
		{
			alert("파일을 첨부중입니다.\n\n첨부가 완료된뒤 메일을 보낼 수 있습니다.");
			return false;
		}

		// upload file data
		var mf_attach_names_upload = form_findElement(document.forms.write_upload, "mf_attach_names[]");
		form.mf_attach_names.value = '';
		for(i = 0 ; i < mf_attach_names_upload.length ; i++)
		{
			form.mf_attach_names.value = form.mf_attach_names.value + "||" + mf_attach_names_upload[i].value;
		}
	} else if (document.forms.write.mail_upload_type.value == '1')
	{
		if (nmail_updown_cp_status != false)
		{
			if (upload() == false) return false;
		}
	}
}

function mailSend()
{
	input_guide(false);

	var form = document.forms.write;
	if (mail_write_check(form) == false) return;

	if (nm_webeditor == 'GM')
	{
		form.mf_body_html.value = SubmitHTML();
	} else if (nm_webeditor == 'OPEN')
	{
		form.mf_body_html.value = document.getElementById('wysiwygmf_body_html').contentWindow.document.body.innerHTML;
	}

	form.submit();
}

function mail_write_preview()
{
	input_guide(false);

	var form = document.forms.write;
	//if (mail_write_check(form) == false) return;

	if (nm_webeditor == 'GM')
	{
		form.mf_body_html.value = SubmitHTML();
	} else if (nm_webeditor == 'OPEN')
	{
		form.mf_body_html.value = document.getElementById('wysiwygmf_body_html').contentWindow.document.body.innerHTML;
	}		

	window_open('', '_mail_write_preview', 750, 550);

	form.target = '_mail_write_preview';
	form.action = nmpath + '/mail_write_preview.php';
	form.submit();
	form.target = '';
	form.action = nmpath + '/' + nm_php_self + '?'+get_qs;
}

var mail_write_draft_save_length = 0;
function mail_write_draft_save(isauto)
{
	// [2006-11-21] uploading check
	// [2010-07-09] iPhone, iPad file upload disabled
	//if (document.forms.write.mail_upload_type.value == '0' && document.forms.write_upload.mf_attach_name.disabled == true)
	if (/iPhone|iPad/.test(window.navigator.userAgent) == false && document.forms.write.mail_upload_type.value == '0' && document.forms.write_upload.mf_attach_name.disabled == true)
	{
		return false;
	}

	input_guide(false);

	var form = document.forms.write;
	//if (mail_write_check(form) == false) return;

	var body_length = 0;
	if (form.mf_content_type_now.value == 'H')
	{
		var html = '';
		if (nm_webeditor == 'GM')
		{
			form.mf_body_html.value = SubmitHTML();
			html = trim(form.mf_body_html.value);
		} else if (nm_webeditor == 'OPEN')
		{
			form.mf_body_html.value = document.getElementById('wysiwygmf_body_html').contentWindow.document.body.innerHTML;
			html = trim(form.mf_body_html.value);
		} else if (nm_webeditor == 'TINYMCE')
		{
			html = trim(tinyMCE.get('mf_body_html').getContent());
		} else
		{
			var oEditor = FCKeditorAPI.GetInstance( 'mf_body_html' ) ;
			html = trim(oEditor.GetHTML(oEditor.Config['FormatOutput']).replace(/<br[ ]*[^>]*>/gi, ""));
		}
		body_length = html.length;
	} else 
	{
		body_length = trim(form.mf_body_text.value).length;
	}

	// 자동 임시저장시 본문 내용이 입력되었고 내용이 변했을때만 저장함.
	// [2007-06-05] 파이어폭스에서 <br> 이 기본으로 적용되므로 10자 이상 입력시에만 자동 저장되도록 함.
	if (isauto >= 1)		// [2008-06-09] 0, 1, 2(ActiveX 모드 전환)
	{
		if (body_length > 10 && body_length != mail_write_draft_save_length)
		{
			mail_write_draft_save_length = body_length;
		} else
		{
			return false;
		}
	}

	// [2007-07-06] 임시보관함에 저장할때도 첨부파일을 함께 저장하도록 함. - upload file data
	if (document.forms.write.mail_upload_type.value == '0')
	{
		var mf_attach_names_upload = form_findElement(document.forms.write_upload, "mf_attach_names[]");
		form.mf_attach_names.value = '';
		for(i = 0 ; i < mf_attach_names_upload.length ; i++)
		{
			form.mf_attach_names.value = form.mf_attach_names.value + "||" + mf_attach_names_upload[i].value;
		}
	} else if (document.forms.write.mail_upload_type.value == '1')
	{
		if (nmail_updown_cp_status != false)
		{
			var fileinfo = frm.fUpload.GetUpFileInfo();
			document.forms.write.mf_attach_names.value = fileinfo.replace(/\|\:\|/g, '||').replace(/\//g, '|');
		}
	}

	form.target = '_nm_hidden_frame';
	form.action = nmpath + '/' + nm_php_self + '?is_draft_save=1&isauto=' + isauto + '&'+get_qs;
	form.submit();
	form.target = '';
	form.action = nmpath + '/' + nm_php_self + '?'+get_qs;
}

function addrSelect(type)
{
	//window_open(nmpath + '/addr_select.php?kind=' + kind, '_nm_addr_select', 400, 550);
	window_open(nmpath + '/addr_select.php?type=' + type, '_nm_addr_select', 750, 550);
}

function input_guide(init)
{
	var nm_input_guide = " 참고) 한명 이상을 입력할때는 콤마(,)나 엔터로 구분하시면 됩니다.";
	var obj = document.forms.write.mf_to;
	if (init && obj.value == "")
	{
		obj.value = nm_input_guide;
	} else if (obj.value == nm_input_guide)
	{
		obj.value = "";
	}
}	// function

function mail_write_me(email)
{
	f = document.forms.write;
	f.mf_to.value = email;
	f.is_mail_save.checked = false;
	f.is_rec_chk.checked = false;
	// [2011-03-11] 서명 위치 변경
	//f.sg_no[0].selected = true;
}	// function()

// [2007-06-05] 웹에디터 입력높이 조절기능.
function mf_body_resize(type)
{
	var form = document.forms.write;
	if (form.mf_content_type_now.value == 'H')
	{
		if (nm_webeditor == 'GM')
		{
			document.getElementById('gmEditor').height = mf_body_resize_act2(document.getElementById('gmEditor').height, type);
		} else if (nm_webeditor == 'OPEN')
		{
			mf_body_resize_act(document.getElementById('wysiwygmf_body_html'), type);
		} else
		{
			document.getElementById('mf_body_html___Frame').height = mf_body_resize_act2(document.getElementById('mf_body_html___Frame').height, type);
		}
	} else 
	{
		mf_body_resize_act(form.mf_body_text, type);
	}		
}	// function()

function mf_body_resize_act(obj, type)
{
	var h = parseInt(obj.style.height, 10);
	if (type == '+')
	{
		obj.style.height = (h + 100) + 'px';
	} else 
	{
		if (h > 200)
			obj.style.height = (h - 100) + 'px';
	}	// if()		
}	// function()

function mf_body_resize_act2(obj, type)
{
	var h = parseInt(obj, 10);
	if (type == '+')
	{
		obj = (h + 100);
	} else 
	{
		if (h > 200)
			obj = (h - 100);
	}	// if()		
	return obj;
}	// function()



/* upload */


function mail_write_upload_object_view(src){
		document.writeln(src);
}	// function

function attachFile()
{
	if (nmail_updown_cp_check() == false) return;

	//frm.fUpload.AddFile(frm.txtAttachFile.value);
	frm.fUpload.AddFindFile();
	uploadAutoResize();
	attachSizeUpdate();
}
function attachFolder()
{
	if (nmail_updown_cp_check() == false) return;

	frm.fUpload.AddFolder();
	uploadAutoResize();
	attachSizeUpdate();

	nm_isAttachFolder = true;
}
function Del_File()
{
	if (nmail_updown_cp_check() == false) return;

	frm.fUpload.DeleteFile();
	attachSizeUpdate();
}
function ListTest()
{
	frm.fUpload.TestLinkListValue();
}
function upload()
{
	if (nmail_updown_cp_check() == false) return;

	if (frm.fUpload.TotalUploadCount() < 1)
	{
		//alert("업로드할 파일을 먼저 추가하여 주십시오.");
	} else 
	{
		frm.fUpload.UploadExistUrl("");
		frm.fUpload.UploadUrlAddress(nm_http_url_root + "/mail_write_upload_activex.php");

		var fileinfo = frm.fUpload.GetUpFileInfo();

		var result = frm.fUpload.UploadStart(0);

		// 업로드 성공시 업로드한 파일 목록을 저장함
		if (result == '1')
		{
			document.forms.write.mf_attach_names.value = fileinfo.replace(/\|\:\|/g, '||').replace(/\//g, '|');
		} else
		{
			if (confirm("파일 첨부가 실패하였습니다.\n\n파일을 다시 첨부하거나 메일 내용을 수정하시겠습니까?\n\n\n※ [취소]를 누르면 메일이 그대로 발송됩니다."))
			{
				// 메일전달시 첨부파일
				forward_attach_add();

				return false;
			}
		}

		document.getElementById('fUpload').height = 105;
		attachSizeUpdate();
	}	// if()
}
function getUploadFile_test()
{
	var tmpsize = frm.fUpload.GetTotalUpSize();  //업로드 파일의 총크기

	var tmpcount = frm.fUpload.TotalUploadCount(); // 업로드 파일 갯수

	var tmpfile = frm.fUpload.GetUpFileInfo(); //업로드 파일정보.

	alert(tmpsize + ' | ' + tmpcount + ' | ' + tmpfile);		
}	// function()

function uploadAutoResize()
{
	var obj = document.getElementById('fUpload');
	var tuc = obj.TotalUploadCount();
	var tcu_base = 18;
	var tcu_margin = 20;
	var tcu_max = 400;
	var u_height = parseInt(obj.height, 10);
	var now_height = (tuc * tcu_base)+tcu_margin;
	if (u_height < now_height)
	{
		if (now_height > tcu_max)
			now_height = tcu_max;
		obj.height = now_height;
	}	// if()
}	// function()

function UpdateSize()
{
	attachSizeUpdate();
}	// function()

function nmail_updown_cp_error()
{
	nmail_updown_cp_status = false;
	//alert("업로드/다운로드용 컴포넌트가 설치되지 않았습니다.");		
}	// function()

function nmail_updown_cp_check()
{
	if (!frm)
	{
		alert("업로드를 위한 ActiveX 컨트롤이 로드되지 않았습니다.\n\n화면이 제대로 보이지 않을 경우 브라우저에서 [새로고침]을 시도해보시기 바랍니다. ");
		return false;
	}	// if()

	if (nmail_updown_cp_status == false)
	{
		alert("업로드를 위한 ActiveX 컨트롤이 설치되지 않았습니다.");
		return false;
	}	// if()		
}	// function()

function SizeCalc(fileSize) {
	var strSize = 0.0;
	var strUnit = "";

	// GB 단위 계산
	if (fileSize > (1024*1024*1024))
	{
		strSize = (fileSize / (1024 * 1024 * 1024));
		strUnit = "G";
	} else if (fileSize > (1024*1024))              // MB 단위 계산
	{
		strSize = (fileSize / (1024 * 1024));
		strUnit = "M";
	}else	// KB 단위 계산
	{
		strSize = (fileSize / 1024);
		strUnit = "K";
	}

	return strSize.toFixed(2) + strUnit;
}

function GetSmallTotalSizeCID()
{
	var _size = 0;
	if (nmail_updown_cp_status != false)
		_size = parseInt(document.forms.write.mf_cid_size.value, 10) + frm.fUpload.GetSmallTotalSize();
	return _size;
}	// function()

function attachSizeUpdate()
{
	if (nmail_updown_cp_status != false)
	{
		//document.getElementById('sUploadSize').innerHTML = SizeCalc(frm.fUpload.GetSmallTotalSize());
		document.getElementById('sUploadSize').innerHTML = SizeCalc(GetSmallTotalSizeCID());
		document.getElementById('bUploadSize').innerHTML = SizeCalc(frm.fUpload.GetBigTotalSize());
	}
}	// function()

var attachFileFromMail_exec = false;
function attachFileFromMail(filename, uniqname, filesize)
{
	if (attachFileFromMail_exec == false)
	{
		if (nmail_updown_cp_check() == false)
		{
			attachFileFromMail_exec = true;
			return;
		}

		frm.fUpload.AttachFileFromMail(filename, uniqname, filesize);
		uploadAutoResize();
		attachSizeUpdate();
	}
}

function upload_mode_change_text()
{
	if (confirm("업로드 방식을 Text 모드로 전환하시겠습니까?\n\n안내) 입력하신 내용은 화면상에서 사라지고, [임시보관함]에 저장됩니다."))
	{
		mail_write_draft_save(2);

		var expDate = new Date();
		expDate.setDate(expDate.getDate() + 365);
		document.cookie = 'nm_upmode=T;expires='+expDate.toGMTString();

		location.reload();
	}	// if()			
}	// function()

function upload_mode_change_activex()
{
	// IE 외의 브라우저에서는 ActiveX를 지원하지 않으므로 비활성화처리
	if (navigator.appVersion.indexOf("MSIE") == -1)
	{
		alert("ActiveX 모드는 Internet Explorer 웹브라우저에서만 지원됩니다.");
	} else if (get_activex_use_support() == false)		// [2012-10-22] IE 10 fix
	{
		alert("현재 브라우저에서는 ActiveX를 지원하지 않습니다.");
	} else if (confirm("업로드 방식을 ActiveX 모드로 전환하시겠습니까?\n\n안내) 입력하신 내용은 화면상에서 사라지고, [임시보관함]에 저장됩니다."))
	{
		mail_write_draft_save(2);

		var expDate = new Date();
		expDate.setDate(expDate.getDate() + 365);
		document.cookie = 'nm_upmode=A;expires='+expDate.toGMTString();

		location.reload();
	}	// if()
}	// function()
