$(document).ready(function () {
    debugger
    sessionStorage.clear();
    CheckCodeTemplate();
    LoadFloor();
});

//CheckCodeTemplate
function CheckCodeTemplate() {
    debugger;
    var codeData = {
        //Dml_Indicator: "CT",
        Dml_Indicator: "BCT",
        hospital_id: $('#HospitalId').val(),
        // for onemaster 
        branch_id: 0
        //branch_id: $('#HospitalBranchID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Code_Templates_Sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(codeData),
        success: function (data) {
            debugger;

            var data1 = JSON.parse(data)
            data1 = data1.data;

            if (data1[0].count1 == 0) {
                // for onemaster 
                //alert('' + $('#branch  option:selected').html() + ' Not Added Code Template. Please Add the Code Template at SetupIds');
                alert('Not Added Code Template. Please Add the Code Template at SetupIds');
                window.location.href = "/Hospitalsetup"
            }
            else {
                ready()
            }

        }
    });
}

function ready() {
    debugger
    Hideshow();
    if (($('#DoctorUser_Id').val() == '' || $('#DoctorUser_Id').val() == null) && ($('#myHiddenVar1').val() == '' || $('#myHiddenVar1').val() == null)) {
        // for onemaster 
        LoadBranch();
        LoadDepartment();
        LoadDesignation();
        LoadRole();
        load_salutation(0);
        loadcountry_I();
        LoadSpecialty();
        //loadcountry()
    }

    if ($('#hiddenRole_id').val() == 3) {
        toastr.warning("You have no rights to access this page")
    }

    if ($('#DoctorUser_Id').val() != '' && $('#DoctorUser_Id').val() != null) {
        //$('#myHiddenVar').val($('#DoctorUser_Id').val());
        $('#btnSubmit1').html('Update');
        loadcountry();
        LoadSpecialty();
    }

    if ($('#myHiddenVar1').val() != '' && $('#myHiddenVar1').val() != null) {

        $('#myHiddenVar').val($('#myHiddenVar1').val());
        $('#btnSubmit1').html('Update');

        loadcountry();
        LoadSpecialty();

        var doctor_type = $('#Hdoctor_type').val()
        load_doctor_type(doctor_type);
    }
    else if ($('#hiddenRole_id').val() == 2 || $('#hiddenRole_id').val() == 5 || $('#hiddenRole_id').val() == 6 || $('#hiddenRole_id').val() == 1) {

        $('#btnSubmit1').html('Submit');
        //  loadcountry_I();
        LoadSpecialty_I();
        // LoadBranch_user();
        // LoadBranch();
        //load_salutation(0);
    }
    else {
        //    $('#btnSubmit1').html('Update');
        //loadcountry();
        //  LoadSpecialty();
    }

    if ($('#myHiddenVar1').val() != '' && $('#myHiddenVar1').val() != null) {
        $('#div_doctor_type').attr('hidden', false)
    }
    else {
        $('#div_doctor_type').attr('hidden', true)
    }

}


$("#btnSubmit1").click(function () {
    debugger;
    $('.da-load-patsearch').toggleClass();
    if ($('#btnSubmit1').html() == 'Submit') {
        DoctorCreation_insert();
        //DoctorAddressValuesinsert();
    }
    else {
        DoctorCreation();
    }
    $('.da-load-patsearch').addClass('hide');
});

$("#btnSpecialty").click(function () {

    AddSpecialty();
});

$("#btnQualification").click(function () {

    AddQualification();
});


function loadcountry() {
    debugger;
    var country = {
        Dml_Indicator: "C"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Country;
                    // var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtCountry').append(Country);
                    $('#txtPCountry').append(Country);
                    $("#txtCountry").change();
                    $('#txtPCountry').change();
                }

                if ($('#DoctorUser_Id').val() != '' && $('#DoctorUser_Id').val() != null) {
                    UserAddressValues();
                }
                else {
                    DoctorAddressValues();

                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

$("#txtdob").change(function () {
    debugger;
    var a = $.trim($("#txtdob").val()).split("/");
    var a1 = a[1] + "-" + a[0] + "-" + a[2];
    dob = new Date(a1);
    var today = new Date();
    var age = Math.floor((today - dob) / (365.25 * 24 * 60 * 60 * 1000));
    $('#txtAge').val(age);
    // $("#txtAge").val(new Date().getFullYear() - new Date(a1).getFullYear());
});

$('.close').click(function () {
    $("#btnSpecialty").html("Add");
    $("#btnQualification").html("Add");
});

$('#documents').change(function () {
    debugger;
    doctorimageupload();
});

function doctorimageupload() {
    debugger;
    var uploadpath = $('#documents').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "JPG" || fileExtension == "JPEG") {
    }
    else {
        $('#Pdocuments').show();
        $('#Pdocuments').fadeOut(8000);
        $('#Pdocuments').html('Please upload file having extensions .jpeg only.');
        $('#documents').val('');
        $('#documents').focus();
        return false;
    }
    if (window.FormData !== undefined) {
        var fileUpload = $("#documents").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();

        $.when(checkimagedimensionvalidation(fileUpload.files[0], 1024, 512)).
        done(function (Results) {
            if (Results == 'Success') {
                for (var i = 0; i < files.length; i++) {
                    fileData.append(files[i].name, files[i]);
                }
                fileData.append('username', 'Manas');
                $.ajax({
                    url: '/DoctorProfile/UploadPhotoFiles',
                    type: "POST",
                    contentType: false,
                    processData: false,
                    data: fileData,
                    success: function (result) {
                        $.each(result, function (i, emp) {
                            $('#profile-photo').empty();
                            $('#profile-photo').show();
                            var d, n, drloimage;
                            d = new Date();
                            n = d.getTime();
                            drloimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Photos/' + emp + '?' + n;
                            $('#profile-photo').append('<a href="' + drloimage + '" target="_blank">Image</a>');
                            $('#drphoto').val(emp);
                            var drphotoSrc = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + "/Photos/" + emp;
                            $('#ProfilePhotoTop,#ProfilePhoto').attr('src', drphotoSrc);
                        });
                    }
                });
            }
            else {
                $('#documents').val('');
                //$('#profile-photo').val('');             
            }
            //this code is executed when all ajax calls are done
        });

    }
}

$('#Resumedocuments').change(function () {
    doctorresumeupload();
});

function doctorresumeupload() {
    var uploadpath = $('#Resumedocuments').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (fileExtension == "pdf" || fileExtension == "doc" || fileExtension == "PDF" || fileExtension == "DOC") {
    }
    else {
        $('#PResumedocuments').show();
        $('#PResumedocuments').fadeOut(8000);
        $('#PResumedocuments').html('Please upload file having extensions .pdf/.doc only.');
        $('#Resumedocuments').val('');
        $('#Resumedocuments').focus();
        return false;
    }
    if (window.FormData !== undefined) {
        var fileUpload = $("#Resumedocuments").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();
        for (var i = 0; i < files.length; i++) {
            fileData.append(files[i].name, files[i]);
        }
        fileData.append('username', 'Manas');
        $.ajax({
            url: '/DoctorProfile/UploadResumeFiles',
            type: "POST",
            contentType: false,
            processData: false,
            data: fileData,
            success: function (result) {
                $.each(result, function (i, emp) {
                    $('#profile-resume').empty();
                    $('#profile-resume').show();
                    var d, n, drreimage;
                    d = new Date();
                    n = d.getTime();
                    drreimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Resume/' + emp + '?' + n;
                    $('#profile-photo').append('<a href="' + drreimage + '" target="_blank">Resume</a>');
                    $('#drresume').val(emp);
                });
            }
        });
    }
}
$('#signdocuments').change(function () {
    signnimageupload();
});

function signnimageupload() {
    var uploadpath = $('#signdocuments').val();
    var fileExtension = uploadpath.substring(uploadpath.lastIndexOf(".") + 1, uploadpath.length);
    if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "JPG" || fileExtension == "JPEG") {
    }
    else {
        $('#Pdocuments').show();
        $('#Pdocuments').fadeOut(8000);
        $('#Pdocuments').html('Please upload file having extensions .jpeg only.');
        $('#documents').val('');
        $('#documents').focus();
        return false;
    }
    if (window.FormData !== undefined) {
        var fileUpload = $("#signdocuments").get(0);
        var files = fileUpload.files;
        var fileData = new FormData();

        $.when(checkimagedimensionvalidation(fileUpload.files[0], 1024, 512)).
        done(function (Results) {
            if (Results == 'Success') {
                for (var i = 0; i < files.length; i++) {
                    fileData.append(files[i].name, files[i]);
                }
                fileData.append('username', 'Manas');
                $.ajax({
                    url: '/DoctorProfile/UploadSignFiles',
                    type: "POST",
                    contentType: false,
                    processData: false,
                    data: fileData,
                    success: function (result) {
                        $.each(result, function (i, emp) {
                            $('#Sign-photo').empty();
                            $('#Sign-photo').show();
                            var d, n, drloimage;
                            d = new Date();
                            n = d.getTime();
                            drloimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Sign/' + emp + '?' + n;
                            $('#Sign-photo').append('<a href="' + drloimage + '" target="_blank">Image</a>');
                            $('#drsign').val(emp);
                            //var drphotoSrc = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + "/Photos/" + emp;
                            //$('#ProfilePhotoTop,#ProfilePhoto').attr('src', drphotoSrc);
                        });
                    }
                });
            }
            else {
                $('#signdocuments').val('');
                //$('#profile-photo').val('');             
            }
            //this code is executed when all ajax calls are done
        });

    }
}


function DoctorCreation() {
    debugger;
    var dat_birth = $("#txtdob").val();
    dat_birth = dat_birth.split("/");
    dat_birth = dat_birth[2] + "-" + dat_birth[1] + "-" + dat_birth[0];
    var DoctorName = $("#txtDoctorName").val();
    var Speciality = $("#SelSpecialty").val();
    var NPI = $("#txtNPI").val();
    var mbl_consultation = $("#mbl_cons").val();
    var Qualification = $("#SelQualification").val();
    var DateofBirth = dat_birth
    var Age = $("#txtAge").val();
    var Gender = $("#ddlGender").val();
    var photo = $("#Hdn_profile_image").val();
    var sign = $("#Hdn_sign_image").val();
    var radio = $("input[name='chkPassPort']:checked").val();
    var txtLine1 = $('#txtLine1').val();
    var txtLine2 = $('#txtLine2').val();
    var Place = $('#txtPlace').val();
    var txtCountry = $('#txtCountry').val();
    var txtState = $('#txtState').val();
    var txtCity = $('#txtCity').val();
    var pin_code = $('#textpin_code').val();
    var department_id = $('#Department').val().toString();
    var subdepartment_id = $('#SubDepartemnt').val().toString();
    var branchid = $('#branch').val();

    var role = $('#SelRole').val() == "" || $('#SelRole').val() == null ? $('#hiddenRole_id').val() : $('#SelRole').val();
    var service_provider_id = $('#SelProvider').val();
    var free_visit = $('#txtfree').val();
    var a = "", drresume, drimage, drsign;

    if ($.trim(branchid) == "" || $.trim(branchid) == "0") {
        $('#PSelectDoctorbranch').show();
        $('#PSelectDoctorbranch').fadeOut(8000);
        $('#PSelectDoctorbranch').html('Select Branch');
        $('#branch').val('');
        $('#branch').focus();
        return false;
    }

    if ($.trim(role) == "" || $.trim(role) == "0") {
        $('#PSelRole').show();
        $('#PSelRole').fadeOut(8000);
        $('#PSelRole').html('Select Role');
        $('#SelRole').val('');
        $('#SelRole').focus();
        return false;
    }

    if ($.trim($('#Department').val()) == "" || $.trim($('#Department').val()) == "0") {
        $('#PDepartment').show();
        $('#PDepartment').fadeOut(8000);
        $('#PDepartment').html('Select Department');
        $('#Department').val('');
        $('#Department').focus();
        return false;
    }
    if ($.trim($('#SubDepartemnt').val()) == "" || $.trim($('#SubDepartemnt').val()) == "0") {
        $('#PSubDepartemnt').show();
        $('#PSubDepartemnt').fadeOut(8000);
        $('#PSubDepartemnt').html('Select SubDepartment');
        $('#SubDepartemnt').val('');
        $('#SubDepartemnt').focus();
        return false;
    }

    if ($.trim($('#SelRole').val()) == 1) {
        if ($('#doctor_type').val() == 0 || $('#doctor_type').val() == null) {
            $('#Pdoctor_type').show();
            $('#Pdoctor_type').fadeOut(8000);
            $('#Pdoctor_type').html('Select Doctor Type');
            $('#doctor_type').val(0);
            $('#doctor_type').focus();
            return false;
        }
    }

    if ($.trim($('#ddSalutation').val()) == "0") {
        $('#PddSalutation').show();
        $('#PddSalutation').fadeOut(8000);
        $('#PddSalutation').html('Select Salutation');
        $('#ddSalutation').val('0');
        $('#ddSalutation').focus();
        return false;
    }

    if ($.trim(DoctorName) == "") {
        $('#PtxtDoctorName').show();
        $('#PtxtDoctorName').fadeOut(8000);
        $('#PtxtDoctorName').html('Enter Name');
        $('#txtDoctorName').val('');
        $('#txtDoctorName').focus();
        return false;
    }
    if ($.trim(DoctorName).length < "2") {
        $('#PtxtDoctorName').show();
        $('#PtxtDoctorName').fadeOut(8000);
        $('#PtxtDoctorName').html('Enter Name minimum of two Charater');
        $('#txtDoctorName').val('');
        $('#txtDoctorName').focus();
        return false;
    }

    if ($.trim($("#txtdob").val()) == "") {
        $('#Ptxtdob').show();
        $('#Ptxtdob').fadeOut(8000);
        $('#Ptxtdob').html('Select date of Birth');
        $('#txtdob').val('');
        $('#txtdob').focus();
        return false;
    }
    if ($.trim(Age) == "") {
        $('#PtxtAge').show();
        $('#PtxtAge').fadeOut(8000);
        $('#PtxtAge').html('Enter the Age');
        $('#txtAge').val('');
        $('#txtAge').focus();
        return false;
    }
    if ($('#ddlGender').val() == "0") {
        $('#PddlGender').show();
        $('#PddlGender').fadeOut(8000);
        $('#PddlGender').html('Select Gender');
        $('#ddlGender').focus();
        return false;
    }

    if ($.trim($('#SelDoctorDesignation').val()) == "" || $.trim($('#SelDoctorDesignation').val()) == "0") {
        $('#PSelectDoctorDesignation').show();
        $('#PSelectDoctorDesignation').fadeOut(8000);
        $('#PSelectDoctorDesignation').html('Select Designation');
        $('#SelDoctorDesignation').val('');
        $('#SelDoctorDesignation').focus();
        return false;
    }

    if ($.trim(role) == 1) {
        if ($.trim(Qualification) == "") {
            $('#PSelQualification').show();
            $('#PSelQualification').fadeOut(8000);
            $('#PSelQualification').html('Select Qualification');
            $('#SelQualification').val('');
            $('#SelQualification').focus();
            return false;
        }
    }


    if (parseInt(role) == 1) {
        if (Speciality == "" || Speciality == "394539006" || Speciality == 0) {
            $('#PSelSpecialty').show();
            $('#PSelSpecialty').fadeOut(8000);
            $('#PSelSpecialty').html('Select Specality');
            $('#SelSpecialty :selected').val('1');
            $('#SelSpecialty').focus();
            return false;
        }

        if ($.trim(NPI) == "") {
            $('#PtxtNPI').show();
            $('#PtxtNPI').fadeOut(8000);
            $('#PtxtNPI').html('National Provider Identifier (NPI)');
            $('#txtNPI').val('');
            $('#txtNPI').focus();
            return false;
        }

        if ($('#doctor_type').val() != 2) {
            if ($('#txtfree').val() == '') {
                $('#Ptxtfree').show();
                $('#Ptxtfree').fadeOut(8000);
                $('#Ptxtfree').html('Enter the free visit');
                $('#txtfree').val('');
                $('#txtfree').focus();
                return false;
            }

            if ($('#mbl_cons').val() == '') {
                $('#Pmbl_cons').show();
                $('#Pmbl_cons').fadeOut(8000);
                $('#Pmbl_cons').html('Select Mobile Consultation');
                $('#mbl_cons').val('');
                $('#mbl_cons').focus();
                return false;
            }
        }
    }

    if ($.trim(txtLine1) == "") {
        $('#PtxtLine1').show();
        $('#PtxtLine1').fadeOut(8000);
        $('#PtxtLine1').html('Enter the Line1 Address');
        $('#txtLine1').val('');
        $('#txtLine1').focus();
        return false;
    }
    if ($.trim(txtLine2) == "") {
        $('#PtxtLine2').show();
        $('#PtxtLine2').fadeOut(8000);
        $('#PtxtLine2').html('Enter the Line2');
        $('#txtLine2').val('');
        $('#txtLine2').focus();
        return false;
    }
    if ($.trim(Place) == "") {
        $('#PtxtPlace').show();
        $('#PtxtPlace').fadeOut(8000);
        $('#PtxtPlace').html('Enter the Place');
        $('#txtPlace').val('');
        $('#txtPlace').focus();
        return false;
    }
    if ($('#txtCountry').val() == "" || $('#txtCountry').val() == "0") {
        $('#PtxtCountry').show();
        $('#PtxtCountry').fadeOut(8000);
        $('#PtxtCountry').html('Select The Country');
        $('#txtCountry').val('');
        $('#txtCountry').focus();
        return false;
    }
    if ($('#txtState').val() == "" || $('#txtState').val() == "0") {
        $('#PtxtState').show();
        $('#PtxtState').fadeOut(8000);
        $('#PtxtState').html('Select The state');
        $('#txtState').val('');
        $('#txtState').focus();
        return false;
    }
    if ($('#txtCity').val() == "" || $('#txtCity').val() == "0") {
        $('#PtxtCity').show();
        $('#PtxtCity').fadeOut(8000);
        $('#PtxtCity').html('Select The City');
        $('#txtCity').val('');
        $('#txtCity').focus();
        return false;
    }

    if ($('#textpin_code').val() == "" || $('#textpin_code').val() == "0") {
        $('#Ptxtpin_code').show();
        $('#Ptxtpin_code').fadeOut(8000);
        $('#Ptxtpin_code').html('Enter the Pin Code');
        $('#textpin_code').val('');
        $('#textpin_code').focus();
        return false;
    }
    if ($('#txtPMobileNumber').val() == "") {
        $('#PtxtPMobileNumber').show();
        $('#PtxtPMobileNumber').fadeOut(8000);
        $('#PtxtPMobileNumber').html('Enter Mobile Number');
        $('#txtPMobileNumber').val('');
        $('#txtPMobileNumber').focus();
        return false;
    }
    //if ($('#txtPMobileNumber').val() != "" && $('#txtPMobileNumber').val().length != "10") {
    //    $('#PtxtPMobileNumber').show();
    //    $('#PtxtPMobileNumber').fadeOut(8000);
    //    $('#PtxtPMobileNumber').html('Invaild Mobile Number');
    //    $('#txtPMobileNumber').val('');
    //    $('#txtPMobileNumber').focus();
    //    return false;
    //}
    if ($('#txtPMobileNumber2').val() != "" && $('#txtPMobileNumber2').val().length != "10") {
        $('#PtxtPMobileNumber2').show();
        $('#PtxtPMobileNumber2').fadeOut(8000);
        $('#PtxtPMobileNumber2').html('Invaild Mobile Number');
        $('#txtPMobileNumber2').val('');
        $('#txtPMobileNumber2').focus();
        return false;
    }
    //if ($('#txtPEmailID').val() == "") {
    //    $('#PtxtPEmailID').show();
    //    $('#PtxtPEmailID').fadeOut(8000);
    //    $('#PtxtPEmailID').html('Enter the EmailId');
    //    $('#txtPEmailID').val('');
    //    $('#txtPEmailID').focus();
    //    return false;
    //}
    if ($("#chkYes").is(":checked")) {
        if ($.trim($('#txtTotalExp').val()) == "" || $('#tblExperience  >tbody >tr').length == 0) {
            $('#PtxtConcern').show();
            $('#PtxtConcern').fadeOut(8000);
            $('#PtxtConcern').html('Fill Experience Detail');
            $('#txtConcern').val('');
            $('#txtConcern').focus();
            return false;
        }
    }
    //if (radio != "on") {
    //    //if ($.trim(tot_exp) == "") {
    //    $('#PtxtConcern').show();
    //    $('#PtxtConcern').fadeOut(8000);
    //    $('#PtxtConcern').html('Fill Experience Detail');
    //    $('#txtConcern').val('');
    //    $('#txtConcern').focus();
    //    return false;
    //    // }
    //}

    if ($.trim($('#user_code').val()) == "") {
        $('#Puser_code').show();
        $('#Puser_code').fadeOut(8000);
        $('#Puser_code').html('Enter the Employee ID');
        $('#user_code').val('');
        $('#user_code').focus();
        return false;
    }

    if ($.trim($('#password').val()) == "") {
        $('#Ppassword').show();
        $('#Ppassword').fadeOut(8000);
        $('#Ppassword').html('Enter Password');
        $('#password').val('');
        $('#password').focus();
        return false;
    }

    var filename = $('#myHiddenVar').val();
    var proffilename = " ", resfilename = " ";
    var profleupload = $('#documents').val();
    drresume = $("#drresume").val();
    drimage = $("#drphoto").val();
    drsign = $("#drsign").val();
    var DoctorId = '', Dml_Indicator = '';

    if ($('#DoctorUser_Id').val() != '' && $('#DoctorUser_Id').val() != null) {
        DoctorId = $('#DoctorUser_Id').val();
        Dml_Indicator = 'UU';
    }
    else {
        DoctorId = $('#myHiddenVar').val();
        Dml_Indicator = 'U';
    }

    if (parseInt($('#hiddenRole_id').val()) == 1) {
        Dml_Indicator = 'DEDIT';
    }

    if ($("#txtDoctorName,#SelSpecialty,#txtNPI,#SelQualification,#txtdob,#txtAge,#ddlGender,#txtLine1,#txtLine2,#txtPlace,#txtCountry,#txtState,#txtCity").val().length > 0) {
        var DoctorUpdationData = {
            doctor_id: DoctorId,
            doctor_name: $("#txtDoctorName").val(),
            speciality: $("#SelSpecialty").val(),
            national_provider_id: $("#txtNPI").val(),
            mobile_consultation: $('#mbl_cons').val(),
            qualification: a,
            dob: DateofBirth,
            age: $("#txtAge").val(),
            gender: $("#ddlGender").val(),
            photo: drimage,
            signature: drsign,
            mobile_number_one: $("#txtPMobileNumber").val(),
            mobile_number_two: $("#txtPMobileNumber2").val(),
            email_id: $("#txtPEmailID").val(),
            marital_status: $("#ddlMart").val(),
            resume: drresume,
            total_experience: $("#txtTotalExp").val(),
            Dml_Indicator: Dml_Indicator,
            hospital_id: $('#HospitalId').val(),
            doctor_designation: $('#SelDoctorDesignation').val(),
            service: $('#d_Service').val(),
            department: department_id,
            subdepartment: subdepartment_id,
            branch_id: branchid,
            user_role_id: role,
            service_provider_id: service_provider_id,
            free_visit: free_visit,
            display_designation: $('#txtDisplayDesignation').val(),
            salutation: $('#ddSalutation').val(),
            user_created_by: $('#hiddenUser_id').val(),
            user_id: $('#user_code').val(),
            password: $('#password').val(),
            user_floorid: $('#ddUser_Floor').val(),
            line_one: $("#txtLine1").val(),
            line_two: $("#txtLine2").val(),
            town_village: $("#txtPlace").val(),
            country: $("#txtCountry").val(),
            state: $("#txtState").val(),
            city: $("#txtCity").val(),
            pin_code: $('#textpin_code').val(),
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/DoctorProfile/DoctorUpdation',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorUpdationData),
            success: function (data) {
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    var data = data.data;
                    multi_branch(data[0].doctoruserid)
                    DoctorAddressValuesinsert(DoctorId, data[0].role_id);
                    if (jQuery.isEmptyObject((data))) {
                    }
                    else {
                        if (data.length > 0) {

                            for (var i = 0; i < data.length; i++) {
                            }
                            var role_id = data[0].role_id;
                            var d = '', Dml_Indicator = '', indicator_id = '';
                            if (data[0].role_id == 1) {

                                d = "D";
                                Dml_Indicator = "U";
                                indicator_id = 'I'
                            }
                            else {
                                doctor_ID = data[0].user_id;
                                d = "DD"
                                Dml_Indicator = "UU"
                                indicator_id = 'UI'
                            }
                            var q = "";
                            //var d = "D";
                            AddExperience(DoctorId, Dml_Indicator);
                            var deletequalification = {
                                doctor_id: DoctorId,
                                qualification_id: q,
                                Dml_Indicator: d
                            }
                            $.ajax({
                                async: true,
                                type: 'POST',
                                url: '/DoctorProfile/Qualification',
                                contentType: 'application/json; charset=UTF-8',
                                data: JSON.stringify(deletequalification),
                                success: function (data) {
                                    var data = JSON.parse(data);
                                    debugger;
                                    if (data.msg == "Success") {
                                        var data = data.data;
                                        var qua = $("#SelQualification").val();
                                        for (var i = 0; i < qua.length; i++) {
                                            //alert(i);
                                            var Dml_Indicator = "I";
                                            var insertqualification = {
                                                doctor_id: DoctorId,
                                                qualification_id: qua[i],
                                                Dml_Indicator: indicator_id
                                            }
                                            $.ajax({
                                                async: true,
                                                type: 'POST',
                                                url: '/DoctorProfile/Qualification',
                                                contentType: 'application/json; charset=UTF-8',
                                                data: JSON.stringify(insertqualification),
                                                success: function (data) {
                                                }
                                            });
                                        }
                                        if ($('#hiddenRole_id').val() == 1) {
                                            toastr.success('User Updated Successfully');
                                            setTimeout(function () {
                                                window.location.href = '/Dashboard';
                                            }, 2000);

                                        }
                                        else {
                                            toastr.success('User Updated Successfully');
                                            setTimeout(function () {
                                                window.location.href = '/previllages';
                                            }, 2000);

                                        }
                                    }
                                    else {
                                        console.log(data);
                                    }
                                },

                                error: function () {
                                }
                            });
                        }
                    }
                }
                else {
                    console.log(data);
                }
            }
        });
    }
    else { }
}

function DoctorDefaultValues() {
    debugger;
    var DoctorId = $('#myHiddenVar').val();
    var DoctorData = {
        doctor_id: DoctorId
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorRecords',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                console.log(data);
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            debugger;
                            var profilephoto = data[i].photo;
                            var signature = data[i].signature;
                            $('#Hdn_profile_image').val(data[i].photo);
                            var resumefile = data[i].resume;
                            $('#Hdn_redume').val(data[i].resume);
                            var filepath = $('#hdnfilepath1').val();
                            var photo = $('#hdnPhoto').val();
                            var resume = $('#hdnResume').val();
                            $('#user_role_id').val(1);
                            var d, n, drphimage, drrrfile;
                            d = new Date();
                            n = d.getTime();
                            if (profilephoto == null || $.trim(profilephoto) == "" || $.trim(profilephoto).length < 1) {
                            }
                            else {
                                $('#profile-photo').show();
                                $('#profile-photo').empty();
                                drphimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Photos/' + profilephoto + '?' + n;
                                $('#profile-photo').append('<a href="' + drphimage + '" target="_blank">Image</a>');
                                $('#drphoto').val(profilephoto);
                            }
                            if (signature == null || $.trim(signature) == "" || $.trim(signature).length < 1) {
                            }
                            else {

                                $('#Sign-photo').empty();
                                $('#Sign-photo').show();
                                drloimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Sign/' + signature + '?' + n;
                                $('#Sign-photo').append('<a href="' + drloimage + '" target="_blank">Image</a>');
                                $('#drsign').val(signature);
                            }
                            if (resumefile == null || $.trim(resumefile) == " " || $.trim(resumefile).length < 1) {
                            }
                            else {
                                $('#profile-resume').show();
                                $('#profile-resume').empty();
                                drrrfile = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Resume/' + resumefile + '?' + n;
                                $('#profile-resume').append('<a href="' + drrrfile + '" target="_blank">Resume</a>');
                                $('#drresume').val(resumefile);
                            }

                            $("#txtAge").val(data[i].age);
                            $("#txtdob").val(data[i].dob);
                            //}
                            $("#txtdoctorid").val(data[i].doctor_id);
                            $("#txtDoctorName").val(data[i].doctor_name);
                            //$('#user_code').attr('disabled', true);
                            // $('#password').attr('disabled', true);
                            $('#user_code').val(data[i].user_code);
                            $('#password').val(data[i].doctor_user_password);
                            load_salutation(data[i].salutation);
                            //$("#ddSalutation").val(data[i].salutation);
                            $("#txtNPI").val(data[i].national_provider_id);
                            $('#SelSpecialty').val(data[i].speciality);
                            $('#husermultibranch').val(data[i].usermultibranchid);
                            $('#ddUser_Floor').val(data[i].user_floorid);

                            if ($('#doctor_type').val() != 2) {

                                $('#divmbl_cons').show();
                                $('#divtxtfree').show();
                                $('#txtfree').val(data[i].review_free_visit_days);

                                if (data[i].mobile_consultation == 1) {
                                    $('a[data-toggle=mbl_cons][data-title="1"]').removeClass('not-active');
                                    $('a[data-toggle=mbl_cons][data-title="1"]').addClass('active');
                                    $('a[data-toggle=mbl_cons][data-title="2"]').removeClass('active');
                                    $('a[data-toggle=mbl_cons][data-title="2"]').addClass('not-active');
                                }
                                else if (data[i].mobile_consultation == 0) {
                                    $('a[data-toggle=mbl_cons][data-title="1"]').removeClass('active');
                                    $('a[data-toggle=mbl_cons][data-title="1"]').addClass('not-active');
                                    $('a[data-toggle=mbl_cons][data-title="0"]').removeClass('not-active');
                                    $('a[data-toggle=mbl_cons][data-title="0"]').addClass('active');
                                }
                                $('#mbl_cons').val(data[i].mobile_consultation);
                            }
                            else {
                                $('#divmbl_cons').hide();
                                $('#divtxtfree').hide();
                            }

                            $("#ddlGender").val(data[i].gender);
                            $("#ddlMart").val(data[i].marital_status);
                            $("#txtPMobileNumber").val(data[i].mobile_number_one);
                            $("#div_NPI").removeClass('hide');
                            //$('#divtxtfree').show();
                            //$('#txtfree').val(data[i].review_free_visit_days);
                            if ($('#myHiddenVar1').val() != '' && $('#myHiddenVar1').val() != null) {
                                $('#txtDisplayDesignation').val(data[i].display_designation);
                                debugger;
                                LoadServiceProvider(data[i].multibranch, data[i].hospital_id, data[i].service_provider_id)
                                if (parseInt(data[i].lab_user) > 0) {
                                    $('#SelProvider').attr('disabled', true);
                                    $('#PSelProvider').html('User Assigned as Service Provider');
                                }
                                if ($("#hiddenRole_id").val() == 2 || $("#hiddenRole_id").val() == 6) {
                                    $('#SelProvider').attr('disabled', false);
                                }
                                else {
                                    $('#SelProvider').attr('disabled', true);
                                }
                                $('#d_Service').val(data[i].service);
                                $('#txtPMobileNumber').attr('disabled', true);
                                $('#txtPEmailID').attr('disabled', true);
                            }
                            if (data[i].mobile_number_two == 0) {
                                $("#txtPMobileNumber2").val('');
                            }
                            else {
                                $("#txtPMobileNumber2").val(data[i].mobile_number_two);
                            }
                            $("#txtPEmailID").val(data[i].email_id);
                            // if ($("#hiddenRole_id").val() == 2) {
                            branch(data[i].multibranch)
                            //  }

                            LoadRole(data[i].doctor_user_role_id)
                            department(data[i].department, data[i].subdepartment);
                            Designation(data[i].doctor_user_designation);
                            if (parseFloat(data[i].total_experience) > 0) {

                                $('#chkYes').attr("checked", "checked");

                                if ($("#chkYes").is(":checked")) {
                                    $("#dvPassport").show();
                                } else {
                                    $("#dvPassport").hide();
                                }
                            }
                            else {
                                $('#chkNo').attr("checked", "checked");
                            }
                        }
                    }
                }
            }
            else {
                console.log(data);
            }
        }
    });
}

function DoctorAddressValues() {
    debugger;


    DoctorId = $('#myHiddenVar').val();

    var DoctorAddressData = {
        doctor_id: DoctorId,
        Dml_Indicator: 'DA'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorAddress',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorAddressData),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    if (data1.length > 0) {
                        $("#txtaddressid1").val(data1[0].doctor_address_id);
                        var is_permanent_address = data1[0].is_permanent_address;
                        if (is_permanent_address == 1) {
                            $('#ChkID').prop('checked', 'checked')
                        }
                        $("#txtLine1").val(data1[0].line_one);
                        $("#txtLine2").val(data1[0].line_two);
                        $("#txtPlace").val(data1[0].town_village);
                        $('#textpin_code').val(data1[0].pincode)
                        var country_id = data1[0].country;
                        var state_id = data1[0].state;
                        var city_id = data1[0].city
                        if (parseFloat(data1[0].is_permanent_address) > 0) {
                            $("#chkAdd").attr("checked", "checked");
                        }
                        else {
                            $("#chkAdd").attr("checked", "");
                        }
                        if (is_permanent_address == 1) {
                            $("#txtpLine1").val(data1[0].line_one).attr('readonly', true);
                            $("#txtpLine2").val(data1[0].line_two).attr('readonly', true);
                            $("#txtPPlace").val(data1[0].town_village).attr('readonly', true);
                            $('#textPpin_code').val(data1[0].pincode).attr('readonly', true);
                            $("#txtaddressid2").val(data1[1].doctor_address_id);
                            var PCountry = data1[0].country;
                            var PState = data1[0].state;
                            var PCity = data1[0].city;
                            Pgetstate(PCountry, PState);
                            Pgetcity(PState, PCity);
                        }
                        else {
                            $("#txtpLine1").val(data1[1].line_one).attr('readonly', true);
                            $("#txtpLine2").val(data1[1].line_two).attr('readonly', true);
                            $("#txtPPlace").val(data1[1].town_village).attr('readonly', true);
                            $('#textPpin_code').val(data1[1].pincode).attr('readonly', true);
                            $("#txtaddressid2").val(data1[1].doctor_address_id);
                            var PCountry = data1[1].country;
                            var PState = data1[1].state;
                            var PCity = data1[1].city;
                            Pgetstate(PCountry, PState);
                            Pgetcity(PState, PCity);
                        }
                        getstate(country_id, state_id);
                        getcity(state_id, city_id);
                    }
                    else {
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
    DoctorDefaultValues();
}



function AddSpecialty() {
    if ($.trim($('#txtSpecialtyName').val()) == ('')) {
        $('#PtxtSpecialtyName').show();
        $('#PtxtSpecialtyName').fadeOut(8000);
        $('#PtxtSpecialtyName').html('Enter Specialty ');
        $('#txtSpecialtyName').val('');
        $('#txtSpecialtyName').focus();
    }
    if ($.trim($('#txtSpecialtyName').val()).length > 0) {
        var DoctorspecialityData = {
            doctor_speciality_name: $.trim($('#txtSpecialtyName').val()),
            Dml_Indicator: "I"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/DoctorProfile/DoctorSpeciality',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorspecialityData),
            success: function (data) {
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    var data = data.data;
                    if (jQuery.isEmptyObject((data))) {
                    }
                    else {
                        if (data.length > 0) {
                            for (var i = 0; i < data.length; i++) {
                                $('#PtxtSpecialtyName').show();
                                $('#PtxtSpecialtyName').fadeOut(8000);
                                $('#PtxtSpecialtyName').html(data[i].StringMsg);
                                $('#txtSpecialtyName').val('');
                                $('#txtSpecialtyName').focus();
                                LoadSpecialty()
                            }
                        }
                    }
                }
                else {
                    console.log(data);
                }
            }
        });
    }
}

function LoadSpecialty() {
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorSpeciality',
        contentType: 'application/json; charset=UTF-8',
        success: function (data) {
            var data = JSON.parse(data)
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var tr = '';
                    var SelSpecialty = '', default_val = '';
                    $('#tblSpecialty tbody').empty();
                    $('#SelSpecialty').empty();
                    default_val = '<option value="0">--Select--</option>'
                    if (data.length > 0) {
                        var j = 0;
                        for (var i = 0; i < data.length; i++) {
                            j++;
                            SelSpecialty = SelSpecialty + '<option value="' + data[i].doctor_speciality_id + '">' + data[i].doctor_speciality_name + '</option>';
                            tr = tr + "<tr id=" + data[i].doctor_speciality_id + ">";
                            tr = tr + "<td>" + j + "</td>";
                            tr = tr + "<td style='display:none;'>" + data[i].doctor_speciality_id + "</td>";
                            tr = tr + " <td>" + data[i].doctor_speciality_name + "</td>";
                            tr = tr + "<td>";
                            tr = tr + "<div class='table_actions'>";
                            tr = tr + '<button class="delete btn btn-default  btn-sm btn-add" data-toggle="popover" title="Delete" onclick=ResourceDelete(\'' + data[i].doctor_speciality_id + '\');><i class="fa fa-trash" aria-hidden="true"></i></button>';
                            tr = tr + "</div>";
                            tr = tr + "</td>";
                            tr = tr + "</tr>";
                        }
                        $('#SelSpecialty').append(default_val + SelSpecialty);
                        $('#tblSpecialty').append(tr);
                    }
                }
                LoadQualification();
            }
            else {
                console.log(data);
            }
        }
    });
}

function ResourceDelete(url) {
    var DeleteResource = {
        doctor_speciality_id: url,
        Dml_Indicator: "D"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorSpeciality',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DeleteResource),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                $('#PtxtSpecialtyName').show();
                $('#PtxtSpecialtyName').fadeOut(8000);
                $('#PtxtSpecialtyName').html(data[0].StringMsg);
                $('#txtSpecialtyName').val('');
                $('#txtSpecialtyName').focus();
                LoadSpecialty();
            }
            else {
                console.log(data);
            }
        }
    });
}

function AddQualification() {
    if ($.trim($('#txtQualification').val()) == ('')) {
        $('#PtxtQualification').show();
        $('#PtxtQualification').fadeOut(8000);
        $('#PtxtQualification').html('Enter qualification');
        $('#txtQualification').val('');
        $('#txtQualification').focus();
    }
    if ($.trim($('#txtQualification').val()).length > 0) {
        var DoctorQualificationData = {
            doctor_qualification_name: $.trim($('#txtQualification').val()),
            Dml_Indicator: "I"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/DoctorProfile/DoctorQualification',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorQualificationData),
            success: function (data) {
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    var data = data.data;
                    if (jQuery.isEmptyObject((data))) {
                    }
                    else {
                        if (data.length > 0) {
                            for (var i = 0; i < data.length; i++) {
                                $('#PtxtQualification').show();
                                $('#PtxtQualification').fadeOut(8000);
                                $('#PtxtQualification').html(data[i].StringMsg);
                                $('#txtQualification').val('');
                                $('#txtQualification').focus();
                                LoadQualification();
                            }
                        }
                    }
                }
                else {
                    console.log(data);
                }
            }
        });
    }
}

function LoadQualification() {
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorQualification',
        contentType: 'application/json; charset=UTF-8',
        success: function (data1) {
            var data = JSON.parse(data1);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var tr = '';
                    var SelQualification = '';
                    $('#SelQualification').empty();
                    $('#tblQualification tbody').empty();
                    var j = 0;
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            j++;
                            SelQualification = SelQualification + '<option value="' + data[i].qualification_id + '">' + data[i].qualification_name + '</option>';
                            tr = tr + "<tr id=" + data[i].qualification_id + ">";
                            tr = tr + "<td>" + j + "</td>";
                            tr = tr + "<td style='display:none;'>" + data[i].qualification_id + "</td>";
                            tr = tr + " <td>" + data[i].qualification_name + "</td>";
                            tr = tr + "<td>";
                            tr = tr + "<div class='table_actions'>";
                            tr = tr + '<button class="delete btn btn-default  btn-sm btn-add" data-toggle="popover" title="Delete" onclick="ResourceQualificationDelete(\'' + data[i].qualification_id + '\')";><i class="fa fa-trash" aria-hidden="true"></i></button>';
                            tr = tr + "</div>";
                            tr = tr + "</td>";
                            tr = tr + "</tr>";
                        }
                        $('#SelQualification').append(SelQualification);
                        $('#tblQualification').append(tr);
                        var DoctorId = '', Dml_Indicator = '';

                        if ($('#DoctorUser_Id').val() != '' && $('#DoctorUser_Id').val() != null) {
                            DoctorId = $('#DoctorUser_Id').val();
                            Dml_Indicator = 'SS';
                        }
                        else {
                            DoctorId = $('#myHiddenVar').val();
                            Dml_Indicator = 'S';
                        }
                        var q = "";
                        var deletequalification = {
                            doctor_id: DoctorId,
                            qualification_id: q,
                            Dml_Indicator: Dml_Indicator
                        }
                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/DoctorProfile/GetQualification',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(deletequalification),
                            success: function (data) {
                                var data1 = JSON.parse(data);
                                debugger;
                                console.log(data1);
                                if (data1.msg == "Success") {
                                    var data1 = data1.data;
                                    var abc = "", s = [];
                                    $.each(data1, function (i, emp) {
                                        abc = abc + '<li class="select2-selection__choice" title="' + emp.qualification_name + '"><span class="select2-selection__choice__remove" role="presentation">×</span>' + emp.qualification_name + '</li>';
                                        document.getElementById("SelQualification").value = emp.qualification_id;
                                        $('option [value=' + emp.qualification_id + ']').attr("selected", "select");
                                        s.push(emp.qualification_id)
                                    });
                                    $('#SelQualification').val(s).trigger('change');
                                    //$('ul.select2-selection__rendered').append(abc);
                                }
                                else {
                                    console.log(data1);
                                }
                            }
                        });
                    }
                }
                LoadExperience();
            }
            else {
                console.log(data);
            }
        }
    });
}

function ResourceQualificationDelete(url) {
    var DeleteResource = {
        doctor_qualification_id: url,
        Dml_Indicator: "D"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorQualification',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DeleteResource),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (data[0].StringMsg == 'already') {
                    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>OK</button>", 'Users already in this qualificaion.Cannot delete the qualification',
                       {
                           closeButton: true,
                           progressBar: true,
                           allowHtml: true,
                           timeOut: 20000000,

                           onShown: function (toast) {
                               $("#confirmationRevertYes").click(function () {
                               })
                           }
                       })
                }
                else {
                    $('#PtxtQualification').show();
                    $('#PtxtQualification').fadeOut(8000);
                    $('#PtxtQualification').html(data[0].StringMsg);
                    $('#txtQualification').val('');
                    $('#txtQualification').focus();
                    LoadQualification();
                }
            }
            else {
                console.log(data);
            }
        }
    });
}

//Doctor Experiance will be loaded
function LoadExperience() {
    debugger;
    var DoctorId = '', Dml_Indicator = '';

    if ($('#DoctorUser_Id').val() != '' && $('#DoctorUser_Id').val() != null) {
        DoctorId = $('#DoctorUser_Id').val();
        Dml_Indicator = 'SS';
    }
    else {
        DoctorId = $('#myHiddenVar').val();
        Dml_Indicator = 'S';
    }
    var LoadExperienceData = {
        doctor_id: DoctorId,
        Dml_Indicator: Dml_Indicator
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorExperience',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(LoadExperienceData),
        success: function (data) {
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                    $('#tblExperience tbody').empty();
                    $('#chkNo').prop('checked', true);
                    var Norecord = '<div class="img-center"><img src="/Content/img/no_record.png" class="img-resposive no_record_img" style="height:65px; width:72px; border-radius:0px;"><br><div class="no_data_content">No data found.</div></div>';
                    $('#tblExperience').html(Norecord);
                    $('#txtTotalExp').val('');
                }
                else {
                    debugger;
                    var tr = '';
                    var TotalExp = 0;
                    var tot_row = 0;
                    var tot_sno = 0;
                    $('#tblExperience tbody').empty();
                    if (data.length > 0) {
                        if (data[0].StringMsg != "Error") {
                            debugger;
                            for (var i = 0; i < data.length; i++) {
                                $('#chkYes').prop('checked', true);
                                tot_sno++;
                                var sess_data_ser = data[i].employer_name + "," + data[i].experience_id + "," + data[i].experience;
                                sessionStorage.setItem(tot_sno, sess_data_ser);
                                tr = tr + "<tr id=" + tot_sno + " class=exp_" + tot_sno + ">";
                                tr = tr + "<td>" + data[i].employer_name + "</td>";
                                tr = tr + "<td style='display:none;'>" + data[i].experience_id + "</td>";
                                tr = tr + " <td>" + data[i].experience + "</td>";
                                tr = tr + "<td>";
                                tr = tr + "<div class='table_actions'>";
                                //tr = tr + '<a class="delete btn btn-default  btn-sm" onclick="Experience_Delete(\'' + data[i].experience_id + '\',' + tot_sno + '); return false;"href=\'' + data[i].experience_id + '\' ><i class="fa fa-trash" aria-hidden="true"></i></a>';
                                tr = tr + '<button class="delete btn btn-danger btn-xs btn-add" onclick="Experiencedelete(' + tot_sno + ')"><i class="fa fa-trash" aria-hidden="true"></i></button>'
                                tr = tr + "</div>";
                                tr = tr + "</td>";
                                tr = tr + "</tr>";
                                TotalExp = parseFloat(TotalExp) + parseFloat(data[i].experience);
                                tot_row++;

                            }
                            $('#ExperienceTableRowCount').val(tot_row);
                            $('#Experience_sno').val(tot_sno)
                            if (TotalExp == 0) {
                                TotalExp = '';
                            }
                            $('#txtTotalExp').val(TotalExp);
                            $('#tblExperience').append(tr);
                            $('#chkYes').click();
                        }
                        else {
                            //var Norecord = '<div class="img-center"><img src="/Content/img/no_record.png" class="img-resposive no_record_img" style="height:65px; width:72px; border-radius:0px;"><br><div class="no_data_content">No data found.</div></div>';
                            //$('#tblExperience tbody').html(Norecord);
                            //$('#txtTotalExp').val('');
                            //$('#chkNo').prop('checked', true);
                        }
                    }
                    else {
                        var Norecord = '<div class="img-center"><img src="/Content/img/no_record.png" class="img-resposive no_record_img" style="height:65px; width:72px; border-radius:0px;"><br><div class="no_data_content">No data found.</div></div>';
                        $('#tblExperience tbody').html(Norecord);
                        $('#txtTotalExp').val('');
                        $('#chkNo').prop('checked', true);
                    }
                }
                //LoadDesignation();
            }
            else {
                console.log(data);
            }
        }
    });
    $('.da-load-patsearch').addClass('hide');
}

//Deletes the Experience in DB Table
function Experience_Delete(url, id) {
    var indicator_id = '';
    if ($('#SelRole').val() == 1 || $('#hiddenRole_id').val() == 1) {
        indicator_id = 'D'
    }
    else {
        indicator_id = 'DD'
    }
    debugger;
    var DeleteExperience = new Array();
    var Experience_data = {};
    Experience_data.Dml_Indicator = indicator_id,
    Experience_data.doctor_experience_id = url
    console.log(DeleteExperience.push(Experience_data));


    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorExperience',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DeleteExperience),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                $('.exp_' + id).remove();
                var total_exp = 0;
                $("#tblExperience").children("tbody").children("tr").each(function (i) {
                    var $this = $(this);
                    var my_td = $this.children("td");
                    total_exp = parseInt(total_exp) + parseInt(my_td.eq(2).html());

                });
                if (total_exp == 0) {
                    total_exp = '';
                }
                $('#txtTotalExp').val(total_exp);
            }
            else {
                console.log(data);
            }
        }
    });
}


$('#ChkID').change(function () {
    debugger;
    var L1 = $('#txtLine1').val();
    var L2 = $('#txtLine2').val();
    var L3 = $('#txtPlace').val();
    var L4 = $('#txtCountry').val();
    var L5 = $('#txtState').val();
    var L6 = $('#txtCity').val();
    var L7 = $('#textpin_code').val();
    if ($(this).is(":checked")) {
        $('#txtpLine1').val(L1).attr('readonly', true);
        $('#txtpLine2').val(L2).attr('readonly', true);
        $('#txtPPlace').val(L3).attr('readonly', true);
        $('#textPpin_code').val(L7).attr('readonly', true);
        Pgetstate(L4, L5);
        Pgetcity(L5, L6);
        ifChecked();
    }
    else {
        $('#txtpLine1').val('').attr('readonly', false);
        $('#txtpLine2').val('').attr('readonly', false)
        $('#txtPPlace').val('').attr('readonly', false)
        $('#txtPState').val(PState).attr('disabled', false);
        $('#txtPCountry').val(PCountry).attr('disabled', false);
        $('#txtPCity').val(PCity).attr('disabled', false);
        $('#textPpin_code').val('').attr('readonly', false);
        $('#txtPCountry').val('0');
        $('#txtPState').val('0');
        $('#txtPCity').val('0');
    }
});


function ifChecked() {
    debugger;
    var state = {
        country_id: $("#txtCountry").val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetState',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            debugger;

            var data1 = JSON.parse(data);
            //if (data1.msg == "Success") {
            //    var data1 = data1.data;
            if (jQuery.isEmptyObject((data1))) {
            }
            else {
                var state;
                var default_opt = '<option value="0">--Select--</option>';
                $.each(data1, function (i, emp) {
                    state = state + '<option value="' + emp.id + '">' + emp.name + '</option>';
                });
                $('#txtPState').append(state);
                var state_id = $("#txtState").val();
                $('#txtPState').val(state_id).attr('disabled', true);
            }
            //}
            //else {
            //    console.log(data1);
            //}
        }
    });
    var state = {
        state_id: $("#txtState").val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCity',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data1 = JSON.parse(data);
            //if (data1.msg == "Success") {
            //    var data1 = data1.data;
            if (jQuery.isEmptyObject((data1))) {
            }
            else {
                var City;
                var default_opt = '<option value="0">--Select--</option>';
                $.each(data1, function (i, emp) {
                    City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                });
                $('#txtPCity').append(City);
                var City_id = $("#txtCity").val();
                $('#txtPCity').val(City_id).attr('disabled', true);
            }
            //}
            //else {
            //    console.log(data1);
            //}
        }
    });
}
function copy() {
    debugger;

    $("#txtLine1").val($("#txtpLine1").val());
    $("#txtLine2").val($("#txtpLine2").val());
    $("#txtPlace").val($("#txtPPlace").val());
    $("#txtCountry").val($("#txtPCountry").val());
    $("#txtState").val($("#txtPState").val());
    $("#txtCity").val($("#txtPCity").val());
    $("#textpin_code").val($("#textPpin_code").val());
}

$("#txtCountry").change(function () {
    debugger;
    var state = {
        country_id: $("#txtCountry").val(),
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var state;
                    $('#txtState').empty();
                    $('#txtCity').empty();
                    // var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        state = state + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtState').append(state);
                    $("#txtState").change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

$("#txtState").change(function () {
    var state = {
        state_id: $("#txtState").val(),
        Dml_Indicator: "CI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var City;
                    $('#txtCity').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtCity').append(default_opt + City);
                }
            }
            if (typeof callback === 'function') {
                callback();
            }
            else {
                console.log(data1);
            }
        }
    });
});


$("#txtPCountry").change(function () {
    var state = {
        country_id: $("#txtPCountry").val(),
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var state1;
                    $('#txtPState').empty();
                    $('#txtPCity').empty();
                    // var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        state1 = state1 + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtPState').append(state1);
                    $("#txtPState").change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

$("#txtPState").change(function () {
    var state = {
        state_id: $("#txtPState").val(),
        Dml_Indicator: "CI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(state),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var City;
                    $('#txtPCity').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        City = City + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtPCity').append(default_opt + City);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

function getstate(country_id, state_id) {
    debugger;
    var country = {
        country_id: country_id,
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Country;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtState').append(default_opt + Country);
                    $('#txtCountry').val(country_id);
                    $('#txtState').val(state_id);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function getcity(state_id, city_id) {
    debugger;
    var country = {
        state_id: state_id,
        Dml_Indicator: "CI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Country;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtCity').append(default_opt + Country);
                    $('#txtCity').val(city_id);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}


function Pgetstate(PCountry, PState) {
    debugger;
    var country = {
        country_id: PCountry,
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Country;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtPState').append(default_opt + Country);
                    $('#txtPState').val(PState).attr('disabled', true);
                    $('#txtPCountry').val(PCountry).attr('disabled', true);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function Pgetcity(PState, PCity) {
    debugger;
    var country = {
        state_id: PState,
        Dml_Indicator: "CI"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var Country;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtPCity').append(default_opt + Country);
                    $('#txtPCity').val(PCity).attr('disabled', true);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
//insert function
function loadcountry_I() {
    debugger;
    var country = {
        Dml_Indicator: "C"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetCountry',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(country),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var Country;
                    // var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        Country = Country + '<option value="' + emp.id + '">' + emp.name + '</option>';
                    });
                    $('#txtCountry').append(Country);
                    $('#txtPCountry').append(Country);
                    $("#txtCountry").change();
                    $("#txtPCountry").change();
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function LoadSpecialty_I() {
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorSpeciality',
        contentType: 'application/json; charset=UTF-8',
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var tr = '';
                    var SelSpecialty = '';
                    $('#tblSpecialty tbody').empty();
                    $('#SelSpecialty').empty();
                    default_val = '<option value="0">--Select--</option>'
                    if (data.length > 0) {
                        var j = 0;
                        for (var i = 0; i < data.length; i++) {
                            j++;
                            SelSpecialty = SelSpecialty + '<option value="' + data[i].doctor_speciality_id + '">' + data[i].doctor_speciality_name + '</option>';
                            tr = tr + "<tr id=" + data[i].doctor_speciality_id + ">";
                            tr = tr + "<td>" + j + "</td>";
                            tr = tr + "<td style='display:none;'>" + data[i].doctor_speciality_id + "</td>";
                            tr = tr + " <td>" + data[i].doctor_speciality_name + "</td>";
                            tr = tr + "<td>";
                            tr = tr + "<div class='table_actions'>";
                            tr = tr + '<button class="delete btn btn-default  btn-sm btn-add" data-toggle="popover" title="Delete" onclick=ResourceDelete(\'' + data[i].doctor_speciality_id + '\');><i class="fa fa-trash" aria-hidden="true"></i></button>';
                            tr = tr + "</div>";
                            tr = tr + "</td>";
                            tr = tr + "</tr>";
                        }
                        $('#SelSpecialty').append(default_val + SelSpecialty);
                        $('#tblSpecialty').append(tr);
                    }
                }
                LoadQualification_I();
            }
            else {
                console.log(data);
            }
        }
    });
}

function LoadQualification_I() {
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorQualification',
        contentType: 'application/json; charset=UTF-8',
        success: function (data1) {
            var data = JSON.parse(data1);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    var tr = '';
                    var SelQualification = '';
                    $('#SelQualification').empty();
                    $('#tblQualification tbody').empty();
                    var j = 0;
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            j++;
                            SelQualification = SelQualification + '<option value="' + data[i].qualification_id + '">' + data[i].qualification_name + '</option>';
                            tr = tr + "<tr id=" + data[i].qualification_id + ">";
                            tr = tr + "<td>" + j + "</td>";
                            tr = tr + "<td style='display:none;'>" + data[i].qualification_id + "</td>";
                            tr = tr + " <td>" + data[i].qualification_name + "</td>";
                            tr = tr + "<td>";
                            tr = tr + "<div class='table_actions'>";
                            tr = tr + '<button class="delete btn btn-default  btn-sm btn-add" data-toggle="popover" title="Delete" onclick="ResourceQualificationDelete(\'' + data[i].qualification_id + '\')";><i class="fa fa-trash" aria-hidden="true"></i></button>';
                            tr = tr + "</div>";
                            tr = tr + "</td>";
                            tr = tr + "</tr>";
                        }
                        $('#SelQualification').append(SelQualification);
                        $('#tblQualification').append(tr);
                    }
                }
            }
            else {
                console.log(data);
            }
            //LoadBranch();
        }
    });
}
//Inserting Doctor and User
function DoctorCreation_insert() {
    debugger;
    var DoctorName = $("#txtDoctorName").val();
    var Speciality = $("#SelSpecialty").val();
    var NPI = $("#txtNPI").val();
    var mbl_consultation = $("#mbl_cons").val();
    var Qualification = $("#SelQualification").val();
    var dat_birth = $("#txtdob").val();
    dat_birth = dat_birth.split("/");
    dat_birth = dat_birth[2] + "-" + dat_birth[1] + "-" + dat_birth[0];
    var DateofBirth = dat_birth;
    var Age = $("#txtAge").val();
    var Gender = $("#ddlGender").val();
    var photo = $("#Hdn_profile_image").val();
    var sign = $("#Hdn_sign_image").val();
    var radio = $("input[name='chkPassPort']:checked").val();
    var txtLine1 = $('#txtLine1').val();
    var txtLine2 = $('#txtLine2').val();
    var Place = $('#txtPlace').val();
    var txtCountry = $('#txtCountry').val();
    var txtState = $('#txtState').val();
    var txtCity = $('#txtCity').val();
    var txtpin_code = $('#textpin_code').val();
    var department_id = $('#Department').val().toString();
    var subdepartment_id = $('#SubDepartemnt').val().toString();
    var branchid = $('#branch').val();
    var role = $('#SelRole').val();
    var service_provider_id = $('#SelProvider').val();
    var free_visit = $('#txtfree').val();
    var user_id = $('#user_code').val();
    var password = $('#password').val();
    if (role == 1) {
        indicator_id = 'I'
    }
    else {
        indicator_id = 'UI'
    }
    var a = "";

    if ($.trim(branchid) == "" || $.trim(branchid) == "0") {
        $('#PSelectDoctorbranch').show();
        $('#PSelectDoctorbranch').fadeOut(8000);
        $('#PSelectDoctorbranch').html('Select Branch');
        $('#branch').val('');
        $('#branch').focus();
        return false;
    }

    if ($.trim(role) == "" || $.trim(role) == "0") {
        $('#PSelRole').show();
        $('#PSelRole').fadeOut(8000);
        $('#PSelRole').html('Select Role');
        $('#SelRole').val('');
        $('#SelRole').focus();
        return false;
    }

    if ($.trim($('#Department').val()) == "" || $.trim($('#Department').val()) == "0") {
        $('#PDepartment').show();
        $('#PDepartment').fadeOut(8000);
        $('#PDepartment').html('Select Department');
        $('#Department').val('');
        $('#Department').focus();
        return false;
    }
    if ($.trim($('#SubDepartemnt').val()) == "" || $.trim($('#SubDepartemnt').val()) == "0") {
        $('#PSubDepartemnt').show();
        $('#PSubDepartemnt').fadeOut(8000);
        $('#PSubDepartemnt').html('Select SubDepartment');
        $('#SubDepartemnt').val('');
        $('#SubDepartemnt').focus();
        return false;
    }

    if ($.trim(role) == 1) {
        if ($('#doctor_type').val() == 0 || $('#doctor_type').val() == null) {
            $('#Pdoctor_type').show();
            $('#Pdoctor_type').fadeOut(8000);
            $('#Pdoctor_type').html('Select Doctor Type');
            $('#doctor_type').val(0);
            $('#doctor_type').focus();
            return false;
        }
    }

    if ($.trim(user_id) == "") {
        $('#Puser_code').show();
        $('#Puser_code').fadeOut(8000);
        $('#Puser_code').html('Enter the Employee ID');
        $('#user_code').val('');
        $('#user_code').focus();
        return false;
    }

    if ($.trim(password) == "") {
        $('#Ppassword').show();
        $('#Ppassword').fadeOut(8000);
        $('#Ppassword').html('Enter Password');
        $('#password').val('');
        $('#password').focus();
        return false;
    }
    else if (password.length < 8) {
        $('#Ppassword').show();
        $('#Ppassword').fadeOut(8000);
        $('#Ppassword').html('Password should be at least 8 characters');
        $('#password').val('');
        $('#password').focus();
        return false;
    }

    if ($.trim($('#ddSalutation').val()) == "0") {
        $('#PddSalutation').show();
        $('#PddSalutation').fadeOut(8000);
        $('#PddSalutation').html('Select Salutation');
        $('#ddSalutation').val('0');
        $('#ddSalutation').focus();
        return false;
    }

    if ($.trim(DoctorName) == "") {
        $('#PtxtDoctorName').show();
        $('#PtxtDoctorName').fadeOut(8000);
        $('#PtxtDoctorName').html('Enter Name');
        $('#txtDoctorName').val('');
        $('#txtDoctorName').focus();
        return false;
    }
    if ($.trim(DoctorName).length < "2") {
        $('#PtxtDoctorName').show();
        $('#PtxtDoctorName').fadeOut(8000);
        $('#PtxtDoctorName').html('Enter Name minimum of two Charater');
        $('#txtDoctorName').val('');
        $('#txtDoctorName').focus();
        return false;
    }

    if ($.trim($("#txtdob").val()) == "") {
        $('#Ptxtdob').show();
        $('#Ptxtdob').fadeOut(8000);
        $('#Ptxtdob').html('Select date of Birth');
        $('#txtdob').val('');
        $('#txtdob').focus();
        return false;
    }
    if ($.trim(Age) == "") {
        $('#PtxtAge').show();
        $('#PtxtAge').fadeOut(8000);
        $('#PtxtAge').html('Enter the Age');
        $('#txtAge').val('');
        $('#txtAge').focus();
        return false;
    }
    if ($('#ddlGender').val() == "0") {
        $('#PddlGender').show();
        $('#PddlGender').fadeOut(8000);
        $('#PddlGender').html('Select Gender');
        $('#ddlGender').focus();
        return false;
    }

    if ($.trim(role) == 1) {
        if ($.trim(Qualification) == "") {
            $('#PSelQualification').show();
            $('#PSelQualification').fadeOut(8000);
            $('#PSelQualification').html('Select Qualification');
            $('#SelQualification').val('');
            $('#SelQualification').focus();
            return false;
        }
    }

    if ($.trim($('#SelDoctorDesignation').val()) == "" || $.trim($('#SelDoctorDesignation').val()) == "0") {
        $('#PSelectDoctorDesignation').show();
        $('#PSelectDoctorDesignation').fadeOut(8000);
        $('#PSelectDoctorDesignation').html('Select Designation');
        $('#SelDoctorDesignation').val('');
        $('#SelDoctorDesignation').focus();
        return false;
    }

    if (parseInt(role) == 1) {
        if (Speciality == "" || Speciality == 0) {
            $('#PSelSpecialty').show();
            $('#PSelSpecialty').fadeOut(8000);
            $('#PSelSpecialty').html('Select Specality');
            $('#SelSpecialty :selected').val('0');
            $('#SelSpecialty').focus();
            return false;
        }

        if ($.trim(NPI) == "") {
            $('#PtxtNPI').show();
            $('#PtxtNPI').fadeOut(8000);
            $('#PtxtNPI').html('National Provider Identifier (NPI)');
            $('#txtNPI').val('');
            $('#txtNPI').focus();
            return false;
        }

        if ($('#doctor_type').val() != 2) {
            if ($('#txtfree').val() == '') {
                $('#Ptxtfree').show();
                $('#Ptxtfree').fadeOut(8000);
                $('#Ptxtfree').html('Enter the free visit');
                $('#txtfree').val('');
                $('#txtfree').focus();
                return false;
            }

            if ($('#mbl_cons').val() == '') {
                $('#Pmbl_cons').show();
                $('#Pmbl_cons').fadeOut(8000);
                $('#Pmbl_cons').html('Select Mobile Consultation');
                $('#mbl_cons').val('');
                $('#mbl_cons').focus();
                return false;
            }
        }
    }


    if ($.trim(txtLine1) == "") {
        $('#PtxtLine1').show();
        $('#PtxtLine1').fadeOut(8000);
        $('#PtxtLine1').html('Please Enter Line1 Address');
        $('#txtLine1').val('');
        $('#txtLine1').focus();
        return false;
    }
    if ($.trim(txtLine2) == "") {
        $('#PtxtLine2').show();
        $('#PtxtLine2').fadeOut(8000);
        $('#PtxtLine2').html('Please Enter Line Address');
        $('#txtLine2').val('');
        $('#txtLine2').focus();
        return false;
    }
    if ($.trim(Place) == "") {
        $('#PtxtPlace').show();
        $('#PtxtPlace').fadeOut(8000);
        $('#PtxtPlace').html('Enter the Area');
        $('#txtPlace').val('');
        $('#txtPlace').focus();
        return false;
    }
    if ($.trim(txtCountry) == "" || $.trim(txtCountry) == "0") {
        $('#PtxtCountry').show();
        $('#PtxtCountry').fadeOut(8000);
        $('#PtxtCountry').html('Select The Country');
        $('#txtCountry').val('');
        $('#txtCountry').focus();
        return false;
    }
    if ($('#txtState').val() == "" || $('#txtState').val() == "0") {
        $('#PtxtState').show();
        $('#PtxtState').fadeOut(8000);
        $('#PtxtState').html('Select The state');
        $('#txtState').val('');
        $('#txtState').focus();
        return false;
    }
    if ($('#txtCity').val() == "" || $('#txtCity').val() == "0") {
        $('#PtxtCity').show();
        $('#PtxtCity').fadeOut(8000);
        $('#PtxtCity').html('Select The City');
        $('#txtCity').val('');
        $('#txtCity').focus();
        return false;
    }
    if ($('#textpin_code').val() == "" || $('#textpin_code').val() == "0") {
        $('#Ptxtpin_code').show();
        $('#Ptxtpin_code').fadeOut(8000);
        $('#Ptxtpin_code').html('Enter the Pin Code');
        $('#textpin_code').val('');
        $('#textpin_code').focus();
        return false;
    }
    if ($('#txtPMobileNumber').val() == "") {
        $('#PtxtPMobileNumber').show();
        $('#PtxtPMobileNumber').fadeOut(8000);
        $('#PtxtPMobileNumber').html('Enter Mobile Number');
        $('#txtPMobileNumber').val('');
        $('#txtPMobileNumber').focus();
        return false;
    }
    if ($('#txtPMobileNumber').val() != "" && $('#txtPMobileNumber').val().length != "10") {
        $('#PtxtPMobileNumber').show();
        $('#PtxtPMobileNumber').fadeOut(8000);
        $('#PtxtPMobileNumber').html('Invaild Mobile Number');
        $('#txtPMobileNumber').val('');
        $('#txtPMobileNumber').focus();
        return false;
    }
    if ($('#txtPMobileNumber2').val() != "" && $('#txtPMobileNumber2').val().length != "10") {
        $('#PtxtPMobileNumber2').show();
        $('#PtxtPMobileNumber2').fadeOut(8000);
        $('#PtxtPMobileNumber2').html('Invaild Mobile Number');
        $('#txtPMobileNumber2').val('');
        $('#txtPMobileNumber2').focus();
        return false;
    }
    //if ($('#txtPEmailID').val() == "") {
    //    $('#PtxtPEmailID').show();
    //    $('#PtxtPEmailID').fadeOut(8000);
    //    $('#PtxtPEmailID').html('Enter the EmailId');
    //    $('#txtPEmailID').val('');
    //    $('#txtPEmailID').focus();
    //    return false;
    //}
    debugger;
    if ($("#chkYes").is(":checked") == false && $("#chkNo").is(":checked") == false) {
        $('#PtxtConcern').show();
        $('#PtxtConcern').fadeOut(8000);
        $('#PtxtConcern').html('Choose Experience');
        $('#txtConcern').val('');
        $('#txtConcern').focus();
        return false;
    }
    if ($("#chkYes").is(":checked")) {
        if ($.trim($('#txtTotalExp').val()) == "" || $('#tblExperience  >tbody >tr').length == 0) {
            $('#PtxtConcern').show();
            $('#PtxtConcern').fadeOut(8000);
            $('#PtxtConcern').html('Fill Experience Detail');
            $('#txtConcern').val('');
            $('#txtConcern').focus();
            return false;
        }
    }
    var filename = $('#myHiddenVar').val();
    var proffilename = " ", resfilename = " ", signupload = " ";
    var profleupload = $('#documents').val();
    if ($.trim(profleupload) != "") {
        var fileExtension = profleupload.substring(profleupload.lastIndexOf(".") + 1, profleupload.length);
        if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "JPG" || fileExtension == "JPEG") {
            proffilename = filename + '_profile.' + fileExtension;
        }
        else {
            $('#Pdocuments').show();
            $('#Pdocuments').fadeOut(8000);
            $('#Pdocuments').html('Please upload file having extensions .jpeg/.jpg only.');
            $('#documents').val('');
            $('#documents').focus();
            return false;
        }

        if (window.FormData === undefined) {
            $('#Pdocuments').show();
            $('#Pdocuments').fadeOut(8000);
            $('#Pdocuments').html('Fle format is not supported');
            $('#documents').val('');
            $('#documents').focus();
            return false;
        }
    }
    else {
        proffilename = $('#Hdn_profile_image').val();
    }
    var resumeupload = $('#Resumedocuments').val();
    if ($.trim(resumeupload) != "") {
        var resumefileExtension = resumeupload.substring(resumeupload.lastIndexOf(".") + 1, resumeupload.length);

        if (resumefileExtension == "pdf" || resumefileExtension == "doc" || resumefileExtension == "PDF" || resumefileExtension == "DOC") {
            resfilename = filename + '_resume.' + resumefileExtension;
        }
        else {
            $('#PResumedocuments').show();
            $('#PResumedocuments').fadeOut(8000);
            $('#PResumedocuments').html('Please upload file having extensions .pdf/.doc only.');
            $('#Resumedocuments').val('');
            $('#Resumedocuments').focus();
            return false;
        }
        if (window.FormData === undefined) {
            $('#PResumedocuments').show();
            $('#PResumedocuments').fadeOut(8000);
            $('#PResumedocuments').html('File format is not supported');
            $('#Resumedocuments').val('');
            $('#Resumedocuments').focus();
            return false;
        }
    }
    else {
        resfilename = $('#Hdn_redume').val();
    }
    var signfleupload = $('#signdocuments').val();
    if ($.trim(signfleupload) != "") {
        var fileExtension = signfleupload.substring(signfleupload.lastIndexOf(".") + 1, signfleupload.length);
        if (fileExtension == "jpg" || fileExtension == "jpeg" || fileExtension == "JPG" || fileExtension == "JPEG") {
            signupload = filename + '_sign.' + fileExtension;
        }
        else {
            $('#Psigndocuments').show();
            $('#Psigndocuments').fadeOut(8000);
            $('#Psigndocuments').html('Please upload file having extensions .jpeg/.jpg only.');
            $('#signdocuments').val('');
            $('#signdocuments').focus();
            return false;
        }

        if (window.FormData === undefined) {
            $('#Psigndocuments').show();
            $('#Psigndocuments').fadeOut(8000);
            $('#Psigndocuments').html('Fle format is not supported');
            $('#signdocuments').val('');
            $('#signdocuments').focus();
            return false;
        }
    }
    else {
        signupload = $('#Hdn_sign_image').val();
    }
    $('#shade_slot_submit').show();
    var DoctorId = $('#myHiddenVar').val();

    var DoctorUpdationData = {
        doctor_id: DoctorId,
        doctor_name: $("#txtDoctorName").val(),
        speciality: $("#SelSpecialty").val(),
        national_provider_id: $("#txtNPI").val(),
        mobile_consultation: $('#mbl_cons').val(),
        qualification: a,
        dob: DateofBirth,
        age: $("#txtAge").val(),
        gender: $("#ddlGender").val(),
        photo: proffilename,
        signature: signupload,
        mobile_number_one: $("#txtPMobileNumber").val(),
        mobile_number_two: $("#txtPMobileNumber2").val(),
        email_id: $("#txtPEmailID").val(),
        marital_status: $("#ddlMart").val(),
        resume: resfilename,
        total_experience: $("#txtTotalExp").val(),
        Dml_Indicator: indicator_id,
        hospital_id: $('#HospitalId').val(),
        doctor_designation: $('#SelDoctorDesignation').val(),
        display_designation: $('#txtDisplayDesignation').val(),
        service: $('#d_Service').val(),
        department: department_id,
        subdepartment: subdepartment_id,
        // branch_id: branchid,
        user_role_id: role,
        service_provider_id: service_provider_id,
        free_visit: free_visit,
        doctor_type: $('#doctor_type').val(),
        salutation: $('#ddSalutation').val(),
        user_created_by: $('#hiddenUser_id').val(),
        user_id: user_id,
        password: password,
        user_floorid: $('#ddUser_Floor').val(),
        line_one: $("#txtLine1").val(),
        line_two: $("#txtLine2").val(),
        town_village: $("#txtPlace").val(),
        country: $("#txtCountry").val(),
        state: $("#txtState").val(),
        city: $("#txtCity").val(),
        pin_code: $('#textpin_code').val(),
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorUpdation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorUpdationData),
        success: function (data) {
            $('#shade_slot_submit').hide();
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                //   DoctorAddressValuesinsert();
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    if (data.length > 0) {
                        //for (var i = 0; i < data.length; i++) {
                        if (data[0].StringResult == "failure") {
                            $('#PtxtPMobileNumber').show();
                            $('#PtxtPMobileNumber').fadeOut(8000);
                            $('#PtxtPMobileNumber').html('Mobile Number already exist');
                            $('#txtPMobileNumber').val('');
                            $('#txtPMobileNumber').focus();

                        }
                        else if (data[0].StringResult == "already exist") {
                            $('#PtxtPEmailID').show();
                            $('#PtxtPEmailID').fadeOut(8000);
                            $('#PtxtPEmailID').html('Email id already exist');
                            $('#txtPEmailID').val('');
                            $('#txtPEmailID').focus();
                            return false;

                        }
                        else if (data[0].StringResult == "UserID_Exist") {
                            $('#Puser_code').show();
                            $('#Puser_code').fadeOut(8000);
                            $('#Puser_code').html('User id already exist');
                            $('#user_code').val('');
                            $('#user_code').focus();
                            return false;
                        }


                        else {
                            multi_branch(data[0].doctoruserid)
                            toastr.success('User Created Successfully');
                            var doctor_ID = '', d;
                            var Dml_Indicator = "";
                            var role_id = data[0].role_id;
                            if (data[0].role_id == 1) {
                                doctor_ID = data[0].DoctorId;
                                d = "D";
                                Dml_Indicator = "I"
                            }
                            else {
                                doctor_ID = data[0].user_id;
                                d = "DD"
                                Dml_Indicator = "UI"
                            }
                            //var doctor_ID = data[0].DoctorId;
                            DoctorAddressValuesinsert(doctor_ID, role_id);
                            AddExperience(doctor_ID, Dml_Indicator);
                            var q = "";
                            // var d = "D";
                            var deletequalification = {
                                doctor_id: doctor_ID,
                                qualification_id: q,
                                Dml_Indicator: d
                            }
                            $.ajax({
                                async: true,
                                type: 'POST',
                                url: '/DoctorProfile/Qualification',
                                contentType: 'application/json; charset=UTF-8',
                                data: JSON.stringify(deletequalification),
                                success: function (data) {
                                    var data = JSON.parse(data);
                                    console.log(data);
                                    debugger;
                                    if (data.msg == "Success") {
                                        var data = data.data;
                                        var qua = $("#SelQualification").val();
                                        for (var i = 0; i < qua.length; i++) {

                                            var insertqualification = {
                                                doctor_id: doctor_ID,
                                                qualification_id: qua[i],
                                                Dml_Indicator: Dml_Indicator
                                            }
                                            $.ajax({
                                                async: true,
                                                type: 'POST',
                                                url: '/DoctorProfile/Qualification',
                                                contentType: 'application/json; charset=UTF-8',
                                                data: JSON.stringify(insertqualification),
                                                success: function (data) {
                                                }
                                            });
                                        }
                                        debugger;

                                        setTimeout(function () {
                                            window.location.href = '/previllages';
                                        }, 2000);

                                    }
                                    else {
                                        console.log(data);
                                    }
                                },
                                error: function () {
                                }
                            });
                        }
                    }
                }
            }
            else {
                console.log(data);
            }
        }
    });
}
//For Inserting /Updating Address values for both doctor and user
function DoctorAddressValuesinsert(doctor_ID, role_id) {
    debugger;
    var is_permanent_address;
    if ($("#ChkID").prop('checked') == true) {
        is_permanent_address = '1';
    }
    else {
        is_permanent_address = '0';
    }
    //var DoctorId = $('#HiddenDoctorID').val();
    DoctorId = doctor_ID;

    if (role_id == 1) {
        if ($("#txtaddressid1").val() != "") {
            Dml_Indicator = "U";
        }
        else {
            Dml_Indicator = "I";
        }
        if ($("#txtaddressid2").val() != "") {
            Dml_Indicator1 = "U";
        }
        else {
            Dml_Indicator1 = "I";
        }
    }
    else {
        if ($("#txtaddressid1").val() != "") {
            Dml_Indicator = "UU";
        }
        else {
            Dml_Indicator = "IU";
        }
        if ($("#txtaddressid2").val() != "") {
            Dml_Indicator1 = "UU";
        }
        else {
            Dml_Indicator1 = "IU";
        }
    }
    //if ($('#hiddenRole_id').val() != "1" && $('#hiddenRole_id').val() != "5") {
    //    DoctorId = doctor_ID;
    //}
    var Dml_Indicator;
    var Dml_Indicator1;

    var DoctorAddressUpdationData = {
        doctor_address_id: $("#txtaddressid1").val(),
        doctor_id: DoctorId,
        line_one: $("#txtLine1").val(),
        line_two: $("#txtLine2").val(),
        town_village: $("#txtPlace").val(),
        country: $("#txtCountry").val(),
        state: $("#txtState").val(),
        city: $("#txtCity").val(),
        pin_code: $('#textpin_code').val(),
        is_permanent_address: is_permanent_address,
        Dml_Indicator: Dml_Indicator
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorAddressUpdation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorAddressUpdationData),
        success: function (data) {
            var DoctorAddressUpdation2Data = {
                doctor_address_id: $("#txtaddressid2").val(),
                doctor_id: DoctorId,
                line_one: $("#txtpLine1").val(),
                line_two: $("#txtpLine2").val(),
                town_village: $("#txtPPlace").val(),
                country: $("#txtPCountry").val(),
                state: $("#txtPState").val(),
                city: $("#txtPCity").val(),
                pin_code: $('#textPpin_code').val(),
                is_permanent_address: is_permanent_address,
                Dml_Indicator: Dml_Indicator1
            }
            $.ajax({
                async: true,
                type: 'POST',
                url: '/DoctorProfile/DoctorAddressUpdation',
                contentType: 'application/json; charset=UTF-8',
                data: JSON.stringify(DoctorAddressUpdation2Data),
                success: function (data) {
                }
            });
        }
    });
}

//Loads the Branch in User Create Page
function LoadBranch() {
    debugger
    document.getElementById("branch").innerHTML = null;

    var Indicator = '', doctor_user_id = ''

    if ($('#hiddenRole_id').val() != 2) {
        Indicator = "LMB"
        doctor_user_id = $('#hiddenUser_id').val()
    }
    else {
        Indicator = "LBRAN",
        doctor_user_id = ''
    }

    var subdepartment = {

        Dml_Indicator: Indicator,
        doctor_user_id: doctor_user_id

    }
    $.ajax({

        async: true,

        type: 'POST',

        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',

        contentType: 'application/json; charset=UTF-8',

        data: JSON.stringify(subdepartment),

        success: function (data) {

            var data1 = JSON.parse(data);

            if (data1.msg == "Success") {

                var data1 = data1.data;

                console.log(data1);

                if (jQuery.isEmptyObject((data1))) {

                }

                else {
                    $('#branch').empty();
                    var SubDepartment;

                    var default_opt = '<option value="0">--Select--</option>';

                    $.each(data1.Table, function (i, emp) {

                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';

                    });
                    if (data1.Table.length == 1) {
                        $('#branch').append(SubDepartment);
                        // for onemaster 
                        //CheckCodeTemplate();
                        //LoadRole();
                        //LoadDepartment();
                        //LoadDesignation();
                        LoadServiceProvider(0, '', 0);
                    }
                    else {

                        $('#branch').append(SubDepartment);
                    }
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#branch').val(SubDepartment)
                        // $('#branch').attr('disabled', true);
                        // for onemaster 
                        // LoadRole();
                        //LoadDepartment();
                        //LoadDesignation();
                        LoadServiceProvider(0, '', 0);
                    }

                }

            }

            else {

                console.log(data1);

            }

        }

    });

}
// for onemaster 
//Loads the Depaertment for the respected branch
$("#branch").change(function () {
    debugger;
    // CheckCodeTemplate();
    //LoadDepartment();
    //LoadDesignation();
    //LoadRole();
    LoadServiceProvider(0, '', 0);
    // $('#Department').val('').change();
});


//Used for Loading the Departments in Hospital for both in Doctorcreation and in EditProfile
function LoadDepartment() {
    var department = {
        Dml_Indicator: "DP",
        // for onemaster 
        //branch_id: $('#branch').val(),
        name: $('#DoctorUser_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                $('#SubDepartemnt').empty();
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#Department').html('');
                    $('#Department').val('');
                    var Department;
                    var default_opt = '<option value="0" disabled>--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#Department').append(Department);
                    $('#Department_Single').append(default_opt + Department);
                    /*$('#Department').val('').change();*/
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Used for Loading the Sub Department DropDown in Doctor Creation and in edit profile
$("#Department").change(function () {

    debugger;
    var subdept = $('#SubDepartemnt').val()
    document.getElementById("SubDepartemnt").innerHTML = null;
    var condition = '(', dep_length = 0;
    if ($('#SelRole').val() != 1) {
        //$('#SubDepartemnt').prop('multiple', true);
        dep_length = $('#Department').val().length;
        for (var i = 0; i < dep_length; i++) {
            condition = condition + 'master_category_department_id=' + $('#Department').val()[i]
            if (i != (dep_length - 1)) {
                condition = condition + ' or '
            }
        }
    }
    else {
        //$('#SubDepartemnt').prop('multiple', false);
        condition = condition + 'master_category_department_id=' + $('#Department').val()
    }
    condition = condition + ') and status=1'
    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SDM",
        condition: condition,
        name: $('#DoctorUser_Id').val()
        //Master_cd_id: $('#Department').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#SubDepartemnt').html(SubDepartment);
                    $('#Subdepartment_Single').append(default_opt + SubDepartment);
                    if ($('#SelRole').val() != 1) {
                        $('#SubDepartemnt').prop('multiple', true);
                    }
                    else {
                        $('#SubDepartemnt').prop('multiple', false);
                    }
                    $('#SubDepartemnt').val('').change();
                    if (subdept != '' || subdept != null) {
                        $('#SubDepartemnt').val(subdept);
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});



//Designation List for both in DoctorCreation and DoctorProfile
function LoadDesignation() {
    debugger;
    var designation = {
        Dml_Indicator: "LD",
        // for onemaster 
        //branch_id: $('#branch').val()

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(designation),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                    $('#SelDoctorDesignation').html('');
                    $('#SelDoctorDesignation').val('');
                    var Designation;
                    var default_opt = '<option value="0">--Select --</option>';
                    $.each(data1.Table, function (i, emp) {
                        Designation = Designation + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';
                    });
                    $('#SelDoctorDesignation').append(default_opt + Designation);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Rebinds the department (UserCreation/DoctorCreation/EditProfile)
function department(department_id, subdepartment_id) {

    var department = {
        Dml_Indicator: "DP",
        // branch_id: branchid
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#Department').html('');
                    $('#Department').val('');
                    $('#Department_user').html('');
                    $('#Department_user').val('');
                    var Department;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#Department').append(default_opt + Department);
                    if ($('#hiddenRole_id').val() == 1 || ($('#myHiddenVar1').val() != null && $('#myHiddenVar1').val() != '')) {
                        //$('#branch').prop('disabled', true);
                        $('#Department').prop('disabled', true);
                        $('#Department').val(department_id);
                    }
                    else {
                        if (department_id != '' && department_id != null) {
                            $('#Department').val(department_id.split(','));
                        }
                    }


                    $('#Department_user').append(default_opt + Department);
                    $('#Department_user').val(department_id)
                    subdepartment(department_id, subdepartment_id)
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Rebinds the subdepartment (UserCreation/DoctorCreation/EditProfile)
function subdepartment(department_id, subdepartment_id) {
    debugger;
    var name_val = $('#HName').val();

    if (name_val != '') {
        var condition = '(', dep_length = 0;
        dep_length = $('#Department').val().length;
        for (var i = 0; i < dep_length; i++) {
            condition = condition + 'master_category_department_id=' + $('#Department').val()[i]
            if (i != (dep_length - 1)) {
                condition = condition + ' or '
            }
        }
        condition = condition + ') and status!=0'
    }
    else {
        var condition = '(', dep_length = 0;
        dep_length = $('#Department').val().length;
        for (var i = 0; i < dep_length; i++) {
            condition = condition + 'master_category_department_id=' + $('#Department').val()[i]
            if (i != (dep_length - 1)) {
                condition = condition + ' or '
            }
        }
        condition = condition + ') and status=1'
    }

    console.log(condition);
    var subdepartment = {
        Dml_Indicator: "SDM",
        condition: condition,
        //Master_cd_id: department_id
        name: name_val

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment;
                    $('#SubDepartemnt').html('');
                    $('#SubDepartemnt').val('');
                    $('#SubDepartemnt_user').html('');
                    $('#SubDepartemnt_user').val('');
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#SubDepartemnt').append(default_opt + SubDepartment);
                    debugger;
                    if ($('#hiddenRole_id').val() == 1 || ($('#myHiddenVar1').val() != null && $('#myHiddenVar1').val() != '')) {
                        $('#SubDepartemnt').prop('disabled', true);
                        $('#SubDepartemnt').val(subdepartment_id);
                    }
                    else {
                        if (subdepartment_id != '' && subdepartment_id != null) {
                            $('#SubDepartemnt').val(subdepartment_id.split(','));
                        }
                    }

                    $('#SubDepartemnt_user').append(default_opt + SubDepartment);
                    $('#SubDepartemnt_user').val(subdepartment_id);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
};

//rebinds the Designation (UserCreation/DoctorCreation/EditProfile)
function Designation(designation_id, branchid) {
    debugger;
    var load_designation = $('#HLDesignation').val();
    var designation = {
        Dml_Indicator: "LD",
        // branch_id: branchid,
        name: load_designation
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(designation),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                    $('#SelDoctorDesignation_user').html('');
                    $('#SelDoctorDesignation_user').val('');
                    $('#SelDoctorDesignation').html('');
                    $('#SelDoctorDesignation').val('');
                    var Designation;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Designation = Designation + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';
                    });
                    $('#SelDoctorDesignation').append(default_opt + Designation);
                    $('#SelDoctorDesignation').val(designation_id);
                    $('#SelDoctorDesignation_user').append(default_opt + Designation);
                    $('#SelDoctorDesignation_user').val(designation_id);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Rebinds the Branch in User Create Page
function branch(branch_id) {
    debugger
    document.getElementById("branch").innerHTML = null;

    var subdepartment = {

        Dml_Indicator: "LBRAN"

    }

    $.ajax({

        async: true,

        type: 'POST',

        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',

        contentType: 'application/json; charset=UTF-8',

        data: JSON.stringify(subdepartment),

        success: function (data) {

            var data1 = JSON.parse(data);

            if (data1.msg == "Success") {

                var data1 = data1.data;

                console.log(data1);

                if (jQuery.isEmptyObject((data1))) {

                }

                else {
                    $('#branch').empty();
                    var SubDepartment;

                    var default_opt = '<option value="0">--Select--</option>';

                    $.each(data1.Table, function (i, emp) {

                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';

                    });

                    $('#branch').append(default_opt + SubDepartment);
                    if ($('#hiddenRole_id').val() == 1 || ($('#myHiddenVar1').val() != null && $('#myHiddenVar1').val() != '')) {
                        $('#branch').val(branch_id.split(','));
                        //LoadRole();
                    }

                    else {
                        if (branch_id != '' && branch_id != null) {
                            $('#branch').val(branch_id.split(','));
                            //LoadRole();
                        }
                    }
                }

            }

            else {

                console.log(data1);

            }

        }

    });

}

//Loads the Branch in User Create Page
function LoadBranch_user() {
    document.getElementById("Branch_user").innerHTML = null;

    var subdepartment = {

        Dml_Indicator: "LBRAN"

    }

    $.ajax({

        async: true,

        type: 'POST',

        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',

        contentType: 'application/json; charset=UTF-8',

        data: JSON.stringify(subdepartment),

        success: function (data) {

            var data1 = JSON.parse(data);

            if (data1.msg == "Success") {

                var data1 = data1.data;

                console.log(data1);

                if (jQuery.isEmptyObject((data1))) {

                }

                else {

                    var SubDepartment;

                    var default_opt = '<option value="0">--Select--</option>';

                    $.each(data1.Table, function (i, emp) {

                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';

                    });

                    $('#Branch_user').append(default_opt + SubDepartment);
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#branch').val($('#HospitalBranchID').val())
                        $('#branch').attr('disabled', true);
                    }

                }

            }

            else {

                console.log(data1);

            }

        }

    });

}

//Loads the Depaertment for the repspected branch
$("#Branch_user").change(function () {
    LoadRole();
    $('#SelRole').attr('disabled', false);
    LoadDepartment_user();
    LoadDesignation_user();
});


//Binds the Departments  in UserCreation
function LoadDepartment_user() {
    var department = {
        Dml_Indicator: "DP",
        branch_id: $('#Branch_user').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                    $('#Department_user').html('');
                    $('#Department_user').val('');
                    var Department;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Department = Department + '<option value="' + emp.master_category_department_id + '">' + emp.name + '</option>';
                    });
                    $('#Department_user').append(default_opt + Department);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}


//Binds the Subdepartment in  UserCreation
$("#Department_user").change(function () {
    document.getElementById("SubDepartemnt_user").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "SDP",
        Master_cd_id: $('#Department_user').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.sub_category_department_id + '">' + emp.sub_cd_name + '</option>';
                    });
                    $('#SubDepartemnt_user').append(default_opt + SubDepartment);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
});

//Binds the Designation in Usercreation page
function LoadDesignation_user() {
    var designation = {
        Dml_Indicator: "LD",
        branch_id: $('#Branch_user').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(designation),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1)
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#SelDoctorDesignation_user').html('');
                    $('#SelDoctorDesignation_user').val('');
                    var Designation;
                    var default_opt = '<option value="0">--Select --</option>';
                    $.each(data1.Table, function (i, emp) {
                        Designation = Designation + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';
                    });
                    $('#SelDoctorDesignation_user').append(default_opt + Designation);
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

//Rebinds the Branch in User Create Page
function branch_user(branch_id) {
    document.getElementById("Branch_user").innerHTML = null;

    var subdepartment = {

        Dml_Indicator: "LBRAN"

    }

    $.ajax({

        async: true,

        type: 'POST',

        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',

        contentType: 'application/json; charset=UTF-8',

        data: JSON.stringify(subdepartment),

        success: function (data) {

            var data1 = JSON.parse(data);

            if (data1.msg == "Success") {

                var data1 = data1.data;

                console.log(data1);

                if (jQuery.isEmptyObject((data1))) {

                }

                else {

                    var SubDepartment;

                    var default_opt = '<option value="0">--Select--</option>';

                    $.each(data1.Table, function (i, emp) {

                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';

                    });

                    $('#Branch_user').append(default_opt + SubDepartment);
                    $('#Branch_user').val(branch_id);

                }

            }

            else {

                console.log(data1);

            }

        }

    });

}

//To Add the experience
$('#btnExperience').click(function () {
    debugger;
    if ($('#txtConName').val() == '') {
        $('#PtxtConName').show();
        $('#PtxtConName').fadeOut(8000);
        $('#PtxtConName').html('Enter the Concern Name');
        $('txtConName').focus();
        return false;
    }
    if ($('#txtExpinMonth').val() == '') {
        $('#PtxtExpinMonth').show();
        $('#PtxtExpinMonth').fadeOut(8000);
        $('#PtxtExpinMonth').html('Enter the Experience');
        $('txtExpinMonth').focus();
        return false;
    }
    else {
        debugger;
        var con_name = $("#txtConName").val();
        var experience = $('#txtExpinMonth').val();
        var sess_key;
        var rowcount = $.trim($('#ExperienceTableRowCount').val());
        var tot_row = (Number(rowcount) + 1);
        var sno = $.trim($('#Experience_sno').val());
        var tot_sno = (Number(sno) + 1);
        $('#Experience_sno').val(tot_sno);
        var doctor_experience_id = 0;
        var sess_data_ser = con_name + "," + doctor_experience_id + "," + experience;
        var afterInc = '';
        var key = '';
        sessionStorage.setItem(tot_sno, sess_data_ser);
        var value = "<tr id=" + tot_sno + " class=exp_" + tot_sno + "><td>" + con_name + "</td><td style='display:none;'>" + doctor_experience_id + "</td><td>" + experience + "</td><td></button><button class='delete btn btn-danger btn-xs btn-add' onclick='Experiencedelete(\"" + tot_sno + "\")'><i class='fa fa-trash' aria-hidden='true'></i></button></td></tr>";
        $('#tblExperience > tbody:last').append(value);
        $('#ExperienceTableRowCount').val(tot_row);
        $('#txtConName').val('');
        $('#txtExpinMonth').val('');
        var total_exp = 0;
        $("#tblExperience").children("tbody").children("tr").each(function (i) {
            var $this = $(this);
            var my_td = $this.children("td");
            total_exp = parseFloat(total_exp) + parseFloat(my_td.eq(2).html());


        });
        if (total_exp == 0) {
            total_exp = '';
        }
        $('#txtTotalExp').val(total_exp);
    }
})

//Remove the Experience from the Table
function Experiencedelete(id) {
    debugger;
    sessionStorage.removeItem(id);
    $('.exp_' + id).remove();
    var rowcount = $.trim($('#ExperienceTableRowCount').val());
    rowcount = rowcount - 1;
    $('#ExperienceTableRowCount').val(rowcount);
    var total_exp = 0;
    $("#tblExperience").children("tbody").children("tr").each(function (i) {
        var $this = $(this);
        var my_td = $this.children("td");
        //total_exp = parseInt(total_exp) + parseInt(my_td.eq(2).html());
        total_exp = parseFloat(total_exp) + parseFloat(my_td.eq(2).html());

    });
    if (total_exp == 0) {
        total_exp = '';
    }
    $('#txtTotalExp').val(total_exp);
}


//Insert Experience into an database
function AddExperience(doctor_id, Indicator) {
    debugger;
    var Experience = new Array();
    var doctorid = doctor_id;
    var Dml_Indicator = Indicator;
    debugger;
    var tot_rowM = $.trim($('#ExperienceTableRowCount').val());
    if (tot_rowM > 0) {
        var L = 0;
        for (j = 1; j <= tot_rowM; j++) {
            var Experience_data = {}
            debugger;
            var a = $('#tblExperience tbody').children('tr').eq(L);
            var b = a.prop('id');
            L++;
            //var C = b.split('_');
            var gkey = b;
            var row_data = sessionStorage.getItem(b);
            var K = 0;
            if (row_data == "") {
                console.log(b + '--------->' + row_data);
            }
            if (row_data != "" && row_data != null) {
                console.log(b + '-------' + row_data);
                row_data = row_data.split(',');
                Experience_data.doctor_experience_id = row_data[1],
                Experience_data.employer_name = row_data[0],
                Experience_data.experience = row_data[2],
                Experience_data.Dml_Indicator = Dml_Indicator,
                Experience_data.doctor_id = doctor_id

            }
            console.log(Experience.push(Experience_data));

        }
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorExperience',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Experience),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                }
            }
            else {
                console.log(data1);
            }
        }
    })
    //location.reload();
}
//Loading Role  based on branch
function LoadRole(role) {
    var RoleData = {
        Dml_Indicator: "SS",
        created_by: $('#HiddenDoctorID').val(),
        // for onemaster 
        //branch_id: $('#branch').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RoleData),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                debugger;
                if (jQuery.isEmptyObject((data))) {

                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('#SelRole').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    if (data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data[i].doctor_user_role_id + '">' + data[i].doctor_user_role_name + '</option>';
                        }

                        $('#SelRole').append(default_opt + SelRole);
                        if ($('#user_role_id').val() != '' && $('#user_role_id').val() != null) {
                            $('#SelRole').val($('#user_role_id').val());

                            if ($('#SelRole').val() == '1') {

                                var date = new Date();

                                date.setFullYear(new Date().getFullYear() - 24);
                                $('.DOB').datepicker('destroy').datepicker({
                                    format: 'dd/mm/yyyy',
                                    endDate: date,
                                    todayHighlight: true,
                                    autoclose: true
                                })
                            }
                            else {
                                var date = new Date();

                                date.setFullYear(new Date().getFullYear() - 18);
                                $('.DOB').datepicker('destroy').datepicker({
                                    format: 'dd/mm/yyyy',
                                    endDate: date,
                                    todayHighlight: true,
                                    autoclose: true
                                })

                            }
                        }
                        if ($('#myHiddenVar1').val() != '' && $('#myHiddenVar1').val() != null) {
                            $('#SelRole').attr('disabled', true);

                            if ($('#SelRole').val() == 1) {

                            }
                            //if (id != 0) {
                            //    $('#SelProvider').val(id);
                            //}

                        }
                    }


                }

            }
            else {
                console.log(data);
            }

        }

    });

}


//load doctor type
function load_doctor_type(doctor_typeid) {
    debugger;
    var load_type = {
        Dml_Indicator: "DT"
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorUpdation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load_type),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);

            if (data1.msg == "Success") {

                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    $('#doctor_type').empty();
                    var doctor_type = '';

                    var default_opt = '<option value="0">--Select--</option>';

                    $.each(data1, function (i, emp) {
                        doctor_type = doctor_type + '<option value="' + emp.id + '">' + emp.type_name + '</option>';
                    });
                    if (data1.length == 1) {
                        $('#doctor_type').append(doctor_type);
                    }
                    else {
                        $('#doctor_type').append(default_opt + doctor_type);
                    }

                    if (doctor_typeid != null && doctor_typeid != '') {
                        $('#doctor_type').val(doctor_typeid)
                        $('#doctor_type').attr('disabled', true)
                    }
                }
            }

            else {
                console.log(data1);
            }
        }
    });
}

//If the role is doctor need to check the license
$('#SelRole').change(function () {
    debugger;

    if ($('#SelRole').val() == '1') {
        $('#ddSalutation').val(3);
        LoadLicenseCount();
        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 24);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').show();
        //$('.div_single_select').show();
        //$('.div_multi_select').hide();


        $('#Department').prop('multiple', false);
        /* $('#Department').val('').change();*/
        //$('#SubDepartemnt').prop('multiple', false);
        $('#div_NPI').removeClass('hide');

        $('#div_doctor_type').attr('hidden', false)
        load_doctor_type()
        $('#mdQualifspan').show();

    }
    else {
        $('#ddSalutation').val(0);
        var date = new Date();

        date.setFullYear(new Date().getFullYear() - 18);
        $('.DOB').datepicker('destroy').datepicker({
            format: 'dd/mm/yyyy',
            endDate: date,
            todayHighlight: true,
            autoclose: true
        })

        $('#divtxtfree').hide();

        $('#Department').prop('multiple', true);
        $('#SubDepartemnt').prop('multiple', true);

        //$('#Department').val('').change();
        $('#div_NPI').addClass('hide');
        //$('.div_single_select').hide();
        //$('.div_multi_select').show();

        $('#div_doctor_type').attr('hidden', true)
        $('#doctor_type').val(0);
        $('#mdQualifspan').hide();

    }
    //if($('#SelRole').val()=='4')
    //{
    //    $('#divSelProvider').show();
    //    LoadServiceProvider(0,'',0);
    //}
    //else {
    //    $('#divSelProvider').hide();
    //}

});

//doctor_type Onchange  -- Antony
$('#doctor_type').change(function () {
    debugger;
    if ($('#doctor_type').val() == 2) {
        $('#divtxtfree').hide();
        $('#divmbl_cons').hide();
        $('#divSelProvider').hide();
    }
    else {
        $('#divtxtfree').show();
        $('#divmbl_cons').show();
        $('#divSelProvider').show();
    }
})


//Load Service Provider
function LoadServiceProvider(branch_id, hospital_id, id) {
    debugger;
    if (branch_id == 0) {
        branch_id = '1'//$('#branch').val().toString()
    }
    else {
        branch_id = branch_id
    }
    if (hospital_id == '') {
        hospital_id = $('#HospitalId').val()
    }
    else {
        hospital_id = hospital_id
    }
    var DoctorData = {
        Dml_Indicator: "LSP",
        multi_branchid: branch_id,
        hospital_id: hospital_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorUpdation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            debugger;

            var data = JSON.parse(data);
            $('#SelProvider').empty();
            if (data.msg == "Success") {
                var data = data.data;
                console.log(data);

                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    debugger;

                    if (data.length > 0) {
                        var Service;
                        var default_opt = '<option value="0">--Select--</option>';
                        $.each(data, function (i, emp) {
                            Service = Service + '<option value="' + emp.labtranid + '">' + emp.lab_name + '</option>';
                        });
                        $('#SelProvider').append(default_opt + Service);
                        if (id != 0) {
                            $('#SelProvider').val(id);
                        }
                        if ($('#doctor_type').val() != 2) {
                            $('#divSelProvider').show();
                        }
                        else {
                            $('#divSelProvider').hide();
                        }
                    }
                }
            }
            else {
                console.log(data);
            }
        }
    });
}
//License count check for doctor
function LoadLicenseCount() {
    var DoctorData = {
        Dml_Indicator: "LS",
        hospital_id: $('#HospitalId').val(),
        branch_id: $('#branch').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorUpdation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    debugger;
                    if (data.length > 0) {
                        var no_of_license = data[0].no_of_license;
                        var total_doctors = data[0].total_doctors;
                        if (no_of_license != null && no_of_license != '' && total_doctors != 0 && total_doctors != '' && total_doctors != null) {
                            if (no_of_license == total_doctors) {

                                toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>OK</button>", 'Doctor License count exceed',
                         {
                             closeButton: true,
                             progressBar: true,
                             allowHtml: true,
                             timeOut: 20000000,

                             onShown: function (toast) {
                                 $("#confirmationRevertYes").click(function () {
                                     //$('#SelRole').val(0);
                                 })
                             }
                         })
                                $('#SelRole').val(0);
                            }
                            else {
                                //$('#divdocdr').show();
                                //$('#divrole').hide();
                                //$('#divroledr').hide();
                            }
                        }


                    }
                }
            }
            else {
                console.log(data);
            }
        }
    });
}
//For User Details Retrieve
function LoadDoctorUser() {
    debugger;
    if ($('#DoctorUser_Id').val() != '') {
        var DoctorID = $.trim($('#DoctorUser_Id').val());
        if (DoctorID == 1) {
            toastr.warning("You have no rights to edit the Doctor Profile");
            window.location.href = '/Previllages';
        }
        else {
            $('#divrole').show();
        }
        var DoctorData = {
            doctor_user_id: DoctorID,
            Dml_Indicator: 'SS'
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/UserCreate/UserCreationUpdated',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(DoctorData),
            success: function (data) {
                debugger;
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    $('#btnSubmit1').html('Update');
                    var data = data.data;
                    console.log(data);
                    if (jQuery.isEmptyObject((data))) {
                    }
                    else {
                        if (data.length > 0) {
                            for (var i = 0; i < data.length; i++) {
                                debugger;

                                var profilephoto = data[i].photo;
                                var signature = data[i].signature;
                                $('#Hdn_profile_image').val(data[i].photo);
                                var resumefile = data[i].resume;
                                $('#Hdn_redume').val(data[i].resume);
                                var filepath = $('#hdnfilepath1').val();
                                var photo = $('#hdnPhoto').val();
                                var resume = $('#hdnResume').val();
                                var d, n, drphimage, drrrfile;
                                d = new Date();
                                n = d.getTime();
                                if (profilephoto == null || $.trim(profilephoto) == "" || $.trim(profilephoto).length < 1) {
                                }
                                else {
                                    $('#profile-photo').show();
                                    $('#profile-photo').empty();
                                    drphimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Photos/' + profilephoto + '?' + n;
                                    $('#profile-photo').append('<a href="' + drphimage + '" target="_blank">Image</a>');
                                    $('#drphoto').val(profilephoto);
                                }

                                if (signature == null || $.trim(signature) == "" || $.trim(signature).length < 1) {
                                }
                                else {

                                    $('#Sign-photo').empty();
                                    $('#Sign-photo').show();
                                    drloimage = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Sign/' + signature + '?' + n;
                                    $('#Sign-photo').append('<a href="' + drloimage + '" target="_blank">Image</a>');
                                    $('#drsign').val(signature);
                                }
                                if (resumefile == null || $.trim(resumefile) == " " || $.trim(resumefile).length < 1) {
                                }
                                else {
                                    $('#profile-resume').show();
                                    $('#profile-resume').empty();
                                    drrrfile = $('#hdnfilepath1').val() + $('#HiddenDoctorID').val() + '/Resume/' + resumefile + '?' + n;
                                    $('#profile-resume').append('<a href="' + drrrfile + '" target="_blank">Resume</a>');
                                    $('#drresume').val(resumefile);
                                }

                                $("#txtAge").val(data[i].age);
                                $("#txtdob").val(data[i].dob);
                                //}
                                //$('#branch').attr('disabled', true);
                                $('#SelRole').attr('disabled', true);
                                $('#SelDoctorDesignation').attr('disabled', true);
                                $('#Department').attr('disabled', true);
                                $('#SubDepartemnt').attr('disabled', true);
                                $('#txtPMobileNumber').attr('disabled', true);
                                $('#txtPEmailID').attr('disabled', true);
                                //$('#user_code').attr('disabled', true);
                                //$('#password').attr('disabled', true);
                                $('#user_code').val(data[i].user_code);
                                $('#password').val(data[i].doctor_user_password);
                                $("#txtdoctorid").val(data[i].doctor_id);
                                $("#txtDoctorName").val(data[i].doctor_name);
                                //load_salutation(data[i].salutation);
                                //$("#ddSalutation").val(data[i].salutation);
                                $("#txtNPI").val(data[i].national_provider_id);
                                $('#SelSpecialty').val(data[i].speciality);
                                $("#ddlGender").val(data[i].gender);
                                $("#ddlMart").val(data[i].marital_status);
                                $("#txtPMobileNumber").val(data[i].mobile_number_one);
                                $('#user_role_id').val(data[i].doctor_user_role_id);
                                $('#txtDisplayDesignation').val(data[i].display_designation);
                                $('#husermultibranch').val(data[i].usermultibranchid);
                                $('#ddUser_Floor').val(data[i].user_floorid);

                                $('#d_Service').val(data[i].service);
                                if (data[i].mobile_number_two == 0) {
                                    $("#txtPMobileNumber2").val('');
                                }
                                else {
                                    $("#txtPMobileNumber2").val(data[i].mobile_number_two);
                                }
                                $("#txtPEmailID").val(data[i].email_id);


                                branch(data[i].multibranch)
                                //var editbranch =branch(data[i].multibranch)
                                //$.when(editbranch)
                                //.then(function (data) {
                                load_salutation(data[i].salutation);
                                department(data[i].department, data[i].subdepartment);
                                Designation(data[i].doctor_user_designation);
                                LoadRole(data[i].doctor_user_role_id)
                                LoadServiceProvider(data[i].multibranch, data[i].hospital_id, data[i].service_provider_id)



                                if (parseInt(data[i].lab_user) > 0) {
                                    $('#SelProvider').attr('disabled', true);
                                    $('#PSelProvider').html('Service Provider assigned.Can\'t edit it');
                                }
                                if ($("#hiddenRole_id").val() == 2 || $("#hiddenRole_id").val() == 6) {
                                    $('#SelProvider').attr('disabled', false);
                                }
                                else {
                                    $('#SelProvider').attr('disabled', true);
                                }
                                if (parseFloat(data[i].total_experience) > 0) {

                                    $('#chkYes').attr("checked", "checked");

                                    if ($("#chkYes").is(":checked")) {
                                        $("#dvPassport").show();
                                    } else {
                                        $("#dvPassport").hide();
                                    }
                                }
                                else {
                                    $('#chkNo').attr("checked", "checked");
                                }



                            }
                        }

                    }
                }
                else {
                    console.log(data);
                }
            }
        });
    }
}
//For User Address Retrieve
function UserAddressValues() {
    debugger;
    var DoctorId = $('#DoctorUser_Id').val();
    var DoctorAddressData = {
        doctor_id: DoctorId,
        Dml_Indicator: 'UA'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/GetDoctorAddress',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorAddressData),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    if (data1.length > 0) {
                        $("#txtaddressid1").val(data1[0].doctor_address_id);
                        var is_permanent_address = data1[0].is_permanent_address;
                        if (is_permanent_address == 1) {
                            $('#ChkID').prop('checked', 'checked')
                        }
                        $("#txtLine1").val(data1[0].line_one);
                        $("#txtLine2").val(data1[0].line_two);
                        $("#txtPlace").val(data1[0].town_village);
                        $('#textpin_code').val(data1[0].pincode)
                        var country_id = data1[0].country;
                        var state_id = data1[0].state;
                        var city_id = data1[0].city
                        if (parseFloat(data1[0].is_permanent_address) > 0) {
                            $("#chkAdd").attr("checked", "checked");
                        }
                        else {
                            $("#chkAdd").attr("checked", "");
                        }
                        if (is_permanent_address == 1) {
                            $("#txtpLine1").val(data1[0].line_one).attr('readonly', true);
                            $("#txtpLine2").val(data1[0].line_two).attr('readonly', true);
                            $("#txtPPlace").val(data1[0].town_village).attr('readonly', true);
                            $("#textPpin_code").val(data1[0].pincode).attr('readonly', true);
                            $("#txtaddressid2").val(data1[1].doctor_address_id);
                            var PCountry = data1[0].country;
                            var PState = data1[0].state;
                            var PCity = data1[0].city;
                            Pgetstate(PCountry, PState);
                            Pgetcity(PState, PCity);
                        }
                        else {
                            $("#txtpLine1").val(data1[1].line_one).attr('readonly', true);
                            $("#txtpLine2").val(data1[1].line_two).attr('readonly', true);
                            $("#txtPPlace").val(data1[1].town_village).attr('readonly', true);
                            $("#textPpin_code").val(data1[0].pincode).attr('readonly', true);
                            $("#txtaddressid2").val(data1[1].doctor_address_id);
                            var PCountry = data1[1].country;
                            var PState = data1[1].state;
                            var PCity = data1[1].city;
                            Pgetstate(PCountry, PState);
                            Pgetcity(PState, PCity);
                        }
                        getstate(country_id, state_id);
                        getcity(state_id, city_id);
                    }
                    else {
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
    LoadDoctorUser();
}

$('#chkNo').change(function () {
    $('#txtConName').val('');
    $('#txtExpinMonth').val('');
    $('#tblExperience tbody').empty();
    $('#txtTotalExp').val('');
})

//load doctor type
function load_salutation(salutationid) {
    debugger;
    var load_type = {
        Dml_Indicator: "USL"
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/DoctorUpdation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load_type),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);

            if (data1.msg == "Success") {

                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    $('#ddSalutation').empty();
                    var Salutation = '';

                    var default_opt = '<option value="0">--Select--</option>';

                    $.each(data1, function (i, emp) {
                        Salutation = Salutation + '<option value="' + emp.Salutationid + '">' + emp.name + '</option>';
                    });
                    if (data1.length == 1) {
                        $('#ddSalutation').append(Salutation);
                    }
                    else {
                        $('#ddSalutation').append(default_opt + Salutation);
                    }

                    if (salutationid != null && salutationid != '') {
                        $('#ddSalutation').val(salutationid);
                    }
                }
            }

            else {
                console.log(data1);
            }
        }
    });
}
function Hideshow() {
    debugger;
    var show = {
        dml_indicator: 'HS'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(show),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            $('#hstatus').empty();
            $('#hvisit').empty();
            if (jQuery.isEmptyObject((data.data))) {
            }
            else {
                debugger;

                var status = data.data.Table[30].status;
                var visit = data.data.Table[30].master_vist_type_id;
                $('#hstatus').val(status);
                $('#hvisit').val(visit);
                if ($('#hstatus').val() == 0 && $('#hvisit').val() == 31) {
                    debugger;
                    $('#divSelProvider').hide();

                }

            }
        }
    });
}
// Changes Multi Branch
//$("#branch").change(function () {
//    debugger;
//    var subdept = $('#SubDepartemnt').val()
//    //document.getElementById("SubDepartemnt").innerHTML = null;
//    var condition = '(', dep_length = 0;

//    //$('#SubDepartemnt').prop('multiple', true);
//    dep_length = $('#branch').val().length;
//    for (var i = 0; i < dep_length; i++) {
//        condition = condition + 'master_category_department_id=' + $('#branch').val()[i]
//        if (i != (dep_length - 1)) {
//            condition = condition + ' or '
//        }
//    }
//    condition = condition + ') and status=1'
//    console.log(condition);
//});

function multi_branch(doctoruserid) {
    debugger
    //var insertbranch = {
    //    Dml_Indicator: 'UMB',
    //    branch_id: $('#branch').val().toString(),
    //    hospital_id: $('#HospitalId').val(),
    //    user_role_id: $('#SelRole').val(),
    //    doctor_id: $('#myHiddenVar').val(),
    //    user_created_by: $('#hiddenUser_id').val()
    //}
    var BranchArray = new Array();

    for (j = 0; j < $('#branch').val().length; j++) {
        var Obj = {};

        Obj.Dml_Indicator = 'UMB';
        Obj.branch_id = $('#branch').val()[j];
        Obj.hospital_id = $('#HospitalId').val();
        Obj.user_role_id = $('#SelRole').val();
        Obj.doctor_id = $('#myHiddenVar').val()
        Obj.user_created_by = $('#hiddenUser_id').val()
        Obj.doctoruser_id = doctoruserid
        BranchArray.push(Obj);
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: '/DoctorProfile/UserMultiBranch',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(BranchArray),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {

                }
            }
            else {
                console.log(data1);
            }

        }
    });
}

function LoadFloor() {
    var Warddata = {
        Dml_Indicator: "WFS",
        //branch_id: $('#ip_branch_id').val()//Medway all branch concept
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Ipmanagement/WardManagement_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Warddata),
        success: function (data) {
            $('#ddUser_Floor').empty();
            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var Floor;


                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {

                        Floor = Floor + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';

                    });
                    $('#ddUser_Floor').append(default_opt + Floor);
                }
                else {
                    console.log(data);
                }
            }
        }
    });
}

