/*
 * Scripts that handle the reloading of the location combo boxes. *
 * TODO : Place all this in an object
 * TODO : AJAXify this.
 */


/*
 * Reloads the page to get the values for the ville and quartier
 * and the cld which are associated with the selected region.
 */
function onRegionChange (page, formId, othersValue) {
	var url = page + "?";
	var form = document.getElementById (formId);
	var box = form.region;
	
	url += "region=" + box.options[box.selectedIndex].value;
	
	url += formToURL (form);
	url += '#' + formId;
	
	window.location = url;
}

/*
 * Reloads the page to get the values for the quartier
 * and the cld which are associated with the selected ville.
 */
function onVilleChange (page, formId, othersValue) {
	var url= page + "?";	

	var form = document.getElementById (formId);
	var box = form.region;
	url += "region=" + box.options[box.selectedIndex].value;
	
	box = form.ville;
	url += "&ville=" + box.options[box.selectedIndex].value;
	
	url += formToURL (form);	
	url += '#' + formId;
	
	window.location = url;
}

/*
 * Takes every inputed data in the form and places it in the url
 * so that when the page is reloaded the data is replaced as well.
 */
function formToURL (form) {
	var url= "";
	for (var i = 0; i < form.elements.length; ++i) {
		var el = form.elements[i];
		
		if (el.type == "file" || el.type == "submit")
			continue;
		if (el.name == 'region' || el.name == 'ville' || el.name == 'quartier') 
		{
			continue;
		}
		if (el.value.length <= 0)
			continue;
			
		url += "&" + el.name + "=" + el.value;
	}
	return url;
}

/*
 * If the other option is selected in one of the location box,
 * display the text input box associated with it. Otherwise it
 * hides it.
 */
function activateOthers (field, othersValue) {
	var box = document.getElementById (field);
	var input = document.getElementById ("r_autre_" + field);
	
	if (box.options[box.selectedIndex].value == othersValue) {
		input.style.display = "";
		return true;
	}
	else {
		input.style.display = "none";
		return false;
	}
}