﻿// JScript 文件
function $(id) {
    return document.getElementById(id);
}

var ajax = {
    url: '',
    method: '',
    parameters: '',
    isAsynchronous: true,
    xmlResult: undefined,
    response: undefined,
    errMes: '',
    getXMLHttp: function() {
        var xmlhttp;
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (ex) {
            try {
                xmlhttp = new window.XMLHttpRequest();
            }
            catch (ex) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (ex) {
                    alert("您的浏览器不支持ajax无法使用我们网站部分功能！！！");
                }
            }
        }
        return xmlhttp;
    },
    onSuccess: function() {
        //     alert(this.xmlResult.xml);
    },
    onErr: function() {
        //    alert(this.errMes);
    },
    open: function() {
        var xmlhttp;
        if (this.url != "") {
            if (!(xmlhttp = this.getXMLHttp())) return;
            if (this.method.toLowerCase() == "post") {
                xmlhttp.open('POST', this.url, this.isAsynchronous);
                xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded");
            }
            else {
                xmlhttp.open('GET', this.url, this.isAsynchronous);
            }
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    if (xmlhttp.status == 200) {
                        ajax.response = xmlhttp.responseText.replace(/<\/?[^>]+>/gi, '').replace('\r\n', '');
                        ajax.xmlResult = xmlhttp.responseXML;
                        if (ajax.xmlResult.getElementsByTagName("err").length > 0) {
                            ajax.errMes = ajax.xmlResult.getElementsByTagName("err")[0].nodeValue;
                            ajax.onErr();
                        }
                        else {
                            ajax.onSuccess();
                        }
                    }
                    else {
                        ajax.errMes = xmlhttp.statusText;
                        ajax.onErr();
                    }
                }
            };
            if (this.method.toLowerCase() == "post") {
                xmlhttp.send(this.parameters);
            }
            else {
                xmlhttp.send(null);
            }
        }
    }
};

function setFontSize(sz) {
    var con = document.getElementById("content");
    con.style.fontSize = sz + 'px';
}
function setFontSizeSmall() {
    setFontSize(12);
}
function setFontSizeBig() {
    setFontSize(16);
}
function setFontSizeMiddle() {
    setFontSize(14);
}
function clearValue(obj) {
    obj.value = "";
}
function showdetail() {
    var d = $('detail');
    var a = $('btnclose');
    if (d.style.display == '') {
        d.style.display = 'none';
        a.value = "打开";
    }
    else {
        d.style.display = '';
        a.value = "关闭";
    }
}
String.prototype.Trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function nSea(id) {
    var key = $(id).value;
    key = key.Trim();
    if (key == '关键字') {
        alert('请输入关键字');
        return false;
    }
    window.open("/common/newslist.aspx?key=" + escape(key));
}
//搜索
function newsSea(id, t) {
    var key = $(id).value;
    key = key.Trim();
    if (key.length < 1) {
        alert('请输入关键字');
        return false;
    }
    if (t == "1")
        window.open("/common/newslist.aspx?key=" + escape(key));
    else
        window.location = "/common/newslist.aspx?key=" + escape(key);
}
window.onload = function() {
    //  islogin();
    GetNews();
    changeA();
}
function GetNews() {
    $('tit').innerHTML = "精彩推荐";
    $('tit1').innerHTML = "最新推荐";
    var url = window.location.href;
    url = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.') + 5);
    ajax.url = "../webService/Renjoin.asmx/MoreNews?url=" + url;
    ajax.onSuccess = function() {
        //精彩推荐-新闻列表
        var jsonString = ajax.response;
        var json = eval("(" + jsonString + ")");
        var node = json.Item0;
        var Inxml = $("newslist");
        if (node != null) {
            for (var i = 0; i < node.length; i++) {
                Inxml.innerHTML += "<li><a href='" + node[i].link + "'>" + node[i].title + "</a></li>";
            }
        }
        node = json.Item1;
        Inxml = $("nlist");
        if (node != null) {
            for (var i = 0; i < node.length; i++) {
                Inxml.innerHTML += "<li><a href='" + node[i].link + "'>" + node[i].title + "</a></li>";
            }
        }
        node = json.Item2;
        Inxml = $('commentlist');
        if (node != null) {
            for (var i = 0; i < node.length; i++) {
                Inxml.innerHTML += "<div class=\"comItem\"><span>" + node[i].username + "</span><span class=\"time\">" + node[i].times + "</span><p>" + node[i].comment + "</p></div>";
            }
            $('morelink').href = "../common/newscom.aspx?nid=" + node[0].newsid;
        }
        //精彩推荐-图片
        node = json.ImgT;
        Inxml = $('limg');
        if (node.length > 0) {
            for (var i = 0; i < node.length; i++) {
                Inxml.innerHTML += "<a href='" + node[i].link + "'><img src='" + node[i].src + "' alt=''/><p>" + node[i].title + "</p></a>";
            }
        }
        node = json.ImgB;
        Inxml = $('limg1');
        if (node.length > 0) {
            for (var i = 0; i < node.length; i++) {
                Inxml.innerHTML += "<a href='" + node[i].link + "'><img src='" + node[i].src + "' alt=''/><p>" + node[i].title + "</p></a>";
            }
        }
        //头部广告
        node = json.top;
        var top = $('topad');
        top.innerHTML = "<img src='" + node[0].src + "' alt='" + node[0].alt + "'/>";
        top.href = node[0].link;

        //右上广告
        node = json.rtop;
        var rtop = $('rtop');

        rtop.innerHTML = "<img src='" + node[0].src + "' alt='" + node[0].alt + "'/>";
        rtop.href = node[0].link;

        //右下广告
        node = json.rbottom;
        var rbottom = $('rbottom');

        rbottom.innerHTML = "<img src='" + node[0].src + "' alt='" + node[0].alt + "'/>";
        rbottom.href = node[0].link;
    };
    ajax.open();
}

var Renjoin = {
    Success: function() { },
    Fail: function() { },
    CheckLogin: function() {
        ajax.url = "../webService/Renjoin.asmx/CheckLogin";
        ajax.onSuccess = function() {
            if (ajax.response != "0") {
                $('logined').style.display = "";
                $('login').style.display = 'none';
                $('uname').innerHTML = "你好：" + ajax.response;
            }
            else {
                $('login').style.display = "";
                $('logining').style.display = 'none';
            }
        };
        ajax.open();
    },
    Login: function(name, pw) {
        ajax.url = "../webService/Renjoin.asmx/Login?name=" + escape(name.Trim()) + "&pw=" + escape(pw.Trim());
        ajax.onSuccess = function() {
            if (ajax.response == "true") {
                Renjoin.Success();
            }
            else {
                Renjoin.Fail();
            }
        };
        ajax.open();
    },
    LoginOut: function() {
        ajax.url = "../webService/Renjoin.asmx/LoginOut";
        ajax.onSuccess = function() {
            Renjoin.Success();
        };
        ajax.open();
    }
};
//登陆
function login() {
    var name = $('username').value.Trim();
    var pw = $('pw').value.Trim();
    if (name.length < 1 || pw.length < 1) {
        alert('用户名和密码不能为空');
        return false;
    }
    $('login').style.display = 'none';
    $('logining').style.display = '';
    Renjoin.Login(name, pw);
    Renjoin.Success = function() {
        $('logined').style.display = "";
        $('logining').style.display = 'none';
        $('detail').style.display = '';
        $('uname').innerHTML = "你好：" + name.Trim();
        $('btnclose').value = "关闭";
    };
    Renjoin.Fail = function() {
        $('login').style.display = "";
        $('logining').style.display = 'none';
    };
}
//退出
function loginout() {
    Renjoin.Success = function() {
        $('login').style.display = '';
        $('logined').style.display = 'none';
        $('detail').style.display = 'none';
    };
    Renjoin.LoginOut();
}
//检测是否已登陆
function islogin() {
    Renjoin.CheckLogin();
}
//调整图片大小
function changeA() {
    var con = $('content');
    var imgs = con.getElementsByTagName('img');
    for (var i = 0; i < imgs.length; i++) {
        if (imgs[i].width > 588) {
            imgs[i].heigth = (imgs[i].heigth / imgs[i].width) * 588;
            imgs[i].width = 588;
        }
    }
}
function putcomment() {
    var url = window.location.href;
    url = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.') + 5);
    var com = $('tbcomment').value;
    com = com.Trim();
    if (com.length < 1) {
        alert('评论内容不能为空');
        return false;
    }
    ajax.url = "../webService/Renjoin.asmx/PutNewscomment?url=" + url + "&comment=" + escape(com);
    ajax.onSuccess = function() {
        GetComment();
    }
    ajax.open();
}
function GetComment() {
    var url = window.location.href;
    url = url.substring(url.lastIndexOf('/') + 1, url.lastIndexOf('.') + 5);
    ajax.url = "../webService/Renjoin.asmx/GetNewscomment?url=" + url;
    ajax.onSuccess = function() {
        var jsonString = ajax.response;
        var json = eval("(" + jsonString + ")");
        var node = json.Item0;
        var Inxml = $('commentlist');
        Inxml.innerHTML = "";
        if (node != null) {
            for (var i = 0; i < node.length; i++) {
                Inxml.innerHTML += "<div class=\"comItem\"><span>" + node[i].username + "</span><span class=\"time\">" + node[i].times + "</span><p>" + node[i].comment + "</p></div>";
            }
            $('morelink').href = "../common/newscom.aspx?nid=" + node[0].newsid;
        }
        $('tbcomment').value = "";
    }
    ajax.open();
}




/////////////////////////////////////////////////////////test
//function img()
//{
////    var re = /<img(.+?)src=""*([^\s]+?)""*(\s|>)/ig;
////        var s = document.getElementById("content").innerHTML;
////        var a = s.match(re);
////        var t;
////        for (var i=0;i<a.length ;i++ )
////        {
////            t = a[i];
////        }
////        alert(t);
//    var re = /<img([^>]+?)>/ig;
//    var s = document.getElementById("content").innerHTML;
//    var str=""
//    var tempImg=""
//    var re1=/alt\s*=\s*([^\s]+)/i
//    var re2=/src\s*=\s*(["'])([^"']+)\1/i
//    while(re.exec(s))
//    {
//        tempImg=RegExp.$1
//    //    str+=((re1.test(tempImg))?RegExp.$1:"")+","
//        re2.test(tempImg)
//        str+="|"+((re2.test(tempImg))?RegExp.$2:"");
//    }
//    if(str.length>0)
//    str=str.substr(1);
//    alert(str);
//}