﻿/* Namespace for all AssurlandPro js script */
if (!window.AssurlandPro) { window.AssurlandPro = {}; }



    /* Hp Carrier animation */
AssurlandPro.HpCarrierAnimation = function(selector, speed_ms) {
    // Element size in pixel
    //var size = 377;    
    // Animation speed AND waiting time between animation
    var speed = speed_ms;
    // Animation container element
    var $elt = $(selector);
    // Update div content style
    $elt.find('.alp_item').addClass("alp_content");
    // Go next
    var next = function() {
        var size = $elt.find('.alp_content:first').width() + 1 * $elt.find('.alp_content:first').css('padding-left').replace("px", "") + 1 * $elt.find('.alp_content:first').css('padding-right').replace("px", "");
        $elt.find('.alp_items').animate({ "left": "-=" + size }, speed, afterNext);
    };
    // Do after next
    var afterNext = function() {
        // Clone previous element to end of line
        $elt.find('.alp_content:first').clone().appendTo($elt.find('.alp_items'));
        $elt.find('.alp_content:first').remove();
        $elt.find('.alp_items').css("left", "0");
        next();
        // Loop
        //  setTimeout(next, speed);
    };
    // Start animation
    this.start = function() {
        setTimeout(next, speed);
    };
};

   
    /* Hp Profession animation */
     AssurlandPro.HpProfessionAnimation = function() {
        this.eltLeftId = null;
        this.eltRightId = null;
        this.enable = true;
        this.$elt = null;       
    }
    AssurlandPro.HpProfessionAnimation.prototype.init = function(elt, eltLeftId, eltRightId) {
        var that = this;
        this.eltLeftId = eltLeftId;
        this.eltRightId = eltRightId;
        this.$elt = $("#" + elt);
        //alp_entreprise
        $("#" + this.eltRightId + " .alp_entreprise .alp_off").click(function() { that.start("alp_entreprise"); });
        $("#" + this.eltRightId + " .alp_association .alp_off").click(function() { that.start("alp_association"); });
        $("#" + this.eltRightId + " .alp_artisan .alp_off").click(function() { that.start("alp_artisan"); });
        $("#" + this.eltRightId + " .alp_autoEntrepreneur .alp_off").click(function() { that.start("alp_autoEntrepreneur"); });
        $("#" + this.eltRightId + " .alp_professionLiberale .alp_off").click(function() { that.start("alp_professionLiberale"); });
    };

    AssurlandPro.HpProfessionAnimation.prototype.start = function(className) {
        var that = this;
        if (this.enable) {
            this.enable = false;
            var leftPartOld = $("#" + this.eltLeftId + " .alp_focus");
            var leftPartNew = $("#" + this.eltLeftId + " ." + className);
            var rightPartOld = $("#" + this.eltRightId + " .alp_focus");
            var rightPartNew = $("#" + this.eltRightId + " ." + className);
            //step 1
            //LeftPart move next div in fourth position
            leftPartNew.insertBefore(leftPartOld);
            //rightPart
            rightPartOld.find(".alp_off").insertBefore(rightPartOld.find(".alp_on"));
            rightPartOld.find(".alp_off").css("display", "block");

            rightPartNew.find(".alp_on").insertAfter(rightPartNew.find(".alp_off"));
            rightPartNew.find(".alp_on").css("display", "none");

            //step 2 move the class focus on first position
            leftPartNew.insertAfter(leftPartOld);
            //step 3 animation
            leftPartNew.fadeIn(1500, function() {
                //step 4 change class alp_focus
                leftPartOld.removeClass(' alp_focus');
                leftPartNew.addClass(' alp_focus');
                //Step 5 empty style tag
                leftPartOld.fadeOut();
            });
            //rightPart
            rightPartOld.find(".alp_on").fadeOut(1500, function() { rightPartOld.removeClass('alp_focus'); rightPartOld.find(".alp_off").fadeIn(); });
            rightPartNew.find(".alp_on").fadeIn(1500, function() { rightPartNew.addClass('alp_focus'); rightPartNew.find(".alp_on").fadeIn(); ; that.enable = true; });

        }
    };
    AssurlandPro.HpProfessionAnimation.prototype.load = function(imgUrl) {
        var that = this;
        var img = new Image();
        $(img).load(function() {
            // Hide image
            $(this).hide();
            // Update slide background
            that.$elt.find("div.alp_leftPart .alp_metier").css("background-image", "url(" + imgUrl + ")");
            // Remove loading
            that.$elt.find(".alp_notLoaded").removeClass("alp_notLoaded");
            // Display slides
            that.$elt.find(".alp_loading").fadeOut(1000);

        }).attr("src", imgUrl);
    };

   
//  data: "typeList=" + type + "&param1=" + param1,   
//data: {'typeList':  type ,'param1':param1 },
//data: "{'typeList':'" + type + "','param1':'" + param1 + "' }",
AssurlandPro.fill = function(type, param1, ddlDestID) {
    var ddlDest = document.getElementById(ddlDestID);
    if (ddlDest) {
        ddlDest.options.length = 0;
        if (param1 && param1 != "") {
            $.ajax({ type: "POST",
            url: "/WebService/Form.asmx/InitDataList",
            data: "{typeList:'" + type + "',param1:'" + param1 + "' }",                       
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                async: false,
                success: function(result) {
                     for (var i = 0; i < result.d.length; i++) {
                        ddlDest.options[i] = new Option(result.d[i].Text, result.d[i].Value);
                    }              
                }
            });
        }
    }
}

function activeValidator(validatorId) {
    var validator = document.getElementById(validatorId);
    validator.enabled = true;
    validator.isvalid = true;
    //ValidatorEnable(validator, true);
}
function disactiveValidator(validatorId) {
    var validator = document.getElementById(validatorId);   
    ValidatorEnable(validator, false);
    ALF_ValidatorPictoExtenderEvaluationFunctionValid(validator);
}


/*Form Page 1 */
AssurlandPro.professionChange = function(value) {
    var ddlActivityDomains = document.getElementById(ddlActivityDomainsId);
    if (value == "") {
        disactiveValidator(valProductId);
        ALF.hide(ProductRowId);
        disactiveValidator(valActivityDomainsId);
        ALF.hide(ActivityDomainsRowId);
        ddlActivityDomains.options.length = 0;
    }
    else {
        AssurlandPro.fill('Product', value, ddlProductId);
        ALF.show(ProductRowId);
        activeValidator(valProductId);

        AssurlandPro.fill('Domain', value, ddlActivityDomainsId);
        ALF.show(ActivityDomainsRowId);
        activeValidator(valActivityDomainsId);
    }
    AssurlandPro.domainsChange(ddlActivityDomains);
}
AssurlandPro.professionChange2 = function(value) {
    var ddlActivityDomains = document.getElementById(ddlActivityDomainsId);
    if (value == "") {       
        disactiveValidator(valActivityDomainsId);
        ALF.hide(ActivityDomainsRowId);
        ddlActivityDomains.options.length = 0;
    }
    else {
         AssurlandPro.fill('Domain', value, ddlActivityDomainsId);
         ALF.show(ActivityDomainsRowId);
        activeValidator(valActivityDomainsId);
    }
    AssurlandPro.domainsChange(ddlActivityDomains);
}
AssurlandPro.domainsChange = function(ddl) {
    var ddlActivitySubDomains = document.getElementById(ddlActivitySubDomainsId);
    if (ddlProfessionId) {
        var ddlProfession = document.getElementById(ddlProfessionId);

        if (ddlProfession.value == 3) {//association
            if (ddl.value == 43) {
                ALF.show(ActivityDescriptionRowId);
                activeValidator(valActivityDescriptionId);
            }
            else {
                disactiveValidator(valActivityDescriptionId);
                ALF.hide(ActivityDescriptionRowId);
            }
        }
        else {
            if (ddl.value == "69" || ddl.value == "70") {
                disactiveValidator(valActivitySubDomainsId);
                ALF.hide(ActivitySubDomainsRowId);
                ddlActivitySubDomains.options.length = 0;
                ALF.show(ActivityDescriptionRowId);
                activeValidator(valActivityDescriptionId);
            }
            else {

                if (ddl.value == "") {
                    disactiveValidator(valActivitySubDomainsId);
                    ALF.hide(ActivitySubDomainsRowId);
                    ddlActivitySubDomains.options.length = 0;
                }
                else {
                    AssurlandPro.fill('SubDomain', ddl.value, ddlActivitySubDomainsId);
                    ALF.show(ActivitySubDomainsRowId);
                    activeValidator(valActivitySubDomainsId);
                }
                AssurlandPro.subDomainChange(ddlActivitySubDomains);
            }
        }
    }
    else {
        if (ddl.value == "") {
            disactiveValidator(valActivitySubDomainsId);
            ALF.hide(ActivitySubDomainsRowId);
            ddlActivitySubDomains.options.length = 0;
        }
        else {
            AssurlandPro.fill('SubDomain', ddl.value, ddlActivitySubDomainsId);
            ALF.show(ActivitySubDomainsRowId);
            activeValidator(valActivitySubDomainsId);
        }
        AssurlandPro.subDomainChange(ddlActivitySubDomains);
    }
}

AssurlandPro.subDomainChange = function(ddl) {
    if (ddl.value == "") {
        disactiveValidator(valActivityDescriptionId);
        ALF.hide(ActivityDescriptionRowId);       
    }
    else {
        if (ddl.options[ddl.selectedIndex].text.indexOf('Autre', 0) >= 0) {
            ALF.show(ActivityDescriptionRowId);
            activeValidator(valActivityDescriptionId);
        }
        else {
            disactiveValidator(valActivityDescriptionId);
            ALF.hide(ActivityDescriptionRowId);
        }

    }

}
AssurlandPro.ClientValidateCorporateDate = function(src, args) {
    if (args.Value == "") {
        args.IsValid = false;
        src.errormessage = "Veuillez saisir l'année de création de votre société";
        $(src).text(src.errormessage);
    }
    else {
        var myDate = new Date();
        myDate = myDate.getFullYear();
        if (args.Value < 1800 || args.Value > myDate) {
            args.IsValid = false;
            src.errormessage = "L'année de création de votre société doit être comprise entre 1800 et " + myDate;
            $(src).text(src.errormessage);

        }
        else
            args.IsValid = true;
    }
}
/* need queries */
AssurlandPro.activeNeedQueries = function() {
    disactiveValidator(valProductId);
    ALF.hide(ProductRowId);
    ALF.hide(byNeedRowId);
    ALF.show(ProductSearchRowId);
    activeValidator(valProductSearchId);
    document.getElementById(hfNeedID).value = "1";
}

AssurlandPro.needChange = function(value) {
    switch (value)
    {
    case "-1":
        ALF.show(disasterTypeRowId);
        activeValidator(valDisasterTypeId);
        disactiveValidator(valFlotteId);
        ALF.hide(flotteRowId);
        disactiveValidator(valCollectiveId);
        ALF.hide(collectiveRowId);
        break;
    case "-6":
        ALF.show(flotteRowId);
        activeValidator(valFlotteId);
        disactiveValidator(valDisasterTypeId);
        ALF.hide(disasterTypeRowId);
        disactiveValidator(valCollectiveId);
        ALF.hide(collectiveRowId);
        break;
    case "-7":
        ALF.show(collectiveRowId);
        activeValidator(valCollectiveId);
        disactiveValidator(valFlotteId);
        ALF.hide(flotteRowId);
        disactiveValidator(valDisasterTypeId);
        ALF.hide(disasterTypeRowId);
        break; 
    default: 
        disactiveValidator(valCollectiveId);
        ALF.hide(collectiveRowId);
        disactiveValidator(valFlotteId);
        ALF.hide(flotteRowId);
        disactiveValidator(valDisasterTypeId);
        ALF.hide(disasterTypeRowId);
        break; 
    }

}
/*Form Page 3 */
AssurlandPro.validateZipCode = function(src, args) {
    if (args.Value == "") {
        args.IsValid = false;
    } else if (args.Value.length < 5) {
        args.IsValid = false;
    } else {
    args.IsValid = true;
//        
//        var ddlInseeCode = document.getElementById(ddlInseeCodeId);
//        if (ddlInseeCode && ddlInseeCode.options.length > 0)
//            args.IsValid = true;
//        else {
//            args.IsValid = false;
//        }
    }
}

AssurlandPro.validateInseeCode = function(src, args) {
    var ddlInseeCode = document.getElementById(ddlInseeCodeId);
    if (ddlInseeCode && ddlInseeCode.options.length > 0) {

        if (args.Value == "") {
            args.IsValid = false;
            $(src).text("Veuillez choisir votre commune");
        }
        else
            args.IsValid = true;
    }
    else {
        args.IsValid = false;
        $(src).text('Aucune ville ne correspond au code postal renseigné. Etes-vous certain de ne pas vous être trompé ?');
    }
}

AssurlandPro.ZipCodeValidate = function(ddl, isvalid, ddlvalidator) {

    if (isvalid) {
        ALF.show(inseeRowId);
        val = document.getElementById(valInseeCode);
        ValidatorEnable(val, true);
    }
    else {
        ALF.hide(inseeRowId);

    }
}
AssurlandPro.validateEmail = function(src, args) {
    var reg = new RegExp(maskEmail, "g");
    args.IsValid = reg.test(args.Value);
}

AssurlandPro.validatePhoneNumber = function(src, args) {
    var phoneNumber = args.Value.replace(/\./g, '');
    var reg = new RegExp("0[1-9][0-9]{8}", "g");
    if (reg.test(phoneNumber)) {
        $.ajax({ type: "POST",
            url: "/WebService/Form.asmx/ValidatePhoneNumber",
            data: "{tel:'" + phoneNumber + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function(result) {
                args.IsValid = result.d;
            },
            error: function(xhr, ajaxOptions, thrownError) {
                args.IsValid = true;
            }
        });
    }
    else {
        args.IsValid = false;
    }

}
AssurlandPro.noSunday=function (date){
          var day = date.getDay();
            return [(day > 0), ''];
      }; 
      
AssurlandPro.validatePhoneCallDate = function(src, args) {
    if (args.Value == "") {
        args.IsValid = false;
        src.errormessage = "Veuillez saisir la date du rdv";
        $(src).text(src.errormessage);
    } else {
        var phoneCallDate = Date._tryParseDate("dd/mm/yy", args.Value);
        if (phoneCallDate == null) {//check format
            args.IsValid = false;
            src.errormessage = "Veuillez vérifier la date saisie";
            $(src).text(src.errormessage);
        } else {//check date > today
            if (phoneCallDate <= Date._today()) {
                args.IsValid = false;
                src.errormessage = "Veuillez saisir une date dans le futur";
                $(src).text(src.errormessage);
            } else {//sunday
            if (phoneCallDate.getDay() == 0) {
                args.IsValid = false;
                src.errormessage = "Veuillez saisir un autre jour que le dimanche";
                $(src).text(src.errormessage);
                } else {
                    args.IsValid = true;
                }
            }
        }

    };
}
//Waiting process
var processId = "";
var timeout = 120 * 1000;
var iteration = 4 * 1000;
var firstIteration = 5 * 1000;
AssurlandPro.startWait = function() {
    // Global timeout
    setTimeout("AssurlandPro.waitError();", timeout);
    // Start periodic process state check
    setTimeout("AssurlandPro.isQuotationProcessFinished();", firstIteration);
};
AssurlandPro.waitError = function() {
    ALF.goToURL("/Pages/Error/ErrorPage.aspx");
};
AssurlandPro.isQuotationProcessFinished = function() {
       $.ajax({ type: "POST",
        url: "/WebService/Form.asmx/ProcessFinish",
        data: "{processId:'" + processId + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
         
            if (result.d != "0") { //finished
                ALF.goToURL("/Pages/Form/restit.aspx?qid=" + result.d);
            }
            else {
              
                setTimeout("AssurlandPro.isQuotationProcessFinished();", iteration);
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            setTimeout("AssurlandPro.isQuotationProcessFinished();", iteration);
        }
    });
};

$(document).ready(function() {
    $('.alp_curency').blur(function() {

        $this = $(this);        
        var num = $this[$this.is('input, select, textarea') ? 'val' : 'html']();
        if (num != '') {
            num2 = '';
            var h = 1;
            for (var i = num.length; i > 0; i--) {
                a = num.substring(i - 1, i);
                if (h % 3 == 0)
                    num2 = ' ' + a + num2;
                else
                    num2 = a + num2;
                h++;
            }
            $this.val(num2);
        }

    });

}
);

(function($) {
    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function() {
        return this.each(function(i) {
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = Math.ceil((ph - ah) / 2);
            $(this).css('margin-top', mh);
        });
    };
})(jQuery);

function form_escapeIFrame() {
    if (top.location != self.document.location) {
        // Post form in a new window
        document.forms[0].target = '_blank';       
    }
}
