
var Contact = {

  EMPTY_COMBO: "-",
  initialized: false,
  lang: null,
  
	// liste des elements HTML utilises (non exhaustif)
  elm_formGarantie:null,
  elm_formContact:null,
  elm_mailSubject: null,
 	elm_country: null,
  elm_state: null,
  elm_phone: null,
  elm_phone_code: null,
  elm_resa_num: null,
  elm_civility: null,
  elm_prenom: null,
  elm_nom: null,
  elm_email: null,
  elm_company: null,
  elm_addr1: null,
  elm_addr2: null,
  elm_code_postal: null,
  elm_ville: null,
  elm_fax: null,
  elm_fax_code: null,
  
  initJSON: function() {
  },
  
	getBeans: function() {
		return "ProfileViewBean;CountriesViewBean;BrandCountriesViewBean;CurrenciesViewBean";
  },

  init: function() {
    if (!this.initialized) {
      
      // PR : press relation
      // MPG : meilleur prix garanti
      // CT : contact
      this.lang = getDirLangFromHtmlAttrib();
      this.elm_form         = this.$(["garantie", "contact"]);            // MPG + CT + PR
      this.elm_civility     = document.getElementById("civility");        // PR
      this.elm_prenom       = this.$(["ID_PRENOM", "firstName"]);         // MPG + CT + PR
      this.elm_nom          = this.$(["ID_NOM", "lastName"]);             // MPG + CT + PR
      this.elm_email        = this.$(["ID_EMAIL", "email"]);              // MPG + CT + PR
      this.elm_company      = document.getElementById("company");         // PR
      this.elm_addr1        = document.getElementById("ID_ADRESSE1");     // CT + PR
      this.elm_addr2        = document.getElementById("ID_ADRESSE2");     // CT 
      this.elm_code_postal  = document.getElementById("ID_CODE_POSTAL");  // CT + PR
      this.elm_ville        = document.getElementById("ID_VILLE");        // CT + PR
      this.elm_country      = document.getElementById("ID_PAYS");         // MPG + CT + PR
      this.elm_state        = document.getElementById("ID_ETAT");         // CT + PR
      this.elm_phone        = document.getElementById("ID_TELEPHONE");    // MPG + CT + PR
      this.elm_phone_code   = document.getElementById("ID_INDICATIF_TEL");// MPG + CT
      this.elm_fax          = document.getElementById("fax");             // MPG + PR
      this.elm_fax_code     = document.getElementById("faxCode");         // MPG + PR
      this.elm_resa_num     = this.$(["ID_RESA_CONF_NUM", "resaNumber"]); // MPG + CT
      this.elm_mailSubject  = document.getElementById("mailSubject");     // MPG + CT + PR
      this.initialized = true;
    }
  },
  
  $: function(ids) {
    var e = null;
    for (var i=0;i<ids.length;i++) {
      e = document.getElementById(ids[i]);
      if (e)
        return e;
    }
  },


  rewrite: function() {
    
    this.init();
    
    // cas Best Price Warrantee
    if (this.elm_form.id == "garantie") 
      this.elm_form.action = "javascript:submitFormBean('label_ok', 'label_ko' ,'/bestPriceWarranty.svlt', Contact.elm_form);";
    else
      this.elm_form.action = "javascript:submitFormBean('label_ok', 'label_ko' ,'/simpleform.svlt', Contact.elm_form);";

    this.rewriteForm(this.lang);
    this.rewriteFunctionality(this.lang);
  },


	rewriteForm: function (lang) {

    var country = null;
    var state = null;
    var beans = viewBeans;
    
    // Taille du champ n° resa
    if (this.elm_resa_num)
      this.elm_resa_num.setAttribute('maxlength', 8);
    
    // Reecriture combo pays pour les hotels de la marque
    var bean = beans["BrandCountriesViewBean"];
    if(bean)
    	fillSelect("hotelLocation", bean.brandCountries, null, this.EMPTY_COMBO);
    
    // Reecriture combos devis
    bean = beans["CurrenciesViewBean"];
    if (bean){
    		fillSelectWithKeys("competitorCurrency", bean.currencies, "EUR", null);
    		fillSelectWithKeys("currency", bean.currencies, "EUR", null);
    }
    
    // Reecriture pour utilisateurs identifies (sauf pays et etat et indicatif tel)
    bean = beans["ProfileViewBean"];
    if (bean) {
      if(this.elm_civility && this.elm_civility.value.length==0) {
    		var civ = civility[lang];
    	  if(!civ) civ = civility["en"];
    		fillSelect(this.elm_civility.id, civ, bean.civilityCode, this.EMPTY_COMBO);
    	}
      
    	this.fill(this.elm_prenom, bean.firstName);
    	this.fill(this.elm_nom, bean.lastName);
    	this.fill(this.elm_email, bean.email);
    	this.fill(this.elm_company, bean.society);
    	this.fill(this.elm_addr1, bean.address1);
    	this.fill(this.elm_addr2, bean.address2);
    	this.fill(this.elm_code_postal, bean.zipCode);
    	this.fill(this.elm_ville, bean.city);
    	this.fill(this.elm_phone, bean.phone);
    	this.fill(this.elm_fax, bean.fax);
    	this.fill(this.elm_fax_code, bean.faxCode);
    	country = bean.country;
    	state = bean.state;
    }
        
    bean = beans["CountriesViewBean"];
    if (bean) {

      // rewrite du pays internaute + list pays (combo)
      fillSelect(this.elm_country.id, bean.countries, country, this.EMPTY_COMBO);
      
      // Maj Etat sur selection du pays (Contacts uniquement) + rewrite
      Event.observe(this.elm_country, "change", function(event){Contact.rewriteStateCombo(null);});
      this.rewriteStateCombo(state);
      
      // Maj indicatif tel du pays (Meilleur Prix uniquement) + rewrite
      Event.observe(this.elm_country, "change", function(event){Contact.rewritePhoneCode();});
      this.rewritePhoneCode();

    }

	},


	fill: function (elem, str) {
		if (elem && Element.empty(elem))
			elem.value = str;
	},


	// Reecriture du mailSubject et du Form suivant la fonctionnalite choisie
	rewriteFunctionality: function (lang){

    var functionality = this.elm_form.name;
    var subj = "";
    var brand = this.getWebapp().capitalize();
    
    if(functionality.indexOf(CONTACT.resa.name) != -1){
    	subj = CONTACT.resa.mailSubject[lang] + " " + brand;
    } else if(functionality.indexOf(CONTACT.question.name) != -1){
    	subj = CONTACT.question.mailSubject[lang] + " " + brand;
    } else if(functionality.indexOf(CONTACT.relation.name) != -1){
    	subj = CONTACT.relation.mailSubject[lang] + " " + brand;
    } else if(functionality.indexOf(CONTACT.suggestion.name) != -1){
    	subj = CONTACT.suggestion.mailSubject[lang] + " " + brand;
    } else if(functionality.indexOf(CONTACT.fid.name) != -1){
    	subj = CONTACT.fid.mailSubject[lang] + " " + brand;
    } else if(functionality.indexOf(CONTACT.probleme.name) != -1){
    	subj = CONTACT.probleme.mailSubject[lang] + " " + brand;
    } else if(functionality.indexOf("BEST_PRICE_WARRANTY") != -1){
      subj = brand + " vous garanti le meilleur prix"; // subject pas utilise sur MPB, mais present pour evol vers simpleForm.svlt
    }

    if (subj.length > 0)
      this.fill(this.elm_mailSubject, subj);
	},

  // Pour la MAJ du combo des etats associes au pays
  rewriteStateCombo: function(state) {
    if (this.elm_state) {  
      var country = this.elm_country.value;
      try { 
        var states = viewBeans["CountriesViewBean"].countriesStates[country];
    		this.elm_state.disabled = '';
    		fillSelect(this.elm_state.id, states, state, this.EMPTY_COMBO);
      } catch (e) {}
      
      if(!states){
    		this.elm_state.disabled = 'true';
    		this.elm_state.options.length=0;
    	}
    }
  },
  
  // Pour la MAJ de indicatif pays
  rewritePhoneCode: function() {
    if (this.elm_phone_code) {
      var indicatif = viewBeans["CountriesViewBean"].countriesPhonePrefixes[this.elm_country.value];  
      if(!indicatif || indicatif==null) 
        indicatif = "";
      this.fill(this.elm_phone_code, indicatif);
    }
  },
  
 
  getWebapp: function() {
  	var h = document.location.host;
  	var app;
  	if (h.indexOf("accorhotels")>-1)   {app="accorhotels";}
  	else if (h.indexOf("sofitel")>-1)  {app="sofitel";}
  	else if (h.indexOf("novotel")>-1)  {app="novotel";}
  	else if (h.indexOf("mercure")>-1)  {app="mercure";}
  	else if (h.indexOf("ibis")>-1)     {app="ibis";}
  	else if (h.indexOf("etap")>-1)     {app="etaphotel";}
  	else if (h.indexOf("formule1")>-1) {app="formule1";}
  	else if (h.indexOf("suite")>-1)    {app="suitenovotel";}
  	else if (h.indexOf("thalassa")>-1) {app="thalassa";}
  	else if (h.indexOf("seasons")>-1)  {app="seasons";}
    else if (h.indexOf("pullman")>-1)  {app="pullman";}
  	else {app="accorhotels";}
  	return app;
  }

}

core.push(Contact);


var CONTACT = {
	resa:{name:"CONTACT_RESA"},
	question:{name:"CONTACT_QUESTION"},
	relation:{name:"CONTACT_RELATIONS"},
	suggestion:{name:"CONTACT_SUGGESTIONS"},
	fid:{name:"CONTACT_FIDELITE"},
	probleme:{name:"CONTACT_PROBLEMES"},
	garantie:{name:"GARANTIE"}
};

var civility = {"fr":{"M":"Mme", "A":"Mlle", "R":"Mr"},
  "en":{"M":"Mrs", "A":"Miss", "R":"Mr"},
  "de":{"M":"Frau", "A":"Fräulein", "R":"Herr","D":"Doktor"},
  "es":{"M":"Señora", "A":"Señorita", "R":"Señor"},
  "it":{"M":"Sig.ra", "A":"Sig.na", "R":"Sig.", "D":"Dr."}
};

// Les differents sujets peuvent et doivent etre definis ailleurs: I18N... Pas utile à partir de V4.5
CONTACT.resa.mailSubject ={
    de: 'Contact : Online Reservierung zur Website',
    fr: 'Contact : Question sur une reservation Internet', //PROD: Contact : réservation internet sur le site Suite Novotel
    en: 'Contact: Online reservation question',
    es: 'Contact: Online reservation question',
    it: 'Contact: Online reservation question'
};

CONTACT.question.mailSubject ={
    de: 'Contact : Fragen zu einem Sofitel Hotel',
    fr: 'Contact : Question sur un hotel', //PROD: Contact : questions hotel dans un Suite Novotel
    en: 'Contact: Question on a hotel',
    es: 'Contact: Question on a hotel',
    it: 'Contact: Question on a hotel'
};

CONTACT.relation.mailSubject ={
    de: 'Contact : Kommentare zu einem Aufenthalt in einem Hotel',
    fr: 'Contact : Question sur un sejour', //PROD: Contact : Commentaires sur un sejour dans un Suite Novotel
    en: 'Contact: Comments regarding your stay in one of our hotels',
    es: 'Contact: Comments regarding your stay in one of our hotels',
    it: 'Contact: Comments regarding your stay in one of our hotels'
};

CONTACT.suggestion.mailSubject ={
    de: 'Contact : Fragen, Anregungen oder Kommentare zu unserer Website',
    fr: 'Contact : Suggestions et commentaires sur le site Internet', //PROD : Contact : questions, suggestions et commentaires sur le site Suite Novotel
    en: 'Contact: Suggestions and questions on our web site',
    es: 'Contact: Suggestions and questions on our web site',
    it: 'Contact: Suggestions and questions on our web site'
};

CONTACT.fid.mailSubject ={
    de: 'Contact : Fragen zu den Clubkarten',
    fr: 'Contact : Question sur le programme de fidelisation', //PROD: Contact : Services cartes de fidélité sur le site Suite Novotel
    en: 'Contact: Questions concerning our loyalty card program',
    Es: 'Contact: Questions concerning our loyalty card program',
    it: 'Contact: Questions concerning our loyalty card program'
};

CONTACT.probleme.mailSubject ={
    de: 'Contact : Fragen zu einem technischen Problem',
    fr: 'Contact : Probleme technique sur le site', // PROD: Contact : Problemes techniques lors de l'utilisation du site Suite Novotel
    en: 'Contact: Questions in reference to a technical problem',
    Es: 'Contact: Questions in reference to a technical problem',
    it: 'Contact: Questions in reference to a technical problem'
};

