﻿//----------------------######### 门户框架公用函数 ##########-----------------------------------------
String.prototype.parseQuery = function (name) {
    var reg = new RegExp("(^|&|\\?)" + name + "=([^&]*)(&|$)", "i"), r;
    if (r = this.match(reg)) return r[2]; return null;
}

function doWork(callback, arg) {
    try {
        if (callback != null && callback != "null" && callback != '') {
            var prefix = callback.indexOf(".") != -1 ? "" : "top.";
            if (!arg) {
                eval(prefix + callback + "()");
            }
            else {
                eval(prefix + callback + "('" + arg + "')");
            }
        }
        else {
            if (typeof (parent.popp) != "undefined") {
                parent.popp.close();
            }
        }
    } catch (e) { alert("执行回调出错，" + e.message); }
}

function doProxy() {
    var url = location.href;
    var callback = url.parseQuery("callback");
    var arg = url.parseQuery("arg") || "";
    //var t = url.parseQuery("timeout") || 2000;

    doWork(callback, arg);
}

//从URL地址栏获取查询参数,如果不存在，返回null;否则返回对应的值
function __bingoGetQuery(queryStrKey) {
    return decodeURI(window.location.href).parseQuery(queryStrKey);
}

//全局函数，跳转到某个页面，自动会带上Source参数，用于目标页面处理后“返回”
//用于列表页面跳转去新增，编辑页面
function __bingoGoTo(url) {
    var serperate = url.indexOf("?") >= 0 ? "&" : "?";

    window.location.href = url + serperate + 'Source=' + window.location;
    return false;
}

function __bingoGoBack() {
    var target = "";
    var len = arguments.length;

    if (len > 0) {
        target = arguments[0]; //url
    }
    else if (typeof __bingoSource != "undefined" && __bingoSource != "") {//__bingoSource 由basepage注册的全局变量
        target = __bingoSource;
    }
    else {
        alert("参数Source为空，无法返回");
        return;
    }

    window.location.href = target;
    return false;
}

//后台管理页面重置函数，需要重置的控件必须加上"_reset='true'"
function __bingoJsReset() {
    //客户端是input的
    $("input[_reset='true']").each(function () {
        var self = $(this);
        self.val("");
    });

    //获取下拉框的
    $("select[_reset='true']").each(function () {
        var self = $(this);
        self.val("");
    });

    return false;
}

function __bingoTmplShowSelect(inputName, isRtfEditor) {
    var jqInputObj = $("input[name='" + inputName + "']"); //根据名字获取input
    var orginValue = jqInputObj.val();
    var sFeatures = "dialogHeight:450px;dialogWidth:600px;status=0;scroll=0;";
    var editorUrl = "/_Layouts/AppPages/WPLibrary/BasicEditor.aspx?ran=" + Math.random(); //普通文本编辑器


    if (isRtfEditor) {
        sFeatures = "dialogHeight:530px;dialogWidth:750px;status=0;scroll=0;";
        editorUrl = "/_Layouts/AppPages/WPLibrary/RtfEditor.aspx?ran=" + Math.random();
    }

    var args = new Object();
    args.value = orginValue;

    var rtnValue = window.showModalDialog(editorUrl, args, sFeatures);

    if (rtnValue != "undefined" && rtnValue != orginValue) {
        jqInputObj.val(rtnValue);
        __bingoEditorZoneApply();
    }
}

//刷新当前页面
function __bingoRefresh(returnValue) {
    if (returnValue != undefined) {
        window.location.href = window.location.href;
    }
}

//显示系统日期 
function __bingoGetFormatDate() {
    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth() + 1
    var day = now.getDate();

    return year + "年" + month + "月" + day + "日";
}

//获取当前天的星期
function __bingoGetDayOfWeek() {
    var now = new Date();
    var day = now.getDay();
    var dayArr = new Array("星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    return dayArr[day];
}

//设为首页
function __bingoSetHomePage(obj) {
    if (obj == null) {
        return;
    }

    var url = window.location.protocol + '//' + window.location.host;
    obj.style.behavior = "url(#default#homepage)";
    obj.setHomePage(url);
    obj.style.behavior = "";
}

function __bingoShowDialog(sURL, title, width, height) {
    __bingoShowDialog(sURL, title, width, height,"0");
}

function __bingoShowDialog(sURL, title, width, height, scroll) {
    var sFeatures = "dialogHeight:" + height + "px;dialogWidth:" + width + "px;status=0;scroll=" + scroll;
    var url = sURL;
    return window.showModalDialog(url, window, sFeatures);
}

function __bingoGetFlashHtml(adid,src, w, h, id) {
    var tmpHtml = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + w + '" height="' + h + '" id="' + id + '">';
    tmpHtml += '<param name="movie" value="' + src + '" />';
    tmpHtml += '<param name="quality" value="high" />'
    tmpHtml += '<embed src="' + src + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + w + '" height="' + h + '"></embed>';
    tmpHtml += '</object>';

    return tmpHtml;
}

function __bingoGetADImageHtml(adid,src, w, h, description,linkUrl,id) {
    var adHtml = '<a href="' + linkUrl + '" _adid="' + adid + '" title="' + description + '" target="_blank"><img height=' + h + ' src="' + src + '" width=' + w + ' /></a>';
    return adHtml;
}

function __bingoLogAdClickCount(adId) {
    var urlLogClickCount = "/_Layouts/AppPages/Proxy/LogAdClickCount.ashx?adid=" + adId + "&ran=" + Math.random();
    $.get(urlLogClickCount, { Action: "get" }, function (data, textStatus) { });
}

function __bingoChangeAdLinkAction() {
    //具有扩展属性的a标签都是记录点击率的广告
    $("a[_adid]").each(function (i) {
        this._ex_href = this.href;
        this.href = "javascript:void(0);";
        this.target = "";//取消的原来的打开方式，如"_blank";
        this.onclick = function () {
            __bingoLogAdClickCount(this._adid);//记录点击率
            window.open(this._ex_href);
        }
    });
}

function __bingoAutoSetIFrame(id, src, autoHeight, autoPageTitle) {
    if (id == '' || src == '')
        return;

    $(document).ready(function () {
        var _iframe = document.getElementById(id);
        //_iframe.width = width == "" ? "100%" : width;

        _iframe.src = src + (src.indexOf('?') != -1 ? '&' : '?') + '_frmId=' + id + "&_autoHeight=" + autoHeight + "&_syncPageTitle=" + autoPageTitle + "&_rand=" + Math.random();
    });
}

function __bingoIFrameAutoSize(objIFrame) {
    try{
        if (objIFrame.id && objIFrame.height) {
            if (objIFrame.height != "") {
                document.getElementById(objIFrame.id).height = objIFrame.height;
            }
        }

        if (typeof (objIFrame.pageTitle) != "undefined" && objIFrame.pageTitle != "") {
            document.title = objIFrame.pageTitle;
        }

        if (typeof (objIFrame.isHide) != "undefined") {
            var iframeContainer = document.getElementById("div-" + objIFrame.id);

            if (!iframeContainer)
                return;

            if (objIFrame.isHide == "true") {
                iframeContainer.style.display = "none";
            }
        }
    }
    catch(ex)
    {
        //alert("out:" + ex.Description);
    }
}

function __bingoShowObject(objId)
{
    $("#"+objId).show('');
}

function __bingoDelayShowDivEditBar(divBarId,delay) {
    window.setTimeout("__bingoShowObject('" + divBarId + "')", delay);
}

function __bingoOnToggleDiv(clickObj, divId) {
    var imgJia = clickObj._jia;
    var imgJian = clickObj._jian;

    var elem = document.getElementById(divId);
    if (elem.style.display == 'none') {
        elem.style.display = '';
        if (imgJian != "") {
            clickObj.src = imgJian;
        }
    }
    else {
        elem.style.display = 'none';
        if (imgJia != "") {
            clickObj.src = imgJia;
        }
    }
}

function __bingoLoadPng(o) {
    if (document.all) {//判断是否ie
        try {
            var img = o;
            var imgName = o.src.toUpperCase();
            if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
                var imgID = (img.id) ? "id='" + img.id + "' " : "";
                var imgClass = (img.className) ? "class='" + img.className + "' " : "";
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
                var imgStyle = "display:inline-block;" + img.style.cssText;
                if (img.align == "left")
                    imgStyle = "float:left;" + imgStyle;
                if (img.align == "right") 
                    imgStyle = "float:right;" + imgStyle;
                if (img.parentElement.href) 
                    imgStyle = "cursor:hand;" + imgStyle;
                var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>";
                img.outerHTML = strNewHTML;
            }
        }
        catch (e) {
            alert(e.Message);
        }
    }
}

//该函数为jQuery的Load函数的的简单封装，加入了显示Loading功能
function __bingoAjaxLoad(htmlContainerId, url, params, callback) {
    var getObjString;
    if (htmlContainerId.indexOf("#") != -1)
        getObjString = htmlContainerId;
    else
        getObjString = "#" + htmlContainerId;

    if (typeof (callback) != "function") {
        $(getObjString).prepend("<div style='text-align:center'>正在刷新中...</div>");
    }

    $(getObjString).load(url, params, callback);
}

//通用Tab切换
function __setTab(tabId, n) {
    var tli = $("li > a", document.getElementById(tabId));
    var containerTagName = "ul";
    var hoverClassName = "current";

    if (arguments.length > 2) {
        containerTagName = arguments[2];
    }

    if (arguments.length > 3) {//4个参数
        hoverClassName = arguments[3];
    }

    var mli = document.getElementById(tabId + "-div").getElementsByTagName(containerTagName);

    try {
        for (i = 0; i < tli.length; i++) {
            tli[i].className = i == n ? hoverClassName : "";
            mli[i].style.display = i == n ? "block" : "none";
        }
    } catch (e) {
        //alert(e.Message);
    }
}

function __newsImageResize(elementID, maxWidth) {
    var _elmentID = typeof (elementID) == "undefined" ? "newcontent" : elementID;
    var _maxWidth = typeof (maxWidth) == "undefined" ? 550 : maxWidth;

    var imgs = document.getElementById(_elmentID).getElementsByTagName("img");

    for (var i = 0; i < imgs.length; i++) {
        if (imgs[i].width > _maxWidth) {
            imgs[i].onclick = function (e) {
                window.open(this.src);
            }

            imgs[i].style.width = _maxWidth + "px";
            imgs[i].style.cursor = "pointer";
//            imgs[i].style.zoom="1";
//            imgs[i].onmousewheel=function(e)
//            {
//                this.style.zoom*= Math.pow(.9,Math.abs(event.wheelDelta)/event.wheelDelta);
//            }
        }
    }
}
//----------------------######### 门户框架公用函数 End   ##########----------------------------------------

//----------------------$$$$$$$$$ 以下开始为项目用的函数 $$$$$$$$------------------------------------------
///显示授权对话框
function __bingoShowPermissionDialog(scopeId, resType, title, isInherit) {
    var url = '/_Layouts/AppPages/Admin/Permission/SysPermissionList.aspx?IsInherit=' + isInherit +
              '&ScopeId=' + scopeId + '&type=' + resType + '&showname=' + encodeURI(title) + "&t=" + Math.random();
    __bingoShowDialog(url, title, 700, 450);
    return false;
}

//列表全选方法
function __bingoSelectAll(obj, gridviewID) {
    if (obj == null) {
        return;
    }

    var grd = document.getElementById(gridviewID);
    for (var i = 1; i < grd.rows.length; i++) {
        if (grd.rows[i].cells[0].children.length > 0) {
            chbItem = grd.rows[i].cells[0].children[0].children[0];
            chbItem.checked = obj.checked;
        }
    }
}

//检察是否有选择删除的记录
function __bingoCheckSelected(gridviewID,isShowConfirm,msg) {
    var isFind = false;
    var grd = document.getElementById(gridviewID);
    for (var i = 1; i < grd.rows.length; i++) {
        if (grd.rows[i].cells[0].children.length > 0) {
            chbItem = grd.rows[i].cells[0].children[0].children[0];
            if (chbItem.checked) {
                isFind = true;
                break;
            }
        }
    }
    if (isFind) {
        if ((isShowConfirm == undefined || isShowConfirm)) {
            if (msg == undefined)
                msg = "确定要删除所选的记录吗？";

            return confirm(msg);
        }
    }
    else {
        alert("请选择要操作的记录！");
        return false;
    }
}

// 获取cookie值
function GetCookie(sName) {
    // cookies are separated by semicolons
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        // a name/value pair (a crumb) is separated by an equal sign
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    // a cookie with the requested name does not exist
    return null;
}

function __bingoRereshEmail(iframeId) {
    var emailIframe = document.getElementById(iframeId);

    if (emailIframe) {
        var orginSrc = "";
        if (typeof (emailIframe._orgUrl) == "undefined") {
            emailIframe._orgUrl = emailIframe.src;
        }
        orginSrc = emailIframe._orgUrl;
        emailIframe.src = orginSrc + "&ran=" + Math.random();
    }
}

function __bingoCheckForEsc(e) {
    if (!e) e = window.event;
    var key = e.keyCode;
    if (key == 27)
        window.close();
}
