﻿/* 
 * myCall Web Remoting, An XMLHttp infrastructure facility, a web remoting library.
 * 
 * Author: toofu (Tc soft, http://www.interlib.com.cn)
 * 
 * Version: 1.0
 * charset=utf-8
 */

/** 
 * @fileoverview MyCall.js is the core component of the MyCall web remoting.
 * This file encapuslate the MyCall.
 *
 * @author toofu toofu@tom.com
 * @version 1.0
 */

/**
*init MyCall
*eg. 
*	var mycall_1=new MyCall("Biblios?")
* var mycall_2=new MyCall("Biblios?","GET")
*/function MyCall(querypath,methodname){
	//default post
	if(typeof(methodname)=="undefined")
		methodname="POST";
	//if input a error method. default POST
	if(!(methodname.toUpperCase()=="GET"||methodname.toUpperCase()=="POST")){
		methodname="POST";
	}
	this.method = methodname;
	this.query=querypath;
	this.params = [];
	this.isTongBu=false;
	this.tongBuFun=null;
	this.xmlhttp =getXmlHttpObj();
	return this;
}
/**
*set Callmethod.
*eg. POST GET
*/
MyCall.prototype.setMethod = function(methodName){
  if (!methodName) return;
  this.method = methodName;
}
/**
*set call path
*eg. ../order/Biblios
*/
MyCall.prototype.setPath = function(querypath){
  if (!querypath) return;
  this.query = ququerypath
}

MyCall.prototype.setTongBu = function(_isTongBu){
	this.isTongBu=_isTongBu;
}
MyCall.prototype.setTongBuFun = function(_tongBuFun){
	this.tongBuFun=_tongBuFun;
}
MyCall.prototype.getHttpObj = function(){
	return this.xmlhttp;
}
/**
* add on parameter translate to Server
*eg. cmdACT=val 
* mycall.add("cmdACT","val");
*/
MyCall.prototype.addParameter = function(paraName,paraValue){
	var tmpObject=new Object();
	tmpObject.key=paraName;
	tmpObject.value=paraValue;
  if (paraName.length==0) return;
  this.params[this.params.length] = tmpObject;
}


function getXmlHttpObj(){
	

   
    

	//return (new XMLHttpRequest());
	//return (new ActiveXObject("Microsoft.XMLHTTP"));
	/*
	if(window.XMLHttpRequest){
		return (new XMLHttpRequest());
	}else if(window.ActiveXObject){
		return (new ActiveXObject("Microsoft.XMLHTTP"));
	}else{
		return null;
	}
	 */
	
                try {
				  return new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
                  try {
					 return new XMLHttpRequest();
				  } catch(e) {
					 return null;
					 
				  }
				}
			

	 
}


/**
* get Remote Access .and return the result to user .When Can't get Result .return "";
*eg. 
* getResultType=TEXT   
*			TEXT:get The Text result
*			DOM:get The XML DOM result
*			XML:get The XML String
*			Other:get on XML DOM node .eg  MESSAGE
*
*/
MyCall.prototype.getResult=function(getResultType)
{
	var reqMethod=this.method;
	var href_str=this.query;
	var sSendVar=this.params;
	if (navigator.onLine==false){
		alert("您没有连接到网络!");
		return "";
  	}
  	if (!this.xmlhttp){
		alert("无法建立XMLHTTP请求,请将您的浏览器级别降低!");
		return "";
  	}
  try{
  	if(this.isTongBu&&this.tongBuFun){
  		this.xmlhttp.onreadystatechange = this.tongBuFun;
  	}
	//alert(this.isTongBu);
  	this.xmlhttp.open(reqMethod,href_str,this.isTongBu);
  	this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  	var sendStr="";
  	if(typeof(sSendVar)=="object"){
		   for(var i=0;i<sSendVar.length;i++){
		   	sendStr=sendStr+((i==0)?"":"&")+encodeURIComponent(sSendVar[i].key)+"="+encodeURIComponent(sSendVar[i].value);
		   }
		}
		this.xmlhttp.Send(sendStr);
	}catch (exception){
		alert("访问服务器失败,请检查网络配置情况!!");
		return "";
	}
	
	if(this.isTongBu){
		return;
	}
	
	var returnStr="";
  try{
  	
  	if(getResultType.toUpperCase()=="TEXT"){
			returnStr=this.xmlhttp.responseText;
		}else if(getResultType.toUpperCase()=="DOM"){
			returnStr=this.xmlhttp.responseXML;
		}else if(getResultType.toUpperCase()=="XML"){
			returnStr=this.xmlhttp.responseXML.xml;
		}else{
			returnStr=this.xmlhttp.responseXML.getElementsByTagName(getResultType)(0).text;
		}
	}catch (exception){
		//alert("处理服务器结果错误!");
	}
	return returnStr;
}