var xhr_objectCategory;
var isRequestActiveCategory;
var elmtCategorySource;

var AjaxSelectSection1 = function (elmtIDsource, elmtIDsection1, elmtIDsection2, elmtIDsection3, url, myform, allsection, lang)
{
	// Set the optional parameter if needed
	if ( (typeof allsection == 'undefined') || (allsection == 'undefined') ) {
		allsection = "0";
   	}
   	
	elmtCategorySource = document.getElementById(elmtIDsource);
	elmtCategorySource.autocomplete = 'off';

	elmtCategorySource.onchange = function (){
		if(isRequestActiveCategory)
			xhr_objectCategory.abort();
			
		// Make request to server
		makeRequestSection1(url, elmtCategorySource.value, elmtIDsection1, elmtIDsection2, elmtIDsection3, allsection, lang);
		// Show Hide fields
		selectCategoryOnChange(elmtCategorySource.value, myform);
	}

	xhr_objectCategory = createXHRObject();
	if(!xhr_objectCategory){
		return;
	}

	isRequestActiveCategory = false;
}


var makeRequestSection1 = function(url, catid, elmtIDsection1, elmtIDsection2, elmtIDsection3, allsection, lang)
{
	clearSelect(elmtIDsection1);
	clearSelect(elmtIDsection2);
	clearSelect(elmtIDsection3);

	if ((elmtCategorySource.selectedIndex>=0)&&(elmtCategorySource.options[elmtCategorySource.selectedIndex].value != "")){
		xhr_objectCategory.open('post', url, true);
		
		xhr_objectCategory.onreadystatechange = function(){
			if(xhr_objectCategory.readyState == 4){
				isRequestActiveCategory = false;
				// On vérifie qu'il y ait des résultats et qu'il n'y ait pas d'erreur
				if( (xhr_objectCategory.responseText == '') || (xhr_objectCategory.status != 200)){
					return;
				}
				parseMessagesSection1(elmtIDsection1,elmtIDsection2,elmtIDsection3,xhr_objectCategory.responseXML);
			}
		}
	
		xhr_objectCategory.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		var str = "catid="+escape(catid)+"&allsection="+escape(allsection)+"&lang="+escape(lang);
		xhr_objectCategory.send(str);
		isRequestActiveCategory = true;
	}

}


function parseMessagesSection1(elmtIDsection1,elmtIDsection2,elmtIDsection3,responseXML) 
{
	clearSelect(elmtIDsection1);
	clearSelect(elmtIDsection2);
	clearSelect(elmtIDsection3);

	var section1s = responseXML.getElementsByTagName('section1s')[0];

    for (loop = 0; loop < section1s.childNodes.length; loop++) {
	    var section1 = section1s.childNodes[loop];
        var sect1id = section1.getElementsByTagName("sect1id")[0];
        var sect1label = section1.getElementsByTagName("sect1label")[0];
        var sect1desc = section1.getElementsByTagName("sect1desc")[0];
        appendOptionInSelect(elmtIDsection1,sect1id.childNodes[0].nodeValue,sect1label.childNodes[0].nodeValue);
    }

	//Pour forcer le chargement des section2
	if(section1s.childNodes.length>0){
		var selectLst = document.getElementById(elmtIDsection1);
		if (selectLst) {
			selectLst.onchange();
		}
    }
}


/* Fonction sur l'event Onchange du select de la catégorie */
function selectCategoryOnChange(catid, myform)
{
	showTR('adactionTR');
	showTR('catidTR');
	showTR('sect1idTR');
	showTR('sect2idTR');
	showTR('sect3idTR');
	showTR('adtitleTR');
	showTR('addescTR');
	showTR('adisprivateTR');
	//
	showTR('adaddressTR');
	showTR('adzipcodeTR');
	showTR('adcityTR');
	showTR('adcountryTR');
	showTR('adnearofTR');
	showTR('adcontactTR');

// 1: Véhicules
	if(catid == 1){ 
		showTR('adpriceTR');
		hideTR('adartistnameTR');
		hideTR('adfromtodateTR');
		showTR('adkmTR');
		hideTR('adhourmotorTR');
		showTR('admadeyearTR');
		hideTR('adturnoverTR');

// 2: Bateaux - Voile - Nautisme
// 3: Aéronautique
	}else if( (catid == 2) || (catid == 3) ){ 
		showTR('adpriceTR');
		hideTR('adartistnameTR');
		hideTR('adfromtodateTR');
		hideTR('adkmTR');
		showTR('adhourmotorTR');
		showTR('admadeyearTR');
		hideTR('adturnoverTR');

// 4: Immobilier
	}else if(catid == 4){ 
		showTR('adpriceTR');
		hideTR('adartistnameTR');
		hideTR('adfromtodateTR');
		hideTR('adkmTR');
		hideTR('adhourmotorTR');
		showTR('admadeyearTR');
		hideTR('adturnoverTR');

// 5: Vacances
// 17: Emploi personnels - Employés - Job étudiant
// 20: Voyage
// 24: Activités nationales régionales et locales
// 33: Billeterie
	}else if( (catid == 5) || (catid == 17) || (catid == 20) || (catid == 24) || (catid == 33) ){ 
		showTR('adpriceTR');
		hideTR('adartistnameTR');
		showTR('adfromtodateTR');
		hideTR('adkmTR');
		hideTR('adhourmotorTR');
		hideTR('admadeyearTR');
		hideTR('adturnoverTR');

// 6: Animaux
// 7: Maman - Bébé
// 8: Produits BIO
// 9: Informatique	
// 10: Maison - Jardin - Extérieur
// 11: Bricolage - Outillage
// 12: TV - Hifi - Son - Téléphone
// 13: Photographie - Optique
// 18: Chasse - Pêche - Plongée
// 19: Modélisme
// 21: Spécial entre Juniors
// 22: Bien-être - Beauté - Parfums
// 25: Musique
// 26: Banque - Argent - Prêts - Sponsors
// 27: Cinéma - Théâtre - Spéctacle - Chant - Opéra
// 28: Sport
// 29: Perdu de vue
// 30: Généalogie
// 31: Surnaturel - Paranormal - Voyance
// 32: Astronomie
// 34: Mode Homme Femme Enfant
// 35: Gastronomie - Vins - Alcools - Spécialités
// 36: Cigares - Tabac
// 37: Loisirs - Hobbies
// 38: Propositions Commerciales
// 41: Entre PDGs et PROs
// 42: Scolarité - Etudes
// 43: Sortir la nuit
// 44: Casino - Jeux d'argent
// 45: Qui peut me renseigner?
// 46: Qui peut m'aider?
// 47: Qui peut faire?
	}else if( (catid == 6) || (catid == 7) || (catid == 8) || (catid == 9) || (catid == 10) 
		|| (catid == 11) || (catid == 12) || (catid == 13) || (catid == 18) || (catid == 19)
		|| (catid == 21) || (catid == 22) || (catid == 25) || (catid == 26)
		|| (catid == 27) || (catid == 28) || (catid == 29) || (catid == 30) || (catid == 31)
		|| (catid == 32) || (catid == 34) || (catid == 35) || (catid == 36) || (catid == 37) 
		|| (catid == 38) || (catid == 41) || (catid == 42) || (catid == 43) || (catid == 44)
		|| (catid == 45) || (catid == 46) || (catid == 47)
		){ 
		showTR('adpriceTR');
		hideTR('adartistnameTR');
		hideTR('adfromtodateTR');
		hideTR('adkmTR');
		hideTR('adhourmotorTR');
		hideTR('admadeyearTR');
		hideTR('adturnoverTR');

// 14: Moderne et Contemporain - Art et Bijoux
// 15: Antiquités
// 16: Collections - Collectionneurs
// 39: Verre - Cristal - Pâte de verre
// 40: Terre cuite - Céramique
	}else if((catid == 14) || (catid == 15) || (catid == 16) || (catid == 39) || (catid == 40)){ 
		showTR('adpriceTR');
		showTR('adartistnameTR');
		hideTR('adfromtodateTR');
		hideTR('adkmTR');
		hideTR('adhourmotorTR');
		hideTR('admadeyearTR');
		hideTR('adturnoverTR');

// 23: Rencontre
	}else if((catid == 23)){ 
		hideTR('adpriceTR');
		hideTR('adartistnameTR');
		hideTR('adfromtodateTR');
		hideTR('adkmTR');
		hideTR('adhourmotorTR');
		hideTR('admadeyearTR');
		hideTR('adturnoverTR');
		// Valeur du champ prix
		document.getElementById('adprice').value='0';
	}
}




