function allSelected (target) {
	//alert(document.getElementById(target).options.length);
	var nbEl = document.getElementById(target).options.length;
	//alert(document.getElementById(target).options[document.getElementById(target).selectedIndex].value);
	var selected = document.getElementById(target).selectedIndex;
	if (selected == 0) {
		for (i=0;i<nbEl;i++) {
			document.getElementById(target).options[i].selected = false;
		}
	} else {
		for (i=0;i<nbEl;i++) {
			document.getElementById(target).options[i].selected = true;
		}
	}
	//alert(document.getElementById(target).options[document.getElementById(target).selectedIndex].value);
	//alert(document.getElementById(target).selectedIndex);
}

function keepState() {
	if(document.getElementById('CandidateDualNationality').checked == false) {
		Effect.toggle('divNationality','blind');
	}
	/*if(document.getElementById('CandidateDualNationality').checked || document.getElementById('CandidateAvailable').checked || document.getElementById('CandidateDrivingLicence').checked ) {
		Effect.toggle('divNationality','blind');
		Effect.toggle('divEventDate','blind');
		return false;
	}*/
}

//function to create a new question
function create_Response(question) {

var valLines = document.getElementById('Question'+question+'ResponseCount');

var countLine = parseInt(valLines.value)+1;
valLines.setAttribute("value",countLine);
var response = document.getElementById("Question1Response1").cloneNode(true);

var td_item = document.getElementById("response"+question);

var div_action = document.createElement("div");


response.setAttribute("id","Question"+question+"response"+countLine);
response.setAttribute("name","data[Question]["+question+"][response]["+countLine+"]");

response.innerHTML="N/A";

div_action.setAttribute("id","delfield_"+question+"_"+countLine);


//div_action.innerHTML+='<span><a href="javascript:remove_Response('Question1Response1','1');">Supprimer</a></span>';
div_action.innerHTML+='<span id="addedfields_'+question+'_'+countLine+'"><a href="javascript:remove_Response(\''+countLine+'\',\''+question+'\');">Supprimer</a></span><br> <br> ';


td_item.appendChild(response);

td_item.appendChild(div_action);

}

function remove_Response(countLine,question) {
var t = document.getElementById("response"+question);

var l=document.getElementById('Question'+question+'response'+countLine);
var l2=document.getElementById('delfield_'+question+'_'+countLine);
t.removeChild(l);
t.removeChild(l2);
}


//function to create a new question
function create_Question() {

i=1;
var valQuestions = 1;

var valLines = document.getElementById('QuestionLinesCount');
var valQuestion = document.getElementById('QuestionQuestionsCount');

items = document.getElementsByTagName("tr");

// if (valLines.value=="") valLines.value=items.length;
if (valQuestion == null){ 
if (valLines.value=="") valLines.value=1;
}else
if (valLines.value=="") valLines.value=valQuestion.value;

var countLine = (parseInt(valLines.value) +1);
valLines.value=countLine;

var i2 = i + 1;

var question = document.getElementById("Question1Question").cloneNode(true);
var questiontypeid = document.getElementById("Question1QuestionTypeId").cloneNode(true);
var search = document.getElementById("Question1SearchId").cloneNode(true);
var num = document.getElementById("Question1Num").cloneNode(true);
var response1 = document.getElementById("Question1Response1").cloneNode(true);
var response2 = document.getElementById("Question1Response2").cloneNode(true);
var responseCount = document.getElementById("Question1ResponseCount").cloneNode(true);



question.setAttribute("id","Question"+countLine+"Question");
question.setAttribute("name","data[Question]["+countLine+"][question]");


questiontypeid.setAttribute("id","Question"+countLine+"QuestionTypeId");
questiontypeid.setAttribute("name","data[Question]["+countLine+"][question_type_id]");

search.setAttribute("id","Question"+countLine+"SearchId");
search.setAttribute("name","data[Question]["+countLine+"][search_id]");

num.setAttribute("id","Question"+countLine+"Num");
num.setAttribute("name","data[Question]["+countLine+"][num]");
num.setAttribute("value","+countLine+");

responseCount.setAttribute("value","2");


response1.setAttribute("id","Question"+countLine+"Response1");
response1.setAttribute("name","data[Question]["+countLine+"][response][1]");


response2.setAttribute("id","Question"+countLine+"Response2");
response2.setAttribute("name","data[Question]["+countLine+"][response][2]");

responseCount.setAttribute("id","Question"+countLine+"ResponseCount");
responseCount.setAttribute("name","data[Question]["+countLine+"][responseCount]");



var body = document.getElementsByTagName("tbody")[0];

var row = document.createElement("tr");
var cell = document.createElement("td");
cell.setAttribute("style","border-right:1px solid #eae9e9;border-bottom:1px solid #eae9e9;text-align:center");
var cell2 = document.createElement("td");
cell2.setAttribute("style","border-right:1px solid #eae9e9;border-bottom:1px solid #eae9e9");
var cell3 = document.createElement("td");
cell3.setAttribute("id","response"+countLine);
cell3.setAttribute("style","border-bottom:1px solid #eae9e9;padding-left:20px");

var div_action = document.createElement("div");
var div_action2 = document.createElement("div");
row.setAttribute("id",countLine);
cell.innerHTML=countLine;

div_action.innerHTML+='<span id="addedfields_'+countLine+'" style="margin-left:30px"><a href="javascript:remove_Question('+countLine+');">Supprimer</a></span>';

cell2.appendChild(question);
cell2.appendChild(search);
cell2.appendChild(num);
cell2.appendChild(questiontypeid);
cell2.appendChild(div_action);

div_action2.innerHTML+='<span id="addedresponse_'+countLine+'"><a href="javascript:create_Response('+countLine+')">Ajouter</a></span>';

cell3.appendChild(div_action2);
cell3.innerHTML+='<br>';
cell3.appendChild(response1);
cell3.innerHTML+='<br>';
cell3.innerHTML+='<br>';
cell3.appendChild(response2);
cell3.innerHTML+='<br>';
cell3.innerHTML+='<br>';
cell3.appendChild(responseCount);


row.appendChild(cell);
row.appendChild(cell2);
row.appendChild(cell3);
body.appendChild(row);

//body.innerHTML += '<td><input type="text" name="toto"></td><td><input type="text" name="toto"></td>';
}

function remove_Question(varline) {
var t = document.getElementsByTagName("tbody")[0];
var l=document.getElementById(varline);
t.removeChild(l);
}


var checkflag = "false";
function check(field, msgSelect,msgNoSelect) {
if (checkflag == "false") {
  for (i = 0; i < field.length; i++) {
  field[i].checked = true;}
  checkflag = "true";
  return msgNoSelect; }
else {
  for (i = 0; i < field.length; i++) {
  field[i].checked = false; }
  checkflag = "false";
  return msgSelect; }
}


//to show/hide date selection on period report
function updatePeriod(form) {

	ob=document.getElementById(form+"_period");
	ob2=document.getElementById(form+"_dates");
	test=0;
	for (i =0; i < ob.options.length; i++) {
			if ((ob.options[i].selected) && (ob.options[i].value == 6)){
				test=1;
				Effect.Appear(ob2);
				//ob2.setAttribute("style","display:true;");
  			}	
	}
	if (test == 0)
	Effect.DropOut(ob2);
	//alert(test);
}
//to show/hide date selection on period report
	function displayClosed() {

	ob=document.getElementById("Action");
	ob3=document.getElementById("divActionOn");
	ob2=document.getElementById("divActionClosed");
	ob4=document.getElementById("divActionMail");
	test=0;
	test2=0;
	test3=0;
	
	//for (i =0; i < ob.options.length; i++) {
	
			//if ((ob.options[i].selected) && (ob.options[i].value < 0 )){
				test=1;
				Effect.Appear(ob2);
  			//}	
  			//if ((ob.options[i].selected) && ((ob.options[i].value == 5)||(ob.options[i].value == 6))){
			//	test2=1;
			//	Effect.Appear(ob2);
  			//}
  		//	if ((ob.options[i].selected) && (ob.options[i].value ==4 )){
		//		test3=1;
		//		Effect.Appear(ob2);
  		//	}
	//}

	if (test == 0)
	Effect.DropOut(ob2);
	if (test2 == 0)
	Effect.DropOut(ob3);
	if (test3 == 0)
	Effect.DropOut(ob4);
}

//to show/hide date selection on period report
function displayDate() {

	ob=document.getElementById("Action");
	ob3=document.getElementById("divActionOn");
	ob2=document.getElementById("divActionDate");
	ob4=document.getElementById("divActionMail");
	ob5=document.getElementById("divActionOn2");
	test=0;
	test2=0;
	test3=0;
	test4=0;
	
	for (i =0; i < ob.options.length; i++) {
			if ((ob.options[i].selected) && (ob.options[i].value == 2)){
				test=1;
				Effect.Appear(ob2);
				Effect.Appear(ob3);
  			}	
  			if ((ob.options[i].selected) && ((ob.options[i].value == 3)||(ob.options[i].value == 4))){
				test2=1;
				Effect.Appear(ob3);
  			}
  			if ((ob.options[i].selected) && (ob.options[i].value == 4)){
  				test4=1;
				Effect.Appear(ob5);
  			}
  			if ((ob.options[i].selected) && (ob.options[i].value ==1 )){
				test3=1;
				Effect.Appear(ob4);
  			}
	}

	if (test == 0) {
		Effect.DropOut(ob2);
	}
	if (test2 == 0 && test == 0) {
		Effect.DropOut(ob3);
	}
	if (test3 == 0) {
		Effect.DropOut(ob4);
	}
	if (test4 == 0) {
		Effect.DropOut(ob5);
	}
}

//to show/hide date selection on period report
function displaySourcingPack() {
	ob1=document.getElementById("CandidateEventTypeId4");
	ob2=document.getElementById("CandidateEventTypeId2");
	ob3=document.getElementById("CandidateEventTypeId3");
	ob=document.getElementById("divSPOn");
	test=0;
	if (ob3.checked){
		test=1;
		Effect.Appear(ob);
	}
	if (test == 0)
		Effect.DropOut(ob);
}

function updateUsers(n,m) {
i=1;
var valObjectives = 1;
var valLines = document.getElementById('ObjectiveLinesCount');
items = document.getElementsByTagName("tr");
if (valLines.value=="") valLines.value=items.length;

var countLine = ( parseInt(valObjectives)+ (items.length-parseInt(valLines.value))+1);
var i2 = i + 1;

//when user change search criterion
if (n==1){

	ob=document.getElementById("Objective"+m+"UserId");
	ob2=document.getElementById("ObjectiveUserId");
//	ob2.setAttribute("type","select");
	//ob=ob2;
	for (i =0; i < ob2.options.length; i++) {
		test=-1;
		for (j =0; j < ob.options.length; j++) {
			if (ob.options[j].value==ob2.options[i].value) test=j;
		}
 
		tmpOptions=ob2.options[i].cloneNode(true);
		if (test==-1) {
			//if (ob2.options[i].selected) {
				try {
   				ob.add(tmpOptions, null); // standards compliant; doesn't work in IE
  				}
 				catch(ex) {
    				ob.add(tmpOptions); // IE only
  				}
  			//}
		}else{
			if (!(ob2.options[i].selected)) {
				//ob.remove(test);
			}	
		}
	}
}

for (i =1; i < countLine; i++) {
	for (k = 1; k < countLine; k++) {
		if ((document.getElementById("Objective"+k+"SearchId").selectedIndex==document.getElementById("Objective"+i+"SearchId").selectedIndex)&&(i!=k)){
			selected = new Array(); 
			ob=document.getElementById("Objective"+i+"UserId");
			ob2=document.getElementById("Objective"+k+"UserId");
			//for (var j = 0; j < ob.options.length; j++) if (ob.options[j].selected) selected.push(ob.options[j].value);
			for (var j = 0; j < ob.options.length; j++)  selected.push(ob.options[j].value);

			for ( var l=0;l<selected.length;l++) {
				for ( var m=0;m<ob2.options.length;m++) {
					if (ob2.options[m].value==selected[l])   ob2.remove(m);
				}
			}

			if (ob2.options.length==0) {
			}
		} 
	}
}
}



//function to create a line in the task table(new task)
function create_Objective() {
i=1;
var valObjectives = 1;
var valLines = document.getElementById('ObjectiveLinesCount');
items = document.getElementsByTagName("tr");
if (valLines.value=="") valLines.value=items.length;

var countLine = ( parseInt(valObjectives)+ (items.length-parseInt(valLines.value))+1);
var i2 = i + 1;

var search = document.getElementById("Objective"+(countLine-1)+"SearchId").cloneNode(true);
if (search.options.length>1){
search.setAttribute("id","Objective"+countLine+"SearchId");
search.setAttribute("name","data[Objective]["+countLine+"][search_id]");
search.setAttribute("onChange","updateUsers(1,"+countLine+")");
//search.remove(document.getElementById("Objective"+(countLine-1)+"SearchId").selectedIndex);

var user = document.getElementById("Objective"+i+"UserId").cloneNode(true);
user.setAttribute("id","Objective"+countLine+"UserId");
user.setAttribute("name","data[Objective]["+countLine+"][user_id][]");

var cvp = document.getElementById("Objective"+i+"CVP").cloneNode(true);
cvp.setAttribute("id","Objective"+countLine+"CVP");
cvp.setAttribute("name","data[Objective]["+countLine+"][CVP]");

var acps = document.getElementById("Objective"+i+"ACPS").cloneNode(true);
acps.setAttribute("id","Objective"+countLine+"ACPS");
acps.setAttribute("name","data[Objective]["+countLine+"][ACPS]");

var pca = document.getElementById("Objective"+i+"PCA").cloneNode(true);
pca.setAttribute("id","Objective"+countLine+"PCA");
pca.setAttribute("name","data[Objective]["+countLine+"][PCA]");

var body = document.getElementsByTagName("tbody")[0];
var row = document.createElement("tr");
var cell = document.createElement("td");
var cell2 = document.createElement("td");
var cell3= document.createElement("td");
var cell4 = document.createElement("td");
var cell5= document.createElement("td");
var cell6 = document.createElement("td");

cell.appendChild(search);
cell2.appendChild(user);
cell3.appendChild(cvp);
cell4.appendChild(acps);
cell5.appendChild(pca);

row.appendChild(cell);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
row.appendChild(cell5);
row.appendChild(cell6);

body.appendChild(row);

//body.innerHTML += '<td><input type="text" name="toto"></td><td><input type="text" name="toto"></td>';
}
updateUsers(1,countLine);
}


//to add fields in a form
function create_champ(i,name,name2,label,radio,val1,val2,label1,label2) {
	var i2 = i + 1;
	if (radio) {
		document.getElementById('addedfields_'+i).innerHTML = '<table id="table_05"><tr><td><label for="'+name+'Op'+i+name2+'" >'+label1+'</label></td><td><input type="radio" value="'+val1+'" name="'+name+'Op'+i+name2+'"></td><td><label for="'+name+'Op'+i+name2+'" >'+label2+'</label></td><td><input type="radio" value="'+val2+'" name="'+name+'Op'+i+name2+'"></td></tr></table>';
		document.getElementById('addedfields_'+i).innerHTML += '<input type="text" name="'+name+'Value'+i+name2+'" style="width:300px"><br />';
	}else{
		document.getElementById('addedfields_'+i).innerHTML = '<input type="text" name="'+name+'Value'+i+name2+'"></span><br />';
	}
	document.getElementById('addedfields_'+i).innerHTML += (i <= 10) ? '<span id="addedfields_'+i2+'"><a href="javascript:create_champ('+i2+',\''+name+'\',\''+name2+'\',\''+label+'\',\''+radio+'\',\''+val1+'\',\''+val2+'\',\''+label1+'\',\''+label2+'\')">'+label+'</a></span>' : '';
	var body = document.getElementById("byFulltext");
	body.setAttribute("width", "200px");
}

//function to create a line in the task table(new task)
function create_line(i,label) {
var valProjects = document.getElementById('TaskProjectsCount');
var valLines = document.getElementById('TaskLinesCount');
items = document.getElementsByTagName("tr");
if (valLines.value=="") valLines.value=items.length;

var countLine = ( parseInt(valProjects.value)+ (items.length-parseInt(valLines.value))+1);
var i2 = i + 1;

var search = document.getElementById("Task"+i+"SearchId").cloneNode(true);
search.setAttribute("id","Task"+countLine+"SearchId");
search.setAttribute("name","data[Task]["+countLine+"][search_id]");

var projectId = document.getElementById("Task"+i+"ProjectId").cloneNode(true);
projectId.setAttribute("id","Task"+countLine+"ProjectId");
projectId.setAttribute("name","data[Task]["+countLine+"][project_id]");

var projectsUser = document.getElementById("Task"+i+"ProjectsUserId").cloneNode(true);
projectsUser.setAttribute("id","Task"+countLine+"ProjectsUserId");
projectsUser.setAttribute("name","data[Task]["+countLine+"][projects_user_id]");

var taskDate = document.getElementById("Task"+i+"TaskDate").cloneNode(true);
taskDate.setAttribute("id","Task"+countLine+"TaskDate");
taskDate.setAttribute("name","data[Task]["+countLine+"][task_date]");

var activity = document.getElementById("Task"+i+"ActivityId").cloneNode(true);
activity.setAttribute("id","Task"+countLine+"ActivityId");
activity.setAttribute("name","data[Task]["+countLine+"][activity_id]");

var project = document.getElementById("Task"+i+"ProjectName");
projectName = document.createTextNode(project.value);

var duration = document.getElementById("Task"+i+"Duration").cloneNode(true);
duration.setAttribute("id","Task"+countLine+"Duration");
duration.setAttribute("value","0");
duration.setAttribute("name","data[Task]["+countLine+"][duration]");
var labelDuration = document.createElement("label");
labelDuration.setAttribute("for","Task"+countLine+"Duration");
labelDuration.innerHTML += label;

var btn1 = document.createElement("input");
btn1.setAttribute("type","button");
btn1.setAttribute("onclick","javascript:increments('"+countLine+"',0,'toto',true)");
btn1.setAttribute("value","+");

var btn2 = document.createElement("input");
btn2.setAttribute("type","button");
btn2.setAttribute("onclick","javascript:decrements('"+countLine+"','toto')");
btn2.setAttribute("value","-");

var body = document.getElementsByTagName("tbody")[0];
var row = document.createElement("tr");
var cell = document.createElement("td");
var cell2 = document.createElement("td");
var cell3= document.createElement("td");
var cell4 = document.createElement("td");
var cell5= document.createElement("td");
var cell6 = document.createElement("td");
var cell7= document.createElement("td");



cell.appendChild(projectName);
cell.appendChild(projectId);
cell.appendChild(projectsUser);
cell.appendChild(taskDate);
cell2.appendChild(search);
cell3.appendChild(activity);
cell4.appendChild(labelDuration);
cell4.appendChild(duration);
//cell4.appendChild(btn1);
//cell4.appendChild(btn2);
row.appendChild(cell);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
row.appendChild(cell5);
row.appendChild(cell6);
row.appendChild(cell7);

body.appendChild(row);

	//body.innerHTML += '<td><input type="text" name="toto"></td><td><input type="text" name="toto"></td>';

}



function create_ObjectiveLine(i,label) {
var valProjects = document.getElementById('TaskProjectsCount');
var valLines = document.getElementById('TaskLinesCount');
items = document.getElementsByTagName("tr");
if (valLines.value=="") valLines.value=items.length;

var countLine = ( parseInt(valProjects.value)+ (items.length-parseInt(valLines.value))+1);
var i2 = i + 1;

var search = document.getElementById("Task"+i+"SearchId").cloneNode(true);
search.setAttribute("id","Task"+countLine+"SearchId");
search.setAttribute("name","data[Task]["+countLine+"][search_id]");

var projectId = document.getElementById("Task"+i+"ProjectId").cloneNode(true);
projectId.setAttribute("id","Task"+countLine+"ProjectId");
projectId.setAttribute("name","data[Task]["+countLine+"][project_id]");

var projectsUser = document.getElementById("Task"+i+"ProjectsUserId").cloneNode(true);
projectsUser.setAttribute("id","Task"+countLine+"ProjectsUserId");
projectsUser.setAttribute("name","data[Task]["+countLine+"][projects_user_id]");

var taskDate = document.getElementById("Task"+i+"TaskDate").cloneNode(true);
taskDate.setAttribute("id","Task"+countLine+"TaskDate");
taskDate.setAttribute("name","data[Task]["+countLine+"][task_date]");

var activity = document.getElementById("Task"+i+"ActivityId").cloneNode(true);
activity.setAttribute("id","Task"+countLine+"ActivityId");
activity.setAttribute("name","data[Task]["+countLine+"][activity_id]");

var project = document.getElementById("Task"+i+"ProjectName");
projectName = document.createTextNode(project.value);

var duration = document.getElementById("Task"+i+"Duration").cloneNode(true);
duration.setAttribute("id","Task"+countLine+"Duration");
duration.setAttribute("value","0");
duration.setAttribute("name","data[Task]["+countLine+"][duration]");
var labelDuration = document.createElement("label");
labelDuration.setAttribute("for","Task"+countLine+"Duration");
labelDuration.innerHTML += label;

var btn1 = document.createElement("input");
btn1.setAttribute("type","button");
btn1.setAttribute("onclick","javascript:increments('"+countLine+"',0,'toto',true)");
btn1.setAttribute("value","+");

var btn2 = document.createElement("input");
btn2.setAttribute("type","button");
btn2.setAttribute("onclick","javascript:decrements('"+countLine+"','toto')");
btn2.setAttribute("value","-");

var body = document.getElementsByTagName("tbody")[0];
var row = document.createElement("tr");
var cell = document.createElement("td");
var cell2 = document.createElement("td");
var cell3= document.createElement("td");
var cell4 = document.createElement("td");
var cell5= document.createElement("td");
var cell6 = document.createElement("td");
var cell7= document.createElement("td");



cell.appendChild(projectName);
cell.appendChild(projectId);
cell.appendChild(projectsUser);
cell.appendChild(taskDate);
cell2.appendChild(search);
cell3.appendChild(activity);
cell4.appendChild(labelDuration);
cell4.appendChild(duration);
cell4.appendChild(btn1);
cell4.appendChild(btn2);
row.appendChild(cell);
row.appendChild(cell2);
row.appendChild(cell3);
row.appendChild(cell4);
row.appendChild(cell5);
row.appendChild(cell6);
row.appendChild(cell7);

body.appendChild(row);

	//body.innerHTML += '<td><input type="text" name="toto"></td><td><input type="text" name="toto"></td>';

}





//function to increment duration task value
function increments(i,j,message,view) {
var task_val = document.getElementById('Task'+i+'Duration');
task_val.value = parseFloat(task_val.value)+0.25;
var task_tot =0;

var valProjects = document.getElementById('TaskProjectsCount');
var valLines = document.getElementById('TaskLinesCount');
items = document.getElementsByTagName("tr");
if (valLines.value=="") valLines.value=items.length;


var countLine = ( parseInt(valProjects.value)+ (items.length-parseInt(valLines.value)));


if (view){

	for (k=1; k<(countLine+1); k++) {

	task_tot =parseFloat(task_tot) + parseFloat(document.getElementById('Task'+parseInt(k)+'Duration').value);

	}
}else{

	while(i>5){
		i=i-5;
	}

	for (k=0; k<(countLine); k++) {
		task_tot =parseFloat(task_tot) + parseFloat(document.getElementById('Task'+parseInt(parseInt(i)+k*5)+'Duration').value);
	}
}

if (task_tot>1){
	task_val.value= parseFloat(task_val.value)-0.25;
	alert(message);
}

}

//function to decrement duration task value
function decrements(i,message) {
var task_val = document.getElementById('Task'+i+'Duration');
task_val.value = parseFloat(task_val.value)-0.25;

if (parseFloat(task_val.value)<0){
	task_val.value= parseFloat(task_val.value)+0.25;
	alert(message);
}





}
