﻿<!-- //
/**
 * Envoie des donnees a l'aide d'XmlHttpRequest ?
 * @param string url
 * @param string methode d'envoi ['GET'|'POST']
 * @param string donnees a envoyer sous la forme var1=value1&var2=value2...
 */

/**
 * Pour ne pas evaluer 2 fois le meme script mais
 * seulement ceux inseres eventuellement par la requete.
 * Pour cela, il faut imperativement placer celui-ci en dernier
 * dans le tag <head></head>.
 */
var SCRIPTS_LENGTH = document.getElementsByTagName( 'script' ).length;

function call( url, method, data, div_id, lg ) {
	var xmlhttp = getHTTPObject();

	if ( xmlhttp ) {
        /* on definit ce qui doit se passer quand la page repondra */
        xmlhttp.onreadystatechange = function() { getData( xmlhttp, div_id, lg ); }

		if ( method == 'GET' ) {
			if ( ( data == null ) || ( data == '' ) ) {
				xmlhttp.open( 'GET', url, true ); // ouverture asynchrone
			} else {
				xmlhttp.open( 'GET', url + '?' + data, true );
			}
			xmlhttp.send( null );
		} else if ( method == 'POST' ) {
			xmlhttp.open( 'POST', url, true ); // ouverture asynchrone
			xmlhttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
			xmlhttp.send( data );
		}
	}
}

function getUrl( url, cb ) {
    var xmlhttp = getHTTPObject();

    xmlhttp.open( 'GET', url );
    xmlhttp.onreadystatechange = function() {
        if ( xmlhttp.readyState == 4 ) {
            cb( xmlhttp.status, xmlhttp.getAllResponseHeaders(), xmlhttp.responseText );
        }
    }
    xmlhttp.send( null );
}

function getHTTPObject() {
	var xmlhttp = false;

	/*
     try {
		netscape.security.PrivilegeManager.enablePrivilege( "UniversalBrowserRead" );
	} catch ( e ) {
		alert( 'Permission UniversalBrowserRead denied.' );
	}
    */

	if ( window.XMLHttpRequest ) { // Mozilla, Safari,...
		xmlhttp = new XMLHttpRequest();

		if ( xmlhttp.overrideMimeType ) {
			// Commenter la ligne ci-dessous pour récupérer du texte et non du xml
			//xmlhttp.overrideMimeType( 'text/xml' );
		}
	} else if ( window.ActiveXObject ) { // IE
		try {
			xmlhttp = new ActiveXObject( 'Msxml2.XMLHTTP' );
		} catch ( e ) {
			try {
				xmlhttp = new ActiveXObject( 'Microsoft.XMLHTTP' );
			} catch ( e ) {}
		}
	}

    return xmlhttp;
}

function getData( xmlhttp, div_id, lg ) {
	var response = false;
	//var scripts  = null;

	try {
		// 4 : etat "complete"
		if ( xmlhttp.readyState == 4 ) {
			// 200 : code HTTP pour OK
			if ( xmlhttp.status == 200 ) {
				// Traitement de la reponse :
				response = xmlhttp.responseText;
				// ici on affiche la reponse qui a ete encapsulee dans un
				// tag <code><![CDATA[]]></code>.
				//response = response.substring( 15, ( response.length - 10 ) );
				////response = response.escapeForJS();

				if ( !div_id.isEmpty() ) {
					var div      = find( div_id );
					var div_copy = find( div_id + '_copy' );

					div.innerHTML     = response;
					div.style.display = 'block'; // 'none'

					var scripts = document.getElementsByTagName( 'script' );

					if ( scripts.length > 0 ) {
						for ( var i = SCRIPTS_LENGTH; i < scripts.length; i++ ) {
							if ( scripts[ i ].text.length > 0 ) {
								eval( scripts[ i ].text );
							}
						}

						if ( div_copy && LOTS_NUMBER ) {
							/*if ( lg == 'FR' ) {
								div_copy.innerHTML = 'R&eacute;sultats : ' + LOTS_NUMBER + '&nbsp;';
							} else {
								div_copy.innerHTML = 'Results : ' + LOTS_NUMBER + '&nbsp;';
							}*/
							div_copy.innerHTML     = response;
							div_copy.style.display = 'block';
						}
					}
				}
			}
		}
	} catch ( e  ) {
		alert( 'Une exception s\'est produite : ' + e.description );
	}

	return response;
}
// -->