var isWorking = false;        // est-ce que le canal est occupé

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
      } catch (e) {
      xmlhttp = false;
      }
  }
  return xmlhttp;
}

var http = getHTTPObject();

function _remote(serveur, esclave) {
	if (!isWorking && http) {
		http.open("GET", serveur, true);
		http.onreadystatechange = esclave;
		isWorking = true;
		http.send(null);
	}
}


// Vérifier si le membre existe dans la BD
function remote_numero(numero) {
	if (numero.length>4)
		_remote("/include/remote.php?fonc=remote_numero&abr=" + escape(numero), _remote_numero);
}
function _remote_numero() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('numero valide') != -1) {
			document.formulaire.etape.value = 'annonce';
			document.formulaire.submit();
		}
		else {
			alert("Le numéro de membre est introuvable.\nVeuillez vérifiez et essayez de nouveau.");
		}

		isWorking = false;
	}
}

// Vérifier si le courriel existe dans la BD
function remote_courriel(user) {
	if (user.length>2)
		_remote("/include/remote.php?fonc=remote_courriel&abr=" + escape(user), _remote_courriel);
}
function _remote_courriel() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('existe') == -1) {
			document.formulaire.submit();
		}
		else {
			alert('Ce courriel se trouve dans la base de données.');
		}

		isWorking = false;
	}
}

// Langues
function remote_langues(contenu) {
	if (contenu != '') {
		_remote("include/remote.php?fonc=remote_langues&abr=" + contenu, _remote_langues);
	}
}
function _remote_langues() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('retour vide') == -1) {
			contenu = http.responseText.split(',');
			document.getElementById('langues_txt').innerHTML = '';
			for (i=0; i < contenu.length; i++)
				document.getElementById('langues_txt').innerHTML += contenu[i] + '<br>';
		}
		isWorking = false;
	}
}

// Liste des section d'un menu
function remote_produits(code) {
	if (code != '' && code > 0)
		_remote("include/remote.php?fonc=remote_produits&abr=" + code, _remote_produits);
}
function _remote_produits() {
	if (http.readyState == 4) {
		if (http.responseText != '') {
			document.getElementById('divTabs').innerHTML = http.responseText;
		}

		isWorking = false;
	}
}

// Liste des questions, au complet
function remote_questionsTotal() {
	_remote("/include/remote.php?fonc=remote_questionsTotal", _remote_questionsTotal);
}
function _remote_questionsTotal() {
	if (http.readyState == 4) {
		totaux['categories'] = 0;
		totaux['questions'] = 0;
		if (http.responseText != '' && http.responseText != 'VIDE') {
			contenu = http.responseText.split('__');
			parent_id = '';
			long = 0;
			long2 = 0;
			for (i=0; i < contenu.length; i++) {
				contenu2 = contenu[i].split('|');
				if (contenu2[1] != parent_id) {
					parent_id = contenu2[1];
					questionsTotal[parent_id] = Array();
					totaux['categories']++;
					categories[categories.length] = Array(contenu2[1], contenu2[0]);
				}
				queue[queue.length] = Array(contenu2[1], contenu2[0]);
				long2 = questionsTotal[parent_id].length;
				questionsTotal[parent_id][long2] = Array(contenu2[0], contenu2[2], contenu2[3], contenu2[4], contenu2[5], contenu2[6], contenu2[7]);
				totaux['questions']++;
			}

			genererQuestions();
			setTimeout("init2();", 300);
		}
		isWorking = false;
	}
}

// Enregister les données
function enregistrer_reponse(rep, val1, val2, val3, val4, _id) {
	serveur = '/include/remote.php?fonc=enregistrer_reponse&id=' + _id + '&rep=' + rep + '&val1=' + val1 + '&val2=' + val2 + '&val3=' + val3 + '&val4=' + val4;
	if (!isWorking && http) {
		http.open("GET", serveur, false);
		isWorking = true;
		http.send(null);
	}
	isWorking = false;
}

// Liste des questions par catégorie
function remote_questions(code) {
	if (code != '' && code > 0)
		_remote("/include/remote.php?fonc=remote_questions&abr=" + code, _remote_questions);
}
function _remote_questions() {
	if (http.readyState == 4) {
		if (http.responseText != '') {
			document.getElementById('questions').innerHTML = http.responseText;
		}

		isWorking = false;
	}
}

// Contenu d'une question
function remote_contenu(code,_id) {
	if (code != '' && code > 0)
		_remote("/include/remote.php?fonc=remote_contenu&abr=" + code + '&id=' + _id, _remote_contenu);
}
function _remote_contenu() {
	if (http.readyState == 4) {
		if (http.responseText != '') {
			d = document.formulaire;
			contenu = http.responseText.split('|');
			document.getElementById('numero').innerHTML = contenu[0];
			document.getElementById('description_id').innerHTML = contenu[1];
			document.getElementById('nom').innerHTML = contenu[2];
			document.getElementById('question').innerHTML = contenu[3];
			d.etude.value = contenu[4];
			d.travail.value = contenu[5];
			d.formation.value = contenu[6];
			d.equivalant.value = contenu[7];
		}

		isWorking = false;
	}
}

// Liste des Provinces
function provinces_liste() {
	ch = document.formulaire.province.value;
	
	if (ch.length>2)
		_remote("/include/remote.php?fonc=provinces_liste&abr=" + escape(ch), _provinces_liste);

	if ((ch.length<3) && (document.getElementById('res_provinces').style.visibility == 'visible'))
		document.getElementById('res_provinces').style.visibility = 'hidden';
}
function _provinces_liste() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			var res = xmlDocument.getElementsByTagName('name').item(0).firstChild.data;
			var elements = res.split(";");
			var result = '';
			if (elements[0] != '') {
				for (var i=0;i<elements.length;i++) {
					result = result + "<a style=\"text-decoration:none;color:red;\" href=\"javascript:setQuery_province('" + elements[i].replace("'","\\'") + "');\">" + elements[i] + "</a><br />";
				}
				document.getElementById('res_provinces').innerHTML = '<div style="width:245px; height:16px; line-height:16px; background-color:#0044BB; color:#FFFFFF; text-align:right; border:#CCCCCC thin solid; padding-right:5px;"> <a href="javascript:fermer_popup(\'res_provinces\')" style="color:#FFFFFF; text-decoration:none;">X</a> </div><div style="text-align:left; background-color:#F5F5F5; width:250px; height:90px; overflow:auto; overflow-x:none;">' + result + '</div>';
				document.getElementById('res_provinces').style.visibility = 'visible';
			}
			else document.getElementById('res_provinces').style.visibility = 'hidden';

			isWorking = false;
		}
	}
}
function setQuery_province(ch){
    document.getElementById('province').value = ch;
    document.getElementById('res_provinces').style.visibility = 'hidden';
	document.getElementById('province').focus();
}

// Liste des Villes
function villes_liste() {
	ch = document.formulaire.ville.value;
	
	if (ch.length>2)
		_remote("/include/remote.php?fonc=villes_liste&abr=" + escape(ch), _villes_liste);

	if ((ch.length<3) && (document.getElementById('res_villes').style.visibility == 'visible'))
		document.getElementById('res_villes').style.visibility = 'hidden';
}
function _villes_liste() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			var xmlDocument = http.responseXML;
			var res = xmlDocument.getElementsByTagName('name').item(0).firstChild.data;
			var elements = res.split(";");
			var result = '';
			if (elements[0] != '') {
				for (var i=0;i<elements.length;i++) {
					result = result + "<a style=\"text-decoration:none;color:red;\" href=\"javascript:setQuery_ville('" + elements[i].replace("'","\\'") + "');\">" + elements[i] + "</a><br />";
				}
				document.getElementById('res_villes').innerHTML = '<div style="width:245px; height:16px; line-height:16px; background-color:#0044BB; color:#FFFFFF; text-align:right; border:#CCCCCC thin solid; padding-right:5px;"> <a href="javascript:fermer_popup(\'res_villes\')" style="color:#FFFFFF; text-decoration:none;">X</a> </div><div style="text-align:left; background-color:#F5F5F5; width:250px; height:90px; overflow:auto; overflow-x:none;">' + result + '</div>';
				document.getElementById('res_villes').style.visibility = 'visible';
			}
			else document.getElementById('res_villes').style.visibility = 'hidden';

			isWorking = false;
		}
	}
}
function setQuery_ville(ch){
    document.getElementById('ville').value = ch;
    document.getElementById('res_villes').style.visibility = 'hidden';
	document.getElementById('ville').focus();
}
function fermer_popup(obj) {
	if ( document.getElementById(obj) )
		document.getElementById(obj).style.visibility='hidden';
}
function vider_boites() {
	document.getElementById('recherche_resultat').innerHTML = '';
	document.getElementById('recherche_fiche').innerHTML = '';
}


// Recherche de membres
function remote_recherche_public(obj) {
	post = faire_POST(obj);
	document.getElementById('recherche_fiche').innerHTML = '';
	_remote("/include/remote.php?fonc=remote_recherche_public" + post, _remote_recherche_public);
}
function _remote_recherche_public() {
	if (http.readyState == 4) {
		if (http.responseText != '') {
			document.getElementById('recherche_resultat').innerHTML = http.responseText;
		}
		else document.getElementById('recherche_resultat').innerHTML = '';

		isWorking = false;
	}
}

// Recherche de membres - Fiche
function remote_recherche_fiche(_id) {
	_remote("/include/remote.php?fonc=remote_recherche_fiche&id=" + _id, _remote_recherche_fiche);
}
function _remote_recherche_fiche() {
	if (http.readyState == 4) {
		if (http.responseText != '') {
			document.getElementById('recherche_fiche').innerHTML = http.responseText;
		}
		else document.getElementById('recherche_fiche').innerHTML = '';

		isWorking = false;
	}
}

// Recherche de laboratoire - Fiche
function remote_laboratoire_fiche(_id) {
	_remote("/include/remote.php?fonc=remote_laboratoire_fiche&id=" + _id, _remote_laboratoire_fiche);
}
function _remote_laboratoire_fiche() {
	if (http.readyState == 4) {
		if (http.responseText != '') {
			document.getElementById('recherche_fiche').innerHTML = http.responseText;
		}
		else document.getElementById('recherche_fiche').innerHTML = '';

		isWorking = false;
	}
}


///////////////////////////////////////////////
// Fonctions et classes
function remplacer(source,varIN,varOUT) {
	retour = '';
	for (i=0; i < source.length; i++) {
		s = source.substr(i,1);
		if (s == varIN) retour += varOUT;
		else retour += s;
	}
	
	return retour;
}

function number_format(valeur) {
	result = Math.floor(valeur) + ".";
	cents = 100*(valeur-Math.floor(valeur))+0.5;
	result += Math.floor(cents/10);
	result += Math.floor(cents%10);
	return result;
}

function faire_POST(obj) {
	post = '';
	for (i=0; i < obj.elements.length; i++) {
		if (obj.elements[i].name && obj.elements[i].name != '') {
			if (obj.elements[i].getAttribute('type') == 'radio' || obj.elements[i].getAttribute('type') == 'checkbox') {
				if (obj.elements[i].checked == true) post += '&' + obj.elements[i].name + '=' + escape(obj.elements[i].value);
			}
			else
				post += '&' + obj.elements[i].name + '=' + escape(obj.elements[i].value);
		}
	}
	
	return post;
}
