////////////////////////////////////////////////////////////////////////
// 「気まぐれプログラマー」氏のスクリプトを改造
// http://blogs.dion.ne.jp/hototogisu/archives/66019.html

var scrx,scry,rngx,rngy,elem = null;
function pos(e){
elem = e || this;
r=document.selection.createRange();
scrx = document.body.scrollLeft;
scry = document.body.scrollTop;
rngx = r.offsetLeft;
rngy = r.offsetTop;
}

function ins(Str){
document.body.scrollLeft = scrx;
document.body.scrollTop = scry;
r=elem.createTextRange();
r.moveToPoint(rngx,rngy);
r.text = Str;
}

window.onload=function(){
for(i=0; i<document.forms.length; i++){
for(j=0; j<document.forms[i].elements.length; j++){
e = document.forms[i].elements[j];
if (e.type&&e.type=="textarea"){
if (elem==null){
elem=e;
}
e.onfocus = e;
e.onmouseup = pos;
e.onkeyup = pos;
   }
  }
 }
}

// 画像挿入
function ImgTag(){
	if (rngx != undefined){
		ImgSrc = window.prompt("画像のパス（src属性）を指定してください。", "http://");
		Alt = window.prompt("代替テキスト（alt属性）を指定してください。", "");
		if (ImgSrc) {
				TagStr = '<img src="' + ImgSrc + '" alt="' + Alt + '">';
			ins(TagStr);
		} else {
			return false;
		}
	} else {
		return false;
	}
}

// 変数挿入
function DwarfVars(VAR){
	if (rngx != undefined){
		if (VAR) {
			ins(VAR);
		} else {
			return false;
		}
	} else {
		return false;
	}
}

////////////////////////////////////////////////////////////////////////
   function tag(TAG,Style){
    if (! document.selection) {
    	return false;
    } else {
	    var SELECTED = document.selection.createRange();
	    if (! SELECTED.text) {
				if (rngx != undefined){
					if (Style) {
						TagText = '<' + TAG + ' style="' + Style + '"></' + TAG + '>';
					} else {
			    	TagText = '<' + TAG + '></' + TAG + '>';
			    }
		    	ins(TagText);
		    } else {
		    	return false;
		    }
	    } else {
	    	if (Style) {
			    SELECTED.text = '<' + TAG + ' style="' + Style + '">' + SELECTED.text + '</' + TAG + '>';
			  } else {
			    SELECTED.text = '<' + TAG + '>' + SELECTED.text + '</' + TAG + '>';
			  }
		  }
	  }
   }

   // ルビ挿入スクリプト
   function ruby(RP1,RP2){
    if (! document.selection) {
    	return false;
    }
    var SELECTED = document.selection.createRange();
    if (! SELECTED.text) {
			if (rngx != undefined) {
				RubyTag = '<ruby><rb>漢字</rb><rp>' + RP1 + '</rp><rt>ルビ</rt><rp>' + RP2 + '</rp></ruby>';
	    	ins(RubyTag);
	    } else {
	    	return false;
	    }
	  } else {
			RUBY = window.prompt(SELECTED.text+"に振るルビは？", "");
			if (! RUBY) return;
	    SELECTED.text = '<ruby><rb>' + SELECTED.text + '</rb><rp>' + RP1 + '</rp><rt>' + RUBY + '</rt><rp>' + RP2 + '</rp></ruby>';
	  }
   }

   // リンク挿入スクリプト
   function link(){
    if (! document.selection) return;
    var SELECTED = document.selection.createRange();
    if (! SELECTED.text) {
			if (rngx != undefined) {
				LinkText = '<a href=""></a>';
	    	ins(LinkText);
	    } else {
	    	return false;
	    }
	  } else {
			URL = window.prompt("リンク先のアドレスを入力してください。", "http://");
			if (! URL) return;
	    SELECTED.text = '<a href="' + URL + '">' + SELECTED.text + '</a>';
	  }
   }

   function CLEAR(AREA) {
    if (AREA == "all") {
     if (window.confirm("入力した内容を全て初期値に戻します。よろしいですか？")) {
      document.Form.reset();
     } 
    } else {
     if (window.confirm("小説本文を消去します。よろしいですか？")) {
      document.Form.document_area.value = "";
     }
    }
   }
   function Save() {
   	if (document.Form.filename) {
	    if (! document.Form.filename.value) {
	    	alert ("ファイル名が指定されていません。");
	    	return false;
	    }
	  } else if (document.Form.html_filename) {
	  	if (! document.Form.html_filename.value) {
	    	alert ("ファイル名が指定されていません。");
	    	return false;
	    }
	  }
   	var TopCheck = 0;
   	if (document.Form.link_top) {
    	if ((document.Form.link_top_word.value) && (document.Form.link_top.value)) {
     		TopCheck = 1;
     	}
    } else {
    	TopCheck = 1;
    }
   	var HomeCheck = 0;
   	if (document.Form.link_home) {
    	if ((document.Form.link_home_word.value) && (document.Form.link_home.value)) {
     		HomeCheck = 1;
     	}
    } else {
     		HomeCheck = 1;
   	}
   	if ((TopCheck == 0) && (HomeCheck == 0)) {
   		if (! window.confirm("目次へのリンクがありませんがよろしいですか？")) {
				return false;
			}
  	}
    document.Form.save.value = 1;
    document.Form.target = "main";
    document.Form.submit();
   }
   
   // テンプレート確認
   function tempconf(Folder,type) {
   	if (type) {
    	L = document.Form[type].length;
	    for (i=0; i<L; i++) {
	     if (document.Form[type][i].selected == true) {
	      TMP = document.Form[type][i].value;
	     }
	    }
    	w = Folder + '/' + TMP;
	  } else {
	    L = document.Form.template.length;
	    for (i=0; i<L; i++) {
	     if (document.Form.template[i].selected == true) {
	      TMP = document.Form.template[i].value;
	     }
	    }
    	w = Folder + '/' + TMP;
	  }
    window.open(w,"");
   }
   function indextempconf(Folder,Name) {
   	if (Name) {
	    L = document.Form[Name].length;
	    for (i=0; i<L; i++) {
	     if (document.Form[Name][i].selected == true) {
	      TMP = document.Form[Name][i].value;
	     }
	    }
	  } else {
	    L = document.Form.template.length;
	    for (i=0; i<L; i++) {
	     if (document.Form.template[i].selected == true) {
	      TMP = document.Form.template[i].value;
	     }
	    }
	  }
    w = Folder + '/index/' + TMP;
    window.open(w,"");
   }
   
   // ページチェック確認
   function IndexSelectCheck(Form) {
   	num = Form["index"].length;
   	i = 0;
   	j = 0;
   	while (i < num) {
   		if (Form["index"][i].checked) {
   			j++;
   		}
   		i++;
   	}
   	if (j == 0) {
			alert("１ページ以上選んでください。");
			return false;
   	}
   }
   // 目次ページ名入力確認
   function IndexLinkCheck(Form) {
   	num = Form.elements.length;
   	i = 0;
   	j = 0;
   	while (i < num) {
   		if (Form.elements[i].name.match(/chapter_title_(\d+)/)) {
   			if (Form.elements[i].value == "") {
	   			j++;
   			}
   		}
   		i++;
   	}
   	if (j > 0) {
			if (! window.confirm("章名が一部未記入ですがよろしいですか？")) {
   			return false;
   		}
   	}
   }

   // ライブラリ埋め込み
   function Libs(tmp) {
   	document.getElementById('LibFrame').style.display = 'block';
   	document.Lib.template.value = tmp;
   	document.Lib.submit();
   }
	
	function LibSet() {
		LibForm = document.forms[0];
		i = 0;
		libnames = new Array(LibForm["libname"][0]);
		libtypes = new Array();
		files = new Array();
		imgs = new Array();
		while (i <= LibForm["libname"].length) {
			libnames = (libnames, LibForm["libname"][i]);
			libtypes = (libtypes, LibForm["type"][i]);
			files = (files, LibForm["lib"][i]);
			imgs = (imgs, LibForm["img"][i]);
			i++;
		}
		window.opener.Form["libnames"].value = libnames.join(",");
		window.opener.Form["libtypes"].value = libnames.join(",");
		window.opener.Form["libfiles"].value = libnames.join(",");
		window.opener.Form["libimgs"].value = libnames.join(",");
		window.close();
	}
