/*	/include/js/ajax.js

	This file is Copyright (C) 2008-2010 Kevin H. Patterson and Hartland Institute. mailto:kpatterson@hartland.edu
	Use requires a valid license or written permission.
*/
function AjaxHTTPObj() {
	var http = false;
	
	if( window.XMLHttpRequest ) // if Mozilla, Safari etc
		http = new XMLHttpRequest();
	else if( window.ActiveXObject ) { // if IE
		try {
			http = new ActiveXObject( "Msxml2.XMLHTTP" );
		} catch( e ) {
			try {
				http = new ActiveXObject( "Microsoft.XMLHTTP" );
			} catch( e ) {}
		}
	}

	return http;
}

function ElementInsertXML( uri, element ) {
	http = AjaxHTTPObj();
	if( http ) {
		http.onreadystatechange = function() {
			if( http.readyState == 4 && http.status == 200 ) {
				element.innerHTML = http.responseText;
			}
		}
		http.open( 'GET', uri, true );
		http.send( null );
	}
}

function InsertXML( uri, id ) {
	ElementInsertXML( uri, document.getElementById( id ) );
}

function AjaxGet( uri, text ) {
	http = AjaxHTTPObj();
	if( http ) {
		if( text ) uri = uri + '?' + text;
		http.open( 'GET', uri, true );
		
		//Call a function when the state changes.
		http.onreadystatechange = function() {
			if( http.readyState == 4 && http.status == 200 ) {
				var result = http.responseText;
				if( result ) alert( result );
			}
		}
		http.send( null );
	}
}

function AjaxPost( uri, text ) {
	http = AjaxHTTPObj();
	if( http ) {
		http.open( 'POST', uri, true );
		
		//Send the proper header information along with the request
		http.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );
		http.setRequestHeader( "Content-length", text.length );
		http.setRequestHeader( "Connection", "close" );
		
		//Call a function when the state changes.
		http.onreadystatechange = function() {
			if( http.readyState == 4 && http.status == 200 ) {
				var result = http.responseText;
				if( result ) alert( result );
			}
		}
		http.send( text );
	}
}

