//si valeur est dans le tableau "tableau", on retourne true, sinon false
function in_array(valeur, tableau){
	var inArray = false;
	
	for(i=0; i<tableau.length; i++){
		if(tableau[i] == valeur){
			inArray = true;
		}
	}
	return inArray;
}


//ces 2 fonctions permettent de retourner la position d'un objet
//il faut appeler get_full_position en lui passant l'id de l'objet
function get_offset(elem, pos)
{
    var offset = 0;
    while(elem) {
        offset += elem[pos];
        elem = elem.offsetParent;
    }
    return offset;
}

//voir ^
function get_full_position(id)
{
    var d = document.getElementById(id);
    var offsets = new Object;
    offsets.top 	= get_offset(d, "offsetTop");
    offsets.left 	= get_offset(d, "offsetLeft");
    offsets.width 	= d.clientWidth;
    offsets.height 	= d.clientHeight;
    return offsets;
}


//supprime le noeud definit par nodeId
function removeNode(nodeId){
	if (document.getElementById(nodeId)){
		document.getElementById(nodeId).parentNode.removeChild(document.getElementById(nodeId));
	}	
}


//netoie un noeud definit par l'ID nodeId 
function clearNode(nodeId){
	if (document.getElementById(nodeId)){
		var node = document.getElementById(nodeId);
		if(node.hasChildNodes()){
			var liste = node.childNodes;		
			//alert(liste[0]);
			for (var i=liste.length-1; i>=0; i--) document.getElementById(nodeId).removeChild(liste[i]);
		}
	}else alert("Auncun element détecté avec l'id "+nodeId);	
}


//retourne la chaine de caractere passée en paramètre avec max caratère suivi de ...
function stringCut(str, max){
	
	if(str.length>max){
		//on recherche le premier espace avant MAX caractères
		str = str.substring(0, str.indexOf(" ",max));
		str = str.substring(0, str.lastIndexOf(" "));
		str += "...";
	}
	
	return str;
}



//si la valeur de l'objet input est identique à str, alros on vide le champs texte
function inputSwitch(obj, str){
	if(obj.value == str) obj.value = "";
}





function erase_combo(id){
	var lg = document.getElementById(id).length;
	for (var i=(lg-1); i>=0; i--){
		document.getElementById(id).options[i] = null;
	}
	document.getElementById(id).length = 0;
	
	var temp = document.getElementById(id).childNodes;	
	for (var i=(temp.length-1); i>=0; i--){
		disparu = document.getElementById(id).removeChild(temp[i]);
	}
}


function addOptionGroup(combo, label){
	var combox 	= document.getElementById(combo);
	var opt   	= document.createElement("OPTGROUP");
	var lab		= document.createAttribute("label");
	lab.nodeValue = label;
	opt.setAttributeNode(lab);
	combox.appendChild(opt);
	return opt;
}

function addOption(optGroup, label, value){
	var option	= document.createElement("OPTION");
	var val		= document.createAttribute("value");
	var txt		= document.createTextNode(label);
	val.nodeValue= value;
	
	option.appendChild(txt);
	option.setAttributeNode(val);	
	optGroup.appendChild(option);
}

function addOptionOnly(label, value){
	var option	= document.createElement("OPTION");
	var val		= document.createAttribute("value");
	var txt		= document.createTextNode(label);
	val.nodeValue= value;
	
	option.appendChild(txt);
	option.setAttributeNode(val);	
	return option;
}

function chargeCommunes(num_canton)
{
	//alert(cpt);
	xmlhttp = getXHR();
	
	//xmlhttp.open("GET", "ajax/get_actus_bloc.xhr.php?bloc="+selection+"&cpt="+cpt, true); 
	//xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	//xmlhttp.send(null);
	
	clearNode('aff_comm');
	
	if(num_canton>0)
	{
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4)
			{
				if(xmlhttp.responseText !=  '')
				{
					//select = xmlhttp.responseText;
					//alert(select);
					
					
					
					var xml = xmlhttp.responseXML;
					
					var listeOptions = xml.getElementsByTagName('option');
					
					
					
					
					var nomOption = 'commune...';
					var valeurOption = '0';
					var elementOption = document.createElement('option');
					elementOption.setAttribute('value',valeurOption);
					elementOption.setAttribute('style','color:black;');
					var textNode = document.createTextNode(nomOption);
					elementOption.appendChild(textNode);
					
					window.document.getElementById('aff_comm').appendChild(elementOption);
					
					for(var i = 0; i<(listeOptions.length); i++)
					{
						var nomOption = listeOptions[i].firstChild.nodeValue;
						var valeurOption = listeOptions[i].getAttribute('value');
						var elementOption = document.createElement('option');
						elementOption.setAttribute('value',valeurOption);
						elementOption.setAttribute('style','color:black;');
						var textNode = document.createTextNode(nomOption);
						elementOption.appendChild(textNode);
						
						window.document.getElementById('aff_comm').appendChild(elementOption);
					}
					
					//document.getElementById('actu_sous_rubrique').innerHTML = select;
					window.document.getElementById('aff_comm').disabled = false;
				}
				//else{
				//	alert(selection+' : Acune sous rubrique associée !');
				//}
			}
		} 
	}

	
	//xmlhttp.open("GET", "ajax/get_actus_bloc.xhr.php?bloc="+selection+"&cpt="+cpt, true); 
	xmlhttp.open("GET", "ajax/charger_communes.xhr.php?num_canton="+num_canton, true); 
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(null);	
}