﻿var IsIE = function() {
    return /MSIE (\d+\.\d+);/.test(navigator.userAgent);
}

Array.prototype.add = function(item, i) {
    if (i)
        this.splice(i, 0, item);
    else
        this[this.length] = item;
}
Array.prototype.del = function(i, num) {
    if (num)
        this.splice(i, num);
    else
        this.splice(i, 1);
}
// 删除数组重复项
Array.prototype.distinct = function() {
    var ret = [],
   resultArr = [],
        returnArr = [],
        i = 1,
        origLen = this.length,
        resultLen;
    function include(arr, value) {
        for (var i = 0, n = arr.length; i < n; ++i) {
            if (arr[i] === value) {
                return true;
            }
        }
        return false;
    }
    resultArr.push(this[0]);
    for (i; i < origLen; ++i) {
        if (!include(resultArr, this[i])) {
            resultArr.push(this[i]);
        } else {
            if (!include(returnArr, this[i])) {
                returnArr.push(this[i]);
            }
        }
    }
    ret[0] = resultArr;
    ret[1] = returnArr;
    return ret;
}
Array.prototype.isRepeat = function() {
    var ary = this.sort();
    for (var i = 0; i < ary.length - 1; i++) {
        if (ary[i] == ary[i + 1]) {
            return true;
        }
    }
    return false;
}

// onkeypress=DoBtn(event, 'btnID')
function DoBtn(evt, btnID) {
    var _event = evt || window.event;
    if (_event.keyCode == 13 && !(_event.srcElement && (_event.srcElement.tagName.toLowerCase() == "textarea"))) {
        $("#" + btnID).click();
    }
    return false;
}
// onkeypress=Keyress(event, 'btnID')
function Keyress(e, btnID) {
    e = e || event;
    if (e.keyCode == 13)
        $("#" + btnID).click();
}

function CheckBoxAll(check, name) {
    var boxs = document.getElementsByName(name);
    for (var i = 0; i < boxs.length; i++) {
        boxs[i].checked = check;
    }
}
function GetBoxValue(name) {
    var value = '';
    var boxs = document.getElementsByName(name);
    for (var i = 0; i < boxs.length; i++) {
        if (boxs[i].checked)
            value += boxs[i].value + ',';
    }
    return value;
}
function GetBoxAbleValue(name) {
    var value = '';
    var boxs = document.getElementsByName(name);
    for (var i = 0; i < boxs.length; i++) {
        if (boxs[i].checked && boxs[i].disabled == false)
            value += boxs[i].value + ',';
    }
    return value;
}
function SetCheckBox(name, value) {
    var items = document.getElementsByName(name);
    for (var i = 0; i < items.length; i++) {
        var bValue = items(i).value;
        if (value.indexOf(bValue + ',') >= 0) {
            items(i).checked = true;
        }
    }
}
function GetRadioValue(name) {
    var value = '';
    var radios = document.getElementsByName(name);
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked)
            value = radios[i].value;
    }
    return value;
}
function GetRadioAbleValue(name) {
    var value = '';
    var radios = document.getElementsByName(name);
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked && radios[i].disabled == false)
            value = radios[i].value;
    }
    return value;
}
function SetRadio(name, value) {
    var items = document.getElementsByName(name);
    for (var i = 0; i < items.length; i++) {
        var iValue = items(i).value;
        if (iValue == value) {
            items(i).checked = true;
        }
    }
}


// js版本的request.querystring
// 调用方法： URLParams["id"]
var URLParams = new Array();
var aParams = document.location.search.substr(1).split('&');
for (i = 0; i < aParams.length; i++) {
    var aParam = aParams[i].split('=');
    URLParams[aParam[0]] = aParam[1];
}


// 获取坐标
function GetPos() {
    var e = e || window.event;
    var x = e.pageX || (e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft))
    var y = e.pageY || (e.clientY + (document.documentElement.scrollTop || document.body.scrollTop));

    return { left: x, top: y };
}

// 获取窗口尺寸
function GetWindowSize() {
    //获取窗口宽度
    if (window.innerWidth) {
        winWidth = window.innerWidth;
    }
    else if ((document.body) && (document.body.clientWidth)) {
        winWidth = document.body.clientWidth;
    }

    //获取窗口高度
    if (window.innerHeight) {
        winHeight = window.innerHeight;
    }
    else if ((document.body) && (document.body.clientHeight)) {
        winHeight = document.body.clientHeight;
    }

    //通过深入Document内部对body进行检测，获取窗口大小
    if (document.documentElement && document.documentElement.clientHeight && document.documentElement.clientWidth) {
        winHeight = document.documentElement.clientHeight;
        winWidth = document.documentElement.clientWidth;
    }

    //结果输出至两个文本框
    return { width: winWidth, height: winHeight }
}

function GetPageSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {   // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    }
    else {
        pageHeight = yScroll;
    }

    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    }
    else {
        pageWidth = xScroll;
    }

    // arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
    // return arrayPageSize;
    return { pageW: pageWidth, pageH: pageHeight, winW: windowWidth, winH: windowHeight };
}


function GetScrollTop() {
    var top;
    if (window.pageYOffset != undefined) {
        top = window.pageYOffset;
    }
    else if (document.compatMode != undefined && document.compatMode != 'BackCompat') {
        top = document.documentElement.scrollTop;
    }
    else if (document.body != undefined) {
        top = document.body.scrollTop;
    }
    return top;
}

function PhotoSrc(src, def) {
    if (src != null && src.length > 0) {
        return src;
    }
    else
        return def;
}


// 图片按比例缩放
// 调用：<img src="图片" onload="javascript:DrawImage(this, iw, ih)">

function DrawImage(ImgD, iw, ih) {
    var image = new Image();
    var iwidth = iw; //定义允许图片宽度
    var iheight = ih; //定义允许图片高度
    image.src = ImgD.src;

    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }

            //ImgD.alt=image.width+"×"+image.height;
        }
        else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            }
            else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }

            //ImgD.alt=image.width + "×" + image.height;
        }
    }
}

// 获取文件名称
var GetFileName = function(src) {
    var str = src.split('/');
    var length = str.length;
    if (length > 0) {
        return str[length - 1];
    }
    return "";
}
// 获取上传图片的微缩图名称
var GetThumFileName = function(src) {
    var fName = GetFileName(src);
    if (fName != "") {
        return src.replace(fName, "t_" + fName);
    }
    return "";
}



Number.prototype.toFixed = function(len) {
    var add = 0;
    var s, temp;
    var s1 = this + "";
    var start = s1.indexOf(".");
    if (s1.substr(start + len + 1, 1) >= 5) add = 1;
    var temp = Math.pow(10, len);
    s = Math.floor(this * temp) + add;
    return s / temp;
}



var SetSelected = function(id, value) {
    var count = $('#' + id + ' option').length;
    for (var i = 0; i < count; i++) {
        if ($("#" + id).get(0).options[i].value == value) {
            $("#" + id).get(0).options[i].selected = true;

            break;
        }
    }
}

// 设置菜单选中项加粗
function SelectMenu(className) {
    var items = document.getElementById("Menu").childNodes[0].childNodes;
    var path = location.href.toLowerCase();
    for (var i = 0; i < items.length; i++) {
        var url = items[i].childNodes[0].href.toLowerCase();
        if (url.indexOf("#") >= 0) {
            url = url.substring(url.indexOf("#"), url.length);
            //alert(url);
        }
        if (path.indexOf(url) >= 0) {
            //items[i].childNodes[0].style.fontWeight = "bold";
            items[i].childNodes[0].className = className;
        }
    }
}


function NewGuid() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid;
}

function GoToUrl(url, target) {
    if (IsIE()) {
        var refLink = document.createElement('a');
        if (target.toLowerCase() == "_blank".toLowerCase()) {
            refLink.setAttribute("target", "_blank");
        }
        refLink.href = url;
        document.body.appendChild(refLink);
        refLink.click();
    }
    else {
        location.href = url;
    }
}

function Focus(elemID, value) {
    var eValue = $("#" + elemID).val().Trim();
    if (eValue == value) {
        $("#" + elemID).val("");
    }
}
function Blur(elemID, value) {
    var eValue = $("#" + elemID).val().Trim();
    if (eValue.length <= 0) {
        $("#" + elemID).val(value);
    }
}

/**************************************** 添加到收藏夹 ***************************************************/

function AddToFav(title, url) {
    if (IsIE()) {
        window.external.AddFavorite(url, title);
    }
    else {
        if (window.sidebar) { // Mozilla Firefox
            window.sidebar.addPanel(title, url, "");
        }
        else if (window.opera) { // Opera 7+
            alert('非常抱歉，该操作不支持当前浏览器，请使用ctrl+d进行手动添加，谢谢！');
        }
        else {
            alert('非常抱歉，该操作不支持当前浏览器，请使用ctrl+d进行手动添加，谢谢！');
        }
    }
}

/**************************************** 设为首页 ***************************************************/

function SetHomePage(obj, vrl) {
    try {
        obj.style.behavior = 'url(#default#homepage)'; obj.setHomePage(vrl);
    }
    catch (e) {
        if (window.netscape) {
            try {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e) {
                alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
            }
            var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
            prefs.setCharPref('browser.startup.homepage', vrl);
        }
    }
}

/******************************************** 小数点后保留位数 **************************************************/

function Decimal(price, digit) {
    var dd = Math.pow(10, digit);
    return Math.round(price * dd) / dd;
}

/**************************************** 此段代码隔离 ***************************************************/

function Combin(CombinList) {
    var Result = new Array();
    var CombineCount = 1;
    for (i in CombinList) {
        CombineCount *= CombinList[i].length;
    }

    var RepeatTime = CombineCount;
    for (i in CombinList) {
        var ClassNo = i;
        var StudentList = CombinList[i];
        RepeatTime = RepeatTime / StudentList.length;
        var StartPosition = 1;
        for (j in StudentList) {
            var TempStartPosition = StartPosition;
            var SpaceCount = CombineCount / StudentList.length / RepeatTime;
            for (var J = 1; J <= SpaceCount; J++) {
                for (var I = 0; I < RepeatTime; I++) {
                    if (typeof (Result[TempStartPosition + I]) == 'undefined') {
                        Result[TempStartPosition + I] = new Array();
                    }
                    Result[TempStartPosition + I][ClassNo] = StudentList[j];
                }
                TempStartPosition += RepeatTime * StudentList.length;
            }
            StartPosition += RepeatTime;
        }
    }
    return Result;
}

/**********************************************************************************************************/
