/*
	<script type='text/javascript' src='/common/js/xmlttprequest.js'></script>

************************************
	SAMPLE: GET
	
	var xml = getXmlHttpObject();
	xml.open( "GET", "/petdisplay.php?petid="+thePet );
	xml.onreadystatechange = function() {
		if ( xml.readyState == 4 ) {
			var pet = document.getElementById( 'petdisplay' );
			pet.innerHTML = xml.responseText; 
			
			assignVars();
			setName();
		}
	}
	xml.send(null);

************************************
	
	SAMPLE: POST
	
	var xml = getXmlHttpObject();
	var url = "ajax/leavecomment.php";

	// encodeURIComponent to include special characters, and not just encodeURI
	var params = "petid="	+ encodeURIComponent( petid ) +
			"&comment="		+ encodeURIComponent( comment );
			
	xml.open("POST", url, true);
	
	//Send the proper header information along with the request
	xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xml.setRequestHeader("Content-length", params.length );
	xml.setRequestHeader("Connection", "close");
	
	xml.onreadystatechange = function() {
		if ( xml.readyState == 4 ) {
			document.getElementById( 'leavecomment' ).disabled = true;;
			document.getElementById('commentButton').value = "Posted";
			
		}
	}
	xml.send(params);
	

************************************

	SAMPLE: ajax
	// 2.3.2010 surrounded the success/unsuccess with try/catch blocks, so code is significantly reduced for error reporting
	
	ajax( "/galacticconquest/multiplayer/gamesList.php", 
		function(code,text) {
			serverGamesList.readString( text );
			fillMyGamesList( text );
		} );

*/

// pass the url, success function, failure function(optional) and params(optional)
function ajax(url, onSuccess, onFailure, params) {
	if (typeof params == 'undefined') params = null;
	var method = (params ? 'POST' : 'GET');
	
	var req = new XMLHttpRequest();
	req.open(method, url, true);
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			try {
				if (req.status == 200) {
					onSuccess(req.status,req.responseText);
				} else {
					if ( onFailure ) {
						onFailure(req.status,req.responseText);
					} else {
						alert( "ERROR: " + req.status + " " + req.responseText );
					}
				}
			} catch ( e ) {
				alert( "ERROR: " + e + " " + req.responseText );
			}
		}
	};
	
	if (params) {
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", params.length );
	}
	//alert( params );
	req.send(params);
}

	
// return xml http object
function getXmlHttpObject() {

	var xmlHttp = null;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();

	} catch (e) {
		
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				//alert("Your browser does not support AJAX!");
				//xmlHttp = null; // already is null
			}
		}
	}
	
	return xmlHttp;
}

