

function getUniqueUrl() {
    var curUrl = window.location.href;
    var totalUrl = curUrl.split("/");
    var totalUrlLength = totalUrl.length - 1;
    var curUniqueUrl = totalUrl[totalUrlLength];
    return curUniqueUrl;
}

function checkAdvance(extras) {
    var Advance = false;
    if (extras != null) {
        var extrasArray = extras.split("|");
        var extrasValue = extrasArray[0];
        if (parseFloat(extrasValue) == 2) {
            Advance = true;
        }
    }
    return Advance;
}

function checkExtraGroupExpense(extras, ExpenseTitle) {
    var result = 99;
    var extrasValue = 0;
    if (extras != null) {
        var extrasArray = extras.split("|");
        extrasValue = extrasArray[0];
    }
    if (parseFloat(extrasValue) == 1 || ExpenseTitle == 'Balance Expense') {
        result = 1;
    } else if (parseFloat(extrasValue) == 2) {
        result = 2;
    }

    return result;
}

function validateEmail(email) {

    var errorMessage = "";

    var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    if (!email.trim()) {
        errorMessage = "Please provide email.";
    }

    else if (!emailRegex.test(email.trim())) {
        errorMessage = "Please enter a valid email address.";
    }

    return errorMessage;

}

function sendToServer(formData, formName,callback) {
    var formUrl = $(formName).attr('action');
    formData.unique_url = getUniqueUrl();
    $.ajax({
        type: 'POST',
        url: formUrl,
        data: formData,
        success: function (data, textStatus, xhr) {
            callback(true,data);
        },
        error: function (xhr, textStatus, error) {
            callback(false,error);
        }
    });

}

function Permission(data) {
    this.id = data.id;
    this.name = data.name;
    this.email = data.email;
    this.permission = data.permission;
    this.permission_name = data.permission_name;
    this.expense_id = data.expense_id;
    this.operation_type = data?.operation_type || '';
}
function setCookie(participantId) {
    $.post(baseUrl+"/Expense/checkCookie",
    {
       participantId: participantId,
       unique_url: getUniqueUrl()
    },
    function(data, status){
     // alert("Data: " + data + "\nStatus: " + status);
    });
}

function getCookie() {
    $.get(baseUrl + "/Expense/isExistCookie",
        {
            unique_url: getUniqueUrl()
        },
        function (data, status) {
            //alert("Data: " + data + "\nStatus: " + status);
            if (data == " ") {
                if(session_profile_id != '') {
                    let agreeStatus = window.confirm('Do you want to add this group?');
                    if(agreeStatus) {
                        setCookie(" ");
                    }
                } else {
                    setCookie(" ");
                }
            }
        });
}
getCookie();
