﻿/*
jQuery对象增强。
*/
jQuery.extend({
    msg: function (str) {
        if (typeof (str) == 'object') { str = $(str).html() }
        //$('body').append('<div onclick="$(this).hide();" style="padding:100px;position:absolute;left:0px;top:0px;width:100%;height:' + document.body.offsetHeight + 'px;background:#000;color:#fff;filter:Alpha(Opacity=60);">' + str + '</div>');
        alert(str);
    },
    rnd: function (max, min) {
        if (max != null) {
            min = min || 0;
            return (min + Math.round(Math.random() * (max - min)));
        }
        return 0;
    },
    empty: function (str) {
        return str == null || str == "";
    },
    trim: function (text) {
        if (!this.empty(text)) {
            if (arguments.length < 2) {
                return this.trim(text, " ");
            } else {
                for (i = 1; i < arguments.length; i++) {
                    text = text.replace(new RegExp("^[" + arguments[i] + "]+", "ig"), "").replace(new RegExp("[" + arguments[i] + "]+$", "ig"), "");
                }
                return text;
            }
        }
        return "";
    },
    subString: function (str, left, right) {
        var a = str.indexOf(left);
        if (a > -1) {
            a += left.length;
            if (right == undefined) {
                return str.substr(a);
            } else {
                var b = str.indexOf(right, a);
                var l = b - a;
                if (l > 0) {
                    return str.substr(a, l);
                }
            }
        }
        return "";
    },
    go: function (url, msg, _new) {
        if (this.empty(msg)) {
            if (_new) { window.open(url); } else { window.location.href = url; }
        } else {
            if (confirm(msg)) {
                if (_new) { window.open(url); } else { window.location.href = url; }
            }
        }
    },
    Check: {
        pwd: function (s) {
            var i = 0;
            if (s.match(/[a-z]/ig)) { i++; }
            if (s.match(/[0-9]/ig)) { i++; }
            if (s.match(/(.[^a-z0-9])/ig)) { i++; }
            if (s.length < 6 && i > 0) { i--; }
            return i;
        },
        mail: function (s) {
            return /^[\w-\.]{1,30}@[a-zA-Z0-9-\.]{1,20}\.[a-zA-Z0-9]{1,5}$/i.test(s);
        },
        user: function (s, id) {
            $.get("/usercheck/?" + s, function (data) {
                if (data != "0") {
                    $("#" + id).html("<a>该用户名以被注册，请重新选择!</a>");
                    $("#" + id).attr("style", "background:url(/img/minus.gif) no-repeat 0px 8px;padding:7px 0 1px 16px;text-decoration: underline;");
                } else {
                    $("#" + id).html("");
                }
            });
        },
        verify: function (s, id) {
            if (s.length == 4) {
                $.get("/verifycheck/?" + s, function (data) {
                    if (data == "OK!") {
                        $("#" + id).show();
                    } else {
                        $("#" + id).hide();
                    }
                });
            }
        },
        form: function (f, m) {
            var tip = function (name, def, msg, obj) {
                var str = (msg != '' ? msg : "$0$1").replace(/\$0/g, name).replace(/\$1/g, def);
                if (m == 1) {
                    if (str != '') { alert(str) };
                } else {
                    $(obj).html("<a style=\"color:red;\">" + str + "</a>");
                    obj.style.cssText = "background:url(/img/minus.gif) no-repeat 0px 8px;padding:7px 0 1px 16px;text-decoration: underline;";
                }
            };
            var list = f.getElementsByTagName("span");
            if (list) {
                for (var i = 0; i < list.length; i++) {
                    var span = list[i].title;
                    if (!$.empty(span)) {
                        var arg = { name: "", title: "", min: "", max: "", type: "", goal: "", str: "", value: "" }
                        if (/^[\w-]{1,20}=.+$/i.test(span)) {
                            eval(("," + span).replace(/,/g, ";arg."))
                        } else {
                            var arr = (span + "||||||||").split("|");
                            arg = { name: arr[0], title: arr[1], min: arr[2], max: arr[3], type: arr[4], goal: arr[5], msg: arr[6], value: "" }
                        }
                        var input = $('[name=\'' + arg.name + '\']');
                        arg.value = input.attr('value');
                        if (arg.value == '') {
                            tip(arg.title, '不能为空!', arg.msg, list[i]);
                            input.focus();
                            return false;
                        } else if (arg.min != '' && arg.value.length <= arg.min) {
                            tip(arg.title, '必须大于' + arg.min + '个字符!', arg.msg, list[i]);
                            input.focus();
                            return false;
                        } else if (arg.max != '' && arg.value.length > arg.max) {
                            tip(arg.title, '必须小于' + arg.max + '个字符!', arg.msg, list[i]);
                            input.focus();
                            return false;
                        } else if (arg.type == '@' && !this.mail(arg.value)) {
                            tip(arg.title, '格式不正确!', arg.msg, list[i]);
                            input.focus();
                            return false;
                        } else if (arg.type == '=' && arg.value != $('input[name=\'' + arg.goal + '\']').attr('value')) {
                            tip("", '两次填写的密码不一致!', arg.msg, list[i]);
                            input.focus();
                            return false;
                        } else if (arg.type == '*' && arg.goal != '' && this.pwd(arg.value) <= arg.goal) {
                            tip(arg.title, '过于简单!', arg.msg, list[i]);
                            input.focus();
                            return false;
                        } else if (arg.type == 'c' && input.attr('checked') != true) {
                            tip(arg.title, '过于简单!', arg.msg, list[i]);
                            input.focus();
                            return false;
                        }
                        tip("", "", "", list[i]);
                    }
                }
            }
            return true;
        }
    },
    readme: function (obj, str, offset) {
        if ($('#readme').length == 0) {
            $('body').append('<div style="position: absolute; z-index: 500; left: 0px; top: 0px; background-color: #E3EFFF; border: 1px solid #BBC8D6; padding: 5px; display: none;" id="readme">' + id + '</div>');
        }
        var id = $('#readme');
        id.html(str);
        offset = offset || $(obj).width() + 5;
        if (id) {
            id.show();
            var p = $(obj).offset();
            var m = id.offset();
            id.offset({ top: p.top, left: (p.left + offset) });
            id.hover(
                function () { },
                function () {
                    id.hide();
                });
        }
    },
    cookie: function (key, value, time) {
        if (value != undefined) {
            time = time || 0;
            var s = (key) + '=' + escape(value);
            if (time > 0) {
                var d = new Date();
                d.setTime(d.getTime() + time);
                s += '; expires=' + d.toGMTString() + '; path=/'
            }
            document.cookie = s;
        } else {
            return decodeURI(unescape(this.subString(document.cookie + ';', key + '=', ';')));
        }
    },
    login: function (id, str) {
        var user = $.cookie('UserName');
        if (!$.empty(user)) {
            $('#' + id).html(str.replace(/\$0/g, user));
        }
    },
    getmsg: function (url, id) {
        $.get(url, function (data) {
            if (data != '') {
                if (id != undefined) {
                    $('#' + id).html(data);
                } else {
                    $.msg(data);
                }
            }
        });
    },
    gethtml: function (url, name) {
        $.get(url, function (data) {
            var xml = $(data);
            $('#' + name + 'list').html(xml.find('list').text());
            $('#' + name + 'page').html(xml.find('page').text());
        });
    },
    menushow: function (obj, id, offset) {
        if (typeof (id) == 'string') {
            $('#menutemp').remove();
            $('body').append('<div class="menu" id="menutemp">' + id + '</div>');
            id = $('#menutemp');
        }
        offset = offset || 0;
        if (id) {
            id.toggle();
            var p = $(obj).offset();
            var m = id.offset();
            if (m.top == 0 && m.left == 0) {
                id.offset({ top: p.top + obj.offsetHeight, left: (p.left + offset) });
                id.hover(
                function () { },
                function () {
                    id.hide();
                });
            }
        }
    },
    getcolor: function () {
        var color = showModalDialog('/Js/getcolor.htm', '', 'dialogWidth:18.5;dialogHeight:16;dialogTop:50;dialogLeft:500;status:no;');
        if (color != null && color != '' && color != '#NaNNaNNaN') {
            return color;
        }
    },
    inputstyle: function (id, str, re, obj) {
        var ok = false;
        re = re || new RegExp(str);
        $('#' + id).val(function () {
            if (re.test(this.value)) {
                return this.value.replace(re, '');
            } else {
                if (typeof (str) == 'function') { str = str(obj); }
                if (str != null && str != '') {
                    ok = true;
                    return this.value + str;
                } else {
                    return this.value;
                }
            }
        })
        return ok;
    },
    typelabel: function (id, index, arr) {
        var key = arr[index].split('|');
        id.each(function (j) {
            this.options.length = 0;
            for (var i = 0; i < key.length; i++) {
                this.options.add(new Option(key[i], i));
            }
        });


        //obj.options[key.length-1].selected=true;
    },
    screentest: function () {
        var s = "";
        s += "网页可见区域宽：" + document.body.clientWidth + '\n';
        s += "网页可见区域高：" + document.body.clientHeight + '\n';
        s += "网页可见区域高：" + document.body.offsetWeight + "  (包括边线的宽)\n";
        s += "网页可见区域高：" + document.body.offsetHeight + "  (包括边线的宽)\n";
        s += "网页正文全文宽：" + document.body.scrollWidth + '\n';
        s += "网页正文全文高：" + document.body.scrollHeight + '\n';
        s += "网页被卷去的高：" + document.body.scrollTop + '\n';
        s += "网页被卷去的左：" + document.body.scrollLeft + '\n';
        s += "网页正文部分上：" + window.screenTop + '\n';
        s += "网页正文部分左：" + window.screenLeft + '\n';
        s += "屏幕分辨率的高：" + window.screen.height + '\n';
        s += "屏幕分辨率的宽：" + window.screen.width + '\n';
        s += "屏幕可用工作区高度：" + window.screen.availHeight + '\n';
        s += "屏幕可用工作区宽度：" + window.screen.availWidth + '\n';
        alert(s);
    },
    open: function (url, w, h) {
        w = w || 500;
        h = h || 300;
        var t = (screen.height - h) / 2;
        var l = (screen.width - w) / 2;
        window.open(url, 'newwindow', 'width=' + w + ', height=' + h + ', top=' + t + ',left=' + l + ', toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no')
    },
    gobase64: function (str) {
        $.base64.is_unicode = true;
        str = $.base64.encode(str);
        str = str.replace(/\+/g, "*").replace(/\//g, "_").replace(/=/g, "!");
        $.go('?' + str);
    },
    downsel: function (obj) {
        var div = $('#dsel');
        if (div.length == 0) {
            $('body').append('<div id="dsel" style="position:absolute;top:100px;left:100px;width:' + (obj.offsetWidth) + 'px;height:200px;z-index:500;background-color : #fff;border : 1px solid #999;display:none;overflow-y:auto;"><iframe src="about:blank" frameborder="0" style="position:absolute;width:80%;height:100%;" id=""uself""></iframe><div id="usels" style="position:absolute;width:100%;height:100%;padding:5px;"></div></div>');
            div = $('#dsel');
        }
        div.show();
        div.hover(
                function () { },
                function () {
                    div.hide();
                });
        $.ajax({
            type: "get",
            url: "/get/xq/?text=" + escape(obj.value),
            data: "",
            cache: false,
            success: function (msg) {
                $('#usels').html(msg);
                $('#dsel iframe').css('height', ($('#usels li').length * 20) + 'px')
            }
        });


        var p = $(obj).offset();
        div.offset({ top: p.top + obj.offsetHeight, left: p.left });
    },
    flash: function (url, width, height, Param, id) {
        var str = '';
        str += '<object id=myvideo classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,47,0" width="' + width + '" height="' + height + '">';
        //str += '<PARAM NAME="_cx" VALUE="15875"><PARAM NAME="_cy" VALUE="9260"><PARAM NAME="Loop" VALUE="-1"><PARAM NAME="SAlign" VALUE="LT"><PARAM NAME="Base" VALUE=""><PARAM NAME="AllowScriptAccess" VALUE="always"><PARAM NAME="Scale" VALUE="NoScale"><PARAM NAME="DeviceFont" VALUE="0"><PARAM NAME="EmbedMovie" VALUE="0"><PARAM NAME="BGColor" VALUE=""><PARAM NAME="SWRemote" VALUE=""><PARAM NAME="MovieData" VALUE=""><PARAM NAME="SeamlessTabbing" VALUE="1"><PARAM NAME="Profile" VALUE="0"><PARAM NAME="ProfileAddress" VALUE=""><PARAM NAME="ProfilePort" VALUE="0"><PARAM NAME="AllowNetworking" VALUE="all">'
        str += '<param name="movie" value="' + url + '">';
        str += '<param name="src" value="' + url + '">';
        str += '<param name="quality" value="high">';
        str += '<param name="menu" value="false">';
        str += '<param name="wmode" value="transparent">';
        str += '<param name="Play" value="-1">';
        str += '<param name="allowFullScreen" value="true" />';
        str += Param || '';
        str += '<embed movie="' + url + '" src="' + url + '" wmode="Opaque" quality="high" menu="false" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"></embed>';
        str += '</object>'
        if (!id) {
            document.write(str);
        } else {
            if ($(id)) {
                document.getElementById(id.replace('#', '')).innerHTML = (str);
            } else {
                return str;
            }
        }
        return str;
    }
});
jQuery.fn.extend({
    msg: function () {
        alert(this.toString());
    },
    selectid: function (max) {
        var id = new Array();
        var j = 0;
        for (var i = 0; i < this.length; i++) {
            if (!isNaN(this[i].value)) {
                id.push(this[i].value);
                j++;
                if (j >= max) { break }
            }
        }
        return id.join(",");
    },
    images: function (img) {
        this.each(function () {
            this.onerror = function () {
                if (img != undefined && img != "") {
                    this.src = img;
                } else {
                    $(this).remove();
                }
                this.onerror = null;
            }
        });
    }
})
