// num_suffix
// setLoader
// remv_loader
// postRequest 
// throwSweetalert 
// getKeyByValue
// return_element_key_in_array
// ajaxRequest

function num_suffix(i) {
    var j = i % 10,
        k = i % 100;
    if (j == 1 && k != 11) {
        return i + "st";
    }
    if (j == 2 && k != 12) {
        return i + "nd";
    }
    if (j == 3 && k != 13) {
        return i + "rd";
    }
    return i + "th";
}
function set_form_data(object) {
    let form_data = new FormData();
    object.forEach(e => {
        form_data.append(e.name,e.value);
    }); 
    return form_data;
}

function setLoader(){
    // $('.loader').attr('style', 'display:block');
    // setTimeout(() => {
        $('.loader').show();
        $(".loader").animate({opacity: '1'});
    // }, 1000);
    
}

function remvLoader(){
    $(".loader").animate({opacity: '0.2'});
    setTimeout(() => {
        $('.loader').removeAttr('style');
    }, 500);
    
}

// former postRequest funtion was having a problem with formData
function ajaxRequest(url,data) {

    return new Promise(function (resolve,reject) {

        $.ajax({
            type: "post",
            url: url,
            data: data, 
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (response) {
                resolve(response);
            }
        });
    })
}


function postRequest(url, params){
    setLoader();
    return new Promise(function (resolve, reject) {

        $.post(url, params, function (data, status) {

            if(status === 'success'){
                remvLoader();
                resolve(data)
            }else{
                remvLoader();
                reject(status)
            }
        })

    })
}
function throwSweetalert(returned,page_redirect,allow_redirect){
    if(returned.error == 1){
        swal('Sorry',returned.msg,'error');
    }else if(returned.error == 0){
        if(allow_redirect == 1){
            swal('Congratulations',returned.msg,'success');
            setTimeout(() => {
                window.location.href = page_redirect;
            }, 3000);
        }
    }
}
function getObjKeyByValue(object, value) { 
    for (var prop in object) { 
        if (object.hasOwnProperty(prop)) { 
            if (object[prop] === value) 
            return prop; 
        } 
    } 
}
function return_element_key_in_array(array,val_exist) {
    for (let i = 0; i < array.length; i++) {
        if(array[i] == val_exist){
            return i;
        }else{
            return 0;
        }
    }

} 