// -----------------------------------------------------------------------------
// mod_search.js
// liste des fonctions pour la gestion de la recherche de véhicule
// -----------------------------------------------------------------------------


// -----------------------------------------------------------------------------
// - Fonctions outils
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function getXhr() {
	var xhr;
	try {  xhr = new ActiveXObject('Msxml2.XMLHTTP');   }
	catch (e) {
	    try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); }
	    catch (e2) {
	      try {  xhr = new XMLHttpRequest(); }
	      catch (e3) { xhr = false; }
	    }
	 }
	 
	 return xhr;
} // fin de la fonction getXhr()
// -----------------------------------------------------------------------------


// -----------------------------------------------------------------------------
// - Gestion de la zone de finition/version
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function div_appear(obj, objDiv, objDivContent, objMarque) {
	// vérif
	if (objDiv == undefined || objDivContent == undefined || objMarque == undefined)
	    return;

	// initialisation
	objDivContent.innerHTML = "Patientez SVP!";

    // récupération de la marque choisie
	var strMarque = objMarque.options[objMarque.selectedIndex].value;

    // récupération du nuage
    var url = "/tagclouds/dbtags.php?makeid=" + strMarque + "&function=div_add_key";

	// appel ajax
	var xhr = getXhr();
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status  == 200) {
			objDivContent.innerHTML = xhr.responseText;
		}
	};

	xhr.open("GET", url, true);
	xhr.send(null);

	// affichage du div
	objDiv.style.display = "block";

	// suppression du contenu
	if (obj.value == "Finition/Version")
		obj.value = "";
} // fin de la fonction div_appear
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function div_disappear(obj, objDiv) {
	// vérif
	if (objDiv == undefined)
	    return;

	// affichage du div
	objDiv.style.display = "none";

	// suppression du contenu si nécessaire
	if (obj.value == "")
		obj.value = "Finition/Version";
} // fin de la fonction div_disappear
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function div_reset() {
    var obj = document.getElementById("finition");
    if (obj == undefined)
        return;

      obj.value = "Finition/Version";
}
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function div_add_key(strKey) {
    var obj = document.getElementById("finition");
    if (obj == undefined)
        return;

	if (obj.value.indexOf(strKey) >= 0)
	    return;

	if (obj.value == "Finition/Version")
	    obj.value = "";

	obj.value += strKey + ", ";
} // fin de la fonction div_add_key(str)
// -----------------------------------------------------------------------------


// -----------------------------------------------------------------------------
// - Gestion du moteur intelligent
// -----------------------------------------------------------------------------

// option de reset forcé :)
var bResetForm = false;

// -----------------------------------------------------------------------------
function search_onchange_form(objForm, strFormWhere) {
	if (objForm == undefined)
	    return;
	    
	// récupération de la situation courante
	var iCategorieId = objForm.cid.options[objForm.cid.selectedIndex].value;
	var iMarqueId = objForm.make.options[objForm.make.selectedIndex].value;
	var iModelId = objForm.model.options[objForm.model.selectedIndex].value;
//	var iFuelId = objForm.fuel.options[objForm.fuel.selectedIndex].value;
//	var iCo2Id = objForm.co2.options[objForm.co2.selectedIndex].value;
//	var iColorId = objForm.color.options[objForm.color.selectedIndex].value;

	// reset forcé
	if (bResetForm) {
	    iMarqueId = iModelId = 0;
        bResetForm = false;
	}
	
	// changement des listes prioritaires
	if (strFormWhere != "model" && strFormWhere != "special")
		search_change_list(objForm.model, new Array());

	// construction de l'url
	var url = "/modules/mod_ezautos_search/ajax/ajax_getElementFromCategorie.php?cid=" + iCategorieId + "&makeid=" + iMarqueId + "&modelid=" + iModelId;

	// récupération du modèle ajax
	var xhr = getXhr();
//	if (xhr == false)
//	    return;
	    
	// fonction de récupération d'événements
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status  == 200) {
			var strContent = xhr.responseText;
			//alert(url + "\n" + strContent);
			eval(strContent);
			
			// changement des listes prioritaires
			if (strFormWhere != "make" && strFormWhere != "model" && strFormWhere != "special")
				search_change_list(objForm.make, arrMarques);

			// changement des liste secondaires
			if (strFormWhere != "special") {
				search_change_list(objForm.fuel, arrEnergie);
				search_change_list(objForm.co2, arrCO2);
				//search_change_list(objForm.color, arrColor);
   			}
		}
	};

	xhr.open("GET", url, true);
	xhr.send(null);
} // fin de la fonction search_onchange_()
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function search_change_list(objList, arrContent) {
	// vérif rapide
	if (objList == undefined)
	    return;
	    
	// variables
	var iIndice = 1;

	// empty the list
	var bFirst = true;
	for (i in objList.options.length) {
	    // on zappe le premier élément
	    if (bFirst) {
            bFirst = false;
            continue;
		}
		objList.options[i] = null;
	}

	// lecture du tableau et saisie des éléments
	for (var i = 0; i < arrContent.length; i++) {
	    // création de l'option et insertion des valeurs
		opt = new Option();
		opt.value = arrContent[i][0];
		opt.text = arrContent[i][1];
		
		// sélection automatique
		//opt.selected = true;

		// insertion de l'option dans la liste
		objList.options[iIndice++] = opt;
	}

	// mise à jour de la taille de la liste
	objList.length = iIndice;
} // fin fonction search_change_liste(obj, arr)
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
function search_reset_listselected(objForm) {
	// vérif rapide
	if (objForm == undefined)
	    return;
	    
	// on sélectionne les premières entrées des listes d'option
    objForm.make.options[0].selected = "selected";
    objForm.model.options[0].selected = "selected";
    
    // reset forcé
    bResetForm = true;
} // fin de la fonction search_reset_listselected(obj)
// -----------------------------------------------------------------------------

