function insertOption(source, destination)
{
	var elSelectSource = document.getElementById(source);
	var elSelectDestination = document.getElementById(destination);
	var elOptNew = document.createElement('option');
	
	if (elSelectSource.selectedIndex != -1)
	{
		elOptNew.text = elSelectSource.options[elSelectSource.selectedIndex].text;
		elOptNew.value = elSelectSource.options[elSelectSource.selectedIndex].value;
		
		
		var indexAjout = 0;
		try {
		  elSelectDestination.add(elOptNew, indexAjout); // standards compliant; doesn't work in IE
		  elSelectSource.remove(elSelectSource.selectedIndex);
		}
		catch(ex) {
		  elSelectDestination.add(elOptNew, null); // IE only
		  elSelectSource.remove(elSelectSource.selectedIndex);
		}
		
		sortSelect(elSelectDestination);
	}

}

function fillSelect(source, destination, index)
{
	var elSelectSource = document.getElementById(source);
	var elSelectDestination = document.getElementById(destination);
	var tableau = index.split(',');
	var i = 0;
	var j = 0;
	
		
	while (tableau[j])
	{
		while (elSelectSource.options[i])
		{
			if ( elSelectSource.options[i].value == tableau[j] )
			{
				elSelectSource.selectedIndex = i;
				insertOption(source, destination);
			}
			i++;
		}
		j++;
		i=0;
	}
}

function sortSelect(selElem) {
                var tmpAry = new Array();
                for (var i=0;i<selElem.options.length;i++) {
                        tmpAry[i] = new Array();
                        tmpAry[i][0] = selElem.options[i].text;
                        tmpAry[i][1] = selElem.options[i].value;
                }
                tmpAry.sort();
                while (selElem.options.length > 0) {
                    selElem.options[0] = null;
                }
                for (var i=0;i<tmpAry.length;i++) {
                        var op = new Option(tmpAry[i][0], tmpAry[i][1]);
                        selElem.options[i] = op;
                }
                return;
}

function getOptions()
{
	var x=document.getElementById("personnes_associees");
	var tableauOptions = new Array;
	
	if (x.length > 0 )
	{
	for (i=0;i<x.length;i++)
	  {
		tableauOptions.push(x.options[i].value);
	  }
	document.getElementById("id_personnes_associees").value = tableauOptions.join(",");
	}
	else
	{
	document.getElementById("id_personnes_associees").value = -1;
	}
}


function setIndex(source, index)
{
	var i = 0;
	var elSelectSource = document.getElementById(source);
	while (elSelectSource.options[i])
	{
		if ( elSelectSource.options[i].value == index )
		{
			elSelectSource.selectedIndex = i;
		}
		i++;
	}
}

function inverserVisibiliteDiv(nom)
{
	if (document.getElementById(nom).style.visibility == 'hidden')
	{
		document.getElementById(nom).style.visibility = "visible";
	}
	else
	{
		document.getElementById(nom).style.visibility = "hidden";
	}
}

function afficherMenu(id)
{
	document.getElementById(id).style.display = "block";
}

function initEcritQuebecoisFormulaire()
{
	document.getElementById("titre").value = "";
	document.getElementById("source").value = "";
	setIndex("auteur","-1");
	setIndex("sujet","-1");
	setIndex("annee","-1");
}
