//////////////////////////////////
//
//   Selection Support
//      by Colspan (Miyoshi)
//     http://colspan.net/
//        License : MIT license
//
// formの要素にあるカーソルor選択範囲の処理
var selectionSupport = {
  version : "20080110",
  author : "Colspan",
  backSpace : function (textElement){ // 文字列後退
	  //IE support
	  if (document.selection) {
		  textElement.focus();
		  sel = document.selection.createRange();
		  if( sel.text == "") sel.moveStart("character",-1);
		  sel.text = "";
		  sel.move("character",0);
		  sel.select();
	  }
	  //Mozilla
	  else if (textElement.selectionStart || textElement.selectionStart == '0') {
		  var startPos = textElement.selectionStart;
		  var endPos = textElement.selectionEnd;
		  var temp = textElement.value;
		  if( startPos != endPos ){
			  textElement.value = textElement.value.substring(0, startPos )
			  + textElement.value.substring(endPos , textElement.value.length);
			  textElement.selectionStart = textElement.selectionEnd = startPos -1;
		  }
		  else{
			  textElement.value = textElement.value.substring(0, startPos - 1)
			  + textElement.value.substring(endPos , textElement.value.length);
			  textElement.selectionStart = textElement.selectionEnd = startPos - 1;
		  }
  //		document.getElementById("debug").innerHTML = startPos+"<br>"+endPos;
	  }
	  else {
		  textElement.value = textElement.value.substring(0, textElement.value.length-1);
	  }
  },
  insert : function (textElement, inputValue) { // 文字列挿入
    if( ! inputValue ) return;
	  //IE support
	  if (document.selection) {
		  textElement.focus();
		  sel = document.selection.createRange();
		  sel.text = inputValue;
		  sel.select();
	  }
	  //Mozilla
	  else if (textElement.selectionStart || textElement.selectionStart == '0') {
		  var startPos = textElement.selectionStart;
		  var endPos = textElement.selectionEnd;
		  textElement.value = textElement.value.substring(0, startPos)
		  + inputValue
		  + textElement.value.substring(endPos, textElement.value.length);
		  textElement.selectionStart = textElement.selectionEnd = startPos + inputValue.length;
	  } else {
		  textElement.value += inputValue;
	  }
  }
}


