
function getXmlHttp() {
	var xmlHttp = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //xmlHttp.overrideMimeType('text/html');
            //xmlHttp.overrideMimeType('text/xml');
			
			xmlHttp.overrideMimeType("text/xhtml");
		}
	} else {
		if (window.ActiveXObject) { // IE
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e) {
				try {
					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e) {
				}
			}
		}
	}
	if (!xmlHttp) {
		alert("Cannot create XMLHTTP instance");
		return false;
	}

	return xmlHttp;
}
// readyState possible values
var REQUEST_NOT_INITIALIZED = 0;
var REQUEST_SENDING = 1;
var REQUEST_SENT = 2;
var REQUEST_IN_PROGRESS = 3;
var REQUEST_COMPLETE = 4;

function ajaxFormSubmit(formNameOrObject, resultDivId, callback, callbackParameters) {
	var form;
	if(typeof formNameOrObject == "string") {
		form = document.forms[formNameOrObject];
	} else {
		form = formNameOrObject;
	}

	if(form) {
		var elements = form.elements;

		var url = form.action;
		var data="";
		var isFirstParam = true;

		for (var i=0; i<elements.length; i++) {
			var elt = form.elements[i];
			if(elt && elt!==null && elt != 'undefined' && elt.name && elt.name !== null && elt.name != 'undefined') {
				if(!isFirstParam) {
					data += "&";
				}
			  	data += elt.name + "=" + escape(elt.value);
			  	isFirstParam = false;
			}
		}
		ajaxSend("POST", url, data, resultDivId, callback, callbackParameters);
	}
}

function ajaxSend(mode, url, data, resultDivId, callback, callbackParameters) {
	var xmlHttp = getXmlHttp();
	xmlHttp.onreadystatechange = function () {
		switch (xmlHttp.readyState) {
		  case REQUEST_COMPLETE:
		  		if(xmlHttp.status == 200) {
		  			if(resultDivId!=null) {

		  				var resultDiv = document.getElementById(resultDivId);

			  			if(resultDiv!=null) {
			  				var respText = xmlHttp.responseText;
			  				var respXML = xmlHttp.responseXML;
							resultDiv.innerHTML = xmlHttp.responseText;							

							// execute the javascript contained in the response (if any)
							var scripts = resultDiv.getElementsByTagName("script");
							for (var i = 0; i < scripts.length; i++) {
								var script = scripts[i];
								eval(script.innerHTML);
							}
							scripts = null;
							resultDiv = null;
			  			}
		  			}

					if(callback && typeof callback == "function") {
						callback(callbackParameters);
					}
				} else {

					if(xmlHttp.status == 409) { //HTTP 409 : SC_CONFLICT
						// using that 409 code to notify this script that the
						// session has logout and whole page should be redirected
						window.location = "/";
					}
					else {
						window.alert("Ajax returned HTTP Error Code : " + xmlHttp.status);
					}
				}
		  		callback = null;
		  		xmlHttp = null;
		  		this.onreadystatechange = null;
			break;
		}
	};
	if(url.indexOf('?') <0) {
		url += '?';
	}
	data = "sentByAjax=1&" + data;

	xmlHttp.open(mode, url, true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlHttp.send(data);
}