var base_url = 'https://rxhealthbeta.com/prosper/GAB_NEW/';


function current_show() {
    $(".fakeloader").css("visibility", "visible");
}

function current_opac() {
    $(".fakeloader").css("visibility", "hidden");
}

function errormsg(action) {
    errors = "";

    if (action == "add_medication") {
        if (!document.getElementById('drugname').value) {
            errors = errors + "Drug name is Required\n";
            document.getElementById('drugname').style.border = "solid red 1px";
        } else {
            document.getElementById('drugname').style.border = "";
        }
    }
}

function getstep2(act, id, id2) {
    //alert(base_url);
    current_show();
    $("#loading_container").show();
    $.ajax({
        type: "POST",
        url: base_url + 'getstep.php',
        data: $('form').serialize() + "&act=" + act + "&id=" + id + "&idd2=" + id2,
        success: function (msg) {
            var resp = id.split("@@");
             var title = resp[2];
            if (act == "all_providers" || act == "pharmacy" || act == "hospital" || act == "laboratory" || act == "optical" || act == "dental" || act == "specialist" || act == "clinic" || act == "provider_details") {
             $("#"+id2).html(msg);
             $("#facility_name").html(title);
         }else if(act == "searchFacility"){
                $("#main-content").html('');
                $("#main-content").html(msg); 

            } else if (act == "searchByDrugType") {
                $("#main-content-drug").html('');
                $("#main-content-drug").html(msg); 
            }else if (act == "add_new_drug"){
                $("#main-content-drug").html('');
                $("#main-content-drug").html(msg);
            }else if (act == "edit_medication"){
                $("#main_content_medication").html('');
                $("#main_content_medication").html(msg);
            }else if(act == "view_allergies"){
                $("#main_content_allergies").html('');
                $("#main_content_allergies").html(msg);
            }else if (act == "add_new_allergy"){
                $("#main-content-allergy").html('');
                $("#main-content-allergy").html(msg);
            }else{
            $("#client_area").html(msg);
        }

            $("#loading_container").hide();
    }
});
}

function getstepInsert(act, id, idd2) {
    var formData = new FormData($("#"+act)[0]);
    formData.append('act', act);
    formData.append('id', id);
    formData.append('idd2', idd2);
    //alert(act);
    errormsg(act);

    if (act == "add_medication" ){
        $("#kt_modal_7").modal("hide");
    }

    if (act == "update_medication"){
        $("#kt_modal_7_edit").modal("hide");
    }

    if (act == "add_allergies"){
        $("#kt_modal_7_allergy123").modal("hide");
    }
    if ((errors == "<br>") || (!errors)) {
        //alert(formData);
        current_show();
        $("#loading_container").show();
        $.ajax({
            type: "POST",
            url: base_url + 'getstep.php',
            data: formData,
            cache:false,
            contentType: false,
            processData: false,
            success: function (msg) {
                $("#loading_container").hide();
                $("#currentarea").html(msg);

            }
        });
    }else{
        errors = 'Please Correct the following errors\n --\n' + errors
        if(act == "saveloanapplication" || act == "saveloanapplicationprosper" || act == "saveloanapplicationpentecost"){
            swal.fire("Error!", 'Fields marked with red are required, and make sure PRINCIPAL amount is between the minimum and maximum amount', "error");
        }else{
            swal.fire("Error!", 'Fields marked with red are required, and Please select an image', "error");
        }

        errors = null;
        return false;
    }
}

function getstepDelete(act, id, id2) {
    if (act == 'deleteMedication' || act == 'deleteAllergy') {
        Swal.fire({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            icon: 'warning',
            type: 'info',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'Yes, delete it!'
        }).then(function (result) {
            if (result.value) {
                $.ajax({
                    type: "POST",
                    url: base_url + 'getstep.php',
                    data: $('form').serialize() + "&act=" + act + "&id=" + id + "&idd2=" + id2,
                    success: function (msg) {
                        $("#loading_container").hide();
                        $("#currentarea").html(msg);
                    }
                });
                Swal.fire(
                    'Deleted!',
                    'Your file has been deleted.',
                    'success'
                )
            } else {
                Swal.fire(
                    'Deleted!',
                    'File not Deleted',
                    'error'
                )
            }
        })
    }
}

function display_yes(value){
    if (value == "Yes"){
        document.getElementById("display_if_yes").style.display = 'Block';
    }else{
        document.getElementById("display_if_yes").style.display = 'None';
    }
}

function question_six_a(value){
    if (value == "Yes"){
        document.getElementById("question_six_a_display").style.display = 'Block';
    }else{
        document.getElementById("question_six_a_display").style.display = 'none';
    }
}

