﻿
        function harmonise_prix_avec_langue(Hmonnaie,Hprix,Hlg) 
		{
			Hlg = Hlg.toLowerCase();
			var result_harmonise="";
			if(Hlg=="nl")
			{
				result_harmonise = Hmonnaie + "&#160;" + formatNumber(Hprix,2,false);
				result_harmonise = replace(result_harmonise,"." ,"," );
			}
			else
			{
				if ( (Hlg=="en") || (Hlg=="de") )
				{
				result_harmonise = Hmonnaie + "&#160;" + formatNumber(Hprix,2,false);
				}
				else
				{
				result_harmonise = replace( formatNumber(Hprix,2,false) + "&#160;" + Hmonnaie ,"." ,"," );
				}
			}
			return(result_harmonise)
		}
		
		function replace(string,text,by) 
		{
			var strLength = string.length, txtLength = text.length;
			if ((strLength == 0) || (txtLength == 0)) return string;
			var i = string.indexOf(text);
			if ((!i) && (text != string.substring(0,txtLength))) return string;
			if (i == -1) return string;
			var newstr = string.substring(0,i) + by;
			if (i+txtLength < strLength)
				newstr += replace(string.substring(i+txtLength,strLength),text,by);
			return newstr
		}
		
		function formatNumber(nombre,apresVirgule,GarderEntier)
		{
			var NB = nombre + '';
			var positionVirgule = NB.indexOf(".");
			if (positionVirgule>=0)
			{
				TAB_NB = NB.split('.');
				var partie_decimale = TAB_NB[1] + '';
				if (partie_decimale.length >= apresVirgule)
                {
					partie_decimale = partie_decimale.substring(0, 2);
                }
				else
                {
					for (var z=0; z< apresVirgule - partie_decimale.length; z++) {partie_decimale = partie_decimale + '0';
				}
            }
			NB = TAB_NB[0] + '.' + partie_decimale;
			}
			else
			{ if (!GarderEntier) {NB = NB +'.00';} }
			return(NB);
		}
	var meShOption ;
	ShOptions = function() {
	    this.IdError = -1;
	    this.MsgError = "";
	    this.RegularShare = -1; //Acompte variable
	    this.VariableShare = -1; //acompte fixe
	    this.TotalPrice = -1; // Prix total
	    this.DepositPrice = -1; //Prix de l'acompte dans frais de dossier
	    this.BookingFees = -1; // Frais de dossier
	    this.TotalDeposit = -1; // Prix de l'acompte à payer (avec frais de dossier)
	    this.BalanceDue = -1; // Restant du
	    this.BasePrice = -1; // Prix de base (sans option)
	    this.OptionPrice = -1; // Prix des options sélectionnées
	    this.Currency = ""; // Monnaie
	    this.StringChoosenOptions = ""; // Chaine splittee des options choisies
	    this.TotalPers = -1; // Nombre de personnes
	    this.IsCancellationInsurance = ""; //Est-ce qu'il y a une assurance pour cet achat
        
	    var callBackFunction;

	    this.Calculate = function(idMoteur, isoLangue, tac, spec, specOption, moos, chaineOption, valide,codePromo, callback) {
	        callBackFunction = callback;
	        meShOption = this;
	        PageMethods.CheckOptions(idMoteur, isoLangue, tac, spec, specOption, moos, chaineOption, valide,codePromo, this.MyMethod_Result_CheckOptions);
	    },

	this.MyMethod_Result_CheckOptions = function(ResultString) {
	    meShOption.IdError = Number(ResultString["errorId"]);
	    meShOption.MsgError = ResultString["error"];

	    if (meShOption.IdError == 0) {
	        var bookingPriceInformations = ResultString["bookingPriceInformations"];
	        meShOption.RegularShare = MyParseFloat(bookingPriceInformations["RegularShare"]);
	        meShOption.VariableShare = MyParseFloat(bookingPriceInformations["VariableShare"]);
	        meShOption.TotalPrice = MyParseFloat(bookingPriceInformations["TotalPrice"]);
	        meShOption.DepositPrice = MyParseFloat(bookingPriceInformations["DepositPrice"]);
	        meShOption.BookingFees = MyParseFloat(bookingPriceInformations["BookingFees"]);
	        meShOption.TotalDeposit = MyParseFloat(bookingPriceInformations["TotalDeposit"]);
	        meShOption.BalanceDue = MyParseFloat(bookingPriceInformations["BalanceDue"]);
	        meShOption.BasePrice = MyParseFloat(bookingPriceInformations["BasePrice"]);
	        meShOption.OptionPrice = MyParseFloat(bookingPriceInformations["OptionPrice"]);
	        meShOption.Currency = bookingPriceInformations["Currency"];
	        meShOption.StringChoosenOptions = bookingPriceInformations["StringChoosenOptions"];
	        meShOption.TotalPers = bookingPriceInformations["TotalPers"];
	        meShOption.IsCancellationInsurance = bookingPriceInformations["IsCancellationInsurance"];
	        meShOption.crcChoosenOption = bookingPriceInformations.crcChoosenOptions;
	        meShOption.CodePromo = bookingPriceInformations["DiscountCodeDetails"];
	    }

	    callBackFunction(meShOption);
	}

	    function MyParseFloat(s) {
	        // var s = s.replace(",",".");
	        return parseFloat(s, 10);
	    }

	}
InfosResa = function(idMoteur,idEtablissement,isoLangue,tac,spec,specOption,moos,typeProduit,idProduit,monnaie){
    this.idMoteur = idMoteur;
    this.isoLangue = isoLangue;
    this.tac = tac ; 
    this.spec = spec;
    this.specOption = specOption;
    this.moos  = moos;
    this.monnaie = monnaie;
    this.idProduit = idProduit;
    this.typeProduit = typeProduit;
    this.idEtablissement = idEtablissement;
}
OptionForm = function(infosResa, ClassSelecteurOption, IdDivPrixTotal, IdDivDeposit, IdDescShareVariable, IdDescShareFixe, IdDivBalance, IdDivFD, IdDivNbPers, IDDivMontant, IDCBAccepte, IDDivAssurance, IDHiddenCrc) {
    this.tab_input_option = getElementsByClassName(ClassSelecteurOption);
    if (this.tab_input_option) {
        this.inputs_options = {};
        for (var i = 0; i < this.tab_input_option.length; i++) {
            var un_input = this.tab_input_option[i];
            this.inputs_options[un_input.attributes.id_option.nodeValue] = un_input;
        }
    }

    this.div_prix_total = document.getElementById(IdDivPrixTotal);
    this.div_deposit = document.getElementById(IdDivDeposit);
    this.div_desc_share_variable = document.getElementById(IdDescShareVariable);
    this.div_desc_share_regular = document.getElementById(IdDescShareFixe);
    this.div_balance = document.getElementById(IdDivBalance);
    this.div_fd = document.getElementById(IdDivFD);
    this.div_nb_pers = document.getElementById(IdDivNbPers);
    this.div_montant = document.getElementById(IDDivMontant);
    this.infoResa = infosResa;
    this.shOption = new ShOptions();
    this.tb_crc_choosen_option = document.getElementById(IDHiddenCrc);
    this.id_div_assurance = IDDivAssurance;
    this.cb_accepte = document.getElementById(IDCBAccepte);
}
OptionForm.prototype.ActiverCodePromo = function(IDTBCodePromo, IDDivTextePromo, IDDivPrixPromo,IDDivErrorCodePromo) {
    this.tb_code_promo = document.getElementById(IDTBCodePromo);
    this.div_texte_promo = document.getElementById(IDDivTextePromo);
    this.div_montant_promo = document.getElementById(IDDivPrixPromo);
    this.div_error_promo = document.getElementById(IDDivErrorCodePromo);
}
OptionForm.prototype.MajResultatCodePromo = function(resultatCodePromo) {
    if (typeof (resultatCodePromo) == "undefined")
        return;
    if (resultatCodePromo.isOK) {
        this.div_texte_promo.innerHTML = resultatCodePromo.message;
        this.div_montant_promo.innerHTML = resultatCodePromo.codePromo.discountAmount;
        this.div_error_promo.innerHTML = "";
    }
    else {
        this.div_texte_promo.innerHTML = "";
        this.div_montant_promo.innerHTML = "";
        this.div_error_promo.innerHTML = resultatCodePromo.message;
    }
}
OptionForm.prototype.SetPrixDiv = function(div,prix){
    if (div) div.innerHTML = harmonise_prix_avec_langue(this.infoResa.monnaie,prix,this.infoResa.isoLangue);
}
OptionForm.prototype.SetOptionSelected = function(options_choisies){
    if(options_choisies == '')
    return;
    tab_options_choisies=options_choisies.split(";");
    for (var p=0;p < tab_options_choisies.length ;p++) 
    {
	    tab_p=tab_options_choisies[p].split("@");
	    var un_input = this.inputs_options[tab_p[0]];
	    un_input.value = tab_p[1];
    }
}
OptionForm.prototype.MajOutput = function(valide,callbackFunction){
this.callbackFunctionMaj = callbackFunction;
this.shOption.Calculate(this.infoResa.idMoteur, this.infoResa.isoLangue, this.infoResa.tac, this.infoResa.spec, this.infoResa.specOption, this.infoResa.moos, this.GetChaineOption(), valide, this.GetCodePromo(), RetourMajOutput);
}
function RetourMajOutput(result){
    optionForm.RetourMajOutput(result);
}
OptionForm.prototype.GetCodePromo = function() {
    if (!this.tb_code_promo)
        return null;
    return this.tb_code_promo.value;
}
OptionForm.prototype.RetourMajOutput = function(result){
    if(this.shOption.IdError != 0){
        alert(this.shOption.MsgError);
        return ;
    }
    this.SetPrixDiv(this.div_prix_total,this.shOption.TotalPrice);
    this.SetPrixDiv(this.div_deposit,this.shOption.TotalDeposit);
    this.SetPrixDiv(this.div_balance,this.shOption.BalanceDue);
    this.SetPrixDiv(this.div_fd,this.shOption.BookingFees);
    this.div_nb_pers.innerHTML = this.shOption.TotalPers;
    this.SetPrixDiv(this.div_montant,this.shOption.TotalPrice - this.shOption.BookingFees);
    if(this.shOption.VariableShare > 100){
        if (this.div_desc_share_variable) this.div_desc_share_variable.innerHTML = tradPage.GetTrad('variable_share')+ '100%';
    }
    else{
        if(this.shOption.VariableShare > 0){
            if (this.div_desc_share_variable) this.div_desc_share_variable.innerHTML = tradPage.GetTrad('variable_share')+ this.shOption.VariableShare +'%';
        }
        if(this.shOption.RegularShare > 0){
            if (this.div_desc_share_regular) this.div_desc_share_regular.innerHTML = tradPage.GetTrad('regular_share')+this.shOption.RegularShare+this.infoResa.monnaie;
        }
        
    }
    this.DoCallBackMaj(result);
}
OptionForm.prototype.DoCallBackMaj = function(result){
    if(this.callbackFunctionMaj)
        this.callbackFunctionMaj(result);
    this.callbackFunctionMaj = null;
}
OptionForm.prototype.GetChaineOption = function(){
    var res = '';
    for(var i = 0;i<this.tab_input_option.length ; i++){
        var un_input = this.tab_input_option[i];
        if(un_input.value != 0){
            if(res != '') 
                res += ';'
            res += un_input.attributes.id_option.nodeValue+'@'+un_input.value+'@'+un_input.attributes.prix.nodeValue;        
        }
    }
    return res;
}
OptionForm.prototype.NextStep = function(){
    if(!this.cb_accepte.checked){
        alert(tradPage.GetTrad('doit_cocher'));
        return;
    }
    this.MajOutput('YES',this.GoNextStep);
}
OptionForm.prototype.NextStepPanier = function() {
    if (!this.cb_accepte.checked) {
        alert(tradPage.GetTrad('doit_cocher'));
        return;
    }
    var actionForm = '';

    actionForm = url_reservation_assurances + '?idM=' + this.infoResa.idMoteur + '&tac=' + this.infoResa.tac + '&spec=' + this.infoResa.spec + '&m=' + this.infoResa.moos + '&o=' + this.GetChaineOption() + '&t=' + this.infoResa.typeProduit + '&idP=' + this.infoResa.idProduit + '&idE=' + this.infoResa.idEtablissement;

    var form = document.forms[0];
    form.action = actionForm;
    form.submit();
}
OptionForm.prototype.AddPanier = function() {
    this.MajOutput('NO', this.GoPanier);
}
OptionForm.prototype.GoPanier = function() {
    if (this.shOption.IdError != 0) {
        alert(this.shOption.MsgError);
        return;
    }
    var actionForm = '';
    this.tb_crc_choosen_option.value = this.shOption.crcChoosenOption;
    actionForm = 'panier.aspx?actionP=AH&idM=' + this.infoResa.idMoteur + '&tac=' + this.infoResa.tac + '&spec=' + this.infoResa.spec + '&m=' + this.infoResa.moos + '&o=' + this.GetChaineOption() + '&t=' + this.infoResa.typeProduit + '&idP=' + this.infoResa.idProduit + '&idE=' + this.infoResa.idEtablissement;
    var form = document.forms[0];
    form.action = actionForm;
    form.submit();
}
OptionForm.prototype.GoNextStep = function() {
    if (this.shOption.IdError != 0) {
        alert(this.shOption.MsgError);
        return;
    }
    var actionForm = '';
    this.tb_crc_choosen_option.value = this.shOption.crcChoosenOption;

    if (this.shOption.IsCancellationInsurance) {
        actionForm = url_reservation_assurances+'?idM=' + this.infoResa.idMoteur + '&tac=' + this.infoResa.tac + '&spec=' + this.infoResa.spec + '&m=' + this.infoResa.moos + '&o=' + this.GetChaineOption() + '&t=' + this.infoResa.typeProduit + '&idP=' + this.infoResa.idProduit + '&idE=' + this.infoResa.idEtablissement;
    }
    else {
        actionForm = domaineSSL + url_reservation_formulaire +'?idM=' + this.infoResa.idMoteur + '&tac=' + this.infoResa.tac + '&spec=' + this.infoResa.spec + '&idE=' + this.infoResa.idEtablissement + '&t=' + this.infoResa.typeProduit + '&idP=' + this.infoResa.idProduit;
    }

    var form = document.forms[0];
    form.action = actionForm;
    form.submit();
}
OptionForm.prototype.afficheCGV = function(idAssurance)
	  	{
	  	
			 document.getElementById(this.id_div_assurance+idAssurance).style.display='block';
			 for(var i = 0;i<this.tab_input_option.length ; i++){
			
				this.tab_input_option[i].style.display='none';
			}

		}
		
OptionForm.prototype.closeCGV = function(idAssurance)
		{
			document.getElementById(this.id_div_assurance+idAssurance).style.display='none';
			for(var i = 0;i<this.tab_input_option.length ; i++){
			
				this.tab_input_option[i].style.display='inline';
			}

		}