/**
 * 基于prototype的ajax.request类远端调用服务
 * 必须包含json.js文件，参数（调用文件地址，传递参数，成功时调用函数，正在请求时调用函数，是否使用json解析返回的结果）
 */ 

Ajax.Service = Class.create();
Ajax.Service.prototype = {
  _defaultMethod: 'get',//方式 2007-9-12 ziming edit "post"->"get"
  asynchronous: true,//是否异步
  initialize: function(){},
  
  Call : function(RemoteUrl, Para, CallBack, LoadingCallBack, pj)
  {
    if ( pj ) EvalResp = true;
    else EvalResp = false;
    options = 
	{
      method: this._defaultMethod,
      asynchronous: this.asynchronous,
      parameters: Para 	  
    }    
        
    if ( EvalResp )
	{
        new Ajax.Request(RemoteUrl, {method:options.method, asynchronous:options.asynchronous, parameters:options.parameters,
		onLoading: LoadingCallBack,
        onSuccess: (function(Req){this.JSONEval(Req, CallBack)}).bind(this)});
    }
    else 
	{
      new Ajax.Request(RemoteUrl, {method:options.method, asynchronous:options.asynchronous, parameters:options.parameters,onLoading: LoadingCallBack, onSuccess: (function(Req){CallBack(Req.responseText)}).bind(this)});
	}				
  },
  
  Call2 : function(RemoteUrl)
  {
    if ( pj ) EvalResp = arguments[4];
    else EvalResp = false;    
    options = 
	{
      method: 'get',
      asynchronous: this.asynchronous
   	}        
   	new Ajax.Request(RemoteUrl, {method: options.method, asynchronous:options.asynchronous});				
  },
	  
  JSONEval : function(Req, CallBack)
  {	
    if (Req.responseText == '')
	{
		CallBack(false);
		return;	
	}		
	var RespValid = true;    
    try
	{
      var res = eval('('+Req.responseText+')');
//      var res = Req.responseText.parseJSON();
    }
    catch(e)
	{
	  alert('加载内容失败，请刷新页面重试。');
      RespValid = false;
    }
    if (RespValid) CallBack(res);
  }

}
