﻿
var SPLITTER_RECORD = "{\\r\\r*\\r\\r}";
var SPLITTER_FIELD = "{\\r*\\r}";


/////////////////////////////////////////字符串操作///////////////////////////
function GetFileNameByPath(s) {
    if (s.indexOf("\\") > -1)
        return s.match(/\\([^\\^.]+)\.[^\\]*$/)[1];
    else
        return s;

}

//定义一个字符stringbuilder类
function StringBuilder() {
    this.arrStr = new Array();
}
StringBuilder.prototype.Append = function (strVelue) {
    this.arrStr.push(strVelue);
}

StringBuilder.prototype.toString = function () {
    

    return this.arrStr.join("");

}
//html编码////////////
function HtmlEncode(text) {
    return text.replace(/&/g, '&amp').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
//html解码
function HtmlDecode(text) {

    return text.replace(/&amp;/g, '&').replace(/&quot;/g, '\"').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/\n/g, '<br>').replace(/\t/g, '<br>').replace(/\r/g, '');

}

function GetJsPram(jsid, strArg) {
    var str = document.getElementById(jsid).src;
    var _url = str + "&";
    var regex = new RegExp("(\\?|\\&)" + strArg + "=([^\\&\\?]*)\\&", "gi");
    if (!regex.test(_url)) return "";
    var arr = regex.exec(_url);
    return (RegExp.$2);
}
//获取url?号后的参数
function GetUrlParams(ParamName) {
    var URLParams = new Object();
    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];
    }

    var sValue = URLParams[ParamName];
    if (sValue == undefined)
        return "";
    return sValue;
}

function cut_string(strString, nLength) {
    var nTheLength = 0;
    for (var nIndex = 0; nIndex < strString.length; nIndex++) {
        if (strString.charCodeAt(nIndex) > 255)
            nTheLength += 2;
        else
            nTheLength += 1;
        if (nTheLength >= nLength)
            break;
    }
    var strResult = strString.substr(0, nIndex);
    if (strResult.length < strString.length)
        strResult = strResult + "...";
    return strResult;
}
function cut_str_mid(strString, iLength) {
    if (strString.length > iLength) {
        var iLength2 = parseInt(iLength / 2);
        var strtemp1 = strString.substr(0, iLength2);
        var strtemp2 = strString.substr(strString.length - iLength2);
        strString = "";
        strString = strtemp1 + "  ...  " + strtemp2;
    }
    return strString;
}
//从url中获取文件类型
function get_type_of_url(sUrl) {
    var strType = "";
    if (sUrl != "") {
        var iLastIndex = sUrl.lastIndexOf(".") + 1;
        strType = sUrl.substring(iLastIndex, sUrl.length);

    }
    if (strType.length > 5) return "";
    return strType;
}
//复制文本
function CopyObText(thisobj) {
    var rng = document.body.createTextRange();
    rng.moveToElementText(thisobj);
    rng.scrollIntoView();
    rng.select();
    clipboardData.setData('text', thisobj.innerText);
    alert("复制成功!!");
}
function JsonToObj(sJson) {

    return eval("({" + sJson + "})");
}

//////////////////////////////////数组操作///////////////////////

//为数组添加一个删除指定索引项  
Array.prototype.removecount = 0;
Array.prototype.removedAt = function (index) {
    if (isNaN(index) || index < 0) {
        return;
    }
    if (index < this.length) {
        this.splice(index, 1);
        this.removecount++;
    }

}
//为数组添加一个新办法:删除重复数组项。
Array.prototype.unique = function () {
    var tempArray = this.slice(0); //复制数组到临时数组
    for (var i = 0; i < tempArray.length; i++) {
        for (var j = i + 1; j < tempArray.length; ) {
            if (tempArray[j].toString() == tempArray[i].toString())
            //后面的元素若和待比较的相同，则删除并计数；
            //删除后，后面的元素会自动提前，所以指针j不移动
            {
                tempArray.splice(j, 1);
            }
            else {
                j++;
            }
            //不同，则指针移动
        }
    }
    return tempArray;
}


//从指定值获取数组索引,如有重复值，只取第一个索引
Array.prototype.GetIndex = function (obj) {
    var tempArray = this.slice(0); //复制数组到临时数组
    var iIndex = 0;
    for (var i = 0; i < tempArray.length; i++) {
        if (tempArray[i].Equals(obj)) {
            iIndex = i;
            break;
        }
    }

    return iIndex;
}
//为数组添加一个新办法:转换成JSON代码,iarr为数组维数,只支持1,2维。
Array.prototype.toJson = function(iarr) {
    var strJson = "";
    var sbJson = new StringBuilder();
    if (iarr == 1) {
        strJson = this.join("\",\"");
        strJson = "[\"" + strJson + "\"]";
    }
    else if (iarr == 2) {
        for (var i = 0; i < this.length; i++) {

            sbJson.append("\"],[\"" + this[i].join("\",\""));
        }

        strJson = sbJson.toString();
        strJson = strJson.replace("\"],", "[");
        strJson = strJson + "\"]]";
    }

    return strJson;

};


//table 常用操作,iTop为保留顶项

function clear_table_rows(obTable, iTop) {
    var delRows = obTable.rows.length;
    for (i = iTop; i < delRows; i++) obTable.deleteRow(1);
};

//////////////////////////////////////ajax操作////////////////
function onAjaxBeforeSend(jqXHR, settings) {

   // AJAX调用需要可直接向真正的物理位置
    // WEB服务/页面的方法。对于这一点，是用来IISPath。
    //如果一个AJAX调用是一个虚拟的URL（的博客实例），虽然
    // URL重写，重写这些URL，我们结束了一个“405不允许的方法”
    //错误的Web服务。在这里，我们设置的请求标头，因此调用服务器
    //是在正确的网站实例ID。


    jqXHR.setRequestHeader('ebsite-instance', SiteConfigs.DomainName);
}
//GET办法运行异步http请求
function run_ajax_async(url, postdata, backfun) {

    $.ajax({
            type: "get",
            url: url,
            data: postdata,
            dataType: "html",
            success: function(msg) {
                if (backfun != null) backfun(msg);

            },
            async: true
        });
}
//GET/post办法运行异步http请求
function run_ajax_async_type(url, postdata, backfun, type) {
    $.ajax({
            type: type,
            url: url,
            data: postdata,
            dataType: "html",
            success: function(msg) {
                if (backfun != null) backfun(msg);

            },
            async: true
        });
    }
//异步json
function run_ajax_async_json(url, postobj, backfun) {
    var obpram = postobj;
    if (postobj == null || postobj == "")
        obpram = {};
    $.ajax({
        url: url,
        data: JSON.stringify(obpram),
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: onAjaxBeforeSend,
        success: function (result) {
            if (backfun != null) backfun(result);
        },
        error: function (xhr, ajaxOptions, thrownError) {

            alert("调用ajax发生错误:" + thrownError);
        }

    });
}
//同步json
function run_ajax_unasync_json(url, postobj) {
    var obpram = postobj;
    if (postobj == null || postobj == "")
        obpram = {};

    var vlMsg;

    $.ajax({
        url: url,
        async: false,
        data: JSON.stringify(obpram),
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        beforeSend: onAjaxBeforeSend,
        success: function (result) {
            vlMsg = result;
        },
        error: function (xhr, ajaxOptions, thrownError) {

            alert("调用ajax发生错误:" + thrownError);
        }

    });

    return vlMsg;
}

//运行同步http请求
function run_ajax_unasync(url) {
    var html = $.ajax({
        url: url,
        async: false
    }).responseText;

    return html;
}
//GET办法运行异步http请求 javascript
function run_ajax_async_js(url, postdata, backfun) {

    $.ajax({
        type: "get",
        url: url,
        data: postdata,
        dataType: "script",
        success: function (msg) {
            if (backfun != null) backfun(msg);

        },
        async: true
    })
}
function run_ajax_xml(url, postdata, backfun) {

    $.ajax({
        type: "POST",
        url: url,
        data: postdata,
        dataType: "xml",
        success: function (msg) {
            if (backfun != null) backfun(msg);

        },
        async: true
    })
}

//获取json数据sPath为文件名称与路径
function get_json(sPath, fun) {
    $.getJSON(sPath,
                fun
        );
}

function JsonToObj(sJson) {

    return eval("({" + sJson + "})");
}
//批量操作----------------------------------------
//获取某个元素下的所有check值，用逗号分开
function GetCheckValues(obid) {
    var aValues = [];
    var obHtml = $("#" + obid);
    obHtml.find("input[type=checkbox]").each(
		function (i) {
		    if (this.checked) {
		        aValues.push($(this).val());
		    }
		}
		);

    return aValues.join(",");
}
//全选
function on_checkall(ob) {
    
    $(ob).find("input[type=checkbox]").each(
		function (i) {
		    this.checked = true;
		}
		);
}
//反向选取
function on_checkback(ob) {
    var obHtml = $(ob);
    
    obHtml.find("input[type=checkbox]").each(
		function (i) {
		    if (this.checked) {
		        this.checked = false;
		    }
		    else {
		        this.checked = true;
		    }
		}
		);
}

//反向选取
function on_check(obchk) {
    var ob = $(obchk).parent().parent().parent();
    $(ob).find("input[type=checkbox]").each(
		function (i) {
		    this.checked = obchk.checked;
		}
		);
}


//获取选择的radio值
function get_checkedradio_value(radios) {
    for (var i = 0; i <= radios.length; i++) {
        if (radios[i].checked) return radios[i].value;
    }
    return null;
}

//获取下拉列表项
function get_selected_value(ob) {

    return ob.options[ob.selectedIndex].value;
}
function set_selected_value(ob, value) {
    $(ob).attr("value", value);

}
//pos = -1 为添加,pos=0为插入
function add_selecte_option(obID, svalue, stext, pos) {

    var strName = stext;
    var strValue =svalue ;
    $("#" + obID).append("<option value=\""+strValue+"\">"+strName+"</option>");
}
// 删除选项,type 为采用不同的方法
function delete_selecte_option(obID,keepindex) {
    
    $("#" + obID).find("option").each(
		function (i) {

		    if (i != keepindex)
		        $(this).remove();

		}
		);
}


//设计一个通用的Tags类
function CustomTags() {
    //tags列表的上一级元素名称
    this.ParentObjName = "";

    this.SubObj = "";
    this.CurrentClassName = "";
    this.ClassName = "";
    this.fun = null;
    this.BoxList = [];
//    var _objThis = this;
    if (typeof CustomTags._initialized == "undefined") {
        CustomTags.prototype.InitOnclickInTags = function () {
            
            var tags = this.GetTags();
            var _objThis = $.data(document.body, "ct", this);
            if (tags != null && tags.length > 0) {
                for (var i = 0; i < tags.length; i++) {
                    //                    tags[i].onclick = _objThis.ToOnClick; //this.fun; 


                    $(tags[i]).click(function () {

                        if (_objThis.fun != null && _objThis.fun != "undefined")
                            _objThis.fun(this);
                        _objThis.OnclickTags(this);
                    });

                    if ($(tags[i]).attr("name") != undefined && $(tags[i]).attr("name") != null) {

                        this.BoxList.push($(tags[i]).attr("name"));
                    }

                }
            }
//            var obBox = document.getElementById(this.BoxContent);
//             $.data(ob, "CustomTags");

        }
//        CustomTags.prototype.ToOnClick = function () {
//            if (_objThis.fun != null && _objThis.fun != "undefined")
//                _objThis.fun(this);
//            _objThis.OnclickTags(this);
//        }
        CustomTags.prototype.OnclickTags = function (obj) {

            var Url = $(obj).attr("u");

            if (Url == undefined) {  //tag只在当前页面使用

                this.InitCurrentClass(obj);
                var tags = this.GetTags();

                if (this.BoxList.length > 0) {
                    for (var i = 0; i < tags.length; i++) {

                        if (this.BoxList[i] != null) {
                            $("#" + this.BoxList[i]).hide();
                        }
                    }
                    $("#" + $(obj).attr("name")).show()

                }

            }
            else { //tag跨页面使用

                location.href = Url + "&tagname=" + obj.name;

            }


        }
        CustomTags.prototype.InitCurrentClass = function (obj) //初始化tag样式表
        {

            var tags = this.GetTags();
            if (tags != null && tags.length > 0) {
                for (var i = 0; i < tags.length; i++) {

                    if (tags[i] != obj) {


                        tags[i].className = this.ClassName;
                    }
                }
            }

            obj.className = this.CurrentClassName;

        }
        CustomTags.prototype.InitCurrent = function (CurrentTagName) //跨页tag初始化
        {
            var obj = null;
            var tags = this.GetTags();
            for (var i = 0; i < tags.length; i++) {

                if (tags[i].name == CurrentTagName) {
                    obj = tags[i];
                    break;
                }
            }
            if (obj == null)
                obj = tags[0];

            this.InitCurrentClass(obj);
        }
        CustomTags.prototype.InitOnclick = function (index) {
            var tags = this.GetTags();
            var obj = tags[index];
            if (obj == "undefined" || obj == null) {
                alert("没有找到对应的Tag");
                return;
            }
            $(obj).click();
        }
        CustomTags.prototype.GetTags = function () {

            var Tags = [];
            $("#" + this.ParentObjName).find(this.SubObj).each(

                    function (i) {
                        Tags.push(this);
                    }
                );
            return Tags;
        }
    }
    CustomTags._initialized = true;

}
// 实现一个类似委托特性的类(js里面function可以相当于一个类)，add方法相当于运算符"+"重载,run相当执行委托链上所有的回调函数

function delegate(func) {
    this.arr = new Array(); // 回调函数数组
    this.add = function (func) {
        this.arr[this.arr.length] = func;
    };
    this.run = function () {
        for (var i = 0; i < this.arr.length; i++) {
            var func = this.arr[i];
            if (typeof func == "function") {
                func(); // 遍历所有方法以及调用
            }
        }
    }
    this.add(func);
}

//窗口操作----------------------------------------------
function ColseGreyBox() {


    if (parent.ColseWinbox != undefined) {
        parent.ColseWinbox();
    }
    else {
        window.close();
    }
}
function OpenGreyBoxFull(sTitle, Url) {

    GB_showFullScreen(sTitle, Url);
}

function OpenGreyBoxCenter(sTitle, Url, width, height) {

    GB_showCenter(sTitle, Url, height, width);

}

// 显示无模式对话框
function ShowDialog(url, object, width, height) {

    var cursor = document.body.style.cursor;
    document.body.style.cursor = "wait";
    var result = window.showModalDialog(url, object, "status:no; center:yes; help:no; minimize:no; maximize:no; scroll:no; border:thin; statusbar:no; dialogWidth:" + width + "px; dialogHeight:" + height + "px");
    document.body.style.cursor = cursor;
    return result;

}


// 显示模式对话框
function ShowLessDialog(url, width, height) {

    var result = showModelessDialog(url, window, "dialogWidth:" + width + "px;dialogHeight:" + height + "px;help:no;scroll:no;status:no");
    if ((result) && (result != "")) {
        alert(result)
    }
}

function getposition(obj) {
    var r = new Array();
    r['x'] = obj.offsetLeft;
    r['y'] = obj.offsetTop;
    while (obj = obj.offsetParent) {
        r['x'] += obj.offsetLeft;
        r['y'] += obj.offsetTop;
    }
    return r;
}

//显示提示层
function showhintinfo(obj, objleftoffset, objtopoffset, title, info, objheight, showtype, objtopfirefoxoffset) {

    var p = getposition(obj);

    if ((showtype == null) || (showtype == "")) {
        showtype == "up";
    }

    document.getElementById('hintiframe' + showtype).style.height = objheight + "px";
    document.getElementById('hintinfo' + showtype).innerHTML = info;
    document.getElementById('hintdiv' + showtype).style.display = 'block';



    if (objtopfirefoxoffset != null && objtopfirefoxoffset != 0 && !isie()) {
        document.getElementById('hintdiv' + showtype).style.top = p['y'] + parseInt(objtopfirefoxoffset) + "px";
    }
    else {
        if (objtopoffset == 0) {
            if (showtype == "up") {
                document.getElementById('hintdiv' + showtype).style.top = p['y'] - document.getElementById('hintinfo' + showtype).offsetHeight - 40 + "px";
            }
            else {
                document.getElementById('hintdiv' + showtype).style.top = p['y'] + obj.offsetHeight + 5 + "px";
            }
        }
        else {
            document.getElementById('hintdiv' + showtype).style.top = p['y'] + objtopoffset + "px";
        }
    }
    document.getElementById('hintdiv' + showtype).style.left = p['x'] + objleftoffset + "px";
}



//隐藏提示层
function hidehintinfo() {
    document.getElementById('hintdivup').style.display = 'none';
    document.getElementById('hintdivdown').style.display = 'none';
}

function wo(url, w, h, m, s) {
    var left = (screen.width - w) / 2;
    var top = m ? (screen.height - h) / 2 : 0;
    return window.open(url, 'player', 'width=' + w + ',height=' + h + ',top=' + top + ',left=' + left + ',scrollbars=0,resizable=0,status=' + s);

}
function woshowbar(url, w, h, m, s) {
    var left = (screen.width - w) / 2;
    var top = m ? (screen.height - h) / 2 : 0;
    return window.open(url, '', 'width=' + w + ',height=' + h + ',top=' + top + ',left=' + left + ',scrollbars=1,resizable=0,status=' + s);

}
function OpenWinCenter(url, w, h) {
    woshowbar(url, w, h, 1, 1);
}
function wo_href(url, w, h, m, s, name) {
    var left = (screen.width - w) / 2;
    var top = m ? (screen.height - h) / 2 : 0;
    window.open(url, name, 'width=' + w + ',height=' + h + ',top=' + top + ',left=' + left + ',scrollbars=0,resizable=0,status=' + s);
    return false;
}

//以post方式打开窗口
function postwinopen(strUrl, postdata) {

    var contrlos = postdata.split("&");
    var form = document.createElement("form");
    for (var i = 0; i < contrlos.length; i++) {

        var strPram = contrlos[i];

        var cotrlosname = strPram.substring(0, strPram.indexOf("="));
        var sValue = strPram.substring(strPram.indexOf("=") + 1, strPram.length);

        var input = document.createElement("input");
        input.setAttribute("type", "text");
        input.setAttribute("style", "display:none");
        input.name = cotrlosname;
        input.value = sValue;

        form.appendChild(input);
    }

    document.body.appendChild(form);
    form.target = "_blank";
    form.method = "post";
    form.action = strUrl;
    form.submit();
    document.body.removeChild(form);
}


//关闭一个距于页面中间的提示框
function CloseTipsToCenter() {

    $("#wCenterWindow").remove();
}
//打开一个距于页面中间的提示框
function OpenTipsToCenter(stitle, msg, iWidth, iHeight) { //titlecolor, backgcolor


    $("body").append("<div  id=\"wCenterWindow\" title=\"" + stitle + "\"  style=\"text-align:center; font-size:14px; font-weight:bold;color:#000; background-color:#FFA44A;display:none;\" >" + msg + "</div>");


    $("#wCenterWindow").dialog({
        draggable: false,
        resizable: false,
        modal: true,
        width:iWidth,
        height: iHeight
    });
    //去掉标题
    if ($.trim(stitle)=='')
        $(".ui-dialog-titlebar").hide();

}

function RefeshParent() {
    parent.parent.document.location.href = parent.parent.document.location.href;
}
function RefeshParent1() {
    parent.document.location.href = parent.document.location.href;
}
//提示操作类--------


function searchsubmit(ob) {
    if (ob.q.value == "") return false;
}

//提示--------------------

function CustomTipsWithCl(ob, sHtml) {
    var ct = "<table style='width:100%;'><tr><td style='text-align:right;'><img id='imTipsColse' style='cursor:pointer;' src='"+SiteConfigs.UrlIISPath+"images/menus/Delete.gif'></td></tr><tr><td>" + sHtml + "</td></tr></table>";
    CustomTips(ob, ct);


    $("#imTipsColse").bind('click', function () {
        CloseCustomTips(ob);
    });
}
function TipsAutoClose(ob, sHtml) {        
    $(ob).bind('mouseout', function () {
        CloseCustomTips(ob);
    });
    CustomTips(ob, sHtml);
}



//var tipsTimer = null;
function TipsClickClose(ob, sHtml) {
    $(document).bind('click', function () {
        CloseCustomTips(ob);
    });
    CustomTips(ob, sHtml);
    //tipsTimer = window.setTimeout(CustomTips, 1000, ob, sHtml);
    $(ob).bind('mouseout', function () {
//        if (tipsTimer != null) {
//            clearTimeout(tipsTimer);
//        }
            

    });
    

}
function CustomTips(ob, sHtml) {
    var _f = $.data(ob, "ebtips");
    var _d = $(ob);
    if (!_f) {

        //
        _f = $("<div class=\"ebcustom-tip\">" + "<span class=\"ebcustom-tip-content\">" + "</span>" + "<span class=\"ebcustom-tip-pointer\">" + "</span>" + "</div>").appendTo("body");

        $.data(ob, "ebtips", _f);

    }
    _f.find(".ebcustom-tip-content").html(sHtml);
    _f.css({ display: "block", left: _d.offset().left + _d.outerWidth(), top: _d.offset().top });

}
function CloseCustomTips(ob) {
    var tip = $.data(ob, "ebtips");
    if (tip) tip.remove();
    $.data(ob, "ebtips", null);
}

/*
    让某个元素闪烁
ele : jQuery Object [object] 要闪动的元素
cls : Class Name [string] 闪动的类
times : Number [Number] 闪动几次 
    
*/
function shake(obj, cls, times) {
    var ele = obj;
    var i = 0, t = false, o = ele.attr("class") + " ", c = "", times = times || 2;
    if (t) return;
    t = setInterval(function () {
        i++;
        c = i % 2 ? o + cls : o;
        ele.attr("class", c);
        if (i == 2 * times) {
            clearInterval(t);
            ele.removeClass(cls);
        }
    }, 200);
};
//提示结束-----------------------


