///////////////////////////////////
// 基本的JAVASCRIPT函数或类型
/////////////////////////////////////////////


//string方法:找?号后的部分
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;
}

//求含中文的字串长度
String.prototype.cn_length = function() {
    var i, sum;
    sum = 0;
    for (i = 0; i < this.length; i++) {
        sum++;
        if (this.charCodeAt(i) > 255) {
            sum++;
        }
    }
    return sum;
}

//求含中文字串的子串
String.prototype.cn_substring = function(len) {
    var a = 0;
    var tmp = "";
    for (var i = 0; i < len; i++) {
        if (this.charCodeAt(i) > 255) {
            a += 2;
        }
        else {
            a++;
        }

        if (a > len) {
            return tmp;
        }
        tmp += this.charAt(i);
    }
    return tmp;
}


//返回某个ID的对象
function $(s) {
    if (document.getElementById) {
        return document.getElementById(s);
    }
    else if (document.getElementByName) {
        return document.getElementByName(s);
    }
    else {
        return document.all[s];
    }
}

function $$(s) {
    return document.frames ? document.frames[s] : $(s).contentWindow;
}

//在某个DIV等ID中,显示HTML内容
function echo(ObjId, html) {
    $(ObjId).innerHTML = html;
}


function isNull(_sVal) {
    return (_sVal === "" || _sVal == null || _sVal == "undefined");
}

function dw(s) {
    document.write(s);
}


function hidden(obj) {
    obj.style.display = (obj.style.display == 'none') ? '' : 'none';
}


function getXY(obj) {
    var o = new Object();
    o.left = 0;
    o.top = 0;
    o.right = 0;
    o.bottom = 0;
    var oWidth = obj.offsetWidth;
    var oHeight = obj.offsetHeight;
    while (obj) {
        o.left += obj.offsetLeft;
        o.top += obj.offsetTop;
        obj = obj.offsetParent;
    }
    o.right = o.left + oWidth;
    o.bottom = o.top + oHeight;
    return o;
}

///////////////////////////////////////
//用服务器交互的类
//LiuBing原创于2006年
//2009年2月进行第三次修改
/////////////////////////////////////////
function jRequest() {
    var _this = this;
    var _CreateRequest = function() {
        var http_request = false;
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        }
        else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) { }
            }
        }

        if (!http_request) {
            return false;
        }
        else {
            return http_request;
        }
    }

    var _CallbackForGetTextA = function(_myRequest, OutObj, IsRemind) {  //for echo
        return function() {
            if (_myRequest.readyState == 4)   //表示已经数据返回
            {
                if (_myRequest.status == 200)  //正常读取
                {
                    var Ree = _myRequest.responseText;   //responseBody;
                    echo(OutObj, Ree);
                }
                else {
                    if (IsRemind) echo(OutObj, "加载失败!");
                }
            }
        }
    }

    var _CallbackForGetTextB = function(_myRequest, uFunc, uFuncArg) {  //for call function
        return function() {
            var res = { responseXML: null, responseText: null, responseBody: null };
            if (_myRequest.readyState == 4) {
                if (_myRequest.status == 200) {
                    res.responseText = _myRequest.responseText;    //文本
                    res.responseXML = _myRequest.responseXML;      //XML文本
                    res.responseBody = _myRequest.responseBody;    //二进制数据
                    eval(uFunc).apply(res, uFuncArg);
                }
                else {
                    eval(uFunc).apply(res, uFuncArg);
                }
            }
        }
    }


    ////////////////////////////////////////
    //  共用方法
    /////////////////////////////////////////////////////////////


    /// 同步读取服务器数据,返回字符
    /////////////////////////////////////
    _this.GetTextAsServer = function(xUrl) {
        if (xUrl == null || xUrl == "") return "";
        _myRequest = _CreateRequest();
        if (!_myRequest) return "";

        _myRequest.open("GET", xUrl, false);

        try {
            _myRequest.send();
        }
        catch (e) { }

        if (_myRequest.readyState == 4)   //表示已经数据返回
        {
            if (_myRequest.status == 200)  //正常读取
            {
                var Ree = _myRequest.responseText;
                return Ree;
            }
            else {
                return "";
            }
        }
    }


    //向服务器以POST方式提交数据并等候返回的内容（同步）
    /////////////////////////////////////////////////////
    _this.PostTextToServer = function(toUrl, txt) {

        if (isNull(toUrl)) return "";
        _myRequest = _CreateRequest();
        if (!_myRequest) return "";

        _myRequest.open("POST", toUrl, false);
        _myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        _myRequest.setRequestHeader("Content-Length", txt.length);
        //_myRequest.setRequestHeader("Connection", "close");

        try {
            _myRequest.send(txt);
        }
        catch (e) { }

        if (_myRequest.readyState == 4)    //表示已经数据返回
        {
            if (_myRequest.status == 200)  //正常读取
            {
                var Ree = _myRequest.responseText;
                return Ree;
            }
            else {
                return "";
            }
        }
    }


    //异步读取并将返回的数据填充到指定的位置
    //参数:读取的网址,填充内容的对象,是否显示提示
    //////////////////////////////////////////////////
    _this.AsyncGetHtml = function(url, obj_Id, IsRemind) {
        _myRequest = _CreateRequest();
        if (!_myRequest) return;
        if (IsRemind == null || IsRemind == "undefined" || IsRemind == "" || typeof (IsRemind) == "undefined")
            IsRemind = false;
        if (IsRemind)
            echo(obj_Id, "正在加载..");
        var RequestCallback = _CallbackForGetTextA(_myRequest, obj_Id, IsRemind);
        _myRequest.onreadystatechange = RequestCallback;
        _myRequest.open("GET", url, true);   //true表示异步
        _myRequest.send(null);

    }


    // 异步调用,返数数据后把数据传给用户指定的函数并执行该函数
    // 注:func是被调函数名,是不加括号的函数名字串,字串要加引号
    // funcArg是参数数组,数组用[]号表示,如果没有参数可以不填
    // 被调函数用this.responseXML和this.responseText取出数据
    //////////////////////////////////////////////////////////////
    _this.AsyncGetHtmlToFunc = function(url, func, funcArg) {
        _myRequest = _CreateRequest();
        if (!_myRequest) return;
        if (funcArg == null || typeof (funcArg) == "undefined" || funcArg == "undefined" || funcArg == "") funcArg = [];
        var RequestCallback = _CallbackForGetTextB(_myRequest, func, funcArg);
        _myRequest.onreadystatechange = RequestCallback;
        _myRequest.open("GET", url, true);   //true表示异步
        _myRequest.send(null);
    }

}



//////////////////////////////
// 其它备用函数
////////////////////////////////////////////////////////////

//-- 图片加载时自动缩略
function imgLoad(obj, maxwid) {
    var img, wid, j;
    if (!maxwid) maxwid = 640;

    for (j = 0; j < 100; j++) {
        img = new Image;
        img.src = obj.src;
        wid = img.width;
        if (wid > 0) break;
    }

    if (wid > maxwid) {
        wid = maxwid;
    }
    if (wid) {
        obj.width = wid;
    }
}

//调入某个脚本文件
function LoadScript(url, language, theType) {
    var head = document.getElementsByTagName("HEAD").item(0);
    var script = document.createElement("script");
    script.src = url;
    script.language = language;
    script.type = theType;
    head.appendChild(script);
}
//LoadScript("index.js","javascript","text/javascript");

