/*
	This is the JavaScript file for the AJAX Suggest Tutorial

	You may use this code in your own projects as long as this 
	copyright is left	in place.  All code is provided AS-IS.
	This code is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	
	For the rest of the code visit http://www.DynamicAJAX.com
	
	Copyright 2006 Ryan Smith / 345 Technical / 345 Group.	

*/

// Fonctions pour apparition et disparition des suggestions si mouseover ou non.
delai = 15000;
est_allume = 0;

function EteintDivSuggest() {
	var ss = document.getElementById('search_suggest')
	ss.style.display = 'none';
}

function EteintDivSuggestQuick() {
	var ss = document.getElementById('search_suggest_quick')
	ss.style.display = 'none';
}

//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Browser incompatible with suggest box! Upgrade to newer version.");
	}
}

//Our XmlHttpRequest object to get the auto suggest
var searchReq = getXmlHttpRequestObject();

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}


/* ZONE POUR APPELS DE LA PAGE ADD */
//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggestAdd() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('nom').value);
		if (str.length < 2) { str = 'wfrf234f3htt43tghn57j6k87k78k78k76'; } // Pour pas faire de recherche valide si trop petit mot et flusher résultats si on a fait des backspaces.
		searchReq.open("GET", '/panda/index.php?module=Vins&class=Affichage&event=suggestionsVins&suggestion_box=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggestAdd; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggestAdd() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		ss.style.display = 'block';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length/2 - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:selectionSuggestionVinAdd(' + str[2*i] + ');" ';
			suggest += 'class="suggest_link">' + str[2*i+1] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}
//Click function
function selectionSuggestionVinAdd(id_vin) {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('nom').value);
		searchReq.open("GET", '/panda/index.php?module=Vins&class=Affichage&event=suggestionsVinsSelectionne&id_vin=' + id_vin, true);
		searchReq.onreadystatechange = afficheSelectionSuggestionVinAdd; 
		searchReq.send(null);
	}		
}
function afficheSelectionSuggestionVinAdd() {
	if (searchReq.readyState == 4) {
		var str = searchReq.responseText.split("\n");
		var nom = document.getElementById('nom');
		nom.value = str[0];
		var annee = document.getElementById('annee');
		annee.value = str[1];
		
		// Reset le pays puis sélectionne le pays choisis.
		var pays = document.getElementById('pays');
		pays.selectedIndex = 0;
		pays_vin = str[2] - 0;
		nombre_pays = pays.options.length;
		for (i=0; i < nombre_pays; i++) {
			if (pays.options[i].value == pays_vin) {
				pays.selectedIndex = i;
			}
		}
		
		document.add_a_wine.region.value = str[3];
		
		var vignoble = document.getElementById('vignoble');
		vignoble.value = str[4];
		
		// Reset tous les cépages puis choisis celui sélectionné.
		var cepage1 = document.getElementById('cepage1');
		cepage1.selectedIndex = 0;
		var cepage2 = document.getElementById('cepage2');
		cepage2.selectedIndex = 0;
		var cepage3 = document.getElementById('cepage3');
		cepage3.selectedIndex = 0;
		var cepage4 = document.getElementById('cepage4');
		cepage4.selectedIndex = 0;
		var cepage5 = document.getElementById('cepage5');
		cepage5.selectedIndex = 0;
		
		cepage = str[5] - 0;
		nombre_cepages = cepage1.options.length;
		for (i=0; i < nombre_cepages; i++) {
			if (cepage1.options[i].value == cepage) {
				cepage1.selectedIndex = i;
			}
		}

		// Reset la catégorie puis choisis celle sélectionnée.
		var categorie = document.getElementById('categorie');
		categorie.selectedIndex = 0;
		
		categorie_vin = str[6] - 0;
		nombre_categorie = categorie.options.length;
		for (i=0; i < nombre_categorie; i++) {
			if (categorie.options[i].value == categorie_vin) {
				categorie.selectedIndex = i;
			}
		}
		
		// Indique le ID du vin pour référence.
		document.add_a_wine.id_vin_suggere.value = str[7];

		var ss = document.getElementById('search_suggest')
		ss.style.display = 'none';
	}
}
/* FIN DE LA ZONE POUR APPELS DE LA PAGE ADD */


/* ZONE POUR APPELS DU QUICK ADD */
//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggestAddQuick() {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('quick_nom').value);
		if (str.length < 2) { str = 'wfrf234f3htt43tghn57j6k87k78k78k76'; } // Pour pas faire de recherche valide si trop petit mot et flusher résultats si on a fait des backspaces.
		searchReq.open("GET", '/panda/index.php?module=Vins&class=Affichage&event=suggestionsVins&suggestion_box=' + str, true);
		searchReq.onreadystatechange = handleSearchSuggestAddQuick; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggestAddQuick() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest_quick')
		ss.innerHTML = '';
		ss.style.display = 'block';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length/2 - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:selectionSuggestionVinAddQuick(' + str[2*i] + ');" ';
			suggest += 'class="suggest_link">' + str[2*i+1] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}
//Click function
function selectionSuggestionVinAddQuick(id_vin) {
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById('quick_nom').value);
		searchReq.open("GET", '/panda/index.php?module=Vins&class=Affichage&event=suggestionsVinsSelectionne&id_vin=' + id_vin, true);
		searchReq.onreadystatechange = afficheSelectionSuggestionVinAddQuick; 
		searchReq.send(null);
	}		
}
function afficheSelectionSuggestionVinAddQuick() {
	if (searchReq.readyState == 4) {
		var str = searchReq.responseText.split("\n");
		var nom = document.getElementById('quick_nom');
		nom.value = str[0];
		var annee = document.getElementById('quick_millesime');
		annee.value = str[1];
		
		// Reset le pays puis sélectionne le pays choisis.
		var pays = document.getElementById('quick_pays');
		pays.selectedIndex = 0;
		pays_vin = str[2] - 0;
		nombre_pays = pays.options.length;
		for (i=0; i < nombre_pays; i++) {
			if (pays.options[i].value == pays_vin) {
				pays.selectedIndex = i;
			}
		}
		
		document.ajouter.quick_appellation.value = str[3];
		
		var vignoble = document.getElementById('quick_producteur');
		vignoble.value = str[4];
		
		// Reset tous les cépages puis choisis celui sélectionné.
		var cepage1 = document.getElementById('quick_cepage');
		cepage1.selectedIndex = 0;
		cepage = str[5] - 0;
		nombre_cepages = cepage1.options.length;
		for (i=0; i < nombre_cepages; i++) {
			if (cepage1.options[i].value == cepage) {
				cepage1.selectedIndex = i;
			}
		}

		var ss = document.getElementById('search_suggest_quick')
		ss.style.display = 'none';
	}
}
/* FIN DE LA ZONE POUR APPELS DU QUICK ADD */
