﻿var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
function $(name) {
    return document.getElementById(name) == "undefined" ? document.getElementsByName(name)[0] : document.getElementById(name);
}
function IeTest() {
    return (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body;
}
//取得客户端信息
function GetClientInfo(x) {
    switch (x) {
        case "w": return ns ? innerWidth : IeTest().clientWidth;
        case "h": return ns ? innerHeight : IeTest().clientHeight;
        case "x": return ns ? pageXOffset : IeTest().scrollLeft;
        case "y": return ns ? pageYOffset : IeTest().scrollTop;
    }
}
//显示验证码
function getCrcCode() {
    $("MyCrcCode").innerHTML = "<img src='/GetCode.aspx?time=" + Math.random() + "' style='cursor:pointer;' id='getcodeimg' border='0' alt='验证码，看不清楚？点击更换。' />";
}
//标签及内容切换
function ShowInfo(x, _c, _x) {
    for (var i = 1; i <= _x; i++) {
        $("t" + _c + "_" + i).className = "";
        $("t" + _c + "_info_" + i).style.display = "none";
    }
    $("t" + _c + "_" + x).className = "select";
    $("t" + _c + "_info_" + x).style.display = "block";
}
function picsize2(obj, MaxWidth, MaxHeight) {
    img = new Image();
    img.src = obj.src;
    if (img.width > MaxWidth) {
        obj.width = MaxWidth;
    }
}
//按长宽比例缩放图片
function picsize(obj, MaxWidth, MaxHeight) {
    img = new Image();
    img.src = obj.src;
    if (img.width > MaxWidth && img.height > MaxHeight) {
        if (img.width / img.height > MaxWidth / MaxHeight) {
            obj.height = MaxWidth * img.height / img.width;
            obj.width = MaxWidth;
        }
        else {
            obj.width = MaxHeight * img.width / img.height;
            obj.height = MaxHeight;
        }
    }
    else if (img.width > MaxWidth) {
        obj.height = MaxWidth * img.height / img.width;
        obj.width = MaxWidth;
    }
    else if (img.height > MaxHeight) {
        obj.width = MaxHeight * img.width / img.height;
        obj.height = MaxHeight;
    }
    else {
        obj.width = img.width;
        obj.height = img.height;
    }
}
//缩放文章中的图片
function checkstatus() {
//    for (i = document.images.length - 1; i > 1; i--) {
//        var img = document.images[i];
//        if (img.width > 560) {
//            img.outerHTML = '<a href="' + img.src + '" target="_blank" title="auto size,click to view big image"><img src="' + img.src + '" border="0px" width="560px" /></a>';
//        }
//    }
}
//创建XML对象
function CreateXMLHTTP() {
    var xmlHttp = false;
    /*@cc_on@*/
    /*@if (@_jscript_version >= 5)
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) {
            xmlHttp = false;
        }
    }
    @end@*/
    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}
function PostInfo()//url,callback,arg1,value1,arg2,value2
{
    if (arguments.length >= 2) {
        var xmlhttp = CreateXMLHTTP();
        var postUrl = arguments[0];
        var fn = arguments[1];
        var postStr = "";
        if (arguments.length >= 3) {
            //postStr += "?";
            for (var i = 2; i < arguments.length; i = i + 2) {
                if (i > 2) postStr += "&";
                postStr += arguments[i] + "=" + arguments[i + 1];
            }
        }
        xmlhttp.open("POST", postUrl, true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        xmlhttp.setRequestHeader("Content-Length", postStr.length);
        xmlhttp.setRequestHeader("Connection", "close");
        xmlhttp.onreadystatechange = function PostReady() {
            if (xmlhttp.readyState == 4) if (xmlhttp.status == 200) return fn(xmlhttp.responseText);
            else return fn("error<a href='javascript:CloseObj();'>Close</a>");
        };
        xmlhttp.send(postStr);
        return;
    }
}
function OpenWindowAndSetValue(Url, Width, Height, WindowObj, SetObj) {
    var ReturnStr = showModalDialog(Url, WindowObj, 'dialogWidth:' + Width + 'pt;dialogHeight:' + Height + 'pt;status:no;help:no;scroll:no;');
    if (ReturnStr != '') SetObj.value = ReturnStr;
    return ReturnStr;
}
function OpenWindow(url, winName, width, height) {
    var styleWin = window.open(url, winName, "width=" + width + ",height=" + height + ",status=0,toolbars=0");
    if (styleWin) {
        styleWin.focus();
    }
    else {
        alert("操作失败，请关掉弹出窗口拦截程序后再继续该操作！");
        return;
    }
}
//日期选择对话框
function open_date_cell(ctrlobj, date_file, fullTime) {
    showx = event.screenX - event.offsetX - 4 - 10;
    // + deltaX;
    showy = event.screenY - event.offsetY - 168;
    // + deltaY;
    newWINwidth = 210 + 4 + 18;
    //alert(ctrlobj.name);
    retval = window.showModalDialog(date_file, "", "dialogWidth:197px; dialogHeight:210px; dialogLeft:" + showx + "px; dialogTop:" + showy + "px; status:no; directories:yes;scrollbars:no;Resizable=no; ");
    if (retval != null) {
        if (fullTime) {
            //add Hour & Minute
            var _today = new Date();
            var _Hours = _today.getHours();
            var _Minutes = _today.getMinutes();
            _Hours = parseInt(_Hours) < 10 ? "0" + _Hours : _Hours;
            _Minutes = parseInt(_Minutes) < 10 ? "0" + _Minutes : _Minutes;
            retval += " " + _Hours + ":" + _Minutes + ":" + "00";
        }
        ctrlobj.value = retval;
    }
    else {
        alert("canceled");
    }
}
//String扩展属性&方法
//以字节方式计算长度
String.prototype.lenB = function() {
    var arr = this.match(/[^\x00-\xff]/ig);
    return this.length + (arr == null ? 0 : arr.length);
}
//从左取n个字符或字节,mode=true时为字节模式
String.prototype.left = function(num, mode) {
    if (!/\d+/.test(num)) return (this);
    var str = this.substr(0, num);
    if (!mode) return str;
    var n = str.lenB() - str.length;
    num = num - parseInt(n / 2);
    return this.substr(0, num);
}
//从右取n个字符或字节,mode=true时为字节模式
String.prototype.right = function(num, mode) {
    if (!/\d+/.test(num)) return (this);
    var str = this.substr(this.length - num);
    if (!mode) return str;
    var n = str.lenB() - str.length;
    num = num - parseInt(n / 2);
    return this.substr(this.length - num);
}
//合并多个空白为一个空白 
String.prototype.resetBlank = function() {
    return this.replace(/s+/g, " ");
}
//去掉前面空格
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
//去掉前空格
String.prototype.lTrim = function() {
    return this.replace(/(^\s*)/g, "");
}
//去掉后空格
String.prototype.rTrim = function() {
    return this.replace(/(\s*$)/g, "");
}
//获取字符数组 
String.prototype.toCharArray = function() {
    return this.split("");
}
//获取N个相同的字符串 
String.prototype.repeat = function(num) {
    var tmpArr = [];
    for (var i = 0; i < num; i++) tmpArr.push(this);
    return tmpArr.join("");
}
//逆序 
String.prototype.reverse = function() {
    return this.split("").reverse().join("");
}
//是否为数字 
String.prototype.isNumeric = function() {
    var tmpFloat = parseFloat(this);
    if (isNaN(tmpFloat)) return false;
    var tmpLen = this.length - tmpFloat.toString().length;
    return tmpFloat + "0".Repeat(tmpLen) == this;
}
//是否为整数 
String.prototype.ssInt = function() {
    if (this == "NaN") return false;
    return this == parseInt(this).toString();
}
//Unicode转化，把所有内容转化为&#xxx的形式
String.prototype.ascW = function() {
    var strText = "";
    for (var i = 0; i < this.length; i++) strText += "&#" + this.charCodeAt(i) + ";";
    return strText;
}
/*
utf-8编码适用 开始
中文
*/
//计算字符串长度
String.prototype.ulength = function() {
    var c, b = 0, l = this.length;
    while (l) {
        c = this.charCodeAt(--l);
        b += (c < 128) ? 1 : ((c < 2048) ? 2 : ((c < 65536) ? 3 : 4));
    };
    return b;
}
//取出指定位置的子字符串 
String.prototype.usubstring = function(start, end) {
    var s = '', p = 0;
    for (i = 0; i < this.length; i++) {
        if (start <= p && p < end) {
            s += this.charAt(i);
        }
        c = this.charCodeAt(i);
        p += c < 128 ? 1 : (c < 2048 ? 2 : (c < 65536 ? 3 : 4));
    }
    return s;
}
//从指定位置开始的指定长度的子字符串
String.prototype.usubstr = function(start, l) {
    var s = '', p = 0;
    for (i = 0; i < this.length; i++) {
        if (start <= p && p < start + l) {
            s += this.charAt(i);
        }
        c = this.charCodeAt(i);
        p += c < 128 ? 1 : (c < 2048 ? 2 : (c < 65536 ? 3 : 4));
    }
    return s;
}
//返回 String 对象内第一次出现子字符串的字符位置
String.prototype.uindexOf = function(str, starIndex) {
    var p = 0, l = str.length;
    for (i = 0; i < this.length; i++) {
        if (this.substring(i, i + l) == str && (starIndex && p >= starIndex)) {
            return p;
        }
        c = this.charCodeAt(i);
        p += c < 128 ? 1 : (c < 2048 ? 2 : (c < 65536 ? 3 : 4));
    }
    return false;
}
/* utf-8编码适用 结束 */
//格式化字符串
String.prototype.format = function()//str.format("0","1","2","3","4")
{
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function(m, i) { return args[i]; });
}
String.format = function()//String.format(str,"you","me","2","3","4")
{
    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;
}
//Array 的扩展方法
//自定义数组删除
Array.prototype.del = function(n) {
    if (n < 0) return this;
    return this.slice(0, n).concat(this.slice(n + 1, this.length));
}
//数组洗牌
Array.prototype.random = function() {
    var nr = [], me = this, t;
    while (me.length > 0) {
        nr[nr.length] = me[t = Math.floor(Math.random() * me.length)];
        me = me.del(t);
    }
    return nr;
}
//数字数组排序
Array.prototype.sortNum = function(f) {
    if (!f) f = 0;
    if (f == 1) return this.sort(function(a, b) {
        return b - a;
    }
   );
    return this.sort(function(a, b) {
        return a - b;
    }
   );
}
//获得数字数组的最大项
Array.prototype.getMax = function() {
    return this.sortNum(1)[0];
}
//获得数字数组的最小项
Array.prototype.getMin = function() {
    return this.sortNum(0)[0];
}
//数组第一次出现指定元素值的位置
Array.prototype.indexOf = function(o) {
    for (var i = 0; i < this.length; i++) if (this[i] == o) return i;
    return -1;
}
//移除数组中重复的项
Array.prototype.removeRepeat = function() {
    this.sort();
    var rs = [];
    var cr = false;
    for (var i = 0; i < this.length; i++) {
        if (!cr) cr = this[i];
        else if (cr == this[i]) rs[rs.length] = i;
        else cr = this[i];
    }
    var re = this;
    for (var i = rs.length - 1; i >= 0; i--) re = re.del(rs[i]);
    return re;
}