﻿window.onload=doInit

//sInitColor is a global variable. It holds the value of the selected color in the color dialog box when it displays
var sInitColor = null;
var sInitFont = "돋움체";
var sInitFontSize = "2";
var currLoc = "1";
var prt_frm = parent.document.forms[0];
var strIntro = "<font face='돋움체' size='2'>이어지는 내용을 입력하세요</font>";
var checkSplit = "0";

function doInit(){
	if (parent.edit_mode=="Y")
		edit_mode=true;	
	else
		edit_mode=false;	

 	for (i=0; i<document.all.length; i++)
		//ensure that all document elements except the content editable DIV are unselectable
    		document.all(i).unselectable = "on";
	oDiv.unselectable = "off";
	txtcontent.unselectable = "off";
	//clear any text in the Document window and set the focus
	//oDiv.innerHTML="";
	//call routines to populate the drop-down list boxes on the toolbar
	getSystemFonts();
//	getBlockFormats();	//changed by ani..
	oDiv2.unselectable = "off";
	oDiv2.innerHTML = strIntro;

	oDiv.focus();	
	InitFontSetup();
	goContext(); 
	
	setTimeout('parent.init()',10);
}
function doInit1(){
	oDiv.focus();	
	//InitFontSetup();
	currLoc="1";
}
function doInit2(){
	oDiv2.focus();	
	//InitFontSetup();
	currLoc="2";
}

function clickSplit() {
	if (checkSplit=="0") {
		oDiv2.innerHTML = oDiv2.innerHTML.replace("이어지는 내용을 입력하세요", "");
		checkSplit="1";
	}
}

function InitFontSetup()	{
    //alert("InitFontSetup");
    
	//굴림체를 기본으로 설정
	sel_opt_txt(0,sInitFont);
	oDiv.document.execCommand("FontName", false, sInitFont);
	oDiv2.document.execCommand("FontName", false, sInitFont);
	ChangeFont();
	
	//폰트사이즈 2 를 기본으로
	oDiv.document.execCommand("FontSize", false, sInitFontSize);
	oDiv2.document.execCommand("FontSize", false, sInitFontSize);
	//changeFontSize();
}

//This function works for all of the command identifiers used in this page
function callFormatting(sFormatString){
	document.execCommand(sFormatString);
}

//changeFontSize detects the value of the item in the drop-down listbox and applies the value to the font of the selected text
function changeFontSize(){
	var sSelected=oToolBar.getItem(1).getOptions().item(oToolBar.getItem(1).getAttribute("selectedIndex"));
   	document.execCommand("FontSize", false, sSelected.value);
}

//VerticalMode changes the orientation of the text from left to right to top to bottom
 function VerticalMode(){	//changed by ani..
 	if (oDiv.style.writingMode == 'tb-rl'){
    		oDiv.style.writingMode = 'lr-tb';
    		oDiv2.style.writingMode = 'lr-tb';
    	}
  	else {
    		oDiv.style.writingMode = 'tb-rl';
    		oDiv2.style.writingMode = 'tb-rl';
    	}
}

//Fonts routines
//getSystemFonts uses the dialog helper object to return an array of all of the fonts on the user's system, then populates a drop-down listbox in the toolbar with the array elements
function getSystemFonts(){
	var a=dlgHelper.fonts.count;
	var fArray = new Array();
	//var oDropDown = oToolBar.createDropDownListAt("0");
	var oDropDown = oToolBar.getItem(0);
	//oDropDown.setAttribute("id","FontNameList");
	for (i = 1;i < dlgHelper.fonts.count;i++){ 
		fArray[i] = dlgHelper.fonts(i);
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = fArray[i];
		oOption.Value = i;
	} 
	//attaching the onchange event is necessary in order to detect when a user changes the value in the drop-down listbox
	//oDropDown.setAttribute("onchange",ChangeFont);
	//oDropDown.setAttribute("style","font-size:7pt;");
}

//changeFont detects the value of the item in the drop-down listbox and applies the value to the font of the selected text
function ChangeFont(){		//changed by ani..
	var sSelected=oToolBar.getItem(0).getOptions().item(oToolBar.getItem(0).getAttribute("selectedIndex"));
	document.execCommand("FontName", false, sSelected.text);
}

//BlockFormats routines
//getBlockFormats uses the dialog helper object to return an array of all of the block formats on the user's system, then populates a drop-down listbox in the toolbar with the array elements
function getBlockFormats(){
	var a=dlgHelper.blockFormats.count;
	var fArray = new Array();
	//var oDropDown = oToolBar.createDropDownListAt("2");
	var oDropDown = oToolBar.getItem(2);
	//oDropDown.setAttribute("id","FormatList");
	for (i = 1;i < dlgHelper.blockFormats.count;i++)
	{ 
		fArray[i] = dlgHelper.blockFormats(i);
		var aOptions = oDropDown.getOptions();	
		var oOption = document.createElement("OPTION");
		aOptions.add(oOption);	
		oOption.text = fArray[i];
		oOption.Value = i;
	} 
	//attach the onchange event
	//oDropDown.setAttribute("onchange",ChangeFormat);
}

//ChangeFormat detects the value of the item in the drop-down listbox and applies the value to the font of the selected text
function ChangeFormat(){	//no use-changed by ani..
	oDiv.focus();
	var sSelected=oToolBar.getItem(2).getOptions().item(oToolBar.getItem(2).getAttribute("selectedIndex"));
	document.execCommand("FormatBlock", false, sSelected.text);
} 

//callColorDlg uses the dialog helper object's ChooseColorDlg method to open the color dialog box, then changes the font or back color of the selected text
function callColorDlg(sColorType){
	if (sInitColor == null) 
		//display color dialog box
		var sColor = dlgHelper.ChooseColorDlg();
	else
		var sColor = dlgHelper.ChooseColorDlg(sInitColor);
		//change decimal to hex
		sColor = sColor.toString(16);
		//add extra zeroes if hex number is less than 6 digits
	if (sColor.length < 6) {
		var sTempString = "000000".substring(0,6-sColor.length);
		sColor = sTempString.concat(sColor);
	}
		//change color of the selected text
		document.execCommand(sColorType, false, sColor);
		sInitColor = sColor;
//		oDiv.focus();	//changed by ani..
}

//These functions create and display the splash screen and are used when the application is launched (called by onInit function) as well as when the user clicks help/about on the menu
var oPopup = window.createPopup()
function goContext()
{
  var oPopupBody = oPopup.document.body;

  oPopupBody.innerHTML = oContext.innerHTML;
  //oPopup.show(125, 125, 400, 265, document.body);
  document.body.onmousedown = oPopup.hide;
}

function sel_opt_val(oSELECTno, str_value) {  
	var sSelect=oToolBar.getItem(oSELECTno).getOptions();
	for (var i = 0; i < sSelect.length; i++) {  
		if (sSelect.item(i).value == str_value) {  
			sSelect.item(i).selected = true;  
			break;  
		}  
	}  
}  

function sel_opt_txt(oSELECTno, str_value) {  
	var sSelect=oToolBar.getItem(oSELECTno).getOptions();
	for (var i = 0; i < sSelect.length; i++) {  
		if (sSelect.item(i).text == str_value) {  
			sSelect.item(i).selected = true;  
			break;  
		}  
	}  
}  

function chkKey(e) { 
    if(window.event){ 
        e = window.event 
        var lkeycode = e.keyCode; 
    } 
    else{ 
        var lkeycode = e.charCode; 
    } 

	//화살표와 백스페이스가 입력되는경우에는...
	if(lkeycode==13 || lkeycode==37 || lkeycode==38 || lkeycode==39 || lkeycode==40 || lkeycode==8){ 
		chkFont();

//        if( window.event ){ 
//            e.keyCode = 0; 
//        }else{ 
//            e.preventDefault(); 
//        } 
    } 
} 

function chkFont(selectNo)	{	//changed by ani..
	if (selectNo=="2") {
		if (oDiv2.innerHTML!="<FONT face=" + sInitFont + " size=" + sInitFontSize + "></FONT>" && oDiv2.innerHTML!="")	{
			if (document.queryCommandValue('FontName')=="System" || document.queryCommandValue('FontSize')==null || document.queryCommandValue('FontName')==null) {
//				InitFontSetup();		//changed by ani..
			}
			else	{
				var curr_FontSize = document.queryCommandValue('FontSize');
				var curr_FontName = document.queryCommandValue('FontName');
				var curr_FormatBlock = document.queryCommandValue('FormatBlock');

				var sSelected0=oToolBar.getItem(0).getOptions().item(oToolBar.getItem(0).getAttribute("selectedIndex"));
				if (sSelected0.text!=curr_FontName) sel_opt_txt(0,curr_FontName);
				var sSelected1=oToolBar.getItem(1).getOptions().item(oToolBar.getItem(1).getAttribute("selectedIndex"));
				if (sSelected1.text!=curr_FontSize) sel_opt_txt(1,curr_FontSize);
//				var sSelected2=oToolBar.getItem(2).getOptions().item(oToolBar.getItem(2).getAttribute("selectedIndex"));
//				if (sSelected2.text!=curr_FormatBlock) sel_opt_txt(2,curr_FormatBlock);
			}
		}
	}
	else {
		if (oDiv.innerHTML!="<FONT face=" + sInitFont + " size=" + sInitFontSize + "></FONT>" && oDiv.innerHTML!="")	{
			if (document.queryCommandValue('FontName')=="System" || document.queryCommandValue('FontSize')==null || document.queryCommandValue('FontName')==null) {
//				InitFontSetup();		//changed by ani..
			}
			else	{
				var curr_FontSize = document.queryCommandValue('FontSize');
				var curr_FontName = document.queryCommandValue('FontName');
				var curr_FormatBlock = document.queryCommandValue('FormatBlock');

				var sSelected0=oToolBar.getItem(0).getOptions().item(oToolBar.getItem(0).getAttribute("selectedIndex"));
				if (sSelected0.text!=curr_FontName) sel_opt_txt(0,curr_FontName);
				var sSelected1=oToolBar.getItem(1).getOptions().item(oToolBar.getItem(1).getAttribute("selectedIndex"));
				if (sSelected1.text!=curr_FontSize) sel_opt_txt(1,curr_FontSize);
//				var sSelected2=oToolBar.getItem(2).getOptions().item(oToolBar.getItem(2).getAttribute("selectedIndex"));
//				if (sSelected2.text!=curr_FormatBlock) sel_opt_txt(2,curr_FormatBlock);
			}
		}
	}
}

function write_HTML1(str)	{	//changed by ani..??
	oDiv.focus();
	var r = range();
	if( ! r.control ) {
		r.pasteHTML(str);
	}
}

function write_HTML2(str)	{	//changed by ani..??
	oDiv2.focus();
	var r = range();
	if( ! r.control ) {
		r.pasteHTML(str);
	}
}

function range() {	//changed by ani..??
	var d = document;
	var s = d.selection;
	var r = s.createRange();
	r.parents = [];
	if( s.type == 'Control' ) {
		r.control = true;
		r.parents.push( r.item(0) );
		r.parent = r.item(0).parentElement;
	} else {
		r.control = false;
		r.parent = r.parentElement();
	}
	while( r.parent && r.parent.id != 'oDiv' ) {
		r.parents.push( r.parent );
		r.parent = r.parent.parentElement;
	}
	return r;
}

function not_support()	{
	alert("수정중에는 지원되지 않습니다.");
}

function setImage(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=350,HEIGHT=390";
	Querystring = "for=file&amp;loc=" + currLoc + "&amp;from_type=" + prt_frm.from_type.value + "&amp;blogid=" + prt_frm.blog_id.value;
	win_append_image = window.open("Inc/AppendFile.aspx?" + Querystring , "image", Param);
}

function setFlash(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=350,HEIGHT=325";
	Querystring = "for=flash&amp;loc=" + currLoc + "&amp;from_type=" + prt_frm.from_type.value + "&amp;blogid=" + prt_frm.blog_id.value;
	win_append_flash = window.open("Inc/AppendFlash.aspx?" + Querystring , "flash", Param);
}

function setMovie(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=350,HEIGHT=375";
	Querystring = "for=movie&amp;loc=" + currLoc + "&amp;from_type=" + prt_frm.from_type.value + "&amp;blogid=" + prt_frm.blog_id.value;
	win_append_movie = window.open("Inc/AppendMovie.aspx?" + Querystring , "movie", Param);
}

function setMusic(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=350,HEIGHT=320";
	Querystring = "for=music&amp;loc=" + currLoc + "&amp;from_type=" + prt_frm.from_type.value + "&amp;blogid=" + prt_frm.blog_id.value;
	win_append_music = window.open("Inc/AppendMusic.aspx?" + Querystring , "music", Param);
}

function setMyartLink(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=320,HEIGHT=230";	
	Querystring = "loc=" + currLoc + "&amp;blogid=" + prt_frm.blog_id.value;
	win_append_mylink = window.open("Inc/AppendMyartLink.aspx?" + Querystring , "mylink", Param);
}

function setURLLink(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=320,HEIGHT=155";	
	Querystring = "loc=" + currLoc + "&amp;blogid=" + prt_frm.blog_id.value;
	win_append_mylink = window.open("Inc/AppendURLLink.aspx?" + Querystring , "urllink", Param);
}

function setMedia(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=350,HEIGHT=150";
	Querystring = "loc=" + currLoc;
	win_append_media = window.open("MediaLink.aspx?" + Querystring , "media", Param);
}

function setGoods(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=yes,MENUBAR=no,WIDTH=620,HEIGHT=775";	
	Querystring = "loc=" + currLoc + "&amp;blogid=" + prt_frm.blog_id.value;
	win_append_good = window.open("Inc/AppendGoods.aspx?" + Querystring , "goods", Param);
}

function makeTEXT(){	//changed by ani..
	return oDiv.innerHTML;
}

var htmlmode=true;

function textmode()	{	//changed by ani..
	if (!htmlmode) return;
	//document.all.
	if (oDiv2.style.display=="block")
		txtcontent.value = oDiv.innerHTML + "\n<!--이 부분은 수정하지 마세요--><div id=moreview></div><!--이 부분은 수정하지 마세요-->\n" + oDiv2.innerHTML;
	else
		txtcontent.value = oDiv.innerHTML;
	textview();
	prt_frm.htmlmode.value = "N";
}

function editormode()	{	//changed by ani..
	if (htmlmode) return;
	//document.all.
	if (oDiv2.style.display=="block") {
	oDiv.innerHTML = txtcontent.value.split("\n<!--이 부분은 수정하지 마세요--><div id=moreview></div><!--이 부분은 수정하지 마세요-->")[0];
	oDiv2.innerHTML = txtcontent.value.split("\n<!--이 부분은 수정하지 마세요--><div id=moreview></div><!--이 부분은 수정하지 마세요-->")[1];
	}
	else
		oDiv.innerHTML = txtcontent.value;
	htmlview();
	prt_frm.htmlmode.value = "Y";
}

function textview()	{
	htmlmode = false;
	img_edit.src = 'img/tab_01.gif';
	img_text.src = 'img/tab_03_o.gif';
	oContainer.style.display = 'none';
	oUpButtonDiv.style.display = 'none';
	oTextDiv.style.display = '';
	txtcontent.focus();
}

function htmlview()	{
	htmlmode = true;
	img_edit.src = 'img/tab_01_o.gif';
	img_text.src = 'img/tab_03.gif';
	oContainer.style.display = '';
	if (!edit_mode)	oUpButtonDiv.style.display = '';
	oTextDiv.style.display = 'none';
	oDiv.focus();	
}

function makeTable(){
	Param = "SCROLLBARS=no,RESIZABLE=no, TOOLBAR=no,STATUS=no,MENUBAR=no,WIDTH=380,HEIGHT=380";
	win_append = window.open("Inc/make_table.asp" , null, Param);
}

function chk_append() {	//삭제된 이미지가 있는지 확인	//changed by ani..
	var currStartPosi1, currStartPosi2;
	var currText1, currText2;
	var files = prt_frm.addFileList.length;

	//for (k=0;k<2;k++) {
		//if (k==0)
			currText1 = oDiv.innerHTML;
		//else
			currText2 = oDiv2.innerHTML;
			
		for (i=0;i<parseInt(prt_frm.addFileList.length);i++) {

			if (i>=parseInt(prt_frm.addFileList.length))
				break;
			if (prt_frm.from_type.value == "admin")
				currFileName = "http://"+prt_frm.file_url.value+"/temp/" + prt_frm.addFileList[i].value;
			else{
				currFileName = "http://" + prt_frm.file_url.value + "/" + prt_frm.from_type.value + "/" + prt_frm.blog_id.value.substr(0, 1) + "/" + prt_frm.blog_id.value.substr(1, 1) + "/" + prt_frm.blog_id.value + "/temp/" + prt_frm.addFileList[i].value;
		    }

			currStartPosi1 = currText1.toLowerCase().indexOf(currFileName.toLowerCase());
			currStartPosi2 = currText2.toLowerCase().indexOf(currFileName.toLowerCase());

			if (currStartPosi1<=0 && currStartPosi2<=0) {
				if (edit_mode)	{
					if (prt_frm.from_type.value == "admin")
						currFileName = "http://"+prt_frm.file_url.value+"/temp/" + prt_frm.addFileList[i].value;
					else{
						currFileName = "http://"+prt_frm.file_url.value+"/"+prt_frm.from_type.value+"/" + prt_frm.blog_id.value.substr(0, 1) + "/" + prt_frm.blog_id.value.substr(1, 1) + "/" + prt_frm.blog_id.value + "/temp/" + prt_frm.addFileList[i].value;
					}

					currStartPosi1 = currText1.toLowerCase().indexOf(currFileName.toLowerCase());
					currStartPosi2 = currText2.toLowerCase().indexOf(currFileName.toLowerCase());
				}
			}

			if (currStartPosi1<=0 && currStartPosi2<=0) {
				prt_frm.addFileList[i].value="";
				prt_frm.addFileList[i].text="";
				for (j=i;j<parseInt(prt_frm.addFileList.length)-1;j++) {
					prt_frm.addFileList[j].value=prt_frm.addFileList[j+1].value;
					prt_frm.addFileList[j].text=prt_frm.addFileList[j+1].text;
				}
				prt_frm.addFileList.length = prt_frm.addFileList.length - 1;
				prt_frm.total_files.value = parseInt(prt_frm.total_files.value) - 1;
				i--;
			}
		}
	//}
}

