var Myselect = null;
var colortone = new Array(10);
colortone[0] = new Array('#FFFFFF','#E5E4E4','#D9D8D8','#C0BDBD','#A7A4A4','#8E8A8B','#827E7F','#767173','#5C585A','#000000');
colortone[1] = new Array('#FEFCDF','#FEF4C4','#FEED9B','#FEE573','#FFED43','#F6CC0B','#E0B800','#C9A601','#AD8E00','#8C7301');
colortone[2] = new Array('#FFDED3','#FFC4B0','#FF9D7D','#FF7A4E','#FF6600','#E95D00','#D15502','#BA4B01','#A44201','#8D3901');
colortone[3] = new Array('#FFD2D0','#FFBAB7','#FE9A95','#FF7A73','#FF483F','#FE2419','#F10B00','#D40A00','#940000','#6D201B');
colortone[4] = new Array('#FFDAED','#FFB7DC','#FFA1D1','#FF84C3','#FF57AC','#FD1289','#EC0078','#D6006D','#BB005F','#9B014F');
colortone[5] = new Array('#FCD6FE','#FBBCFF','#F9A1FE','#F784FE','#F564FE','#F546FF','#F328FF','#D801E5','#C001CB','#8F0197');
colortone[6] = new Array('#E2F0FE','#C7E2FE','#ADD5FE','#92C7FE','#6EB5FF','#48A2FF','#2690FE','#0162F4','#013ADD','#0021B0');
colortone[7] = new Array('#D3FDFF','#ACFAFD','#7CFAFF','#4AF7FE','#1DE6FE','#01DEFF','#00CDEC','#01B6DE','#00A0C2','#0084A0');
colortone[8] = new Array('#EDFFCF','#DFFEAA','#D1FD88','#BEFA5A','#A8F32A','#8FD80A','#79C101','#3FA701','#307F00','#156200');
colortone[9] = new Array('#D4C89F','#DAAD88','#C49578','#C2877E','#AC8295','#C0A5C4','#969AC2','#92B7D7','#80ADAF','#9CA53B');

function SetFont(ch) {
	HTMLEDITOR.document.execCommand("FontName", false, ch);
    Editor_focus();
}

function SetFontSize(ch) {
	HTMLEDITOR.document.execCommand("FontSize", false, ch);
    Editor_focus();
}

function menu_click(obj) {
    if( Myselect != null ) Myselect.select();
    var setsel = obj.id.substring(3,obj.id.length);
    if( setsel == "link" ) Set_link();
	else if( setsel == "image" ) Set_image();
	else if( setsel == "reset" ) Set_remove();
	else if( setsel == "fore" || setsel == "back" ) Set_piccolor(setsel);
	else HTMLEDITOR.document.execCommand(setsel);
}

function Set_color(type, color) {
	Close_layer();
    if( type == "1" ) HTMLEDITOR.document.execCommand( "ForeColor", false, color );
    else if( type == "2" ) HTMLEDITOR.document.execCommand( "BackColor", false, color );
}

function Set_link() {
	//var mylink = prompt("リンクを入力してください.","http://");
	var targetLink = HTMLEDITOR.document.selection.createRange();
	if(targetLink != '') HTMLEDITOR.document.execCommand("createlink");

	//Myselect = HTMLEDITOR.document.selection.createRange();
	//if( Myselect != null ) Myselect.pasteHTML("<a href=\"" + mylink + "\" target=\"_blank\">" + Myselect.text + "</a> ");
	Editor_focus();
}

function Set_remove() {
	if( confirm('入力した内容を皆消します. 大丈夫ですか?') ) {
		HTMLEDITOR.document.body.innerHTML = "";
	}
}

function Set_piccolor(setsel) {
	var type = "", instr = "";
	if( setsel == "fore" ) type = 1;
	else type = 2;
	instr += "<table width='250' border='0' cellspacing='2' cellPadding='0' bgcolor='#E7E7E7'>";
	for( var k=0; k<10; k++ ) {
		instr += "<tr>";
		for( var l=0; l<10; l++ ) {
			instr += "<td onclick=\"Set_color('"+type+"','"+colortone[k][l]+"')\" unselectable='on' style='background-color:"+colortone[k][l]+"' width=20 height=10></td>";
		}
		instr += "</tr>";
	}
	instr += "</table>";

	View_layer(instr);
}

function View_layer(instr) {
	if( instr ) set_content_layer.innerHTML = instr;

	id_layer_box.style.display = "";
	id_layer_box.style.pixelTop = window.event.clientY + document.body.scrollTop + 10;
	id_layer_box.style.pixelLeft = window.event.clientX + document.body.scrollLeft - 100;
}

function Close_layer() {
	id_layer_box.style.display = "none";
	id_layer_box.style.pixelTop = 0;
	id_layer_box.style.pixelLeft = 0;
}

function Set_image() {
	var prtsize = Set_file_size_cal(Upfilesize);

	if(prtsize < 2000) {
		var upurl = "/jp/editor/img_upload.php?tmpid="+tmpid+"&tname="+tname;
		window.open(upurl, "upload", "status=no,scrollbars=no,toolbar=no,help=no,location=no,menu=no,resizable=no,top=350,left=300,width=350,height=270");
	} else {
		alert('現在('+prtsize+'K) 登録するファイル大きさの限度(2000K)を越しました.');
	}
}

function Set_multimedia() {
	var upurl = "/jp/editor/multimedia_link.php?tname="+tname;
	window.open(upurl, "upload", "status=no,scrollbars=no,toolbar=no,help=no,location=no,menu=no,resizable=no,top=350,left=300,width=350,height=250");
}

function preview() {
	var upurl = "/jp/editor/preview.html";
	var pvwin = window.open(upurl, "upload", "status=no,scrollbars=yes,toolbar=no,help=no,location=no,menu=no,resizable=yes,top=350,left=300,width=600,height=550");

	var cont = new String(HTMLEDITOR.document.body.innerHTML);

	var write_cont = "<HTML>"+
					 "<HEAD>"+
					 "<TITLE> Preview </TITLE>"+
					 "<META HTTP-EQUIV='Content-type' CONTENT='text/html; charset=shift-jis'>"+
					 "<META HTTP-EQUIV='Cache-Control' CONTENT=No-Cache>"+
					 "<META HTTP-EQUIV='Pragma' CONTENT=No-Cache>"+
					 "<META HTTP-EQUIV='expires' CONTENT=now>"+
					 "<STYLE TYPE='TEXT/CSS'>"+
					 "<!--"+
					 "P {margin-top:2px;margin-bottom:2px;}"+
					 "BODY, TD  { FONT-SIZE:9PT; FONT-FAMILY:転がし;}"+
					 "-->"+
					 "</STYLE>"+
					 "</HEAD>"+
					 "<BODY>"+ cont +"</BODY>"+
					 "</HTML>";

	pvwin.document.write(write_cont);
	//upload.preview_contents.innerHTML=cont;

	pvwin.focus();
}

function Set_file_sized(img_name, img_size) {
	if( (img_name != null) && (img_name != "") ) {
		var len = document.frm.file_info.options.length;
		document.frm.file_info.options[len] = new Option(img_name, img_name+'||'+img_size);
		Upfilesize = parseInt(Upfilesize) + parseInt(img_size);
		Set_file_size_print(Upfilesize);
		document.frm.tot_file_size.value = Upfilesize;
		Set_attact();
	}
}


function Set_attact() {
	document.frm.attact.value = '';
	for(var i=0; i<document.frm.file_info.options.length; i++) {
		document.frm.attact.value += document.frm.file_info.options[i].value +'/';
	}
}


function Del_image(flag) {
	fi = document.frm.file_info;
	if( !fi.value ) alert('ファイルを選択してください.');
	else {
		var idx = fi.selectedIndex;
		var selfile = fi.options[idx].value;
		flist = selfile.split('||');
		var fn = flist[0];
		var fsize = flist[1];
		var cont = new String(HTMLEDITOR.document.body.innerHTML);
		if( cont.indexOf(fn) > -1 ) alert('エディタに入力されたイメージを先に削除してください.');
		else {
			if( confirm('選択したイメージを削除しますか?') ) {
				fi.options.remove(idx);
				Upfilesize = parseInt(Upfilesize) - parseInt(fsize);
				Set_file_size_print(Upfilesize);
				Set_attact();
				document.frm.tot_file_size.value = Upfilesize;
				var deurl = "/jp/editor/img_upload.php?mode=delete&tmpid="+tmpid+"&fn="+fn+"&fsize="+fsize;
				if(flag==1) {	// ワケギである削除要請
					window.open(deurl, "delimg", "status=no,scrollbars=no,toolbar=no,help=no,location=no,menu=no,resizable=no,top=0,left=3000,width=1,height=1");
				}
			}
		}
	}
}

function Set_file_size_cal(fsize) {
	var prtsize = (parseInt(fsize) / 1024);
	prtsize = Math.round(prtsize * 10) / 10;

	return prtsize;
}

function Set_file_size_print(fsize) {

	var prtsize = Set_file_size_cal(fsize);

	var instr = "";
	instr = "<font color='#0000FF'><b>"+prtsize+"</b></font>";
	set_file_size.innerHTML = instr;
}

function Editor_focus() {
	//if( document.getElementById('HTMLEDITOR').contentWindow ) {
	//	Editd = document.getElementById('HTMLEDITOR').contentWindow;
	//} else {
	//	Editd = HTMLEDITOR.document;
	//}
	Editd = HTMLEDITOR.document;
	var editR = Editd.body.createTextRange();
	editR.collapse(false);
	editR.select();
	Editd.focus();
}

function edit_type(ch) {
	if( !ch ) ch = 1;
	if( ch == 3 && HTMLEDITOR.document.body.innerHTML ) {
		if( !confirm('HTMLに表現された文書様式が消えます. 切り替えますか?') ) return;
	}
	if( edittype != ch ) {
        if( ch == 1 ) {
            document.all.htmleditbox.style.display='';
            document.frm.content.style.display = 'none';
            if( edittype == 2 ) HTMLEDITOR.document.body.innerHTML = document.frm.content.value;
        	else if( edittype == 3 ) HTMLEDITOR.document.body.innerHTML = text2html(document.frm.content.value);
            Editor_focus();
        }
        else if( ch == 2 ) {
            document.all.htmleditbox.style.display='none';
            document.frm.content.style.display = '';
            if( edittype == 1 ) document.frm.content.value = HTMLEDITOR.document.body.innerHTML;
            else if( edittype == 3 ) document.frm.content.value = text2html(document.frm.content.value);
            document.frm.content.focus();
        }
        else if( ch == 3 ) {
            document.all.htmleditbox.style.display='none';
            document.frm.content.style.display = '';
            if( edittype == 1 ) document.frm.content.value = html2text(HTMLEDITOR.document.body.innerHTML);
            else if( edittype == 2 ) document.frm.content.value = html2text(document.frm.content.value);
            document.frm.content.focus();
        }

        edittype = ch;
	}
}


function on_view_org_img (src, w, h) {
	var width = parseInt(w) + 37;
	var height = parseInt(h) + 40;
	var content  = "<html><head><title>Image View</title></head>";
		content += "<body><a href='javascript:self.close();'>";
		content += "<img src=\""+src+"\" width="+w+" hegith="+h+" border=0></a></body>";
		content + "</html>";
	var org_img_win = window.open('about:blank','oiw', 'width='+width+', height='+height+', scrollbars=yes, resizable=yes');
	org_img_win.document.write(content);
	org_img_win.focus();
}

/*
function on_upload(img_pos, img_path, img_name, width, height, size, flag) {
	if(img_pos=='left') {
		var add_img = "<img src='"+img_path+img_name+"' width='"+width+"' height='"+height+"' align='absmiddle'>";
	} else if (img_pos=='right') {
		var add_img = "<img src='"+img_path+img_name+"' width='"+width+"' height='"+height+"' align='right'>";
	} else {
		var add_img = "<img src='"+img_path+img_name+"' width='"+width+"' height='"+height+"' align='top'>";
	}

	//opener.HTMLEDITOR.document.focus();
	opener.Editor_focus();
	Myselect = opener.HTMLEDITOR.document.selection.createRange();
	Myselect.pasteHTML(add_img);
	if(flag==1) opener.Set_file_sized(img_name, size);
	self.close();
}
*/

function on_upload(img_pos, img_path, img_name, width, height, size, flag) {
	if(img_pos=='left') {
		var add_img = "<img src='"+img_path+img_name+"' width='"+width+"' height='"+height+"' align='absmiddle'>";
	} else if (img_pos=='right') {
		var add_img = "<img src='"+img_path+img_name+"' width='"+width+"' height='"+height+"' align='right'>";
	} else {
		var add_img = "<img src='"+img_path+img_name+"' width='"+width+"' height='"+height+"' align='top'>";
	}

	//opener.HTMLEDITOR.document.focus();
	Editor_focus();
	Myselect = HTMLEDITOR.document.selection.createRange();
	Myselect.pasteHTML(add_img);
	if(flag==1) Set_file_sized(img_name, size);
	self.close();
}


function text2html(cont) {
	cont = cont.replace(/</gi, '&lt;');
	cont = cont.replace(/>/gi, '&gt;');
	cont = cont.replace(/\r\n/g, '<br>');
	cont = cont.replace(/\n/g, '<br>');
	cont = cont.replace(/\r/g, '<br>');
	return cont;
}

function html2text(cont) {
	cont = cont.replace(/\r\n/g, '');
	cont = cont.replace(/<br>/gi, '\r\n');
	cont = cont.replace(/<\/br>/gi, '');
	cont = cont.replace(/<p>/gi, '\r\n');
	cont = cont.replace(/<\/p>/gi, '');
	cont = cont.replace(/<[^>]+>/gi, '');
	cont = cont.replace(/&nbsp;/gi, ' ');
	cont = cont.replace(/&gt;/gi, '>');
	cont = cont.replace(/&lt;/gi, '<');
	return cont;
}                          
