/* vim:set ts=2 sw=2 sts=2 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 function copyToClipBoard(s) {
            //alert(s);
            if (window.clipboardData) {
                window.clipboardData.setData("Text", s);
                
            } else if (navigator.userAgent.indexOf("Opera") != -1) {
                window.location = s;
            } else if (window.netscape) {
              
                var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
                if (!clip)
                    return;
                var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
                if (!trans)
                    return;
                trans.addDataFlavor('text/unicode');
                var str = new Object();
                var len = new Object();
                var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                var copytext = s;
                str.data = copytext;
                trans.setTransferData("text/unicode", str, copytext.length * 2);
                var clipid = Components.interfaces.nsIClipboard;
                if (!clip)
                    return false;
                clip.setData(trans, null, clipid.kGlobalClipboard);
            
            }
        }
function json2str(obj) {
    var THIS = this;
    switch (typeof (obj)) {
        case 'string':
            return "\"" + obj.replace(/(["\\])/g, '\\$1') + "\"";
        case 'array':
            return '[' + obj.map(THIS.json2str).join(',') + ']';
        case 'object':
            if (obj instanceof Array) {
                var strArr = [];
                var len = obj.length;
                for (var i = 0; i < len; i++) {
                    strArr.push(THIS.json2str(obj[i]));
                }
                return '[' + strArr.join(',') + ']';
            } else if (obj == null) {
                return 'null';

            } else {
                var string = [];
                for (var property in obj) string.push(THIS.json2str(property) + ':' + THIS.json2str(obj[property]));
                return '{' + string.join(',') + '}';
            }
        case 'number':
            return obj;
        case 'boolean':
            return obj;
        case false:
            return obj;
    }
}
var autologin={
	
	serverurl:["http://dts.37.com","http://dtszj.yy.com/","http://dtszj.wan.360.cn","http://dts.6711.com/","http://youxi.baidu.com/dts/index/","http://wan.sogou.com/login.do?gid=269",
	"http://dts.yilewan.com/","http://wan.liebao.cn/dts/server_list.html","http://dts.swjoy.com/","http://dts.niu.xunlei.com/index.shtml","http://game.weibo.com/","http://games.ifeng.com/webgame/dtszj/"],
	open:function(agent){
		//brow.loadURI(autologin.serverurl[agent], null, null);
		if(agent<0||agent>=autologin.serverurl.length)return "chrome://simple/content/index.html";
		var url=autologin.serverurl[agent];
		return url;
	},
	login:function(doc,agent,user,pass,serverid){
		var agentlist=[autologin.f_37wan,autologin.f_yy,autologin.f_360,autologin.f_6711,autologin.f_baidu,autologin.f_sougou,autologin.f_shengda,autologin.f_jinshan,
		autologin.f_shunwang,autologin.f_xunlei,autologin.f_xinlang,autologin.f_fenghuang];
		if(agent<0||agent>=agentlist.length)return;
		return agentlist[agent](doc ,user,pass,serverid);
	},
	
	f_fenghuang:function(doc,user,pass,serverid){	
		if(doc.getElementById("nologin")==null)return;
		if(doc.getElementById("nologin").ownerDocument.defaultView.getComputedStyle(doc.getElementById("nologin"), null)["display"]=="none"){
			//已经登陆成功
			return "http://play.ifeng.com/?_c=dtszj&_a=enterGame&area="+serverid;
			
		}else{
			
			if(doc.getElementById("_M_username")==null||doc.getElementById("_M_password")==null||doc.getElementById("M_inpass")==null||doc.getElementById("M_btnConfirm")==null)return;
			doc.getElementById("_M_username").value=user;
			doc.getElementById("_M_password").value=pass;
			doc.getElementById("M_inpass").checked=true;			
			doc.getElementById("M_btnConfirm").click();
			var timer=setInterval(function (){
					if(doc.getElementById("nologin")==null)return;
					if(doc.getElementById("nologin").ownerDocument.defaultView.getComputedStyle(doc.getElementById("nologin"), null)["display"]=="none"){
						//已经登陆成功
						clearInterval(timer);
						document.getElementById("textbox").value="http://play.ifeng.com/?_c=dtszj&_a=enterGame&area="+serverid;
						simple.brow.loadURI(document.getElementById("textbox").value,null, null);
						return ;
					}
			},500);
		}

			
	

	},
	f_xinlang:function(doc,user,pass,serverid){	
		

			
		if(doc.getElementById("newWYX_loginUname")!=null&&doc.getElementById("newautoLogin")!=null&&doc.getElementById("newWYX_loginUpwd")!=null&&doc.getElementById("newWYX_loginBtn")!=null)
		{
			doc.getElementById("newWYX_loginUname").focus();
			doc.getElementById("newWYX_loginUname").value=user;
			doc.defaultView.wrappedJSObject.wyxIndexLogin.medata.username=user;
			doc.defaultView.wrappedJSObject.wyxIndexLogin.prelogin();
			doc.getElementById("newWYX_loginUpwd").focus();
			doc.getElementById("newWYX_loginUpwd").value=pass;
			doc.getElementById("newautoLogin").checked=true;	
			setTimeout(function(){
					doc.getElementById("newWYX_loginBtn").click();},800);			
		
		}
		else
		{
	
			if(doc.getElementsByClassName("topnav_con").length>0&&doc.getElementsByClassName("topnav_con")[0].getElementsByClassName("pr").length>0&&
			doc.getElementsByClassName("topnav_con")[0].getElementsByClassName("pr")[0].getElementsByClassName("user").length>0)
			return "http://game.weibo.com/datianshizhijian?action=login&service="+(180012401000+parseInt(serverid))+"&ownpt=weibo";
		}
		

	},
	f_xunlei:function(doc,user,pass,serverid){	
		if(doc.getElementById("loginedBox")==null)return;
		if(doc.getElementById("loginedBox").ownerDocument.defaultView.getComputedStyle(doc.getElementById("loginedBox"), null)["display"]=="block"){
			//已经登陆成功
			return "http://niu.xunlei.com/entergame/dts/?fenQuNum="+serverid;
			
		}else{
		
			if(doc.getElementById("accountBox")==null||doc.getElementById("passwordBox")==null||doc.getElementById("autoLoginLink")==null||doc.getElementById("loginBtn")==null)return;
			doc.getElementById("accountBox").getElementsByTagName("label")[0].click();
			doc.getElementById("accountBox").getElementsByTagName("input")[0].value=user;
			doc.defaultView.wrappedJSObject.OfficialLoginModule.getInstance().checkVerifyCode();
			doc.getElementById("passwordBox").getElementsByTagName("label")[0].click();
			doc.getElementById("passwordBox").getElementsByTagName("input")[0].value=pass;
			doc.getElementById("autoLoginLink").className=doc.getElementById("autoLoginLink").className+" chked";			
				setTimeout(function(){
					doc.getElementById("loginBtn").click();},800);
			var timer=setInterval(function (){
					if(doc.getElementById("loginedBox")==null)return;
					if(doc.getElementById("loginedBox").ownerDocument.defaultView.getComputedStyle(doc.getElementById("loginedBox"), null)["display"]=="block"){
						//已经登陆成功
						clearInterval(timer);
						document.getElementById("textbox").value="http://niu.xunlei.com/entergame/dts/?fenQuNum="+serverid;
						simple.brow.loadURI(document.getElementById("textbox").value,null, null);
						return ;
					}
			},500);
			
		}
	},
	f_shunwang:function(doc,user,pass,serverid){	
	//这服务器商太慢了，暂时不支持
		if(doc.title=="顺网SSO 登录页"){
			if(doc.getElementById("login_form_")==null)return;
			//if(doc.getElementById("lpad").ownerDocument.defaultView.getComputedStyle(doc.getElementById("secid"), null)["display"]=="none")
			{
				if(doc.getElementById("userName")==null||doc.getElementById("passwordInp")==null||doc.getElementById("loginButton_YHJ")==null)return;
				doc.getElementById("userName").value=user;
				doc.getElementById("passwordInp").value=pass;
					
				doc.getElementById("loginButton_YHJ").click();
			}
			
		}else{
			
			if(doc.getElementById("contents")==null)return;
			var timer=setInterval(function (){
					if(doc.getElementsByClassName("yesLoginCID").length<=0)return;
					
					//已经登陆成功
					clearInterval(timer);
					document.getElementById("textbox").value="http://dts.swjoy.com/front/play.htm?serverid="+serverid;
					simple.brow.loadURI(document.getElementById("textbox").value,null, null);
					return ;
					
				},500);
		}
		
		
	
			
		
	},
	f_jinshan:function(doc,user,pass,serverid){	
		if(doc.title=="登录"){
			if(doc.getElementById("lpad")==null)return;
			//if(doc.getElementById("lpad").ownerDocument.defaultView.getComputedStyle(doc.getElementById("secid"), null)["display"]=="none")
			{
				if(doc.getElementById("unm")==null||doc.getElementById("pwd")==null||doc.getElementById("rm")==null||doc.getElementById("loginbtn_img")==null)return;
				doc.getElementById("unm").value=user;
				doc.getElementById("pwd").value=pass;
				doc.getElementById("rm").checked=true;			
				doc.getElementById("loginbtn_img").parentElement.click();
			}
			
		}
		if(doc.getElementById("log")==null)return;
		if(doc.getElementById("log").ownerDocument.defaultView.getComputedStyle(doc.getElementById("log"), null)["display"]=="none"&&
		doc.getElementById("log").ownerDocument.defaultView.getComputedStyle(doc.getElementById("loged"), null)["display"]=="block"
		){
			//已经登陆成功
			return "http://wan.liebao.cn/game_frame/play_1040.php?sid="+serverid;
			//return "http://gameapp.37.com/controller/enter_game.php?game_id=237&sid="+serverid+"&showlogintype=3";
		}
		
	},
	f_shengda:function(doc,user,pass,serverid){
		if(doc.getElementById("js-nologin")==null||doc.getElementById("js-logined")==null)return;
		if(doc.getElementById("js-nologin").ownerDocument.defaultView.getComputedStyle(doc.getElementById("js-logined"), null)["display"]=="block"){
			//已经登陆成功
			return "http://www.yilewan.com/Game/play/gameId/DTS/serverId/s"+serverid+".html";
			
		}else{
			
			if(doc.getElementById("login_account")==null||doc.getElementById("password")==null||doc.getElementById("js-cross-form")==null)return;
			if(doc.getElementById("js-nologin").ownerDocument.defaultView.getComputedStyle(doc.getElementById("js-nologin"), null)["display"]=="block")
			{
				doc.getElementById("login_account").value=user;
				doc.getElementById("password").value=pass;
					
				doc.getElementById("js-cross-form").getElementsByTagName("Button")[0].click();
				var timer=setInterval(function (){
					if(doc.getElementById("js-nologin")==null||doc.getElementById("js-logined")==null)return;
					if(doc.getElementById("js-nologin").ownerDocument.defaultView.getComputedStyle(doc.getElementById("js-logined"), null)["display"]=="block"){
						//已经登陆成功
						clearInterval(timer);
						document.getElementById("textbox").value="http://www.yilewan.com/Game/play/gameId/DTS/serverId/s"+serverid+".html";
						simple.brow.loadURI(document.getElementById("textbox").value,null, null);
						return ;
					}
				},500);
			}

		}
	},
	f_37wan:function(doc,user,pass,serverid){
		if(doc.getElementById("log")==null)return;
		if(doc.getElementById("log").ownerDocument.defaultView.getComputedStyle(doc.getElementById("log"), null)["display"]=="none"){
			//已经登陆成功
			return "http://www.37.com/game/play.php?game_id=237&sid="+serverid;
			//return "http://gameapp.37.com/controller/enter_game.php?game_id=237&sid="+serverid+"&showlogintype=3";
		}else{
			
			if(doc.getElementById("username")==null||doc.getElementById("password")==null||doc.getElementById("remember")==null||doc.getElementById("log-btn")==null)return;
			doc.getElementById("username").value=user;
			doc.getElementById("password").value=pass;
			doc.getElementById("remember").checked=true;			
			doc.getElementById("log-btn").click();
			var timer=setInterval(function (){
					if(doc.getElementById("log")==null)return;
					if(doc.getElementById("log").ownerDocument.defaultView.getComputedStyle(doc.getElementById("log"), null)["display"]=="none"){
						//已经登陆成功
						clearInterval(timer);
						document.getElementById("textbox").value="http://www.37.com/game/play.php?game_id=237&sid="+serverid;
						simple.brow.loadURI(document.getElementById("textbox").value,null, null);
						return ;
					}
			},500);
		}
		
	},
	f_yy:function(doc,user,pass,serverid){
		if(doc.title=="用YY帐号登录"){
			if(doc.getElementById("secid")==null)return;
			if(doc.getElementById("secid").ownerDocument.defaultView.getComputedStyle(doc.getElementById("secid"), null)["display"]=="none"){
				if(doc.getElementById("username")==null||doc.getElementById("password")==null||doc.getElementById("login_button")==null)return;
				doc.getElementById("username").value=user;
				doc.getElementById("password").value=pass;
				doc.getElementById("login_button").click();
			}
			
		}
		if(doc.getElementById("notlogin")==null)return;
		if(doc.getElementById("notlogin").ownerDocument.defaultView.getComputedStyle(doc.getElementById("notlogin"), null)["display"]=="none")
		{
			return "http://udblogin.duowan.com/login.do?game=DTSZJ&server=s"+serverid;
			//已经登陆
		}else{
			if(doc.getElementById("udbsdk_login")==null)return;
			if(doc.getElementById("udbsdk_login").ownerDocument.defaultView.getComputedStyle(doc.getElementById("udbsdk_login"), null)["display"]=="none")
			{
				doc.getElementById("notlogin").getElementsByTagName("input")[0].click();
			//还没点开登陆窗口
				//doc.getElementById("udbsdk_login")
			}else{
				//已经点开登陆窗口
				
			}
		}
	},
	f_360:function(doc,user,pass,serverid){
		
		if(doc.getElementById("loginWrap")!=null){
			if(doc.getElementById("loginAccount")==null||doc.getElementById("loginSubmit")==null||doc.getElementById("iskeepalive")==null||doc.getElementById("lpassword")==null)return;
			doc.getElementById("loginAccount").value=user;
			doc.getElementById("lpassword").value=pass;
			doc.getElementById("iskeepalive").checked="checked";			
			doc.getElementById("loginSubmit").click();
			
		}else
		{
			if(doc.getElementsByClassName("j-logout").length>0)
			return "http://dtszj.wan.360.cn/game_login.php?server_id=S"+serverid+"&src=360wan-jp-dtszj";
		}
	},
	f_6711:function(doc,user,pass,serverid){
		if(doc.getElementById("user")==null)return;
		if(doc.getElementById("user").ownerDocument.defaultView.getComputedStyle(doc.getElementById("user"), null)["display"]=="none"){
			//已经登陆成功
			return "http://www.6711.com/entergame.php?game=dts&server=S"+serverid+"&showlogintype=3";
		}else{
			
			if(doc.getElementById("login_account")==null||doc.getElementById("password")==null||doc.getElementById("cookie_save")==null||doc.getElementById("login_button")==null)return;
			doc.getElementById("login_account").value=user;
			doc.getElementById("password").value=pass;
			doc.getElementById("cookie_save").checked=true;			
			doc.getElementById("login_button").click();
		}
		
		
	},
	f_baidu:function(doc,user,pass,serverid){
		if(doc.getElementsByClassName("login-form").length<=0)return;
		var timer=setInterval(function (){		
					clearInterval(timer);
					if(doc.getElementsByClassName("login-form")[0].ownerDocument.defaultView.getComputedStyle(doc.getElementsByClassName("login-form")[0], null)["display"]=="none"){
						//已经登陆成功
						
						document.getElementById("textbox").value="http://youxi.baidu.com/login_game_by_post.xhtml?id=842&serverId=S"+serverid;
						simple.brow.loadURI(document.getElementById("textbox").value,null, null);
						
					}else{
						
						if(doc.getElementById("username")==null||doc.getElementById("password")==null||doc.getElementById("remember")==null||doc.getElementById("log-btn")==null)return;
						doc.getElementById("username").value=user;
						doc.getElementById("password").value=pass;
						doc.getElementById("remember").checked=true;			
						doc.getElementById("log-btn").click();
					}
			},1000);
						
		
		
		
	
			
	},
	f_sougou:function(doc,user,pass,serverid){
		if(doc.getElementById("name")==null||doc.getElementById("userPass")==null||doc.getElementById("auto-login")==null||doc.getElementById("qfname")==null||doc.getElementById("playNow")==null)return;
			var event = doc.createEvent('HTMLEvents');  

			event.initEvent("focus", true, true);  
			event.eventType = 'message'; 

			doc.getElementById("name").dispatchEvent(event);  

			doc.getElementById("name").value=user;
			doc.getElementById("userPass").value=pass;
			doc.getElementById("auto-login").getElementsByClassName("auto-login")[0].className=doc.getElementById("auto-login").getElementsByClassName("auto-login")[0].className+" auto";
			doc.getElementById("qfname").setAttribute("sid",""+serverid);
			doc.getElementById("playNow").click();
	
		
	}
};
var simple = {
    debug: true,
    brow: null,
	gamename:"DTS",
	version:21,
    botcfg: {
        tasklist:["狼魂要塞","赤色要塞","世界BOSS","血色城堡","恶魔广场","精英挑战","日常任务","主线任务","自动挂机"],	
		dailygametask:{Enable:true,RunTils:300,CanDoTime:[],CanNotDoWeekDay:[],cfg:{}},//任务设置
		maingametask:{Enable:true,RunTils:300,CanDoTime:[],CanNotDoWeekDay:[],cfg:{}},//任务设置		
		equip:{Enable:true,RunTils:10,CanDoTime:[],CanNotDoWeekDay:[],cfg:{}},//基本设置
		shop:{Enable:true,RunTils:60,CanDoTime:[],CanNotDoWeekDay:[],cfg:{HPCount:1000,MPCount:1000,intensify:1,q_equip_steplv:1,onlyforme:true,luck:1,addAttributLevel:1,q_quality_lv:2,q_remarkable:1}},//买卖设置
		addpoint:{Enable:true,RunTils:30,CanDoTime:[],CanNotDoWeekDay:[],cfg:{minpoint:30}},		//基本设置
		guaji:{Enable:true,RunTils:60,CanDoTime:[],CanNotDoWeekDay:[],cfg:{pos:[],autofightback:true,changepostils:60,huntmode:false,killermode:false}},//挂机设置
		
		globalset:{Enable:true,RunTils:1200,CanDoTime:[],CanNotDoWeekDay:[],cfg:{offlineexpenable:true,usegoldexp:false,usemoneyexp:false,autosign:true,autogetsignreward:true,clearmode:true,livenessreward:true}},//基本设置
		zhuzao:{Enable:true,RunTils:1800,CanDoTime:[],CanNotDoWeekDay:[],cfg:{moneymin:1400000}},
		moniter:{Enable:true,RunTils:1,CanDoTime:[],CanNotDoWeekDay:[],cfg:{}},
		boss:{Enable:true,RunTils:60,CanDoTime:[],CanNotDoWeekDay:[],cfg:{bosslist:[]}},//boss设置
		
		chiseyaosai:{Enable:true,RunTils:60,CanDoTime:[{BeginTime:900,EndTime:910},{BeginTime:1260,EndTime:1270}],CanNotDoWeekDay:[],cfg:{onlyFightBack:true}},//副本设置
		langhunyaosai:{Enable:true,RunTils:60,CanDoTime:[{BeginTime:1230,EndTime:1320}],CanNotDoWeekDay:[],cfg:{}},//副本设置
		jingyintiaozhan:{Enable:true,RunTils:600,CanDoTime:[],CanNotDoWeekDay:[],cfg:{usemoney:true,usegold:false,autohecheng:true,autobuy:true,fightpowerhigh:100}},		//副本设置
		xuese:{Enable:true,RunTils:300,CanDoTime:[],CanNotDoWeekDay:[],cfg:{autohecheng:true,autobuy:true}},//副本设置
		emo:{Enable:true,RunTils:300,CanDoTime:[],CanNotDoWeekDay:[],cfg:{autohecheng:true,autobuy:true}},//副本		
		system:{autostart:false,flashtils:60,autoreconnect:true}//基本设置
	
    },
	username:"",
	cfgfilename:"",
	autologinset:{enable:false,user:"",pass:"",agent:1,serverid:"231"},	
	maplist:["坎特鲁废墟","黄金火龙王","失落之塔六层","勇者大陆","VIP5副本","VIP7副本","地下城二层","失落之塔一层","失落之塔二层","失落之塔三层","失落之塔四层","罗兰峡谷","地下城一层","失落之塔七层","地下城三层","死亡沙漠",
"天空之城","古战场(自由PK区)","魔炼之地(自由PK区)","冰风谷","坎特鲁遗址","亚特兰蒂斯","冰霜之城","幽暗森林","失落之塔五层","VIP4副本"],
    urlfixer: null,
	flashtimer:null,
    runstate: {start:false},
    GetCfg: function () {
       
        return json2str(simple.botcfg);
    },
    GetRunState: function () {
        return json2str(simple.runstate);
    },
    ShowGameInfo: function (str) {
        document.getElementById("GameInfo").label = str
    },
	GameStart:function(rolename){
		
		simple.cfgfilename=simple.username+"_"+rolename;
		var obj=simple.LoadcfgFile(simple.cfgfilename)
		simple.botcfg=(obj==null?simple.botcfg:obj);				
		simple.CfgToView(document.getElementById("cfgdlg"));
		document.getElementById("Cfgbutton").style.display="block";
		if(simple.flashtimer!=null)clearTimeout(simple.flashtimer);
		simple.flashtimer=setTimeout(function(){simple.brow.reload();},1000*60*simple.botcfg.system.flashtils);
		if(simple.botcfg.system.autostart&&simple.runstate.start==false){
			window.setTimeout(function(){
				simple.runstate.start=true;
				document.getElementById("Startbutton").label="停止挂机";
				
				},
			3000);
		}
	},
	GameDisConnect:function(){
		if(!simple.botcfg.system.autoreconnect)return;
		simple.brow.reload();

	},
	shellExe:function(url){
		
		simple.urlfixer.shell(url);
		return false;
	},
	Heart:function(seed){
		return this.urlfixer.tick(seed);
	},
	Log: function (str) {
		
		if(document.getElementById("ShowLog").checked!=true)return;
		var list=document.getElementById("LogList");
		if(list.getRowCount()>100){
		
			list.removeItemAt( list.getRowCount()-1 );
		}

		list.insertItemAt(0,str);
    },

    LoadcfgFile: function (filename) {
        
        var mt = new MTB_File(filename+".cfg");
        if (mt.open("r")) {
            var s = mt.read(mt.fileSize());
            mt.close();
            var b = new Base64();
            var str = b.decode(s);
			
            var obj = eval("(" + str + ")");
        
			return obj;
		
        } 
		return null;
    },
	SavecfgFile:function(filename,obj){
		var mt = new MTB_File(filename+".cfg");
        if (mt.open("w+")) {
			
            var b = new Base64();
            var str = b.encode(json2str(obj));
            mt.write(str)
            mt.close();
			return true;
        }
		return false
	},
	AddGuajiPoint:function(){
		
		
		
		var row = document.createElement('listitem');
		var cell = document.createElement('listcell');
		cell.setAttribute('label',document.getElementById("maplist").selectedItem.label);
		row.appendChild(cell);
		cell = document.createElement('listcell');
		cell.setAttribute('label', document.getElementById("mapx").value);
		row.appendChild(cell);
		cell = document.createElement('listcell');
		cell.setAttribute('label', document.getElementById("mapy").value);
		row.appendChild(cell);
		document.getElementById("botcfg.guaji.cfg.pos").appendChild(row);
	},
	ClearGuajiPoint:function(){
		var ele=document.getElementById("botcfg.guaji.cfg.pos");
		while(ele.getRowCount()>0)ele.removeItemAt(0);
	},
	DelGuajiPoint:function(){
		var ele=document.getElementById("botcfg.guaji.cfg.pos");
		while(ele.selectedCount>0){
			ele.removeItemAt(ele.selectedIndex);
		}
		
	},
	CfgToView:function(container){
		var list=container.getElementsByClassName("cfg");
		
		for(var i=0;i<list.length;i++){
			var ele=list[i];			
			if(ele.tagName=="textbox"){
				
				ele.value=eval(("simple."+ele.id));
			}
			
		
			if(ele.tagName=="menulist"){
				var items=ele.getElementsByTagName("menuitem");
				var value=eval(("simple."+ele.id));
				for(var j=0;j<items.length;j++){
					if(items[j].value==value){
						
						ele.selectedItem=items[j];
						break;
					}
				}
			}
			if(ele.tagName=="checkbox"){
				ele.checked=eval(("simple."+ele.id));
			}
			if(ele.tagName=="groupbox"){
				
				var items=ele.getElementsByTagName("checkbox");
				var value=eval(("simple."+ele.id));
				for(var j=0;j<items.length;j++){
					var label=items[j].label;
					items[j].checked=false;
					for(var k=0;k<value.length;k++)
					{
						if(label==value[k])
						{
							items[j].checked=true;
							break;
						}
					}
				}
			}
			if(ele.tagName=="listbox"){
				while(ele.getRowCount()>0)ele.removeItemAt(0);
				var header=ele.getElementsByTagName("listheader");
				var value=eval(("simple."+ele.id));
				for(var k=0;k<value.length;k++){
					var row = document.createElement('listitem');
					for(var j=0;j<header.length;j++){
						var cell = document.createElement('listcell');
						cell.setAttribute('label', eval(  ("simple."+ele.id+"["+k+"]."+header[j].id) ));
						row.appendChild(cell);
					}
					ele.appendChild(row);
				}				
			}
		}
	},

	ViewToCfg:function(container){
		var list=container.getElementsByClassName("cfg");
		
		for(var i=0;i<list.length;i++){
			var ele=list[i];			
			if(ele.tagName=="textbox"){
				
				eval(("simple."+ele.id+"=ele.value"));
			}
			
		
			if(ele.tagName=="menulist"){
				
				
				eval(("simple."+ele.id+"=ele.selectedItem.value"));				
			}
			if(ele.tagName=="checkbox"){
				eval(("simple."+ele.id+"=ele.checked"));
			}
			if(ele.tagName=="groupbox"){
				
				var items=ele.getElementsByTagName("checkbox");
				eval(("simple."+ele.id+".length=0"));
				for(var j=0;j<items.length;j++){
					var label=items[j].label;
					if(items[j].checked){
						eval(("simple."+ele.id+".push('"+label+"')"));
					}					
				}
			}
			if(ele.tagName=="listbox"){
				eval(("simple."+ele.id+".length=0"));
				var header=ele.getElementsByTagName("listheader");
				for(var k=0;k<ele.getRowCount();k++)
				{
					var row=ele.getItemAtIndex(k);
					var cells=row.getElementsByTagName("listcell");
					var obj={};
					for(var j=0;j<cells.length;j++){
						
						eval("obj."+header[j].id+"=cells[j].getAttribute('label')");
					}
					eval(("simple."+ele.id+".push(obj)"));
				}
				
					
			}
		}
	},
	SaveAutoLogin:function(){
		simple.ViewToCfg(document.getElementById("autologinset"));
		simple.SavecfgFile(simple.username,simple.autologinset);
	},
	CancelAutoLogin:function(){
		simple.CfgToView(document.getElementById("autologinset"));
		document.getElementById("autologinset").hidePopup();
	},
	onReg:function(){
		var username=document.getElementById("reg_username").value;
		var p=document.getElementById("reg_password").value;
		var sp=document.getElementById("reg_superpassword").value;
		if(username==""){
			document.getElementById("logintip").value="请输入辅助帐号";
			return;
		}
		if(p==""){
			document.getElementById("logintip").value="请输入辅助密码"
			return;
		}
		if(document.getElementById("reg_password2").value!=p){
			document.getElementById("logintip").value="两次密码输入不一致"
			return;
		}
		if(sp==""){
			document.getElementById("logintip").value="请输入辅助超级密码"
			return;
		}
		var result=simple.urlfixer.reg(username,p,sp);
		result= eval("(" + result + ")");
		if(result.success){
			var sr=simple.urlfixer.activegame(username,p,simple.gamename);			
			alert("注册成功");
			document.getElementById("logintip").value="注册成功";
		}else{
			alert(result.text);
			document.getElementById("logintip").value=result.text;
		}
	},
	onRecharge:function(){
		var username=document.getElementById("recharge_username").value;
		var c=document.getElementById("recharge_cardid").value;
		var p=document.getElementById("recharge_cardps").value;
		var code=document.getElementById("recharge_code").value;
		if(username==""){
			document.getElementById("logintip").value="请输入辅助帐号";
			return;
		}
		if(c==""){
			document.getElementById("logintip").value="请输入充值卡卡号";
			return;
		}
		if(p==""){
			document.getElementById("logintip").value="请输入充值卡密码";
			return;
		}
		var result=simple.urlfixer.recharge(c,p,simple.gamename,username,code,document.getElementById("recharge_force").checked?1:0);
		result= eval("(" + result + ")");
		if(result.success){
			var s="充值成功,帐号到期时间为"+result.endTime+(result.code==""?"":(" 获得亲友码:"+result.code+" (已经复制到剪切板)"));
			document.getElementById("logintip").value="充值成功,帐号到期时间为"+result.endTime;
			copyToClipBoard(result.code);
			alert(s);
		}else{
			alert(result.text);
			document.getElementById("logintip").value=result.text;
		}
	},
	onLogin:function(){
		
		simple.username=document.getElementById("username").value;
		var p=document.getElementById("password").value
		if(simple.username==""){
			document.getElementById("logintip").value="请输入辅助帐号";
			return;
		}
		if(p==""){
			document.getElementById("logintip").value="请输入辅助密码"
			return;
		}
		
		var result=simple.urlfixer.login(simple.username,p,simple.gamename,simple.version);
		
		
		result= eval("(" + result + ")");
		var mt = new MTB_File("rp.cfg");
		if (mt.open("w+")) {
			mt.write("");
			mt.close();
			
		}
		if(result.success){
			if(document.getElementById("remeberps").checked==true)
			{
				var mt2 = new MTB_File("rp.cfg");
				if (mt2.open("w+")) {
					
					var b = new Base64();
					var str = b.encode(simple.username+"【】"+p);
					mt2.write(str)
					mt2.close();
					
				}
			}	
			
			
			document.getElementById("logintip").value=result.text;
			simple.ShowGameInfo(simple.username+"使用中,"+result.text);
			var obj=simple.LoadcfgFile(simple.username)
			simple.autologinset=(obj==null?simple.autologinset:obj);				
			if(document.getElementById("disableAutoLogin").checked)
				simple.autologinset.enable=false;
			simple.CfgToView(document.getElementById("autologinset"));
		//	document.getElementById("Autologinbutton").style.display="block";
			//document.getElementById("Startbutton").style.display="block"
			document.getElementById("toolbox").style.display="block"
			document.getElementById("urlenter").style.maxHeight=null;
			
			document.getElementById("logindlg").hidePopup();
			
			if(simple.autologinset.enable){
				
				document.getElementById("textbox").value=autologin.open(simple.autologinset.agent);
				simple.brow.loadURI(document.getElementById("textbox").value,null, null);
			}else{
				document.getElementById("textbox").value="";
				simple.brow.loadURI("chrome://simple/content/index.html" ,null, null);
			}
			
		}else{
			//弹出错误信息		
			
			alert(result.text);
			document.getElementById("logintip").value=result.text;
		}
		return;

		
	},
    onCfgSave: function () {      
		simple.ViewToCfg(document.getElementById("cfgdlg"));		
		simple.SavecfgFile(simple.cfgfilename,simple.botcfg);
			if(simple.flashtimer!=null)clearTimeout(simple.flashtimer);
		simple.flashtimer=setTimeout(function(){simple.brow.reload();},1000*60*simple.botcfg.system.flashtils);
        simple.addDocJs(simple.brow.contentWindow);
		
    },
    onCfgCancel: function () {
        
		simple.CfgToView(document.getElementById("cfgdlg"));
		document.getElementById("cfgdlg").hidePopup();
    },
    onLoad: function () 
	{		
		this.brow = document.getElementById("br");
		
		window.setTimeout(function(){
			var menu = document.getElementById("maplist");
			for (var i = 0; i < simple.maplist.length; i++) {
				menu.appendItem(simple.maplist[i]);
			}
			var mt = new MTB_File("rp.cfg");
			if (mt.open("r")) 
			{
				var s = mt.read(mt.fileSize());
				mt.close();
				if(s.length>0)
				{
					var b = new Base64();
					var str = b.decode(s);
					var ar=str.split("【】");
					document.getElementById("username").value=ar[0];
					document.getElementById("password").value=ar[1];
					document.getElementById("remeberps").checked=true;
				}
			} 
			document.getElementById("logindlg").openPopup(null ,"overlap",window.screen.width/2-200,window.screen.height/2-200,false,false,null);
		},500);
		
		
        
		document.getElementById("infodlg").sizeTo( window.screen.width/2,window.screen.height/2 );
		this.urlfixer = Components.classes["@urlfix.com/urlfixer;1"].
        createInstance(Components.interfaces.IUrlfixerInterface);
		document.getElementById("textbox").value=this.urlfixer;
        this.brow.addEventListener("DOMContentLoaded", this.onPageLoad, true);
        this.brow.addEventListener("DOMFrameContentLoaded", this.onPageLoad, true);
        return;
     
		
    },

    onPageLoad: function (aEvent) {
		if(simple.brow.currentURI.host=="simple")
		document.getElementById("textbox").value=""
		else
		document.getElementById("textbox").value="http://"+simple.brow.currentURI.host+simple.brow.currentURI.path;
        var doc = aEvent.originalTarget;
        if (doc.nodeName != "#document" && doc.nodeName != "IFRAME") { return; }
        // perform substitutions on the loaded document
        simple.addDocJs(doc.defaultView);
    },
    addDocJs: function (aWindow) {
        if (!aWindow) aWindow = window.content;
        if (aWindow.frames.length > 0) {
            var nFrames = aWindow.frames.length;
            for (var i = 0; i < nFrames; i++) this.addDocJs(aWindow.frames[i]);
        }
        var doc = aWindow.document;

        if (!doc || !("body" in doc)) return;

        var url = doc.URL;
        if (!url) return;
		aWindow.addEventListener('load',function(e)
		{
			var d=e.currentTarget.document;
			if(d==null)return;
			var Alist=d.getElementsByTagName("A");
			for(var i=0;i<Alist.length;i++)
			{
				Alist[i].target="";
				Alist[i].addEventListener("click",function(){
						aWindow.setTimeout(function(){var Alist=d.getElementsByTagName("A");
						for(var i=0;i<Alist.length;i++)
						{
							Alist[i].target="_parent";
						}},300);
						aWindow.setTimeout(function(){var Alist=d.getElementsByTagName("A");
						for(var i=0;i<Alist.length;i++)
						{
							Alist[i].target="_parent";
						}},500);
						aWindow.setTimeout(function(){var Alist=d.getElementsByTagName("A");
						for(var i=0;i<Alist.length;i++)
						{
							Alist[i].target="_parent";
						}},1000);
						aWindow.setTimeout(function(){var Alist=d.getElementsByTagName("A");
						for(var i=0;i<Alist.length;i++)
						{
							Alist[i].target="_parent";
						}},2000);
					
				}, true);
				
				
			}
		;
	
			//加入自动登陆
			if(simple.autologinset.enable){
				var url=autologin.login(d,simple.autologinset.agent,simple.autologinset.user,simple.autologinset.pass,simple.autologinset.serverid);
				if(url!=""&&url!=null){
					document.getElementById("textbox").value=url;
					simple.brow.loadURI(url, null, null);
				}
				/*
				doc.getElementById("login_account").value="wyklfg";	
				doc.getElementById("password").value="000123";	
				doc.getElementById("login_account").value="wzquyin";	
				doc.getElementById("password").value="stsatan";	*/
			}
			
		},false);
		

		var Alist=doc.getElementsByTagName("A");
		for(var i=0;i<Alist.length;i++)
		{
			Alist[i].target="_parent";			
		}
        

        //写入配置
		aWindow.wrappedJSObject.window.open=function(url){simple.brow.loadURI(url, null, null);return false;}
		aWindow.wrappedJSObject.window.onbeforeunload=null;
		if(doc.getElementsByClassName("fastEnterGameLink").length>0)
		{
			
			for(var i=0;i<doc.getElementsByClassName("fastEnterGameLink").length;i++)
			{
				var ele=doc.getElementsByClassName("fastEnterGameLink")[i];		
				aWindow.wrappedJSObject.window.jQuery.event.remove(ele, 'click', null,null)
				
				ele.addEventListener("click",function(){
				
					simple.brow.loadURI("http://niu.xunlei.com/entergame/dts/?fenQuNum="+this.parentElement.getElementsByClassName('fastEnterGameInput')[0].value,null,null);
					
					return false;
				},false);
				//ele.parent.getElementsByClassName("fastEnterGameInput").value
			}
		}
		if(doc.getElementsByClassName("btn_serv").length>0)
		{
			for(var i=0;i<doc.getElementsByClassName("btn_serv").length;i++)
			{
				var ele=doc.getElementsByClassName("btn_serv")[i];		
				aWindow.wrappedJSObject.window.jQuery.event.remove(ele, 'click', null,null)
				
				ele.addEventListener("click",function(){
					
					simple.brow.loadURI("http://niu.xunlei.com/entergame/dts/?fenQuNum="+this.getAttribute('sid'),null,null);
					return false;
				},false);
				//ele.parent.getElementsByClassName("fastEnterGameInput").value
			}
		}
		/*if(aWindow.wrappedJSObject.ishook==null);
		{
		alert("asdf");
			aWindow.wrappedJSObject.document.createElement2 = aWindow.wrappedJSObject.document.createElement;	
			aWindow.wrappedJSObject.document.createElement = function(tag){		
			 if (tag != "a"&&tag!="A")return aWindow.wrappedJSObject.document.createElement2(tag);
				
				var node = aWindow.wrappedJSObject.document.createElement2(tag)
				
				//node.click =function(){simple.brow.loadURI(node.getAttribute("href"),null,null);};
				return node

			}
			aWindow.wrappedJSObject.ishook=function(){}
		}*/
		aWindow.wrappedJSObject.Heart=function(seed)
		{
			return simple.Heart(seed);
		}
		aWindow.wrappedJSObject.GetCfg=function()
		{
			return simple.GetCfg();
		}
		aWindow.wrappedJSObject.GameStart=function(rolename)
		{
			return simple.GameStart(rolename);
		}
		
		aWindow.wrappedJSObject.GameDisConnect=function()
		{
			return simple.GameDisConnect();
		}
		
		aWindow.wrappedJSObject.GetRunState=function()
		{
			return simple.GetRunState();
		}
		aWindow.wrappedJSObject.ShowGameInfo=function(srt)
		{
			return simple.ShowGameInfo(srt);
		}
		aWindow.wrappedJSObject.Log=function(srt)
		{
			return simple.Log(srt);
		}
		aWindow.wrappedJSObject.saveToFile=function(srt)
		{
			var mt=new MTB_File(Math.random()+".sswf");
			mt.open("w+");
			mt.write(srt);
			mt.close();
		}
		
	
    },

    onGoButtonClick: function () {
        var textbox = document.getElementById("textbox");

        if (textbox.value == "") {
           
        } else {
			if(textbox.value.indexOf("http://")==-1)textbox.value="http://"+textbox.value;
            simple.brow.loadURI(textbox.value, null, null);
        }

        return;

    },
	onBackIndex:function(){
		document.getElementById("textbox").value="";
				simple.brow.loadURI("chrome://simple/content/index.html" ,null, null);
	},
	onReFlashButtonClick:function(){
	 var textbox = document.getElementById("textbox");

        if (textbox.value == "") {
            
        } else {
			if(textbox.value.indexOf("http://")==-1)textbox.value="http://"+textbox.value;
            simple.brow.loadURI(textbox.value, null, null);
        }
	},
	
   
	onStartButtonClick:function(){
		simple.runstate.start=!simple.runstate.start;
		document.getElementById("Startbutton").label=simple.runstate.start?"停止挂机":"开始挂机";
	}
	
};


window.addEventListener("load", function () {
    simple.onLoad();
}, false);
window.addEventListener("unload",function(){
	simple.urlfixer.loginout(simple.gamename);
},false);
window.addEventListener("close", function( event ) { 
   simple.urlfixer.loginout(simple.gamename);
}, false);
//*****************************************************************************************************//
