jQuery.extend({
    request: {
        queryString: function(val) {
            var uri = window.location.search;
            var re = new RegExp("" + val + "\=([^\&\?]*)", "ig");
            return ((uri.match(re)) ? (uri.match(re)[0].substr(val.length + 1)) : null);
        },
        queryStrings: function(uri) {
            uri = uri || window.location.search;
            var re = /\w*\=([^\&\?]*)/ig;
            var retval = [];
            while ((arr = re.exec(uri)) != null)
                retval.push(arr[0]);
            return retval;
        },
        setQuery: function(a, val1, val2) {
            var a = this.queryStrings(a);
            var retval = "";
            var seted = false;
            var re = new RegExp("^" + val1 + "\=([^\&\?]*)$", "ig");
            for (var i = 0; i < a.length; i++) {
                if (re.test(a[i])) {
                    seted = true;
                    a[i] = val1 + "=" + val2;
                }
            }
            retval = a.join("&");
            return "?" + retval + (seted ? "" : (retval ? "&" : "") + val1 + "=" + val2);
        }
    }
});
jQuery.extend({
    parseXml: function(text) {
        var xmlDoc = null;
        try //Internet Explorer   
        {
            xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
            xmlDoc.async = "false";
            xmlDoc.loadXML(text);
        }
        catch (e) {
            try //Firefox, Mozilla, Opera, etc.   
            {
                alert("Firefox");
                parser = new DOMParser();
                xmlDoc = parser.parseFromString(text, "text/xml");
            }
            catch (e) { }
        }
        return xmlDoc;
    }
});

///搜索框 灰度文本提示
jQuery.fn.searchTip = function() {
    return this.each(function() {
        var title = $(this).attr("title");
        $(this).attr("oldcss", $(this).css("color") || "");
        if (title) {
            var $this = $(this);
            $this.val(title);
            $this.css("color", "#ccc");
            $this.focus(function() {
                if ($(this).val() == $(this).attr("title")) {
                    $(this).val("");
                }
                $(this).css("color", $(this).attr("oldcss"));
            });
            $this.blur(function() {
                if (!$(this).val()) {
                    $(this).css("color", "#ccc");
                    $(this).val($(this).attr("title"));
                }
            });
            $this.bind("dragenter", function() { if (this.value == this.title) { this.value = ""; } });
            $this.bind("dragleave", function() { if (this.value == "") { this.value = this.title; } });
        }
    });
};          //end clearSearchBox
jQuery.extend(Array.prototype, {
    contain: function(p) {
        for (var ii = 0; ii < this.length; ii++) {
            if (this[ii] == p) {
                return true;
            }
        }
        return false;
    }
});
Date.prototype.format = function(mask) {
    var d = this;
    var zeroize = function(value, length) {
        if (!length) length = 2;
        value = String(value);
        for (var i = 0, zeros = ''; i < (length - value.length); i++) {
            zeros += '0';
        }
        return zeros + value;
    };

    return mask.replace(/"[^"]*"|'[^']*'|\b(?:d{1,4}|m{1,4}|yy(?:yy)?|([hHMstT])\1?|[lLZ])\b/g, function($0) {
        switch ($0) {
            case 'd': return d.getDate();
            case 'dd': return zeroize(d.getDate());
            case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][d.getDay()];
            case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()];
            case 'M': return d.getMonth() + 1;
            case 'MM': return zeroize(d.getMonth() + 1);
            case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()];
            case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()];
            case 'yy': return String(d.getFullYear()).substr(2);
            case 'yyyy': return d.getFullYear();
            case 'h': return d.getHours() % 12 || 12;
            case 'hh': return zeroize(d.getHours() % 12 || 12);
            case 'H': return d.getHours();
            case 'HH': return zeroize(d.getHours());
            case 'm': return d.getMinutes();
            case 'mm': return zeroize(d.getMinutes());
            case 's': return d.getSeconds();
            case 'ss': return zeroize(d.getSeconds());
            case 'l': return zeroize(d.getMilliseconds(), 3);
            case 'L': var m = d.getMilliseconds();
                if (m > 99) m = Math.round(m / 10);
                return zeroize(m);
            case 'tt': return d.getHours() < 12 ? 'am' : 'pm';
            case 'TT': return d.getHours() < 12 ? 'AM' : 'PM';
            case 'Z': return d.toUTCString().match(/[A-Z]+$/);
                // Return quoted strings with the surrounding quotes removed   
            default: return $0.substr(1, $0.length - 2);
        }
    });

};
Date.prototype.addDays = function(d) {
    this.setDate(this.getDate() + d);
};
Date.prototype.addWeeks = function(w) {
    this.addDays(w * 7);
};
Date.prototype.addMonths = function(m) {
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);
    if (this.getDate() < d)
        this.setDate(0);
};
Date.prototype.addYears = function(y) {
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);
    if (m < this.getMonth()) {
        this.setDate(0);
    }
};
//测试 var now = new Date(); now.addDays(1);//加减日期操作 alert(now.Format("yyyy-MM-dd")); 
Date.prototype.dateDiff = function(interval, endTime) {
    switch (interval) {
        case "s": //計算秒差 
            return parseInt((endTime - this) / 1000);
        case "n": //計算分差 
            return parseInt((endTime - this) / 60000);
        case "h": //計算時差 
            return parseInt((endTime - this) / 3600000);
        case "d": //計算日差 
            return parseInt((endTime - this) / 86400000);
        case "w": //計算週差 
            return parseInt((endTime - this) / (86400000 * 7));
        case "m": //計算月差 
            return (endTime.getMonth() + 1) + ((endTime.getFullYear() - this.getFullYear()) * 12) - (this.getMonth() + 1);
        case "y": //計算年差 
            return endTime.getFullYear() - this.getFullYear();
        default: //輸入有誤 
            return undefined;
    }
} 


