var total_cost; 
var building_area;
var site_area;
var params;
var project_certification_cost;
$(document).ready(function(){
    // building_type = $("#UD-payments-buildingtype");
    // building_area = $("#UD-payments-buildingarea");
    // site_area = $("#UD-payments-sitearea");

    var url = window.location.search.split("=");
    var projectId = url[2];
    alert(projectId);
    // params={};window.location.search.replace(/[?&;#]+([^=&]+)=([^&]*)/gi,function(str,key,value){params[key] = value;});

    // if(params.redir){
    //     redir_addr = params.redir;
    // }

    /*fetching values for project summary from database*/
    if(projectId){
         $.post("API.php",{payments: "GET_BILLING", id: projectId}, function(data){
            alert(JSON.stringify(data));
            populate_fields(data);
        }, "json");

        $.post("API.php",{payments: "GET_BILLING_INFO", id: projectId}, function(data){
             alert(JSON.stringify(data));
            populate_billing_info(data);
        }, "json");
    }
});


/*populate project summary fields*/
function populate_fields(data){
    if(data == null || data[0] == null)
        return;

    data = data[0];
  
    $("#id-project-name").html(data.project_title);
    $("#id-building-area").html(data.habitable_floor_area+" sq. ft ");
    $("#id-site-area").html(data.site_area+" sq. ft ");

    var buildArea = parseInt(data.habitable_floor_area);
    var siteArea = parseInt(data.site_area); 
    var precertification = data.pre_certification;
    displayCalculations(buildArea, siteArea, precertification);
}

function populate_billing_info(data){
    if(data == null)
        return;
    
    //alert(JSON.stringify(data));
    // data = JSON.parse(data);
    // alert(data.first_name);
  
    $("#id-fname").html(data.first_name);
    $("#id-lname").html(data.last_name);
    $("#id-addr").html(data.address);
    $("#id-city").html(data.city);
    $("#id-state").html(data.state);
    $("#id-zip").html(data.zip);
    $("#id-country").html(data.country);
    $("#id-email").html(data.email);
    $("#id-phno").html(data.phone);
}

//Calculator function
function displayCalculations(buildArea, siteArea, precertification){
    var checked = precertification.split(",");
    checked.pop();
    var buildingamt = 0;
    var siteamt = 0;

    var precertbuildingamt = 0;
    var precertsiteamt = 0;
    var onsitebuildingamt = 0;
    var onsitesiteamt = 0;
    
    if(buildArea > 1 && buildArea <= 100000){
        buildingamt += 4000;
    }
    else if(buildArea > 100000){
        buildingamt += 4000;
        buildadds = (buildArea - 100000) / 10000;
        //alert(adds);
        buildingamt += 200 * Math.ceil(buildadds);
    }
    
    if(siteArea >= 3 && siteArea <= 20){
        siteamt += 1000;
    }
    else if(siteArea > 20){
        siteamt += 1000;
        siteadds = (siteArea - 20);
        //alert(siteadds);
        siteamt += 40 * (siteadds);
    }

    if(checked.length == 0){
        precertbuildingamt = 0;
        precertsiteamt = 0;
        onsitebuildingamt = 0;
        onsitesiteamt = 0;
    }
    else if(checked.length == 1)
    {
        if(checked.includes("1")){
            // alert("Inside 1");
            var container = document.getElementById("pre-cert-div");
            container.style.display = "block";
            precertbuildingamt = buildingamt/2;
            precertsiteamt = siteamt/2; 
            onsitebuildingamt = 0;
            onsitesiteamt = 0;
            $('#id-precert-buidling').html('$ '+precertbuildingamt);
            $('#id-precert-site').html('$ '+precertsiteamt);
            $('#precertcontainer').hide();
        }
        else if(checked.includes("2")){
            // alert("Inside 2");
            var container = document.getElementById("on-site-div");
            container.style.display = "block";
            precertbuildingamt = 0;
            precertsiteamt = 0; 
            onsitebuildingamt = buildingamt;
            onsitesiteamt = siteamt;
            $('#id-onsite-buidling').html('$ '+onsitebuildingamt);
            $('#id-onsite-site').html('$ '+onsitesiteamt);
            $('#onsitecontainer').hide();
        }   
    } else{
        // alert("Inside 3");
        var container = document.getElementById("pre-cert-div");
        container.style.display = "block";
        var container1 = document.getElementById("on-site-div");
        container1.style.display = "block";
        precertbuildingamt = buildingamt/2;
        precertsiteamt = siteamt/2; 
        onsitebuildingamt = buildingamt;
        onsitesiteamt = siteamt;
        $('#id-onsite-buidling').html('$ '+onsitebuildingamt);
        $('#id-onsite-site').html('$ '+onsitesiteamt);
        $('#id-onsite-total').html('$ '+(onsitebuildingamt+onsitesiteamt));
        $('#id-precert-buidling').html('$ '+precertbuildingamt);
        $('#id-precert-site').html('$ '+precertsiteamt);
        $('#id-precert-total').html('$ '+(precertbuildingamt+precertsiteamt));
        var x = document.getElementById("onsitecontainer");
        x.style.background = "#f9f9f9";
        var y= document.getElementById("precertcontainer");
        y.style.background = "#f9f9f9";
    }

    var totalamt = onsitebuildingamt+onsitesiteamt+precertbuildingamt+precertsiteamt;
    $('#id-total-est').html('$ '+totalamt);
    return totalamt;
    
};