/**********************************************
*	Scott Ladyman 2006
*
***********************************************/
/* needs dom.js library */
var ajax = {
	
	delay 			: 0,
	loadingTarget	: false,	/* simple ajax class self loading - passed obj */

	
	init : function() {
		
		var xmlhttp = false; //Clear our fetching variable
			try {
				xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
			} catch (e) {
				try {
					xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
				} catch (e) {
					xmlhttp = false;
				}
			}
			if( !xmlhttp && typeof XMLHttpRequest != 'undefined') {
				xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
			}
		
		
		return xmlhttp;	
	},
		
	request : function(url, callback, params) {
				
		// create the handle for the XMLHttpRequest
		ajaxRequest = this.init();
		
		// open the request, based on the form method type
		ajaxRequest.open("GET" , url, true); 
		
		/* when the server request is completed */
		ajaxRequest.onreadystatechange = function() {
			
			/* four main states/state codes, Loading-1, Loaded-2, Interactive-3, and Complete-4. */
			if( ajaxRequest.readyState == 1 ){
				// loading
				ajax.customLoading(true);
			}
			if ( ajaxRequest.readyState == 4 ) { 
				 /* Upon successful HTTP request 200 = OK  */
				if ( ajaxRequest.status == 200 ) {
					/* delay is only needed for testing */
					setTimeout(function(e){
							//	check to see if callback function needs params						
							if(!params){
								callback(ajaxRequest);
							}else{
								callback(ajaxRequest, params);
							}

						}, ajax.delay);
					
					//	turn off custom loading if setup to do so.						
					ajax.customLoading(false);
				}  
				else {
					alert("Error contacting the server :: " + ajaxRequest.status);
					
				}
			}
		}
				
		// send the request
		ajaxRequest.send(null);
	},
	
	
	/* ********************************************** 
		easiest way to decode json for javascript
	********************************************** */
	decodeJson : function( string )
	{
		return eval( '('+ string +')' );
	},
	
	
	/* ********************************************** 
	dynamically loads the javascript into the browser 
	********************************************** */
	loadDynamicJS : function(content, file)
	{
		//find and add to head tag
		var head   = dom.byTag("head")[0];
		var script = dom.create("script");
		
		script.type   = 'text/javascript';
		script.source = file;		
		//	text is used because IE doesn't understand to load the js with innerHTML
		if(document.all){
			script.text = content;
		}else{
			script.innerHTML = content;
		}
		head.appendChild(script);		
	},
	

	/* ********************************************** 
		simple custom loading on each ajax request
	********************************************** */
	customLoading : function( showLoading )
	{
		if( ajax.loadingTarget != false ){
			
			var al = dom.byId(ajax.loadingTarget);
			if( showLoading ){
				//	custom ajax loading
				al.className = "on";		
			}else{
				setTimeout(function(e){al.className = "";}, 1000);
			}
		}
	}


};

	 