/////////////////////////////////////////////////////////////////////////////////////
// SCRIPT : fonctionnalités liées au planicook
//
// SOMMAIRE :
// Affichage du planicook en Ajax
// Recettes
//	- Imprimer une recette
//	- Changer une recette
// Ajout / suppression des repas d'une semaine en Ajax
// Ajout / suppression d'un repas en Ajax
// Calcul du budget en ajax
// Ajout / suppression d'un plat en Ajax
// Changement de menu en ajax
// Fonctions relatives à la liste de courses
/////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////
// Affichage du planicook en Ajax
/////////////////////////////////////////////////////////////////////////////////////

// Fonction appeler pour afficher un planicook
// Paramètres :
//		- idplck : identifiant du planicook a afficher
//		- rand : nombre aléatoire (pour rafraichissement du cache)
//		- option : option d'affichage (c : consultation ; m : modification)
// Résultat : lance une requête AJAX pour récupérer le code HTML du planicook
function afficherPlanicook(idplck,rand,option){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-view.php?idplck="+idplck+"&r="+Math.random(0,64525)+"&option="+option;
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_plck(xhr,idplck,option); };
    
    //on affiche le message d'acceuil
	startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}

// Fonction de traitement de l'affichage du planicook
// Paramètres : 
//		- xhr : requete AJAX
//		- identifiant du planicook a afficher
//		- option : option d'affichage (c : consultation ; m : modification)
// Résultat : affiche le planicook
function alert_plck(xhr,idplck,option){
    if(xhr.readyState==4){
    	var docXML= xhr.responseXML;
    	var itemB = docXML.getElementsByTagName("bloc");
		if(itemB && itemB.item(0) && itemB.item(0).firstChild && itemB.item(0).firstChild.data){
			bloc = itemB.item(0).firstChild.data;
		
			document.getElementById("divPlck").innerHTML = bloc;
			
			calculBudget(idplck);
			
			stopLoader();
		}else{
			afficherPlanicook(idplck,Math.random(0,64525),option);
		}
    }
}


/////////////////////////////////////////////////////////////////////////////////////
// Recettes
/////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
// Imprimer une recette
function imprimerRecette(num_plck){
	ouvrirPopup('imprimer','?id_plck='+num_plck);
}

////////////////////////////////////////////////////////////////////
// Changer une recette
function changerRecette(id_repas,pos_recette,jour){
	ouvrirPopup('recette','?id_repas='+id_repas+'&pos_recette='+pos_recette+'&jour='+jour);
}
function changerRecette2(id_repas,pos_recette,jour){
	ouvrirPopup('recette','?id_repas='+id_repas+'&pos_recette='+pos_recette+'&renvoi=complet&jour='+jour);
}
function changerRecetteOption(idplck,option){
	//window.parent.startLoader();
	document.getElementById('option').value = option;
	document.getElementById('formRecette').action = "script.changer-repas.php";
	document.getElementById('formRecette').submit();
}
function selectOngletRR(onglet){
	//window.parent.startLoader();
	document.getElementById('onglet').value = onglet;
	document.getElementById('formRecette').submit();
}


/////////////////////////////////////////////////////////////////////////////////////
// Ajout / suppression des repas d'une semaine en Ajax
/////////////////////////////////////////////////////////////////////////////////////
function suppRepasSemaine(idplck,repas,semaine){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-repas-semaine.php?supp=1&idplck="+idplck+"&repas="+repas+"&semaine="+semaine+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_repas(xhr,idplck); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function ajouRepasSemaine(idplck,repas,semaine){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-repas-semaine.php?ajou=1&idplck="+idplck+"&repas="+repas+"&semaine="+semaine+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if (window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_repas(xhr,idplck); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function alert_repas(xhr,idplck){
    if (xhr.readyState==4){
    	afficherPlanicook(idplck,Math.random(0,64525),'m');
    }
}


/////////////////////////////////////////////////////////////////////////////////////
// Ajout / suppression d'un repas en Ajax
/////////////////////////////////////////////////////////////////////////////////////
function suppRepas(idrepas,jour){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-repas.php?supp=1&idrepas="+idrepas+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_repas_jour(xhr,jour); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function ajouRepas(idrepas,jour){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-repas.php?ajou=1&idrepas="+idrepas+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_repas_jour(xhr,jour); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function alert_repas_jour(xhr,jour){
    if (xhr.readyState==4){
		stopLoader();
    	document.location.href='mon-planicook-complet.php?jour='+jour;
    }
}


/////////////////////////////////////////////////////////////////////////////////////
// Calcul du budget en ajax
/////////////////////////////////////////////////////////////////////////////////////
function calculBudget(idplck){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-budget.php?idplck="+idplck+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_budget(xhr,idplck); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function alert_budget(xhr,idplck){
    if(xhr.readyState==4){
    	var docXML= xhr.responseXML;
    	var itemB = docXML.getElementsByTagName("bloc");
		if(itemB && itemB.item(0) && itemB.item(0).firstChild && itemB.item(0).firstChild.data){
			bloc = itemB.item(0).firstChild.data;
			
			document.getElementById('divBudget').innerHTML=bloc;
			
			stopLoader();
		}else{
			calculBudget(idplck);
		}
    }
}

// Alerte calcul du budget lorsque l'on cocher une case "j'ai déjà"
function cocherJaiDeja(){
	elt = document.getElementById('cocheJaiDeja');
	
	if(elt){
		if(elt.value=='0'){
			ouvrirPopup('alert-calcul','');
			elt.value='1';
		}
	}
}


/////////////////////////////////////////////////////////////////////////////////////
// Ajout / suppression d'un plat en Ajax
/////////////////////////////////////////////////////////////////////////////////////
function suppPlat(idrepas,position,jour){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-plat.php?supp=1&idrepas="+idrepas+"&position="+position+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_plat_jour(xhr,jour); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function ajouPlat(idrepas,position,jour){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-plat.php?ajou=1&idrepas="+idrepas+"&position="+position+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_plat_jour(xhr,jour); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
	
}
function alert_plat_jour(xhr,jour){
    if(xhr.readyState==4){
		stopLoader();
    	document.location.href='mon-planicook-complet.php?jour='+jour;
    }
}


/////////////////////////////////////////////////////////////////////////////////////
// Changement de menu en ajax
/////////////////////////////////////////////////////////////////////////////////////
function changeMenu(idrepas,moment,jour){
	libType = "type_"+moment;
	libSousType = "soustype_"+moment;
	leSelect = document.getElementById(libType);
	lIndex = leSelect.selectedIndex ;
	type = leSelect.options[lIndex].value;
	leSelect2 = document.getElementById(libSousType);
	lIndex2 = leSelect2.selectedIndex ;
	soustype = leSelect2.options[lIndex2].value;
	
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-menu.php?action=change&idrepas="+idrepas+"&type="+type+"&soustype="+soustype+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_menu_jour(xhr,jour); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function changeMenu3(idrepas,moment,jour){
	libType = "type_"+moment;
	libSousType = "soustype_"+moment;
	leSelect = document.getElementById(libType);
	lIndex = leSelect.selectedIndex ;
	type = leSelect.options[lIndex].value;
	leSelect2 = document.getElementById(libSousType);
	lIndex2 = leSelect2.selectedIndex ;
	soustype = leSelect2.options[lIndex2].value;
	
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-menu.php?action=mouline&idrepas="+idrepas+"&type="+type+"&soustype="+soustype+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_menu_jour(xhr,jour); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function alert_menu_jour(xhr,jour){
    if(xhr.readyState==4){
		stopLoader();
    	document.location.href='mon-planicook-complet.php?jour='+jour;
    }
}
function changeMenu2(idrepas,idplck){
	
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-menu.php?action=mouline&idrepas="+idrepas+"&option=2"+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_menu(xhr,idplck); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function alert_menu(xhr,idplck){
    if (xhr.readyState==4){
    	afficherPlanicook(idplck,Math.random(0,64525),'m');
    }
}


/////////////////////////////////////////////////////////////////////////////////////
// Fonctions relatives à la liste de courses
/////////////////////////////////////////////////////////////////////////////////////
function afficherListeCourse(idplck){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-liste.php?act=view&idplck="+idplck+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_listeCourse(xhr,'afficher_liste',idplck); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function afficherListeCourseImp(idplck){
    var xhr=null;
	var adr=urlsite+"script/ajax/plck-liste.php?act=imp&idplck="+idplck+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_listeCourse(xhr,'afficher_imp',idplck); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function ajoutListeCourse(idplck){
    cat = escape(document.getElementById('catLC').value);
    nom = escape(document.getElementById('nomLC').value);
    qte = escape(document.getElementById('qteLC').value);
    uni = escape(document.getElementById('uniLC').value);
    pxu = escape(document.getElementById('pxuLC').value);
	//alert ("cat : "+cat+" nom : "+nom+" qte : "+qte+" uni : "+uni+" pxu : "+pxu);

	var xhr=null;
	var adr=urlsite+"script/ajax/plck-liste.php?act=ajout&idplck="+idplck+"&cat="+cat+"&nom="+nom+"&qte="+qte+"&uni="+uni+"&pxu="+pxu+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_listeCourse(xhr,'ajout',idplck); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function suppListeCourse(idplck,idl){
	var xhr=null;
	var adr=urlsite+"script/ajax/plck-liste.php?act=supp&idplck="+idplck+"&idl="+idl+"&r="+Math.random(0,64525);
    
    if(window.XMLHttpRequest){ 
        xhr = new XMLHttpRequest();
    }else if(window.ActiveXObject){
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { alert_listeCourse(xhr,'supp',idplck,idl); };
    
    //on affiche le message d'acceuil
    startLoader();
    
    //on appelle le fichier reponse.txt
    xhr.open("GET", adr, true);
    xhr.send(null);
}
function alert_listeCourse(xhr,type,idplck,opt1){
    if (xhr.readyState==4){
    	var docXML= xhr.responseXML;
    	var itemB = docXML.getElementsByTagName("bloc");
		if(itemB && itemB.item(0) && itemB.item(0).firstChild && itemB.item(0).firstChild.data){
			bloc = itemB.item(0).firstChild.data;
			
			document.getElementById("divListeCourse").innerHTML = bloc;
			
			stopLoader();
		}else{
			switch(type){
				case 'afficher_liste' : afficherListeCourse(idplck); break;
				case 'afficher_imp' : afficherListeCourseImp(idplck); break;
				case 'ajout' : ajoutListeCourse(idplck); break;
				case 'supp' : suppListeCourse(idplck,opt1); break;
			}
		}
    }
}