/** 
 * Objet javascript permettant de gerer les donnees d'affinage
 * des hotels de la liste hotel.
 */
var HotelsFilter = {
    bookingEngineViewBean: null,
    hotelsViewBean : null,
    originViewBean : null,
    marqueArrayAll: new Array("SOF","PUL","MGA","NOV","MER","SUI","COR","IBI","ASE","ETP","FOR","VIL","HLB","CM","RDR","MOT","STD","ADG"),
    marqueArrayName: new Array("Sofitel","Pullman","Mercure Gallery","Novotel","Mercure","Suitehotel","Coralia Club","Ibis", "All Seasons Hotels","Etaphotel","Formule1","Village Hotel","Lucien BarriÃ¨re","Club Med","Red Roof Inns","Motel 6","Studio 6","Adagio"),
    critereArrayAll: new Array("restaurant","swimmingPool","parking","meetingRoom","wifi","petAllowed"),
    critereArraySvlt: new Array("restaurant","swimmingpool","parking","meetingroom","wifi","pet"),
    localisationArrayAll: new Array("TOW","SUB","AIR"),
    isServicesHotel: 0,
    isMarque: 0,
    isLocalisation: 0,
    
    getBeans: function() {
      return "BookingEngineViewBean;HotelsViewBean";
    },
    
    /**
     * Initialisation/Mise a jour de la variable JSON
     */
    initJSON: function()
    {
        var num;
        try {
            for( num in beans) {
                // A cause de prototype, il ne faut recuperer que nos objet et pas ceux de prototype.
                if(beans.hasOwnProperty(num)) {
                    if("BookingEngineViewBean" == beans[num].nom && null != beans[num].bean)
                        this.bookingEngineViewBean = beans[num].bean;
                        
                    if("OriginViewBean" == beans[num].nom && null != beans[num].bean) 
                        this.originViewBean = beans[num].bean;
                        
                    if("HotelsViewBean" == beans[num].nom && null != beans[num].bean) 
                        this.hotelsViewBean = beans[num].bean;
                }
            }
        }
        catch(e) {
            //console.log(e);
        }
    },
    
    rewrite: function() {
        this.displayFilterBloc();
    	this.displayBrandsFilter();
    	this.displayLocationFilter();
    	this.displayServicesFilter();
    
    	var avaiblableCount = 0;
    	for(var count=0 ; count < this.hotelsViewBean.hotelsNumber ; count++)
    		if (this.hotelsViewBean.hotels[count].available==true)
    			avaiblableCount++;
    	// Gestion de l'affichage du bloc d'affinage de recherche
    	var elem = $('bloc_affine_rech');
    	if (elem) {
    		if((this.isServicesHotel==1 || this.isMarque==1 || this.isLocalisation==1) && avaiblableCount>1 || this.bookingEngineViewBean.context == 'meetingBookOnline' )  {
    			elem.style.display="block";
    			/*if($('colonne_gauche_inner'))
    			    $('colonne_gauche_inner').style.display="block";*/
    		}
    		else {
    			elem.style.display="none";
    			/*if($('colonne_gauche_inner'))
    			    $('colonne_gauche_inner').style.display="none";*/
    		}
    	}
    
    
    	// Gestion d'affichage du nombre d'hotel
    	elem = $('nbHotel');
    	if (elem)
    		if (avaiblableCount == 0)
    			elem.style.display="none";
    
    
    	$('filtre').nom_ville.value= this.bookingEngineViewBean.destination;
    	if(null != this.bookingEngineViewBean.dayIn && null != this.bookingEngineViewBean.monthIn
    	    && null != this.bookingEngineViewBean.yearIn && null != this.bookingEngineViewBean.nightsNumber) {
    		$('filtre').jour_arrivee.value=this.bookingEngineViewBean.dayIn;
    		$('filtre').mois_arrivee.value=this.bookingEngineViewBean.monthIn;
    		$('filtre').annee_arrivee.value=this.bookingEngineViewBean.yearIn;
    		$('filtre').nb_nuit.value=this.bookingEngineViewBean.nightsNumber;
    	}
    	// Positionne la bonne url sur le filtre
    	$('filtre').action='/rechercheGeographique.svlt';
    	this.calHotel();
    },
    
    /**
     * Affichage des blocs d'affinage des marques et de la localisation.
     */
    displayFilterBloc: function() {
    	if($("tit-marques")){
    		$("tit-marques").onclick=	function() {HotelsFilter.showDIVTri('marques')};
    		$("tit-marques").onmouseover=	function() {$("tit-marques").style.cursor="pointer"};
    	}
    	if($("tit-localisation")){
    		$("tit-localisation").onclick=	function() {HotelsFilter.showDIVTri('localisation')};
    		$("tit-localisation").onmouseover=	function() {$("tit-localisation").style.cursor="pointer"};
    	}
    },
    
    /**
     * Affichage du filtre des services
     */
    displayServicesFilter: function() {
    	//services a l hotel
    	$('services-hotel').innerHTML="";
    	for (var critere in this.critereArrayAll){
    	    if(this.critereArrayAll.hasOwnProperty(critere)) {
        		present = 0;
        		for(i=0;i<this.hotelsViewBean.hotelsNumber;i++){
        			nomCritere = eval('this.hotelsViewBean.hotels[i].'+this.critereArrayAll[critere]);
        				if (nomCritere == "1")
        					present=1;
        		}
        
        		if (present == 1){
        			this.isServicesHotel=1;
        			$('services-hotel').innerHTML+=('<p>&nbsp;<input type="checkbox" id="'+this.critereArrayAll[critere]+'" name="'+this.critereArraySvlt[critere]+'" value="1" onClick="javascript:HotelsFilter.calHotel();" /><label for="'+this.critereArrayAll[critere]+'"> '+I18N._("hotels.filter", this.critereArrayAll[critere])+'<\/label></p>');
        		} else {
        			$('services-hotel').innerHTML+=('<input type="hidden" id="'+this.critereArrayAll[critere]+'" name="'+this.critereArraySvlt[critere]+'" value="0" />');
        		}
        	}
    	}
    	if(this.isServicesHotel!=1){
    		$('services-hotel').style.display="none";
    		$('tit-services-hotel').style.display="none";
    		$('hr-marques').style.display="none";
    	}
    },
    
    /**
     * Affichage du filter des marques
     */
    displayBrandsFilter: function() {
        var siteBrandCode = this.getSiteBrand();
        // sur AH on affiche le filtre des marques.
        
        $('marques').innerHTML="";
    
      	for (var code in this.marqueArrayAll){
      	    if(this.marqueArrayAll.hasOwnProperty(code)) {
          		present = 0;
          		for(i=0;i<this.hotelsViewBean.hotelsNumber;i++){
          			if (this.hotelsViewBean.hotels[i].brandCode == this.marqueArrayAll[code]){
          				present = 1;
      				}
          		}
          		if (present == 1){
          			this.isMarque=1;
          			if (this.marqueArrayAll[code]!="ALL" && this.marqueArrayAll[code]==siteBrandCode)
          				$('marques').innerHTML+=('<p><input type="checkbox" id="'+this.marqueArrayAll[code]+'" name="'+this.marqueArrayAll[code]+'" value="1" onClick="javascript:HotelsFilter.calHotel();" checked="checked"/><label for="'+this.marqueArrayAll[code]+'"> '+this.marqueArrayName[code]+'<\/label></p>');
          			else
          				$('marques').innerHTML+=('<p><input type="checkbox" id="'+this.marqueArrayAll[code]+'" name="'+this.marqueArrayAll[code]+'" value="1" onClick="javascript:HotelsFilter.calHotel();" /><label for="'+this.marqueArrayAll[code]+'"> '+this.marqueArrayName[code]+'<\/label></p>');
          		} else {
          			$('marques').innerHTML+=('<input type="hidden" id="'+this.marqueArrayAll[code]+'" name="'+this.marqueArrayAll[code]+'" value="0" />');
          		}
          	}
      	}

    	if(siteBrandCode != "ALL") { //pas d affichage du bloc marque si la recherche se fait depuis un site marque
    		$('marques').style.display="none";
    		$('tit-marques').style.display="none";
    		$('hr-marques').style.display="none";
    		this.isMarque=0;
    	}
    },
    
    /**
     * Affichage du filtre de la localisation
     */
    displayLocationFilter: function() {
    	$('localisation').innerHTML="";
    	for (var local in this.localisationArrayAll){
    	    if(this.localisationArrayAll.hasOwnProperty(local)) {
        		present = 0;
        		for(i=0;i<this.hotelsViewBean.hotelsNumber;i++){
        			if (this.hotelsViewBean.hotels[i].environmentCode == this.localisationArrayAll[local])
        				present = 1;
        		}
        		if (present == 1){
        			this.isLocalisation=1;
        			$('localisation').innerHTML+=('<p>&nbsp;<input type="checkbox" id="'+this.localisationArrayAll[local]+'" name="'+this.localisationArrayAll[local]+'" value="1" onClick="javascript:HotelsFilter.calHotel();" /><label for="'+this.localisationArrayAll[local]+'"> '+I18N._("hotels.filter", this.localisationArrayAll[local])+'</label></p>');
        		} else {
        			$('localisation').innerHTML+=('<input type="hidden" id="'+this.localisationArrayAll[local]+'" name="'+this.localisationArrayAll[local]+'" value="0" />');
        		}
        	}
    	}
    	if(this.isLocalisation!=1){
    		$('localisation').style.display="none";
    		$('tit-localisation').style.display="none";
    		$('hr-localisation').style.display="none";
    	}
    },
    
    /**
     * affichage du div et du bouton + -
     */
    showDIVTri: function(DIVname) {
    	if($(DIVname).style.display=="none"){
    		$(DIVname).style.display="block";
    		$('tit-'+DIVname).className='fd-div-affine-moins';
    	} else {
    		$(DIVname).style.display="none";
    		$('tit-'+DIVname).className='fd-div-affine-plus';
    	}
    },
    
    /**
     * recherche du code du site
     */
    getSiteBrand: function() {
        var siteCode = "";
        var url = location.host;
        var urldecoup  = url.split (".");
        var brand = urldecoup[1].toLowerCase();
        switch(brand) {
            case "accorhotels": 
            case "meetings": 
            case "travelagencies": siteCode="ALL"; break;
            case "etaphotel": siteCode="ETP"; break;
            case "hotelformule1": siteCode="FOR"; break;
            case "ibishotel": siteCode="IBI"; break;
            case "mercure": siteCode="MER"; break;
            case "novotel": siteCode="NOV"; break;
            case "sofitel": siteCode="SOF"; break;
            case "suite-hotel": siteCode="SUI"; break;
            case "thalassa": siteCode="THA"; break;
            case "all-seasons-hotels": siteCode="ASE"; break;
        }
        return siteCode;
    },
    
    /**
     *
     */
    calHotel: function(){

    	if(this.hotelsViewBean.hotelsNumber!=0) {
    		var filtre=$('filtre');
    		// Marques
    		var marqueArray=new Array();
    		if (filtre.SOF.checked == true) {marqueArray.push("SOF");}
    		if (filtre.PUL.checked == true) {marqueArray.push("PUL");}
    		if (filtre.MGA.checked == true) {marqueArray.push("MGA");}
    		if (filtre.NOV.checked == true) {marqueArray.push("NOV");}
    		if (filtre.MER.checked == true) {marqueArray.push("MER");}
    		if (filtre.SUI.checked == true) {marqueArray.push("SUI");}
    		if (filtre.COR.checked == true) {marqueArray.push("COR");}
    		if (filtre.IBI.checked == true) {marqueArray.push("IBI");}
    		if (filtre.ASE.checked == true) {marqueArray.push("ASE");}
    		if (filtre.ETP.checked == true) {marqueArray.push("ETP");}
    		if (filtre.FOR.checked == true) {marqueArray.push("FOR");}
    		if (filtre.VIL.checked == true) {marqueArray.push("VIL");}
    		if (filtre.HLB.checked == true) {marqueArray.push("HLB");}
    		if (filtre.CM.checked == true) {marqueArray.push("CM");}
    		if (filtre.RDR.checked == true) {marqueArray.push("RDR");}
    		if (filtre.MOT.checked == true) {marqueArray.push("MOT");}
    		if (filtre.STD.checked == true) {marqueArray.push("STD");}
    		if (filtre.ADG.checked == true) {marqueArray.push("ADG");}
    
    		// Criteres
    		var critereArray=new Array();
    		if (filtre.restaurant && filtre.restaurant.checked == true) {critereArray.push("restaurant");}
    		if (filtre.swimmingPool && filtre.swimmingPool.checked == true) {critereArray.push("swimmingPool");}
    		if (filtre.parking && filtre.parking.checked == true) {critereArray.push("parking");}
    		if (filtre.meetingRoom && filtre.meetingRoom.checked == true) {critereArray.push("meetingRoom");}
    		if (filtre.wifi && filtre.wifi.checked == true) {critereArray.push("wifi");}
    		if (filtre.petAllowed && filtre.petAllowed.checked == true) {critereArray.push("petAllowed");}
    
    		// Criteres
    		var localArray=new Array();
    		if (filtre.AIR.checked == true) {localArray.push("AIR");}
    		if (filtre.SUB.checked == true) {localArray.push("SUB");}
    		if (filtre.TOW.checked == true) {localArray.push("TOW");}
    
    
    		// Nombre hotel
    		var nbrHotel = 0;
    		if (marqueArray.length > 0) {
    			// marque + critere
    			if (critereArray.length > 0 && localArray.length == 0) {
    				// Avec critere
    				for(brandCount=0; brandCount < marqueArray.length; brandCount++) {
					    for(i=0;i<this.hotelsViewBean.hotelsNumber;i++) {
						    var criteriaFound = 0;
						    if(this.hotelsViewBean.hotels[i].brandCode == marqueArray[brandCount]) {
    						    for(criteriaCount=0; criteriaCount < critereArray.length; criteriaCount++) {
        							if (eval("this.hotelsViewBean.hotels[i]."+critereArray[criteriaCount])) {
    								    criteriaFound++;
    								}
    							}
                                // on ajoute l'hotel si le nombre de critère de l'hotel correspond au 
                                // nombre de critere recherche.
        						if (criteriaFound == critereArray.length) {
        							nbrHotel++;
    							}
    						}
						}
					}
				}
    			//marque + localisation
    			else if (critereArray.length == 0 && localArray.length > 0) {
    				for(brandCount=0; brandCount < marqueArray.length; brandCount++) {
    					for(i=0;i<this.hotelsViewBean.hotelsNumber;i++) {
    						nomCritere = this.hotelsViewBean.hotels[i].environmentCode;
    						for(localCount=0; localCount < localArray.length; localCount++) {
    							if ((nomCritere ==localArray[localCount]) && (this.hotelsViewBean.hotels[i].brandCode == marqueArray[brandCount])) {
    								nbrHotel++;
    						    }
    						}
    					}
    				}
    			}
    			// marque + localisation + critere
    			else if (critereArray.length > 0 && localArray.length > 0) {
    				// Avec critere
    				for(brandCount=0; brandCount < marqueArray.length; brandCount++) {
    					for(hotelsCount=0;hotelsCount<this.hotelsViewBean.hotelsNumber;hotelsCount++) {
    					    if(this.hotelsViewBean.hotels[hotelsCount].brandCode == marqueArray[brandCount]) {
        						criteriaFound = 0;
        						for(criteriaCount=0; criteriaCount < critereArray.length; criteriaCount++) {
        							if (eval("this.hotelsViewBean.hotels[hotelsCount]."+critereArray[criteriaCount])) {
        								criteriaFound++;
    								}
    							}
    							
    							for(localCount=0; localCount < localArray.length; localCount++) {
        							if (this.hotelsViewBean.hotels[hotelsCount].environmentCode ==localArray[localCount]
        							    && criteriaFound == critereArray.length) {
        								nbrHotel++;
    								}
    							}
    						}
						}
					}
				}
				// marque seule
    			else /*if (critereArray.length == 0 && localArray.length == 0)*/ {
    			    for(brandCount=0; brandCount < marqueArray.length; brandCount++) {
    					for(i=0;i<this.hotelsViewBean.hotelsNumber;i++) {
    						if (this.hotelsViewBean.hotels[i].brandCode == marqueArray[brandCount]) {
    							nbrHotel++;
							}
					    }
					}
				}
    		}
    		else {
    			// Toutes les marques
    			if (critereArray.length > 0 && localArray.length == 0) {
    				// Avec critere
    				for(i=0;i<this.hotelsViewBean.hotelsNumber;i++) {
    					criteriaFound = 0;
    					for(criteriaCount=0; criteriaCount < critereArray.length; criteriaCount++) {
    						if (eval("this.hotelsViewBean.hotels[i]."+critereArray[criteriaCount])) {
    							criteriaFound++;
							}
						}
                        // on ajoute l'hotel si le nombre de critère de l'hotel correspond au 
                        // nombre de critere recherche.
    					if (criteriaFound == critereArray.length) {
						    nbrHotel++;
						}
					}
				}
    			//localisation
    			else if (critereArray.length == 0 && localArray.length > 0) {
    				// Avec critere
    				for(i=0;i<this.hotelsViewBean.hotelsNumber;i++) {
    					for(localCount=0; localCount < localArray.length; localCount++) {
    						if (this.hotelsViewBean.hotels[i].environmentCode ==localArray[localCount]) {
    							nbrHotel++;
							}
						}
					}
				}
				// localisation + critere
    			else if (critereArray.length > 0 && localArray.length > 0) { 
    			    for(i=0;i<this.hotelsViewBean.hotelsNumber;i++) {
    					criteriaFound = 0;
    					for(criteriaCount=0; criteriaCount < critereArray.length; criteriaCount++) {
    						if (eval("this.hotelsViewBean.hotels[i]."+critereArray[criteriaCount])) {
    							criteriaFound++;
							}
						}
						for(localCount=0; localCount < localArray.length; localCount++) {
    						if (this.hotelsViewBean.hotels[i].environmentCode ==localArray[localCount] 
    						    && criteriaFound == critereArray.length) {
    							nbrHotel++;
							}
						}
					}
				}
				// Sans critere
    			else /*if (critereArray.length == 0 && localArray.length == 0)*/ { 
    				nbrHotel += this.hotelsViewBean.hotelsNumber;
    			}
    		}
    
            var textehotels;
    		if (nbrHotel>1) {
    			textehotels = I18N._("hotels.filter","hotels match");
    		}
    		else {
    			textehotels = I18N._("hotels.filter","hotel match");
    		}
    
    		$('info').innerHTML = nbrHotel+" "+textehotels;
    		var buttonSubmit;
    		if (nbrHotel==0)
    			buttonSubmit='<img src="/'+this.originViewBean.codeLangDirectory+'/images/reservation/bt-aff-hotel-off.gif" alt="'+I18N._("hotels.filter", "No result")+'" title="'+I18N._("hotels.filter", "No result")+'" />';
    		else
    			buttonSubmit='<a href="#" onclick="document.filtre.submit();"><img src="/'+this.originViewBean.codeLangDirectory+'/images/reservation/bt-aff-hotel-on.gif" alt="'+I18N._("hotels.filter", "Show list of hotels")+'" title="'+I18N._("hotels.filter", "Show list of hotels")+'" /></a>';
    	    $('bt_recherche').innerHTML=buttonSubmit;
    	}
    }
}

core.push(HotelsFilter);