
var cpid=[]//所以htmlid
var glob={
	"khh":"",
	"zjzh":"",
	"gddm":""
}
/*var quest=[{"wtid":"1","stty":"0","sttit":"港股通交易时报价","stcon":[{"xh":"1","nr":"人民币报价"},{"xh":"2","nr":"港币报价"},{"xh":"3","nr":"港币交收"}]},
	      {"wtid":"2","stty":"1","sttit":"港股通交易时报价2","stcon":[{"xh":"1","nr":"人民币报价2"},{"xh":"2","nr":"港币报价2"},{"xh":"3","nr":"港币交收2"}]}]*/
$(function(){
	/*$.each(quest,function(i,v){
		createQuest(i+1,v)
	})*/
	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{
				glob.khh=info[2];
				glob.zjzh=info[3];
			}
			catch(e){
				msgbox(["获取资金账号失败！\r\n"+data])
				return
			}
		}
	}
	get_Gddm()
}
function get_Gddm(){

	var _ix = new IXContent();
	_ix.Set('F113', '0');
	Win_CallTQL('ret_Gddm', 'JY:1122', _ix, '');
}
function ret_Gddm(_fromid,_funid,_flagtype,_json) {

	if (_funid == 'JY:1122'){
		var data = FormatResult(_json, 1);
		if (data.ErrorCode == 0){
			glob.gddm=data.rows[0]["F123"]
		}else{
			msgbox([data.ErrorInfo])
			
			return
		}
	}
	get_wjans()
}

function get_wjans(){
	set_ix({"fn":"req_wjans","fnid":"1452",'F120':glob.khh,"F121":glob.zjzh,'F110':'7',
			'F234':"1",'F1217':'9'})
}
function req_wjans(_fromid,_funid,_flagtype,_json){
	// 编号#题目#类型#选项数量#选项序号#选项内容#选项序号#选项内容#|编号#题目#类型#选项数量#选项序号#选项内容#选项序号#选项内容#
	if (_funid == 'JY:1452'){
		var data = FormatResult(_json, 1);

		if (data.ErrorCode == 0){
			if(data.rows==undefined||data.rows.length==0){
				return
			}
			var req=data.rows[0]["F149"].split("|")
			var j=1;
			$.each(req,function(i,tm){
				if(tm.length){
					var rm=tm.split("#")
					if(rm.length>4){
						var quest=[]
						for(var t=0;t<parseInt(rm[3]);t++){
							quest.push({"xh":rm[4+2*t],"nr":rm[5+2*t]})
						}
						createQuest(j,{"wtid":rm[0],"stty":rm[2],"sttit":rm[1],"stcon":quest})
						j++
					}
				}
			})
		}else{
			msgbox([data.ErrorInfo])
			
			return
		}
	}
}
function createQuest(i,quest){
	/*quest:{"wtid":问题编号,"stty":试题类型,"sttit":"试题题目","stcon":[{"xh":序号,"nr":试题}]}*/
	var id="st"+i+quest["wtid"]
	cpid.push(id)
	$("<div id="+id+" class='quest' tmnum="+i+" xh="+quest["wtid"]+" stty="+quest["stty"]+">"+i+"."+quest["sttit"]+"</div>").appendTo($("#zscp"))
	var ctent=$("<div class='aslt'></div>")
	ctent.appendTo($("#zscp"))
	var iptype=quest["stty"].toString()=="0"?"radio":"checkbox"
	$.each(quest["stcon"],function(k,val){
		$("<div class='slt'><input class='sltin' sth="+val["xh"]+" type="+iptype+" name="+id+" value="+val["nr"]+" />"+"<span class='spntit' onclick='titcli(this)'>"+val["nr"]+"</span></div>").appendTo(ctent)
	})
}
function titcli(ts){
	$(ts).siblings('.sltin').each(function(){
		if(this.checked){
			if($(this).attr("type")=="checkbox")
				this.checked=false
		}
		else
			this.checked=true
	})
	
	
	
}
function getQuest(){
	
	var b=true;
	var stdn=["ANSWER,"+cpid.length]
	$.each(cpid,function(i,vid){
		var wtid=$("#"+vid).attr("xh")
		var stty=$("#"+vid).attr("stty")
		var rd=getRadio(vid,stty)
		if(rd==-1){
			msgbox(["请选择第"+$("#"+vid).attr("tmnum")+"题答案"])
			
			b=false
			return false
		}
		stdn.push(wtid+"#"+rd)
	})
	if(b)
		subWJ(stdn.join("@"))
}

function ok_ret(){
	$.each(cpid,function(i,vid){
		var wtid=$("#"+vid).attr("xh")
		var stty=$("#"+vid).attr("stty")
		var chkObjs = document.getElementsByName(vid);
		for(var i=0;i<chkObjs.length;i++){
	        if(chkObjs[i].checked){
	        	chkObjs[i].checked=false
	        }
    	}
	})
}
function ok_sub(){
	getQuest()
}
function subWJ(strqu){
	var _ix = new IXContent();
	_ix.Set('F120', glob.khh);
	_ix.Set('F134', "##PWD##");
	_ix.Set('F123', glob.gddm);
	_ix.Set('F121', glob.zjzh);
	_ix.Set('F1227', strqu);

	Win_CallTQL('req_wj', 'JY:1450', _ix, '');
}
function req_wj(_fromid,_funid,_flagtype,_json){
	if (_funid == 'JY:1450'){
		var data = FormatResult(_json, 1);
		if (data.ErrorCode == 0){
			msgbox([data.rows[0]["F149"]],"提示","info",function(){
				ok_ret()
				$(document).scrollTop(0) 
			})
			
		}else{
			msgbox([data.ErrorInfo])
			
			//alert(data.ErrorInfo);
			return
		}
	}
}

function getRadio(name,stype){
	var chkObjs = document.getElementsByName(name);
	var chk="";
	var txt=""
	var bf=false;
    for(var i=0;i<chkObjs.length;i++){
        if(chkObjs[i].checked){
        	bf=true
        	if(stype=="0"){
        		chk=$(chkObjs[i]).attr("sth")
        		txt=$(chkObjs[i]).val()
        		break;
        	}else{
        		if(chk!="")
        			chk+="^"
        		if(txt!="")
        			txt+="^"
        		chk+=$(chkObjs[i]).attr("sth")
        		txt+=$(chkObjs[i]).val()
        	}
        }
    }
    return bf?chk+"#"+txt:-1
}

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

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, '');
}
