

var baseDir = "http://vfportail.com//";


Array.prototype.inArray = function(val) {
	lt = this.length;
	for(var a2 = 0; a2 < lt; ++a2) {if(this[a2] == val){return true;}}
	return false;
};

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	le = elem.length;
	for (var i = 0; i < le; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
};

function verif_form(form) {
	return true;
	valid = true;
	lf = form.length;
	for(var i = 0; i < lf; i++) {
		if(form[i].value == "") {
			valid = false;
		}
	}
	if(!valid) {
		alert("Ce formulaire doit être complètement rempli.");
		return false;
	}
	else {
		return true;
	}
}

function confirmation(msg) {
	if(confirm(msg)) {return true;} else {return false;}
}

function confirm_supp() {
	if(confirm("La suppression est définitive.\nContinuer ?")) {return true;} else {return false;}
}

function showElm(id) {
	if($(id).style.display != "block") {$(id).style.display = "block";}
	else {$(id).style.display = "none";}
}

function showElmCarte(id,id2,id3){
	if($(id).style.display != "block") {
		$(id).style.display = "block";
		if($(id2)){
			$(id2).style.display = "none";
		}
		if($(id3)){
			$(id3).style.display = "none";
		}
	}
	else {
		$(id).style.display = "none";
	}
}

function showHelp() {
	collection = document.getElementsByClassName('help');
	lc = collection.length;
	for(var i = 0; i < lc; i++) {
		if(collection[i].style.display != "block") {collection[i].style.display = "block";}
		else {collection[i].style.display = "none";}
	}
}

function showTab(bt,id) {
	collection = document.getElementsByClassName('box');
	tabs = document.getElementsByClassName('bt_tab');
	// Réinitialisation de chaque élément
	lc = collection.length;
	lt = tabs.length;
	for(var i = 0; i < lc; i++) {collection[i].style.display = "none";}
	for(var j = 0; j < lt; j++) {
		tabs[j].style.backgroundPosition = "0 0";
		tabs[j].style.borderBottom = "solid #CCC 1px";
	}
	// Mise en évidence des éléments concernés
	$(id).style.display = "block";
	bt.style.backgroundPosition = "0 -22px";
	bt.style.borderBottom = "solid #FEFEEE 1px";
}

function detect_xhr() {
	var xhr_object = null;
	if(window.XMLHttpRequest) { // Firefox
		xhr_object = new XMLHttpRequest();
		return xhr_object;
	}
	else if(window.ActiveXObject) { // Internet Explorer
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
		return xhr_object;
	}
	else { // XMLHttpRequest non supporté par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return;
	}
}

function envoie(lien,post) {
	
	xhr_object = detect_xhr();
		
	xhr_object.open("POST",baseDir+lien,true);
	
	xhr_object.onreadystatechange = function() {
		if(xhr_object.readyState == 3) {
			$('msg_info').innerHTML = "<p>Enregistrement...</p>";
		}
		if(xhr_object.readyState == 4) {
			$('msg_info').innerHTML = "";
		}
	}
	
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr_object.send(post);
}
function maj(table,name,value,id) {
	var data = "action=update&table="+table+"&"+name+"="+value+"&id="+id;
	envoie("ajax/save.php",data);
}
function maj_old(name,value,id) {
	var data = "action=update&table=carte&"+name+"="+value+"&id="+id;
	envoie("ajax/save.php",data);
}
function active_carte_addon(id,active) {
	var data = "action=active_carte_addon&active="+active+"&id="+id;
	envoie("ajax/save.php",data);
}
function save_liste(name,id) {
	var data = 'id='+id+'&table='+name;
	var dataArr = new Array();
	List = $(name);
	lt = List.length;
	for(var j = 0; j < lt; j++) {
		if(List.options[j].selected == true) {
			dataArr.push(name+'[]='+List.options[j].value);
		}
	}
	data += '&'+dataArr.join('&');
	
	envoie("ajax/save_list.php",data);
}

function save_departement(obj,departements,id) {
	var data = "table=departement&departements="+departements+"&value="+obj.checked+"&id="+id;
	envoie("ajax/save_list.php",data);
}


function save_rubrique(obj,rubrique,id) {
	var data = "table=rubrique&id_rubrique="+rubrique+"&value="+obj.checked+"&id="+id;
	envoie("ajax/save_list.php",data);
}

function save_utilisateur(name,value,id) {
	var data = "action=update&table=utilisateur&"+name+"="+value+"&id="+id;
	envoie("ajax/save.php",data);
}




function add_rubrique_elm() {
	xhr_object = detect_xhr();
	value = prompt("Entrez le nom du nouveau thème :");
	if(value == null || value == "") {return false;}
	else {
		xhr_object.open("POST",baseDir+"ajax/save.php",true);
		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 3) {$('msg_info').innerHTML = "Enregistrement...";}
			if(xhr_object.readyState == 4) {
				$('msg_info').innerHTML = "";
				id = xhr_object.responseText;
				var elm = document.createElement("li");
				elm.setAttribute("id","item_"+id);
				var inp = document.createElement("input");
				inp.setAttribute("type", "text");
				inp.setAttribute("name", id);
				inp.setAttribute("id", id);
				inp.setAttribute("value", value);
				inp.setAttribute("onblur","save_rubrique('"+id+"',this.value);");
				var del = document.createElement("input");
				del.setAttribute("type", "image");
				del.setAttribute("src","img/cross.png");
				del.setAttribute("onclick","return delete_rubrique('"+id+"');");
				$('rubriques_list').appendChild(elm);
				$("item_"+id).appendChild(inp);
				$("item_"+id).appendChild(del);
				Sortable.create('rubriques_list');
				return true;
			}
		}
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = "table=rubrique&"+"action=add&nom_rubrique="+value;
		xhr_object.send(data);
	}
}

function save_ordre_rubrique(str) {
	var data = str;
	envoie("ajax/save_ordre_rubrique.php",data);
}

function delete_rubrique(id) {
	if(!confirm("Supprimer cette rubrique ?")) {
		return false;
	}
	else {
		xhr_object = detect_xhr();
		xhr_object.open("POST",baseDir+"ajax/save.php",true);
		xhr_object.onreadystatechange = function() {
			if(xhr_object.readyState == 3) {$('msg_info').innerHTML = "Enregistrement...";}
			if(xhr_object.readyState == 4) {
				$('msg_info').innerHTML = "";
				if(xhr_object.responseText == "DONE") {
					to_del = $('item_'+id);
					$('rubriques_list').removeChild(to_del);
				}
			}
		}
		xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var data = "table=rubrique&"+"action=delete&id="+id;
		xhr_object.send(data);
	}
}

function set_domaine_rubriques(id_domaine) {
	var data = "table=domaine&action=update&id="+id_domaine;
	var domaine_rubriques = document.getElementsByClassName('domaine_rubriques');
	data += "&rubriques="
	var arr = new Array();
	var l = domaine_rubriques.length;
	for(var i = 0; i < l; i++) {
		if(domaine_rubriques[i].checked) {
			arr.push(domaine_rubriques[i].value);
		}
	}
	data += arr.join(',');


	envoie("ajax/save.php",data);
}

function toInvisible(fromList,toList,selection) {
	var fromList = $(fromList);
	var toList = $(toList);
	var lf = fromList.length;
	var lt = toList.length;
	for(var i = 0; i < lf; i++) {
		if(fromList.options[i].selected == true) {
			for(var j = 0; j < lt; j++) {
				if(fromList.options[i].value == toList.options[j].value) {
					toList.options[j].selected = selection;
				}
			}
		}
	}	
}

function refreshList(list1,list2,list3) {
	var list1 = $(list1);
	var list2 = $(list2);
	var list3 = $(list3);
	var l3 = list3.length;
	list1.innerHTML = '';
	list2.innerHTML = '';
	for(var i = 0; i < l3; i++) {
		if(list3.options[i].selected == true) {
			addOption(list1,list3.options[i]);
		}
		else {
			addOption(list2,list3.options[i]);
		}
	}
}

function transfer_list_multiple(listFrom, listTo){
	var fromList=$(listFrom);
	var toList = $(listTo);
	while (fromList.selectedIndex != -1){
		addOption(toList,fromList.options[fromList.selectedIndex]);
		if(document.all) { // Internet Explorer
			fromList.options.remove(fromList.selectedIndex);
		}
		else { // Les autres
			fromList.options[fromList.selectedIndex] = null;
		}
	}
}

function addOption(list, option){
	list.options[list.options.length]=new Option(option.innerHTML,option.value);
}

function addfileinput(file_list,id_file) {
	id = $(id_file).value;
	var elm = document.createElement("li");
	var inp = document.createElement("input");
	inp.setAttribute("type", "file");
	inp.setAttribute("name", "file_"+id);
	$(file_list).appendChild(elm);
	elm.appendChild(inp);
	id = parseInt(id)+1;
	$(id_file).value = id;
}

	
function redirection(url) {
	window.location.replace(url);
}
