function fmtStr() {
	var fmt = arguments[0],
	tS = fmt.split("%s"),
	rtn = new Array();
	for (var i = 0; i < tS.length; i++) {
		rtn.push(tS[i]);
		if (i + 1 < arguments.length && i != tS.length - 1)
			rtn.push(arguments[i + 1]);
	}
	return rtn.join('');
}

(function($) {	
	/*
	功能:创建题目
	输入参数:t:对象实例,n:题目序号,rm:[编号,题目,类型,选项数量,选项序号,选项内容.....]
	返回值:无
	*/
	function setexam(t, n, rm) {
		var opt = $.data(t, "wj").options;
		var num = parseInt(rm[3])
		var id = "exam" + n + rm[0]
		var stty=(rm[2]+"")=="1"?"checkbox":"radio"
		opt.wjtyid.push(id)
		//id:题目节点id,tmxx:题目序号(自定义),bh：题目编号,stty:题目类型
		var s = '<div class="quest" id="%s" tmxh="%s" bh="%s" stty="%s">'
		var div_r =fmtStr(s, id, n, rm[0], rm[2])
		if (opt.examxh)
			div_r += (n + "." + rm[1])
		else
			div_r += rm[1]
		var xxh=""
		if(opt.test)
			xxh=opt.testquest[n+""]

		var sxh = '<div class="aslt">'
		//sth:选项序号,type:input类型
		for (var i = 0; i < num; i++) {
			sxh +='<div class="slt">'

			if(xxh&&xxh==rm[4+2*i])
				sxh += fmtStr('<input class="sltin" sth="%s" type="%s" name="%s" value="%s" checked/>',rm[4+2*i],stty,id,rm[5+2*i])
			else
				sxh += fmtStr('<input class="sltin" sth="%s" type="%s" name="%s" value="%s" />',rm[4+2*i],stty,id,rm[5+2*i])
			sxh += fmtStr('<span class="spntit">%s</span>',rm[5+2*i])
			sxh	+="</div>"
		}
		sxh+="</div>"
		div_r+=sxh
		div_r+="</div>"
		$(t).append($(div_r))
	};
	/*
	功能:解析题目参数
	输入参数:t:对象实例,tm:编号#题目#类型#选项数量#选项序号#选项内容#选项序号#选项内容#|编号#题目#类型#选项数量#选项序号#选项内容#选项序号#选项内容#
	返回值:无
	*/
	function parexam(t, tm) {
		var opt = $.data(t, "wj").options;
		opt.wjtyid=[]
		var j = 1;
		var sinex = tm.split("|"); //每个题目
		$.each(sinex, function(index, exam) {
			var rm = exam.split("#"); //exam:编号#题目#类型#选项数量#
			if (rm.length > 4) {
				setexam(t, j, rm);
				j++;
			}
		})
		$("span",t).bind('click',function(){
			$(this).siblings('input').each(function(){
				if(this.checked){
					if($(this).attr("type")=="checkbox")
						this.checked=false
				}
				else{
					this.checked=true
				}
			})
		});
	};
	function getexam(t){
		var opt = $.data(t, "wj").options;
		var stdn=["ANSWER,"+opt.wjtyid.length]
		var f=0
		var b=true

		$.each(opt.wjtyid,function(index,wid){
			var bh=$("#"+wid).attr("bh")
			var stty=$("#"+wid).attr("stty")
			var rd=getxxasn(t,wid,stty)

			if(rd==-1){
				msgbox({"txt":["请选择第"+$("#"+wid).attr("tmxh")+"题答案"]})

				b=false
				return false
			}
			if(opt.client){
				if(rd==opt.rightquest[bh+""]){//证券答案
					f+=opt.rightfs[stty+""]||10
					$("#"+wid).removeClass('errinfo')
				}else{
					if(opt.errinfo){//答错题目提示
						$("#"+wid).addClass('errinfo')
					}
				}
			}else
				stdn.push(bh+"#"+rd)
		})
		if(!b)
			return -1
		if(opt.client)
			return f
		return stdn.join("@")
	};
	function getxxasn(t,xh,ty){
		var opt = $.data(t, "wj").options;
		var chkObjs = document.getElementsByName(xh);
		var chk="";
		
		var bf=false;
	    for(var i=0;i<chkObjs.length;i++){
	        if(chkObjs[i].checked){
	        	bf=true
	        	if(ty=="1"){
	        		if(chk!="")
	        			chk+=opt.mullink
	        		chk+=$(chkObjs[i]).attr("sth")
	        	}else{
	        		chk=$(chkObjs[i]).attr("sth")
	        		break;
	        	}
	        }
	    }
	    return bf?chk:-1
	};
	$.fn.wj = function(pin, p) {
		if (typeof pin == "string") {
			var fn = $.fn.wj.methods[pin];
			if (fn) {
				var args = $.makeArray(arguments).slice(1);
				return fn.apply(this, args);
			}
			return
		}
		pin = pin || {};
		return this.each(function() {
			var tput = $.data(this, "wj");
			if (tput) {
				$.extend(tput.options, pin);
			} else {
				tput = $.data(this, "wj", {
					options: $.extend({}, $.fn.wj.defaults, pin)
				});
			}
		});
	};


	$.fn.wj.methods = {
		options: function() {
			return $.data(this[0], "inputbox").options;
		},
		parser: function(tm) {
			this.each(function(){
				parexam(this, tm)
			})
		},
		getans:function(){
			return getexam(this[0])
		},
		setret:function(){
			return this.each(function() {
				var opt = $.data(this, "wj").options;
				$.each(opt.wjtyid,function(index,wid){
					var chkObjs = document.getElementsByName(wid);
					for(var i=0;i<chkObjs.length;i++){
				        if(chkObjs[i].checked){
				        	chkObjs[i].checked=false
				        }
		    		}
		    		if(opt.errinfo){
		    			$("#"+wid).removeClass('errinfo')
		    		}
				})
			})
		}
	};
	$.fn.wj.defaults = $.extend({}, {
		rightquest:{},//正确答案
		test:false,//设置true,测试数据
		testquest:{},//默认选择的选项
		client:false,//客户端计算分数
		examxh:true,//题目前面是否加序号
		errinfo:false,//答题错误是否提示,默认不提示
		rightfs:{"0":10,"1":10},//每个类型分数,只有在client=true才用到
		mullink:"^"//多选时答案连接
	})
})(jQuery)

var user={

}
function fmttsRst(funid,id,data){
	if(funid=="JY:"+id){
		try{
			data=FormatResult(data,1)
		}catch(e){
			msgbox({"txt":["返回数据错误"]})
			return -1
		}
		if(data.ErrorCode!=0){
			msgbox({"txt":[data.ErrorInfo],"width":500})
			return -1
		}else{
			if(data.rows==undefined)
				data.rows=[]
			return data
		}
	}
	return -1
}

function msgbox(msg) {
	txt = msg["info"] || "提示"
	type = msg["type"] || "info"
	var tx = ""
	$.each(msg["txt"], function(i, v) {
		if (i != 0) {
			tx += "<br/>"
		}
		tx += v
	})
	var win = $.messager.alert(txt, tx, type)
	if (msg["width"])
		win.window({
			width: msg["width"]
		})
}

$(function(){
	
	get_zjzh()
})

function get_zjzh(){
	Win_CallTQL('ret_zjzh', 'getkhxx', new IXContent() ,'');
}
function ret_zjzh(_fromid,_funid,_flagtype,data) {
	if(_funid == 'getkhxx'){
		if(data!=""){
			var info=data.split("#");
			try{
				user.khh=info[2];
				user.zjzh=info[3]||info[2];
			}
			catch(e){
				msgbox(["获取资金账号失败！\r\n"+data])
				return
			}
		}
	}
	get_gddm()
}
function get_gddm(){

	set_ix({"fn":"ret_gddm","fnid":"1122",'F113':"0"})
}
function ret_gddm(_fromid,_funid,_flagtype,_json) {
    //debugger;
	var d=fmttsRst(_funid,"1122",_json)
	if(d==-1)
		return
	else{
		user.gddm=getgddm(d)
		
	}
	init()
}

function set_ix(x){
	var _ix = new IXContent();
	$.each(x,function(k,v){
		_ix.Set(k, v);
	})
	Win_CallTQL(x["fn"], 'JY:'+x['fnid'],_ix, '');
}

function transs(s){
	var i=s.indexOf(":")
	if(s.substring(0,i).toUpperCase()=="FAIL"){
		$("#fsts").text(s.substring(i+1))
		return 0
	}else{
		return 1
	}

}

function jumpfunc(funname){
	var _ix=new IXContent();
	_ix.Set("ID", funname||"StockHGT.KTQX");
	Win_CallTQL('ret_executefeature', 'executefeature', _ix ,'');
}
function ret_executefeature(){

}
function getgddm(d){
	for(var i=0;i<d.rows.length;i++){

		if(d.rows[i]["F125"]=="41"){
			user.zhlb="41"

			return d.rows[i]["F123"]
		}
		else if(d.rows[i]["F125"]=="0"){

			user.zhlb="0"
			
			return d.rows[i]["F123"]
		}
	}
	return ""
}