//par defaut la ville n'est pas bien saisie
var villeOk=false;

function updateSelected(dropdownName, selectedValue) {
	var dropdown = document.getElementById(dropdownName);
	var dropdownLength = dropdown.options.length;
	for (var i =0; i< dropdownLength; i++) {
		if (selectedValue == dropdown[i].value) {
		dropdown.selectedIndex=i;
		}
	}
}
function updateSelectedText(dropdownName, selectedValue) {
	if (selectedValue)
	{
		var dropdown = document.getElementById(dropdownName);
		var dropdownLength = dropdown.options.length;
		for (var i =0; i< dropdownLength; i++) {
			if (supprAccent(selectedValue.toLowerCase()) == supprAccent(dropdown[i].text.toLowerCase())) {
				dropdown.selectedIndex=i;
			}
		}
	}
}
function updateTypeDropDown(transact) {
	var typeDropDown = document.getElementById('what');
	
	// alert('updateTypeDropDown'+' transac='+transact);
	
	if (transact == "5") {
		
		
		$('#type_location_select').hide();
		
		$('#type_vendre_select').show();
		document.getElementById('what').selectedIndex='';
		if(document.getElementById('what2') != null){
			document.getElementById('what2').selectedIndex=''; 
		}
	/*
		typeDropDown.options.length = 8;
		typeDropDown.options[2].value = '011';
		typeDropDown.options[2].text = 'Appartement';
		typeDropDown.options[1].value = '012';
		typeDropDown.options[1].text = 'Loft';
		typeDropDown.options[0].value = '013';
		typeDropDown.options[0].text = 'Maison';
		typeDropDown.options[3].value = '014';
		typeDropDown.options[3].text = 'Propri\351t\351';
		typeDropDown.options[4].value = '017';
		typeDropDown.options[4].text = 'Garage';
		typeDropDown.options[5].value = '016';
		typeDropDown.options[5].text = 'Terrain';
		typeDropDown.options[6].value = '015';
		typeDropDown.options[6].text = 'Immeuble';
		typeDropDown.options[7].value = '019';
		typeDropDown.options[7].text = 'Meubl\351';
	*/
	} else {	
		
		$('#type_vendre_select').hide();
		$('#type_location_select').show();
		if(document.getElementById('what2') != null){
			document.getElementById('what2').selectedIndex=''; 
		}
		document.getElementById('what').selectedIndex='';
		/*
		typeDropDown.options.length = 6;
		typeDropDown.options[2].value = '011';
		typeDropDown.options[2].text = 'Appartement';
		typeDropDown.options[1].value = '012';
		typeDropDown.options[1].text = 'Loft';
		typeDropDown.options[0].value = '013';
		typeDropDown.options[0].text = 'Maison';
		typeDropDown.options[3].value = '017';
		typeDropDown.options[3].text = 'Garage';
		typeDropDown.options[4].value = '018';
		typeDropDown.options[4].text = 'Etudiant';
		typeDropDown.options[5].value = '019';
		typeDropDown.options[5].text = 'Meubl\351';
		*/
	}	
	updateSelected('what', '');
	$('#subtypeSelect').hide();
	
}
function updateTypeDropDown(typeTransact,typeBien,typeBienTexte) {
	var idSelectTypeBien = '';
	
	if(typeTransact == "5")
	{
		idSelectTypeBien="what";
		$('#type_vendre_select').show();
		$('#type_location_select').hide();
		$('#subtypeSelect').hide();
	}
	else
	{
		idSelectTypeBien="what2";
		$('#type_vendre_select').hide();
		$('#type_location_select').show();
		$('#subtypeSelect').show();
	}

	
	$('#typeBien').val(typeBien);
	updateSelectedText(idSelectTypeBien,typeBienTexte);
	checkRightType();
}

function checkRightType(){
	if($('#newsearch').val()== 'ok'){
		//alert($('#what').val());
		$('#typeBien').val($('#what').val());
	}
	else{
		var transact = '';
		var type = '';
		if($('#vente').is(':checked')){transact = $('#vente').val();}
		else {transact = $('#location').val();}
		if (transact == "5") {
			type = $('#what').val();
		}
		else {
			type = $('#what2').val();
		}
		//alert(transact + " " + type);
		$('#typeBien').val(type);
	}
	
	if($('#vente').is(':checked')){$('#typeBien').val($('#what').val())}
	else {$('#typeBien').val($('#what2').val())}
	
	/* pour cacher le sous type si ce n'est pas un appart */
	if($('#location').is(':checked')){
		if ($('#what2').val() == '011')
		{
			$('#subtypeSelect').show();
		}
		else
		{
			$('#subtypeSelect').hide();
		}
	}
	
}



function updateAllDropDown(transact) {
	//alert('update allDropDown for'+transact);
	updateTypeDropDown(transact) ;
}
function initTypeDropDown() {
	if ($('#location').is(':checked')){
		updateTypeDropDown("1");
	}
	else{
		updateTypeDropDown("5");
	}
}
function initTypeDropDown(transac) {
	updateTypeDropDown(transac);
}
function initTypeDropDown(typeTransac,typeBien,typeBienTexte) {
	updateTypeDropDown(typeTransac,typeBien,typeBienTexte);
}
function toggleRegion() {
	villeOk=false;
	$('#inputString').val('');
	$('#idMonoCity').val('');
	
	var regionIndex = document.getElementById('where').selectedIndex;
	if (document.getElementById('where').options[regionIndex].value=='2') {
		document.getElementById('npdc').style.display='';
		document.getElementById('npdc2').style.display='';
	} else if (document.getElementById('where').options[regionIndex].value=='3'){
		document.getElementById('npdc').style.display='none';
		document.getElementById('npdc2').style.display='none';
	} else if (document.getElementById('where').options[regionIndex].value=='0') {
		document.getElementById('npdc').style.display='none';
		document.getElementById('npdc2').style.display='none';
	} else if (document.getElementById('where').options[regionIndex].value=='4') {
		document.getElementById('npdc').style.display='none';
		document.getElementById('npdc2').style.display='none';
	}else if (document.getElementById('where').options[regionIndex].value=='5') {
		document.getElementById('npdc').style.display='';
		document.getElementById('npdc2').style.display='';
	}
}

function valider() {
	if(villeOk == true)
	{
		return true;
	}
	else
	{
		var idRegion = $('#idRegion').val() ;
		var where = $('#where').val() ;
		
		if ( typeof(idRegion) == 'undefined' )
		{
			idRegion = where;
		}

		if ( idRegion != 2 && idRegion != 5 )
		{
			return true;
		}
		else
		{
			var idVille = $('#idMonoCity').val();
			if( idVille != '' && typeof(idVille) != 'undefined' )
			{
				 return true;
			 }
			 else
			 {
				 alert('Saisissez la ville pour lancer votre recherche');
				 return false;
			 }
		}
	}
}
function mapViewer(etat){
	if(etat == "visible"){
		$('div#carte').show();
	}
	else {
		$('div#carte').hide();
	}
}
function printSubType(){
	var selected = $('#what').val();
	if (typeof(selected) != 'undefined')
	{
		if(document.getElementById('what2') != null){
			var selected2 = document.getElementById('what2').value;
		}
		if (selected == "011" || selected2 == "011" ){
			$('#subtypeSelect').show();
		}
		else {
			$('#subtypeSelect').hide();
		}
	}
}
function printSubTypeConstruct(){
	var selected = document.getElementById('what3').value;
	if (selected == "024"){
		$('#subTypeHouse').show();
		$('#subTypeGround').hide();
	}
	else {
		$('#subTypeHouse').hide();
		$('#subTypeGround').show();
	}
}

function printSubTypeHouse(){        
	var selected = document.getElementById('what3').value;        
	if (selected == "024"){  
		$('#subTypeHouse').show();        
	}        
	else {  
		$('#subTypeHouse').hide();      
	}
}

function lookup(contextUrl,inputString, mode) {
	
	if ($('#idRegion').val() == 2 || $('#idRegion').val() == 5)
	{
		//on remet villeOk a false car on doit reselectionner la ville souhaite
		villeOk=false;
	}
	
	var region;
	//alert('mode:'+mode+' input: '+inputString);
	
    if(inputString.length == 0) {
        // Hide the suggestion box.
        $('#suggestions').hide();
    } else {
    	if(inputString.length > 2) {
    	if (mode==1){
    		region=document.getElementById("where")[document.getElementById("where").selectedIndex].value;
    	}else if(mode==2){
    	//If we are on the result search, idRegion is used to specify the selected region
    		region = document.getElementById("idRegion").value;
    	}else if(mode==3){
    	//If It's an agency search
    		region="selective";
    	}
    	
        $.post(contextUrl+"/searchCity.do", {autocomplete_parameter_city: ""+$.trim(inputString)+"",autocomplete_parameter_region:""+region+""}, function(data){
         
            if(data.length >0) {
	                $('#suggestions').show();
	                $('#autoSuggestionsList').html(data);
	                $('#suggestions').css("visibility","visible");
            }
        });
    }}
}
function fill(thisValue,id_ville) {
    $('#inputString').val(thisValue);
    $('#idMonoCity').val(id_ville);
    $('#suggestions').hide();
    villeOk=true;
}

function writeStats(action,webid,user,type) {
	if(action.length>0&&webid.length>0&&user.length>0&&type.length>0){
		$.post("/vdnimmo/writeStats.do",{ACTION: ""+action+"",ID: ""+webid+"",USERID: ""+user+"",TYPE: ""+type+""})
	}
}
function validSearchDistrict(context,district) {
	// On récupère la valeur courante du select
	var choice = $("#"+district+" :selected").val();
	// On construit l'url
	if (choice != "none"){
		urlRedirectBase = context+"/infos/recherche-geographique/immobilier-"+choice+".html";
		window.location = urlRedirectBase;
	}
	else {
		alert("Vous n'avez pas sélectionné de secteur.");
	}
}

function sousMenuOver(id)
{
	$("#"+id).css("top","29px");
	$("#"+id).css("display", "block");
	$("#"+id).css("z-index","1000");
}

function sousMenuOut(id)
{
	$("#"+id).css("top","29px");
	$("#"+id).css("display", "block");
	$("#"+id).css("z-index","1000");
	
	window.setTimeout(function() {
		$("#"+id).css("display", "none");
	}, 5000);
}

function supprAccent(chaine) {
	tmp = chaine.replace(/[éèêë]/g,"e");
	tmp = tmp.replace(/[àâä]/g,"a");
	tmp = tmp.replace(/[îï]/g,"i");
	tmp = tmp.replace(/[ôö]/g,"o");
	tmp = tmp.replace(/[ùûü]/g,"u");
	return tmp;
}

function validFormRecherche() {
	/*// 2 = npdc 5=autres dept
	if( $("#idRegion").val() != 2 && $("#idRegion").val() != 5)
	{
		if( !villeOk )
		{
			//si la ville etait sauvegarder
			if ( $("#inputString").length > 0 )
			{
				alert('input > 0 et parametre : <c:out value="${VdnSearchForm.autocomplete_parameter}"/>');
				return true;
			}
			else
			{
				alert( <c:out value='${VdnSearchForm.autocomplete_parameter}'/> );
				alert('ville non sauvegarder');
			}
		}
		else
		{
			alert('villeOk = '+ villeOk);
			return false;
		}
	}
	else
	{
		alert('pas npdc ou autres dept');
		return true;
	}*/
	return true;
}

function ongletActif(numeroOnglet)
{
	if ( $('#onglets').length )
	{
		$('#onglets').children().removeClass('ui-tabs-selected');
	
		var index=0;
		$('#onglets').children().each(function(){
			index++;
			if (numeroOnglet == index)
			{
				$(this).addClass('ui-tabs-selected');
			}
		});
	}
}
