var xmlhttp
var formulaire

function go(form)
{
	formulaire = form;
	var alerted
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	try {
	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
	try {
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	} catch (e) {
	alert("You must have Microsofts XML parsers available")
	}
	}
	@else
	alert("You must have JScript version 5 or above.")
	xmlhttp=false
	alerted=true
	@end @*/
	if (!xmlhttp && !alerted)
	{
		// Non ECMAScript Ed. 3 will error here (IE<5 ok), nothing I can
		// realistically do about it, blame the w3c or ECMA for not
		// having a working versioning capability in <SCRIPT> or
		// ECMAScript.
		try
		{
			xmlhttp = new XMLHttpRequest();
		}
		catch (e)
		{
			alert("You need a browser which supports an XMLHttpRequest")
		}
	}
	xmlhttp.onreadystatechange=waitResponse;
	<!-- URL sur le serveur Web adressant les services web -->
	xmlhttp.open("POST", "/webservices/gsoap/",true);
	var xml = getWSReq("javascript", formulaire);
	xmlhttp.setRequestHeader("Content-Type", "text/xml");
	xmlhttp.send(xml);
}

function waitResponse()
{
	if (xmlhttp.readyState==4)
	{
		if (xmlhttp.status == 200)
		{
			try
			{
				xml = xmlhttp.responseText;
	//alert(xml);
				getWSResp(xml, formulaire);
			}
			catch (e)
			{
				alert("You need a browser which supports a DOM Interface" + e.message)
			}
		}
		else
			alert("Problème de connexion :\n" + xmlhttp.statusText + ' ' +	xmlhttp.responseText)
	}
}

function getWSReq(imputation, form)
{
	var xml;
	imputation = 'media1525';
	xml = getAdresseReq(imputation, form);
	return xml;
}

function getWSResp(xml, form)
{
	getAdresseResp(xml, form);
}

function getAdresseReq(imputation, form)
{
	var xml='<?xml version="1.0" encoding="utf-8"?>'+
	'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'+
	'xmlns:xsd="http://www.w3.org/2001/XMLSchema"'+
	'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
	'<soap:Header>'+
	'<imputation xmlns="http://www.amabis.com/ns.xsd">'+imputation+'</imputation>'+
	'</soap:Header>'+
	'<soap:Body>'+
	'<traiteadresse xmlns="http://www.amabis.com/ns.xsd">'+
	'<volets>'+
	'<nbVolets>'+'5'+'</nbVolets>'+
	'<numVoletNom>'+''+'</numVoletNom>'+
	'<numVoletLocalite>'+''+'</numVoletLocalite>'+
	'<volet1>'+form.adresse1.value+'</volet1>'+
	'<volet2>'+form.adresse2.value+'</volet2>'+
	'<volet3>'+form.adresse3.value+'</volet3>'+
	'<volet4>'+form.postcode.value+'</volet4>'+
	'<volet5>'+form.ville.value+'</volet5>'+
	'</volets>'+
	'<niveau>'+'3'+'</niveau>'+
	'<type>'+'A'+'</type>'+
	'<typescorenom>'+''+'</typescorenom>'+
	'<scoremini>'+'50'+'</scoremini>'+
	'<distinction>'+'30'+'</distinction>'+
	'<nbcols>'+'38'+'</nbcols>'+
	'<uppercase>'+'false'+'</uppercase>'+
	'</traiteadresse>'+
	'</soap:Body>'+
	'</soap:Envelope>';
	return xml;
}

function getAdresseResp(xml, form)
{
	var dom = getDOM(xml);

	//form.result.value = xml;

	// Valeurs originales
	var adresse1_original = form.adresse1.value;
	var adresse2_original = form.adresse2.value;
	var adresse3_original = form.adresse3.value;
	var postcode_original = form.postcode.value;
	var ville_original = form.ville.value;
	var precision_correction_adresse = "";

	// Erreur retournée (0 pour OK)
	volets = dom.getElementsByTagName("erreurnum");
	stringNode = volets[0].firstChild;
	var errornum = "";
	if (stringNode!=null)
		errornum = volets[0].firstChild.nodeValue;

	// Correction
	volets = dom.getElementsByTagName("optionlist");
	stringNode = volets[0].firstChild;
	var optionList = "";
	if (stringNode!=null)
		optionList = volets[0].firstChild.nodeValue;

	// Récupère la précision de la correction retournée
	if(optionList == "")
		precision_correction_adresse = "100%"
	else
		precision_correction_adresse = optionList.substring(0, optionList.indexOf("%", 0)+1);

	// Récupération de la valeur du volet 1 (habituellement vide)
	volets = dom.getElementsByTagName("volet1");
	stringNode = volets[0].firstChild;
	var volet1 = "";
	if (stringNode!=null)
		volet1 = volets[0].firstChild.nodeValue;

	if(errornum != 0 || (volet1 != "" && optionList == ""))
	{
		// Si erreur, remise en place des valeurs par défaut & qualité de l'adresse = 0%
		form.adresse1.value = adresse1_original;
		form.adresse2.value = adresse2_original;
		form.adresse3.value = adresse3_original;
		form.postcode.value = postcode_original;
		form.ville.value = ville_original;
		precision_correction_adresse = "0%";
	}
	else if(optionList == "")
	{
		// Traitement de la correction UNIQUE retournée
		volets = dom.getElementsByTagName("volet2");
		stringNode = volets[0].firstChild;
		var volet2 = "";
		if (stringNode!=null)
			volet2 = volets[0].firstChild.nodeValue;

		volets = dom.getElementsByTagName("volet3");
		stringNode = volets[0].firstChild;
		var volet3 = "";
		if (stringNode!=null)
			volet3 = volets[0].firstChild.nodeValue;

		volets = dom.getElementsByTagName("volet4");
		stringNode =volets[0].firstChild;
		var volet4 = "";
		if (stringNode!=null)
			volet4 = volets[0].firstChild.nodeValue;

		volets = dom.getElementsByTagName("volet5");
		stringNode = volets[0].firstChild;
		var volet5 = "";
		if (stringNode!=null)
			volet5 = volets[0].firstChild.nodeValue;

		// Change les données avec les valeurs corrigées
		var adresse1OK = false;
		var adresse2OK = false;
		var adresse3OK = false;

		if(volet2 != "")
		{
			form.adresse1.value = volet2;
			adresse1OK = true;
		}
		if(volet3 != "")
		{
			if(adresse1OK == false)
			{
				form.adresse1.value = volet3;
				adresse1OK = true;
			}
			else
			{
				form.adresse2.value = volet3;
				adresse2OK = true;
			}
		}
		if(volet4 != "")
		{
			if(adresse1OK == false)
			{
				form.adresse1.value = volet4;
				adresse1OK = true;
			}
			else if(adresse2OK == false)
			{
				form.adresse2.value = volet4;
				adresse2OK = true;
			}
			else
			{
				form.adresse3.value = volet4;
				adresse3OK = true;
			}
		}
		if(volet5 != "")
		{
			if(adresse1OK == false)
			{
				form.adresse1.value = volet5;
				adresse1OK = true;
			}
			else if(adresse2OK == false)
			{
				form.adresse2.value = volet5;
				adresse2OK = true;
			}
			else
			{
				form.adresse3.value = volet5;
				adresse3OK = true;
			}
		}
		// Reset des textbox non renseignées
		if(adresse1OK == false)
			form.adresse1.value = "";
		if(adresse2OK == false)
			form.adresse2.value = "";
		if(adresse3OK == false)
			form.adresse3.value = "";

		// Traitement de la ville et du code postal
		volets = dom.getElementsByTagName("volet6");
		stringNode = volets[0].firstChild;
		var postcode = "";
		var ville = "";
		if (stringNode!=null)
		{
			var tmp = volets[0].firstChild.nodeValue;
			var separer = tmp.indexOf(" ", 0);
			postcode = tmp.substring(0, separer);
			ville = tmp.substring(separer+1, tmp.length);
		}
		form.postcode.value = postcode;
		form.ville.value = ville;
	}
	else
	{
		// Récupération de la correction la plus probable
		var value_corrected = optionList.substring(optionList.indexOf(":", 0)+1, optionList.indexOf("|", 0));

		// Vérifie le type de donnée retournée (ville ou rue corrigée)
		if(value_corrected.indexOf("(",0) != -1 && value_corrected.indexOf(")",0) != -1)
		{
			// Récupération du nom de rue proposé
			var newrue = value_corrected.substring(0, value_corrected.indexOf("(",0));
			var new_postcode = value_corrected.substring(value_corrected.indexOf("(",0)+1, value_corrected.indexOf(")",0))

			// Recherche du champ textbox correspondant à l'adresse retournée (1, 2 ou 3)
			volets = dom.getElementsByTagName("volet2");
			stringNode = volets[0].firstChild;
			var volet2 = "";
			if (stringNode!=null)
				volet2 = volets[0].firstChild.nodeValue;

			if(volet2 == "")
			{
				// champ 1
				form.adresse1.value = newrue;
				form.adresse2.value = "";
				form.adresse3.value = "";
			}
			else
			{
				form.adresse1.value = adresse1_original;

				volets = dom.getElementsByTagName("volet3");
				stringNode = volets[0].firstChild;
				var volet3 = "";
				if (stringNode!=null)
					volet3 = volets[0].firstChild.nodeValue;

				if(volet3 == "")
				{
					// Champ 2
					form.adresse2.value = newrue;
					form.adresse3.value = "";
				}
				else
				{
					// Champ 3
					form.adresse2.value = "";
					form.adresse3.value = newrue;
				}
			}

			// Récupération de la ville et du code postal
			volets = dom.getElementsByTagName("volet6");
			stringNode = volets[0].firstChild;
			var volet6 = "";
			if (stringNode!=null)
				volet6 = volets[0].firstChild.nodeValue;

			// Si la ville à été corrigée automatiquement, récupération
			if(volet6 != "")
			{
				var separer = volet6.indexOf(" ", 0);
				form.postcode.value = volet6.substring(0, separer);
				form.ville.value = volet6.substring(separer+1, volet6.length);
			}
			else
			{
				// Sinon, enregistrement du nouveau code postal
				form.postcode.value = new_postcode;
				form.ville.value = ville_original;
				// Relance de la fonction de vérification avec les nouvelles données
				//go(form);
			}
		}
		else
		{
			// Correction de la ville
			form.postcode.value = value_corrected.substring(0, value_corrected.indexOf(" ", 0));
			form.ville.value = value_corrected.substring(value_corrected.indexOf(" ", 0)+1, value_corrected.length);
			//go(form);
		}
	}
	form.precision_correction_adresse.value = precision_correction_adresse;
	form.submit();
}

function getDOM(xml)
{
	// alert (xml);
	var myDocument;
	if (document.implementation.createDocument)
	{
		// Mozilla, create a new DOMParser
		var parser = new DOMParser();
		myDocument = parser.parseFromString(xml, "text/xml");
	}
	else if (window.ActiveXObject)
	{
		// Internet Explorer, create a new XML document using ActiveX
		// and use loadXML as a DOM parser.
		myDocument = new ActiveXObject("Microsoft.XMLDOM")
		myDocument.async="false";
		myDocument.loadXML(xml);
	}
	return myDocument;
}
