var xhr_objectSection1;
var isRequestActiveSection1;
var elmtSection1Source;

var AjaxSelectSection2 = function (elmtIDsource, elmtIDsection2, elmtIDsection3, url, allsection, lang)
{
	// Set the optional parameter if needed
	if ( (typeof allsection == 'undefined') || (allsection == 'undefined') ) {
		allsection = "0";
   	}
   
	elmtSection1Source = document.getElementById(elmtIDsource);
	elmtSection1Source.autocomplete = 'off';

	elmtSection1Source.onchange = function (){
		if(isRequestActiveSection1)
			xhr_objectSection1.abort();
		makeRequestSection2(url, elmtSection1Source.value, elmtIDsection2, elmtIDsection3, allsection, lang);
	}

	xhr_objectSection1 = createXHRObject();
	if(!xhr_objectSection1){
		return;
	}

	isRequestActiveSection1 = false;
}


var makeRequestSection2 = function(url, sect1id, elmtIDsection2, elmtIDsection3, allsection, lang)
{
	clearSelect(elmtIDsection2);
	clearSelect(elmtIDsection3);

	if ((elmtSection1Source.selectedIndex>=0)&&(elmtSection1Source.options[elmtSection1Source.selectedIndex].value != "")) {
	
		xhr_objectSection1.open('post', url, true);

		xhr_objectSection1.onreadystatechange = function(){

			if(xhr_objectSection1.readyState == 4){
				isRequestActiveSection1 = false;
				// On vérifie qu'il y ait des résultats et qu'il n'y ait pas d'erreur
				if( (xhr_objectSection1.responseText == '') || (xhr_objectSection1.status != 200)){
					return;
				}
				parseMessagesSection2(elmtIDsection2,elmtIDsection3,xhr_objectSection1.responseXML);
			}
		}
	
		xhr_objectSection1.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		var str = "sect1id="+escape(sect1id)+"&allsection="+escape(allsection)+"&lang="+escape(lang);
		xhr_objectSection1.send(str);
		isRequestActiveSection1 = true;
	}
}


function parseMessagesSection2(elmtIDsection2,elmtIDsection3,responseXML) 
{
	clearSelect(elmtIDsection2);
	clearSelect(elmtIDsection3);
	
	var section2s = responseXML.getElementsByTagName('section2s')[0];

    for (loop = 0; loop < section2s.childNodes.length; loop++) {
	    var section2 = section2s.childNodes[loop];
        var sect2id = section2.getElementsByTagName("sect2id")[0];
        var sect2label = section2.getElementsByTagName("sect2label")[0];
        var sect2desc = section2.getElementsByTagName("sect2desc")[0];
        appendOptionInSelect(elmtIDsection2,sect2id.childNodes[0].nodeValue,sect2label.childNodes[0].nodeValue);
    }
    
	//Pour forcer le chargement des section3
    if(section2s.childNodes.length>0){
		var selectLst = document.getElementById(elmtIDsection2);
		if (selectLst) {
			selectLst.onchange();
		}
    }
    
}



