<h3 class="lead"><span class="text-muted">New Location for</span> <strong><?= $_REQUEST['clientname']?></strong></h3>
<div class="row">
    <div class="col text-end mb-2"><a href="javascript:void(0)" class="btn btn-secondary btn-sm" onClick="loadInfo('<?= $_REQUEST['clientnumber']?>')"><i class="fad fa-cloud-download" aria-hidden="true"></i> Load Primary Address</a></div>
</div>
<form id="locationForm" class="form-horizontal">
    <fieldset>
        <?php require_once(ROOT_URL.'/forms/elements/location_form.php')?>
        <div class="spacer"></div>
        <div class="col-12text-center">
            <div class="form-group row">
                <input type="hidden" name="clientnumber" value="<?= $_REQUEST['clientnumber']?>">
                <input type="hidden" name="FUNCTION" value="insert">
                <input type="hidden" name="tenant" value="<?=TENANT_URL?>">
    </fieldset>
    <input id="reset_form" class="btn btn-default" type="reset" value="Reset Form" style="display:none" />
    <input id="btn_save" class="btn btn-info" type="submit" name="submit" value="Submit" style="display:none" />
    </div>
    </div>
</form>
<script>
document.querySelector("#locationForm input[type='submit']").addEventListener('click', function(event) {
    event.preventDefault();

    var xhr = new XMLHttpRequest();
    var form = document.getElementById('locationForm');
    var formData = new FormData(form); // FormData handles serialization of the form

    xhr.open('POST', ROOT_URL + '/includes/locations/location_functions.php', true);

    // Set the callback function for when the request completes
    xhr.onload = function() {
        if (xhr.status === 200) {
            var result = xhr.responseText;
            <?php if($_REQUEST['ref'] == "asset") { ?>
            window.location.href = 'index.php?t=includes/master&p=client.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&view=asset';
            <?php } else { ?>
            window.location.href = 'index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&clientname=<?= addslashes($_REQUEST['clientname']) ?>';
            <?php } ?>
        }
    };

    // Send the FormData object directly without converting it to a string
    xhr.send(formData);
});

$(document).ready(function() {
    document.getElementById('navBox').innerHTML =
        '<li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&view=list">Client List</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=client.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&clientname=<?= addslashes($_REQUEST['clientname']) ?>&view=asset"><?= $_REQUEST['clientname'] ?></a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item"><a href="index.php?t=includes/master&p=locations.php&clientnumber=<?= $_REQUEST['clientnumber'] ?>&clientname=<?= addslashes($_REQUEST['clientname']) ?>">Locations</a><i class="fad fa-angle-double-right mx-2 mt-1"" aria-hidden="true"></i></li><li class="breadcrumb-item active">New Location</li>'

    $('#footerControls').html('<button class="btn btn-secondary" onclick="triggerAction(\'btn_save\')">SAVE</button>')
})
</script>