function nodeCleaner(n) {

  if(!n.data.replace(/\s/g,'')) n.parentNode.removeChild(n);

}



function cleanXML(docElement) {
  var node = docElement.getElementsByTagName('*');

  for(i = 0; i < node.length; i++) {

    a = node[i].previousSibling;

    if(a && a.nodeType == 3) nodeCleaner(a);

    b = node[i].nextSibling;

    if(b && b.nodeType == 3) nodeCleaner(b);

  }

  return docElement;

}



function affichecounties(index)
{
	var seltag='';
	var idtag='';
	index=index-1;
	var nom="region"+index;
	seltag = document.getElementById(nom);
	idtag = seltag.options[seltag.selectedIndex].value;	
	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");
        }
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() 
	{
		if(xhr.readyState==4 && xhr.status==200){
			counties(xhr,index); 
		}
	};
    
    //on appelle le fichier 
   var envoi="idregion="+idtag;
	//alert(envoi);
	/*xhr.open("GET", "../inc/affreponse.php"+envoi, true);
	xhr.send(null);
	*/
	xhr.open("POST", urlitdistri+"inc/affcounties.php", true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(envoi);
	
	
	

}

function counties(xhr,index)
{	
	var docXML= xhr.responseXML;
	
	//alert(xhr.responseText);
	//response = docXML.documentElement;
	response = cleanXML(docXML.documentElement);
	
	
	//document.getElementById('region').innerHTML = 'TEST';
	var items = response.getElementsByTagName("county");
	
	
	//on fait juste une boucle sur chaque element "donnee" trouvé
	
	document.getElementById('county'+index).innerHTML = '';
	
	count = items.length;
	var vhtmlpays='';	
	var taille=count+1;
	for(i = 0; i < count; i++)
	{
		vhtmlpays += '<option value="'+ items[i].getElementsByTagName("id_county")[0].firstChild.nodeValue +'">';
		vhtmlpays += ''+ items[i].getElementsByTagName("lib_county")[0].firstChild.nodeValue + '</option>';
	}
	document.getElementById('county'+index).innerHTML += vhtmlpays;
	
}