﻿//Initialize component
$().ready(function() {
    AutomaticRecall.init();
});

AutomaticRecall = {

    ControlModeType: { AutomaticRecall: 1, OnlineQuote: 2 },
    ControlMode: 1, /* 1 = AutomaticRecall mode, 2 = OnlineQuote mode */

    init: function() {
        if ($(".pnlValidationSummaryAR").html() != "")
            $(".pnlValidationSummaryAR").html("");

        ADom.ClientUtils.initPopup("#divContentAutomaticRecall");
        AutomaticRecall.setEventHandlers();
        AutomaticRecall.setEventHandlersPopUp();
        $(".txtTelephoneAR").watermark("Votre téléphone");
    },

    setEventHandlers: function() {
        $(".AutoRecallLauncherOk").unbind().click(AutomaticRecall.show);
    },

    setEventHandlersPopUp: function() {
        $(".btnOkAR").unbind().click(AutomaticRecall.onClickOk);
        $(".btnCancelAR").unbind().click(AutomaticRecall.onClickCancel);
        $(".btnCloseAR").unbind().click(AutomaticRecall.onClickCancel);
    },

    setEventHandlersResult: function() {
        $(".btnResultCloseARR").unbind().click(AutomaticRecall.onClickCancelResult);
        $(".btnResultPopupCloseARR").unbind().click(AutomaticRecall.onClickCancelResult);
    },

    show: function() {
        var res = WSK.ADom.ClientServices.Main.GetAutomaticRecallControl($(".popup_phone").val(),
                                                                        AutomaticRecall.ControlMode);
        if (res.error == null) {

            if ($("#divContentAutomaticRecall").length == 0) {
                $("body").append("<div style=\"display:none;\" id=\"divContentAutomaticRecall\"></div>");
            }
            $("#divContentAutomaticRecall").html(res.value);

            if (Validation.AreDigitsValidating($(".txtTelephoneAR"))) {
                $(".txtPhoneAR").val($(".txtTelephoneAR").val());
            } else {
                $(".txtPhoneAR").val("");
            }

            AutomaticRecall.setEventHandlersPopUp();

            $.blockUI({ message: $("#divContentAutomaticRecall") });
        }
        return false;
    },

    hide: function() {
        $.unblockUI({ message: $(".divContentAutomaticRecall") });
        return false;
    },

    onClickOk: function(e) {

        //XITI Tagging Measurements
        if (AutomaticRecall.ControlMode == 1)
            xt_med('F', xtn2, 'bouton_rappel_validation&x3=' + $(".ddlRequirementsAR").val() + '&x4=' + $(".txtZipCodeAR").val());
        else
            xt_med('F', xtn2, 'bouton_rappel_validation&x3=' + $(".ddlActivityOQ").val() + '&x4=' + $(".txtZipCodeAR").val());

        xt_med('F', xtn2, 'bouton_devis_validation');

        if (AutomaticRecall.validate()) {
            if (AutomaticRecall.ControlMode == 1) { //AutomaticRecall
                var now = new Date();

                var res = WSK.ADom.ClientServices.Main.DoRecallActions(
                AutomaticRecall.ControlMode,
                $(".txtNameAR").val(),
                $(".txtPhoneAR").val(),
                $(".txtZipCodeAR").val(),
                "",
                document.getElementById($(".ddlRequirementsAR")[0].id).options[document.getElementById($(".ddlRequirementsAR")[0].id).selectedIndex].text,
                now.toLocaleDateString(),
                now.toLocaleDateString(),
                "0",
                false,
                "");
            } else { //OnlineQuote

                var res = WSK.ADom.ClientServices.Main.DoRecallActions(
                AutomaticRecall.ControlMode,
                $(".txtNameAR").val(),
                $(".txtPhoneAR").val(),
                $(".txtZipCodeAR").val(),
                $(".txtEmailAR").val(),
                document.getElementById($(".ddlActivityOQ")[0].id).options[document.getElementById($(".ddlActivityOQ")[0].id).selectedIndex].text,
                $(".date_debut").val(),
                $(".date_fin").val(),
                $(".txtHoursNumberOQ").val(),
                $(".vehicule_oui :input").attr("checked"),
                $(".txtMessage").val());
            }


            if ($(".pnlResultsAR").length == 0) {
                res = WSK.ADom.ClientServices.Main.GetAutomaticRecallResultsControl();
                var htmlPanel = $("<div />").append(res.value).find(".pnlResultsAR");
                htmlPanel.css("display", "none");
                $("#divContentAutomaticRecall").after(htmlPanel); //Set panels html
                AutomaticRecall.setEventHandlersResult();
            }
            AutomaticRecall.hide();
            AutomaticRecall.clearControls();
            $.blockUI({ message: $(".pnlResultsAR") });

        }
        return false;
    },

    clearControls: function() {
        $(".txtNameAR").val("");
        $(".txtPhoneAR").val("");
        $(".txtZipCodeAR").val("");
        $(".txtEmailAR").val("");
        $(".txtMessage").val("");
        $(".ddlRequirementsAR").val("");
    },

    onClickCancel: function(e) {
        //XITI Tagging
        xt_med('F', xtn2, 'bouton_devis_annulation');
        AutomaticRecall.hide();
        return false;
    },

    onClickCancelResult: function(e) {
        //XITI Tagging
        xt_med('F', xtn2, 'bouton_devis_fermeture');
        $.unblockUI({ message: $(".pnlResultsAR") });
        return false;
    },

    validate: function() {
        var result = true;

        //Common validations
        result = Validation.isNotNullOrEmpty($(".txtPhoneAR"), ErrorMessages.PHONE_EMPTY, $(".pnlValidationSummaryAR"));
        if (result) result = Validation.isLengthEqualTo($(".txtPhoneAR"), 10, ErrorMessages.PHONE_LENGTH, $(".pnlValidationSummaryAR"));
        result = Validation.isNotNullOrEmpty($(".txtNameAR"), ErrorMessages.SURNAME_EMPTY, $(".pnlValidationSummaryAR"));
        result = Validation.isNotNullOrEmpty($(".txtZipCodeAR"), ErrorMessages.ZIPCODE_EMPTY, $(".pnlValidationSummaryAR"));
        if (result) result = Validation.isLengthEqualTo($(".txtZipCodeAR"), 5, ErrorMessages.ZIPCODE_LENGTH, $(".pnlValidationSummaryAR"));

        if (AutomaticRecall.ControlMode == AutomaticRecall.ControlModeType.AutomaticRecall) {
            result = Validation.EmptyListValidating($(".ddlRequirementsAR"), ErrorMessages.ACTIVITY_NOTSELECTED, $(".pnlValidationSummaryAR"));
        } else {
            result = Validation.isNotNullOrEmpty($(".txtEmailAR"), ErrorMessages.EMAIL_EMPTY, $(".pnlValidationSummaryAR"));
            if (result) result = Validation.IsValidEmailAddress($(".txtEmailAR"), ErrorMessages.EMAIL_NOTVALID, $(".pnlValidationSummaryAR"));
            result = Validation.isLengthLessThan($(".txtMessage"), 1000, ErrorMessages.NOTVALID_LENGTH, $(".pnlValidationSummaryAR")) & result;
        }

        if ($(".pnlValidationSummaryAR").val() != "") {
            var errorMessages = "";
            var htmlErrors;

            errorMessages = $(".pnlValidationSummaryAR").val();
            htmlErrors = "<div>" + errorMessages + "</div>";
            if ($(".pnlValidationSummaryAR").html() != "")
                $(".pnlValidationSummaryAR").html("");
            $(".pnlValidationSummaryAR").html(htmlErrors);
            $(".pnlValidationSummaryAR").val("");
            result = false;
        } else {
            result = true;
        }
        return result;
    }
}

if (!$.fn.watermark) { //If watermark is not defined
    $.fn.watermark = function(text) {
        return this.each(function() {
            //Add watermark
            $(this).addClass("defaultText").attr({ value: text }).focus(function() {
                //Onfocus: remove text if it's default text and remove defaultText class
            if ($(this).val() == text || (text.indexOf($(this).val()) != -1)) {
                    $(this).val("").removeClass("defaultText");
                }
            }).blur(function() {
                //Onblur: if text is empty, add default text and defaultText class
                if ($(this).val() == "" || (text.indexOf($(this).val()) != -1)) {
                    $(this).addClass("defaultText").val(text);
                }
            });
        });
    };
}

