var xhr_objectSection2;
var isRequestActiveSection2;
var elmtSection2Source;

var AjaxSelectSection3 = function (elmtIDsource, elmtIDtarget, url, allsection, lang)
{
	// Set the optional parameter if needed
	if ( (typeof allsection == 'undefined') || (allsection == 'undefined') ) {
		allsection = "0";
   	}

	elmtSection2Source = document.getElementById(elmtIDsource);
	elmtSection2Source.autocomplete = 'off';

	elmtSection2Source.onchange = function (){
		if(isRequestActiveSection2)
			xhr_objectSection2.abort();
		makeRequestSection3(url, elmtSection2Source.value, elmtIDtarget, allsection, lang);
	}

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

	isRequestActiveSection2 = false;
}


var makeRequestSection3 = function(url, sect2id, elmtIDtarget, allsection, lang)
{
	clearSelect(elmtIDtarget);

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

		xhr_objectSection2.onreadystatechange = function(){

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


function parseMessagesSection3(elmtIDtarget,responseXML) {
	clearSelect(elmtIDtarget);
	
	var section3s = responseXML.getElementsByTagName('section3s')[0];

    for (loop = 0; loop < section3s.childNodes.length; loop++) {
	    var section3 = section3s.childNodes[loop];
        var sect3id = section3.getElementsByTagName("sect3id")[0];
        var sect3label = section3.getElementsByTagName("sect3label")[0];
        var sect3desc = section3.getElementsByTagName("sect3desc")[0];
        appendOptionInSelect(elmtIDtarget,sect3id.childNodes[0].nodeValue,sect3label.childNodes[0].nodeValue);
    }
}



