﻿var shouldsubmit = false; // Represent the button should ask for confirmation or not.
var Global_LastSucceededPingToServer;
var LastFocus;

function GetCaption(CapCode) {
  var capDesc =  $("#UI_ClientCaptioning table tr td:contains('" + CapCode + "')").parent().find("td:eq(1)").text() ;

  return capDesc;
}






function clearDefault(el) {
    if (el.defaultValue == el.value) el.value = "";
}

function ResetDefault(el) {
    if (el.value == "") el.value = el.defaultValue;
}

jQuery.extend(
    jQuery.expr[':'], {
        regex: function(a, i, m, r) {
            var r = new RegExp(m[3], 'i');
            return r.test(jQuery(a).text());
        }
    }
);





    $(document).ready(function() {
        // initialise the page
        Maininit();
        KeepAlive_Init();
    });


function pageLoad() {
    Maininit();
}

function Maininit() {
    // do initialisation stuff here.



    //DOB COV
    $("#UI_SearchForClient_DOB, #UI_VSearchForClient_DOB, .UI_CustomerDateOfBirth_TextBox_Class, .ArticleDeclaration1_DateMedicalPrescription_TextBox_Class, .ArticleDeclaration2_DateMedicalPrescription_TextBox_Class, .ArticleDeclaration3_DateMedicalPrescription_TextBox_Class, .ArticleDeclaration4_DateMedicalPrescription_TextBox_Class").change(function() {

        if ($(this).val().indexOf("-") <= 0) {
            if ($(this).val().length == 8) {

                // needs the "-" character
                var Daystr = $(this).val().substr(0, 2);
                var Monthstr = $(this).val().substr(2, 2);
                var Yearstr = $(this).val().substr(4, 4);

                $(this).val(Daystr + "-" + Monthstr + "-" + Yearstr);

            }
        }

    });





    var BlanketHeight = $(document).height();
 $(".UI_Blanket").css({ 'height': BlanketHeight }); 




    // do width correction.

    if (screen.width <= 1024) {
  
        $("#Appview").attr("style", "margin-left:-10px;");

    } else {
    
    }





    //get the correct date in the insurance right box
    var currentTime = new Date();
    currentTime.setDate(currentTime.getDate());
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();

    $("#UI_SearchForClient_InvoiceDate").val(day + "-" +  month +"-" + year);

    
    
    
    Init_MessageBoxes();

    $(".jquery_date").datepicker({ dateFormat: 'dd-mm-yy' });
    
    $(".jquery_date_dd").datepicker({
        dateFormat: 'dd-mm-yy',
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true,
        showButtonPanel: true,
       // defaultDate: '-30y',
        regional: 'nl'
    });
    //$( ".jquery_date_dd" ).datepicker( $.datepicker.regional[ "nl" ] );

    Init_Buttons();


    $(".alphabet").keypress(function(event) {
    });

    $(".validateEmail").keyup(function() {

        var email = $(".validateEmail").val();

        if (email != 0) {
            if (isValidEmailAddress(email)) {
                $("#validEmailImg").css({
                    "background-image": "url('../WebResources/Img/icons/v.png')"
                });
            } else {
            $("#validEmailImg").css({
                    "background-image": "url('../WebResources/Img/icons/x.png')"
                });
            }
        } else {
        $("#validEmailImg").css({
                "background-image": "none"
            });
        }

    });


    $(".numeric").keypress(function(event) {
        // Backspace, tab, enter, end, home, left, right
        // We don't support the del key in Opera because del == . == 46.
        var controlKeys = [8, 9, 13, 35, 36, 37, 39];
        // IE doesn't support indexOf
        var isControlKey = controlKeys.join(",").match(new RegExp(event.which));
        // Some browsers just don't raise events for control keys. Easy.
        // e.g. Safari backspace.
        if (!event.which || // Control keys in most browsers. e.g. Firefox tab is 0
      (48 <= event.which && event.which <= 57) || // Always 1 through 9
      //(48 == event.which && $(this).attr("value")) || // No 0 first digit
      isControlKey) { // Opera assigns values for control keys.
            return;
        } else {
            event.preventDefault();
        }
    });
    
    
    

}


function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}



function Init_Buttons() {

//    $(".FocusButton_Link ").bind("mouseenter", function() {

//        $(this).find(".FocusButton").addClass("FocusButton_H");
//    }).bind("FocusButton_Link", function() {
//        alert("binding");
//    });


    $(".FocusButton_Link").hover(
  function() {
    $(this).find(".FocusButton").addClass("FocusButton_H");
  },
  function() {
  $(this).find(".FocusButton").removeClass("FocusButton_H");
  }
);


  
  



}






function Init_MessageBoxes() {
    $('.MessageBlob .MessageBlob_ContentBox').click(function() {

        var PopUp_Title
        PopUp_Title = $(this).closest(".MessageBlob").find(".MessageBlob_PopUp_Content .MessageBlob_Title").html();

        var PopUp_SubTitle;
        PopUp_SubTitle = $(this).closest(".MessageBlob").find(".MessageBlob_PopUp_Content .MessageBlob_SubTitle").html();

        var PopUp_Message;
        PopUp_Message = $(this).closest(".MessageBlob").find(".MessageBlob_PopUp_Content .MessageBlob_Message").html();

        var PopUp_RightSide;
        PopUp_RightSide = $(this).closest(".MessageBlob").find(".MessageBlob_PopUp_Content .MessageBlob_Right");

        var PopUp_MessageID;
        PopUp_MessageID = $(this).closest(".MessageBlob").find(".MessageBlob_PopUp_Content .messageID").html();



        $("#ctl00_Content_UI_MessageToShow").val(PopUp_MessageID);


        // Fill Messagebox
        $("#PopupDiv_MsgBox_holder  .MessageBlob_Title").html(PopUp_Title);
        $("#PopupDiv_MsgBox_holder  .MessageBlob_SubTitle").html(PopUp_SubTitle);

        if (PopUp_Message != "") {
            $("#PopupDiv_MsgBox_holder  .MessageBlob_Message").html(PopUp_Message).show();
        } else {
            $("#PopupDiv_MsgBox_holder  .MessageBlob_Message").html(PopUp_Message).hide();

        }
        // ANd the right side
        $("#PopupDiv_MsgBox_holder  .MessageBlob_Right").html(PopUp_RightSide.html());
        $("#PopupDiv_MsgBox_holder  .MessageBlob_Right").css("background-color", PopUp_RightSide.css("background-color"));


        // Show the msgbox.
        $(".UI_Blanket").fadeTo(500, "0.8", function() {
            $("#PopupDiv_MsgBox_holder").fadeTo(500, "1");
            //document.body.scroll = "no"


            $("body").css("overflow", "hidden");
        });



        $("#PopupDiv_MsgBox_holder .MessageSaveAndReturn").click(function() {
            if ($("#ctl00_Content_UI_MessageToShow").length > 0) {
                var MessageID = $("#ctl00_Content_UI_MessageToShow").val().toString();
                if (MessageID !== undefined && MessageID != "0") {

                    Save_UserHasSeenMessage(MessageID);
                    remove_Blanket();
                    $("body").css("overflow", "");

                }
            }
        });





    });

    // Hover functions


    $(".MessageBlob").fadeTo(100, "0.6");



    $(".MessageBlob ").bind("mouseenter", function() {
        $(this).fadeTo(200, "1");
        $("p:last", this).text(++n);
    }).bind("mouseleave", function() {
        $(this).fadeTo(100, "0.6");
    });


// Unbind the popup for clicks and hovers, otherwise it will be messy :)
    $('#PopupDiv_MsgBox_holder .MessageBlob .MessageBlob_ContentBox').unbind('click');
    $('#ctl00_PopupDiv_MsgBox_holder_Important .MessageBlob .MessageBlob_ContentBox').unbind('click');
    
    $("#PopupDiv_MsgBox_holder .MessageBlob").unbind('mouseenter').unbind('mouseleave');
    $("#ctl00_PopupDiv_MsgBox_holder_Important .MessageBlob").unbind('mouseenter').unbind('mouseleave');
    
    $("#PopupDiv_MsgBox_holder .MessageBlob").fadeTo(10, "1");
    $("#ctl00_PopupDiv_MsgBox_holder_Important .MessageBlob").fadeTo(10, "1");









    $(".MessageSaveAndReturn").click(function() {
        if (LastFocus != null) {
            $(LastFocus).focus();
            LastFocus = null;
        }


        if ($("#ctl00_Content_UI_MessageToShow").length > 0) {
            var MessageID = $("#ctl00_Content_UI_MessageToShow").val().toString();


            Save_UserHasSeenMessage(MessageID);
            remove_Blanket();
        } else {
            remove_Blanket();

        }
    });
    
    
    
    
    
    
}

function remove_Blanket() {

    $("#PopupDiv_MsgBox_holder,#ctl00_PopupDiv_MsgBox_holder_Important, #PopupDiv_IFrame_holder, #PopupDiv_IcodisDown_holder ").fadeOut(500, function() {
    $(".UI_Blanket , #blanket_IcodisDownPage").fadeOut(200);
    $("body").css("overflow", "");
    });


    
    
}





function Save_UserHasSeenMessage(MessageID) {
    // Call Function
    

    var sendMessageID = MessageID*1;

    if (sendMessageID > 0) {

        $.jmsajax({
            type: "POST",
            url: "../../WebMethods/HomePage.aspx",
            method: "UserSeesMessage",
            dataType: "msjson",
            data: { MID: sendMessageID },

            error: function(data) {
                //alert(data);
            },
            success: function(data) {
                // Set Global var.
                // alert(data);

            }
        });
    }

}











function Msgbox(Title, Subtitle, Message, RightColor, RightCharacter) {
  

        var PopUp_Title
        PopUp_Title = Title;

        var PopUp_SubTitle;
        PopUp_SubTitle = Subtitle;
        
        var PopUp_Message;
        PopUp_Message = Message;


        // Fill Messagebox
        $("#PopupDiv_MsgBox_holder  .MessageBlob_Title").html(PopUp_Title);
        $("#PopupDiv_MsgBox_holder  .MessageBlob_SubTitle").html(PopUp_SubTitle);

        $("#PopupDiv_MsgBox_holder  .MessageBlob_Message").css("display", "block");
        
        $("#PopupDiv_MsgBox_holder  .MessageBlob_Message").html(PopUp_Message);
        // ANd the right side
        $("#PopupDiv_MsgBox_holder  .MessageBlob_Right").html(RightCharacter);
        $("#PopupDiv_MsgBox_holder  .MessageBlob_Right").css("background-color", RightColor);


        // Show the msgbox.
        $(".UI_Blanket").fadeTo(500, "0.8", function() {
        $("#PopupDiv_MsgBox_holder").fadeTo(500, "1");
        $("body").css("overflow", "hidden");
        });

        $("#ctl00_Content_UI_MessageToShow").val("0");

}

function KeyDownHandler(btnid, e) {
    var btn = document.getElementById(btnid);
    if (btn != null) {
        var intKey;
        if (e.keyCode == null) {
            intKey = e.which;
        }
        else {
            intKey = e.keyCode;
        }
        if (intKey == 13) { //enter key
            // cancel the default submit
            shouldsubmit = true;
            $('#' + btnid).click();
            window.location = $('#' + btnid).attr('href');
            
            return false;
            // submit the form by programmatically clicking the specified button
        } return true;
    }

}

function DisableKeyDownHandler(e) {
    var intKey;
    if (e.keyCode == null) {
        intKey = e.which;
    }
    else {
        intKey = e.keyCode;
    }
    return (intKey != 13);
}

/* Dutch (UTF-8) initialisation for the jQuery UI date picker plugin. */
/* Written by Mathias Bynens <http://mathiasbynens.be/> */
jQuery(function($) {
    $.datepicker.regional['nl'] = {
        closeText: 'Sluiten',
        prevText: '←',
        nextText: '→',
        currentText: 'Vandaag',
        monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
		'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
        monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
		'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
        dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
        dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
        dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
        weekHeader: 'Wk',
        dateFormat: 'dd-mm-yy',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: false,
        yearSuffix: ''
    };
    $.datepicker.setDefaults($.datepicker.regional['nl']);
});


function KeepAlive_Init() {
    // first time
    KeepAlive_Check();
    
    // kep alive every minute
    setInterval("KeepAlive_Check();", 60000);

}
function KeepAlive_Check() {


    if ($.jmsajax !== undefined) {

        $.jmsajax({
            type: "POST",
            url: "../../WebMethods/WM_KeepAlive.aspx",
            method: "Ping",
            dataType: "msjson",
            data: {},

            error: function(data) {
                //alert(data);
            },
            success: function(data) {
                // Set Global var.
                if (data !== undefined && data !== null) {
            
                    if (data == true) {
                        Global_LastSucceededPingToServer = new Date();

                    } else {
                        var now = new Date();

                    }

                }
            }
        });
        
    }

}


function getQueryString(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function HideRealTimeDeclarationBlanket() {
    $("#PopUpDiv_RealTimeDeclarationForm").hide();
    $("#iframe_RealtimeDeclaration").empty();
    $("#blanket_RealTimeDeclarationForm").hide();
}

function CloseRealTimeDeclaration() {
    shouldsubmit = true;
    $("#PopUpDiv_RealTimeDeclarationForm").hide();
    $("#iframe_RealtimeDeclaration").empty();
    $("#blanket_RealTimeDeclarationForm").hide();
    window.location.href = 'overviewdeclarations.aspx';
}
