var mousePosition = 0;
function btnEvent(val) {
    var searchKeyobj = document.getElementById("searchKey");
    searchKeyobj.value = searchKeyobj.value.substring(0, mousePosition) + val + searchKeyobj.value.substring(mousePosition);
    mousePosition++;
    setCaretPosition(searchKeyobj, mousePosition);
    currentPage = 1;
    search();
}
function btnEvent1(val) {
    var searchKeyobj = document.getElementById("searchKey");
    searchKeyobj.value = searchKeyobj.value.substring(0, mousePosition) + val + searchKeyobj.value.substring(mousePosition);
    mousePosition++;
    setCaretPosition(searchKeyobj, mousePosition);
    currentPage = 1;
}
var st = 0;
function blurEvent(obj) {
//        if (mousePosition == 0 && st == 0){//alert(mousePosition + "  " + state);
//            st = 1;
//            mousePosition = getCursortPosition(obj);
//        }
}
function clickEvent(obj) {
    mousePosition = getCursortPosition(obj);
}
//该事件主要是有键盘的情况下防止用户按了左右键，导致光标位置计算不准确，平板电脑上没用
function downEvent(obj) {
    var keycode = 0;
    if (event.keyCode)
        keycode = event.keyCode;
    else
        keycode = obj.which;
    if(keycode == 37 || keycode == 39)
        mousePosition = getCursortPosition(obj);
}
//退格键
function backspaceEvent() {
    var searchKeyobj = document.getElementById("searchKey");
    if(mousePosition > 0) {//alert(mousePosition);
        searchKeyobj.value = searchKeyobj.value.substring(0, mousePosition-1) + searchKeyobj.value.substring(mousePosition);
        mousePosition--;
        currentPage = 1;
        search();
    }
    setCaretPosition(searchKeyobj, mousePosition);
}
function getCursortPosition(ctrl) {//获取光标位置函数
    var CaretPos = 0; // IE Support
    if (document.selection) {
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -ctrl.value.length);
        CaretPos = Sel.text.length;
    }
    // Firefox support
    else if (ctrl.selectionStart || ctrl.selectionStart == '0')
        CaretPos = ctrl.selectionStart;
    return (CaretPos);
}
function setCaretPosition(ctrl, pos) {//设置光标位置函数
    if (ctrl.setSelectionRange) {
        ctrl.focus();
        ctrl.setSelectionRange(pos, pos);
    } else if (ctrl.createTextRange) {
        var range = ctrl.createTextRange();
        range.collapse(true);
        range.moveEnd('character', pos);
        range.moveStart('character', pos);
        range.select();
    }
}
function CursorEvent(ty) {
    var searchKeyobj = document.getElementById("searchKey");
    if(ty == 1){//往左移动
        if(mousePosition > 0 )
            mousePosition--;
    } else if(ty == 2) {//往右移动
        if(mousePosition <  searchKeyobj.value.length)
            mousePosition++;
    }
    setCaretPosition(searchKeyobj, mousePosition);
}
function tabEvent(ty) {
	if (searchType != ty) {
		$("#tab_btn_" + ty).attr("src", $("#tab_btn_" + ty).attr("src").replace("0", "1"));
		$("#tab_btn_" + searchType).attr("src", $("#tab_btn_" + searchType).attr("src").replace("1", "0"));
		$("#searchKey").val("");
		searchType = ty;
		search();
	}
}
function toPage(p) {
    currentPage = p;
    search();
   }
function toIndex() {
	location.href = "Bas_Sel_Index.aspx";
}
$(function() {
    search();
});