//infohost,TP数据请求接口

//写入正文 和 queryContent匹配
//id:自定义编号
//title: 标题
//txt:正文
function setZXContent(id, title, txt) {
    var contentid = "content" + id;
    var content_headid = "content" + id + "_header";
    var hd = document.getElementById(content_headid);
    var cnt = document.getElementById(contentid);
    if (hd != null)
        hd.innerHTML = title == undefined ? "" : title;
    if (cnt != null)
        cnt.innerHTML = txt == undefined ? "" : txt;
}

//请求资讯标题列表
//no:自定义编号
//companyid: 资讯公司ID
//treeid:树节点ID
//wantnum: 请求个数
function queryZX(no, companyid, treeid, wantnum) {
    window.external.INFO_FUNCTitle(no, companyid, treeid, wantnum);
    //alert('标题请求成功');
}

//写入标题
function SetINFOTitle(iNo, txt) {
    /*
    //	alert('标题应答收到' + iNo);
    //	alert(iNo + ' '+ txt);	
    var titles = txt.split("<br>");		//取出每一行资讯
    for(index in titles)	
    {
    var title = trim_s(titles[index]);	//去掉空格
    if(title)
    {
    var fields 	= title.split("|");		//取出每条资讯的各个字段
    var divid 	= 'KeyId3';			//要插入的div id
    var href  	= document.createElement('a');
    var span 	= document.createElement('span');
    var br	  	= document.createElement('br');
    var text 	= document.createTextNode(fields[3] + " " + fields[5]); //日期 + 标题 格式
    var link	  	= "http://www.treeid/infocontent_"+title;						 //生成链接
    href.appendChild(text);
    href.setAttribute('href', link);
    span.appendChild(href);
    document.getElementById(divid).appendChild(span);
    document.getElementById(divid).appendChild(br);
    }
    }
    */
    
    var titles = txt.split("<br>"); 	//取出每一行资讯

    var str_temp = "<ul>";
  
  	for(var i = 0;i<titles.length;i++)
    {
    		var title = trim_s(titles[i]); //去掉空格
    		if (title) {
            var fields = title.split("|");        
            var time_str = trim(fields[3]).substring(4, 6) + '-' + trim(fields[3]).substring(6);
            str_temp += "<li style='margin: 10px 0 0 10px;'><a class='ack' style='text-decoration:none;' href='http://www.treeid/infocontent_" + title + "'>" + time_str + " " + trim(fields[5]) + "</a></li>";
        }
    }
    
/*    
    for (index in titles) {
        var title = trim_s(titles[index]); //去掉空格
        if (title) {
            var fields = title.split("|");        
            var time_str = trim(fields[3]).substring(4, 6) + '-' + trim(fields[3]).substring(6);
            str_temp += "<li style='margin: 10px auto;'><a style='text-decoration:none;' href='http://www.treeid/infocontent_" + title + "'>" + time_str + " " + trim(fields[5]) + "</a></li>";
        }
    }
*/    
    
    str_temp += "</ul>";
    
    $("#KeyId" + iNo).html(str_temp);
}

//请求用户相关信息
function queryUserInfo(iNo) {
    window.external.SYS_FUNCUserInfo(iNo);
    //	alert('请求用户信息成功');
}

//写入用户相关信息
function SetSYSUserInfo(iNo, txt) {
    //	alert('收到用户信息成功');
    var div = document.getElementById("userinfo" + iNo);
    if (div != null) {
        var fields = txt.split("|");
        var info = "姓名:" + fields[2] + "<br>" + "账号:" + fields[0]; //连接字符串
        div.innerHTML = info;
    }

}
//请求带摘要的资讯标题列表
//id:自定义编号
//companyid: 资讯公司ID
//treeid:树节点ID
//wantnum: 请求个数
function queryZXBrief(id, companyno, treeid, wantnum) {
    window.external.CB_FUNCZXBRIEF(id, companyno, treeid, wantnum);
}

//按日期请求资讯
//参照queryZXBrief说明
function queryZXBriefByDate(id, companyno, treeid, wantnum, date) {
    window.external.CB_FUNCZXBRIEFBYDATE(id, companyno, treeid, wantnum, date);
}

//请求自选股资讯 id:自定义编号 type:10自选股 11持仓股 
//zxtype:0 研究 1评级 2行业 3公司公告 4公司新闻 5公告和新闻合并
function queryMyStockZX(id, type, zxtype) {
    window.external.INFO_FUNCZXGTitle(id, type, zxtype, 300);
    //  alert('请求自选股成功');
}

//写入自选股请求数据
function SetINFOZXGTitle(iNo, txt) {
    //	alert('收到自选/持仓应答\n'+txt);
    var titles = txt.split("<br>"); 	//取出每一行资讯
    for (index in titles) {
        if (index > 7) //最多只显示7条好了
            break;
        var title = trim_s(titles[index]); //去掉空格
        if (title) {
            var fields = title.split("|"); 	//取出每条资讯的各个字段
            var divid = 'KeyId3'; 		//要插入的div id
            var href = document.createElement('a');
            var span = document.createElement('span');
            var br = document.createElement('br');
            var text = document.createTextNode("(" + fields[1] + ")" + fields[5]); //股票代码 + 标题 格式
            var link = "http://www.treeid/infocontent_" + title; 					 //生成链接
            href.appendChild(text);
            href.setAttribute('href', link);
            span.appendChild(href);
            document.getElementById(divid).appendChild(span);
            document.getElementById(divid).appendChild(br);
        }
    }
}

//请求资讯标题列表
//no:自定义编号
//companyid: 资讯公司ID
//treeid:树节点ID
//wantnum: 请求个数
function queryZX_TP(no, companyid, treeid, wantnum) {
    window.external.TP_FUNCZX(no, companyid, treeid, wantnum);
}

//请求带摘要的资讯标题列表
//id:自定义编号
//companyid: 资讯公司ID
//treeid:树节点ID
//wantnum: 请求个数
function queryZXBrief_TP(id, companyno, treeid, wantnum) {
    window.external.TP_FUNCZXBRIEF(id, companyno, treeid, wantnum);
}

//按日期请求资讯
//参照queryZXBrief说明
function queryZXBriefByDate_TP(id, companyno, treeid, wantnum, date) {
    window.external.TP_FUNCZXBRIEFBYDATE(id, companyno, treeid, wantnum, date);
}

//请求自选股资讯 id:自定义编号 type:10自选股 11持仓股 
//zxtype:0 研究 1评级 2行业 3公司公告 4公司新闻 5公告和新闻合并
function queryMyStockZX_TP(id, type, zxtype) {
    window.external.TP_FUNCMYSTOCKZX(id, type, zxtype);
}

//请求正文
//no:自定义编号
//companyid: 资讯公司ID
//treeid:树节点ID
function queryContent_TP(no, companyid, treeid) {
    window.external.TP_FUNCCONTENT(no, companyid, treeid);
}


//清空页面某个区域
//id:自定义编号
function ClearZX(id) {
    //清空资讯列表
    var divid = 'ZxDiv' + id;
    var zx = document.getElementById(divid)
    if (zx != null)
        zx.innerHTML = "";

    //使"更多"链接隐藏
    var moreid = "more" + id;
    var more = document.getElementById(moreid);
    if (more != null)
        more.style.visibility = "hidden";
}

//显示错误提示信息
//id:自定义编号 msg:错误信息
function SetErrMsg(id, msg) {
    var tableid = "mytable" + id;
    cleartable(tableid);
    AddRow(id, msg, '', '');
}

//点击标题列表第一个链接
function ClickFirstTitle(id) {
    var divid = 'ZxDiv' + id;
    var div = document.getElementById(divid);
    var a = div.firstChild.firstChild.firstChild;
    a.click();
}

//写入标题和摘要 和queryZX、queryZXBrief相匹配
//id:自定义编号
//title:资讯标题
//brief:摘要
//s_link:链接
function AddZXBriefItem(id, title, brief, s_link) {
    //标题
    var divid = 'ZxDiv' + id;
    var newElement = document.createElement('div');
    var href = document.createElement('a');
    var p = document.createElement('p');
    var newText = document.createTextNode(title);
    href.appendChild(newText);
    href.setAttribute('href', s_link);
    p.appendChild(href);
    newElement.appendChild(p);
    newElement.className = 'title';
    document.getElementById(divid).appendChild(newElement);

    //摘要信息
    if (brief != undefined && brief != null && brief != "") {
        var txt = trim(brief);
        var newBrief = document.createElement('div');
        document.getElementById(divid).appendChild(newBrief);
        newBrief.className = 'title_brief';
        newBrief.innerHTML = txt;
    }

    //使"更多"链接可见
    var moreid = "more" + id;
    var more = document.getElementById(moreid);
    if (more != null)
        more.style.visibility = "visible";
}

//当所有标题添加完成后被调用 和queryZX、queryZXBrief相匹配
function AfterAdd(id) {
}

//请求正文
//no:自定义编号
//companyid: 资讯公司ID
//treeid:树节点ID
function queryContent(no, companyid, treeid) {
    window.external.CB_FUNCCONTENT(no, companyid, treeid);
}

var flag = true;
var bbb = 3;
//TP数据请求
function queryTPXML(mainid, assistid, prostr, str) {
    while (flag == false) {
        sleep(500);
    }

    window.external.TP_FUNCXML(mainid, assistid, prostr, str);

    falg = false;
}


//TP数据返回写入HTML
function setTPXML(mainid, assistid, txt) {

    if (mainid == "5") {
        alert("感谢您的反馈！");
        return;
    }

    /*
    alert(txt);
    var tpdivid = "tpdiv"+mainid;
    var cnt = document.getElementById(tpdivid);
    if(cnt != null)
    {
    cnt.innerHTML = txt==undefined?"":txt;		
    }
    */
    txt == undefined ? "" : txt;

    var html_str = "";
    var myObject_dy = eval('(' + txt + ')');

    if (mainid == '1') {  //显示排行数据信息
        for (var i = 0; i < myObject_dy.length; i++) {
            //        for (var key in myObject_dy[i]) {
            //            alert("key：" + key + ",value：" + myObject_dy[i][key]);
            //        }
            if (i < 3) {

                html_str += "<li><p><span>" + (i + 1) + "</span><a href='http://www.treeid/py_GF金融超市首页'>" + myObject_dy[i]["d_name"] + "</a></p><img src='images/icon_hot.gif'/></li>";
            }
            else {
                html_str += "<li><span>" + (i + 1) + "</span><a href='http://www.treeid/py_GF金融超市首页'>" + myObject_dy[i]["d_name"] + "</a></li>";
            }
        }

        bbb = 2;
    }

    if (mainid == '2') {
        for (var i = 0; i < myObject_dy.length; i++) {
            html_str += "<li><span>" + (i + 1) + "</span><a href='http://www.treeid/py_GF金融超市首页'>" + myObject_dy[i]["d_name"] + "</a></li>";
        }
        bbb = 1;
    }

    if (mainid == '3') {

        $("#tb_name_span").html(myObject_dy["name"]);
        $("#tb_tel_span").html(myObject_dy["phone"]);
        $("#tb_mb_span").html(myObject_dy["mobile"]);
        $("#tb_mail_span").html(myObject_dy["mail"]);

        bbb = 0;
    }

    if (mainid == '4') {

        html_str = "<p>" + myObject_dy["Title"] + "</p><p>&nbsp;<input name='Radio1' type='radio' value='1' checked='checked'/>" + myObject_dy["A"] + "&nbsp;<input name='Radio1' type='radio' value='2' />" + myObject_dy["B"] + "&nbsp;<input name='Radio1' type='radio' value='3' />" + myObject_dy["C"] + "&nbsp;<input id='Button1' type='button' class='btn_53in' value='投 票' onclick='Tp_Ck(" + myObject_dy["ID"] + ")' /></p>";

        $("#wdcp_span").html(html_str);
        
        bbb = -1;
        flag = true;
        return;
    }


    $("#span_div" + mainid).html(html_str);


    if (bbb == 2) {
        queryTPXML(2, 2, 'GFGetPackRankInfohttp:GetFillPack', "{'Param': {'Client_ID': '" + khh_str + "','YYB':'"+ yyb_str +"', 'ResFlag': 1}}");
        return;
    }
    if (bbb == 1) {
        queryTPXML(3, 3, 'GFGetPackRankInfohttp:GetTGInfo', "{'Param': {'Client_ID': '" + khh_str + "','YYB':'"+ yyb_str +"', 'ResFlag': 1}}");
    }
    if (bbb == 0) {
        queryTPXML(4, 4, 'GFGetPackRankInfohttp:GetWJ', "{'Param': {'Client_ID': '" + khh_str + "','YYB':'"+ yyb_str +"', 'ResFlag': 1}}");
    }


    flag = true;
}

/*
funno:功能号
01 自选股实时行情数据(证券代码、名称、当前价、涨跌幅、昨收、今开、成交量、最高、最低、量比)
02 持仓股实时行情数据(同上)
03 持仓股资产数据(货 币、股票市值、基金市值、可用余额、总 资 产)
04 资产配置信息(证券账户、基金产品、理财产品、期货产品)
05 净值增长率信息
06 客户信息(客户账号、姓名、客户级别、签约状态、专治理财顾问、联系电话、客户登记、客户基本分、投资风险类型,)
			 
param1: 附加参数1
param2: 附加参数2
*/
function SYS_FUNCData(mainid, assistid, funno, param1, param2) {
    window.external.SYS_FUNCData(mainid, assistid, funno, param1, param2);
}

/*
回调接口
data		:json格式组织数据
errmsg	:错误信息
*/
function SetSYSData(mainid, assistid, funno, data, errmsg) {
}

function sleep(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

//去掉空格
function trim(str) {
    return str.replace(/\s|　/g, " ");
}

function trim_s(str) {
    return str.replace(/(^\s*)|(\s*$)/g, "");
}


function trim_str(str) {
    return str.replace(/^\s+|\s+$/g, "");
}

//获取当天日期yyyymmdd
function getymd() {
    date = new Date();
    var ymd = date.getFullYear() * 10000 + (date.getMonth() + 1) * 100 + date.getDate();
    return ymd;
    //alert(str);
}

//得到当天日期yyyy-mm-dd
function getFmtYmd() {
    date = new Date();
    var ymd = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    return ymd;
}

//保存大中小字体设置
function SetCookie(name, value) { var exp = new Date(); exp.setTime(exp.getTime() + 365 * 24 * 60 * 60 * 1000); document.cookie = name + '=' + escape(value) + ';expires=' + exp.toGMTString(); }
function getCookie(name) { var arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)')); if (arr != null) return unescape(arr[2]); return null; }
function doZoom(size) {
    var cnt = document.getElementById('content1');
    if (size == null)
        size = 14;

    if (cnt != null) {
        cnt.style.fontSize = size + 'px';
        SetCookie('fontsize', size);
    }
}

//屏蔽右键和选择
document.oncontextmenu = new Function("event.returnValue=false;");
document.onselectstart = new Function("event.returnValue=false;");

