﻿function LogOut() {
    pop({ text: '确定要退出吗？', title: '请确认' }, function () {
        TNN.Helpers.AjaxMethods.Login();
    }, function () { });
}

String.prototype.getQuery = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = this.substr(this.indexOf("\?") + 1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

//获取当前文件名：http://www.tnntnn.com/a/b/c.aspx 返回c
function GetPageName(url) {
    var substr = url.match(/\/([^\/]*)\.aspx/i)[1];
    return substr;
}


//判断是否为数组
function isArray(obj) {
    return (typeof obj == 'object') && obj.constructor == Array;
}
//判断是否为对象
function isObject(obj) {
    return (typeof obj == 'object') && obj.constructor == Object;
}

//string.format
//var a = "I Love {0}, and You Love {1},Where are {0}! {4}";
//alert(String.format(a, "You", "Me")); 
String.format = function () {
    if (arguments.length == 0)
        return null;

    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}


/*
等比例缩放图片
$(function(){ $("图片组所在的容器").ImageAutoSize(限制最大宽,限制最大高);});
*/
jQuery.fn.ImageAutoSize = function (width, height) {
    $("img", this).each(function () {
        var image = $(this);
        var orgWidth = image.width();
        var orgHeight = image.height();
        if (image.width() > width) {
            image.width(width);
            image.height(width / orgWidth * orgHeight);
        }
        orgWidth = image.width();
        orgHeight = image.height();
        if (image.height() > height) {
            image.height(height);
            image.width(height / orgHeight * orgWidth);
        }

        image.show();
    });
}

//格式化时间
Date.prototype.pattern = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份        
        "d+": this.getDate(), //日        
        "h+": this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, //小时        
        "H+": this.getHours(), //小时        
        "m+": this.getMinutes(), //分        
        "s+": this.getSeconds(), //秒        
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度        
        "S": this.getMilliseconds() //毫秒        
    };
    var week = {
        "0": "\u65e5",
        "1": "\u4e00",
        "2": "\u4e8c",
        "3": "\u4e09",
        "4": "\u56db",
        "5": "\u4e94",
        "6": "\u516d"
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    if (/(E+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[this.getDay() + ""]);
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

var Common = {
    //转换json的时间
    ConverDate: function (v) {
        var rx = /\/Date\((-?[0-9]+)(\+[0-9]+)?\)\//g;
        if (rx.test(v)) {
            var milli = Number(v.replace(rx, '$1'));
            if (!isNaN(milli))
                v = new Date(Number(v.replace(rx, '$1')));
            else
                throw new Error("Date format not recognised.");
        }
        return new Date(v).pattern("yyyy-MM-dd hh:mm:ss");
    },
    //获取文件名
    GetFileName: function (path) {
        var url = path;
        var tmp = new Array(); //临时变量，保存分割字符串
        tmp = url.split("/"); //按照"/"分割
        var pp = tmp[tmp.length - 1]; //获取最后一部分，即文件名和参数
        tmp = pp.split("?"); //把参数和文件名分割开
        return tmp[0];
    },
    RemoveHTML: function (strText) {
        var regEx = /<[^>]*>/g;
        return strText.replace(regEx, "");
   }
}