function orderDisplayedChapters(direction) {
	var thelist_toorder = document.chapterform.displayedchapters;	
	if(direction == "up") {
		var i=0;
		while(i<thelist_toorder.length) {
			if(i!=0 && thelist_toorder.options[i].selected==true){			
				var nameCur = thelist_toorder.options[i].text;
				var valueCur = thelist_toorder.options[i].value;
				var nameBef = thelist_toorder.options[i-1].text;
				var valueBef = thelist_toorder.options[i-1].value;
				thelist_toorder.options[i-1].text = nameCur;
				thelist_toorder.options[i-1].value = valueCur;
				thelist_toorder.options[i].text = nameBef;
				thelist_toorder.options[i].value = valueBef;
				thelist_toorder.options[i-1].selected = true;
				thelist_toorder.options[i].selected = false;
			}
			i++;				
		}
	} else {
		var i=thelist_toorder.length-1;
		while(i>=0){
			if(i!=thelist_toorder.length-1 && thelist_toorder.options[i].selected==true){			
				var nameCur = thelist_toorder.options[i].text;
				var valueCur = thelist_toorder.options[i].value;
				var nameFol = thelist_toorder.options[i+1].text;
				var valueFol = thelist_toorder.options[i+1].value;
				thelist_toorder.options[i+1].text = nameCur;
				thelist_toorder.options[i+1].value = valueCur;
				thelist_toorder.options[i].text = nameFol;
				thelist_toorder.options[i].value = valueFol;
				thelist_toorder.options[i].selected = false;
				thelist_toorder.options[i+1].selected = true;
			}
			i--;		
		}
	}	
}

function displayChapters() {			
	var thelist_origine = document.chapterform.availablechapters;
	var thelist_destination = document.chapterform.displayedchapters;
	var theindex_destination = thelist_destination.length;			
	var i=0;
	while(i<thelist_origine.length) {					
		if(thelist_origine.options[i].selected==true){			
			var name = thelist_origine.options[i].text;
			var value = thelist_origine.options[i].value;
			thelist_destination.options[theindex_destination] = new Option(name,value);
			thelist_origine.options[i] = null;
			theindex_destination++;
		} else {
			i++;
		}	
	}
}

function undisplayChapters() {	
	var thelist_origine = document.chapterform.displayedchapters;
	var thelist_destination = document.chapterform.availablechapters;
	var theindex_destination = thelist_destination.length;
	var i=0;
	while(i<thelist_origine.length) {
		if(thelist_origine.options[i].selected==true){			
			var name = thelist_origine.options[i].text;
			var value = thelist_origine.options[i].value;
			thelist_destination.options[theindex_destination] = new Option(name,value);
			thelist_origine.options[i] = null;
			theindex_destination++;
		} else {
			i++;
		}
	}	
}

function concatenateAndSubmit() {	
	var value="";
	for(var i=0;i<document.chapterform.displayedchapters.length;i++){
		value = value + document.chapterform.displayedchapters.options[i].value + "@";	
	}
	document.chapterform.concatenateIdsDisplayedChapters.value = value;		
	value="";
	for(var i=0;i<document.chapterform.displayedchapters.length;i++){
		value = value + document.chapterform.displayedchapters.options[i].text + "@";	
	}
	document.chapterform.concatenateNamesDisplayedChapters.value = value;		
	value="";
	for(var i=0;i<document.chapterform.availablechapters.length;i++){
		value = value + document.chapterform.availablechapters.options[i].value + "@";	
	}
	document.chapterform.concatenateIdsAvailableChapters.value = value;		
	value="";
	for(var i=0;i<document.chapterform.availablechapters.length;i++){
		value = value + document.chapterform.availablechapters.options[i].text + "@";	
	}
	document.chapterform.concatenateNamesAvailableChapters.value = value;		
	//document.chapterform.submit();
}

function initializing(){
	// displayed chapters
	var ids_string = document.chapterform.concatenateIdsDisplayedChapters.value;
	var names_string = document.chapterform.concatenateNamesDisplayedChapters.value;
	var id="";
	var name="";
	while(ids_string.length !=0 ) {
		id = ids_string.substring(0,ids_string.indexOf("@"));
		ids_string = ids_string.substring(ids_string.indexOf("@")+1,ids_string.length);
		name = names_string.substring(0,names_string.indexOf("@"));
		names_string = names_string.substring(names_string.indexOf("@")+1,names_string.length);
		document.chapterform.displayedchapters.options[document.chapterform.displayedchapters.length] = new Option(name,id);
	}	
	// available chapters
	var ids_string = document.chapterform.concatenateIdsAvailableChapters.value;
	var names_string = document.chapterform.concatenateNamesAvailableChapters.value;
	var id="";
	var name="";	
	while(ids_string.length != 0){
		id = ids_string.substring(0,ids_string.indexOf("@"));
		ids_string = ids_string.substring(ids_string.indexOf("@")+1,ids_string.length);
		name = names_string.substring(0,names_string.indexOf("@"));
		names_string = names_string.substring(names_string.indexOf("@")+1,names_string.length);
		document.chapterform.availablechapters.options[document.chapterform.availablechapters.length] = new Option(name,id);
	}
}
function init() {
      valueIcd = document.getElementById('icd').value;
      valuePhf = document.getElementById('phf').value;
      valueThr = document.getElementById('thr').value;
      if (valueThr) {
        // aspect graphique
        document.getElementById('name').value = "";
        document.getElementById('radioIcd').checked = true;
        // champs cach?s
        document.getElementById('icd').value = "";
        document.getElementById('phf').value = "";
      } else if (valueIcd) {
        document.getElementById('name').value = valueIcd;
        document.getElementById('radioIcd').checked = true;
      } else if (valuePhf) {
        document.getElementById('name').value = valuePhf;
        document.getElementById('radioPhf').checked = true;
      } else {
        document.getElementById('name').value = "";
        document.getElementById('radioIcd').checked = true;
      }      
    }
    function radioValue(paramToSet, paramToErase) {
    	document.getElementById(paramToSet).value = document.getElementById('name').value;
    	document.getElementById(paramToErase).value = "";
    	document.getElementById('thr').value = "";
    }
    function eraseThr() {
      document.getElementById('thr').value = "";
      if (document.getElementById('radioPhf').checked) {
        radioValue('phf','icd');
      } else {
        radioValue('icd','phf');
      }
    }
    function erasePhfIcd() {
      document.getElementById('name').value = "";
      document.getElementById('radioPhf').checked = false;
      document.getElementById('radioIcd').checked = true;
      document.getElementById('icd').value = "";
      document.getElementById('phf').value = "";
    }
