//------------------------------------------------------------------------------
//						与委托系统相关的接口内容
//------------------------------------------------------------------------------
//注册委托的数据请求
//FunName-功能名  CondVal-请求条件值     CallBackFunName-回调函数名(字符串)   FunPara-回调函数的参数
function client_wt_regfunc(FunName , CondVal , CallBackFunName , FunPara )
{
	try
  {
      var obj = client_core_interface() ;
      var rlt = obj.c( "C_WT_PrepareRequest" , FunName + "\r\n" + CondVal , CallBackFunName , FunPara ) ;
      return rlt ;
      
  }
  catch(e)
  {
      _trace("出错(cltRegWTRequest()):"+e.message) ;
      return "0" ;
  }
}

//取得持仓股列表
//reqFields-请求的数据列内容  
//sortField-需要排序的数据列内容 
//sortType-排序类型（0-不排序 1-以int型参与排序 2-以double型参与排序 3-以string型参与排序)
//AscFlag - 升降序标记(1-降序 2-升序 0-不排序) 
//MaxRltCount - 最大返回的记录条目数
function client_wt_holdstocks(reqFields , sortField , sortType , AscFlag , MaxRltCount )
{
	try
	{
		var obj = client_core_interface() ;
		var vPara = reqFields + "\r\n" + sortField + "\r\n" + sortType + "\r\n" + AscFlag + "\r\n" + MaxRltCount ;
		var vData = obj.r( "G_WT_HoldStockList" , vPara ) ;
		
		if( vData == undefined || vData == null || vData.length == 0 ) return null;
		var alDataList = vData.split("\n\n") ;
		if( alDataList == null || alDataList.length <= 1 ) return null;
		
		var alRlt = new Array() ;
		var alTitles = alDataList[0].split("|") ; //标题列
		for( var iPos = 1 ; iPos<alDataList.length ; iPos++ )
		{
			var alTmpData = alDataList[iPos].split("|") ;
			if( alTmpData == undefined || alTmpData == null || alTmpData.length != alTitles.length ) continue;
			
			var tmpObj = new Object() ;
			for( var j=0 ; j<alTmpData.length ; j++ ) tmpObj[alTitles[j]] = alTmpData[j] ;
			alRlt.push( tmpObj ) ;
		}
		
		return alRlt ;
	}
	catch(e)
	{
		_trace("出错(cltRegWTRequest()):"+e.message) ;
		return null ;
	}
}

function client_wt_query_holdstocks(funcname)
{
	//通知委托进行数据准备(由日历模块已经对数据进行通知调用)
	client_wt_regfunc( "FQ_QueryStock" , "" , funcname, "CallBack" ) ;
}

var client_loadscripts = "";
function client_config_valid()
{
		var allSuit = client_suit_namelist();
		if (allSuit == null) { setTimeout("client_config_valid();", 100); return; }
		
		var found = false;
    for(var i=0 ; i<allSuit.length ; i++ )
    {
    	//if (allSuit[i].toLowerCase() != "qlfree") continue;
    	
    	var webbase = client_core_urlbase(allSuit[i]);
    	found = (webbase != null && webbase.length > 0) ; //检测基地址是否正确
    	if( !found ) break ;
    	
    	var tmpCfgStr = client_suit_info(allSuit[i]) ;
    	found = (tmpCfgStr != null && tmpCfgStr.length > 0) ;
    	if( !found ) break ;
    }
    if (! found) { setTimeout("client_config_valid();", 100); return; }
    
    G_STR_WEBROOT = client_core_urlbase("QLFREE");
    setTimeout(client_loadscripts, 10);
}


/******************************************************************************
rssitem 格式
  key             name of rss
  subkeys
******************************************************************************/
var _rss_config = null;
var _rss_hash = null;
var _macromap = null;
function rss_loadconfig()
{
	_rss_config = new Array();
	_rss_hash = new Object();
	_macromap = new Object();
	
	var config = client_suit_info(G_STR_SUITNAME);
	var configxml = $(ql_xml_stringtoxml(config));
	
	configxml.find("Sub > MACRO > I").each(function() {
		if(typeof($(this).attr("name")) != "undefined" ) {
			_macromap[$(this).attr("name")] = $(this).text() ;
		}
	});

	configxml.find("Sub > I").each(function() {
		var thisobj = $(this);
		var rssdef = new Object();

		rssdef.suit = G_STR_SUITNAME;
		rssdef.group = thisobj.attr("g");
		rssdef.key = thisobj.attr("s");
		rssdef.title = typeof(thisobj.attr("t")) == "undefined" ? "" : thisobj.attr("t");
		rssdef.mode = typeof(thisobj.attr("m")) == "undefined" ? "d" : thisobj.attr("m");
		rssdef.isdefault = typeof(thisobj.attr("a")) == "undefined" ? 0 : parseInt(thisobj.attr("a"));
		rssdef.type = typeof(thisobj.attr("type")) == "undefined" ? 1 : parseInt(thisobj.attr("type"));
		rssdef.desc = typeof(thisobj.attr("desc")) == "undefined" ? "" : thisobj.attr("desc");
		rssdef.refresh = typeof(thisobj.attr("refresh")) == "undefined" ? 900 : parseInt(thisobj.attr("refresh"));

		var rssurl =  typeof(thisobj.attr("u")) == "undefined" ? "{1}{s}{2}" : thisobj.attr("u");
		rssurl = rssurl.replace(new RegExp("\\{s\\}","g"), rssdef.key) ;
		if (rssurl != "")
			for (var p in _macromap)
				rssurl = rssurl.replace(new RegExp("\\{" + p + "\\}","g"), _macromap[p]) ;
		rssdef.rssurl = client_core_applymacro(G_STR_SUITNAME, rssurl);

		rssdef.hasfilter = 0;
		var subitems = new Array();
		thisobj.find("F").each(function() {
			var subitemobj = $(this);
			rssdef.hasfilter = 1;
			
			var subobj = new Object();
			subobj.key = subitemobj.attr("s");
			subobj.title = typeof(subitemobj.attr("t")) == "undefined" ? "" : subitemobj.attr("t");
			subobj.msgtype = typeof(subitemobj.attr("p")) == "undefined" ? "" : subitemobj.attr("p");
			
			subitems.push(subobj);
		});
		rssdef.subitems = subitems;

		_rss_config.push(rssdef);
		_rss_hash[rssdef.key] = rssdef;
	});
}

function rss_load()
{
	var rsslist = client_data_load_json(G_CLTKEY_SUBSCRIBE, null, 0);
	if (rsslist == null) return rsslist;
	var retlist = new Array();
	for(var p in rsslist) retlist.push(rsslist[p]);
	return retlist;
}

function rss_save(rsslist)
{
	if (rsslist == null) rsslist = new Array();
	
	{
		var left = 0, right = 0;
		for (var i = 0; i < rsslist.length; i++)
		{
			if (typeof(rsslist[i]["column"]) == "undefined") continue;
			if (rsslist[i]["column"] == 2) right++;
			else left++;
		}
		left += 100;
		right += 100;

		var leftkeys = "", rightkeys = "";

		for (var i = 0; i < rsslist.length; i++)
		{
			if (typeof(rsslist[i]["column"]) != "undefined") continue;

			if (left <= right) {
				left++;
				rsslist[i].column = 1;
				rsslist[i].columnindex = left;
				leftkeys += "," + rsslist[i].key;
			} else {
				right++;
				rsslist[i].column = 2;
				rsslist[i].columnindex = right;
				rightkeys += "," + rsslist[i].key;
			}
		}
		
		rsslist.sort(function(a, b) {
			return (a.column < b.column) * 100 + a.columnindex - b.columnindex;
		});
		
		left = 1;
		right = 1;
		var leftkeys = "", rightkeys = "";
		for (var i = 0; i < rsslist.length; i++)
		{
			if (rsslist[i].column == 1) { rsslist[i].columnindex = left++, leftkeys += "," + rsslist[i].key }
			else { rsslist[i].columnindex = right++, rightkeys += "," + rsslist[i].key };
		}

	}
	
	client_data_save_json(G_CLTKEY_SUBSCRIBE, rsslist, 0);
}

function rss_subscribe_set(rssitem)
{
	var rsslist = rss_load();
	
	if (rsslist == null)
	{
		rsslist = new Array();
		rssitem.column = 1;
		rssitem.columnindex = 1;
		rsslist.push(rssitem);
	}
	else
	{
		var left = 0, right = 0;
		var found = false;
		for (var i = 0; i < rsslist.length; i++)
		{
			if (rsslist[i].key == rssitem.key)
			{
				rsslist[i] = rssitem;
				found = true;
				break;
			}

			if (rsslist[i]["column"] == 2) right++;
			else left++;
		}
		
		if (! found) {
			if (left <= right) {
				rssitem.column = 1;
				rssitem.columnindex = left + 1;
			} else {
				rssitem.column = 2;
				rssitem.columnindex = right + 1;
			}
			rsslist.push(rssitem);
		}
	}
	
	rss_save(rsslist);
}

function rss_subscribe_remove(rsskey)
{
	var rsslist = rss_load();
	if (rsslist == null) return;

	for (var i = 0; i < rsslist.length; i++)
	{
		if (rsslist[i].key == rsskey)
		{
			rsslist.splice(i, 1);
			break;
		}
	}
	
	rss_save(rsslist);
}

function rss_subscribe_get(rsskey)
{
	var rsslist = rss_load();
	if (rsslist == null) return null;
	
	for (var i = 0; i < rsslist.length; i++)
		if (rsslist[i].key == rsskey) return rsslist[i];
	
	return null;	
}
