

var PERCENTAGE_THRESHOLD = 60;

var submit_project_button_clicked = function(project_id)
{
    $.post("API.php", {project: "PRE_CHECK_SUBMIT_REVIEW", project_id: project_id, submit_type: "self_certification" }, function(data)
    {
        var missing_profile_items = get_missing_project_profile_items(data.project_dict);
        if ( missing_profile_items.length != 0 )
        {
            //alert("Project profile check failed",missing_profile_items);
            launch_project_profile_check_failure_dialog(project_id,missing_profile_items);
        }
        else if (data.success === false)
        {
            alert("Project requirement Failed");
            // show_error_popup_on_submit(data);
        }
        else
        {
            // show submit project modal
            // launch_project_submit_dialog(project_id);
            // removing the popup dialog and directly redirecting to the payment page
            var target = "payments.php?p=" + project_id;
            window.location.href = target;
        }                
    },"json");
}

var launch_project_profile_check_failure_dialog = function (project_id, missing_profile_items)
{
    var modal = $("#id-modal-project-profile-check")
    $(modal).attr("data-project-id", project_id );
    $(modal).modal('show');
    show_missing_profile_items(modal,missing_profile_items);
};

var show_missing_profile_items = function(modal,missing_profile_items)
{
    var list_control = $(modal).find("#id-ul-unchecked-required-project-profile-items");
    list_control.empty();
    var index = 0;
    for (index=0;index<missing_profile_items.length;index++)
    {
        list_control.append("<li>"+missing_profile_items[index]+"</li>");
    }
}

$("#id-btn-project-profile-check-update").click(function()
{
    var target = "createProject.php?edit=true&p=" + $("#id-modal-project-profile-check").attr("data-project-id");
    window.location.href = target;
});


var get_missing_project_profile_items = function(project)
{
    var missing_profile_items = [];

    if (project.project_title == "")
        missing_profile_items.push("Title");
    
    if (project.project_contact_person == "")
        missing_profile_items.push("Contact Person");
    
    if (project.project_contact_email == "")
        missing_profile_items.push("Contact Email");

    if (project.project_contact_telephone == "")
        missing_profile_items.push("Contact Telephone");

    if (project.project_architect == "")
        missing_profile_items.push("Architect");

    if (project.project_owner == "")
        missing_profile_items.push("Owner");

    if (project.project_address_line_1 == "")
        missing_profile_items.push("Project Address");

    if (project.project_city == "")
        missing_profile_items.push("City");
    
    if (project.project_state == "")
        missing_profile_items.push("State");
        
    if (project.project_country == "")
        missing_profile_items.push("Country");

    if (project.project_zipcode == "")
        missing_profile_items.push("Zipcode");

    return missing_profile_items;
}

;