// JavaScript Document
function xhr_init () {
	var xhr = null; 
	
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest(); 
	} else if(window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xhr;	
}

function getformobjdata (obj) {
    var postdata = "";
    var i;
    
    for (i=0; i<obj.childNodes.length; i++) {
        if (obj.childNodes[i].tagName == "INPUT") {
            if (obj.childNodes[i].type == "text") {
                postdata += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }else if (obj.childNodes[i].type == "checkbox") {
                if (obj.childNodes[i].checked) {
                    postdata += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
                } else {
                    postdata += obj.childNodes[i].name + "=&";
                }
            }else if (obj.childNodes[i].type == "radio") {
                if (obj.childNodes[i].checked) {
                    postdata += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
                }
            }
        } else if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            postdata += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
        } else if (obj.childNodes[i].tagName == "TEXTAREA") {
            postdata += obj.childNodes[i].name + '=' + obj.childNodes[i].value + "&";
        } else {
            postdata += getformobjdata (obj.childNodes[i]);
        }    
    }

    return postdata;
}

function getformdata(formid) {
    var obj = document.getElementById(formid);
    return getformobjdata (obj);
}

function aj_post (aj_script, aj_data, aj_callback) {
	var xhr = xhr_init();
	if (!xhr) {
		alert("Votre navigateur ne supporte pas certaines fonctionnalités nécessaires pour cette application ..."); 
		return false;
	}
	
	show_ajpopup ('Veuillez patienter ...');
	
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			show_ajpopup (xhr.responseText);
			if (aj_callback) {
				eval (aj_callback);
			}
		}
	}	
	xhr.open("POST", aj_script, true);
	xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xhr.send(aj_data);
}

function aj_get (aj_script, aj_callback) {
	var xhr = xhr_init();
	if (!xhr) {
		alert("Votre navigateur ne supporte pas certaines fonctionnalités nécessaires pour cette application ...");
		return false;
	}

	show_ajpopup ('Veuillez patienter ...');

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			show_ajpopup (xhr.responseText);
			if (aj_callback) {
				eval(aj_callback);
			}
		}
	}
	xhr.open("GET", aj_script, true);
	xhr.send(null);
}

function aj_exec_stealth (aj_script, aj_callback) {
	var xhr = xhr_init();
	if (!xhr) {
		alert("Votre navigateur ne supporte pas certaines fonctionnalités nécessaires pour cette application ...");
		return false;
	}

	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {
			if (aj_callback) {
				eval(aj_callback);
			}
		}
	}
	xhr.open("GET", aj_script, true);
	xhr.send(null);
}

