/*--------------------------------------------------*/
/*ブログなんかでよくみかけるJavaScriptを利用したタグ挿入機能。*/
/*--------------------------------------------------*/
function ahrefThis() {
var strHref;
	if (document.selection)
		strSelection = document.selection.createRange().text;
	else
		strSelection = '';

num1=document.form1.select.length;
for (i=0;i<num1;i++) {
	flag1=document.form1.select[i].checked;
	if (flag1) strHref = document.form1.select[i].value;
}
	if (strHref == null) return;

	var textpre = "<< " + strHref.replace(/&/g,'&') + " >>\n";
	insertAroundCaret(textpre);
}


// stores the caret
var lastSelected, lastCaretPos;
function storeCaret (textEl) {

	// store caret
	if (textEl.createTextRange) 
		lastCaretPos = document.selection.createRange().duplicate();

	// also store lastselectedelement
	lastSelected = textEl;

	nonie_FormType = textEl.name;

	scrollTop = textEl.scrollTop;
}


// inserts text at caret (overwriting selection)
function insertAtCaret (text) {
	var textEl = lastSelected;
	if (textEl && textEl.createTextRange && lastCaretPos) {
		var caretPos = lastCaretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
	} else if (!document.all && document.getElementById) {
		mozReplace(document.getElementById('input' + nonie_FormType), text);
		if(scrollTop>-1) {
			document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
		}
	} else if (textEl) {
		textEl.value  += text;
	} else {
		document.getElementById('input' + nonie_FormType).value += text;		
		if(scrollTop>-1) {
			document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
		}
	}

	updAllPreviews();

}



// inserts a tag around the selected text

function insertAroundCaret (textpre, textpost) {

	var textEl = lastSelected;

	if (textEl && textEl.createTextRange && lastCaretPos) {
		var caretPos = lastCaretPos;
		caretPos.text = textpre + caretPos.text + textpost;
	} else if (!document.all && document.getElementById) {
		mozWrap(document.getElementById('input' + nonie_FormType), textpre);	
		if(scrollTop>-1) {
			document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
		}
	} else {
		document.getElementById('input' + nonie_FormType).value += textpre;
		if(scrollTop>-1) {
			document.getElementById('input' + nonie_FormType).scrollTop = scrollTop;
		}
	}

	updAllPreviews();
}



/* some methods to get things working in Mozilla as well */
function mozWrap(txtarea, lft) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + lft + s2 + s3;
}

function mozReplace(txtarea, newText) {
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + newText + s3;
}

function mozSelectedText() {
	var txtarea = document.getElementById('input' + nonie_FormType);
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd==1 || selEnd==2) selEnd=selLength;
	return (txtarea.value).substring(selStart, selEnd);
}