﻿/*  AjaxClass JavaScript, version 1.0.0
 *  (c) 2007 Shadowzhang <shadow.zhang@unisono.com>
 *  url is a *.do such as  "/**Action.do"  OR   "/**Action.do?command=get"
 *  paras is  the parameters such as  "command=get&cityId=1&channelId=2" OR "cityId=1&channelId=2"
 *  functionName is the feedback functionName after get data
 *  
 *
/*--------------------------------------------------------------------------*/
var AjaxClass = Class.create();

AjaxClass.prototype = {
	initialize: function(objName,url,paras,functionName) {
		this.objName =objName;
		this.url = url;	
		this.paras = paras;	
		this.functionName = functionName;	
		this.delayTime=0;
		this.timeoutObj=null;	   
	},	
	setUrl:function(url){
		this.url = url;	
	},
	setParas:function(paras){
		this.paras = paras;		
	},
	setDelayTime:function(delayTime){
		this.delayTime = delayTime;
	},
	setFunctionName:function(functionName){
		this.functionName = functionName;	
	},
	showAjaxFailMessage:function(){
		alert("A network error has occurred.Please,first check your network connection and then login again after network error has been resolved.");
						
	},
	getRequestedUrl: function(){
		 if(this.url.indexOf('?')>-1){
		 	 return this.url+'&'+this.paras;
		 }else{
		   return this.url+'?'+this.paras;
		 }
	},		
	getData:function(){
			var ajax = new Ajax.Request(		
		  this.url, 
			{
				method: 'post', 
				parameters: this.paras, 
				requestHeaders:['If-Modified-Since','0'],
				onComplete: eval(this.functionName).bind(this),
				onFailure: this.showAjaxFailMessage
			});			
	},
	doRequest:function(){		
		var delayTimeMs = this.delayTime;
		if(delayTimeMs*1.0 > 0){
			clearTimeout(this.timeoutObj);			
 			this.timeoutObj = setTimeout(this.objName+".getData()",this.delayTime); 			
		}else{
		  this.getData();
		}		
	}
 	
};

