/**
 * Objet javascript permettant de gérer les données d'affichage de l'identification star
 * il concerne 3 pages : our offer, star, access codes
 * et gère 3 niveaux d'identification : non identifié, partiellement identifié, identifié  
 */
var Star= {
	  originViewBean: null,
		countriesViewBean: null,
		bookingEngineViewBean: null,
		actionErrorViewBean: null,
    
    /**
     * Liste des noms d'objets à utiliser.
     */
		getBeans: function() {
        return "CountriesViewBean;BookingEngineViewBean;OriginViewBean;ActionErrorViewBean";
    },
    
    /**
     * Initialisation de la variable JSON
     */
    initJSON: function() {
        var num;
    	try {
        	for( num in beans){
                if("CountriesViewBean" == beans[num].nom) {
                    countriesViewBean = beans[num].bean;
            	} else if("BookingEngineViewBean" == beans[num].nom) {
                    bookingEngineViewBean = beans[num].bean;
            	} else if("OriginViewBean" == beans[num].nom) {
                    originViewBean = beans[num].bean;
            	} else if("ActionErrorViewBean" == beans[num].nom) {
            				actionErrorViewBean = 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() {        
        // test du nom du formulaire pour déterminer la page sur laquelle faire le rewrite 
        if(document.forms["OUR_OFFER_IDENTIFICATION"]){
          Event.observe($("submit_ourOffer"),'click',	function(event) {this.cleanInputFieldsOurOffer();}.bind(this));
          this.rewriteOurOfferOrStar('OUR_OFFER');
        } else if(document.forms["STAR_IDENTIFICATION"]) {
          Event.observe($("submit_star"),'click',	function(event) {this.cleanInputFieldsStar();}.bind(this));
          this.rewriteOurOfferOrStar('STAR');
        } else {
          this.rewriteAccessCodes();                  
        }                    					
    },

    rewriteOurOfferOrStar: function(ourOfferOrStar)
    {
    	if(bookingEngineViewBean==null || ("IA"!=bookingEngineViewBean.agencyReserverType && "RT"!=bookingEngineViewBean.agencyReserverType)){
        // mode non identifié
        this.displayAndHideDiv(ourOfferOrStar+'_IDENTIFICATION_KO',new Array(ourOfferOrStar+'_IDENTIFICATION_partiel',ourOfferOrStar+'_IDENTIFICATION_OK'));
    	}	
    	else {
          if(bookingEngineViewBean.companyReserverId == null && bookingEngineViewBean.companyReserverContract == null ){
            // mode partiellement identifié
            this.displayAndHideDiv(ourOfferOrStar+'_IDENTIFICATION_partiel',new Array(ourOfferOrStar+'_IDENTIFICATION_KO',ourOfferOrStar+'_IDENTIFICATION_OK'));
          }
          else{
            // mode identifié
            this.displayAndHideDiv(ourOfferOrStar+'_IDENTIFICATION_OK',new Array(ourOfferOrStar+'_IDENTIFICATION_KO',ourOfferOrStar+'_IDENTIFICATION_partiel'));          
          }    			
    	}
	  },

    // affiche la div 1er param, masque les div dans tableau 2ème param 
    displayAndHideDiv: function(div_block,tab_div_none){    
      $(div_block).style.display="block";
      for (i=0;i<tab_div_none.length;i++){
	       $(tab_div_none[i]).style.display="none"; 
	    }	
    },
    
    rewriteAccessCodes: function()
    {
    	if(bookingEngineViewBean==null || ("IA"!=bookingEngineViewBean.agencyReserverType && "RT"!=bookingEngineViewBean.agencyReserverType)){
    	    // mode non identifié
    			//window.location="/"+originViewBean.codeLangDirectory+"/offers/index.shtml";
    	}	
    	else {
    	  // mode (partiellement) identifié
    		if($("ID_NOM_STRUCTURE"))
    			$("ID_NOM_STRUCTURE").value=bookingEngineViewBean.agencyReserverName;
    		if($("ID_NUM_IATA_TIDS"))
    			$("ID_NUM_IATA_TIDS").value=bookingEngineViewBean.agencyReserverId;
    		if($("ID_CHOIX_IATA") && $("ID_CHOIX_RT") && bookingEngineViewBean.agencyReserverType){
    			if("IA"==bookingEngineViewBean.agencyReserverType)
    				$("ID_CHOIX_IATA").checked=true;
    			else if("RT"==bookingEngineViewBean.agencyReserverType)
    				$("ID_CHOIX_RT").checked=true;    				
    		}
    			
    	}
    	this.rewriteCountriesStates();
	  },    
    
    rewriteCountriesStates: function()
    {
	    	if($("ID_PAYS")){
	    		fillSelect("ID_PAYS", countriesViewBean.countries, null, "-");
	    		if($("ID_ETAT")){
	    			this.rewriteStates("ID_PAYS","ID_ETAT","div_ID_ETAT");
	    			Event.observe("ID_PAYS", "change", function(event){ Star.rewriteStates("ID_PAYS","ID_ETAT","div_ID_ETAT");});
	    		}
	    	}
	  },
	  
    rewriteStates: function(countryId, stateId, stateBlockId)
    {
	    	if(countriesViewBean.countriesStates[$(countryId).value]){
	    		fillSelect(stateId, countriesViewBean.countriesStates[$(countryId).value], null, "-");
	    		$(stateBlockId).style.display="block";
	    	} else {
	    		$(stateBlockId).style.display="none";
	    		$(stateId).selectedIndex=0;
	    	}
    },

    
    // appel à l'action d'identification  doTravelAgencyLoginAction ou doStarLoginAction  
    doLoginAction: function(ourOfferOrStarAction, formName){     
    	var url = buildParamsUrl_ourOffer_usingNames(formName);
      if(ourOfferOrStarAction == 'doStarLoginAction'){
        url = url+buildParamsUrl_star_usingNames(formName);  
      } 		    	
    	updateView(ourOfferOrStarAction, "", url+"&"+beanToLoad);	
    	this.initJSON();  
    	if(actionErrorViewBean !=null){
    		$("actionErrorMessage").style.display = "block";
    		$("actionErrorMessage").innerHTML =  actionErrorViewBean.errorMessage;
    		
    	} else {
    		$("actionErrorMessage").style.display = "none";
    		$("actionErrorMessage").innerHTML =  '';
    		// si identif OK sur Our Offer on passe sur la page star
    		if(formName == "OUR_OFFER_IDENTIFICATION"){
          window.location="/"+originViewBean.codeLangDirectory+"/offers/special-travel-agent-rates/star.shtml";
        }
        // si identif OK sur Star on ré-écrit la page en mode partiellement identifié ou totalement identifié
        else{
          this.rewriteOurOfferOrStar('STAR');
        }
			}
  	  
    },
    
    cleanInputFieldsStar: function(){
      var formular = document.forms["STAR_IDENTIFICATION"];      
      Cleaner.cleanDefaultValue(formular.agencyId);
      Cleaner.cleanDefaultValue(formular.agencyAccountId);
      Cleaner.cleanDefaultValue(formular.agencyAccountContract);
    },		      
 
    cleanInputFieldsOurOffer: function(){
      var formular = document.forms["OUR_OFFER_IDENTIFICATION"];      
      Cleaner.cleanDefaultValue(formular.agencyId);
    }		      
 
 
}

core.push(Star);

/**
 * 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.
 */

// param url pour identif partielle - trav Ad  : iata/rt
function buildParamsUrl_ourOffer_usingNames(formName){			
	var agencyType = '';
  for (i=0;i<document.forms[formName].agencyType.length;i++){
	  if (document.forms[formName].agencyType[i].checked){
	  	agencyType = document.forms[formName].agencyType[i].value;
		}
	}	
	var agencyId = document.forms[formName].agencyId.value;			
	return '&agencyType='+agencyType+"&agencyId="+agencyId;
}

// param url pour identif complète - star  : sc/contrat
function buildParamsUrl_star_usingNames(formName){			
	var agencyAccountId = document.forms[formName].agencyAccountId.value;
	var agencyAccountContract = document.forms[formName].agencyAccountContract.value;			
	return '&agencyAccountId='+agencyAccountId+"&agencyAccountContract="+agencyAccountContract;
}

function doTravelAgencyLoginAction(){
    Star.doLoginAction('doTravelAgencyLoginAction','OUR_OFFER_IDENTIFICATION');
}

function doStarLoginAction(){
    Star.doLoginAction('doStarLoginAction','STAR_IDENTIFICATION');
}


function setDefaultValuesOurOfferIdentification(){
  if($("actionErrorMessage")){
    $("actionErrorMessage").style.display = "none";
    $("actionErrorMessage").innerHTML =  '';
  }
  var formular = document.forms["OUR_OFFER_IDENTIFICATION"];      
  Cleaner.setDefaultValue(formular.agencyId);        
}


function setDefaultValuesStarIdentification(){
  if($("actionErrorMessage")){
    $("actionErrorMessage").style.display = "none";
    $("actionErrorMessage").innerHTML =  '';
  }
  var formular = document.forms["STAR_IDENTIFICATION"];      
  Cleaner.setDefaultValue(formular.agencyId);
  Cleaner.setDefaultValue(formular.agencyAccountId);
  Cleaner.setDefaultValue(formular.agencyAccountContract);        
}
	