function setState() {
    $('.gradient_blue').css('display', 'none');
    $(document).ready(function() {
        $('.hidden').hide();
        //Update the Label of the forms by replacing the text used for metrics
        $("li .description").each(function(index) {
            var labelFull = $(this).html();
            if (labelFull.indexOf("{{") != -1) {
                var reg = /(\{{.*?\}})/gi;
                var str = labelFull;
                var firstlabel = str.replace(reg, "");
                $(this).html(firstlabel);
            }
        });
        //======================End here=======================
        var formId = getParameterByName('id');
        var entryId = getParameterByName('entryId');
        var uid = getParameterByName('uid');
        var getForm = getParameterByName('getForm');
        var getEntry = getParameterByName('getEntry');
        var type = getParameterByName('type');
        var scheduleDetails = JSON.parse(getParameterByName('scheduleDetails'));
        var populateData = JSON.parse(getParameterByName('populateData'));
        if (uid) {
            $('.uniqueid input').val(uid);
        }
        if(populateData || scheduleDetails){
            let data = populateData ? populateData :scheduleDetails;
            data.forEach(element => {
                
                if (element.type == "date") {
                    var dateobj = element.value.split('-');
                    $('#element_' + element.mapField + '_datepick').datepick('setDate', $.datepick.newDate(dateobj[0], dateobj[1], dateobj[2]));
                }
                // if(element.type == "simple_name"){
                //     var name = element.value.split(' ');
                //     $("#element_" + element.mapField + "_1").val(name[0]);
                //     $("#element_" + element.mapField + "_2").val(name[1]);
                // }
                if (element.type == "phone") {
                    var ph = element.value;
                    if(ph) {
                        $("#element_" + element.mapField + "_1").val(ph.slice(0, 3));
                        $("#element_" + element.mapField + "_2").val(ph.slice(3, 6));
                        $("#element_" + element.mapField + "_3").val(ph.slice(6, 10));
                    }
                }
                if (element.type == "radio") {
                    $(":radio[name='element_" + element.mapField + "'][value='" + element.value + "']").attr('checked', 'checked');
                }
                if (element.type == "time") {
                    if (element.value) {
                        var timeobj = element.value.split(':');
                        $("#element_" + key + "_1").val(timeobj[0]);
                        $("#element_" + key + "_2").val(timeobj[1]);
                    }
                }
                if (element.type == "checkbox") {
                    $(":checkbox[id='element_" + key + "'][value='1']").attr('checked', 'checked');
                }
                if(element.linkField == 'email' && element.value != '' && element.value != undefined) {
                    console.log('email', element.value);
                    if(element.patientStatus && element.patientStatus == 'Enrolled') {
                        $('#element_' + element.mapField).val(element.value);
                    } else {
                        if(element.value.startsWith("unknown_") == false && ValidateEmail(element.value) == true) {
                            $('#element_' + element.mapField).val(element.value);
                        }
                    }
                } else {
                    $('#element_' + element.mapField).val(element.value);
                }
            });
        }
        if (type == 'visit') {
            var schedule = getParameterByName('schedule');
            console.log(schedule);
            if (schedule && schedule != '') {
                var d = new Date(Number(schedule) * 1000);
                $('#element_33_datepick').datepick('setDate', $.datepick.newDate(d.getFullYear(), (d.getMonth() + 1), d.getDate()));
            }
        }
        if(type == 'new'){
            console.log('type', type);
            if (uid) {
                $('.uniqueid input').val(uid);
            }
        }
    });
}
function ValidateEmail(mail) 
{
 if (/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(mail)){
    return true;
  } else{
    return false;
  }
    
}
function updatedLabel() {
    $('.gradient_blue').css('display', 'none');
    $(document).ready(function() {
        $('.hidden').hide();
        //Update the Label of the forms by replacing the text used for metrics
        $("li .description").each(function(index) {
            var labelFull = $(this).html();
            if (labelFull.indexOf("{{") != -1) {
                var reg = /(\{{.*?\}})/gi;
                var str = labelFull;
                var firstlabel = str.replace(reg, "");
                $(this).html(firstlabel);
            }
        });
        //======================End here=======================
    });
}

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}
if (!getParameterByName('entry_id')) {
    setState();
} else {
    updatedLabel();
}