// require Url.js
/**
 * Objet javascript permettant de gérer les données d'affichage des moteurs CORPORATE
 *   
 */
var Corpo= {
		bookingEngineViewBean: null,
    fidelityCardsViewBean: null,
    userViewBean: null,
   
    /**
     * Liste des noms d'objets à utiliser.
     */
		getBeans: function() {        
        return "BookingEngineViewBean;FildelityCardsViewBean;UserViewBean";
    },

        
    /**
     * Initialisation de la variable JSON
     */
    initJSON: function() {
      bookingEngineViewBean   =  viewBeans["BookingEngineViewBean"];
      fidelityCardsViewBean   =  viewBeans["FildelityCardsViewBean"];
      userViewBean            =  viewBeans["UserViewBean"]; 
    },
        
    /**
     * Cette méthode est appelée sur le onload de la page.
     */
    rewrite: function() {  
               
      // vidage remplissage des zones de saisie                      
      Event.observe("companyId", 'focus', function(event){Cleaner.cleanDefaultValue($("companyId"));});
      Event.observe("companyId", 'blur' , function(event){Cleaner.setDefaultValue($("companyId"));}); 
      Event.observe("companyContract", 'focus', function(event){Cleaner.cleanDefaultValue($("companyContract"));});
      Event.observe("companyContract", 'blur' , function(event){Cleaner.setDefaultValue($("companyContract"));});              
      Event.observe("hotel_ou_ville", 'focus', function(event){Cleaner.cleanDefaultValue($("hotel_ou_ville"));});
      Event.observe("hotel_ou_ville", 'blur' , function(event){Cleaner.setDefaultValue($("hotel_ou_ville"));});
             
             
      // affichage code pref
      if($("code_preferentiel")){
    		  Event.observe("code_preferentiel", "click", function(event){affiche_ou_masquePref();});    		  
    		}
    		    
  
    		    
      // affichage carte de fid sur moteur complet 		    
      if ($("form_corpo") && fidelityCardsViewBean && fidelityCardsViewBean.fidelityCards){        
        var lang = getDirLangFromHtmlAttrib();
        var titre = '-';
        if(fidelityCardTitle[lang]){
          titre = fidelityCardTitle[lang];
        }                
        fillSelect("type_carte_fidelite", fidelityCardsViewBean.fidelityCards, "-", titre);
        Event.observe("porteur_carte", "click", function(event){affiche_ou_masqueFid();});        
        Event.observe("allBrands_NO", 'click' , function(event){affiche_ou_masqueBrands();});
        Event.observe("allBrands_YES", 'click' , function(event){affiche_ou_masqueBrands();});
  		}  		
  		else{
        // passage au moteur complet depuis le moteur light si on coche marque 
        Event.observe("allBrands_NO", 'click' , function(event){fireEvent($("moreOptions"), 'click');document.location.href=$("moreOptions").href;});
        // passage au moteur complet depuis le moteur light si on coche carte de fid, ou vidage des champs cachés si on décoche
        Event.observe("porteur_carte", 'click' , function(event){Corpo.onFidelityCardCheck();});
        // par défaut "carte de fid" et "Sélectionnez vos marques" sont décochés
        $("porteur_carte").checked = false;
        $("allBrands_YES").checked = true;
        $("allBrands_NO").checked = false;        
      }             
                  
      // rewrite destination                    
  		if(bookingEngineViewBean.destination)
  			$('hotel_ou_ville').value=bookingEngineViewBean.destination;
  		else if(bookingEngineViewBean.hotelCode)
  			$('hotel_ou_ville').value=bookingEngineViewBean.hotelCode;  			
  		// rewrite date	
  		if(bookingEngineViewBean.dayIn && bookingEngineViewBean.monthIn && bookingEngineViewBean.yearIn){
    		$('jour_arrivee').value=bookingEngineViewBean.dayIn;
    		$('mois_arrivee').value=bookingEngineViewBean.monthIn;
    		$('annee_arrivee').value=bookingEngineViewBean.yearIn;    		
    		$('nb_nuit').value = bookingEngineViewBean.nightsNumber;
    		this.rewriteDate();	    
      }       
      // rewrite sc/contrat
	    if(bookingEngineViewBean.companyReserverId){
          $('companyId').value=bookingEngineViewBean.companyReserverId;
      }  
      if(bookingEngineViewBean.companyReserverContract){      
  	     $('companyContract').value=bookingEngineViewBean.companyReserverContract;
	    }	  
      
    	// code préférentiel 	    		    		
    	if(bookingEngineViewBean.preferentialCode && $('code_avantage')){	
	    	$('code_preferentiel').checked = true;
	    	fireEvent($('code_preferentiel'), 'change');
			  fireEvent($('code_preferentiel'), 'click');
	    	$('code_avantage').value=bookingEngineViewBean.preferentialCode;			  
    	}
    	// carte Accor
      // sur formulaire détaillé les champs sont dévoilés et pré-remplis    	
    	if($('form_corpo') && (bookingEngineViewBean.fidelityCardType || bookingEngineViewBean.fidelityCardNumber)){	
	    	$('porteur_carte').checked = true;
	    	fireEvent($('porteur_carte'), 'change');
			  fireEvent($('porteur_carte'), 'click');
	    	if(bookingEngineViewBean.fidelityCardType)
          $('type_carte_fidelite').value=bookingEngineViewBean.fidelityCardType;
	    	if(bookingEngineViewBean.fidelityCardNumber)
          $('num_carte_fidelite').value=bookingEngineViewBean.fidelityCardNumber;
      // sur formulaire light les valeurs sont stockées en champs cachés
      // et la coche carte de fid est pré-cochée (sans passage au formulaire détaillé)          
      }else{
        if($('porteur_carte') && (bookingEngineViewBean.fidelityCardType || bookingEngineViewBean.fidelityCardNumber)){
          $('porteur_carte').checked = true;
          $('type_carte_fidelite').value=bookingEngineViewBean.fidelityCardType;
          $('num_carte_fidelite').value=bookingEngineViewBean.fidelityCardNumber;
        }
      
      }
                       
      // précoche marque si en provenance d'un site marque
      this.setBrandSelection();
      
      // pré-saisie des codes sc et contrat s'il ya eu identification       
      if(userViewBean && userViewBean.professional){
          if(userViewBean.professionalData.companyId){
            $("companyId").value = userViewBean.professionalData.companyId;
          }
          if(userViewBean.professionalData.contracts){
            $("companyContract").value = userViewBean.professionalData.contracts;
          }  
      }
  
      // action sur le submit      			
      Event.observe($("submit_group"),'click',	function(event) {Event.stop(event);this.onSubmitForm();}.bind(this));    
      				        			       		     			     		     			
    },

    rewriteDate: function() {
			var arrival_date = new Date($('annee_arrivee').value, $('mois_arrivee').value-1, $('jour_arrivee').value);
			var arrival_date2 =arrival_date.print(Calendar._TT["DEF_DATE_FORMAT"]);
			if($('arrivee')){
				$('arrivee').value = arrival_date2;
				fireEvent($('arrivee'), 'change');				
			}	
	  },

	  setBrandSelection: function(formular) {
	    	var chainSelection = Url.getUrlParameter("code_chaine");
	    	if(chainSelection){
	    		$("allBrands_NO").checked=true;
	    		//$('brandSelection').style.display = 'block';
	    		chainSelection.split("|").each(
						function(element){
			    		if($("code_chaine_"+element))
								$("code_chaine_"+element).checked=true;
		        }	    			
	    		)	    	
					this.rewriteBrandSelection();
				}
	  },

	  rewriteBrandSelection: function() {
	  		var checkboxes = $$("#brandSelection input");
  	  	if($("allBrands_YES").checked && $("brandSelection").style.display=="block"){
  	  		$("brandSelection").style.display="none";
					checkboxes.each(
						function(element){
							element.disabled=true;
		        }
					)  	  		
  	  	} else if($("allBrands_NO").checked && $("brandSelection").style.display=="none") {
  	  		$("brandSelection").style.display="block";
					checkboxes.each(
						function(element){
							element.disabled=false;
		        }
					)  	  		
				}								
	  },
	  
    // action sur la coche carte de fid du moteur light
    onFidelityCardCheck: function(){      
      if($('porteur_carte').checked == false){
        $('type_carte_fidelite').value='';
        $('num_carte_fidelite').value='';      
      }else{
        fireEvent($("moreOptions"), 'click');  
        document.location.href=$("moreOptions").href;    
      }
    
    
    },

    // actions sur le submit 
	  onSubmitForm: function(){               
      Cleaner.cleanDefaultValue($("hotel_ou_ville"));
      Cleaner.cleanDefaultValue($("companyId"));
      Cleaner.cleanDefaultValue($("companyContract"));
      if($("code_avantage")){
        Cleaner.cleanDefaultValue($("code_avantage"));
      }
      if($("num_carte_fidelite") && !$("num_carte_fidelite").type == 'hidden'){
        Cleaner.cleanDefaultValue($("num_carte_fidelite"));
      }
      document.CORPORATE_BOOKING.submit();      
    }
	  
	  
    

      
 
}
core.push(Corpo);

// remplissage des zones de saisie après retour validator   
function setDefaultValuesCorporateBooking(nomForm){
  Cleaner.setDefaultValue($("hotel_ou_ville"));
  Cleaner.setDefaultValue($("companyId"));
  Cleaner.setDefaultValue($("companyContract"));
}

 function affiche_ou_masqueFid(){
  if($("porteur_carte").checked==true){
    $("div_carte_accor_a").style.display="block";
  }else{
    $("div_carte_accor_a").style.display="none";
    $("type_carte_fidelite").value="";
    $("num_carte_fidelite").value="";
  }
 }
 
 function affiche_ou_masquePref(){
  if($("code_preferentiel").checked==true){
    $("div_code_pref_a").style.display="block";
  }else{
    $("div_code_pref_a").style.display="none";
    $("code_avantage").value="";
  }
 } 
 
  function affiche_ou_masqueBrands(){
    if ($('allBrands_YES').checked) {
			showHideBrandSelection(true);
		} else if ($('allBrands_NO').checked) {
			showHideBrandSelection();
		}
  }
  
  function showHideBrandSelection(hide) {
		$('brandSelection').style.display = hide ? 'none' : 'block';
	}

  fidelityCardTitle ={
      de: 'Kartentyp',
      gb: 'Type of card',
      es: 'Tipo de tarjeta',
      fr: 'Type de carte',        
      it: 'Tipo di carta',
      pt: 'Tipo de cartão',
      nl: 'Type van kaart',
      zh: '卡的类型'
  };	
