﻿//使用说明:在标签input中插入method属性:
//现在支持的判断函数有：
//email(判断是否是邮件地址)
//empty(判断是否为空)
//number(判断是否为整数)
//2|6(判断是否是2-6为字符串)
//=objid(验证是否相等) objid为判断该控件内容的对应控件
//url(验证是否url)
//username
//float(判断是否为浮点数)
var path = "../js/img/";
var url = "../samename.aspx";

//验证用户名是否存在
function this_username(obj_id) {
    if ($("#" + obj_id).length > 0) {
        $.ajax({
            type: "post",
            data: { "username": escape($("#" + obj_id).val()) },
            url: url,
            cache: false,
            async: true,

            beforeSend: function(XMLHttpRequest) {
                static_message(obj_id, "查找是否有相同用户名中，请稍候..");
            },
            success: function(data, textStatus) {
                if (data == "false") {
                }
                else if (data == "true") {
                    static_message(obj_id, "该用户名已被注册!");
                }
                else {
                    static_message(obj_id, data);
                }
            },
            error: function() {
                static_message(obj_id, "查找相同用户名页面出现异常!");
            }
        });
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}




//判断是否URL
function this_url(obj_id) {
    if ($("#" + obj_id).length > 0) {
        var reg = "[a-zA-z]+://[^s]*";
        var txt = $("#" + obj_id).val();
        if (txt == "") {
            return "pass";
        }
        else {
            if (txt.match(reg) == null) {
                return "internet地址形式,如：http://www.baidu.com!";
            }
            else {
                return "pass";
            }
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}

//判断对象是否为空
function this_empty(obj_id) {
    if ($("#" + obj_id).length > 0) {
        if ($("#" + obj_id).val() == "") {
            return "必填!";
        }
        else {
            return "pass";
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}
//判断对象是否为数字
function this_number(obj_id) {
    if ($("#" + obj_id).length > 0) {
        var txt = $("#" + obj_id).val();
        if (txt == "") {
            txt = "0";
        }
        if (txt.search("^-?\\d+$") != 0) {
            return "请输入整数!";
        }
        else {
            return "pass";
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}
//判断对象是否为邮件格式
function this_email(obj_id) {
    if ($("#" + obj_id).length > 0) {
        var txt = $("#" + obj_id).val();
        if (txt.search("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*") != 0) {
            return "邮件地址格式!";
        }
        else {
            return "pass";
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}
//判断是否相等
function this_equals(obj_id, obj_id2) {
    var name = obj_id2.toString().substring(1, obj_id2.toString().length);
    if ($("#" + obj_id).length > 0) {
        var txt = $("#" + obj_id).val();
        var txt1 = $("#" + name).val();
        if (txt != txt1) {
            return "确认信息和该信息不一致!";
        }
        else {
            return "pass";
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}
//判断是否几位到几位
function this_length(obj_id, methodstring) {
    var m_array = methodstring.toString().split("|");
    var x = parseInt(m_array[0]);
    var y = parseInt(m_array[1]);
    if ($("#" + obj_id).length > 0) {
        var txt = $("#" + obj_id).val();
        if (txt.length >= x && txt.length <= y) {
            //alert(m_array[0] + "-" + m_array[1] + "位!");
            return "pass";
        }
        else {
            return m_array[0] + "-" + m_array[1] + "位!";
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}
//判断是否为数字
function this_rate(obj_id) {
    if ($("#" + obj_id).length > 0) {
        var txt = $("#" + obj_id).val();
        if (isNaN(txt)) {
            return "请输入有效数字!";
        }
        else {
            return "pass";
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}


//在对象后面跟随显示相关提示信息
function static_message(obj_id, discription) {
    var img = "2.gif";
    if ($("#" + obj_id).length > 0) {
        if ($("#" + obj_id + "_msgbox").length > 0) {
            $("#" + obj_id + "_msgbox").html(discription);
        }
        else {
            $("#" + obj_id).after('<span id="' + obj_id + '_msgbox" style="font-size:12px;padding-left:5px;color:red;"></span>');
            $("#" + obj_id + "_msgbox").html("* " + discription);
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }

}
//浮动显示相关提示信息
function float_message(obj_id, discription) {
    if ($("#" + obj_id).length > 0) {
        var width = $("#" + obj_id).width();
        var height = $("#" + obj_id).height();
        var top = $("#" + obj_id).offset().top;
        var left = $("#" + obj_id).offset().left;
        var thistop = top - height - 23;
        var thisleft = left + width;
        if ($("#" + obj_id + "_msgbox").length > 0) {
            $("#" + obj_id + "_msgbox").html('<table style="border:0px;border-spaceing:0px;border-collapse:collapse;font-size:12px;color:red;font-weight:normal;white-space:nowrap;"><tr><td style="width:33px;height:43px;background-image:url(' + path + 'bg1.gif);background-repeat:no-repeat;white-space:nowrap;"></td><td style="height:43px;background-image:url(' + path + 'bg2.gif);background-repeat:repeat-x;">' + discription + '</td><td style="width:11px;height:43px;background-image:url(' + path + 'bg3.gif);background-repeat:no-repeat;"></td></tr></table>');
        }
        else {
            $("#" + obj_id).after('<div id="' + obj_id + '_msgbox" style="position:absolute;z-index:50;white-space:nowrap;top:' + thistop + 'px;left:' + thisleft + 'px;display:none;font-size:12px;"></div>');
            $("#" + obj_id + "_msgbox").html('<table style="border:0px;border-spaceing:0px;border-collapse:collapse;font-size:12px;color:red;font-weight:normal;white-space:nowrap;"><tr><td style="width:33px;height:43px;background-image:url(' + path + 'bg1.gif);background-repeat:no-repeat;white-space:nowrap;"></td><td style="height:43px;background-image:url(' + path + 'bg2.gif);background-repeat:repeat-x;">' + discription + '</td><td style="width:11px;height:43px;background-image:url(' + path + 'bg3.gif);background-repeat:no-repeat;"></td></tr></table>');
            $("#" + obj_id + "_msgbox").show('300');
        }
    }
    else {
        alert("不存在ID为" + obj_id + "的对象!");
    }
}
//form验证
function check_form(form_id, yesfloat) {
    var name = "";
    var method = "";
    var str;
    var msgbox = "";
    $("#" + form_id + " input").each(function() {
        name = $(this).attr('id');
        if ($("#" + name + "_msgbox").length > 0) {
            $("#" + name + "_msgbox").hide('300').remove();
        }
    });
    $("#" + form_id + " input:text").each(function() {
        for (var i = 0; i < 20; i++) {
            $(this).val($(this).val().replace('\'', ''));
        }
    });
    $("#" + form_id + " select").each(function() {
        name = $(this).attr('id');
        if ($("#" + name + "_msgbox").length > 0) {
            $("#" + name + "_msgbox").hide('300').remove();
        }
    });
    $("#" + form_id + " input").each(function() {
        name = $(this).attr('id');
        method = $(this).attr('method');
        if (method == null) {
            method = "";
        }
        var this_array = method.split(",");
        for (var i = 0; i < this_array.length; i++) {
            //这里增加新的验证方法开始
            msgbox += check_msg(this_array[i], name);
            //这里增加新的验证方法结束
        }
        if (msgbox == "") {
            $("#" + name + "_msgbox").hide('300').remove();
            return true;
        }
        else {
            return false;
        }
    });


    if (msgbox == "") {
        $("#" + form_id + " select").each(function() {
            name = $(this).attr('id');
            method = $(this).attr('method');
            if (method == null) {
                method = "";
            }
            var this_array = method.split(",");
            for (var i = 0; i < this_array.length; i++) {
                //这里增加新的验证方法开始
                msgbox += check_msg(this_array[i], name);
                //这里增加新的验证方法结束
            }
            if (msgbox == "") {
                $("#" + name + "_msgbox").hide('300').remove();
                return true;
            }
            else {
                return false;
            }
        })
        if (msgbox == "") {
            str = true;
        }
        else {
            static_message(name, msgbox);
            str = false;
        }
    }
    else {
        static_message(name, msgbox);
        str = false;
    }
    return str;
}
//onkeyup事件
function onthis(obj_id, yesfloat) {
    var msgbox = "";
    var method = $("#" + obj_id).attr('method');
    if (method == null) {
        method = "";
    }
    var this_array = method.split(",");
    for (var i = 0; i < this_array.length; i++) {
        //这里增加新的验证方法开始
        msgbox += check_msg(this_array[i].toString(), obj_id);
        //alert(123);
        //这里增加新的验证方法结束
    }
    if (msgbox != "") {
        $("form").find('input').each(function() {
            var name = $(this).attr('id');
            if ($("#" + name + "_msgbox").length > 0) {
                $("#" + name + "_msgbox").hide('300').remove();
            }
        });
        $("form").find('select').each(function() {
            var name = $(this).attr('id');
            if ($("#" + name + "_msgbox").length > 0) {
                $("#" + name + "_msgbox").hide('300').remove();
            }
        });
        if (yesfloat == true) {
            float_message(obj_id, msgbox);
        }
        else {
            static_message(obj_id, msgbox);
        }
    }
    else {
        $("form").find('input').each(function() {
            var name = $(this).attr('id');
            if ($("#" + name + "_msgbox").length > 0) {
                $("#" + name + "_msgbox").hide('300').remove();
            }
        });
    }
}

//验证列表
function check_msg(title, obj_id) {
    var msgbox = "";
    if (title == "empty") {
        var thisbox = this_empty(obj_id);
        if (thisbox == "pass") {
            thisbox = "";
        }
        msgbox += thisbox;
    }
    if (title == "email") {
        var thisbox = this_email(obj_id);
        if (thisbox == "pass") {
            thisbox = "";
        }
        msgbox += thisbox;
    }
    if (title == "number") {
        var thisbox = this_number(obj_id);
        if (thisbox == "pass") {
            thisbox = "";
        }
        msgbox += thisbox;
    }
    if (title == "url") {
        var thisbox = this_url(obj_id);
        if (thisbox == "pass") {
            thisbox = "";
        }
        msgbox += thisbox;
    }
    if (title.indexOf("|") >= 0) {
        var thisbox = this_length(obj_id, title);
        if (thisbox == "pass") {
            thisbox = "";
        }
        msgbox += thisbox;
    }
    if (title.indexOf("=") >= 0) {
        var thisbox = this_equals(obj_id, title);
        if (thisbox == "pass") {
            thisbox = "";
        }
        msgbox += thisbox;
    }
    if (title == "float") {
        var thisbox = this_rate(obj_id);
        if (thisbox == "pass") {
            thisbox = "";
        }
        msgbox += thisbox;
    }
    if (title == "username") {
        this_username(obj_id);
    }
    return msgbox;
}
//以下是我自己创作的datalist控件组合.方法
function show_selectText(objId, content) {
    var path = '/js/img/';
//    $("#" + objId).after('<div id="' + objId + '_layer" style="display:none;z-index:100;position:absolute;top:0px;left:0px;padding:5px;height:auto;border:Solid 1px Silver;background-color:White;line-height:14px;" class="contains">' + content + '</div>');
//    $("#" + objId + "_layer").remove();
    $("#" + objId).html('<div id="'+objId +'_flow" style="position:relative;background-image:url(' + path + 'aqua-bg.gif);line-height:20px;background-repeat:repeat-x;background-position:left 1px;"><div style="position:absolute;z-index:100;right:0px;top:0px;#top:1px;*top:1px;"><img src="' + path + 'aqua-arrow.gif" alt="" id="' + objId + '_img" style="cursor:pointer;"/></div><input type="text" style="border:0px; height:16px;background-image:url(' + path + 'aqua-bg.gif); padding-left:10px;" id="' + objId + '_box" readonly="readonly" method="empty" value=""/><input id="' + objId + '_hid" name="' + objId + '_hid" type="hidden" value=""/><div id="' + objId + '_layer" style="display:none;z-index:100;position:absolute;top:0px;left:0px;padding:5px;height:auto;border:Solid 1px Silver;background-color:White;line-height:14px;" class="contains">' + content + '</div></div>');
    var myWidth = $('#' + objId + '_layer').width();
    var myTextWidth = $('#' + objId + '_layer').width() - 13;
    $('#' + objId + '_flow').css({ 'width': myWidth + 'px' });
    $('#' + objId + '_box').css({ 'width': myTextWidth + 'px' });
    $('#' + objId + '_img').toggle(function() {
        var left = $("#" + objId).offset().left;
        var top = $("#" + objId).offset().top + 16;
        $('.contains').hide();
        $('#' + objId + '_layer').css({ 'top': '17px', 'left': '0px', 'width': myTextWidth + 'px' }).show();
    }, function() {
        $('#' + objId + '_layer').hide();

    });
    $('.mytreeBar').bind('mouseover', function() {
        $(this).parent().find('.expandable-hitarea').trigger('click');
    });
}

function show_selectTextSame(objId, content) {
    var path = '../js/img/';
    $("#" + objId).html('<div style="position:relative;width:200px;background-image:url(' + path + 'aqua-bg.gif);line-height:20px;background-repeat:repeat-x;"><div style="position:absolute;z-index:3;right:0px;top:0px;"><img src="' + path + 'aqua-arrow.gif" alt="" onclick="show_layer(\'' + objId + '\')" style="cursor:pointer; float:right;"/></div><input type="text" style="border:0px; height:16px;width:110px; float:left;background-image:url(' + path + 'aqua-bg.gif); padding-left:10px;" id="' + objId + '_box" readonly="readonly" method="empty"/><input id="' + objId + '_hid" name="type_hid" type="hidden" value=""/><div style="clear:both;"></div></div><div id="' + objId + '_layer" style="display:none;z-index:10;position:absolute;top:0px;left:0px;padding:5px;height:auto;border:Solid 1px Silver;background-color:White;line-height:14px;width:187px;" class="contains">' + content + '</div>');
}
//产品搜索用的
function show_searchText(objId, content) {
    var path = '/js/img/';
    $("#" + objId).after('<div id="' + objId + '_layer" style="display:none;z-index:100;position:absolute;top:0px;left:0px;padding:5px;height:auto;border:Solid 1px Silver;background-color:White;line-height:14px;" class="contains">' + content + '</div>');
    var myWidth = $('#' + objId + '_layer').width() + 23;
    var myTextWidth = $('#' + objId + '_layer').width();
    $("#" + objId + "_layer").remove();
    $("#" + objId).html('<div style="position:relative;width:' + myWidth + 'px;background-image:url(' + path + 'aqua-bg.gif);line-height:20px;background-repeat:repeat-x;background-position:left 1px;"><div style="position:absolute;z-index:100;right:0px;top:0px;#top:1px;*top:1px;"><img src="' + path + 'aqua-arrow.gif" alt="" id="' + objId + '_img" style="cursor:pointer;"/></div><input type="text" style="border:0px; height:16px;width:' + myTextWidth + 'px;background-image:url(' + path + 'aqua-bg.gif); padding-left:10px;" id="' + objId + '_box" readonly="readonly" method="empty" value=""/><input id="' + objId + '_hid" name="' + objId + '_hid" type="hidden" value=""/><div id="' + objId + '_layer" style="display:none;z-index:100;position:absolute;top:0px;left:0px;padding:5px;height:auto;border:Solid 1px Silver;background-color:White;line-height:14px;" class="contains">' + content + '</div></div>');
    $('#' + objId).hover(function() {
        var left = $(this).offset().left + 110;
        var top = $(this).offset().top - 20;
        $('.contains').hide();
        $('#' + objId + '_layer').css({ 'top': '17px', 'left': '0px' }).show();
    },
        function(e) {
            var position_x = $('#' + objId + '_layer').offset().left;
            var position_y = $('#' + objId + '_layer').offset().top + $(document).scrollTop;
            var obj_width = $('#' + objId + '_layer').width();
            var obj_height = $('#' + objId + '_layer').height() + 50;

            var x = e.clientX;
            var y = e.clientY;

            if (x < position_x || x > position_x + obj_width || y < position_y || y > position_y + obj_height) {
                $('#' + objId + '_layer').fadeOut(50);
            }
        });
    $('.mytreeBar').bind('mouseover', function() {
        $(this).parent().find('.expandable-hitarea').trigger('click');
    });
    $('#' + objId + '_layer').hover(function() {

    }, function(e) {
    var position_x = $('#' + objId + '_layer').offset().left;
    var position_y = $('#' + objId + '_layer').offset().top + $(document).scrollTop;
    var obj_width = $('#' + objId + '_layer').width();
    var obj_height = $('#' + objId + '_layer').height() + 50;

    var x = e.clientX;
    var y = e.clientY;

    if (x < position_x || x > position_x + obj_width || y < position_y || y > position_y + obj_height) {
        $('#' + objId + '_layer').fadeOut(50);
    }
    });
}
