// 一个简单的方法直接设置document.body的背景颜色，即开关灯效果

var currentPageUrl = "";

//djcs点击次数，如果为0可点击，1不点击，valuekey点击关键字；
//sx选择手机顺序，用#号隔开；sxtz登录是否停止，0运行，1停止。
//joke为0传参数到后台页面，可点击关键字；1不点击关键字；2手机选择停止
//lb为0，转为点击关键字，lb=2，设置关键字，lb=3设置选择序号
function qg(djcs,valuekey,sx,sxtz,ckjb){
	currentPageUrl = document.location.toString().toLowerCase();
	var Request = new Object();
	Request = GetRequest();
	var lb;
        var txtwjm;
	lb= Request['lb'];


       if(currentPageUrl.substr(0,53)=="http://qzs.qq.com/qzone/mall/v8/module/act/index.html")
        {
                //document.getElementById('J_GetCode').click();
       var as = document.getElementsByTagName("a");
			var cs=0;
			var tj="";
                         
			for(var i=0; i<as.length; i++){
                            
                            //alert(as[i].innerHTML);
				if(valuekey==""){
					if(as[i].innerHTML=="<span class=\"btn_txt\">立即抢购机码</span>"  ){
						cs=cs+1
						as[i].click();
                                                
						
					}
				}
				else{
					if(as[i].innerHTML==valuekey){
						cs=cs+1
						as[i].click();
					        
					}
				}
			    
                       }
       
       }





        if(ckjb!=0)
        {
            document.title="窗口句柄:" + ckjb.toString();
        }
//lb为0，开始抢购
	if(lb=='0')
        {
           var qqdlwjm,qqdlzt;
            qqdlwjm="c:\\temp\\" + ckjb.toString() + "_qqdl.txt";
             if(plugin.fileExists(qqdlwjm))
             {
                qqdlzt=readtext(qqdlwjm);
               if(qqdlzt=="0")
               {
                 document.getElementById('onekey').click();
                 writetext(qqdlwjm,"1")
               } 
             }
	}


//设置窗口句柄
	if(lb=='4')
        {
		var jb;
		jb='4/' + Request['ckjb'];
                //alert(jb);
		var port = chrome.extension.connect({
			name: "qgks"
		}
		);
		port.postMessage({
			joke: jb
		}
		);
	}

//lb=2，设置关键字
	if(lb=='2')
        {
		var keys="";
		keys="2/"+Request['key'];
		var port = chrome.extension.connect({
			name: "qgks"
		}
		);
		port.postMessage({
			joke: keys
		}
		);
	}

	
	
	
       
		
	
    
	     
                  


       if(currentPageUrl.substr(0,20)=="http://pt.3g.qq.com/"){
                document.getElementById('guideSkip').click(); 
               if(sxtz==0)
               { 
		var username,pw;
                txtwjm="c:\\temp\\" + ckjb.toString() + "_dl.txt";
                var dlm=readtext(txtwjm);
		username= dlm.split("/")[0];
		pw = dlm.split("/")[1];
		document.getElementById('ipt_uin').value =username;
		document.getElementById('ipt_pwd').value =pw;
                document.getElementById('btn_submit').click();
                
              var port = chrome.extension.connect({
			name: "qgks"
		}
		);
		port.postMessage({
			joke: 3
		}
		);
                   
                } 
	}

      
	
}

var plugin = document.createElement("embed");
plugin.setAttribute("type", "application/x-npapi-file-io");
document.documentElement.appendChild(plugin);

//读取本地文本
function readtext(txtwjm){
	var a = txtwjm
        return plugin.getTextFile(txtwjm);
}

//写入本地文本
function writetext(txtwjm,text)
{
    var a = txtwjm;
    if(plugin.fileExists(a))
    {
      plugin.removeFile(a);
    }
    plugin.saveTextFile(a,text);
}

//按给定顺序选择手机
function jxrz(xmsx){
	sx = xmsx.split("#");
	var as = document.getElementsByTagName("div");
	for(var n=0; n<sx.length-1 ;n++){
		for(var i=0; i<as.length-1; i++){
			if(as[i].getAttribute("mid")==sx[n] && as[i].getAttribute("class")!="radio soldOver"){
				as[i].click();
                                //var txtwjm="c:\\temp\\" + ckjb.toString() + "_1.txt";
                                //writetext(txtwjm);
				var port = chrome.extension.connect({
					name: "qgks"
				}
				);
				port.postMessage({
					joke: 2
				}
				);
				return;
			}
		}
	}
}

function qg01(){
	var port = chrome.extension.connect({
		name: "qgks"
	}
	);
	port.postMessage({
		joke: 1
	}
	);
}

window._alert = window.alert;
function fun(){
window.alert = function(str){
	return;
}
}
function fun1(){
	window.alert = window._alert;
}
function max(){
	self.moveTo(0,0);
	self.resizeTo(800,600);
}

function GetRequest(){
	var url = location.search;
	var theRequest = new Object();
	if (url.indexOf("?") != -1){
		var str = url.substr(1);
		strs = str.split("&");
		for(var i = 0; i < strs.length; i ++){
			theRequest[strs[i].split("=")[0]]=DecodeURI(strs[i].split("=")[1],false);
		}
	}
	return theRequest;
}

var EncodeURI = function(unzipStr,isCusEncode){
	if(isCusEncode){
		var zipArray = new Array();
		var zipstr = "";
		var lens = new Array();
		for(var i=0;i<unzipStr.length;i++){
			var ac = unzipStr.charCodeAt(i);
			zipstr += ac;
			lens = lens.concat(ac.toString().length);
		}
		zipArray = zipArray.concat(zipstr);
		zipArray = zipArray.concat(lens.join("O"));
		return zipArray.join("N");
	}
	else{
		var zipstr="";
		var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%";
		var tt= "";
		for(var i=0;i<unzipStr.length;i++){
			var chr = unzipStr.charAt(i);
			var c=StringToAscii(chr);
			tt += chr+":"+c+"n";
			if(parseInt("0x"+c) > 0x7f){
				zipstr+=encodeURI(unzipStr.substr(i,1));
			}
			else{
				if(chr==" ")
				zipstr+="+";
				else if(strSpecial.indexOf(chr)!=-1)
				zipstr+="%"+c.toString(16);
				else
				zipstr+=chr;
			}
		}
		return zipstr;
	}
}
var DecodeURI = function(zipStr,isCusEncode){
	if(isCusEncode){
		var zipArray = zipStr.split("N");
		var zipSrcStr = zipArray[0];
		var zipLens;
		if(zipArray[1]){
			zipLens = zipArray[1].split("O");
		}
		else{
			zipLens.length = 0;
		}
		var uzipStr = "";
		for(var j=0;j<zipLens.length;j++){
			var charLen = parseInt(zipLens[j]);
			uzipStr+= String.fromCharCode(zipSrcStr.substr(0,charLen));
			zipSrcStr = zipSrcStr.slice(charLen,zipSrcStr.length);
		}
		return uzipStr;
	}
	else{
		var uzipStr="";
		for(var i=0;i<zipStr.length;i++){
			var chr = zipStr.charAt(i);
			if(chr == "+"){
				uzipStr+=" ";
			}
			else if(chr=="%"){
				var asc = zipStr.substring(i+1,i+3);
				if(parseInt("0x"+asc)>0x7f){
					uzipStr+=decodeURI("%"+asc.toString()+zipStr.substring(i+3,i+9).toString());;
					i+=8;
				}
				else{
					uzipStr+=AsciiToString(parseInt("0x"+asc));
					i+=2;
				}
			}
			else{
				uzipStr+= chr;
			}
		}
		return uzipStr;
	}
}
var StringToAscii = function(str){
	return str.charCodeAt(0).toString(16);
}
var AsciiToString = function(asccode){
	return String.fromCharCode(asccode);
}
