

function ServerRequest() {
  this.Init();
}

ServerRequest.prototype.Init = function() {
  this.XmlHttp = this.GetHttpObject();
}

ServerRequest.prototype.TimerID = 0;
ServerRequest.prototype.TimeOutInterval = 20000;

ServerRequest.prototype.GetHttpObject = function() { 
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else @*/
  xmlhttp = false;
  /*@end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

ServerRequest.prototype.AbortCallBack = function() {
    if (this.TimerID != 0 )
	    window.clearTimeout(this.TimerID);
	if( this.XmlHttp )
		this.XmlHttp.abort();
}
 
ServerRequest.prototype.OnLoading = function() {
  // Loading
}
 
ServerRequest.prototype.OnLoaded = function() {
  // Loaded
}
 
ServerRequest.prototype.OnInteractive = function() {
  // Interactive
}
 
ServerRequest.prototype.OnComplete = function(serverResponse) {
  // Complete
}
 
ServerRequest.prototype.OnAbort = function() {
  // Abort
}
 
ServerRequest.prototype.OnError = function(status, statusText, responseText) {
  // Error
}

ServerRequest.prototype.TimeOut = function() {
  return this.OnError(0,"Connection Timed Out","");
}
 
ServerRequest.prototype.ReadyStateChange = function() {
  if( this.XmlHttp.readyState == 1 )
  {
    this.OnLoading();
  }
  else if( this.XmlHttp.readyState == 2 )
  {
    this.OnLoaded();
  }
  else if( this.XmlHttp.readyState == 3 )
  {
    this.OnInteractive();
  }
  else if( this.XmlHttp.readyState == 4 )
  {
    window.clearInterval(this.TimerID);
    if( this.XmlHttp.status == 0 )
      this.OnAbort();
    else if( this.XmlHttp.status == 200 && this.XmlHttp.statusText == "OK" )
      this.OnComplete(new ServerResponse(this.XmlHttp));
    else
      this.OnError(this.XmlHttp.status, this.XmlHttp.statusText, this.XmlHttp.responseText);   
  }
}

ServerRequest.prototype.SendGetRequest = function(theUrl){

    this.AbortCallBack();
	if( this.XmlHttp )
	{		
		if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
		{
			var oThis = this;
			
            this.TimerID = window.setInterval(function(){ oThis.TimeOut(); }, this.TimeOutInterval);	
            
			this.XmlHttp.open('GET', theUrl, true);
			this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); };
			this.XmlHttp.send(null);
		}
        else
        {
            this.OnError(-2,"Request already in progress","");
        }
	}
    else
    {
        this.OnError(-1,"Not Supported","");
    }

}

ServerRequest.prototype.SendPostRequest = function(url,postData){

    this.AbortCallBack();
	
	if( this.XmlHttp )
	{		
		if( this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0 )
		{
			var oThis = this;
			var strData = '';
            
            if( isString(postData) )
                strData = postData;
            else {
                var key;
                for (key in postData)
                {
                    strData += '&' + this.EncodePostData(key) + '=' + this.EncodePostData(postData[key]);
                }
                strData = strData.substring(1,strData.length);
            }
            
            this.TimerID = window.setInterval(function(){ oThis.TimeOut(); }, this.TimeOutInterval);	
            
			this.XmlHttp.open('POST', url, true);
			this.XmlHttp.onreadystatechange = function(){ oThis.ReadyStateChange(); };
			this.XmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            this.XmlHttp.setRequestHeader('Content-length', strData.length );
			this.XmlHttp.send(strData);
		}
        else
        {
            this.OnError(-2,"Request already in progress","");
        }
	}
    else
    {
        this.OnError(-1,"Not Supported","");
    }
}

ServerRequest.prototype.EncodePostData = function(data)
{
	return data.split("%").join("%25").split("=").join("%3d").split("&").join("%26").split("+").join("%2b");
}


// ServerResponse Object

function ServerResponse(xmlHttp) {
  this.ResponseXML =  xmlHttp.responseXML;
  this.ResponseText =  xmlHttp.responseText;
}

ServerResponse.prototype.Xml = function () {
    return this.ResponseXML;
}

ServerResponse.prototype.Text = function () {
    return this.ResponseText;
}

ServerResponse.prototype.RunScript = function () {
    return eval(this.ResponseText);
}

ServerResponse.prototype.parseJSON = function () {
    /*try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.ResponseText.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this.ResponseText + ')');
    } catch (e) {
        return false;
    }
    */
    try {
        return eval('(' + this.ResponseText + ')');
    }catch (e) {
        return false;
    }
};


function isArray(a) {
    return (a && typeof a == 'object' && a.constructor == Array);
}

function isString(a) {
    return typeof a == 'string';
}


