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

function makeInput(name, type){
	var inputX = document.createElement('input');
	inputX.type=type; //other than file, type can be any valid html input type
	inputX.name=name; //some name you make up for the generated file input
	inputX.id=name; //same name as above
	//inputX.style.zIndex=7; //if you wan to define style for the generated file input it's easy
	//inputX.style.top='0px';
	//inputX.style.position='absolute';
	//inputX.style.border='inset 2px';
	//inputX.size='50';
	inputX.size='20';
	return inputX;
}

function makeInput1(name, type){
	var inputX = document.createElement('input');
	inputX.type=type; //other than file, type can be any valid html input type
	inputX.name=name; //some name you make up for the generated file input
	inputX.id=name; //same name as above
	inputX.style.zIndex=7; //if you wan to define style for the generated file input it's easy
	//inputX.style.top='0px';
//	inputX.style.position='absolute';
	inputX.className = 'input1';
	inputX.size='30';
	return inputX;
}

function doAddFile()
{
	var form = document.aform;
	var divDoc = document.all['addFileDiv'];

	var fileName;

	var addFileLength = getDocumentObjectLength(divDoc, 'input', 'upFile');
	var upFile = getDocumentObject(divDoc, 'input', 'upFile', addFileLength -1);

	if(upFile.value == '')
	{
		alert('파일을 선택해주세요.');
		upFile.focus();
		return;
	}
	if(!LimitAttach(upFile.value))
	{
		divDoc.removeChild(upFile);
		var inputX = makeInput('upFile', 'file');
		inputX.attachEvent("onchange", doAddFile);
		divDoc.appendChild(inputX);
		alert("업로드가 지원되는 파일형식은 "
		+ (extArray.join(",")) + " 입니다."
		+ "\n\n파일형식을 확인해보시기 바랍니다.");
		return;
	}

	var pos = upFile.value.lastIndexOf("\\");
	fileName = upFile.value;
	if(pos > -1)
		fileName = fileName.substring(pos + 1);

	for(var i = 0 ; i < form.fileList.options.length ; i++)
	{
		if(form.fileList.options[i].text == fileName)
		{
			form.fileList.options[i].selected = true;
			alert('이미 등록된 파일입니다.');
			divDoc.removeChild(upFile);
			var inputX = makeInput('upFile', 'file');
			inputX.attachEvent("onchange", doAddFile);
			divDoc.appendChild(inputX);
			return;
		}
	}

	myEle = document.createElement("option") ;
	myEle.value = upFile.value ;
	myEle.text = fileName;
	form.fileList.add(myEle);

	//upFile.style.visibility = 'visible';
	upFile.style.display = 'none';

	var inputX = makeInput('upFile', 'file');
	inputX.attachEvent("onchange", doAddFile);

	divDoc.appendChild(inputX);
}

function doAddFile1()
{
	var form = document.aform;
	var divDoc = document.all['addFileDiv'];

	var fileName;

	var addFileLength = getDocumentObjectLength(divDoc, 'input', 'upFile');
	var upFile = getDocumentObject(divDoc, 'input', 'upFile', addFileLength -1);

	if(upFile.value == '')
	{
		alert('파일을 선택해주세요.');
		upFile.focus();
		return;
	}

	if(!LimitAttach(upFile.value))
	{
		divDoc.removeChild(upFile);
		var inputX = makeInput1('upFile', 'file');
		inputX.attachEvent("onchange", doAddFile1);
		divDoc.appendChild(inputX);
		alert("업로드가 지원되는 파일형식은 "
		+ (extArray.join(",")) + " 입니다."
		+ "\n\n파일형식을 확인해보시기 바랍니다.");
		return;
	}

	var pos = upFile.value.lastIndexOf("\\");
	fileName = upFile.value;
	if(pos > -1)
		fileName = fileName.substring(pos + 1);

	for(var i = 0 ; i < form.fileList.options.length ; i++)
	{
		if(form.fileList.options[i].text == fileName)
		{
			form.fileList.options[i].selected = true;
			alert('이미 등록된 파일입니다.');
			divDoc.removeChild(upFile);
			var inputX = makeInput1('upFile', 'file');
			inputX.attachEvent("onchange", doAddFile1);
			divDoc.appendChild(inputX);
			return;
		}
	}

	myEle = document.createElement("option") ;
	myEle.value = upFile.value ;
	myEle.text = fileName;
	form.fileList.add(myEle);

	//upFile.style.visibility = 'visible';
	upFile.style.display = 'none';

	var inputX = makeInput1('upFile', 'file');
	inputX.attachEvent("onchange", doAddFile1);

	divDoc.appendChild(inputX);
}

	function doDelFile(){
		var form = document.form;
		var addDivDoc = document.all['addFileDiv'];
		var delDivDoc = document.all['delFileDiv'];
		var Obj = document.getElementById("fileList");

		if(Obj.selectedIndex < 0 ){
			alert('파일을 선택해주세요.');
			Obj.focus();
			return;
		}
		for(var i = 0 ; i < Obj.options.length ; i++){
			if(Obj.options[i].selected == true) {
				var addFileLength = getDocumentObjectLength(addDivDoc, 'input', 'upFile');
				for(var j = 0 ; j < addFileLength -1; j++){
					var upFile = getDocumentObject(addDivDoc, 'input', 'upFile', j);
					if(upFile.value == Obj.options[i].value){
						addDivDoc.removeChild(upFile);
					}
				}
				var inputX = makeInput('delFile', 'text');
				inputX.value = Obj.options[i].value;
				delDivDoc.appendChild(inputX);
				Obj.options[i] = null;
				i--;
			}
		}
	}

		extArray = new Array();
		function LimitAttach(file){
			 //return true
			var form = document.form;
			allowSubmit = false;

			if (!file) return;
			while (file.indexOf("\\") != -1)
				file = file.slice(file.indexOf("\\") + 1);
				//ext = file.slice(file.indexOf(".")).toLowerCase();
				ext = file.slice(file.lastIndexOf(".")).toLowerCase();

			if(extArray.length == 0) return true;

			for (var i = 0; i < extArray.length; i++)	{
				if (extArray[i] == ext){
					allowSubmit = true; break;
				}
			}
			return allowSubmit;
		}

	function doBodyTypeChange(){
		var form = document.aform;
		if(form.bodyType.checked == true){
			type_change('HTML');
		} else {
			type_change('TEXT');
		}
	}

	function initContent(){
		init('HTML');
	}

	function initContent2(){
		init('TEXT');
	}

	function changeContent(){
		aform.body.value = bltn_body.document.body.innerHTML;
	}

	String.prototype.replaceAll = function(str1, str2){
	  var temp_str = this.trim();
	  temp_str = temp_str.replace(eval("/" + str1 + "/gi"), str2);
	  return temp_str;
	}

	function HtmlEscape ( text ) {
		if ( typeof( text ) != "string" )
			text = text.toString() ;

		text = text.replaceAll("&", "&amp;") ;
		text = text.replaceAll('"', "&quot;") ;
		text = text.replaceAll("<", "&lt;") ;
		text = text.replaceAll(">", "&gt;") ;
		text = text.replaceAll("'", "&#39;") ;

		return text ;
	}

	function HtmlUnEscape ( text ) {
		if ( typeof( text ) != "string" )
			text = text.toString() ;

			text = text.replaceAll("&amp;", "&");
			text = text.replaceAll("&lt;", "<");
			text = text.replaceAll("&lt;", "<");
			text = text.replaceAll("&gt;", ">");
			text = text.replaceAll("&quot;", "\"");
			text = text.replaceAll("&039;", "'");

		return text ;
	}
