var glob={
	'text':'',
	'utype':''
}

window.onresize=function(){
	$("#newwin").window("resize")
	$("#newwin2").window("resize")
	$("#newwin3").window("resize")
}
var dssi;
function init(){
	
	
	$("#newwin3").window("close")
	$("#newwin2").window("close")
	$("#newwin").window("close")

	$("#bom").datagrid({
		singleSelect:true,
		fitColumns:true,
		
		width:410,
		scrollbarSize:0,
		border:false,
		remoteSort: false,

		clirow:null,

		columns:[[
			{field:'market',title:'市场',halign:'center',align:'center',width:60},
			{field:'gddm',title:'股东账号',halign:'center',align:'center',width:90},
			{field:'ktflag',title:'开通标志',halign:'center',align:'center',width:80},
			{field:'operate',title:'操作',halign:'center',align:'center',width:40},
			{field:'xyck',title:'协议查看',halign:'center',align:'center',width:130}

		]]  	   	
	});	
	//req_ktqx()
	$("#next").bind("click",function(){
		$("#newwin2").window("close")
		onClowin2()
		ktktqx()
		

	})
	$("#lxfx").bind("click",function(){

		if(this.checked)
			$("#next").attr('disabled',false)
		else
			$("#next").attr('disabled',true)
		
	})

	$("#cance").bind("click",function(){
		$("#newwin2").window("close")
		onClowin2()
		clearInterval(dssi)
	})
	/*glob.utype="2"
	glob.text="http://www.gtja.com/webtrade/fxjss.jsp?comeFrom=fy&code=000014"
	xyshow()
	return*/
	get_ktqx()
}
function onkt(f){
	//debugger;
	if(f==0){
		qxktqx()
		return
	}
	if(f==1){
		var _ix=new IXContent();
		_ix.Set("ID", "service.service2");//操作方式
		Win_CallTQL('ret_executefeature', 'executefeature', _ix ,'');
	}
	// createwindow3()
//	createwindow1("2")
//	$("#ifr").attr("src","https://yw.gtja.com/kh/ywbl/loginPage.jsp?targetUrl=YWBLSGTYW")	
}
function ret_executefeature() {}

function onhref(){
	//debugger;
	createwindow1("2")
	//if(glob.utype=="2"){
		$("#ifr").attr("src",glob.text)
	// }else{
	
	// 	$("#txts").text(glob.text)
		
	//}
}

function get_ktqx(){
	set_ix({"fn":"req_ktqx","fnid":"1452",'F1217':'7','F123':user.gddm,'F125':'41'})
}
function req_ktqx(_fromid,_funid,_flagtype,_json){
	//debugger;
	var ktqx="未开通",
		xy="",
		flag=""
	var d=fmttsRst(_funid,"1452",_json)
	//var d={"rows":[{"F1223":"1","F149":"#港股通委托协议#@2#港股通风险揭示书#www.gtja.com/webtrade/fxjss.jsp?comeFrom=fy&code=000014@"}]}
	if(d==-1){
		$("#bom").datagrid("loadData",{"total":0,"rows":[]})
		return
	}
		
	//var url="#港股通委托协议#@1#港股通风险揭示书#http://www.gtja.com/webtrade/fxjss.jsp?comeFrom=fy&code=000014@"
	
	
	
		
	//$("#bom").datagrid("loadData",{"total":1,"rows":[{"market":"沪港通","gddm":glob.gddm,"ktflag":ktqx,"operate":flag,"xyck":xy}]});
	var url=d.rows[0]["F149"]
	// var lur=url.split("#")
	// var su=lur[4].split("@")[0]
	//0未开通 2开通
	if((d.rows[0]["F1223"]+"")=="0"){//url地址    
	//	glob.utype="2"
		glob.text=url;
		xy='<a onclick="onhref()" style="text-decoration:underline" href="javascript:void(0)">风险提示书及委托协议<a>'
		if((d.rows[0]["F1223"]+"")=="2")
			flag='<a onclick="onkt(0)" style="text-decoration:underline" href="javascript:void(0)">取消<a>';
		else
			flag='<a onclick="onkt(1)" style="text-decoration:underline" href="javascript:void(0)">开通<a>';
	}
	if((d.rows[0]["F1223"]+"")=="2"){//url地址    
	//	glob.utype="2"
		glob.text=url;
		xy='<a onclick="onhref()" style="text-decoration:underline" href="javascript:void(0)">风险提示书及委托协议<a>'
		if((d.rows[0]["F1223"]+"")=="2")
			flag='<a onclick="onkt(0)" style="text-decoration:underline" href="javascript:void(0)">取消<a>';
		else
			flag='<a onclick="onkt(1)" style="text-decoration:underline" href="javascript:void(0)">开通<a>';
	}

	// else if(lur[2]=="@1"){
	// 	glob.text=text
	// 	glob.utype="1"
	// 	if((d.rows[0]["F1223"]+"")=="2")
	// 		flag='<a onclick="onkt(0)" style="text-decoration:underline" href="javascript:void(0)">取消<a>';
	// 	else
	// 		flag='<a onclick="onkt(1)" style="text-decoration:underline" href="javascript:void(0)">开通<a>';
	// 	xy='<a onclick="onhref()" style="text-decoration:underline" href="javascript:void(0)">风险提示书及委托协议<a>'
	// }
	if((d.rows[0]["F1223"]+"")=="2"){
		ktqx="已开通"

	}

	$("#bom").datagrid("loadData",{"total":1,"rows":[{"market":"深港通","gddm":user.gddm,"ktflag":ktqx,"operate":flag,"xyck":xy}]})

}

function ktktqx(){
	set_ix({"fn":"req_ktktqx","fnid":"1450","F123":user.gddm,"F125":user.zhlb||"",'F1227':'PROTOCOL,0'})
}
function req_ktktqx(_fromid,_funid,_flagtype,_json){
	var d=fmttsRst(_funid,"1450",_json)
	if(d==-1)
		return
	msgbox({"txt":[d.rows[0]["F149"]],"width":500})
	get_ktqx()
}
//取消
function qxktqx(){
	var win=$.messager.confirm("委托确认",'<div style="color:#ff0000;font-size: 16px">取消权限确认</div><div style="font-size: 12px"">您是否确认注销港股通权限？如确认注销，您将无法再进行港股通交易，<br/>须重新申请开通港股通权限后方可交易</div>',function(r){
		if (r){
			var win2=$.messager.confirm("委托确认",'<div style="color:#ff0000;font-size: 16px">取消权限再次确认</div><div style="font-size: 12px"">请再次确认是否注销港股通权限?</div>',function(r2){
				if (r2){
					set_ix({"fn":"req_qxktqx","fnid":"1450","F123":user.gddm,"F125":'41'||"",'F1227':'PROTOCOL,1'})
				}
			})
			win2.window({width: 400})
		}
	})
	win.window({
			width: 500
		})
	
}
function req_qxktqx(_fromid,_funid,_flagtype,_json){

	var d=fmttsRst(_funid,"1450",_json)
	if(d==-1)
		return
	msgbox({"txt":[d.rows[0]["F149"]],"width":500})
	get_ktqx()
}


function onClowin(){
	closewindow1()
}
function onClowin2(){
	closewindow2()
}
function onClowin3(){
	closewindow3()
}
function closewindow1(){
	if($("#ifr").length)
		$("#ifr").remove()
	if($("#txts").length)
		$("#txts").remove()
}

function closewindow2(){

	if($("#ifr2").length)
		$("#ifr2").remove()
	if($("#txts2").length)
		$("#txts2").remove()
}
function closewindow3(){

	if($("#zscp").length)
		$("#ifr2").empty()
	
}
function createwindow1(flag){
	if($("#ifr").length)
		$("#ifr").remove()
	if($("#txts").length)
		$("#txts").remove()
	$("#newwin").window("open")
	$("#newwin").window("maximize")
	if(flag=="2")
		$('<iframe  id="ifr" width=100% height=100% frameborder=0 border=0 bordercolor=red></iframe>').appendTo($("#cce").layout("panel","center"))
	else{

		$('<textarea style="resize: none;overflow:auto; background-attachment:fixed;background-repeat:no-repeat;border-style:solid;border-color:#FFFFFF;width:100%;height:100%" id="txts"></textarea>').appendTo($("#cce").layout("panel","center"))
		
	}
	
}

function createwindow2(flag){
	if($("#ifr2").length)
		$("#ifr2").remove()
	if($("#txts2").length)
		$("#txts2").remove()
	$("#newwin2").window("open")
	$("#newwin2").window("maximize")

	if(flag=="2"){
		$('<iframe  id="ifr2" width=100% height=100% frameborder=0 border=0 bordercolor=red></iframe>').appendTo($("#cce2").layout("panel","center"))	
	}
		
	else{

		$('<textarea style="resize: none;overflow:auto; background-attachment:fixed;background-repeat:no-repeat;border-style:solid;border-color:#FFFFFF;width:100%;height:100%" id="txts2"></textarea>').appendTo($("#cce2").layout("panel","center"))
		
	}
	
}
function createwindow3(){
	$("#zscp").empty()
	$("#newwin3").window("open")
	$("#newwin3").window("maximize")
	get_wjans()
	
}



function get_wjans(){
	set_ix({"fn":"req_wjans","fnid":"1452",'F120':user.khh,"F121":user.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
			}
			$("#zscp").wj({rightquest:{"1":"4","2":"1","3":"4","4":"4","5":"1","6":"5","7":"2","8":"3","9":"5","10":"5"},client:true,test:false,testquest:{"1":"4","2":"1","3":"4","4":"4","5":"1","6":"5","7":"2","8":"3","9":"5","10":"5"}})
			$("#zscp").wj("parser",data.rows[0]["F149"])
		}else{
			msgbox({"txt":[data.ErrorInfo]})
			return
		}
	}
}

function ok_sub(){
	var f=$("#zscp").wj("getans")
	if(f==-1)
		return
	if(f<80){
		$.messager.confirm("提示","您的测试得分:"+f+"分,只有测评分数达到80分才允许开通港股通权限。<br/>是否现在进入国泰君安港股通网站学习",function(t){
			if(t){
				ok_ret()
				window.open ('http://www.gtja.com/portal/channel/hugangtong')
			}
				
		})

		
	}else{
		var win = $.messager.alert("提示", "您的测试得分:"+f+"分,已通过港股通知识测试,您可以退出测评页继续权限开通操作。", "info",function(){
			ok_ret()
			$("#newwin3").window("close")
			closewindow3()
			xyshow()

		})
		win.window({width: 500})
	}
}


function xyshow(){
	createwindow2(glob.utype)
	$("#next").attr("disabled",true)
	$("#lxfx").attr("disabled",true)
	if(glob.utype=="2"){//url地址
		$("#ifr2").attr("src",glob.text)
	}else{
		$("#txts2").text(glob.text)
	}
	var i=10
	function sety(){
		if(i==0){
			$("#lxfx").attr("disabled",false)
			$("#next").val("同意")
			//$("#next").attr('disabled',false)
			clearInterval(dssi)
			return
		}
		$("#next").val("同意("+i+")")
		i-=1;
	}
	sety()
	dssi=setInterval(sety, 1000)

}
function ok_ret(){
	$("#zscp").wj("setret")
}