// ce javascript est utile pour remplir les champs des pages de contact: 
// SOF 6 contacts + meilleur prix garantie + presse

document.write('<script type="text/javascript" src="/scripts-v66/view/json/jsonrpc_ah.js"></script>');

Event.observe(window, 'load', onLoadEvent, false);
	
// Declaration des variables nom de tous les elements pouvant se trouver sur la page
var n_viewName = "formular_contact";

// Nom des beans
var n_OriginViewBean = "OriginViewBean";
var n_CountriesViewBean = "CountriesViewBean";
var n_BrandCountriesViewBean = "BrandCountriesViewBean";
var n_CurrenciesViewBean = "CurrenciesViewBean";
var n_ProfileViewBean = "ProfileViewBean";

//liste des beans a charger
var n_ListBean = n_CountriesViewBean
                 + ";" + n_OriginViewBean 
                 + ";" + n_BrandCountriesViewBean  
                 + ";" + n_CurrenciesViewBean
                 + ";" + n_ProfileViewBean;


function onLoadEvent(viewBeans) {
    initBeanList(n_ListBean);
 
		updateView(null, n_viewName, beanToLoad);
	}

function rewriteView(){

	try {
		var num;
		var countriesViewBean;
		var profileViewBean;
		var originViewBean;
		for( num in beans){
		  var bean = beans[num].bean;
			if(n_CountriesViewBean == beans[num].nom){
			   // pour maj de la combo pays et state
			   countriesViewBean = bean;
			} else if(n_BrandCountriesViewBean == beans[num].nom){
			   // maj des combo pays pour la marque
				if($("hotelLocation")) fillSelect("hotelLocation", bean.brandCountries, null,"-");
			} else if (n_CurrenciesViewBean == beans[num].nom){
		      // maj des combos devis
		      if ($("competitorCurrency")) fillSelectWithKeys("competitorCurrency", bean.currencies,"EUR",null);
		      if ($("currency")) fillSelectWithKeys("currency", bean.currencies,"EUR",null);
			} else if (n_ProfileViewBean == beans[num].nom){
		      // maj des infos du client si il existe
		      profileViewBean = bean;
			} else if (n_OriginViewBean == beans[num].nom){
		      // Pour connaitre langue
		      originViewBean = bean;
			}
		}
		buildProfileView(profileViewBean, countriesViewBean, originViewBean);       
		if(! (typeof SOF != "undefined") ){
		      // On initialise le formulaire si il existe!
		      if ( $(id_formContact) )
	            rewriteForm(originViewBean);
			    initEventHandlers(countriesViewBean, originViewBean);
		}
	} catch(e) {
		alert(e);
	}
}

// Pour remplir un select de civite:  a completer avec d'autres langues 
function fillSelectCivility(selectId, selectedValue, selectedLanguage, blankValue){
	var civility = { 
	                "de":{"M": "Frau", "A": "Fräulein","R": "Herr","D": "Doktor"},
                  "es":{"M": "Señora", "A": "Señorita","R": "Señor"},
                  "fr":{"M":"Mme", "A": "Mlle","R": "Mr"},
                  "en":{"M":"Mrs", "A": "Miss","R": "Mr"}  
                };
  if(civility[selectedLanguage]){
    fillSelect(selectId, civility[selectedLanguage], selectedValue, blankValue);
  } 
  else{
    fillSelect(selectId, civility["en"], selectedValue, blankValue);    
  }
}

//Pour les interfaces autres que Sofitel: a reverifier
function updateStateCombo(countryCode, countriesStatesMap, stateCode, stateComboId){
	if($(stateComboId+"_block") && $(stateComboId)){
		if(countriesStatesMap[countryCode]){								
			$(stateComboId+"_block").style.display="block";
			fillSelect(stateComboId, countriesStatesMap[countryCode], stateCode, "-");
		}	else {
			$(stateComboId+"_block").style.display="none";
			$(stateComboId).options.length=0;
		}
	}
}

/*Pour la construction des informations du profil*/
function buildProfileView(profileBean, countriesBean, originViewBean){
    
	if(profileBean){
		if(getElementByIdIfExists("civility").value == "" ){
		  fillSelectCivility("civility", profileBean.civilityCode, originViewBean.codeLang, "-");
		}
		if( profileBean.firstName ){
        if(getElementByIdIfExists("ID_PRENOM").value == "" ){
			    $("ID_PRENOM").value = profileBean.firstName;
		    }
		    if(getElementByIdIfExists("firstName").value == "" ){
		        $("firstName").value = profileBean.firstName;
		    }
    }		
		if( profileBean.lastName ){
	    if(getElementByIdIfExists("lastName").value == "" ){
			    $("lastName").value = profileBean.lastName;
		  }
		  if(getElementByIdIfExists("ID_NOM").value == "" ){
			    $("ID_NOM").value = profileBean.lastName;
		  }
		}
		if(profileBean.email){
	    if(getElementByIdIfExists("ID_EMAIL").value == ""){
			    $("ID_EMAIL").value = profileBean.email;
		  }
		  if(getElementByIdIfExists("email").value == "" ){
			    $("email").value = profileBean.email;
		  }    
    }  	
		if(getElementByIdIfExists("company").value == "" && profileBean.society){
			$("company").value = profileBean.society;
		}
		if(getElementByIdIfExists("ID_ADRESSE1").value == "" && profileBean.address1){
			$("ID_ADRESSE1").value = profileBean.address1;
		}
		if(getElementByIdIfExists("ID_ADRESSE2").value == "" && profileBean.address2){
			$("ID_ADRESSE2").value = profileBean.address2;
		}
		if(getElementByIdIfExists("ID_CODE_POSTAL").value == "" && profileBean.zipCode){
			$("ID_CODE_POSTAL").value = profileBean.zipCode;
		}
		if(getElementByIdIfExists("ID_VILLE").value == "" && profileBean.city){
			$("ID_VILLE").value = profileBean.city;
		}
		if(getElementByIdIfExists("ID_PAYS").value == ""){	
			fillSelect("ID_PAYS", countriesBean.countries, profileBean.country, "-");			
			if( countriesBean.countriesStates[profileBean.country]){
			    getElementByIdIfExists("ID_ETAT_block").style.display="block";
			    fillSelect("ID_ETAT", countriesBean.countriesStates[profileBean.country], profileBean.state, "-");
			}else{
		    getElementByIdIfExists("ID_ETAT_block").style.display="none";
		  }	
		}
		if(getElementByIdIfExists("ID_PAYS_o").value == ""){	
	    fillSelect("ID_PAYS_o", countriesBean.countries, profileBean.country, "-");  
		}	
		if( profileBean.phone ){
			if (getElementByIdIfExists("ID_TELEPHONE").value == ""){
		    $("ID_TELEPHONE").value = profileBean.phone;
		  }
			if (getElementByIdIfExists("telephone").value == ""){
		    $("telephone").value = profileBean.phone;
		  }
		}
		if( profileBean.phoneCode ){
			if(getElementByIdIfExists("phoneCode").value == "" ){
		    $("phoneCode").value = profileBean.phoneCode;
		  }
			if(getElementByIdIfExists("indicatifNumber").value == ""){
		    $("indicatifNumber").value = profileBean.phoneCode;
		  }
		}
		
		if(getElementByIdIfExists("fax").value == "" && profileBean.fax ){
			$("fax").value = profileBean.fax;
		}	
		if(getElementByIdIfExists("faxCode").value == "" && profileBean.faxCode ){
			 $("faxCode").value = profileBean.faxCode;
		}	
	}else{
    // On contruit les select necessaires pays et civilites
    if(getElementByIdIfExists("ID_PAYS").value == "" ){	
			fillSelect("ID_PAYS", countriesBean.countries, null, "-");
		}
		if(getElementByIdIfExists("ID_PAYS_o").value == "" ){	
	    fillSelect("ID_PAYS_o", countriesBean.countries, null, "-");  
		}
		if(getElementByIdIfExists("civility").value == "" ){
		  fillSelectCivility("civility", null, originViewBean.codeLang, "-");
		}
			
  }

}

function initEventHandlers(CountriesStatesViewBean, originViewBean){
	if($("ID_PAYS"))
		Event.observe("ID_PAYS", "change", function(event){ updateStateCombo($("ID_PAYS").value, CountriesStatesViewBean.countriesStates, null, "ID_ETAT");});
	if( $(id_selectContact) && ( $(id_selectContact).type.indexOf("select-") != -1) )
	  Event.observe(id_selectContact, "change", function(event){updateViewBySelect(originViewBean);});
	
}

var id_selectContact = "formName";
var id_formContact = "contact";
var id_inputMailSubject = "mailSubject";

if(!CONTACT) { 
    var CONTACT = { 
        resa:{name : "CONTACT_RESA"}, 
        relation:{name : "CONTACT_RELATIONS"}, 
        suggestion:{name : "CONTACT_SUGGESTIONS"}, 
        fid:{name : "CONTACT_FIDELITE"}, 
        probleme:{name : "CONTACT_PROBLEMES"} };
}

// Les differents sujets peuvent et doivent etre definis ailleurs: I18N... Pas utile à partir de V4.5
CONTACT.resa.mailSubject ={ 
    de: 'Contact :',
    es: 'Contact :',
    fr: 'Contact : resa: réservation internet sur le site Pullman',
    gb: 'Contact :',
    it: 'Contact :',
    nl: 'Contact :',
    pt: 'Contact :',
    zh: 'Contact :'};
		                    
CONTACT.relation.mailSubject ={ 
    de: 'Contact :',
    es: 'Contact :',
    fr: 'Contact : relation: réservation internet sur le site Pullman',
    gb: 'Contact :',
    it: 'Contact :',
    nl: 'Contact :',
    pt: 'Contact :',
    zh: 'Contact :'};
    
CONTACT.suggestion.mailSubject ={ 
    de: 'Contact :',
    es: 'Contact :',
    fr: 'Contact : suggestion: réservation internet sur le site Pullman',
    gb: 'Contact :',
    it: 'Contact :',
    nl: 'Contact :',
    pt: 'Contact :',
    zh: 'Contact :'};
        
CONTACT.fid.mailSubject ={ 
    de: 'Contact :',
    es: 'Contact :',
    fr: 'Contact : fid: réservation internet sur le site Pullman',
    gb: 'Contact :',
    it: 'Contact :',
    nl: 'Contact :',
    pt: 'Contact :',
    zh: 'Contact :'};          
            
CONTACT.probleme.mailSubject ={ 
    de: 'Contact :',
    es: 'Contact :',
    fr: 'Contact : probleme: réservation internet sur le site Pullman',
    gb: 'Contact :',
    it: 'Contact :',
    nl: 'Contact :',
    pt: 'Contact :',
    zh: 'Contact :'}; 
    


// On doit gérer le select
function updateViewBySelect(originViewBean){
  d_userLang = originViewBean.codeLang;
  $(id_formContact).name = getElementByIdIfExists(id_selectContact).value;
	if(getElementByIdIfExists(id_selectContact).value.indexOf(CONTACT.resa.name) != -1  ){
      $(id_inputMailSubject).value = CONTACT.resa.mailSubject[d_userLang];         
  } else if(getElementByIdIfExists(id_selectContact).value.indexOf(CONTACT.relation.name) != -1){
      $(id_inputMailSubject).value = CONTACT.relation.mailSubject[d_userLang];          
  } else if(getElementByIdIfExists(id_selectContact).value.indexOf(CONTACT.suggestion.name) != -1){
      $(id_inputMailSubject).value = CONTACT.suggestion.mailSubject[d_userLang];          
  } else if(getElementByIdIfExists(id_selectContact).value.indexOf(CONTACT.fid.name) != -1 ){
      $(id_inputMailSubject).value = CONTACT.fid.mailSubject[d_userLang];          
  } else if(getElementByIdIfExists(id_selectContact).value.indexOf(CONTACT.probleme.name) != -1){      
      $(id_inputMailSubject).value = CONTACT.probleme.mailSubject[d_userLang];          
  }
  // On ecrit les champs en fonction
	rewriteForm(originViewBean);
}


// On initialise le formulaire
function rewriteForm( originViewBean ){
  d_userLang = originViewBean.codeLang;
  //le minimum syndical
  if ($(id_formContact).name == "" ){  
	    $(id_formContact).name = getElementByIdIfExists(id_selectContact).value;
	    $(id_inputMailSubject).value = CONTACT.resa.mailSubject[d_userLang];
	}
	// On affiche les champs en fonction du nom du formulaire: definir si ceci doit etre realisé ici!!!
//	if($(id_formContact).name ==  CONTACT.resa.name ){
//     //TODO              
//  } else if($(id_formContact).name == CONTACT.relation.name ){
//     //TODO              
//  } else if($(id_formContact).name ==  CONTACT.suggestion.name ){
//     //TODO                
//  } else if($(id_formContact).name ==  CONTACT.fid.name ){
//     //TODO                  
//  } else if($(id_formContact).name ==  CONTACT.probleme.name ){
//     //TODO                   
//  }
}

