//-------------------------------------------------------------------------------------------------
// client core interface
//-------------------------------------------------------------------------------------------------

var G_Parent = null;
var G_MODE="debug";

//套组名（钱龙免费资讯）
var G_STR_SUITNAME = "QLFREE" ;

//全局变量，是否为模拟的Demo数据
var gModeDemo = 0 ;

//得到操作的控件
function client_external()
{
	return window.external.qlC;
}

function client_core_interface()
{
	if (G_Parent == null)
		G_Parent = getParent(window);
    return G_Parent.client_external() ;
}

function getParent(win)
{
	if (win.opener == null)
	{
		
		if (win.parent == win)
		{
			return win;
		}
		else
		{
			return getParent(win.parent);
		}
	}
	else
	{
		return getParent(win.opener);
	}
}
///////////////////////////register debug key/////////////////////////////////////////
//constructor
function Keymap(bindings){
	this.map = {};
	if(bindings){
		for(name in bindings) this.map[name.toLowerCase()] = bindings[name];
	}
}

//bind func to key
Keymap.prototype.bind = function(key, func){
 this.map[key.toLowerCase()] = func;
}

//unbind key
Keymap.prototype.unbind = function(key){
 delete this.map[key.toLowerCase()];
}

//install the keymap on specified html element
Keymap.prototype.install = function(element){
	var keymap = this;
	function handler(event){ return keymap.dispatch(event);}

	if(element.addEventListener){
		element.addEventListener("keydown", handler, false);
		element.addEventListener("keypress", handler, false);
	}
	else if(element.attachEvent){
		element.attachEvent("onkeydown", handler);
		element.attachEvent("onkeypress", handler);
	}
	else{
		element.onkeydown = element.onkeypress = handler;
	}
};

//function key
Keymap.keyCodeToFunctionkey = {
	8: "backspace", 9: "tab", 13: "return", 27: "escape", 32: "space",
	33: "pageup", 34: "pagedown", 35: "end", 36: "end", 37: "left", 38: "up",
	112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7",
	119: "f8", 120: "f9", 121: "f10", 122: "f11", 123: "f12",
	114: "numlock", 145: "scrolllock"
};

//printable key
Keymap.keyCodeToPrintableChar = {
	48: "0", 49: "1", 50: "2", 51: "3", 52: "4", 53: "5", 54: "6", 55: "7", 56: "8", 57: "9",
	59: ";", 61: "=",
	65: "a", 66: "b", 67: "c", 68: "d", 69: "e", 70: "f", 71: "g", 72: "h", 73: "i", 74: "j", 75: "k", 76: "l", 77: "m",
	78: "n", 79: "o", 80: "p", 81: "q", 82: "r", 83: "s", 84: "t", 85: "u", 86: "v", 87: "w", 88: "x", 89: "y", 90: "z",
	107: "+", 109: "-", 110: ".", 188: ",", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\", 219: "]", 222: "\""
};

//dispatch key events based on the keymap bindings
Keymap.prototype.dispatch = function(event){
	var e = event || window.event;
	
	var modifiers = "";
	var Keyname = null;
	
	if(e.type == "keydown"){
		var code = e.keyCode;
		if(code==16 || code==17 || code==18) return;
	
		keyname = Keymap.keyCodeToFunctionkey[code];
	
		if(!keyname && (e.altKey || e.ctrlKey))
		keyname = Keymap.keyCodeToPrintableChar[code];
	
		if(keyname){
			if(e.altKey) modifiers += "alt_";
			if(e.ctrlKey) modifiers += "ctrl_";
			if(e.shiftKey) modifiers += "shift_";
		}
		else return;
	}
	else if(e.type == "keypress"){
		if(e.altKey || e.ctrlKey) return;
	
		if(e.charCode != undefined && e.charCode ==0) return;
	
		var code = e.charCode || e.keyCode;
	
		keyname = String.fromCharCode(code);
	
		var lowercse = keyname.toLowerCase();
		if(keyname != lowercse){
			keyname = lowercse;
			modifiers = "shift_"; 
		}
	}
	var func = this.map[modifiers + keyname];
	if(!func) func = this.map["default"];
	
	if(func){
		var target = e.target;
		if(!target) target = e.srcElement;
	
		func(target, modifiers+keyname, e);
	
		if(e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
		if(e.preventDefault) e.preventDefault();
		else e.returnValue = false;
		return false;
	}
};

function handlerReflash(){
	window.location = window.location;
}

function handlerDebug(){
	var strHref = window.location.href; 
	var pageName = strHref.slice(strHref.lastIndexOf( "/ ")+1);

	window.open(pageName,'debug');
}
	
keymap = new Keymap({"ctrl_r":handlerReflash,"ctrl_left":handlerDebug});
keymap.install(document);
///////////////////////////register debug key/////////////////////////////////////////



function _trace(s)
{
    try
    {

        var obj = client_core_interface() ;
        obj.r("S_Debug_Print" , s );
    }
    catch(e)
    {}
}


//注册动态数据的刷新
// stkList 股票列表，格式为 市场,商品代码|市场,商品代码|市场,商品代码
// reqDataId 请求的数据列编号
// CallbackFun 回调的函数名称（function对象）
// FunPara 回调函数的参数
function client_data_dynamic_callback(stkList , reqDataId , CallBackFun , FunPara)
{
    try
    {
        var obj = client_core_interface() ;
        var iStockCount = obj.c( "C_Dyn_CheckData" , stkList + "\r\n" + reqDataId , __client_getfunctionname(CallBackFun) , FunPara ) ;
        return iStockCount ;
    }
    catch(e)
    {
        _trace("出错(RegDynData()):"+e.message) ;
    }
}
function __client_getfunctionname(funcobj)
{
    var strFun = funcobj.toString() ;
    strFun = strFun.substr(0,strFun.indexOf('(')) ;
    strFun = strFun.replace('function', '') ;
    strFun = strFun.replace(' ','') ;
    return strFun ;
}

function client_callback_blocksort_stop(block)
{
    try
    {
        var obj = client_core_interface() ;
        var iStockCount = obj.c(
        	"C_Dyn_CheckBlcokSort" ,
        	block + "\r\n1\r\n99",
        	"",
        	""
        ) ;
        return iStockCount ;
    }
    catch(e)
    {
        _trace("出错(RegDynData()):"+e.message) ;
    }
}

//-------------------------------------------------------------------------------------------------
// 宏、配置方法
//-------------------------------------------------------------------------------------------------
//活的runtime的键值
//iType==1 完整的runtime包
//iType==2 runtime的MD5摘要
function client_core_runtimekey(suitName , iType)
{
	try
	{
		var obj = client_core_interface() ;
		var rlt = obj.r( "G_SYS_Runtime" , suitName + "\r\n" + iType ) ;
		return rlt ;
	}
	catch(e)
	{
		_trace("出错(GetStockData()):"+e.message) ;
		return "" ;
	}
}

//对于系统的宏指令进行替换
//链接中出现的[M:WEBBASE],[M:RTMMD5],[M:RTM]进行宏替换
function client_core_applymacro(suitName , inStr)
{
	if( typeof(suitName) == "undefined" || suitName == null || suitName.length == 0 )
		return inStr ;
	if( typeof(inStr) == "undefined" || inStr == null || inStr.length == 0 )
		return inStr ;
	if( inStr.toUpperCase().indexOf("[M:WEBBASE]") >= 0 )
	{
		var baseUrl = client_core_urlbase(suitName) ;
		if( baseUrl != null && baseUrl.length > 0 )
			inStr = inStr.replace(/\[M:WEBBASE\]/ig , baseUrl ) ;
	}
	if( inStr.toUpperCase().indexOf("[M:RTMMD5]") >= 0 )
	{
		var chg = client_core_runtimekey(suitName , 2 ) ;
		if( chg.length > 0 )
			inStr = inStr.replace(/\[M:RTMMD5\]/ig , chg ) ;
	}
	if( inStr.toUpperCase().indexOf("[M:RTM]") >= 0 )
	{
		var chg = client_core_runtimekey(suitName , 1 ) ;
		if( chg.length > 0 )
			inStr = inStr.replace(/\[M:RTM\]/ig , chg ) ;
	}
	return inStr ;
}

//得到某一类资讯的基地址
function client_core_urlbase(suitName)
{
	if( suitName == null )
		return null ;	
	if( gModeDemo == 1 )
	{
		//Debug 返回一个固定的地址
		return "http://192.168.3.144/" ;
	}
	try
  {
      var obj = client_core_interface() ;
      var stkData = obj.r( "G_SYS_SuitUrl" , suitName ) ;
      return stkData ;
  }
  catch(e)
  {
      _trace("出错(GetStockData()):"+e.message) ;
  }
}

//检测用户的权限情况
function client_core_checkright(rightval)
{
  try
  {
  	if( typeof(rightval) == "undefined" || rightval == null || rightval.length == 0 )
  		return false ;
    if( isNaN(Number(rightval)) )
        return false ;
    var obj = client_core_interface() ;
    return (obj.r( "G_SYS_URight" , rightval ) == "1") ;
  }
  catch(e)
  {
    _trace("出错(client_core_checkright()):"+e.message) ;
    return false ;
  }
}

function client_core_checkrightlist(rightval)
{
  try
  {
  	if( typeof(rightval) == "undefined" || rightval == null || rightval.length == 0 ) return true;
		var rightlist = rightval.split(',');

    var obj = client_core_interface() ;
		for (var i = 0; i < rightlist.length; i++)
		{
			if( isNaN(Number(rightlist[i])) ) continue;
			if (obj.r( "G_SYS_URight" , rightlist[i] ) == "1") return true;
		}
  }
  catch(e)
  {
    _trace("出错(client_core_checkright()):"+e.message) ;
  }

  return false ;
}

//-------------------------------------------------------------------------------------------------
// 鼠标、视图方法等涉及界面的方法
//-------------------------------------------------------------------------------------------------
//模拟点击一次客户端的上下文关联菜单
function client_core_contentmenu(menustr)
{
	try
	{
		var obj = client_core_interface() ;
		obj.r( "S_View_ClickContextMenu" , menustr ) ;
	}
	catch(e)
	{
		_trace("出错(cltClickContextMenu()):"+e.message) ;
	}
}

//得到当前客户端视图的属性内容
//viewinfo 1-当前的视图信息 2-视图的全局信息
function client_core_viewattr(viewinfo)
{
	try
	{
		var obj = client_core_interface() ;
		var tmpStr = obj.r( "G_View_Attr" , viewinfo ) ;
		if( tmpStr == null || tmpStr.length == 0 )
			return null ;
		var rltObj = new Object() ;
		var tmpArr = tmpStr.split("\r\n") ;
		var i = 0;
		for( i=0 ; tmpArr!=null && i<tmpArr.length ; i++ )
		{
			var tmpPos = tmpArr[i].indexOf(",") ;
			if( tmpPos>0 )
				rltObj[tmpArr[i].substr(0,tmpPos)] = tmpArr[i].substr(tmpPos+1 , tmpArr[i].length - tmpPos - 1 ).replace(/\n\n/g,"\r\n") ;
		}
		return rltObj ;
	}
	catch(e)
	{
		_trace("出错(cltGetViewAttr()):"+e.message) ;
		return null ;
	}
}

function client_core_gotoview(viewName , viewParaObject)
{
	try
	{
		var obj = client_core_interface() ;
		var viewPara = "" ;
		if( viewParaObject != null )
		{
            for(prop in viewParaObject)  
            {
                if( viewParaObject[prop].indexOf(",") > 0 )
                    viewPara += prop.toString() + "=(" + viewParaObject[prop] + ")," ;
                else
                    viewPara += prop.toString() + "=" + viewParaObject[prop] + "," ;
            }
		}
		if( viewPara.length > 0 )
		    viewPara = viewPara.substr(0,viewPara.length -1 ) ;
		var sViewGotoPara = viewName + "\r\n" + viewPara ;
		obj.r( "S_View_Goto" , sViewGotoPara ) ;
	}
	catch(e)
	{
		_trace("出错(GetStockData()):"+e.message) ;
	}
}

function client_core_SendMessage(msgName , WParam , LParam)
{
	try
	{
		var obj = client_core_interface() ;
		var para = msgName+"\r\n"+WParam+"\r\n"+LParam ;
		obj.r("S_View_Param_Message" , msgName+"\r\n"+WParam+"\r\n"+LParam ) ;
	}
	catch(e)
	{
		_trace("出错(client_core_SendMessage()):"+e.message) ;
	}
}

function client_core_gotoworktable(viewName , viewParaObject)
{
	try
	{
		var obj = client_core_interface() ;
		var viewPara = "" ;
		if( viewParaObject != null )
		{
            for(prop in viewParaObject)  
            {
                if( viewParaObject[prop].indexOf(",") > 0 )
                    viewPara += prop.toString() + "=(" + viewParaObject[prop] + ")," ;
                else
                    viewPara += prop.toString() + "=" + viewParaObject[prop] + "," ;
            }
		}
		if( viewPara.length > 0 )
		    viewPara = viewPara.substr(0,viewPara.length -1 ) ;
		var sViewGotoPara = viewName + "\r\n" + viewPara ;
		obj.r( "S_WT_Goto" , sViewGotoPara ) ;
	}
	catch(e)
	{
		_trace("出错(GetStockData()):"+e.message) ;
	}
}

//-------------------------------------------------------------------------------------------------
// 市场准备
//-------------------------------------------------------------------------------------------------
function client_market_prepare(market, callback)
{
	try
	{
		var obj = client_core_interface() ;
		return obj.c( "C_Dyn_PrepareMarketConnect", market, callback, "[JS:MKCODE]" );
	}
	catch(e)
	{
		_trace("出错(client_market_prepare()):"+e.message) ;
		return null ;
	}
}

//-------------------------------------------------------------------------------------------------
// 工作台
//-------------------------------------------------------------------------------------------------
//得到的内容为一项HASH信息
function client_worktable_initenv()
{
	try
	{
		var obj = client_core_interface() ;
		var tmpStr = obj.r( "G_View_WorkTableInitEnv" , "" ) ;
		if( tmpStr == null || tmpStr.length == 0 )
			return null ;
		var rltObj = new Object() ;
		var tmpArr = tmpStr.split("\r\n") ;
		var i = 0;
		for( i=0 ; tmpArr!=null && i<tmpArr.length ; i++ )
		{
			var tmpPos = tmpArr[i].indexOf(",") ;
			if( tmpPos>0 )
				rltObj[tmpArr[i].substr(0,tmpPos)] = tmpArr[i].substr(tmpPos+1 , tmpArr[i].length - tmpPos - 1 ).replace(/\n\n/g,"\r\n") ;
		}
		return rltObj ;
	}
	catch(e)
	{
		_trace("出错(cltGetWorkTableInitEnv()):"+e.message) ;
		return null ;
	}
}

//创建一个工作台
function client_worktable_create(para)
{
	try
	{
		var obj = client_core_interface() ;
		var tmpStr = obj.r( "S_View_CreateWorkTable" , para ) ;
	}
	catch(e)
	{
		_trace("出错(cltCreateWorkTable()):"+e.message) ;
		return ;
	}
}


//-------------------------------------------------------------------------------------------------
// 套组
//-------------------------------------------------------------------------------------------------
//得到所有的套组名
function client_suit_namelist()
{
	try
	{
		var obj = client_core_interface() ;
		var t1 = obj.r( "G_Frame_AllSuitName" , "" ) ;
		if( t1 == null || t1.length == 0 )
			return null ;
		var suitArr = t1.split("\r\n") ;
		if( suitArr == null || suitArr.length == 0 )
			return null ;
		return suitArr ;
	}
	catch(e)
	{
		_trace("出错(cltGetAllSuitName()):"+e.message) ;
		return null ;
	}
}

//得到某个套组的配置信息内容
//入参：suitname-套组名
function client_suit_info(suitName)
{
	try
	{
		var obj = client_core_interface() ;
		var t1 = obj.r( "G_Frame_ClientInfoCfg" , suitName ) ;
		if( t1 == null || t1.length == 0 )
			return "" ;
		return t1 ;
	}
	catch(e)
	{
		_trace("出错(cltGetSuitInfoCfg()):"+e.message) ;
		return "" ;
	}
}

//得到券商编号
function client_qscode()
{
	return QSCODE_CSCO ;
}

//-------------------------------------------------------------------------------------------------
// 资讯中心
//-------------------------------------------------------------------------------------------------
//资讯中心中，导航到某一个网页
//pTarget 打开的目标，0-资讯中心的主显示画面 1-弹出的IE窗口
//strUrl 打开的连接地址
function client_infocenter_navurl(pTarget , strUrl)
{
	try
    {
        var obj = client_core_interface() ;
        obj.r( "S_Info_NavUrl" , pTarget + "\r\n" + strUrl ) ;
    }
    catch(e)
    {
        _trace("出错(GotoInfoCenterMenu()):"+e.message) ;
    }
}


//-------------------------------------------------------------------------------------------------
// 股票数据获取
//-------------------------------------------------------------------------------------------------
//得到某一个股票的数据内容
//mkstkcode 为 市场,代码
function client_stock_data_fullcode(fullcode, colids, colnames)
{
	var obj = client_core_interface();
	var rawdata = obj.r( "G_StkData" , fullcode + "\r\n" + colids ) ;
	if( rawdata == null || rawdata.length == 0 ) return null;
	var lines = rawdata.split("\r\n");
	
	var columns = colnames.split('|');
	var dataarray = new Array();
	for (var i = 0; i < lines.length; i++)
	{
		if (lines[i] == "") continue;
		var values = lines[i].split('|');
		
		var obj = new Object();
		for (var j = 0; j < columns.length++; j++)
		{
			if (j >= values.length) break;
			obj[columns[j]] = values[j];
		}
		dataarray.push(obj);
	}
	
	return dataarray;
}
function client_stock_data(market, code, colids, colnames)
{
	client_stock_data_fullcode(market + ',' + code, colids, columns);
}

//得到股票开盘，收盘的时间(按照东八当前时区处理)
//入参的格式为 市场,代码
//出参的格式为 数组，每个内容为 BeginTime , EndTime
function client_stock_timeperiod_fullcode(fullcode)
{
	try
	{
		var obj = client_core_interface() ;
		
		var stkData = obj.r( "G_Dyn_StockOpenTimePeriod" , fullcode + "\r\n8" ) ;
		if( stkData == null || stkData == undefined || stkData.length == 0 )
			return null ;
		var stkArr = stkData.split("\n\n") ;
		if( stkArr == null || stkArr.length == 0 )
			return null ;
		var alRlt = new Array() ;
		for( var i=0 ; i<stkArr.length ; i++ )
		{
			var tmpData = stkArr[i].split("|") ;
			if( tmpData != null && tmpData.length == 2 )
			{
				var tmpObj = new Object() ;
				tmpObj.BeginTime = parseInt( tmpData[0] ) ;
				tmpObj.EndTime = parseInt( tmpData[1] ) ;
				alRlt.push( tmpObj ) ;
			}
		}//for
		return alRlt ;
		
	}
	catch(e)
	{
		_trace("出错(cltGetStockData2()):"+e.message) ;
		return null ;
	}
}
function client_stock_timeperiod(market, code)
{
	client_stock_timeperiod_fullcode(market + "," + code);
}

//得到商品的历史数据内容
//strPtr 商品代码 SH,600000
//BeginDate 开始时间 YYYYMMDD ,  填 0 表示从第一根开始请求
//EndDate 结束时间 YYYYMMDD , 填 0 表示请求道最近的一根数据
//WeightType 除权复权方式 0-不除权 2-后复权 3-前复权
function client_stock_hisdata(strPtr , BeginDate , EndDate , WeightType)
{
	try
	{
		var obj = client_core_interface() ;
		var p1 = obj.r( "G_Dyn_StockHisData" , strPtr + "\r\n" + BeginDate + "\r\n" + EndDate + "\r\n" + WeightType ) ;
		//无任何数据内容
		if( p1 == null || p1 == "" )
			return null ;
		var stkArr = p1.split("\n\n") ;
		if( stkArr == null || stkArr.length == 0 )
			return null ;
		var alRlt = new Array() ;
		for( var i=0 ; i<stkArr.length ; i++ )
		{
			var tmpData = stkArr[i].split("|") ;
			if( tmpData != null && tmpData.length >= 6 )
			{
				var tmpObj = new Object() ;
				tmpObj.Date = tmpData[0] ;
				tmpObj.Open = tmpData[1] ;
				tmpObj.High = tmpData[2] ;
				tmpObj.Low = tmpData[3] ;
				tmpObj.Close = tmpData[4] ;
				tmpObj.Volume = tmpData[5] ;
				tmpObj.Amount = tmpData[6] ;
				alRlt.push( tmpObj ) ;
			}
		}//for
		return alRlt ;
		
	}
	catch(e)
	{
		_trace("出错(cltGetStockData2()):"+e.message) ;
		return null ;
	}
}

//注册股票的历史数据内容
function client_stock_hisdata_callback(stkList , startdate , CallBackFunctionName , CallBackPara)
{
	try
	{
		var obj = client_core_interface() ;
		
		var stkData = obj.c( "C_Dyn_SynStockHisData" , stkList + "\r\n" + startdate , CallBackFunctionName , CallBackPara ) ;
		return stkData ;
		return "" ;
	}
	catch(e)
	{
		_trace("出错(cltGetStockData2()):"+e.message) ;
		return "" ;
	}
}

function client_stock_selflist(blockid)
{
    try
    {
        var selfStock ;
        if( gModeDemo == 1 )
        {
            selfStock = "SH,600682|SZ,000550|SH,600637|SH,600602|SZ,000550|SZ,000625|SH,600592|SH,600893|SH,600535|SZ,000779|SH,600521" ;
        }
        else
        {
            var obj = client_core_interface() ;
            selfStock = obj.r( "G_SelfStocks" , blockid ) ;
        }
        if( selfStock == null || selfStock.length == 0 ) return null;

        var items = selfStock.split("|") ;
        var i ;
        var rltArr = new Array() ;
        for(  i=0 ; i<items.length ; i++ )
        {
            if( items[i] == null || items[i].length == 0 ) continue;
            var tmp1 = items[i].split(",") ;
            if( tmp1 == null || tmp1.length < 2 ) continue ;

            var pObj = new Object() ;
            pObj.Market = tmp1[0] ;
            pObj.StockCode = tmp1[1] ;
            rltArr.push( pObj ) ;
        }
        return rltArr ;
    }
    catch(e)
    {
        _trace("出错(GetSelfStockList()):"+e.message) ;
    }
}

//-------------------------------------------------------------------------------------------------
// 股票数据获取
//-------------------------------------------------------------------------------------------------
//4.3	请求将某一个系统板块保持在排序的刷新状态
//blockPtr 板块名 市场,板块名称
//sortId 排序序号
//ascFlag 是否升序(1-升序 0-降序)
function client_block_callback_sort(blockPtr , sortId , ascFlag ,CallBackFunctionName , CallBackPara)
{
	try
	{
		var obj = client_core_interface() ;
		var callPara = blockPtr + "\r\n" + sortId + "\r\n" + ascFlag ;
		var rlt = obj.c( "C_Dyn_CheckBlockSort" , callPara , CallBackFunctionName , CallBackPara ) ;
		return rlt ;
	}
	catch(e)
	{
		_trace("出错(cltRegSysBlockDyn()):"+e.message) ;
		return "0" ;
	}
}

function client_block_callback_sortstop(blockPtr , sortId , CallBackFunctionName , CallBackPara)
{
    try
    {
        var obj = client_core_interface() ;
        var iStockCount = obj.c(
        	"C_Dyn_CheckBlcokSort" ,
        	blockPtr + "\r\n" + sortId + "\r\n99",
        	"",
        	""
        ) ;
        return iStockCount ;
    }
    catch(e)
    {
        _trace("出错(RegDynData()):"+e.message) ;
    }
}


//4.4	请求某一个系统板块的排序数据内容
//blockPtr 板块名 市场,板块名称
//sortId 排序序号
//ascFlag 是否升序(1-升序 0-降序)
//Cols 需要显示的列
//MaxRltCount 最大返回条数
function client_block_rptdata(blockPtr , sortId , ascFlag , Cols , ColNames, MaxRltCount)
{
	try
	{
		var ColArr = Cols.split("|") ;
		if( ColArr == null || ColArr.length == 0 )
			return null ;
		var obj = client_core_interface() ;
		var rltData = obj.r( "G_Dyn_GroupData" , blockPtr + "\r\n" + sortId + "\r\n" + ascFlag + "\r\n" + Cols + "\r\n" + MaxRltCount ) ;

		if( rltData == null || rltData.length == 0 ) return null;
		
		rltData = rltData.replace(/\r/g, "");
		var lines = rltData.split("\n");
		
		var columns = ColNames.split('|');
		var dataarray = new Array();
		for (var i = 0; i < lines.length; i++)
		{
			if (lines[i] == "") continue;
			var values = lines[i].split('|');
			
			var obj = new Object();
			for (var j = 0; j < columns.length++; j++)
			{
				if (j >= values.length) break;
				obj[columns[j]] = values[j];
			}
			dataarray.push(obj);
			
			if (dataarray.length >= MaxRltCount) break;
		}
	
		return dataarray;
	}
	catch(e)
	{
		_trace("出错(clgGetSysBlockDataRpt()):"+e.message) ;
		return null ;
	}
	
}

//----------------------------------------------------------------------------------------------------------
// 数据保存、载入相关                                              -------------
//----------------------------------------------------------------------------------------------------------
function client_data_save(keyName , opType , dataValue , regionType)
{
  try
  {
      var obj = client_core_interface() ;
      var stkData = obj.r( "S_Etc_SaveData" , keyName + "\r\n" + opType + "\r\n" + dataValue.replace(/\r\n/g , "\n\n") + "\r\n" + regionType ) ;
      return stkData ;
  }
  catch(e)
  {
      _trace("出错(client_data_save()):"+e.message) ;
      return "0" +e.message ;
  }
}

//9.5	提取某个已经寄存的数据项(S_Etc_LoadData)
//keyName 参数名称
//regionType 数据存放的位置 0-存放于永久数据区 1-存放于可替换数据区
function client_data_load(keyName , regionType)
{
	try
  {
      var obj = client_core_interface() ;
      var stkData = obj.r( "S_Etc_LoadData" , keyName + "\r\n" +  regionType ) ;
      if( stkData == null || stkData.length == 0 )
      	return null ;
      var iPos = stkData.indexOf("\r\n") ; //查看附带的时间戳
      if( iPos <= 0 )
      	return null ;
      var timeStamp = stkData.substring(0,iPos) ;
      var pData = stkData.substring( iPos + 2 , stkData.length ) ;
      //时间戳的格式 YYYYMMDD HH:MM:SS
      if( timeStamp.length != 17 )
      	return null ;
      	
      var rltObj = new Object() ;
      //特别注意，javascript中，mm月份的定义为(0-11)，所以这里必须做 -1 操作
      rltObj.TimeStamp = new Date( timeStamp.substring(0,4) , timeStamp.substring(4,6)-1 , timeStamp.substring(6,8) ,
      															timeStamp.substring(9,11) , timeStamp.substring(12,14) , timeStamp.substring(15,17) ) ;
      rltObj.dValue = pData.replace(/\n\n/g , "\r\n");
      return rltObj ;
  }
  catch(e)
  {
      _trace("出错(client_data_load()):"+e.message) ;
      return null ;
  }
}

// get client string of datakey,
// return null if datakey is not exists
function _clientdata_get(datakey,regionType)
{
	if( typeof(client_data_load) == "undefined" )
		return null ;
	var dataObj = client_data_load(datakey , regionType ) ;
	if( dataObj == null )
		return null ;
	if( typeof(dataObj.dValue) == "undefined"  )
		return null ;
	return dataObj.dValue ;
}

// set datastr save in datakey in client
function _clientdata_set(datakey, datastr , regionType)
{
	if( typeof(client_data_save) == "undefined" )
		return ;
	client_data_save(datakey , 1 , datastr , regionType );
	return ;
}

// set datastr save in datakey in client
function _clientdata_del(datakey , regionType)
{
	if( typeof(client_data_save) == "undefined" )
		return ;
	client_data_save(datakey , 0 , "" , regionType ) ;
}

/*******************************************************************************
 javascript interface functions
 datakey : key for client save, must be a string
 dataobj : object to save, can be object, array and other value types
 regionType : 数据存放的位置 0-存放于永久数据区 1-存放于可替换数据区
*******************************************************************************/
function client_data_load_json(datakey, dataobj , regionType)
{
	// get data
	var datastr = _clientdata_get(datakey , regionType);
	
	if (typeof(datastr) == "undefined" || datastr == null) 
		return dataobj;
	
	// parse JSON object
	var tmpobj = JSON.parse(datastr);
	
	
	if (typeof(tmpobj) == "undefined" || tmpobj == null) 
		return dataobj;
	// for object, copy property to dataobj
	if (typeof(tmpobj) == "object" ) 
	{
		if( dataobj == null || typeof(dataobj)=="undefined" )
			dataobj = new Object() ;
		for (p in tmpobj) 
		{
			dataobj[p] = tmpobj[p];
		}
		return dataobj;
	}
	
	// otherwise, return value directly
	// (Array / Function / String / Number / Boolean)
	return tmpobj;
}

//保存数据内容
//regionType : 数据存放的位置 0-存放于永久数据区 1-存放于可替换数据区
function client_data_save_json(datakey, dataobj , regionType)
{
	// delete datakey if dataobj is null
	if (typeof(dataobj) == "undefined") 
	{
		_clientdata_del(datakey,regionType);
		return;
	}
	// save data
	var datastr = JSON.stringify(dataobj);
	_clientdata_set(datakey, datastr , regionType);
}

//删除一个已经保存的json对象
//regionType : 数据存放的位置 0-存放于永久数据区 1-存放于可替换数据区
function client_data_delete_json(datakey , regionType)
{
	_clientdata_del(datakey,regionType);
}

function client_data_exist_json(datakey , regionType)
{
	var datastr = _clientdata_get(datakey,regionType);
	return typeof(datastr) != "undefined";
}


//----------------------------------------------------------------------------------------------------------
// cache 相关                                              -------------
//----------------------------------------------------------------------------------------------------------
function QLClientCacheObj(){
};

QLClientCacheObj.prototype={
    KeyName : "" , //存放的键值名
    DataValue : null ,  //数据内容
    Attr : 0 ,          //特别属性，0-普通 1-xml
    SecSpan : 0 ,       //失效的秒时间间隔
    LastSaveTimeTick : 0  //最近一次的存放时间的tick值
}

//获取Cache中的缓存对象
//KeyName为键值内容
function client_cache_get(KeyName)
{
    var pItem = null ;
    pItem = client_data_load_json( KeyName , pItem , 1 ) ;
    if( pItem == null ) //未取得数据内容
        return null ;
    var curTime = (new Date()).getTime() ;
    if( (curTime-pItem.LastSaveTimeTick)/1000 > pItem.SecSpan ) //已超时
    {
        client_data_delete_json(KeyName,1) ;
        return null ;
    }
    if( pItem.Attr == 1 ) //DOM对象，需要向转
    {
        return xmlFromString( pItem.DataValue ) ;
    }
    else
    {
        return pItem.DataValue ;
    }
}


//设置客户端保存缓存对象
//KeyName为键值内容
//DataValue 为数据内容
//SecSpan 超时时间，单位秒
function client_cache_set(KeyName , DataValue , SecSpan)
{
    var pItem = new QLClientCacheObj() ;
    pItem.KeyName = KeyName ;
    if( typeof(DataValue.documentElement) == "undefined" ) //普通对象
    {
        pItem.Attr = 0 ;
        pItem.DataValue = DataValue ;
    }
    else //DOM对象需要转换成为XML对象再存储
    {
        pItem.Attr = 1 ;
        pItem.DataValue = DataValue.xml ;
    }
    pItem.SecSpan = SecSpan ;
    pItem.LastSaveTimeTick = (new Date()).getTime() ;
    client_data_save_json( KeyName , pItem , 1 ) ;
    return ;
}

//记录行为统计日志
function write_log(id,text)
{
	try
	{
		var obj = client_core_interface();
		obj.r("S_WriteOPLog", id + "\r\n" + text);
	}
	catch(e)
	{
		return;
	}
}

function client_show_messagebox(Msg,Title)
{
    var obj = client_core_interface();
    return obj.r("S_MessageBox", Msg + '\r\n' +Title + '\r\n0');
}

rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd() {
　　　　rnd.seed = (rnd.seed*9301+49297) % 233280;
　　　　return rnd.seed/(233280.0);
};
function rand(number) {
　　　　return Math.ceil(rnd()*number);
};

//+---------------------------------------------------
//| 获取系统登录账户信息
//+---------------------------------------------------
function getSysUser()
{
	var obj = client_core_interface(),
		ret = obj.r('G_SYS_UserName', '');
	try{
		return ret.split('\r\n');
	}finally{
		ret = null;
		obj = null;
	}
}


// 页面加载时清空注册回调
{
	try
	{
		var obj = client_core_interface();
		obj.r('G_Frame_CancleAllReq','')
	}
	catch(e)
	{
	}
}
