/*Description: Ajax Util
*Author hanpoyangtitan
*Date 2008-8-8
*/
//
//Ajax asynchronized method
/**
* AJAX工具
* @param method 提交方式 "GET/get","POST/post"
* @param url 访问地址
* @param param 参数{id:"1",name:"2"}
* @param retType 返回值类型 xmlhttp/text/json/xml/binary/cn
* @param fun 回调对象 {Success:a,Error:b,Process:c}
* @param timeout 请求超时
* @param async 是否异步提交 true/false
* @return
*/
function Ajax(method,url,param,retType,fun,async){
this.xhr=null;
this.method=method;
this.url=url;
this.param=param;
this.retType=retType.toLowerCase();
this.fun_success=null;
this.fun_error=null;
if(fun['Success']!=null){
this.fun_success=fun.Success;
}
if(fun['Error']!=null){
this.fun_error=fun.Error;
}
if(fun['Process']!=null){
this.fun_process=fun.Process;
}
this.async=async
this.init();
if(this.method.toLowerCase()=="get"){
this.get();
}else if(this.method.toLowerCase()=="post"){
this.post();
}
}
Ajax.prototype.init=function(){
if(this.xhr==null){
if(window.ActiveXObject){
try{
this.xhr=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
this.xhr=new ActiveXObject("MSXML2.XMLHttp");
}
}else{
this.xhr=new XMLHttpRequest();
if(this.xhr.overrideMimeType){
this.xhr.overrideMimeType("text/xml");
}
}
}
}
Ajax.prototype.get=function(){
if(this.xhr==null)return;
var param="";
for(var item in this.param){
param+=item+"="+this.param[item]+"&";
}
if(param.length>0 && this.url.indexOf("?") < 0){
param="?"+param.substring(0,param.length-1);
}else if(param.length > 0 && this.url.indexOf("?") >= 0){
param="&"+param.substring(0,param.length-1);
}
this.xhr.open("get",this.url+param,this.async);
obj=this;
this.xhr.onreadystatechange=function(){
if(obj.xhr.readystate==4){
if(obj.xhr.status==200){
if(obj.retType=="xmlhttp"){
obj.fun_success(obj.xhr);
}else if(obj.retType=="text"){
obj.fun_success(obj.xhr.responseText);
}else if(obj.retType=="xml"){
obj.fun_success(obj.xhr.responseXML);
}else if(obj.retType=="json"){
var jsonObj=eval(obj.xhr.responseText);
obj.fun_success(jsonObj);
}else if(obj.retType=="binary"){
var bin=obj.xhr.responseBody;
obj.fun_success(bin);
}else if(obj.retType=="cn"){
var bin=obj.xhr.responseBody;
var html=byte2str(bin);
obj.fun_success(html);
}
}else{
if(typeof fun_error =="function"){
obj.fun_error(obj.xhr);
}
}
}else{
if(typeof(fun_process)=="function"){
obj.fun_process(obj.xhr);
}
}
}
this.xhr.send(null);
}
Ajax.prototype.post=function(){
if(this.xhr==null)return;
var param="";
for(var item in this.param){
param+=item+"="+this.param[item]+"&";
}
if(param.length>0)param=param.substring(0,param.length-1);
this.xhr.open("post",this.url,this.async);
obj=this;
this.xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
this.xhr.onreadystatechange=function(){
if(obj.xhr.readystate==4){
if(obj.xhr.status==200){
if(obj.retType=="xmlhttp"){
obj.fun_success(obj.xhr);
}else if(obj.retType=="text"){
obj.fun_success(obj.xhr.responseText);
}else if(obj.retType=="xml"){
obj.fun_success(obj.xhr.responseXML);
}else if(obj.retType=="json"){
var jsonObj=eval(obj.xhr.responseText);
obj.fun_success(jsonObj);
}else if(obj.retType=="binary"){
var bin=obj.xhr.responseBody;
obj.fun_success(bin);
}else if(obj.retType=="cn"){
var bin=obj.xhr.responseBody;
var html=byte2str(bin);
obj.fun_success(html);
}
}else{
if(typeof fun_error =="function"){
obj.fun_error(obj.xhr);
}
}
}else{
if(typeof(fun_process)=="function"){
obj.fun_process(obj.xhr);
}
}
}
this.xhr.send(param);
}
//Util function
String.prototype.trim=function(){
return this.replace(/^/s+|/s+$/g,"");
}
//parse binary to utf-8
function byte2str(data){
var glbEncode = [];
gb2utf8_data = data;
execScript("gb2utf8_data = MidB(gb2utf8_data, 1)", "VBScript");
var t=escape(gb2utf8_data).replace(/%u/g,"").replace(/(.{2})(.{2})/g,"%$2%$1").replace(/%([A-Z].)%(.{2})/g,"@$1$2");
t=t.split("@");
var i=0,j=t.length,k;
while(++i<j) {
k=t[i].substring(0,4);
if(!glbEncode[k]) {
gb2utf8_char = eval("0x"+k);
execScript("gb2utf8_char = Chr(gb2utf8_char)", "VBScript");
glbEncode[k]=escape(gb2utf8_char).substring(1,6);
}
t[i]=glbEncode[k]+t[i].substring(4);
}
gb2utf8_data = gb2utf8_char = null;
return unescape(t.join("%"));
}