function Devis() {

    this.isChangeValueByOptionsInternet = false;
    this.idCheckBoxOptionInternet = null;
    
    this.setOnClickToSelectGamme = function SetOnClickToSelectGamme() {
        $("#menuVehicules li a").click( function() {
            // 00, EB, 08/06/2010, Nothing to do here
        });
    };

    this.displaySelectedOptions = function DisplaySelectedOptions() {
        $("#tabs").tabs('select', 0);
    };

    this.addOptionSelectedComputeValues = function AddOptionSelectedComputeValues() {
        $(".inptCheck").click(function() {
            
            if($(this).attr("id") == "chkPromotion") 
                mazedia.ui.devis.uncheckAllPromotions($(this));
        
            var siblings = $(this).siblings(".quantite");
            if (siblings) {
                var inputText = siblings.children(".fieldNbre");
                if (inputText) {
                    if ($(this).attr("checked")) {
                        inputText.val("1");
                    } else
                        inputText.val("");
                    mazedia.ui.devis.isChangeValueByOptionsInternet = false;
                    mazedia.ui.devis.computeTotalPrice();
                }
            }
        });
    };

    this.addSpecificPromotionOnChange = function AddSpecificpromotionOnChange() {
        $(".specificpromotion").change(function() {
            var hasValue = $(this).val().length > 0;
            $(this).prev().prev().attr("checked", hasValue);
            mazedia.ui.devis.computeTotalPrice();
        });
    };

    this.addSelectTypePaiementChange = function AddSelectTypePaiementChange() {
        $(".inptRadio").click(function() {
            $("#internetPriceSpan").show();
            $("#agencyPriceSpan").show();
            mazedia.ui.devis.idCheckBoxOptionInternet = $(this).attr("id");
            mazedia.ui.devis.isChangeValueByOptionsInternet = true;
            mazedia.ui.devis.computeTotalPrice();
        });
    };

    this.addOnClickMoreLessButtons = function AddOnClickMoreLessButtons() {
        $("a.plus").click(function() {
            var value = $(this).prev().val();
            if (value == null || value == "") {
                value = 1;
                $(this).parent().siblings(".inptCheck").attr('checked', true);
            } else {
                value++;
            }
            $(this).prev().val(value);
            if (value != "") {
                mazedia.ui.devis.isChangeValueByOptionsInternet = false;
                mazedia.ui.devis.computeTotalPrice();
            }
            return false;
        });

        $("a.moins").click(function() {
            var value = $(this).next().val();
            var valueMin = -1;
            if (value == null || value == "") {
                value = "";
            } else {
                value--;
                valueMin = value;
                if (value < 1) {
                    value = "";
                    $(this).parent().siblings(".inptCheck").attr('checked', false);
                }
            }
            $(this).next().val(value);
            if (value != "" || valueMin == 0) {
                value = value == "" ? valueMin : value;
                mazedia.ui.devis.isChangeValueByOptionsInternet = false;
                mazedia.ui.devis.computeTotalPrice();
            }
            return false;
        });
    };

    this.getSelectedOptionsFromOneDevis = function GetSelectedOptionsFromOneDevis(options, devis) {
        devis.find(".field_line").each(function() {
            var hiddenIdPrestation = $(this).find("input:hidden[name=id]").val();
            var fieldNbre = $(this).find(".fieldNbre").val();

            if (!hiddenIdPrestation)
                hiddenIdPrestation = $(this).find("input:hidden[name=id_promotion]").val();
            if (!fieldNbre)
                fieldNbre = $(this).find(".specificpromotion").val();

            var optSelected = $(this).find("input[type=checkbox]");
            var isCanAddOption = false;
            if (optSelected.length == 0) // Default option => doesn't need to check it
            {
                var defaultOption = $(this).find("input[id='IsDefault']");
                isCanAddOption = defaultOption.length == 1;
            }
            else
                isCanAddOption = optSelected.attr("checked");

            if (isCanAddOption) {
                var option = new Array();
                option.push(hiddenIdPrestation);
                option.push(fieldNbre);
                options.push(option);
            }
        });
        return options;
    };
    
    this.uncheckAllPromotions = function UncheckAllPromotions(current) {
        var oldCheckState = current.attr('checked');
        
        $(".promotions .inptCheck").each(function() {
                $(this).attr('checked', false);
        });   
        current.attr('checked', oldCheckState);
    };

    this.getSelectedOptions = function GetSelectedOptions() {
        var options = new Array();

        $(".single").each(function() {
            mazedia.ui.devis.getSelectedOptionsFromOneDevis(options, $(this));
        });

        $(".options").each(function() {
            mazedia.ui.devis.getSelectedOptionsFromOneDevis(options, $(this));
        });

        return options;
    };

    this.getSelectedPromotions = function GetSelectedPromotions() {
        var promotions = new Array();

        $(".promotions").each(function() {
            mazedia.ui.devis.getSelectedOptionsFromOneDevis(promotions, $(this));
        });

        return promotions;
    };
    
    this.putClassPriceLoading = function PutClassLoading(option) {
        option.html("");
        option.attr("class", "prixLoading");
    };

    this.putClassNormalPrice = function PutClassNormalPrice(option, price) {
        var priceToString = price.toString();
        var strPriceEuros = (priceToString != "" ? " €" : "");
        var value = priceToString + strPriceEuros;
        option.html(value);
        option.attr("class", "prix");
    };
    
    this.blockInputFields = function blockInputFields(isBlock) {
        if(isBlock)  {
            $(".inptCheck").attr('disabled', true);
            $(".inptRadio").attr('disabled', true);
            $(".valide_resa").attr('disabled', true);
            $(".fieldNbre").attr('disabled', true);
        } else {
           $(".inptCheck").removeAttr('disabled'); 
           $(".inptRadio").removeAttr('disabled'); 
           $(".valide_resa").removeAttr('disabled'); 
           $(".fieldNbre").removeAttr('disabled'); 
        }        
    };

    this.computeTotalPrice = function computeTotalPrice() {
        mazedia.ui.devis.putClassPriceLoading($(".devis_total .prix"));

        var internetPriceOption = $("#InternetPaiement").parent().next().find(".prix");
        var defaultPriceOption = $("#AgencyPaiement").parent().next().find(".prix");
        var upSellingPriceOption = $("#prixUpSelling");

        mazedia.ui.devis.blockInputFields(true);

        mazedia.ui.devis.putClassPriceLoading(internetPriceOption);
        mazedia.ui.devis.putClassPriceLoading(defaultPriceOption);
        mazedia.ui.devis.putClassPriceLoading(upSellingPriceOption);

        $.post(mazedia.url.rRequestComputeDevis,
                {
                    isInternetPrice: $("#InternetPaiement").attr("checked"),
                    gammeID: $("#gammeID").val(),
                    agenceIDDepart: $("#agenceIDDepart").val(),
                    dateDepart: $("#dateDepart").val(),
                    agenceIDArrivee: $("#agenceIDArrivee").val(),
                    dateArrivee: $("#dateArrivee").val(),
                    nbKms: $("#nbKms").val(),
                    options: mazedia.ui.devis.getSelectedOptions(),
                    promotions: mazedia.ui.devis.getSelectedPromotions(),
                    promoCode: $("#promoCodeInput").val(),
                    jeuneConducteur: $("#jeuneConducteur:checked").val()
                },
                function(results) {
                    mazedia.ui.devis.displayNewTarif(results);
                },
                "json"
        );
    };

    this.putClassNormalUpSellingPrice = function PutClassNormalUpSellingPrice(option, price, libelle) {
        if (price != 0) {
            var priceToString = price.toString();
            var strPriceEuros = (priceToString != "" ? " €" : "");
            strPriceEuros = strPriceEuros + " " + libelle;
            var value = priceToString + strPriceEuros;
            option.html(value);
            option.attr("class", "prix");
        } else {
            option.html(libelle);
            option.attr("class", "prix");
        }
    };

    this.displayNewTarif = function DisplayNewTarif(results) {
        if (results.RETOUR == "OK") {
            mazedia.ui.devis.putClassNormalPrice($(".devis_total .prixLoading"), results.NewPrice);

            var internetPriceOption = $("#InternetPaiement").parent().next().find(".prixLoading");
            var defaultPriceOption = $("#AgencyPaiement").parent().next().find(".prixLoading");
            var upSellingPriceOption = $("#prixUpSelling");

            var newPrice = 0;

            mazedia.ui.devis.putClassNormalPrice(internetPriceOption, results.NewInternetPrice);
            mazedia.ui.devis.putClassNormalPrice(defaultPriceOption, results.NewAgencyPrice);

            var oldUpSellingPrice = $("#upSellingPrice").val();
            var upSellingText = $("#upSellingTextMore").val();
            var prixAffiche = results.NewUpSellingPrice;

            if (results.IsUpSellingBetter == 0) {
                upSellingText = $("#upSellingTextSame").val();
            }
            else if (results.IsUpSellingBetter < 0) {
                upSellingText = $("#upSellingTextLess").val();
                prixAffiche = results.NewUpSellingPrice.replace("-", " ");
            }

            mazedia.ui.devis.putClassNormalUpSellingPrice(upSellingPriceOption, prixAffiche, upSellingText);

            var showPromoVacancesInfos = false;
            if (results.ShowPromoHoliday)
                showPromoVacancesInfos = true;

            if (showPromoVacancesInfos)
                $("#promo_vacances").show();
            else
                $("#promo_vacances").hide();

            $("#promoCodeInput").val("");

            if (results.BestPromo != "") {
                $("#promotionText").show();
                $("#promotionName").html(results.BestPromo);
            } else {
                $("#promotionText").hide();
            }

            if (results.NbPromotionsSelected > 1) {
                $("#promotionCumul").show();
            } else {
                $("#promotionCumul").hide();
            }

            if (results.CodePromoNotExists) {
                $("#promoCodeInvalide").show();
            } else {
                $("#promoCodeInvalide").hide();
            }

            if (results.InvalidPromoName != "") {
                $("#promoCodeConditions_text").html(results.InvalidPromoName + ": " + results.InvalidPromoConditions);
                $("#promoCodeConditions").show();
            } else {
                $("#promoCodeConditions").hide();
            }

            $("#upSellingPrice").val(results.InternetUpSellingPrice);
            $("#upSellingPriceAgence").val(results.AgencyUpSellingPrice);

            if (results.NameCodePromoAdded != "") {
                divStr1 = '<div class="field_line_sel"><div class="libele_field_line">' +
                          '<input type="checkbox" class="promoListSelectedCheckBox" checked="checked" disabled="disabled" /><label>';
                divStr2 = '<a title="' + results.ConditionsPromoCodeAdded + '" href="#" class="devis_info vtip">' +
                          '<img src="/Content/medias/info_bt.png" alt="Plus d’infos"></a></label></div></div>';
                divHtml = $("#promoListSelectedIn").html();
                $("#promoListSelectedIn").html(divHtml + divStr1 + results.NameCodePromoAdded + divStr2);
                mazedia.ui.global.addToolTips();
            }

            $("#priceInternetCurrent").val(results.NewInternetPrice);
            $("#priceAgencyCurrent").val(results.NewAgencyPrice);

            if (results.ErrorWS == "true" || results.ErrorWS == "True") {
                //error WS
            }

            mazedia.ui.devis.blockInputFields(false);
        }
    };

    this.goToSuperieure = function GoToSuperieure() {
        $("#GoToSuperieure").click(function() {
            linkCateg = $("#LinkCateg").val();
            if (linkCateg != "") {
                window.location = linkCateg;
            }
        });
    }
}
