// confirmExit
function confirmExit (frmID) {
	if (confirm("Voulez vous enregistrer vos modifications en quittant la page ?"))
		return changeSection (frmID, '');
	return true;
}

// changeSection - navigation entre les sections du formulaire
function changeSection(frmID, section) {
	var frm = document.forms[frmID];
	if (frm.section && frm.section.value.length > 0) {
		if (frm.next_section) 
			frm.next_section.value = section;
		if (frm.onsubmit && frm.onsubmit())
			frm.submit();
	} else {
		var x = 0, y = 0, el = document.getElementById(section);
		while(el != null) {
			x += el.offsetLeft;
			y += el.offsetTop;
			el = el.offsetParent;
		}
		if (x || y)
			window.scrollTo(x, y);
	}
	return false;
}

function editSection(section) {
	url = url_base + (url_base.match(/\?/) ? '&' : '?') + 'next_section=' + section;
	setTimeout( function () { document.location = url; }, 10);
	return false;
}

// toggleDiv
function toggleMatch (a, frmID, divID)
{
	var frm = document.getElementById(frmID);
	var div = document.getElementById(divID);
	if (!frm || !div) return false;
	frm.style.display = (frm.style.display != 'none' ? 'none' : 'block');
	div.style.display = (div.style.display != 'none' ? 'none' : 'block');
	a.innerHTML = (div.style.display == 'none' ? '[Afficher les correspondances]' : '[Afficher le formulaire]');
	return false;
}

/* lightAjax
Usage :
	var a = new lightAjax();
	a.makeRequest(url+'?'+params, completeFunc);
où
function completeFunc (ajax)
{
	if (!(txt = ajax.req.responseText) || !txt.length)
		return;
}
*/
function lightAjax ()
{
	this.req= {};
};
lightAjax.prototype.makeRequest = function (url, fnComplete)
{
	this.onComplete = fnComplete;
	var pointer = this;
	this.req = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	if (this.req)
	{
		this.req.onreadystatechange = function() 
		{
			pointer.statusChange()
		};
		this.req.open("GET", url, true);
		this.req.send(null);
	}
}
lightAjax.prototype.statusChange = function()
{
	if (this.req.readyState==4 && this.req.status==200)
		this.onComplete(this);
}

// populateMenu		
function populateMenu(xml, ctrl, def_val)
{
	if (!ctrl)  return;
	// mémorise la valeur active
	val = (ctrl.selectedIndex >= 0) ? ctrl.options[ctrl.selectedIndex].value : "";
	if (!val || !val.length)
		val = def_val;
	// ne garde que la première option
	ctrl.options.length = 1;
	// xml vide => ne rien faire
	if (!xml) return;
	// recréer les éléments dans l'ordre
	for (var i=0, lst=xml.getElementsByTagName("option"); i<lst.length; i++)
		ctrl.options.add(new Option(lst[i].getAttribute("title"), lst[i].getAttribute("val"), false, (lst[i].getAttribute("val") == val || lst[i].getAttribute('selected')=='1')));
}
// changement de secteur territorial
function dm_secteurterritorial_change()
{
	var ajax = new lightAjax();
	var url = 'ajax.communes.php?territoire='+this.options[this.selectedIndex].value;
	if (this.form.dm_id.value.length)
		url += '&demande=' + this.form.dm_id.value;
	var lst = document.getElementById('communesouhaitee');
	ajax.makeRequest (url, function () { populateMenu(ajax.req.responseXML, lst, null); });
}
// changement de departement
function dm_departement_change()
{
	var ajax = new lightAjax();
	var url = 'ajax.communes.php?dpt='+this.options[this.selectedIndex].value;
	if (this.form.dm_id.value.length)
		url += '&demande=' + this.form.dm_id.value;
	var lst = document.getElementById('commune');
	ajax.makeRequest (url, function () { populateMenu(ajax.req.responseXML, lst, null); });
}