/*!
 * bluprintsmedia ajax framework
 * http://www.bluprintsmedia.net
 *
 * Copyright 2010, Micah Blu
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Copyright 2010, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 Timeline:
 
 Thurs September 30th 10:24am 2010 - 0.9
 
 	- added callback paramater to ajaxGet method 	
 	
 Friday November 12th 3:11pm 2010 - 1.0
 
 	- encapsulated all methods into a singleton class
	
	get method: ([url "string"], [data "object"], [callback "object" or "function"])
		-the get method's callback parameter can be either an object or function, the object
		may want to be used to pass data to be used by the callback method within that object
		or if multiple methods are needed to be run.
 */

var bpm_ajax = {
	
	request : null,
	
	callback : {},
	
	createRequest : function() {
	  try {
			this.request = new XMLHttpRequest();
			if (this.request.overrideMimeType) {
				this.request.overrideMimeType('text/xml');
			}
			if (!this.request) {
				alert('Cannot create XMLHTTP instance');
				return false;
			}
		} catch (trymicrosoft) {
		  try {
			   this.request = new ActiveXObject("msxml2.XMLHTTP");
		} catch (othermicrosoft) {
		  try {
			   this.request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (failed) {
			   this.request = null;
			 }
		   }
		}
		if (this.request == null){
		  alert("Error creating request object!");
		}
	}, //ends createRequest function
	
	send : function(url, data, callback){
		/*
		This is the ajax invoking method
		*/
		var self = this;
		self.createRequest();
		
		self.data = data;
		self.callback = callback;
		
		//prepare url with data appended from obj
		url = url + "?" + this.makeQueryString(data);	
				
		self.request.open("GET", url, true);
		
		self.request.onreadystatechange = function(){ self.checkin(); };
		
		self.request.send(null);
	},

	checkin : function(){
		
		if (this.request.readyState == 4) { //we're ready to return response
				
			var response = this.request.responseText;
			
			if(typeof this.callback == 'object'){
				//look for a callback function in this obj
				for(prop in this.callback){
										
					if(typeof this.callback[prop] =='function'){
						
						this.callback[prop](response); //a function has been detected in the callback object	
					}
				}
			}
			else if(typeof this.callback == 'function'){
				this.callback(data);
			}
		}
		
		return false;
	},

	makeQueryString : function(obj){
		
		var query = [];
		
		for(var key in obj){
			
			if(typeof (obj[key]) == "string" || typeof (obj[key]) == "number"){
			
				query.push(key + "=" + encodeURIComponent(obj[key]));
			
			}
															 
		}
	
		return query.join('&');
	
	},

	processPosChange : function() {
		//page loaded "complete"
		if (pos.readyState == 4) {
			// page is "OK"
			if (pos.status == 200){
				if(grabPosXML("posStatus") == 'NOTOK') { 
					alert('There were problems Sending Email. Please check back in a couple minutes');
				}
			}
		}
		return false;
	}
}
