﻿// JScript 文件

//    var xmlHttp;
function CallBackObject() {
    this.xmlHttp = this.GetCallBackObject();
}
CallBackObject.prototype.GetCallBackObject = function() {
    var xmlHttp = false;
    /*@cc_on
    /*@if(@_jscript_version >=5)
    {

        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                xmlHttp = false;
            }
        }
    }
    /*@else
    xmlHttp=false;
  @end
    @*/

    if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            xmlHttp = false;
        }
    }

    return xmlHttp;
}

CallBackObject.prototype.DoCallBack = function(url, types, tongbu) {
    if (this.xmlHttp) {
        if (this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0) {
            var othis = this;

            this.xmlHttp.open(types, url, tongbu); //一个页面用了多个AJAX的时，同步与异步最好能统一
            var post = null;
            if (types.toLowerCase() == "post") {
                this.xmlHttp.setRequestHeader("content-length", url.length); //post提交设置项
                this.xmlHttp.setRequestHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); //post提交设置项
                if (url.lastIndexOf("?") != -1) {
                    post = url.substring(url.lastIndexOf("?") + 1); //获取要post的数据
                    post = encodeURI(post);
                }
            }
            this.xmlHttp.onreadystatechange = function() {
                othis.ReadyStateChange();
            }
            this.xmlHttp.send(post);
        }

    }
}
CallBackObject.prototype.AbortCallBack = function() {
    if (this.xmlHttp) {
        this.xmlHttp.abort();
        //alert("请求关闭了");
    }
}
CallBackObject.prototype.OnLoading = function() {
    //alert("请求正在加载");
}

CallBackObject.prototype.OnLoaded = function() {
    //alert("请求已加载");
}

CallBackObject.prototype.OnInteractive = function() {
    //  alert("交互中");
}

CallBackObject.prototype.OnAbort = function() {
    //alert("取消中:");
}

CallBackObject.prototype.OnComplete = function(responseText, responseXML, element) {
    // alert("加载完成");
}

CallBackObject.prototype.OnError = function(status, statusText) {
    alert("出错了,请与管理员联系" + statusText + status);
}

CallBackObject.prototype.ReadyStateChange = function() {
    if (this.xmlHttp.readyState == 1) {
        this.OnLoading();
    }
    if (this.xmlHttp.readyState == 2) {
        this.OnLoaded();
    }
    if (this.xmlHttp.readyState == 3) {
        this.OnInteractive();
    }
    if (this.xmlHttp.readyState == 4) {
        if (this.xmlHttp.status == 0) {
            this.OnAbort();
        }
        else if (this.xmlHttp.status == 200 && this.xmlHttp.statusText == "OK") {
            this.OnComplete(this.xmlHttp.responseText, this.xmlHttp.responseXML);
        }
        else {
            this.OnError(this.xmlHttp.status, this.xmlHttp.statusText, this.xmlHttp.responseText);
        }
    }
}

String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.Insert = function(index, str) {
    return this.substring(0, index) + str + this.substr(index);
}

function getBrowserType() {
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] : (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] : (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] : (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0;
    return Sys;
}

function isDate(data) {
    var reg = /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|(1[0-9])|(2[0-3]))\:(([0-5][0-9])|([0-9]))(((\s)|(\:(([0-5][0-9])|([0-9]))))?)))?$/;
    if (reg.test(data))
        return true;
    return false;
}
function isEmail(eMail) {
    var reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    if (reg.test(eMail))
        return true;
    return false;
}
function isNumber(num) {
    var reg = /^\d+/;
    if (reg.test(num))
        return true;
    return false;
}
function isHanZhi(message) {
    var reg = /[u4e00-u9fa5]/;
    if (reg.test(message))
        return true;
    return false;
}
function isEnAndNumber(message) {
    var reg = /^[A-Za-z0-9_-]+$/;
    if (reg.test(message))
        return true;
    return false;
}
function $(obj) {
    if (typeof obj == "string")
        return document.getElementById(obj);
    return obj;
}

function Ajax(url, callback_f, submittype, errorf, syn) {
    if (submittype.toLowerCase() != "get" && submittype.toLowerCase() != "post") {
        submittype = "get";
    }
    var cbo = new CallBackObject();
    cbo.OnComplete = callback_f;    //请求成功后执行的方法
    cbo.OnError = errorf;
    cbo.DoCallBack(url, submittype, syn); //true为不同步,false为同步
}

/*
callback_f method template
*/
function result(responseText, responseXML) {
    alert(responseText);
}

/*js 跨域*/
function GetResultByJS(script_e, url, callback_f) {
    if (typeof script_e == "string") {
        script_e = document.getElementById("script_e");
    }
    if (!script_e) {
        document.body.appendChild(script_e = document.createElement("script"));
    }
    if (!callback_f) {
        callback_f = function() { };
    }
    script_e.onload = function() {
        callback_f();
    }
    script_e.onreadystatechange = function() {
        if (script_e.readyState == "loaded") {
            callback_f();
        }
    }

    script_e.src = url;
}

function GetSrcEvent() {
    var event = window.event || arguments.callee.caller.arguments[0];
    return event;
}
function getSrcElement() {
    var e = window.event || arguments.callee.caller.arguments[0];
    if (e == null || e == undefined)
        return null;
    var element = e.srcElement || e.target;
    return element;
}

Array.prototype.inArray = function(value) {
    var i; for (i = 0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] == value) {
            return true;
        }
    } return false;
}
//html page get querystring
//var act_value = GetQueryString("act");
function GetQueryString() {
    var Url = top.window.location.href;
    var u, g, StrBack = '';
    if (arguments[arguments.length - 1] == "#")
        u = Url.split("#");
    else
        u = Url.split("?");
    if (u.length == 1) g = '';
    else g = u[1];
    if (g != '') {
        gg = g.split("&");
        var MaxI = gg.length;
        str = arguments[0] + "=";
        for (i = 0; i < MaxI; i++) {
            if (gg[i].indexOf(str) == 0) {
                StrBack = gg[i].replace(str, "");
                break;
            }
        }
    }
    return StrBack;
}


function getObjectfromDataTable(tables) {
    var keyvalue = {};
    for (var i = 0; i < tables.childNodes.length; i++) {
        if (tables.childNodes[i].nodeName == "#text")
            continue;  //FF 中特有 nodeName == "#text"

        var tagName = tables.childNodes[i].tagName;
        var value;
        if (tables.childNodes[i].firstChild) {
            value = tables.childNodes[i].firstChild.nodeValue;
        }
        else {
            value = tables.childNodes[i].nodeValue;
        }
        keyvalue[tagName] = value;
    }
    return keyvalue;
}

//获取window的尺寸
function getWindowSize() {

    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return ([myWidth, myHeight]);
}

function getOffsetLeft() {
    var left = 0;
    var offsetParent = getSrcElement();
    while (offsetParent != null && offsetParent != document.body) {
        left += offsetParent.offsetLeft;
        offsetParent = offsetParent.offsetParent;
    }
    return left;
}
function GetAbsoluteLocationEx(element) {
    if (arguments.length != 1 || element == null) {
        return null;
    }
    var elmt = element;
    var offsetTop = elmt.offsetTop;
    var offsetLeft = elmt.offsetLeft;
    var offsetWidth = elmt.offsetWidth;
    var offsetHeight = elmt.offsetHeight;
    while (elmt = elmt.offsetParent) {
        // add this judge 
        if (elmt.style.position == 'absolute' || elmt.style.position == 'relative'
            || (elmt.style.overflow != 'visible' && elmt.style.overflow != '')) {
            break;
        }
        offsetTop += elmt.offsetTop;
        offsetLeft += elmt.offsetLeft;
    }
    return { absoluteTop: offsetTop, absoluteLeft: offsetLeft,
        offsetWidth: offsetWidth, offsetHeight: offsetHeight
    };
}


function forEach(object, block, context, fn) {
    if (object == null) return;
    if (!fn) {
        if (typeof object == "function" && object.call) {
            //遍历普通对象
            fn = Function;
        } else if (typeof object.forEach == "function" && object.forEach != arguments.callee) {
            //如果目标已经实现了forEach方法，则使用它自己的forEach方法（如标准游览器的Array对象）
            object.forEach(block, context);
            return;
        } else if (typeof object.length == "number") {
            // 如果是类数组对象或IE的数组对象
            _Array_forEach(object, block, context);
            return;
        }
    }
    _Function_forEach(fn || Object, object, block, context);
};

function _Array_forEach(array, block, context) {
    if (array == null) return;
    var i = 0, length = array.length;
    if (typeof array == "string")
        array = array.split("");
    for (; i < length; i++) {
        block.call(context, array[i], i, array);
    }
};


_Function_forEach = function(fn, object, block, context) {
    // 这里的fn恒为Function
    for (var key in object) {
        //只遍历本地属性
        if (object.hasOwnProperty(key)) {
            //相当于  block(object[key], key)
            block.call(context, object[key], key, object);
        }
    }
};


function print(el, index) {
    alert(index + "  :  " + el)
}

//         forEach({ a: "aa", b: "bb", c: "cc" }, print);
//         forEach("司徒正美", print);
//         forEach(document.styleSheets, function (el) {
//             if (el.href) alert(el.href)
//         });
