/*
 * This function creates an XMLHttpRequest object
 * and stores it in a global variable called request.
 * This method tries to create the object different ways
 * to support Mozilla-based and IE browsers
 */


function initajax() {
			// Call the method from ajaxComm.js to create the Request
			createXMLHttpRequest();
			// identify the select box that will change
		} 

function commentaire(idcase,datec,heure,login,titre,texte,action) {
			// Start by clearing the food select
			// Clear the entire list by setting length to zero
			document.getElementById("message").className="tumevois";
			sendHttpGetRequest3("script_php/commentaire_ajax.php?cle="+idcase+"&date="+datec+"&heure="+heure+"&login="+login+"&titre="+titre+"&texte="+texte+"&action="+action);
		}
		
function validationajax(idcase,login,nom_compte) {
			// Start by clearing the food select
			// Clear the entire list by setting length to zero
			document.getElementById("message").className="tumevois";
			sendHttpGetRequest("script_php/validationajax.php?cle="+idcase+"&login="+login+"&nom_compte="+nom_compte);
		}

function adminajax(cle,login,action) {
			// Start by clearing the food select
			// Clear the entire list by setting length to zero
			document.getElementById("message").className="tumevois";
			sendHttpGetRequest("script_php/admin_ajax.php?cle="+cle+"&login="+login+"&action="+action);
		}
		
function suivi_comptes_ajax(cle,login,requ,nom_compte,cpt_page,nbre_page,action) {
			// Start by clearing the food select
			// Clear the entire list by setting length to zero
			document.getElementById("message").className="tumevois";
			sendHttpGetRequest2("script_php/suivi_comptes_ajax.php?cle="+cle+"&login="+login+"&requ="+requ+"&nom_compte="+nom_compte+"&cpt_page="+cpt_page+"&nbre_page="+nbre_page+"&action="+action);
		}

function ajout_comptes_ajax(login,requ,nom_compte,cpt_page,nbre_page,action,date,libele,type,moyen,credit,debit,cle) {
			// Start by clearing the food select
			// Clear the entire list by setting length to zero
			document.getElementById("message").className="tumevois";
			sendHttpGetRequest2("script_php/suivi_comptes_ajax.php?date="+date+"&libele="+libele+"&type="+type+"&moyen="+moyen+"&credit="+credit+"&debit="+debit+"&login="+login+"&requ="+requ+"&nom_compte="+nom_compte+"&cpt_page="+cpt_page+"&nbre_page="+nbre_page+"&action="+action+"&cle="+cle);
		}	
 
function createXMLHttpRequest() {
	request = null;
	if (window.XMLHttpRequest) {        // Mozilla-Based Browsers
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {  // newer IE browsers
		request = new ActiveXObject("Msxml2.XMLHTTP");
		if (request == null) {           // older IE Browsers
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	//The request could still be null
	if (request == null) {
	
		alert("Votre navigateur ne supporte pas AJAX");
	}
	else
	{
	if (document.getElementById) {
	    if(document.getElementById('bouton_valider')){document.getElementById('bouton_valider').style.visibility = 'hidden';}
	} else if (document.layers) {
	    if(document['bouton_valider']){document['bouton_valider'].visibility = 'hidden';}
		}
	}
}

function displayResults(ajaxResultText) {
			var somme_reel = ajaxResultText.split(',,');
			if (document.getElementById) {
			if(document.getElementById('somme_reel')){document.getElementById('somme_reel').innerHTML = somme_reel[0];}
			if(document.getElementById('op_non_valid')){document.getElementById('op_non_valid').innerHTML = somme_reel[1];}
			if(document.getElementById('test_retour')){document.getElementById('test_retour').innerHTML = somme_reel[0];}
			}
			else if (document.layers) {
			if(document['somme_reel']){document['somme_reel'].innerHTML = somme_reel[0];}
			if(document['op_non_valid']){document['op_non_valid'].innerHTML = somme_reel[1];}
			if(document['test_retour']){document['test_retour'].innerHTML = somme_reel[0];}
			}
		}
		
function displayResults2(ajaxResultText) {
			var tableau = ajaxResultText.split(',,');
			if (document.getElementById) {
			if(document.getElementById('grand_tableau')){document.getElementById('grand_tableau').innerHTML = tableau[0];}
			if(document.getElementById('somme_total')){document.getElementById('somme_total').innerHTML = tableau[1];}
			if(document.getElementById('op_non_valid')){document.getElementById('op_non_valid').innerHTML = tableau[2];}
			if(document.getElementById('somme_reel')){document.getElementById('somme_reel').innerHTML = tableau[3];}
			}
			else if (document.layers) {
			if(document['grand_tableau']){document['grand_tableau'].innerHTML = tableau[0];}
			if(document['somme_total']){document['somme_total'].innerHTML = tableau[1];}
			if(document['op_non_valid']){document['op_non_valid'].innerHTML = tableau[2];}
			if(document['somme_reel']){document['somme_reel'].innerHTML = tableau[3];}
			}
		}

function displayResults3(ajaxResultText) {
			var tableau = ajaxResultText.split(',,');
			if (document.getElementById) {
			if(document.getElementById('les_commentaires')){document.getElementById('les_commentaires').innerHTML = tableau[0];}
			}
			else if (document.layers) {
			if(document['les_commentaires']){document['les_commentaires'].innerHTML = tableau[0];}
			}
		}

/*
 * This function will send the URL passed in as an AJAX call
 * using the HTTP GET method.
 */
function sendHttpGetRequest(theURL) {
	//alert("Sending GET to "+theURL);
	request.open('GET', theURL, true);
	request.onreadystatechange=handleHttpResponse;
	request.send(null);
}

function sendHttpGetRequest2(theURL) {
	//alert("Sending GET to "+theURL);
	request.open('GET', theURL, true);
	request.onreadystatechange=handleHttpResponse2;
	request.send(null);
}

function sendHttpGetRequest3(theURL) {
	//alert("Sending GET to "+theURL);
	request.open('GET', theURL, true);
	request.onreadystatechange=handleHttpResponse3;
	request.send(null);
}

/*
 * This function will send the URL passed in as an AJAX call
 * using the HTTP POST method.
 * The postData variable should contain all the name=value
 * pairs to be sent to theURL.
 */
function sendHttpPostRequest(theURL, postData) {
	//alert("Sending POST to "+ theURL);
	request.onreadystatechange=handleHttpResponse;
	request.open('POST', theURL, true);
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	request.send(postData);
}

/*
 * This method is called by the browser when the AJAX response is
 * received from the server
 */
function handleHttpResponse() {
	//alert("handle response, ready state = "+request.readyState);
	if (request.readyState == 4) {     // ReadyState of 4 means finished
		if (request.status == 200) {    // HTTP status of 200 means ALL OK
			// Get the response as plain text
			ajaxResultText = request.responseText;
			document.getElementById("message").className="tumevoispas";
			// Call displayResults. This method must be implemented
			// for each page to do the page specific logic.
			displayResults(ajaxResultText);
		} else {
			alert("Erreur : "+request.status + ":"+request.statusText);
		}
	}
}

function handleHttpResponse2() {
	//alert("handle response, ready state = "+request.readyState);
	if (request.readyState == 4) {     // ReadyState of 4 means finished
		if (request.status == 200) {    // HTTP status of 200 means ALL OK
			// Get the response as plain text
			ajaxResultText = request.responseText;
			document.getElementById("message").className="tumevoispas";
			// Call displayResults. This method must be implemented
			// for each page to do the page specific logic.
			displayResults2(ajaxResultText);
		} else {
			alert("Erreur : "+request.status + ":"+request.statusText);
		}
	}
}

function handleHttpResponse3() {
	//alert("handle response, ready state = "+request.readyState);
	if (request.readyState == 4) {     // ReadyState of 4 means finished
		if (request.status == 200) {    // HTTP status of 200 means ALL OK
			// Get the response as plain text
			ajaxResultText = request.responseText;
			document.getElementById("message").className="tumevoispas";
			// Call displayResults. This method must be implemented
			// for each page to do the page specific logic.
			displayResults3(ajaxResultText);
		} else {
			alert("Erreur : "+request.status + ":"+request.statusText);
		}
	}
}


