function getXMLHTTP()
{
	var xhr=null;
	
	if(window.XMLHttpRequest){ // Firefox et autres
		xhr = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){ // Internet Explorer
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e1) {
				xhr = null;
			}
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
	}	
	return xhr;
}

function show_or_hide(to_hide, flag, to_show)
{
	var count;
	var cont;
	var res = 0;
	
	if (flag == 0)
	{
		if (to_show != '')
		{
			to_show = to_show.replace(/\|/gi, '"');
			array_id = eval(to_show);
			for (count = 0; count < array_id.length; count++)
				if (document.getElementById("rep_id_"+array_id[count]).checked == true)
					res++;
		}
		if (res == 0)
		{
			to_hide = to_hide.replace(/\|/gi, '"');
			array_id = eval(to_hide);
			for (count = 0; count < array_id.length; count++)
			{
				if (document.getElementById(array_id[count]).style.display != "none")
				{
					document.getElementById(array_id[count]).style.display = "none";
					document.getElementById('margin_' + array_id[count]).style.display = "none";
					for (cont = 0; document.getElementById(array_id[count] + '_' + cont); cont++)
						document.getElementById(array_id[count] + '_' + cont).style.display = "none";
				}
			}
		}
	}
	else if (flag == 1)
	{
		to_show = to_show.replace(/\|/gi, '"');
		array_id = eval(to_show);
		for (count = 0; count < array_id.length; count++)
		{
			if (document.getElementById(array_id[count]).style.display == "none")
			{
				document.getElementById(array_id[count]).style.display = "";
				document.getElementById('margin_' + array_id[count]).style.display = "";
				for (cont = 0; document.getElementById(array_id[count] + '_' + cont); cont++)
					document.getElementById(array_id[count] + '_' + cont).style.display = "";
			}
		}
	}
}

function insert_question_change_reponse_isParent(id_select_question, id_select_reponse)
{
	var json_res;
	var i;
	var tmp;
	var object_sel_question = document.getElementById(id_select_question);
	var object_sel_reponse = document.getElementById(id_select_reponse);
	var id_question = object_sel_question.options[object_sel_question.selectedIndex].value;
	if (id_question > 0)
	{
		var xhr = getXMLHTTP();
		xhr.open("GET", "./src/ajax.php?action=retrieve_reponses&id_question="+id_question, true);
		xhr.onreadystatechange = function()
		{
			if(xhr.readyState == 4)
			{
				while(object_sel_reponse.options.length > 0)
					object_sel_reponse.options[0] = null;
				json_res = eval(xhr.responseText);
				for(i=0 ; i < json_res.length; i++)
				{
						tmp = json_res[i][1];
					json_res[i][1] = tmp.replace(/\|/gi, "'");
					object_sel_reponse.options[object_sel_reponse.options.length] = new Option(json_res[i][1],json_res[i][0]);
				}
			}
		}
		xhr.send(null);
	}
	else
	{
		while(object_sel_reponse.options.length > 0)
			object_sel_reponse.options[0] = null;
		object_sel_reponse.options[object_sel_reponse.options.length] = new Option("Aucune", 0);
	}
}

function delete_question_id(id_select_qcm, id_select_question)
{
	var object_sel_qcm = document.getElementById(id_select_qcm);
	var object_sel_question = document.getElementById(id_select_question);
	var id_qcm = object_sel_qcm.options[object_sel_qcm.selectedIndex].value;
	var xhr = getXMLHTTP();
	xhr.open("GET", "./src/ajax.php?action=retrieve_questions&id_qcm="+id_qcm, true);
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
		{
			while(object_sel_question.options.length > 0)
				object_sel_question.options[0] = null;
			object_sel_question.options[object_sel_question.options.length] = new Option("Aucune", 0);
			json_res = eval(xhr.responseText);
			for(i = 0 ; i < json_res.length; i++)
			{
				tmp = json_res[i][1];
				json_res[i][1] = tmp.replace(/\|/gi, "'");
				object_sel_question.options[object_sel_question.options.length] = new Option(json_res[i][1],json_res[i][0]);
			}
		}
	}
	xhr.send(null);
}

function insert_question_change_id_isParent(id_select_qcm, id_select_question, id_select_reponse, id_select_parent)
{
	var object_sel_qcm = document.getElementById(id_select_qcm);
	var object_sel_question = document.getElementById(id_select_question);
	var object_sel_reponse = document.getElementById(id_select_reponse);
	var object_sel_parent = document.getElementById(id_select_parent);
	var id_qcm = object_sel_qcm.options[object_sel_qcm.selectedIndex].value;
	var xhr = getXMLHTTP();
	xhr.open("GET", "./src/ajax.php?action=retrieve_questions&id_qcm="+id_qcm, true);
	xhr.onreadystatechange = function()
	{
		if(xhr.readyState == 4)
		{
			while(object_sel_question.options.length > 0)
				object_sel_question.options[0] = null;
			object_sel_question.options[object_sel_question.options.length] = new Option("Aucune", 0);
			while(object_sel_parent.options.length > 0)
				object_sel_parent.options[0] = null;
			object_sel_parent.options[object_sel_parent.options.length] = new Option("Aucune", 0);
			while(object_sel_reponse.options.length > 0)
				object_sel_reponse.options[0] = null;
			object_sel_reponse.options[object_sel_reponse.options.length] = new Option("Aucune", 0);
			json_res = eval(xhr.responseText);
			for(i = 0 ; i < json_res.length; i++)
			{
				tmp = json_res[i][1];
				json_res[i][1] = tmp.replace(/\|/gi, "'");
				object_sel_question.options[object_sel_question.options.length] = new Option(json_res[i][1],json_res[i][0]);
				object_sel_parent.options[object_sel_parent.options.length] = new Option(json_res[i][1],json_res[i][0]);
			}
		}
	}
	xhr.send(null);
}

function disp_on_type(flag, id_select_question, id_select_reponse, id_select_parent)
{
	if (flag == 1)
	{
		document.getElementById(id_select_question).disabled = false;
		document.getElementById(id_select_reponse).disabled = false;
		document.getElementById(id_select_parent).disabled = false;
	}
	else
	{
		var id_question;
		id_question = document.getElementById(id_select_question);
		id_question.disabled = true;
		var id_reponse;
		id_reponse = document.getElementById(id_select_reponse);
		id_reponse.disabled = true;
		var id_parent;
		id_parent = document.getElementById(id_select_parent);
		id_parent.disabled = true;
		id_parent[0].selected = 'selected';
		id_question[0].selected = 'selected';
		id_reponse[0].selected = 'selected';
	}
}

function aff_day(nb_days, lang)
{
	var count;
	if (lang == 'fr'){
		document.write('<option value="-1">Jour :</option>');
	}
	if (lang == 'en' || lang == 'us'){
		document.write('<option value="-1">Day :</option>');
	}
	for (count = 1; count <= nb_days; count++){
		if (count < 10){
			document.write('<option value="0'+count+'">'+count+'</option>');
		}
		else{
			document.write('<option value="'+count+'">'+count+'</option>');
		}
	}
}

function aff_month(lang)
{
	var count;
	if (lang == "fr"){
		month = new Array("janvier", "f&eacute;vrier", "mars", "avril", "mai", "juin", "juillet", "ao&ucirc;t", "septembre", "octobre", "novembre", "d&eacute;cembre");
		document.write('<option value="-1">Mois :</option>');
	}
	if (lang == 'en' || lang == 'us'){
		month = new Array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
		ocument.write('<option value="-1">Month :</option>');
	}
	for (count = 1; count <= month.length; count++){
		if (count < 10){
			document.write('<option value="0'+count+'">'+month[count - 1]+'</option>');
		}
		else{
			document.write('<option value="'+count+'">'+month[count -1]+'</option>');
		}
	}
}

function aff_year(begin, end, lang)
{
	if (lang == 'fr'){
		document.write('<option value="-1">Ann&eacute;e :</option>');
	}
	else if (lang == 'en' || lang == 'us'){
		document.write('<option value="-1">Year :</option>');
	}
	if (begin <= end){
		for (; begin <= end; begin++){
			document.write('<option value="'+begin+'">'+begin+'</option>');
		}
	}
	else{
		for (; begin >= end; begin--){
			document.write('<option value="'+begin+'">'+begin+'</option>');
		}
	}
}

function test_if_bissectile_year(year)
{
	if(eval(year%400)==0)
		return  (1);
	if(year % 100 == 0)
		return (0);
	if(year % 4 == 0)
		return (1);
	return (0);
}

function calc_nb_day_month(month, year)
{
	if (month == 4 || month == 6 || month == 9 || month == 11)
		return (30);
	else if (month == 2)
		{
			if ((year < 0) || (test_if_bissectile_year(year) == 1)){
				return (29);
			}
			else
				return (28);
		}
	else if (month > 12 || month < 1)
		return (-1);
	else
		return (31);
}

function update_day(sd, vd, vm, vy)
{
	var nb_day = calc_nb_day_month(vm, vy);
	if (nb_day < 0)
		return (-1);
	if (sd.options.length < (nb_day + 1)){
		while (sd.options.length <= (nb_day + 1))
			sd.options[sd.options.length] = new Option(sd.options.length,sd.options.length);
	}
	if (sd.options.length > (nb_day + 1)){
		while (sd.options.length > (nb_day + 1) )
			{
				sd.options[sd.options.length] = null;
				sd.options.length -= 1;
			}
		sd.options.length = nb_day + 1;
	}
	if (vd > nb_day){
		sd.selectedIndex = 0;
		return (-1);
	}
}

function check_select_date(id_day, id_month, id_year, flag)
{
	var sd = document.getElementById(id_day);
	var sm = document.getElementById(id_month);
	var sy = document.getElementById(id_year);
	var vd = sd.options[sd.selectedIndex].value;
	var vm = sm.options[sm.selectedIndex].value;
	var vy = sy.options[sy.selectedIndex].value;
	
	update_day(sd, vd, vm, vy);
	if (flag == 2){
		if (vy == -1){
			sd.selectedIndex = 0;
			sd.disabled = true;
			sm.selectedIndex = 0;
			sm.disabled = true;
		}
		else{
			sm.disabled = false;
		}
	}
	else if (flag == 1){
		if (vm == -1){
			sd.selectedIndex = 0;
			sd.disabled = true;
		}
		else{
			sd.disabled = false;
		}
	}
}
