/*
	################################################################
	# > This is ajaxRequest Module V2										#
	# >> part of 																#
	# >>> Gallo-belgic Pottery Database			 						#
	################################################################
	# > Authors:  Lucian Pricop											#
	# > E-mail:  lucian.gabriel.pricop@gmail.com						#
	# > Date: 	 23 May 2007												#
	#																			#
	################################################################
	#	Copyright (C) 2007  Oxford Archaeology							#
	#																			#
	# This program is free software; you can redistribute 			#
	# it and/or modify it under the terms of the GNU General 		#
	# Public License as published by the Free Software 				#
	# Foundation; either version 2 of the License, or (at your	 	#
	# option) any later version.											#
	#																			#
	# This program is distributed in the hope that it will be 		#
	# useful, but WITHOUT ANY WARRANTY; without even the 				#
	# implied warranty of MERCHANTABILITY or FITNESS FOR A 			#
	# PARTICULAR PURPOSE.  See the GNU General Public License 		#
	# for more details.														#
	#																			#
	# You should have received a copy of the GNU General 				#
	# Public License along with this program; if not, write to 		#
	# the Free Software Foundation, Inc., 59 Temple Place - 			#
	# Suite 330, Boston, MA  02111-1307, USA.							#
	################################################################
*/

//NOTE: I need an implicit timeout function launched at the same time with the request  
function ajaxSendXMLtoPHP(url, message, callback,obj){

	function ajaxBindCallback(){
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				if (ajaxCallback){
					ajaxCallback(obj,ajaxRequest.responseXML);
				} else {
					alert('no callback defined');
				}
			} else {
				alert("There was a problem retrieving the xml data:\n"
				+ ajaxRequest.status + ":\t" + ajaxRequest.statusText 
				+ "\n" + ajaxRequest.responseText);
			}
			if(body !=null)
				body.removeChild(loadingLabel);
		}
	}

	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	var ajaxCallback = callback;
	var obj = obj; //this is an object to send to the callbakc function
	var body = null;
	var loadingStyle = "position: fixed; right: 2px; top: 2px; color: #fff; background-color: #c06;padding: 1px 6px";
	if(window.navigator.userAgent.indexOf("MSIE 5")!=-1 || window.navigator.userAgent.indexOf("MSIE 6")!=-1)
		loadingStyle = "position: absolute; right: 2px; top: 2px; color: #fff; background-color: #c06;padding: 1px 6px";
	//adding a loading label on the page
	var loadingLabel = document.createElement("div");
	loadingLabel.style.cssText = loadingStyle;
	loadingLabel.appendChild(document.createTextNode("Loading..."));
	loadingLabel.id = "ajaxRequestModuleLoadingLabel";
	body = document.getElementsByTagName("body")[0];
	body.appendChild(loadingLabel);
	
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("POST", url, true);
		ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		ajaxRequest.send("xmlResponse="+encodeURIComponent(message));
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("POST", url, true);
			ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			ajaxRequest.send("xmlResponse="+encodeURIComponent(message));
		}
	}
}


