function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

// Add Record
function addRecord() {
    // get values
	var clinica = getParameterByName('clinica');
	var mio = getParameterByName('mio');
    var first_name = $("#first_name").val();
    var last_name = $("#last_name").val();
    var dni = $("#dni").val();
    var email = $("#email").val();
    var doc = $("#doc").val();

    // Add record
    $.post("ajax/addRecord.php", {
        first_name: first_name,
        last_name: last_name,
        dni: dni,
        email: email,
        doc: doc,
        clinica: clinica,
    }, function (data, status) {
        // close the popup
        $("#add_new_record_modal").modal("hide");
 window.location="pacientes.php?mio="+mio+"&clinica="+clinica+"&doc="+doc;
        // read records again
        readRecords();

        // clear fields from the popup
        $("#first_name").val("");
        $("#last_name").val("");
        $("#dni").val("");
        $("#email").val("");
        $("#doc").val("");
    });
}

function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
    return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

// READ records
function readRecords() {
	var mio = getParameterByName('mio');	
	var clin = getParameterByName('clinica');
	var doc = getParameterByName('doc');
    if(mio==1)
	$.get("ajax/readRecords.php?clinica="+clin+"&doc="+doc, {}, function (data, status) {
        $(".records_content").html(data);
    });
	else
	$.get("../../ajax4/crud2/ajax/readRecords1.php?clinica="+clin, {}, function (data, status) {
        $(".records_content").html(data);
    });
}


function DeleteUser(id) {
    var conf = confirm("Are you sure, do you really want to delete User?");
    if (conf == true) {
        $.post("ajax/deleteUser.php", {
                id: id
            },
            function (data, status) {
                // reload Users by using readRecords();
                readRecords();
            }
        );
    }
}

function GetUserDetails(id) {
    // Add User ID to the hidden field for furture usage
    $("#hidden_user_id").val(id);
    $.post("ajax/readUserDetails.php", {
            id: id
        },
        function (data, status) {
            // PARSE json data
            var user = JSON.parse(data);
            // Assing existing values to the modal popup fields
            $("#update_first_name").val(user.paciente);
            $("#update_last_name").val(user.nombre);
            $("#update_dni").val(user.dni);
            $("#update_email").val(user.email);
            $("#update_doc").val(user.usuario);
        }
    );
    // Open modal popup
    $("#update_user_modal").modal("show");
}

function UpdateUserDetails() {
    // get values
    var first_name = $("#update_first_name").val();
    var last_name = $("#update_last_name").val();
    var dni = $("#update_dni").val();
    var email = $("#update_email").val();
    var doc = $("#update_doc").val();

    // get hidden field value
    var id = $("#hidden_user_id").val();

    // Update the details by requesting to the server using ajax
    $.post("ajax/updateUserDetails.php", {
            id: id,
            first_name: first_name,
            last_name: last_name,
            dni: dni,
            email: email,
            doc: doc,
        },
        function (data, status) {
            // hide modal popup
            $("#update_user_modal").modal("hide");
            // reload Users by using readRecords();
            readRecords();
        }
    );
}

$(document).ready(function () {
    // READ recods on page load
    readRecords(); // calling function
});