﻿$(document).ready(function() {
    jQuery.fn.extend({
        insertAtInput: function(str){
            var objText = document.getElementById(this.attr('id'));
            
		    if (document.selection) {
                objText.focus();
                sel = document.selection.createRange();
                sel.text += str;
                objText.focus();
		    }
            else if (objText.selectionStart || objText.selectionStart == '0') {
                var startPos = objText.selectionStart;
                var endPos = objText.selectionEnd;
                var scrollTop = objText.scrollTop;
                objText.value = objText.value.substring(0, startPos)+str+objText.value.substring(endPos,objText.value.length);
                objText.focus();
                objText.selectionStart = startPos + str.length;
                objText.selectionEnd = startPos + str.length;
                objText.scrollTop = scrollTop;
            } 
            else {
                objText.value += str;
                objText.focus();
            }
        }
    });
});