﻿/*http://lysender.co.cc/2009/01/enter-to-tab-navigation-jquery-plugin/*/
jQuery.fn.enter2tab = function() {
    this.keypress(function(e) {
        // get key pressed (charCode from Mozilla/Firefox and Opera / keyCode in IE)
        var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;

        var tmp = null;
        var maxTabIndex = 96;

        // get tabindex from which element keypressed
        var nTabIndex = this.tabIndex + 1;

        // get element type (text or select)
        var myNode = this.nodeName.toLowerCase();

        // allow enter/return key (only when in an input box or select)
        if (nTabIndex > 0 && key == 13 && nTabIndex <= maxTabIndex && ((myNode == "textarea") || (myNode == "input") || (myNode == "select"))) {
            for (var x = 0; x <= 10; x++) {
                tmp = $("textarea[tabIndex='" + nTabIndex + "'],select[tabIndex='" + nTabIndex + "'],input[tabIndex='" + nTabIndex + "']").get(0);
                if (typeof tmp != "undefined" && !$(tmp).attr("disabled")) {
                    $(tmp).focus();
                    return false;
                    //break;
                }
                else {
                    if (x == 9) nTabIndex = 1;
                    else nTabIndex++;
                }
            }
            return false;
        }
        else if (key == 13) {
            return false;
        }
    })
    return this;
}

$.format = function(source, params) {
    if (arguments.length == 1)
        return function() {
            var args = $.makeArray(arguments);
            args.unshift(source);
            return $.format.apply(this, args);
        };
    if (arguments.length > 2 && params.constructor != Array) {
        params = $.makeArray(arguments).slice(1);
    }
    if (params.constructor != Array) {
        params = [params];
    }
    $.each(params, function(i, n) {
        source = source.replace(new RegExp("\\{" + i + "\\}", "g"), n);
    });
    return source;
};

function postajax(url, data, f) {
    $.ajax({
        type: "POST",
        url: url,
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: f
    });
}

// 填充下拉列表（select）数据
function genDropDownList(objID, objArray, defaultValue) {
    var ddlist = $(objID)
    var obj = ddlist[0];
    var isIE = $.browser.msie;
    ddlist.empty();

    for (i = 0; i < objArray.length; i++) {
        var parts = objArray[i].split("_");
        var option = new Option(parts[1], parts[0]);

        if (isIE) obj.add(option);
        else obj.add(option, null);
    }

    if (defaultValue == "" || defaultValue == undefined) obj.selectedIndex = 0;
    else obj.value = defaultValue;
}

// 定位第一个可用的输入框(最多探测10个)
function FocusFirst() {
    var tmp = null;
    var maxTabIndex = 96;
    var nTabIndex = 1;

    for (var x = 0; x < 10; x++) {
        tmp = $("textarea[tabIndex='" + nTabIndex + "'],select[tabIndex='" + nTabIndex + "'],input[tabIndex='" + nTabIndex + "']").get(0);

        if (typeof tmp != "undefined" && !$(tmp).attr("disabled")) $(tmp).focus();
        else nTabIndex++;
    }
}

