function resetUserPassword(url, userId) {
    if (confirm("确定重置该用户的登录密码？")) {
        $.ajax({
            type: "POST",
            url: url,
            data: {userId:userId},
            success: function(data) {
                if (data.indexOf("/backend/login") >= 0) {
                    alert(need_login_system);
                } else {
                    alert(data);
                }
            }
        });
    }
}

function validateUsernameExistent(username, userTypeCode, callBackFn) {
    $.ajax({
        type: "POST",
        url: context_path + "/backend/common/validateUsernameExistent.action",
        data: {username:username,userTypeCode:userTypeCode},
        success: function(data) {
            callBackFn(data);
        }
    });
}

function validateUsernameFormat(obj) {
    $(obj).val($.trim($(obj).val()));
    var username = $(obj).val();


    if (username.length > 20) {
        alertMessage($(obj), validate_username_length_error);
        return false;
    }
    if (!/^[a-zA-Z]([0-9a-zA-Z_-]){0,19}$/.test(username)) {
        alertMessage($(obj), validate_username_format_error);
        return false;
    }

    return true;
}

function alertMessage(obj, msg) {
    var alertDiv = $("#alertMsg");
    alertDiv.css("top", obj.offset().top + obj.height() + 5 + "px");
    alertDiv.css("left", obj.offset().left + "px");
    alertDiv.css("zIndex", 10000000000000);
    alertDiv.html(msg);
    alertDiv.show();

    initAlertMsg();

    obj.blur(function() {
        $("#alertMsg").css("display", "none");
        $("#alertMsgFrame").css("display", "none");
        $("#alertMsgFrame").remove();
    });

    obj.focus();
}

function initAlertMsg() {
    if ($.browser.msie) {
        iframeDecorateDiv("alertMsg", "alertMsgFrame");
    }

    document.body.onmousedown = function() {
        $("#alertMsg").css("display", "none");
        $("#alertMsgFrame").css("display", "none");
        $("#alertMsgFrame").remove();
    }
}

function iframeDecorateDiv(divId, iframId) {
    if ($.browser.msie && $.browser.version < 7) {

        if ($("#" + divId + " > iframe").length > 0) {
            return;
        }

        var div = $("#" + divId);
        var divIframe = $('<iframe/>');

        div.append(divIframe);
        if (iframId) {
            divIframe.attr("id", iframId);
        }
        divIframe.css("position", "absolute");
        divIframe.css("display", "none");
        divIframe.css("display", "block");
        divIframe.css("z-index", "-1");
        divIframe.css("top", "-2");
        divIframe.css("left", "-2");
        try {
            divIframe.css("width", div.width() + parseInt(div.css("padding")) * 2 + 4 + "px");
            divIframe.css("height", div.height() + parseInt(div.css("padding")) * 2 + 4 + "px");
        } catch(e) {
        }
        divIframe.css("filter", "mask(color=#a09261)");
    }
}

function showMoreInfo(target, popBoxId) {
    if ($('#' + popBoxId).html()) {
        $('#' + popBoxId).css('top', $(target).offset().top + $(target).height());
        $('#' + popBoxId).css('left', $(target).offset().left);
        $('#' + popBoxId).show();
    }
}

var StringUtils = {};
StringUtils.isEmail = function(value) {
    var trimValue = $.trim(value);
    if (trimValue == "")
        return true;
    var reg = /^\b(^[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+(\.[A-Za-z0-9-]+)*((\.[A-Za-z0-9]{2,})|(\.[A-Za-z0-9]{2,}\.[A-Za-z0-9]{2,}))$)\b$/;
    return reg.test(trimValue);
}

StringUtils.isBlank = function(value) {
    var trimValue = $.trim(value);
    return trimValue == "";
};

StringUtils.isInteger = function(value) {
    var reg = /^\d+$/;
    return reg.test(value);
}

StringUtils.isNotInteger = function(value) {
    return !StringUtils.isInteger(value);
}

popMinWin = function(url) {
    window.open(url, "_blank", 'height=400,width=700,top=200,left=200,titlebar=no, toolbar=no,menubar=no,scrollbars=yes, resizable=no,location=no, status=no');
}

resetForm = function(formId) {
    var formObj = document.getElementById(formId);
    formObj.reset();
    return false;
}

/**
 * 此方法用于弹出模态窗体页面上树状结构的数据回显
 * @param elementName       隐藏域name属性的值
 */
function getSelectedNode(elementName) {
    var elements = document.getElementsByName(elementName);
    var values = '';
    if (elements != null && elements != undefined) {
        for (i = 0; i < elements.length; i++) {
            values += elements[i].value + ',';
        }
        values = values.substring(0, values.length - 1);
    }
    return values;
}

/**
 * 打开模态窗体的样式
 * 此样式用于树状结构的页面，比如选择字典数据（景区类型）
 */
var _treeModalDialog = "dialogWidth=450px;dialogHeight=500px;dialogLeft=300px;dialogTop=100px;";

/**
 * 图片自动按宽高比例缩放
 * 在图片的onload事件中调用此方法
 * 例如：<img src="${CONTEXT_PATH}/images/test.jpg" onload="setImgSize(this,70,76);"/>
 * @param img       图片对象
 * @param width     目标宽度
 * @param height    目标高度
 *
 */
function setImgSize(img, width, height) {
    var MaxWidth = width;//设置图片宽度界限
    var MaxHeight = height;//设置图片高度界限
    var HeightWidth = img.offsetHeight / img.offsetWidth;//设置高宽比
    var WidthHeight = img.offsetWidth / img.offsetHeight;//设置宽高比
    if (img.offsetWidth > MaxWidth) {
        img.width = MaxWidth;
        img.height = MaxWidth * HeightWidth;
    }
    if (img.offsetHeight > MaxHeight) {
        img.height = MaxHeight;
        img.width = MaxHeight * WidthHeight;
    }
}
