﻿function HttpClient() { }
HttpClient.prototype = {
   // type GET, POST passed to open
   responseXML: false,
   requestType:'GET',
   // when set to true async calls are made 
   isAsync: false,

   // where an XMLHttpRequest instance is stored
   xmlhttp: false,
   
   // what is called when a successful async call is made
   callback: false,

   // what is called when send is called on XMLHttpRequest
   // set your own function to onSend to have a custom loading effect
   onSend:function() {
       //OBSOLETE document.getElementById('HttpClientStatus').style.display = 'block';
	},
	
	// what is called when readyState 4 is reached, this is called before your callback
	onload:function() {
	  //OBSOLETE document.getElementById('HttpClientStatus').style.display = 'none';
	},
	
	// what is called when an http error happens
	onError: function(error) {
	   alert(error);
	},
	
	// method to initialize an xmlhttpclient
	init:function() { 

	try {
	   this.xmlhttp = new XMLHttpRequest();
	} catch (e) {
	    var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','Microsoft.XMLHTTP');
		var success = false;
		for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
		   try {
			  this.xmlhttp = new ActiveXobject(XMLHTTP_IDS[i]);
			   success = true;
			} catch (e) {}
		}
		if (!success) {
			 throw new Error('Unable to create HMLHttpRequest.');
			}
	 }
},

// method to make a page request
// @param string url The page to make the request o
// @param string payload What you're sending if this is a POST request
	makeRequest: function(url, payload) {
		if (!this.xmlhttp) {
			this.init();
			
		}
		try {		
		this.xmlhttp.open(this.requestType, url, this.isAsync);
		} catch (e) {
			alert(e);}
		
		//set onreadystatechange here since it will be reset after a completed call in Mozilla
		var self = this;
		this.xmlhttp.onreadystatechange = function() {
		   self._readyStateChangeCallback(); }
		if (this.responseXML) {
		 try {
		 this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		 } catch (e) {
		    alert('ouch');
		    }
        }   
		   
		
		this.xmlhttp.send(payload);
		
		if (!this.isAsync) {
		   if (!responseXML) {
		     return this.xmlhttp.responseText;
		   }
		   return this.xmlhttp.responseXML;
		}
		
	},

  // internal method used to handle ready state changes
  _readyStateChangeCallback:function() {
     switch(this.xmlhttp.readyState) {
    case 2:
	  this.onSend();
	  break;
	case 4:
	  this.onload();
	    if (this.xmlhttp.status == 200) {
	       if (this.responseXML) {
	          this.callback(this.xmlhttp.responseXML);
	           } else {
	              this.callback(this.xmlhttp.responseText);
	       }
	  } else {
	       tempstr = 'HTTP Error Making Request: [' + this.xmlhttp.status + ']' + this.xmlhttp.statusText;
	       this.onError(tempstr);
	  }
	  break;
	}
  }
}// JScript File


