var gf_dzjy_share ={};
var nullGridStr ='没有相应的查询信息!';
gf_dzjy_share.pageSize =50;

/*
*设置IX协议
*@param cfg为{},其中funcid和funcname2个key必须填入
*/
gf_dzjy_share.setIX=function(cfg){
	var _ix = new IXContent();
	_ix.Set('op_branch_no', User.yyb);//操作分支机构
	_ix.Set('op_entrust_way', User.wtfs);//委托方式
	_ix.Set('@MAC',User.minfo);//站点地址
	_ix.Set('branch_no',User.yyb);//分支机构
	_ix.Set('client_id',User.khh);//客户号
	_ix.Set('fund_account',User.zjzh);//资金账户
	_ix.Set('password','##PWD##');//密码
	_ix.Set('user_token',' ');//用户口令
	_ix.Set('@COUNT',gf_dzjy_share.pageSize);//返回条数
	$.each(cfg,function(key,val){
		if(key=="funcid"||key=="funcname"){}
		else{
			_ix.Set(key, val);
		}
	})
	Win_CallTQL(cfg.funcname, '5010:SIMPLE.'+cfg.funcid, _ix, '');
}

gf_dzjy_share.createGrid =function(id,panel_id,width,columnsList,border){
	var defaultWidth = width || $('#'+panel_id).width(); 
	var borderDisplay = (border==false)?false:true;
	$("#"+id).datagrid({
		singleSelect:false,
		fit:true,
		// fitColumns:true,
		scrollbarSize:0,
		border:borderDisplay,
		rownumbers:true,
		remoteSort: false,
		loadMsg:'正在请求数据，请稍后...',
		columns:columnsList
    });
}

/*
*获取input选中文本
*@param：inputDom：input节点
*@return:选中文本
*/
function getSelectedText(inputDom){ 
    if (document.selection) 
     {
        return document.selection.createRange().text;
    } 
    else { 
        return inputDom.value.substring(inputDom.selectionStart, 
                inputDom.selectionEnd); 
    } 
}	

/*
	设置web页面为文字提示
*/
gf_dzjy_share.SetPage = function(pageStr){
	$('body').html("<p>"+pageStr+"</p>");
}

/*
	金额大写转换函数
*/
gf_dzjy_share.getChineseCurrencyString = function(n) {
	if (!/^(0|[1-9]\d*)(\.\d+)?$/.test(n))
		return "数据非法";
	if(n==0) return '零';
	var unit = "千百拾亿千百拾万千百拾元角分",
		str = "";
	n += "00";
	var p = n.indexOf('.');
	if (p >= 0)
		n = n.substring(0, p) + n.substr(p + 1, 2);
	unit = unit.substr(unit.length - n.length);
	for (var i = 0; i < n.length; i++)
		str += '零壹贰叁肆伍陆柒捌玖'.charAt(n.charAt(i)) + unit.charAt(i);
	return str.replace(/零(千|百|拾|角)/g, "零").replace(/(零)+/g, "零").replace(/零(万|亿|元)/g, "$1").replace(/(亿)万|壹(拾)/g, "$1$2").replace(/^元零?|零分/g, "").replace(/元$/g, "");
}

gf_dzjy_share.addTab = function(id,tabList,cur,tabWidth){
	var firstSel=false;
	var width = 80||tabWidth;
	for(var i=0;i<tabList.length;i++){
		firstSel = (i==cur)?true:false;
		$('#'+id).tabs("add",{
			title: tabList[i],
			selected: firstSel,
			width:width
		})
	};
}

/*获取客户号*/
function get_clientzh(){
	Win_CallTQL('ret_clientzh', 'getclientzh', new IXContent() ,'');
}

function ret_clientzh(_fromid,_funid,_flagtype,data) {
	if(_funid == 'getclientzh'){
		if(data!=""){
			try{
				User.khh=data;
				get_uidxml();
			}
			catch(e){
				alert("获取客户号失败！\r\n"+data);
			}
		}
	}
}

/*获取登录信息的sysnode_id*/
function get_uidxml(){
	var _ix = new IXContent();
	_ix.Set('keyid ', 'F1299');
	Win_CallTQL('ret_uidxml', 'getuidxml', _ix  ,'');
}

function ret_uidxml(_fromid,_funid,_flagtype,data) {
	if(_funid == 'getuidxml'){
		if(data!=""){
			try{
				User.sysnode_id=data;
			}
			catch(e){
				alert("获取登录信息的sysnode_id！\r\n"+data);
			}
		}
	}
}

/*
	翻译股东类型
*/
var transGddm = {
	"0":"深A",
	"1":"沪A",
	"2":"深B",
	"3":"沪B",
	"12":"三板A",
	"13":"三板B"
}

/*
	委托状态字段
*/
var wtztDict = {
	"0":"未报",
	"1":"待报",
	"2":"已报",
	"3":"已报待撤",
	"4":"部成待撤",
	"5":"部撤",
	"6":"已撤",
	"7":"部成",
	"8":"已成",
	"9":"废单",
	"C":"正报",
	"D":"撤废",
	"G":"单腿成交",
	"U":"已确认待撤",
	"V":"已确认",
	"W":"待确认",
	"X":"预成交",
	"Y":"购回待确认",
	"Z":"已回购"
}

/*
	委托类别字段
*/
var wtlbDict = {
	"0":"委托",
	"1":"查询",
	"2":"撤单",
	"3":"补单",
	"4":"确认",
	"5":"大宗",
	"6":"信用融资",
	"7":"信用融券",
	"8":"信用平仓",
	"9":"信用交易",
	"A":"撤单补单"
}

/*
	买卖方向字段
*/
var mmfxDict = {
	"1":"买入",
	"2":"卖出"
}

/*
	交易类别字段
*/
var jylbDict = {
	"0":"资金",
	"1":"上海",
	"2":"深圳",
	"9":"特转A",
	"A":"特转B",
	"D":"沪B",
	"H":"深B",
	"Q":"青岛产权",
	"U":"转融通",
	"J":"金华基金",
	"K":"香港市场",
	"X":"固定收益",
	"B0":"内债市场",
	"Z1":"已回购"
}

/*
	委托方式字段
*/
var wtfsDict = {
	"0":"自助委托",
	"1":"电话委托",
	"2":"驻留委托",
	"3":"远程委托",
	"4":"柜台委托",
	"5":"漫游委托",
	"6":"分支委托",
	"7":"网上委托",
	"8":"手机委托",
	"9":"网页委托",
	"b":"代理委托",
	"d":"短信委托",
	"g":"GPRS委托",
	"m":"银行ATM",
	"p":"银行POS",
	"r":"BREW委托",
	"s":"ETF算法交易",
	"t":"融资外围",
	"u":"银行柜台",
	"v":"第三方柜台",
	"z":"集中柜面",
	"A":"自定义委托",
	"B":"自定义委托",
	"C":"自定义委托",
	"D":"自定义委托",
	"E":"自定义委托",
	"F":"自定义委托",
	"G":"自定义委托",
	"H":"自定义委托",
	"I":"自定义委托",
	"J":"自定义委托",
	"K":"自定义委托",
	"L":"UFT委托",
	"O":"离柜委托",
	"P":"热自助2",
	"Q":"电话内线",
	"U":"要约收购",
	"Y":"证券托银",
	"Z":"VIP委托",
	"~":"大宗交易",
	"+":"网下现金",
	"-":"网下证券"
}

/*
	委托属性字段
*/
var wtsxDict = {
	"0":"买卖",
	"1":"配股",
	"2":"转托",
	"3":"申购",
	"4":"回购",
	"5":"配售",
	"6":"指定",
	"7":"转股",
	"8":"回售",
	"9":"股息",
	"A":"深圳配售确认",
	"B":"配售放弃",
	"G":"配售冲正",
	"H":"特殊业务",
	"J":"预受要约解除",
	"K":"基金设红",
	"L":"基金申赎",
	"M":"跨市转托",
	"N":"ETF申赎",
	"P":"权证行权",
	"Q":"对手方最优价格",
	"R":"最优五档即时成交剩余转限价",
	"S":"本方最优价格",
	"T":"即时成交剩余撤销",
	"U":"最优五档即时成交剩余撤销",
	"V":"全额成交或撤单",
	"W":"基金转换",
	"X":"牛熊宝交易",
	"Y":"要约收购预受",
	"f":"质押出入库",
	"a":"意向委托",
	"b":"定价委托",
	"c":"确认委托",
	"g":"报价出入库",
	"h":"报价回购",
	"OFC":"货币基金申购",
	"OFR":"货币基金赎回",
	"R1":"担保证券提交与返还",
	"R2":"融券券源划拨",
	"R3":"现券还券划拨",
	"QNE":"报价回购",
	"QBD":"报价入库",
	"QBW":"报价出库",
	"QCA":"报价回购提前购回",
	"QNP":"报价回购展期买入",
	"RNA":"约定购回补充交易",
	"RNB":"约定购回回补交易",
	"RNE":"约定购回初始交易",
	"RNL":"约定购回零碎股购回",
	"RNP":"约定购回配股权证购回",
	"RNR":"约定购回购回交易",
	"REA":"转融券出借非约定申报",
	"REB":"转融通借入交易",
	"REC":"转融券出借约定申报",
	"REF":"转融通保证金业务",
	"AFC":"盘后大宗收盘价委托",
	"AFW":"盘后大宗成交量加权平均价委托"
}

/*
	判断为空
*/
function checkNull(obj){
	return (obj==undefined||obj==null||obj=='')?true:false;
}

/*
	获取数据字典数据
*/
function getDataDict(dict,key){
	if(checkNull(dict)&&checkNull(key)) return '';
	return (checkNull(dict[key])?'':dict[key]);
}

/*
	翻译委托方式
*/
function formatWtfs(val){
	return getDataDict(wtfsDict,val);
}

/*
	翻译买卖方向
*/
function formatMmfx(val){
	return getDataDict(mmfxDict,val);
}

/*
	翻译交易类别
*/
function formatJylb(val){
	return getDataDict(jylbDict,val);
}

/*
	翻译委托类别
*/
function formatWtlb(val){
	return getDataDict(wtlbDict,val);
}

/*
	翻译委托状态
*/
function formatWtzt(val){
	return getDataDict(wtztDict,val);
}

/*
	翻译委托属性
*/
function formatWtsx(val){
	return getDataDict(wtsxDict,val);
}

/*
	格式化时间
*/
function formatTimeStr(time){
	var timeStr= '';
	if(time==null||time==undefined||time=='') return timeStr;
	if(time.length<6) time='0'+time;
	for(var i=0;i<time.length;i++){
		if(i%2==0){
			timeStr +=(i==0)?'':':';
			timeStr+=time.substr(i,2);
		}
	}
	return timeStr;
}

/*
	数字排序
*/
gf_dzjy_share.parseFloat =function(num){
	return parseFloat((num==null||num==undefined||num=='')?0:num);
}

function sortNumber(a,b){
	a =gf_dzjy_share.parseFloat(a);
	b =gf_dzjy_share.parseFloat(b);
	return (a>=b)?1:-1;
}


/*
*功能:更新easy表格数据,表格无数据时加提示信息
*备注：表格第一列为check时不要调用这个函数
*参数:rt表格id,
	  data表格更新数据
	  opts：{}对象 {"info"}
	  		info:表格无数据的提示文本,默认"没有相应的查询信息"
*返回:无
*/
function gridUpdateData(gid, sCol, data, opts) {
	var opt = $("#" + gid).datagrid("options");
	opts = opts || {};
	var field = $("#" + gid).datagrid("getColumnFields");
	for (var i = 0; i < sCol; i++) {
		field.shift();
	}
	if (data.rows == undefined || data.rows.length == 0) {
		var r = {};
		$.each(field, function(k, v) {
			if (k) r[v] = "";
			else r[v] = opts["info"] || nullGridStr;
		})
		data.rows = [r];
		var t = {};
		t["align"] = opt.columns[0][sCol].align;
		t["styler"] = opt.columns[0][sCol].styler;
		opt.columns[0][sCol].align = "left";
		opt.columns[0][sCol].styler = function() {
			return "color:red;"
		};
		for (var k = 0; k < field.length; k++) {
			opt.columns[0][k + sCol].sortable = false;
		}
		$("#" + gid).datagrid("loadData", data.rows);
		$("#" + gid).datagrid("mergeCells", {
			"index": 0,
			"field": field[0],
			"colspan": field.length
		});
		opt.columns[0][sCol].align = t["align"];
		opt.columns[0][sCol].styler = t["styler"];

	} else {
		for (var k = 0; k < field.length; k++) {
			opt.columns[0][k + sCol].sortable = true;
		}
		$("#" + gid).datagrid("loadData", data);
	}
}

/*
	设置表格列字段对齐方式
*/
function gridColAlign() {

	var panel = $(this).datagrid('getPanel');
	var Fields = $(this).datagrid("getColumnFields");
	var opt = $(this).datagrid("options");
	var tr = panel.find('div.datagrid-body tr');
	tr.each(function() {
		for (val in opt.alignColList) {
			var td = $(this).children('td[field="' + opt.alignColList[val] + '"]');
			td.children("div").css({
				"text-align": opt.text_align
			});
		}
	});
}

$(function(){
	get_clientzh();
})

