// JavaScript Document

function Snoopy() {
	
	this.req = null;
	this.url = null;
	this.method = 'GET';
	this.async = true;
	this.status = null;
	this.statusText = '';
	this.postData = null;
	this.readyState = null;
	this.responseText = null;
	this.responseXML = null;
	this.handleResp = null;
	this.responseFormat = 'text'; // 'text', 'xml', or 'object'
	this.mimeType = null;
	
	this.init = function() {
	
		if (!this.req) {
		
			try {
				//ff, safari, ie7
				this.req = new XMLHttpRequest();
			}
			catch (e) {
				try {
					//late ie
					this.req = new ActiveXObject('MSXML2.XMLHTTP');
				}
				catch(e) {
					try {
						//early ie
						this.req = new ActiveXObject('Microsoft.XMLHTTP');
					}
					catch(e) {
						//could not create object
						return false;
					}
				}
			}
		}
		
		return this.req;
	};

	this.doReq = function() {
		
		if (!this.init()) {	
			alert('Could not create XMLHttpRequest Object');
			return;
		}
		
		this.req.open(this.method, this.url, this.async);
		
		if (this.method = 'POST') {
			this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');	
		}
		
		if (this.mimeType) {
		
			try {
				req.overrideMimeType(this.mimeType);
			}
			catch (e) {
				//couldnt override mime type -- IE6 and Opera	
				alert('Could not override MIME Type.');
			}
		}
		
		var self = this; //fix loss of scope in inner function
		
		self.req.onreadystatechange = function() {
			var resp = null;
			if (self.req.readyState == 4) {
				//handle response	
				switch (self.responseFormat) {
					case 'text':
						resp = self.req.responseText;
						break;
					case 'xml':
						resp = self.req.responseXML;
						break;
					case 'object':
						resp = req;
						break;
				}
				
				if (self.req.status >= 200 && self.req.status <= 299) {
					self.handleResp(resp);	
				}
				else {
					self.handleErr(resp); 
				}
			}
			else {
				resp = "Loading...";
				self.handleResp(resp);
			}
		};
		
		this.req.send(this.postData);
	};
	
	this.setMimeType = function(mimeType) {
		this.mimeType = mimeType;	
	}
	
	this.handleErr = function() {
		//var errorWin;
		try {
			alert('An error occurred. \n Status Code: ' + this.req.status + '\nStatus Description: ' + this.req.statusText);	
			//errorWin = window.open('', 'errorWin');
			//errorWin.document.body.innerHTML = this.responseText;
		}
		catch(e) {
			alert('An error occurred but your pop-up blocker blocked it. \n Status Code: ' + this.req.status + '\nStatus Description: ' + this.req.statusText);	
		}
	};
	
	this.abort = function() {
		if (this.req) {
			this.req.onreadystatechange = function() { };
			this.req.abort;
			this.req = null;
		}
	};
	
	this.doGet = function(url, hand, format) {
		
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.doReq();
		
	};
	
	this.doPost = function(url, postData, hand, format) {
		
		this.url = url;
		this.handleResp = hand;
		this.responseFormat = format || 'text';
		this.method = 'POST';
		this.postData = postData;
		this.doReq();
		
	};

}




