/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function ajaxSendForm(form)
{
	var request = new Request({method: $(form).get('method'), url: $(form).get('action'),
		onSuccess: function(responseText, responseXML)
		{
			var erreur = responseXML.getElement('erreur').get('text');
			var msg = responseXML.getElement('msg').get('text');

			if (erreur == '0')
			{
				if ($('div_ghost_edit_prod'))
				{
					$('div_ghost_edit_prod').destroy();
				}
			}
			alert(msg);
		}
	});
	request.send();
}

function ajaxFormUpdateDiv(id_form, id_div, url_to, params, callback)
{
	var request = new Request({method: $(id_form).get('method'), url: url_to,
		onSuccess: function(responseText)
		{
			$(id_div).set('html', responseText);
			eval(callback);
		}
	});
	request.send(params);
}
function ajaxRestCompleter()
{
	new Autocompleter.Ajax.Json('nom_prod', '/search_ajax.php', {
                        'minLength': 2,
                        'postVar': 'search'
	});
}
function ajaxSend(url_send)
{
	var request = new Request({method: 'get', url: url_send});
	request.send();
}

function chargeListeFromInput(input_id, select_id)
{
	var input_value = $(input_id).get('value');
	var select = $(select_id);

	if (input_value.length >= 3)
	{
		var myRequest = new Request({
			url: '/user.xml?list_user=1',
			method: 'get',
			onSuccess: function(responseText, responseXML) {
				var donnees = responseXML.getElementsByTagName('donnee');
				// Vide la liste
				select.length = 0;

				$$(donnees).each(function (d) {
					var opt = new Element('Option', {value:d.getElementsByTagName('id').item(0).get('text')})
					opt.set('text', d.getElementsByTagName('valeur').item(0).get('text'))
					select.adopt(opt);
				});

				if (select.length == 0)
				{
					var opt = new Element('Option', {value:-1})
					opt.set('text', 'Aucun résultat')
					select.adopt(opt);
				}
			}
		});
		myRequest.send('debut='+input_value);
	}

}

var items = null;
var item_sleeper = false;
function ajaxGetResults(url)
{
	items = null;

	var xhr_object = null;
	if(window.XMLHttpRequest) // Firefox
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // Internet Explorer
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else { // XMLHttpRequest non supportï¿½ par le navigateur
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return;
	}

	xhr_object.open("GET", url, true);
	xhr_object.onreadystatechange= function() {
		if (xhr_object.readyState == 4) /* 4 : ï¿½tat "complete" */
		{
			if (xhr_object.status == 200) /* 200 : code HTTP pour OK */
			{
				var docXML= xhr_object.responseXML;
				items = docXML.getElementsByTagName("donnee");
				item_sleeper = true;
			}
		}
	}
	xhr_object.send(null);
}

function ckbx_all(ckbx_name, ckbx_checked_value)
{
	var lesCkbx = document.getElementsByName(ckbx_name);
	for (var i = 0; i < lesCkbx.length; i++)
	{
		lesCkbx[i].checked = ckbx_checked_value;
	}
}

function cocheTous(ckbx_name)
{
	ckbx_all(ckbx_name, true);
}

function decocheTous(ckbx_name)
{
	ckbx_all(ckbx_name, false);
}

function chargeListe(input_id, hidden_id, select_id, url)
{
	var input_value = document.getElementById(input_id).value;
	if (input_value.length != 5)
	{
		return false;
	}

	hidden_field = document.getElementById(hidden_id);
	la_liste = document.getElementById(select_id);
	la_liste.options.length = 0;
	la_liste.options[la_liste.options.length] = new Option('Chargement des villes');

	url = url+"?input="+input_value;
	ajaxGetResults(url)

	setInterval(return_interval = function() {
		if (item_sleeper == false)
			return false;

		item_sleeper = false;

		hidden_field.value = items.item(0).getElementsByTagName("id")[0].firstChild.data;

		// Vider la liste
		la_liste.options.length = 0;
		for (i=0; i < items.length; i++)
		{
			var id = items.item(i).getElementsByTagName("id");
			var valeur = items.item(i).getElementsByTagName("valeur");

			la_liste.options[la_liste.options.length] = new Option(valeur[0].firstChild.data, id[0].firstChild.data);
		}
		la_liste = null;
		clearInterval(return_interval);
	}, 100);

}
var la_liste = null;
var return_interval = null;

/* EDITION DES LISTES */
function addFromListToList(id_list, id_to)
{
	var sel_id = $(id_list).selectedIndex;
	var val = $(id_list).options[sel_id].text;
	var id = $(id_list).options[sel_id].value;

	for(i in $(id_to).options)
	{
		if ($(id_to).options[i].text == val)
		{
			alert('Dï¿½jï¿½ prï¿½sent dans la liste');
			return '';
		}
	}
	$(id_to).options[$(id_to).options.length] = new Option(val, id);
}

function deleteSelectedFromList(id_list)
{
	$(id_list).removeChild($(id_list).options[$(id_list).selectedIndex]);
}

function selectAllList(id_list)
{
	for(i in $(id_list).options)
	{
		$(id_list).options[i].selected = 'selected';
	}
}


function userCheckDispo(pseudo, pro)
{
	if (pseudo != '')
	{
		var myRequest = new Request({url: '/user.xml?action=check_pseudo', method: 'get', onSuccess: function(responseText, responseXML) {
			var reponse = responseXML.getElementsByTagName('valeur').item(0).get('text');

		    if(reponse == 1)
		    {
		    	if (pro)
			    	$('pseudo_retour').innerHTML = '<span style="color: green">Pseudo disponible</span>';
			    else
			    	$('pseudo_retour').innerHTML = '<span style="color: green">Raison Social disponible</span>';
		    }
		    else
		    {
		    	if (pro)
			    	$('pseudo_retour').innerHTML = 'Pseudo déjà pris';
			    else
			    	$('pseudo_retour').innerHTML = 'Raison Social déjà prise';
		    }
		}});
		myRequest.send('pseudo='+pseudo)
	}
}
function carrousselParam(id,nbSlide, tps,speed){


		var duration = tps,

			links = $$('div.tabs a'),

			tab = new Carousel.Extra({
				container: id,
				scroll: nbSlide,
				circular: true,
				current: 0,
				previous: links.shift(),
				next: links.pop(),
				/* mode: 'horizontal', */
				onChange: function (index) {

					links.each(function (el, off) {

						el[off == index ? 'addClass' : 'removeClass']('selected')
					})
				},
				fx: {

					duration: duration
				}
			});

		links.each(function (el, index) {

			el.addEvent('click', function (e) {

				e.stop();

				tab.move(index);
			})
		})

}

function openDiv(id){
    document.getElementById(id).style.display='block';
}
function closeDiv(id){
    document.getElementById(id).style.display='none';
    
}
function commentaireOpen(id_table,id_ligne){
    document.getElementById("avatar_"+id_table+"_"+id_ligne).style.display='block';
    document.getElementById("commentaire_"+id_table+"_"+id_ligne).style.height='50px';
    document.getElementById("submit_"+id_table+"_"+id_ligne).style.display='block';
}

function commentaireSend(id_table,id_ligne){
	var comment= document.getElementById('commentaire_'+id_table+'_'+id_ligne).value;
	var params = 'id_table='+id_table+'&id_ligne='+id_ligne+'&comment='+comment;
	var id_form= 'comment_form_'+id_table+'_'+id_ligne;
	var id_comm=  'comment_'+id_table+'_'+id_ligne;
	ajaxFormUpdateDiv(id_form, id_comm, '/commentaire/affichage/commentaireAjax.php', params);

}

function periodTts(){
    var i=0;
    hoverTts.periodical(2000);
    function hoverTts(){
        document.getElementById('tts_'+i).style.backgroundPosition='';
        document.getElementById('tts_'+i).style.height='';
        document.getElementById('tts_'+i).style.width='';
        if (i==14)
            i=-1;
        i=i+1;
        document.getElementById('tts_'+i).style.backgroundPosition='top right';
        document.getElementById('tts_'+i).style.height='69px';
        document.getElementById('tts_'+i).style.width='60px';
        
    }
}
function verifCookie(){
    if( (document.cookie.indexOf('fbs_144288762276534')!= -1 || document.cookie.indexOf('fcauth16161691774311017275')!= -1) && document.cookie.indexOf('m2sLoginExt')== -1){
        var date=new Date;
        date.setHours(date.getHours()+1);
        document.cookie="m2sLoginExt = oui ; expires=" + date.toGMTString();
        location.reload() ; //recharger la page
        document.getElementById("loader").style.display="block";

    }

}
function qtes(id,action){
    var qte_init=document.getElementById('qte_'+id).value;
     alert(id + " qte init = "+qte_init);
    if(action=="plus"){

        var retour=document.getElementById('qte_'+id).value=qte_init+1;
        alert(retour);
    }
    else if(action=="moins"){
       var retour2= document.getElementById(id).value=qte_init-1;
        alert(retour2);
    }
    else if(action=="supp"){
        alert('bcp de chose a supp');
    }
    else
        alert('totot');
}
