﻿/*---------公共JS函数库---------*/
/*--------Lzeasy 2009.10-------*/

///去除空格///
String.prototype.Trim = function()
{ return this.replace(/^\s+/g,"").replace(/\s+$/g,""); }
///Cookie操作
function CookieOper()
{
    this.GetCookie = function(key)
    {
        var cookie = document.cookie;
        var cookieArray = cookie.split(';');
        var getvalue = "";
        for(var i = 0; i < cookieArray.length; i++)
        {
            if(cookieArray[i].Trim().substr(0,key.length) == key)
            {
                getvalue = cookieArray[i].Trim().substr(key.length + 1);
                break;
            }
        }
        return getvalue;
    };
    this.GetChild = function(cookiekey,childkey)
    {
        var child = this.GetCookie(cookiekey);
        var childs = child.split('&');
        var getvalue = "";
        for(var i = 0; i < childs.length; i++)
        {
            if(childs[i].Trim().substr(0,childkey.length) == childkey)
            {
                getvalue = childs[i].Trim().substr(childkey.length + 1);
                break;
            }
        }
        return getvalue;
    };
    this.SetCookie = function(key,value,expire,domain,path)
    {
        var cookie = "";
        if(key != null && value != null)
        { cookie += key + "=" + value + ";"; }
        if(expire != null)
        { cookie += "expires=" + expire.toGMTString() + ";"; }
        if(domain != null)
        { cookie += "domain=" + domain + ";"; }
        if(path != null)
        { cookie += "path=" + path + ";"; }
        document.cookie = cookie;
    };
    this.Expire = function(key)
    {
        expire_time = new Date();
        expire_time.setFullYear(expire_time.getFullYear() - 1);
        var cookie = " " + key + "=e;expires=" + expire_time + ";";
        document.cookie = cookie;
    }
}
///日期格式化///
Date.prototype.format = function(format)
{
    var o = {
        "M+" : this.getMonth()+1,
        "d+" : this.getDate(),
        "h+" : this.getHours(),
        "m+" : this.getMinutes(),
        "s+" : this.getSeconds(),
        "q+" : Math.floor((this.getMonth()+3)/3),
        "S" : this.getMilliseconds()
}
if(/(y+)/.test(format))
{ format = format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length)); }
for(var k in o)
{
    if(new RegExp("("+ k +")").test(format))
    { format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] :("00"+ o[k]).substr((""+ o[k]).length)); }
}
return format;
}
///文本截取///
String.prototype.sub = function(n){
var r = /[^\x00-\xff]/g;
if(this.replace(r, "mm").length <= n) return this;
var m = Math.floor(n/2);
for(var i=m; i<this.length; i++) {
if(this.substr(0, i).replace(r, "mm").length>=n) {
return this.substr(0, i) +"..."; }     
} return this;    
};
///输出日期时间///
///<param=obj>输出日期时间文本的对象ID</param>
function OutputDateTime(obj)
{
    var today,year,month,date;
    var strDate;
    today = new Date();
    var n_day = today.getDay();
    switch (n_day)
    {
        case 0:{strDate = "星期日"}break;
        case 1:{strDate = "星期一"}break;
        case 2:{strDate = "星期二"}break;
        case 3:{strDate = "星期三"}break;
        case 4:{strDate = "星期四"}break;
        case 5:{strDate = "星期五"}break;
        case 6:{strDate = "星期六"}break;
        case 7:{strDate = "星期日"}break;
    }
    year = today.getFullYear();
    month = today.getMonth()+1;
    date = today.getDate();
    document.getElementById(obj).innerHTML = year + "年" + month + "月" + date + "日 " +  strDate;
}
///显示或隐藏对象
///<param=menuobj>需显示或隐藏对的ID</param>
///<param=obj>需显示或隐藏对的ID</param>
///<param=show>显示时的样式类名</param>
///<param=hidd>隐藏时的样式类名</param>
function SHObj(menuobj,obj,show,hidd)
{
    var menuobj = document.getElementById(menuobj);
    var obj = document.getElementById(obj);
    if (obj.style.display != 'none')
    {
        obj.style.display = "none";
        if(menuobj != null)
        {menuobj.className = show;}
    }
    else
    {
        obj.style.display = "block";
        if(menuobj != null)
        {menuobj.className = hidd;}
    }
}
///清除文本框内默认内容///
///<param=obj>文本框ID</param>
///<param=checktxt>文本框内默认内容</param>
function ImportTxt(obj,checktxt)
{
    var obj = document.getElementById(obj);
    if(obj.value == checktxt)
    { obj.value = ""; }
}
///取Url参数///
function GetUrlParam()
{
    var urlparam = new Object();
    var query = document.location.search.substring(1);
    var pairs = query.split("&");
    for(var i = 0; i < pairs.length; i++)
    {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0,pos);
        var value = pairs[i].substring(pos+1);
        value = decodeURIComponent(value);
        urlparam[argname] = value;
    }
    return urlparam;
}
///文字大小增减///
///<param=hiddenid>存储当前字体大小的隐藏域</param>
///<param=obj>需要增减字体大小对象的ID</param>
///<param=tags>增加或减少操作标记</param>
function TxtChangeSize(hiddenid,obj,tags)
{
    var hdid = document.getElementById(hiddenid);
    var obj = document.getElementById(obj);
    var tsize = hdid.value;
    if(tags == "add")
    { tsize = parseFloat(tsize) + 0.1; }
    if(tags == "minus")
    { tsize = parseFloat(tsize) - 0.1; }
    tsize = tsize.toFixed(1);
    hdid.value = tsize;
    obj.style.fontSize = tsize + "em";
    obj.style.lineHeight = tsize * 20 + "px";
}
///改变文本内容配色///
///<param=obj>文本内容对象的ID</param>
///<param=clasname>样式类名</param>
function ChangeTextColor(obj,clasname)
{
    var obj = document.getElementById(obj);
    obj.className = clasname;
}
///判断url中文件类型///
function GetFileType(url)
{
    var patt=/\.[^\.]+$/g;
    var blank = /\s/g;
    url = url.replace(blank, "");
    if(url.length > 0)
    { return patt.exec(url); }
}

///创建XML对象///
function GetXmlData()
{
    var xmlDoc = null;
    try//IE浏览器
    { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); }
    catch(e)
    {
        try//兼容其他浏览器
        { xmlDoc = document.implementation.createDocument("","",null); }
        catch(e)
        { xmlDoc = false; }
    }
    if(!xmlDoc)
    { alert("创建XML对象失败，请刷新页面重试！") }
    return xmlDoc;
}
///将单节点的XML内容转换输出///
///<param=top>最多显示几条数据</param>
///<param=property>节点属性名称字符串，以|分隔。</param>
///<param=loopcode>重复输出代码块</param>
///<param=strlen>标题字数，默认为0</param>
///<param=conlen>内容字数，默认为0</param>
//使用注意：property参数：1、XML文件地址；2、子节点名称；3、过期时间判断取值属性名称（不进行过期判断时必须为null）；
//4、New图标地址属性名称（无图标时必须为null，且代码块中不放入参数同名字符串），同时代码块中要显示图标处要有参数值字符，以便替换；
//5、ID值（取多条数据时，top应该为相应数值且参数为null，取单条数据时top为0参数为要取数据的id属性相同值）。
//6、日期格式，如不取日期时为null。a为09-12；b为2009-12-12；c为2009年12月12日
//前6项为必传参数，其后可根据实际传值。
function ConvertSingleXml(top, property, loopcode, strlen ,conlen)
{
    //拆分属性值串
    var propertarr = property.split("|");
    //建立XML对象
    var xmlDoc = GetXmlData();
    //数据传输方式为同步处理
    xmlDoc.async = false;
    xmlDoc.load(propertarr[0]);
    //获取子节点
    var parentnode = xmlDoc.getElementsByTagName(propertarr[1]);
    var thiscontent = "";
        //如果为0取所有，如果要取的数比实际数大取实际数
        var i = 0;
        if(top == 0 || top > parentnode.length)
        { top = parentnode.length; }
        while (i < top)
        {
            var tempcontent = loopcode;
            var xmlcontent = (parentnode[i].childNodes[0].nodeValue);
            //根据属性值取数据
            for (var n = 6; n < propertarr.length; n++)
            {
                var tempvalue = (parentnode[i].getAttribute(propertarr[n]));
                //替换内容
                if(propertarr[n] == "published" && tempvalue != null)
                {
                    var temptime = new Date(tempvalue.replace(/-/g,'/'));
                    switch (propertarr[5])
                    {
                        case "a"://09-11
                        tempvalue = temptime.format('yy-MM');
                        break;
                        case "b"://2009-11-22
                        tempvalue = temptime.format('yyyy-MM-dd');
                        break;
                        case "c"://2009年11月22日
                        tempvalue = temptime.format('yyyy年MM月dd日');
                        break;
                    }
                }
                //因为代码块中可能存在多个需要替换的内容，所以要用eval把属性值转为/属性值字串/g的形式进行全部替换
                if(propertarr[n] == "infotit")
                {
                
                    tempcontent = tempcontent.replace("infotit", tempvalue.sub(strlen));
                    tempcontent = tempcontent.replace("linktit", tempvalue);
                }
                else
                { tempcontent = tempcontent.replace(eval("/" + propertarr[n] + "/g"), tempvalue); }
            }
            //是否需要输出最新信息图标
            if(propertarr[3] != "null")
            {
                var newicopath = (parentnode[i].getAttribute(propertarr[3]));
                if(newicopath.length > 0)
                { newicopath = '<img src="' + newicopath + '" alt="" class="newico" />'; }
                tempcontent = tempcontent.replace(propertarr[3], newicopath);
            }
            //判断是否需要判断数据是否过期
            if(propertarr[2] == "null")
            {
                //判断是否取一条数据，根据id属性值取数
                if(propertarr[4] != "null")
                {
                    var thisid = (parentnode[i].getAttribute("id"));
                    if(propertarr[4] != thisid)
                    { tempcontent = ""; }
                }
                //如果输入了内容字数，则按输入值截取
                if(conlen > 0)
                { xmlcontent = xmlcontent.sub(conlen); }
                tempcontent = tempcontent.replace(/—/g, xmlcontent);
                thiscontent = thiscontent + tempcontent;
            }
            else
            {
                var thisdate = (parentnode[i].getAttribute(propertarr[2]));
                var newdate = new Date(thisdate);
                var nowdate = new Date();
                if(newdate > nowdate)
                {
                    tempcontent = tempcontent.replace(/—/g, xmlcontent);
                    thiscontent = thiscontent + tempcontent;
                }
                else
                { top = top + 1; }
            }
            i++;
        }
        document.write(thiscontent);
    xmlDoc = null;
}
///广告显示///
///<param=top>显示数量</param>
///<param=property>参数字串</param>
///参数顺序：文件名|子节点|模版编号|分类编号|是否判断过期|链接打开方式
function ConvertAdverts(top,property)
{
    var propertarr = property.split("|");
    var xmlDoc = GetXmlData();
    xmlDoc.async = false;
    xmlDoc.load(propertarr[0]);
    var parentnode = xmlDoc.getElementsByTagName(propertarr[1]);
    var thiscontent = "";
    var i = 0;
    //如果为0取所有，如果要取的数比实际数大取实际数
    if(top == 0 || (top != 0 & top > parentnode.length))
    { top = parentnode.length; }
    while (i < top)
    {
        var typeid = parentnode[i].getAttribute("typeid");
        var tempid = parentnode[i].getAttribute("tempid");
        var codeblock = "";
        //如果节点类型等分类编号
        if(Number(propertarr[2]) == Number(tempid) & Number(propertarr[3]) == Number(typeid))
        {
            var twrite = false;
            //如果要进行过期时间判断
            if(propertarr[4] == "yes")
            {
                var startime = parentnode[i].getAttribute("startime");
                var newstar = new Date(startime);
                var endtime = parentnode[i].getAttribute("endtime");
                var newend = new Date(endtime);
                var tday = new Data();
                if(newstar <= tday & newend >= tday)
                { twrite = true; }
            }
            else
            { twrite = true; }
            //如果写入开关为true执行转换
            if(twrite == true)
            {
                var xmlcontent = (parentnode[i].childNodes[0].nodeValue);
                var anner = parentnode[i].getAttribute("anner");
                var tid = parentnode[i].getAttribute("id");
                var filetypes = GetFileType(anner);
                //如果是flash文件
                if(filetypes[0].indexOf("swf") > 0)
                {
                    codeblock = "<div style=\"position:relative;overflow:auto\"><a href=\"link\" onclick=\"PageViews(" + tid + ");\" target=\"" + propertarr[5] + "\" style=\"width:thewidth;height:theheight;position:absolute;left:0;top:0;z-index:999;display:block;filter:alpha(opacity=1);opacity:0.01;-moz-opacity:0.01;\" title=\"titles\"></a><object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" width=\"thewidth\" height=\"theheight\"><param name=\"movie\" value=\"anner\" /><param name=\"quality\" value=\"high\" /><param name=\"wmode\" value=\"transparent\" /><param name=\"wmode\" value=\"Opaque\" /><embed src=\"anner\" quality=\"high\" pluginspage=\"http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash\" type=\"application/x-shockwave-flash\" width=\"thewidth\" height=\"theheight\" wmode=\"transparent\" wmode=\"opaque\"></embed></object></div>";
                }
                else
                {
                    codeblock = "<a href=\"link\" target=\"" + propertarr[5] + "\"><img src=\"anner\" alt=\"titles\" style=\"width:thewidth;height:theheight\" /></a>";
                }
                var tlink = parentnode[i].getAttribute("link");
                codeblock = codeblock.replace(eval("/link/g"), tlink);
                var twidth = parentnode[i].getAttribute("thewidth");
                twidth = twidth + "px";
                codeblock = codeblock.replace(eval("/thewidth/g"), twidth);
                var theight = parentnode[i].getAttribute("theheight");
                theight = theight + "px";
                codeblock = codeblock.replace(eval("/theheight/g"), theight);
                codeblock = codeblock.replace(eval("/anner/g"), anner);
                codeblock = codeblock.replace("titles", xmlcontent);
            }
            thiscontent = thiscontent + codeblock;
        }
        else
        { top = top + 1 }
        i++;
    }
    document.write(thiscontent);
    xmlDoc = null;
}
///转换输出多节点xml文件///
///<param=xmlfile>XML文件地址</param>
///<param=node>子节点名称字串以|分隔</param>
///<param=property>属性名称字串</param>
///<param=style>输出样式，―表示要替换的内容</param>
function ConvertMoreNodesXML(xmlfile,node,property,style)
{
    var xmlDoc = GetXmlData();
    xmlDoc.async = false;
    xmlDoc.load(xmlfile);
    var nodes = node.split('|');//分离出子节点名称
    var htmltxt = "";
    for(var i = 0; i < nodes.length; i++)
    {
        htmltxt += style.replace('―',xmlDoc.getElementsByTagName(nodes[i])[0].childNodes[0].nodeValue);
        if(property != null)
        {
            var props = property.split('|');//分离出属性名称
            for(var n = 0; n < props.length; n++)
            {
                htmltxt = htmltxt.replace(props[n],xmlDoc.getElementsByTagName(nodes[i])[0].getAttribute(props[n]));
            }
        }
    }
    document.write(htmltxt);
    xmlDoc = null;
}
///不间断滚动内容，需要三个Div///
///<param=div1>层1ID</param>
///<param=div2>层2ID</param>
///<param=div3>层3ID</param>
///<param=scrolltime>滚动速度</param>
function ScrollContfor3Div(div1,div2,div3,scrolltime)
{
    var ii = 0;
    sd = document.getElementById(div1);
    sd1 = document.getElementById(div2);
    sd2 = document.getElementById(div3);
    sd2.innerHTML = sd1.innerHTML;
    sd.onmouseover = function(){ ii = 1; };
    sd.onmouseout = function(){ ii = 0; }
    function theMarquee()
    {
        if(ii==1)
        { return; }
        if(sd2.offsetTop - sd.scrollTop <= 0)
        { sd.scrollTop -= sd1.offsetHeight; }
        else
        { sd.scrollTop++; }
    }
    setInterval(theMarquee,scrolltime);
}
///多层自适应高度，兼容IE、FF、Opera///
///参数示例（'div1','div2'...)
function DivHeightAuto()
{
    var maxheight = 0;
    for (var i = 0; i < DivHeightAuto.arguments.length; i++)
    {
        if(maxheight < document.getElementById(DivHeightAuto.arguments[i]).offsetHeight)
        { maxheight = document.getElementById(DivHeightAuto.arguments[i]).offsetHeight; }
    }
    for(var i = 0; i < DivHeightAuto.arguments.length; i++)
    {
        document.getElementById(DivHeightAuto.arguments[i]).style.height = maxheight + 'px';//必须加上单位否则FF下不会自适应
    }
}