<script language="jscript" runat="server">
//HashTable类
function GetJSON(str){
	var json_=null;
	try{
		eval("json_=(function(){return " + str + "})()");
		if(typeof json_=="object"){
			json_["mo__"]=true;
			json_["exists__"]=function(key){return this[key]!=undefined;};
			json_["value__"]=function(key){return (this[key]!=undefined?this[key]:"");};
		}
		return json_;
	}catch(ex){return {"mo__":false,"desc":ex.description};}
}
function OAUTH20(client_id, client_secret, redirect_uri){
	return new __OAUTH20__(client_id, client_secret, redirect_uri);
}
function __OAUTH20__(client_id, client_secret,redirect_uri){
	this.table=[];
	this.client_id = client_id;
	this.redirect_uri = redirect_uri;
	this.client_secret = client_secret;
	if(!redirect_uri) this.redirect_uri = "";
	if(!client_id) this.client_id = "";
	if(!client_secret) this.client_secret = "";
	this.debug={};
}
__OAUTH20__.prototype.toString=function(){
	return "OAUTH 2.0(__OAUTH20__ 1.0) BY Anlige";	
};
__OAUTH20__.prototype.getAuthorizationUrl = function(authorization_url){
	var url = authorization_url + "?client_id=" + utf8(this.client_id) + "&response_type=code";
	if(this.redirect_uri!="") url += "&redirect_uri=" + utf8(this.redirect_uri);
	var data = this.APISTring("utf-8")
	if(data!="") url += "&" + data;
	return url;
}
__OAUTH20__.prototype.getAccessToken = function(access_token_url, code , method, format){
	if(!format){format = "txt";}
	if(!method){method = "GET";}
	var data = "grant_type=authorization_code&code=" + utf8(code)
	if(this.redirect_uri!="") data += "&redirect_uri=" + utf8(this.redirect_uri);
	if(this.client_id!="") data += "&client_id=" + utf8(this.client_id);
	if(this.client_secret!="") data += "&client_secret=" + utf8(this.client_secret);
	var data1 = this.APISTring("utf-8")
	if(data1!="") data += "&" + data1;
	return this.SendData(access_token_url,method,data,format);
};

__OAUTH20__.prototype.RefreshToken = function(refresh_token_url, refresh_token , method, format){
	if(!format){format = "txt";}
	if(!method){method = "GET";}
	var data = "grant_type=refresh_token&client_id=" + utf8(this.client_id) + "&refresh_token=" + utf8(refresh_token) + "&client_secret=" + utf8(this.client_secret)
	if(this.redirect_uri!="") data += "&redirect_uri=" + utf8(this.redirect_uri);
	if(this.client_id!="") data += "&client_id=" + utf8(this.client_id);
	if(this.client_secret!="") data += "&client_secret=" + utf8(this.client_secret);
	var data1 = this.APISTring("utf-8")
	if(data1!="") data += "&" + data1;
	return this.SendData(refresh_token_url,method,data,format);
};

__OAUTH20__.prototype.SendData = function(url,method,data,format){
	var myhttp = new _SmartHttp(url,method,data);
	myhttp.autoClearBuffer=false;
	myhttp.send();
	if(format=="json"){
		var result =myhttp.getjson("utf-8");
	}else if(format=="xml"){
		var result =myhttp.getxml("utf-8");
	}else{
		var result =myhttp.gettext("utf-8");	
	}
	this.debug["url"]=myhttp.url;
	this.debug["data"]=data;
	myhttp = null;
	return result;	
};

__OAUTH20__.prototype.Sort = function(order,key){
	if(this.Count()<=0){return ;}
	var isASC = true;
	if(order.toLowerCase()=="asc"){
		isASC=true;
	}else if(order.toLowerCase()=="desc"){
		isASC=false;
	}
	if(key!="key" && key !="value"){key="key";}
	var __temp = null;
	for(var i=0;i<this.Count()-1;i++){
		for(var j=i+1;j<this.Count();j++){
			if(this.table[i][key]>this.table[j][key] == isASC){
				var thevalue=this.table[i];
				this.table[i] = this.table[j];
				this.table[j] = thevalue;
			}
		}
	}
};


__OAUTH20__.prototype.APISTring = function(charset,split1,split2){
	charset ? charset : (charset="no");
	charset = charset.toLowerCase();
	if(!split1){split1="=";}
	if(!split2 && split2!=""){split2="&";}
	if(this.table.length<=0){return "";}
	var str = "";
	for(var i=0;i< this.table.length;i++){
		if(this.table[i]["value"]==""){continue;}
		var val = this.table[i]["value"];
		if(charset=="gb2312"){
			str += this.table[i]["key"] + split1 + escape(val) + split2;
		}else if(charset=="gbk"){
			str += this.table[i]["key"] + split1 + Server.URLEncode(val) + split2;
		}else if(charset=="no"){
			str += this.table[i]["key"] + split1 + val + split2;
		}else{
			str += this.table[i]["key"] + split1 + encodeURIComponent(val) + split2;
		}
	}
	if(str!=""){
		str = str.substr(0,str.length-split2.length);	
	}
	return str;	
};

__OAUTH20__.prototype.Set = function(key,value,isdefault){
	if(!value){value="";}
	if(!isdefault){isdefault=false;};
	if(this.table.length==0){
		this.table[0]={"key":key,"value":value,"isdefault":isdefault};
	}else{
		var isIn = false;
		for(var i=0;i<this.table.length;i++){
			if(this.table[i]["key"].toLowerCase()==key.toLowerCase()){
				this.table[i]["value"]= value;
				this.table[i]["isdefault"]= isdefault;
				isIn = true;
				break;
			}
		}	
		if(!isIn){
			this.table.push({"key":key,"value":value,"isdefault":isdefault});	
		}
	}
};

__OAUTH20__.prototype.Add = function(key,value){
	if(!value){value="";}
	if(this.table.length==0){
		this.table[0]={"key":key,"value":value};
	}else{
		var isIn = false;
		for(var i=0;i<this.table.length;i++){
			if(this.table[i]["key"].toLowerCase()==key.toLowerCase()){
				if(this.table[i]["value"]!=""){
					this.table[i]["value"]= this.table[i]["value"] + ", " + value;
				}else{
					this.table[i]["value"]= value;
				}
				isIn = true;
				break;
			}
		}	
		if(!isIn){
			this.table.push({"key":key,"value":value});	
		}
	}
};

__OAUTH20__.prototype.Count = function(){return this.table.length;};

__OAUTH20__.prototype.Parm = function(key){
	if(this.Count()<=0){return "";}
	for(var i=0;i<this.Count();i++){
		if(this.table[i]["key"].toLowerCase()==key.toLowerCase()){
			return this.table[i]["value"];
		}	
	}
	return "";
};

__OAUTH20__.prototype.Remove = function(key){
	if(this.Count()<=0){return;}
	if(!key){this.table=[];return;}
	//key = key.toLowerCase();
	var ___temp=[];
	for(var i=0;i<this.Count();i++){
		if(this.table[i]["key"].toLowerCase()!=key.toLowerCase()){
			___temp.push(this.table[i]);
		}	
	}
	this.table=___temp;
};
__OAUTH20__.prototype._rndstr=function(len){
	var slen = "0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM";
	var retstr="";
	for(var i=0;i<len;i++){
		retstr+=slen.substr(parseInt(Math.random()*slen.length),1);
	}
	return retstr;
};
__OAUTH20__.prototype.utf8 = function(str,lcase){
	if(lcase){return encodeURIComponent(str).toLowerCase();}else{
	return encodeURIComponent(str);}	
};

__OAUTH20__.prototype.utc = function(){
	var dat = new Date();
	return Date.UTC(dat.getFullYear(),dat.getMonth(),dat.getDate(),dat.getHours(),dat.getMinutes(),dat.getSeconds());
};
__OAUTH20__.prototype.GMT = function(){
	return utc()/1000 - 8 * 3600;
};
__OAUTH20__.prototype.rndstr = __OAUTH20__.prototype._rndstr;
function callback(v){
	v['mo__']=true;
	return v;
}
</script>