function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest){
		xhr = new XMLHttpRequest(); 
	} else if(window.ActiveXObject){ 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else {
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	return xhr;
}

function go(c){
	if(!c.data.replace(/\s/g,''))
		c.parentNode.removeChild(c);
}

function clean(d){
	var bal=d.getElementsByTagName('*');

	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			go(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			go(b);
	}
	return d;
} 

function fillSocCompletion(value) {
	var xhrSoc = getXhr() ;
	xhrSoc.onreadystatechange = function(){
		if(xhrSoc.readyState == 4 && xhrSoc.status == 200) {
			node_list = xhrSoc.responseXML.getElementsByTagName("root").item(0);
			var soc_completion = document.getElementById('soc_completion') ;
			while(soc_completion.hasChildNodes()) { 
				soc_completion.removeChild(soc_completion.firstChild); 
			}
			if(node_list.childNodes.length == 0) {
				var option = document.createElement('option');
				option.value = null ;
				option.appendChild(document.createTextNode('Rien :\'('));
				soc_completion.appendChild(option);
			}
			var i = 0 ;
			for(i; i < node_list.childNodes.length; i++){
				var option = document.createElement('option');
				option.value = node_list.childNodes.item(i).getAttribute('value') ;
				option.appendChild(document.createTextNode(node_list.childNodes.item(i).firstChild.nodeValue));
				soc_completion.appendChild(option);
			}
		}
	}
	xhrSoc.open("POST","index.php?action=ajaxFiltreSoc&search="+value,true);
	xhrSoc.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrSoc.send(null);
}

function fillJvCompletion(value) {
	var xhrTags = getXhr() ;
	xhrTags.onreadystatechange = function(){
		if(xhrTags.readyState == 4 && xhrTags.status == 200) {
			node_list = xhrTags.responseXML.getElementsByTagName("root").item(0);
			var jv_completion = document.getElementById('jv_completion') ;
			while(jv_completion.hasChildNodes()) { 
				jv_completion.removeChild(jv_completion.firstChild); 
			}
			if(node_list.childNodes.length == 0) {
				var option = document.createElement('option');
				option.value = null ;
				option.appendChild(document.createTextNode('Rien :\'('));
				jv_completion.appendChild(option);
			}
			var i = 0 ;
			for(i; i < node_list.childNodes.length; i++){
				var option = document.createElement('option');
				option.value = node_list.childNodes.item(i).getAttribute('value') ;
				option.appendChild(document.createTextNode(node_list.childNodes.item(i).firstChild.nodeValue));
				jv_completion.appendChild(option);
			}
		}
	}
	xhrTags.open("POST","index.php?action=ajaxFiltreJv&search="+value,true);
	xhrTags.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrTags.send(null);
}

function fillTagsCompletion(value) {
	var xhrTags = getXhr() ;
	xhrTags.onreadystatechange = function(){
		if(xhrTags.readyState == 4 && xhrTags.status == 200) {
			node_list = xhrTags.responseXML.getElementsByTagName("root").item(0);
			var tags_completion = document.getElementById('tags_completion') ;
			while(tags_completion.hasChildNodes()) { 
				tags_completion.removeChild(tags_completion.firstChild); 
			}
			if(node_list.childNodes.length == 0) {
				var option = document.createElement('option');
				option.value = null ;
				option.appendChild(document.createTextNode('Rien :\'('));
				tags_completion.appendChild(option);
			}
			var i = 0 ;
			for(i; i < node_list.childNodes.length; i++){
				var option = document.createElement('option');
				option.value = node_list.childNodes.item(i).getAttribute('value') ;
				option.appendChild(document.createTextNode(node_list.childNodes.item(i).firstChild.nodeValue));
				tags_completion.appendChild(option);
			}
		}
	}
	xhrTags.open("POST","index.php?action=ajaxFiltreTags&search="+value,true);
	xhrTags.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrTags.send(null);
}

function fillUserCompletion(value) {
	var xhrTags = getXhr() ;
	xhrTags.onreadystatechange = function(){
		if(xhrTags.readyState == 4 && xhrTags.status == 200) {
			node_list = xhrTags.responseXML.getElementsByTagName("root").item(0);
			var user_completion = document.getElementById('user_completion') ;
			while(user_completion.hasChildNodes()) { 
				user_completion.removeChild(user_completion.firstChild); 
			}
			if(node_list.childNodes.length == 0) {
				var option = document.createElement('option');
				option.value = null ;
				option.appendChild(document.createTextNode('Rien :\'('));
				user_completion.appendChild(option);
			}
			var i = 0 ;
			for(i; i < node_list.childNodes.length; i++){
				var option = document.createElement('option');
				option.value = node_list.childNodes.item(i).getAttribute('value') ;
				option.appendChild(document.createTextNode(node_list.childNodes.item(i).firstChild.nodeValue));
				user_completion.appendChild(option);
			}
		}
	}
	xhrTags.open("POST","index.php?action=ajaxFiltreUser&search="+value,true);
	xhrTags.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrTags.send(null);
}

function makeGroupList(select_id,group_id_default) {
	var xhrGroup = getXhr() ;
	xhrGroup.onreadystatechange = function(){
		if(xhrGroup.readyState == 4 && xhrGroup.status == 200) {
			node_list = xhrGroup.responseXML.getElementsByTagName("root").item(0);
			var list_group = document.getElementById(select_id) ;
			while(list_group.hasChildNodes()) { 
				list_group.removeChild(list_group.firstChild); 
			}
			var i = 0 ;
			for(i; i < node_list.childNodes.length; i++){
				var option = document.createElement('option');
				option.value = node_list.childNodes.item(i).getAttribute('value') ;
				if(option.value == group_id_default) {
					option.selected = "selected" ;
				}
				option.appendChild(document.createTextNode(node_list.childNodes.item(i).firstChild.nodeValue));
				list_group.appendChild(option);
			}
		}
	}
	xhrGroup.open("POST","index.php?action=ajaxGroup",true);
	xhrGroup.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrGroup.send(null);
}

function addGroup(select_id,group_id_default,group_nom) {
	var xhrGroup = getXhr() ;
	xhrGroup.onreadystatechange = function(){
		if(xhrGroup.readyState == 4 && xhrGroup.status == 200) {
			node_list = xhrGroup.responseXML.getElementsByTagName("root").item(0);
			var list_group = document.getElementById(select_id) ;
			while(list_group.hasChildNodes()) { 
				list_group.removeChild(list_group.firstChild); 
			}
			var i = 0 ;
			for(i; i < node_list.childNodes.length; i++){
				var option = document.createElement('option');
				option.value = node_list.childNodes.item(i).getAttribute('value') ;
				if(option.value == group_id_default) {
					option.selected = "selected" ;
				}
				option.appendChild(document.createTextNode(node_list.childNodes.item(i).firstChild.nodeValue));
				list_group.appendChild(option);
			}
		}
	}
	xhrGroup.open("POST","index.php?action=ajaxAddGroup&group_nom="+group_nom,true);
	xhrGroup.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrGroup.send(null);
}

function addSoc(soc_nom) {
	var xhrGroup = getXhr() ;
	xhrGroup.onreadystatechange = function(){
		if(xhrGroup.readyState == 4 && xhrGroup.status == 200) {
			makeSocList('soc_ed',$('def_soc_ed').value) ;
			makeSocList('soc_dev',$('def_soc_dev').value) ;
		}
	}
	xhrGroup.open("POST","index.php?action=ajaxAddSoc&soc_nom="+soc_nom,true);
	xhrGroup.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrGroup.send(null);
}

function makeSocList(select_id,soc_id_default) {
	var xhrSoc = getXhr() ;
	xhrSoc.onreadystatechange = function(){
		if(xhrSoc.readyState == 4 && xhrSoc.status == 200) {
			node_list = xhrSoc.responseXML.getElementsByTagName("root").item(0);
			var list_soc = document.getElementById(select_id) ;
			while(list_soc.hasChildNodes()) { 
				list_soc.removeChild(list_soc.firstChild); 
			}
			var i = 0 ;
			for(i; i < node_list.childNodes.length; i++){
				var option = document.createElement('option');
				option.value = node_list.childNodes.item(i).getAttribute('value') ;
				if(option.value == soc_id_default) {
					option.selected = "selected" ;
				}
				option.appendChild(document.createTextNode(node_list.childNodes.item(i).firstChild.nodeValue));
				list_soc.appendChild(option);
			}
		}
	}
	xhrSoc.open("POST","index.php?action=ajaxAllSoc",true);
	xhrSoc.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrSoc.send(null);
}

function loadTest(id) {
	var xhrTest = getXhr() ;
	xhrTest.onreadystatechange = function(){
		if(xhrTest.readyState == 4 && xhrTest.status == 200) {
			node_list = xhrTest.responseXML.getElementsByTagName("root").item(0);
			$('com_text').innerHTML = node_list.childNodes.item(0).firstChild.nodeValue ;
			$('com_note').innerHTML = node_list.childNodes.item(1).firstChild.nodeValue ;
			$('com_username').innerHTML = node_list.childNodes.item(2).firstChild.nodeValue ;
		}
	}
	xhrTest.open("POST","index.php?action=ajaxGetTest&id="+id,true);
	xhrTest.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrTest.send(null);
}

function addPointPost(post_id,news_id,user_id) {
	var xhrNewsV2 = getXhr() ;
	xhrNewsV2.onreadystatechange = function(){
		if(xhrNewsV2.readyState == 4 && xhrNewsV2.status == 200) {
			switch(xhrNewsV2.responseText) {
				case "-1" :
					alert('Vous avez déjà plussoyé ce post !') ;
					break ;
				case "-2" :
					alert('Erreur de formulaire.') ;
					break ;
				case "-3" :
					alert('Vous n\'étes pas vous') ;
					break ;
				default :
					$('post_'+post_id).innerHTML = xhrNewsV2.responseText ;
					break ;
			}
		}
	}
	xhrNewsV2.open("POST","index.php?action=ajaxAddScore&news_id="+news_id+"&post_id="+post_id+"&user_id="+user_id,true);
	xhrNewsV2.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrNewsV2.send(null);
}

function getNewsComment(news_id,ajax_page,user_id,comment_by_page) {
	if(!user_id) user_id = '0' ;
	var morphComment = new Fx.Morph('list_comments',{duration: 200}); 
	var morphListTop = new Fx.Morph('list_page_top',{duration: 200}); 
	var morphListBot = new Fx.Morph('list_page_bot',{duration: 200}); 
	morphListTop.start({
			'opacity': 0
			});
	morphListBot.start({
			'opacity': 0
			});
	morphComment.start({
			'opacity': 0
			});
	var xhrNewsComments = getXhr() ;
	xhrNewsComments.onreadystatechange = function(){
		if(xhrNewsComments.readyState == 4 && xhrNewsComments.status == 200) {
			var xhrListPage = getXhr() ;
			xhrListPage.onreadystatechange = function(){
				if(xhrListPage.readyState == 4 && xhrListPage.status == 200) {
					var temp_f = function() {
						$('list_comments').innerHTML = xhrNewsComments.responseText ;
						$('list_page_top').innerHTML = xhrListPage.responseText ;
						$('list_page_bot').innerHTML = xhrListPage.responseText ;
						morphListTop.start({
							'opacity': 1
						});
						morphListBot.start({
							'opacity': 1
						});
						morphComment.start({
							'opacity': 1
						});

						var list_comment_text = $$(".comment_text");
						list_comment_text.each(function(text, i) {
							var full = false ;
							var cutHeight = 200 ;
							var fx_text = new Fx.Morph(text, {duration: 1000, transition: Fx.Transitions.Bounce.easeOut});
							var fx_text2 = new Fx.Morph(text, {duration: 500});
							text.style.display = 'block';
							text.style.zIndex = text.offsetHeight ;
							if(text.offsetHeight > (cutHeight + 20)) {
								$('lls_'+text.id).style.display = 'block' ;
								text.style.height = cutHeight+'px' ;
							}
							$('lls_'+text.id).addEvent("click", function(event) {
								if(text.style.height == cutHeight+'px') {
									fx_text.cancel();
									fx_text2.cancel();
									fx_text.start({
									    'height': text.style.zIndex+'px'
									});
									$('lls_'+text.id).src = '/img/template/enrouler.png' ;
								} else {
									fx_text.cancel();
									fx_text2.cancel();
									fx_text2.start({
									    'height': cutHeight+'px'
									});
									$('lls_'+text.id).src = '/img/template/lirelasuite.png' ;
								}
								
							});
						});
					}
					temp_f.delay(250) ;
				}
			}
			xhrListPage.open("POST","index.php?action=ajaxListPage&comment_by_page="+comment_by_page+"&news_id="+news_id+"&page="+ajax_page+"&user_id="+user_id,true);
			xhrListPage.setRequestHeader('Content-Type','text/html;charset=utf-8');
			xhrListPage.send(null);
		}
	}
	xhrNewsComments.open("POST","index.php?action=ajaxGetComment&ajax_user_id="+user_id+"&ajax_news_id="+news_id+"&ajax_page="+ajax_page+"&comment_by_page="+comment_by_page,true);
	xhrNewsComments.setRequestHeader('Content-Type','text/html;charset=utf-8');
	xhrNewsComments.send(null);
}

