﻿//Define the Date format for Javascript.
Date.firstDayOfWeek = 7;
Date.format = 'mm/dd/yyyy';

var createaccount = false;

var root = window.addEventListener || window.attachEvent ? window : document.addEventListener ? document : null;
if (root) {
    // root.onbeforeunload = function () { return 'If you use your browser’s back button your information will not be saved.  Please use the “< Back” button on the web page instead.'; };
    root.onbeforeunload = function () {
        if (!createaccount) {
            return 'If you use your browser’s back button your information will not be saved.  Please use the “<< Previous” button on the web page instead.';
            openTab(0);
        }
    };
}



function ClearAlert() {
    $("#ValidationSummary").html("");
    return false;
}

$(document).ready(function () {

    $('#navContainer').tabs({ fxFade: true, fxSpeed: 'fast', disabled: [1, 2, 3, 4, 5],
        onClick: function (url) {
            // disableTabs(url);
        }
    });

    // Tabs
    $("#provider_ReceiveProviderNumberDate").datepicker({ showOn: 'button', buttonImage: '../Content/images/calendar.gif', buttonImageOnly: true, maxDate: '0d' });

    $("#independentButton").click(registerIndependent);
    $("#agencyButton").click(registerAgency);

    // append * for required label.
    $('label.required').append('<strong style=\"color:red;\">&nbsp;*</strong>');

    // Back button at step 2.
    $(".open1").click(function () {

        // Validate form before openning previous tab.
        //  if ($('form').validate().form()) {
        // Verify if remote validations are valid.
        //      if ($('#provider_MedicaidID').valid()) {

        //        $("#ValidationSummary").html("");
        //        $("#provider_BusinessName").attr('disabled', 'disabled');
        //        $("#provider_TaxID").attr('disabled', 'disabled');
        //        $("#provider_SSN").attr('disabled', 'disabled');
        //        $("#provider_ReferringProviderUserName").attr('disabled', 'disabled');
        //        $("#provider_PreviousBillingService").attr('disabled', 'disabled');

        //     }
        //  }
        //  else {
        //     $("#ValidationSummary").html("Please fix following errors.");
        //  }
        $("#ValidationSummary").html("");
        createaccount = false;
        openTab(0);

    });

    $(".open2").click(function () {

        // Validate form before openning previous tab.
        if ($('form').validate().form()) {

            $("#provider_ReceiveProviderNumberDate").attr('disabled', 'disabled');
            $("#provider_user_UserName").attr('disabled', 'disabled');
            $("#provider_user_Password").attr('disabled', 'disabled');
            $("#provider_user_RePassword").attr('disabled', 'disabled');
            $("#provider_ReferrerTypeId").attr('disabled', 'disabled');
            createaccount = false;
            $("#ValidationSummary").html("");
            openTab(1);
        }
    });

    $(".open4").click(function () {
        $("#ValidationSummary").html("");
        createaccount = false;
        openTab(4);
    });
    // Previous button handling.
    $(".openZ").click(function () {
        createaccount = false;
        $("#ValidationSummary").html("");
        openTab(2);
    });
    // SignUp Cancel button.
    $(".openCancel").click(function () {
        window.location = 'https://www.rhinobill.com';
    });
    // Formatting.
    $('#provider_NameFirst').keyfilter(/[a-zA-Z-.]/);
    $('#provider_NameLast').keyfilter(/[a-zA-Z-.]/);
    $('#provider_NameMiddle').keyfilter(/[a-zA-Z-.]/);
    $('#provider_AddressStreet1').keyfilter(/[a-zA-Z0-9 -.#]/);
    $('#provider_AddressStreet2').keyfilter(/[a-zA-Z0-9 -.#]/);
    $('#provider_AddressCity').keyfilter(/[a-zA-Z .]/);
    $('#provider_BusinessName').keyfilter(/[a-zA-Z -']/);
    $('#provider_user_NameLast').keyfilter(/[a-zA-Z-.]/);
    $('#provider_user_NameFirst').keyfilter(/[a-zA-Z-.]/);
    $('#provider_user_UserName').keyfilter(/[_a-zA-Z0-9]/);
    $('#provider_ReferringProviderUserName').keyfilter(/[_a-zA-Z0-9]/);

    $("#provider_MedicaidID").mask("9999999", { placeholder: "" });
    $("#provider_AddressZip").mask("99999", { placeholder: "" });
    $("#provider_Telephone").mask("(999) 999-9999", { placeholder: "_" });
    $("#provider_SSN").mask("999-99-9999", { placeholder: "_" });

    $("#provider_TaxID").mask("99-9999999", { placeholder: "_" });
    $("#provider_NPI").mask("9999999999", { placeholder: "" });

    // $('#provider_NameFirst').mask("############", { placeholder: "" })
    //  $('#provider_NameLast').mask("############", { placeholder: "" })

    bindingRadioEventHandler();
    bindingDropdownlistEventHandler();

    //    $("#frmSignUp").bind("submit", frmSignUpCallback);

    $("#btnStep2").click(ProcessStep2);
    // $("#btnStep3").click(ProcessStep3);
    $("#btnStep4").click(ProcessStep4);
    $("#btnStep5").click(ProcessStep5);

    // unblock when ajax activity stops
    //    $().ajaxStop($.unblockUI);

    $("#provider_user_NameLast").attr('disabled', 'disabled');
    $("#provider_user_NameFirst").attr('disabled', 'disabled');
    $("#provider_user_UserName").attr('disabled', 'disabled');
    $("#provider_user_Password").attr('disabled', 'disabled');
    $("#provider_user_RePassword").attr('disabled', 'disabled');
    $("#provider_BusinessName").attr('disabled', 'disabled');
    $("#provider_TaxID").attr('disabled', 'disabled');
    $("#provider_SSN").attr('disabled', 'disabled');
    $("#provider_ReferringProviderUserName").attr('disabled', 'disabled');
    $("#provider_PreviousBillingService").attr('disabled', 'disabled');
    $("#provider_ReferrerTypeId").attr('disabled', 'disabled');
    $("#UserSignature").hide();


    $('#provider_user_NameLast').keyup(function () { ShowSign(); });
    $('#provider_user_NameFirst').keyup(function () { ShowSign(); });

    $('#ProviderNumberDateLink').hover(
           function () {
               $(".ProviderNumberDateLink").show();
           },
           function () {
               $(".ProviderNumberDateLink").hide();
           }
                  );

});


function ProcessStep2(e) {
    var res1 = true;
    var res2 = true;
    var res3 = true;

    if ($('form').validate().form()) {

        if ($('#provider_MedicaidID').val() != '') {
            res1 = $('#provider_MedicaidID').valid();
        }

        if ($('#provider_NPI').val() != '') {
            res2 = $('#provider_NPI').valid();
        }

        // Valid taxid only for agency.
        if ($("#provider___ProviderType").val() == "Agency") {

            if ($('#provider_TaxID').val() == '__-_______') {
                res3 = false;
            }

            if ($('#provider_TaxID').val() != '' )  {
                res3 = $('#provider_TaxID').valid();
            }
        }

        if (res1 && res2 && res3 ) {
            $("#ValidationSummary").html("");
            $('#navContainer').tabs('enable', 2)

            $('#provider_user_UserName').removeClass("input-validation-error");
            $('#provider_user_Password').removeClass("input-validation-error");
            $('#provider_user_RePassword').removeClass("input-validation-error");

            // Reset dropdown selected values in case of page refresh.
            $("#FindWaysDropDownList").val('0')
            $("#provider_ReferrerTypeId").val('');

            $("#provider_ReferrerTypeId").attr('disabled', '');
            $("#provider_ReceiveProviderNumberDate").attr('disabled', '');
            $("#provider_user_UserName").attr('disabled', '');
            $("#provider_user_Password").attr('disabled', '');
            $("#provider_user_RePassword").attr('disabled', '');
            createaccount = false;
            $("#provider___Step2").val("true")
            openTab(2);
        }
    }
    else {
        $("#ValidationSummary").html("Please fix following errors.");
    }
}



function ProcessStep4(e) {

    if ($('form').validate().form()) {
        $("#ValidationSummary").html("");
        $('#navContainer').tabs('enable', 4)
        $("#provider___Step4").val("true")
        ShowSign();
        $("#agreerec").hide();
        createSummaryInfo();
        openTab(4);
        createaccount = false;
    }
    else {
        $("#ValidationSummary").html("Please fix following errors.");
    }


}
function ProcessStep5() {

    if ($("#agree_yes").attr("checked")) {
        createaccount = true;
        $("#agreerec").hide();
        return true;
    }
    else {
        createaccount = false;
        $("#agreerec").show();
        return false;
    }


}

//function SubmitfrmSignUp(e) {
////    $.blockUI();
//    //$.blockUI.defaults.timeout = 500;
//    
////    $.post($(e).attr('action'), $(this).serialize(), frmSignUpCallback);
////    frmSignUpCallback();
//    
//    //$.unblockUI();
//    // This is important, this will stop the event from bubbling and your form from submitting twice.
//    return false;  
//}

function GetCurrentHostUrl() {
    var ctr = location.pathname.indexOf("/", 1);
    return location.protocol + "//" + location.host + location.pathname.substring(0, ctr);
    //return location.protocol + "//" + location.host + location.pathname.substring(0, ctr) + "/ASP";
}

function openTab(index) {

    // switch to given tab.
    $('#navContainer').tabs('select', index);
}


function registerIndependent() {
    $("#navUl").removeClass("hid");
    $("#provider___ProviderType").val("Independent");
    $("#Api,#businessNameArea,#businessNameArea,#taxIDArea").hide();
    $("#Ipi,#npiWarningArea,#ssnArea,#middleNameArea,#ReceiveProviderNumberDateLabel").show();
    $("#provider_SSN").attr('disabled', '');

    $("#provider_ReceiveProviderNumberDate").attr('disabled', 'disabled');
    $("#provider_ReferrerTypeId").attr('disabled', 'disabled');
    $("#provider_BusinessName").attr('disabled', 'disabled');
    $("#provider_TaxID").attr('disabled', 'disabled');
    if ($("#provider_SSN").val() == "999-99-9999") {
        $("#provider_SSN").val("");
    }
    $('#navContainer').tabs('enable', 1);
    openTab(1);
}

function registerAgency() {
    $("#navUl").removeClass("hid");
    $("#provider___ProviderType").val("Agency");
    $("#Api,#businessNameArea,#businessNameArea,#taxIDArea").show();
    $("#Ipi,#npiWarningArea,#ssnArea,#middleNameArea,#ReceiveProviderNumberDateLabel,#ReceiveProviderNumberDateInput").hide();
    $("#npiArea label").append('<strong style=\"color:red;\">&nbsp;*</strong>');

    $("#provider_ReceiveProviderNumberDate").attr('disabled', 'disabled');
    $("#provider_ReferrerTypeId").attr('disabled', 'disabled');

    $("#provider_ReceiveProviderNumberDate").val("01/01/01");
    $("#provider_SSN").val("999-99-9999");

    $("#provider_BusinessName").attr('disabled', '');
    $("#provider_TaxID").attr('disabled', '');

    $('#navContainer').tabs('enable', 1)
    openTab(1);
}


// Tabs
function disableTabs(url) {
    var urlStr = url.toString();
    var n = parseInt(urlStr.charAt(urlStr.length - 1));
    var defaultTabs = [1, 2, 3, 4, 5];
    var tabs = defaultTabs.splice(0, n);
    var tabController = $("#navContainer");
    for (var i = 0; i < defaultTabs.length; i++) {
        tabController.disableTab(defaultTabs[i]);
    }

    if (n == 1) {
        $(":text,:password").val("").removeClass("tooltipinputerr").removeClass("tooltipinputok");
    }

    // Hide all error tips
    $(".onError, .onFocus").hide();
}

function bindingRadioEventHandler() {
    $("#provider_PreviousBillingServiceShow").click(function () {
        $("#PreviousBillingServiceArea").show();
        //$("#provider_PreviousBillingService").val("");
        $("#provider_PreviousBillingService").attr('disabled', '');
    });
    $("#provider_PreviousBillingServiceHide").click(function () {
        $("#PreviousBillingServiceArea").hide();
        $("#provider_PreviousBillingServiceTip").removeClass();
        //        $("#provider_PreviousBillingService").val("DefaultValue"); // This will skip validation message.
        $("#provider_PreviousBillingService").attr('disabled', 'disabled');
    });
}

function bindingDropdownlistEventHandler() {
    $("#StateDropDownList").change(function () {
        $("#provider_AddressState").val($("#StateDropDownList option:selected").text());
    });

    $("#FindWaysDropDownList").change(function () {
        $("#provider_ReferrerTypeId").val($("#FindWaysDropDownList option:selected").val());
        $('form').validate().form();
        if ($("#FindWaysDropDownList option:selected").val() == 1) {
            $("#refererUserNameArea").show();
            $("#provider_ReferringProviderUserName").attr('disabled', '');
        } else {
            $("#refererUserNameArea").hide();
            $("#provider_ReferringProviderUserNameTip").removeClass();
            $("#provider_ReferringProviderUserName").attr('disabled', 'disabled');
        }
    });
}

function createSummaryInfo() {

var conditionalHtml = "";

    if ($("#provider___ProviderType").val() == "Agency") {
        conditionalHtml  = "<tr><td class='tdLeft'><label>Business:</label></td>"
                         + "<td class='tdLeft'>" + $("#provider_BusinessName").val() + "</td></tr>"
    }

    var confirmHtml1 = "<table>"
                    + "<tr><td class='tdLeft'><label>First Name:</label></td>"
                    + "<td class='tdLeft'><span style='display:block;'>" + $("#provider_NameFirst").val() + "</span><a style='display:block;text-align:right;padding-right:15px;' href='#' onclick='openTab(1);'>Edit</a></td></tr>"
                    + "<tr><td class='tdLeft'><label>Last Name:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_NameLast").val() + "</td></tr>"
                    + conditionalHtml
                    + "<tr><td class='tdLeft'><label>Address:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_AddressStreet1").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'></td>"
                    + "<td class='tdLeft'>" + $("#provider_AddressStreet2").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>City:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_AddressCity").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>State:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_AddressState").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>Zip Code:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_AddressZip").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>Phone #:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_Telephone").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>Email:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_Email").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>SSN:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_SSN").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>Medicaid ID:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_MedicaidID").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>NPI:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_NPI").val() + "</td></tr>"
                    + "</table>"

    var confirmHtml2 = "<table><tr><td colspan='2'><hr></td></tr>"
                    + "<tr><td class='tdLeft'><label>Received Date:</label></td>"
                    + "<td class='tdLeft'>" + $("#provider_ReceiveProviderNumberDate").val() + "</td></tr>"
                    + "<tr><td class='tdLeft'><label>UserName:</label></td>"
                    + "<td class='tdLeft'><span style='display:block;'>" + $("#provider_user_UserName").val() + "</span><a style='display:block;text-align:right;padding-right:15px;' href='#' onclick='openTab(2);'>Edit</a></td></tr>"
                    + "<tr><td class='tdLeft'><label>Password:</label></td>"
                    + "<td class='tdLeft'>********</td></tr>"
                    + "</table>"

    $("#confirmTableLeft").html("").append(confirmHtml1);
    $("#confirmTableRight").html("").append(confirmHtml2);
}

//function ShowBlock() {
//    $("#UserSignature").show();
//}

function ShowSign() {

    if ($('#provider_NameFirst').val() != "" && $('#provider_NameLast').val() != "") {

        var params = "&NameLast=" + $('#provider_NameLast').val() +
        "&NameFirst=" + $('#provider_NameFirst').val() +
        "&BusinessName=" + $('#provider_BusinessName').val() +
        "&AddressCity=" + $('#provider_AddressCity').val() +
        "&SSN=" + $("#provider_SSN").val() +
        "&AddressStreet1=" + $('#provider_AddressStreet1').val() +
        "&AddressZip=" + $("#provider_AddressZip").val() +
        "&Telephone=" + $("#provider_Telephone").val() +
        "&AddressState=" + $("#StateDropDownList").val() +
        "&NPI=" + $("#provider_NPI").val() +
        "&MedicaidID=" + $("#provider_MedicaidID").val() +
        "&PreviousBillingService=" + $("#provider_PreviousBillingService").val() +
        "&Email=" + $("#provider_Email").val() +
        "&fontname=Arial";

        $("#pdfprint").attr('href', '/ASP/SignUp/PdfRetriever?pdfType=1010' + params);
    }
    else {
        $("#UserSignature").hide();
        $("#pdfprint").attr('href', '');
        $("#SignImage").attr('src', '');
    }

}