/**
 * Objet javascript permettant de gérer les données d'affichage
 * du "bloc" d'un profil utilisateur.
 */
var TravelIndividualBooking= {
		bookingEngineViewBean: null,
		leisureGroupBookingEngineViewBean: null,
    
    /**
     * Liste des noms d'objets à utiliser.
     */
		getBeans: function() {
        return "BookingEngineViewBean;LeisureGroupBookingEngineViewBean";
    },
    
    /**
     * Initialisation de la variable JSON
     */
    initJSON: function() {
        var num;
    	try {
        	for( num in beans){
              if("BookingEngineViewBean" == beans[num].nom) {
                    bookingEngineViewBean = beans[num].bean;
            	} else if("LeisureGroupBookingEngineViewBean" == beans[num].nom) {
                    leisureGroupBookingEngineViewBean = beans[num].bean;            	
            	}
            }
        } catch(e) {
    		alert(e);
    	}
    },
        
    /**
     * Ecrivez votre code sur l'initialisation des affichage de la page
     * ici.
     * Cette méthode est appelée sur le onload de la page.
     */
    rewrite: function() {
    	  this.rewriteForm();
    	  if($("allBrands_YES"))
    	  	Event.observe($("allBrands_YES"), "click", function(event){TravelIndividualBooking.rewriteBrandSelection();});
    	  if($("allBrands_NO"))
    	  	Event.observe($("allBrands_NO"), "click", function(event){TravelIndividualBooking.rewriteBrandSelection();});
    	  	
    	  Event.observe($("submit_group"),'click',	function(event) {this.cleanInputFields();}.bind(this));	
    },
    
    rewriteForm: function(){
    	    	
    	if(document.forms["TRAVEL_GROUP_BOOKING"] && bookingEngineViewBean){
    		var formular = document.forms["TRAVEL_GROUP_BOOKING"];
    	   
    	  if(formular.code_chaine_IBI){
      		Event.observe(formular.code_chaine_SOF, "click", function(event){gestionAffichIBI();});    		
  				Event.observe(formular.code_chaine_PUL, "click", function(event){gestionAffichIBI();});
  				Event.observe(formular.code_chaine_MGA, "click", function(event){gestionAffichIBI();});
  				Event.observe(formular.code_chaine_NOV, "click", function(event){gestionAffichIBI();});
  				Event.observe(formular.code_chaine_MER, "click", function(event){gestionAffichIBI();});
  				Event.observe(formular.code_chaine_SUI, "click", function(event){gestionAffichIBI();});
  				Event.observe(formular.code_chaine_IBI, "click", function(event){gestionAffichIBI();});
  				Event.observe(formular.code_chaine_ASE, "click", function(event){gestionAffichIBI();});
  				Event.observe(formular.allBrands[0], "click", function(event){decocheTout();gestionAffichIBI();});
				}	
  			
  			Event.observe(formular.chambre_simples, "change", function(event){calcule_nb_chambre('chambre_simples');});
				Event.observe(formular.chambre_doubles, "change", function(event){calcule_nb_chambre('chambre_doubles');});
        Event.observe(formular.chambre_twins, "change", function(event){calcule_nb_chambre('chambre_twins');});
  			Event.observe(formular.chambre_triples, "change", function(event){calcule_nb_chambre('chambre_triples');});  			
  			Event.observe(formular.chambre_quadruples, "change", function(event){calcule_nb_chambre('chambre_quadruples');});
  			
        Event.observe(formular.agencyTypeIA, "click", function(event){showHideContratField(formular.name);setDefaultValuesTravelGroupBooking();});
  			Event.observe(formular.agencyTypeRT, "click", function(event){showHideContratField(formular.name);setDefaultValuesTravelGroupBooking();});
  			Event.observe(formular.agencyTypeSC, "click", function(event){showHideContratField(formular.name);setDefaultValuesTravelGroupBooking();});            		    				
	
	    	// société sc/contrat
        if (bookingEngineViewBean.reserverType=="SC"){
          formular.agencyType[2].checked=true;
          document.getElementById('bloc_agencyId').style.display = 'none'
          document.getElementById('bloc_agencyAccountId').style.display = 'block'
          if(bookingEngineViewBean.companyReserverId){
            formular.agencyAccountId.value=bookingEngineViewBean.companyReserverId;
          }
          if(bookingEngineViewBean.companyReserverContract){  
	    	    formular.agencyAccountContract.value=bookingEngineViewBean.companyReserverContract;
	    		}
	    		
        }  
        // AgVoy (IATA/RT + num)
        else{
          if(bookingEngineViewBean.agencyReserverId){
            formular.agencyId.value=bookingEngineViewBean.agencyReserverId;
          }
          document.getElementById('bloc_agencyId').style.display = 'block'
          document.getElementById('bloc_agencyAccountId').style.display = 'none'
          if (bookingEngineViewBean.reserverType=="IA"){
            formular.agencyType[0].checked=true;          
          } 
          else if (bookingEngineViewBean.reserverType=="RT"){
        	 formular.agencyType[1].checked=true;        	
          } 
        }		    	      	    		    		    	
        
        if(getUrlParameter("search")==null || getUrlParameter("search")=="" || getUrlParameter("search")!="new"){
	    		if(bookingEngineViewBean.destination)
	    			formular.hotel_ou_ville.value=bookingEngineViewBean.destination;
	    		if(bookingEngineViewBean.dayIn && bookingEngineViewBean.monthIn && bookingEngineViewBean.yearIn){
		    		formular.jour_arrivee.value=bookingEngineViewBean.dayIn;
		    		formular.mois_arrivee.value=bookingEngineViewBean.monthIn;
		    		formular.annee_arrivee.value=bookingEngineViewBean.yearIn;
		    		//selectOption(formular.nb_nuit.id, bookingEngineViewBean.nightsNumber)
		    		formular.nb_nuit.value = bookingEngineViewBean.nightsNumber;
		    		this.rewriteDate(formular);	    
	        }         	
	      	
		    	if(document.forms["TRAVEL_GROUP_BOOKING"] && leisureGroupBookingEngineViewBean){
		    	  // dossier
	      	  if($("ref_tour"))
	            $("ref_tour").value=leisureGroupBookingEngineViewBean.tourReference;      
	          // repas  
	      		if($("petit_dejeuner") && leisureGroupBookingEngineViewBean.breakfastType)      
	      			selectOption("petit_dejeuner", leisureGroupBookingEngineViewBean.breakfastType);      			
	      		if($("dejeuner") && leisureGroupBookingEngineViewBean.lunchType)
	      			selectOption("dejeuner", leisureGroupBookingEngineViewBean.lunchType);
	      		if($("diner") && leisureGroupBookingEngineViewBean.dinerType)
	      			selectOption("diner", leisureGroupBookingEngineViewBean.dinerType);
	          // chambres        
	      		if($("chambre_simples") && leisureGroupBookingEngineViewBean.singleRoomNumber)      
	      			$("chambre_simples").value=leisureGroupBookingEngineViewBean.singleRoomNumber;      
	      		if($("chambre_doubles") && leisureGroupBookingEngineViewBean.doubleRoomNumber)      
	      			$("chambre_doubles").value=leisureGroupBookingEngineViewBean.doubleRoomNumber;      
	      		if($("chambre_twins") && leisureGroupBookingEngineViewBean.twinRoomNumber)      
	      			$("chambre_twins").value=leisureGroupBookingEngineViewBean.twinRoomNumber;      
	      		if($("chambre_triples") && leisureGroupBookingEngineViewBean.tripleRoomNumber)      
	      			$("chambre_triples").value=leisureGroupBookingEngineViewBean.tripleRoomNumber;      
	      		if($("chambre_quadruples") && leisureGroupBookingEngineViewBean.quadrupleRoomNumber)      
	      			$("chambre_quadruples").value=leisureGroupBookingEngineViewBean.quadrupleRoomNumber;
	          // calcul nb chambres        
	      		calcule_nb_chambre();	    		    	
		    	}
		
					this.setBrandSelection();
				}
	    	
    	}
	  },
	  
	  rewriteDate: function(formular) {
			var arrival_date = new Date(formular.annee_arrivee.value, formular.mois_arrivee.value-1, formular.jour_arrivee.value);
			var arrival_date2 =arrival_date.print(Calendar._TT["DEF_DATE_FORMAT"]);
			if(formular.arrivee){
				formular.arrivee.value = arrival_date2;
				fireEvent(formular.arrivee, 'change');				
			}	
	  },

	  setBrandSelection: function(formular) {
	    	var chainSelection = getUrlParameter("code_chaine");
	    	if(chainSelection){
	    		$("allBrands_NO").checked=true;
	    		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;
		        }
					)  	  		
				}								
	  },	  
	  	  	     
    cleanInputFields: function(){
      var formular = document.forms["TRAVEL_GROUP_BOOKING"];      
      Cleaner.cleanDefaultValue(formular.agencyId);
      Cleaner.cleanDefaultValue(formular.hotel_ou_ville);
      Cleaner.cleanDefaultValue(formular.agencyAccountId);
      Cleaner.cleanDefaultValue(formular.agencyAccountContract);
      Cleaner.cleanDefaultValue(formular.ref_tour);        
    }	  	  	  
}

core.push(TravelIndividualBooking);

/**
 * Mettez ci-dessous les méthodes utilisées par la page, utilisant le JSON.
 * Toute méthode n'ayant pas de rapport avec le JSON ne sera pas acceptée.
 */
 
function calcule_nb_chambre(nom_champ) {
  if(nom_champ){
    champ = document.TRAVEL_GROUP_BOOKING[nom_champ];
    if(isNaN(champ.value)){champ.value=0;}
  }  
  somme = (Number(document.TRAVEL_GROUP_BOOKING.chambre_doubles.value) + Number(document.TRAVEL_GROUP_BOOKING.chambre_triples.value) + Number(document.TRAVEL_GROUP_BOOKING.chambre_simples.value) + Number(document.TRAVEL_GROUP_BOOKING.chambre_quadruples.value) + Number(document.TRAVEL_GROUP_BOOKING.chambre_twins.value))
  if(isNaN(somme)){ somme = 0;}  
	document.getElementById('nb_chambres').innerHTML = somme;
}

function gestionAffichIBI() {
  var nb_check=0;
  var hotelCheckBox = new Array('PUL','SOF','NOV','MER','IBI');
  var len = hotelCheckBox.length; 
	for(var i=0 ; i<len; i++){
		if($('code_chaine_'+hotelCheckBox[i]).checked==true){
			nb_check++;
		}
	}
	if(nb_check==1 && $('code_chaine_IBI').checked==true){
		affichageIBI('IBIseul');
	}
	else{
		affichageIBI('nonIBI');
	}
}

function decocheTout(){
  if($('allBrands_YES').checked == true){
    var formular = document.forms["TRAVEL_GROUP_BOOKING"];
    formular.code_chaine_SOF.checked = false;    		
    formular.code_chaine_PUL.checked = false;
    formular.code_chaine_MGA.checked = false;
    formular.code_chaine_NOV.checked = false;
    formular.code_chaine_MER.checked = false;
    formular.code_chaine_SUI.checked = false;
    formular.code_chaine_IBI.checked = false;
    formular.code_chaine_ASE.checked = false;
  }  	
}


function affichageIBI(affich){
	if(affich=='IBIseul'){
    document.getElementById('petitDejNonIBI').style.display = 'none'
	  document.getElementById('petitDejIBI').style.display = 'block'
    document.getElementById('chambreNonIBI').style.display = 'none'	
    document.TRAVEL_GROUP_BOOKING.chambre_twins.value='0';
    document.TRAVEL_GROUP_BOOKING.chambre_triples.value='0';
    document.TRAVEL_GROUP_BOOKING.chambre_quadruples.value='0';
    calcule_nb_chambre();
    }
  else{    
	  document.getElementById('petitDejIBI').style.display = 'none'
	  document.getElementById('petitDejNonIBI').style.display = 'block'
	  document.getElementById('chambreNonIBI').style.display = 'block'
  }
}		

function showHideContratField(formName){
	if(document.forms[formName].agencyType[2].checked){
	  document.getElementById('bloc_agencyId').style.display = 'none'
	  document.getElementById('bloc_agencyAccountId').style.display = 'block'
    document.forms[formName].agencyId.value='';     		
	} else {		
	  document.forms[formName].agencyAccountId.value='';
	  document.forms[formName].agencyAccountContract.value='';
	  document.getElementById('bloc_agencyAccountId').style.display = 'none'
	  document.getElementById('bloc_agencyId').style.display = 'block'
	}
}
	
function setDefaultValuesTravelGroupBooking(){
  var formular = document.forms["TRAVEL_GROUP_BOOKING"];      
  Cleaner.setDefaultValue(formular.agencyId);
  Cleaner.setDefaultValue(formular.hotel_ou_ville);
  Cleaner.setDefaultValue(formular.agencyAccountId);
  Cleaner.setDefaultValue(formular.agencyAccountContract);
  Cleaner.setDefaultValue(formular.ref_tour);        
}	  	

 