(function(mainApp) { mainApp(window.jQuery, window, document); }(function($, window, document) { var saveTool = $('#save_tool'); var saveResults = $('#save_results'); var saveInquiry = $('#save_inquiry'); var inquiryForm = saveInquiry.find('form.form'); var jobTypesA = [ "Automatic sprinkler systems", "Bridges", "Ceilings", "Clearing and grubbing", "Culverts", "Curbing", "Dredging, hydraulic only", "Elevators and escalators", "Fences and railings", "Glazing contracts", "Grading", "Guttering", "Highways, streets, alleys and toads", "Janitorial service", "Millwork", "Other (not otherwise classified) machinery made to special order or design", "Overpasses", "Playground", "Rip rap", "Sidewalks", "Siding", "Soil conservation or erosion control", "Tanks", "Tower construction", "Underpass", "Viaducts", "Waterproofing" ]; var jobTypesA1 = [ "Alarm systems", "Bituminous resurfacing contract", "Boiler repair and retubing", "Computer equipment", "Fire escapes", "Gravel, spreading", "Guardrails", "Insulation", "Iron or bronze, ornamental", "Mosquito control contract", "Parking meters", "Pipelines, above ground", "Police alarm systems", "Radio towers", "Revolving doors", "Seeding coal handling machinery", "Signal systems", "Signs", "Signs, highway", "Sound systems", "Street lighting", "Supply and installation only", "Telephone equipment", "Thermostat equipment", "Traffic control systems", "Vacuum equipment", "Water tanks and towers", "Weather stripping", "Window cleaning" ]; var jobTypesB = [ "Air Conditioning Systems", "Aqueducts", "Asbestos", "Boring - Horizontal", "Buildings of all kinds, erection and repair", "Buildings of all kinds, renovation", "Burial vaults or mausoleums", "Cable - Underground or Buried", "Canals and Canal Linings", "Carpentry", "Concrete Work - not otherwise rated", "Dams and spillways", "Dikes, levees & revetments", "Ditches, including drainage", "Docks", "Drilling", "Drywall", "Electrical", "Embankments", "Erosion control", "Excavations", "Filter plants", "Floors", "Foundations", "Fountains", "Gas mains and laterals, with trenching", "Gas piping", "Golf courses", "Gunite Contracts", "Heating Systems", "Incinerators", "Insulation", "Jetties", "Landscaping", "Locks and Lock Gates", "Masonry", "Other (not otherwise classified)", "Painting", "Piers", "Piling", "Pipelines - underground", "Plastering", "Plumbing", "Pre-manufactured buildings", "Railroads, including grading, ballasting, track laying", "Reservoirs", "Sandblasting", "Sculpture", "Septic tanks", "Sewer disposal plants", "Sewers (sanitary or storm)", "Sheet Metal", "Solar Panels", "Spillways", "Steel, erection and/ or fabrication", "Swimming pools", "Test boring", "Tile and Terrazzo", "Transmission lines", "Tunnels, including subways", "Underground storage tanks", "Ventilation Systems", "Waterlines", "Waterworks", "Wells", "Wharves" ]; var jobTypesD = [ "Design build" ]; var jobTypesS = [ "Supply" ]; var jobTypes = { A: jobTypesA, A1: jobTypesA1, B: jobTypesB, D: jobTypesD, S: jobTypesS } var rateTable = { A: { standard: [15.00, 10.00, 7.00, 5.50, 5.00, 4.50], select: [10.80, 10.80, 6.70, 5.30, 4.90, 4.40], preferred: [8.60, 8.60, 5.70, 5.00, 4.60, 4.30], merit: [7.20, 7.20, 4.80, 4.20, 3.85, 3.60] }, A1: { standard: [9.40, 7.20, 6.00, 5.00, 4.50, 4.00], select: [7.20, 7.20, 6.00, 5.00, 4.50, 4.40], preferred: [5.40, 5.40, 4.50, 4.00, 3.70, 3.40], merit: [4.50, 4.50, 4.50, 3.90, 3.60, 3.30] }, B: { standard: [25.00, 15.00, 10.00, 7.50, 7.00, 6.50], select: [14.40, 14.40, 8.70, 6.90, 6.30, 5.70], preferred: [10.80, 10.80, 7.50, 6.40, 5.70, 5.70], merit: [9.00, 9.00, 6.30, 5.40, 4.80, 4.80] }, D: { standard: [25.00, 20.00, 15.00, 12.00, 11.00, 10.00], select: [21.60, 21.60, 13.20, 10.50, 9.60, 8.60], preferred: [17.20, 17.20, 10.40, 8.20, 7.50, 6.80], merit: [14.40, 14.40, 8.70, 6.90, 6.30, 5.70] }, S: { standard: [4.30, 4.30, 3.50, 3.50, 3.50, 3.50], select: [3.40, 3.40, 2.80, 2.80, 2.80, 2.80], preferred: [2.80, 2.80, 2.40, 2.40, 2.40, 2.40], merit: [2.00, 2.00, 2.00, 2.00, 2.00, 2.00] } }; var qualifyingFactorFields = [ ".Qualifying_Factors_Three_References", ".Qualifying_Factors_On_Time_Completion", ".Qualifying_Factors_Three_Years_Experience", ".Qualifying_Factors_Vendors_Paid", ".Qualifying_Factors_Continuity_of_Ownership", ".Qualifying_Factors_CPA_Prepared", ".Qualifying_Factors_Common_Geographic_Area", ".Qualifying_Factors_No_Outside_Investments", ".Qualifying_Factors_Sufficient_Equipment", ".Qualifying_Factors_Working_Capital_Line", ".Qualifying_Factors_Worth_Quarter_Liquid_Assets", ".Qualifying_Factors_Liabilities_No_More_3x", ".Qualifying_Factors_Interim_Regular", ".Qualifying_Factors_Business_5_Years", ".Qualifying_Factors_Banking_3_Years", ".Qualifying_Factors_Profit_2_Years" ]; var tierLevels = [ 100000, 400000, 2000000, 2500000, 2500000, Number.MAX_VALUE ]; var optionTexts = [ "Company can obtain three references from past clients", "Company has a history of on-time completion without liquidated damages", "Company has at least three years of prior experience in the work performed", "All vendors are paid according to their terms", "Continuity of ownership, management and key personnel is in place", "Company has CPA prepared year end financial statements", "At least 75% of company's work is in a common geographic area", "No outside investments or businesses unrelated to construction exist", "Sufficient equipment and capital assets to complete the work is on hand", "A working capital line of credit is in place but unused", "Net worth of owners is comprised of at least 25% liquid assets", "Company's total liabilities are no more than three times net worth", "Interim financial and work on hand reports are regularly produced", "Company has been in business for over five years", "Company's banking relationship has been established for at least three years", "Company has reported a profit in two out of three prior years" ]; $(function() { // Setup generateJobSelect(); generateOptions(); // Initialize AutoNumeric for currency fields saveTool.find('#save_bond_value').autoNumeric("init", {aSign: "$", mDec: 0}); saveTool.find('#save_bond_cost').autoNumeric("init", {aSign: "$", mDec: 0}); saveResults.find('.savings').autoNumeric("init", {aSign: "$", mDec: 0}); /* Load the following *after* the form has been rendered */ // Hide system-filled fields and qualifying factors (4 + 16) saveResults.hide() inquiryForm.find(".form-field").slice(4, 24).hide(); // Initialize AutoNumeric for currency fields inquiryForm.find('.Last_Bonded_Contract_Value input').autoNumeric("init", {mDec: 0, aSep: ''}); inquiryForm.find('.Last_Bond_Cost input').autoNumeric("init", {mDec: 0, aSep: ''}); inquiryForm.find('.Last_Bond_Direct_Surety_Savings input').autoNumeric("init", {aSign: "$", mDec: 0}); $('form#pardot-form p.form-field').css('vertical-align','top'); $('form#pardot-form .email input').each(function(i) { var input_id = $(this).attr('id'); $('#error_for_' + input_id).remove(); $(this).after(''); }); // Calculate //saveInquiry.find('.submit input').submit(function(event){ saveInquiry.find('form').submit(function(event) { // Disable default posting event.preventDefault(); var validated_fields = validateFields(); var validated_form = validateForm(); if (validated_fields && validated_form) { calculate(); // For reactiveness, display before ajax call saveInquiry.hide(); saveResults.show(); $.ajax({ type: "POST", url: $(this).attr('action'), data: $(this).serialize(), /*success: function(response) { // },*/ error: function(jqXHR, textStatus, errorThrown) { console.log("Error occurred: " + textStatus + ", " + errorThrown); } }); } else if (!validated_fields) { var topOffset = saveTool.find("#save_fields").offset().top - 115; // 115 header offset $('html, body').animate({scrollTop: topOffset}, 500); } }); /* saveInquiry.find('.submit input').click(function(event) { // For testing //event.preventDefault(); if (validateFields()) { calculate(); //saveInquiry.hide(); saveResults.show(); } else { event.preventDefault(); var topOffset = saveTool.find("#save_fields").offset().top - 115; // 115 header offset $('html, body').animate({scrollTop: topOffset}, 500); } console.log('done'); //event.preventDefault(); }); */ /* Load the previous 25 lines *after* the form has been rendered */ }); function generateJobSelect() { var output = '
'; saveTool.find('#save_fields div:first-child').after(output); } function generateOptions() { var output = '

Qualifying Factors

Please check all that apply.

'; for (var i = 0; i < optionTexts.length; ++i) { if (i == 6 || i == 12) { output += '
'; } output += '
' } output += '
'; saveTool.append(output); } function validateFields() { saveTool.find('input, select').removeClass('error'); var valid = validateInput(saveTool.find('#save_bond_value')) + validateInput(saveTool.find('#save_bond_cost')) + validateInput(saveTool.find("#save_job")); return valid == 3; } function validateInput(selector) { if (!selector.val()) { selector.addClass("error"); return 0; } return 1; } function validateForm() { $('.pb-custom-errors').remove(); var found_errors = false; $('form#pardot-form .required input:visible').each(function(i) { var not_contains_errors = !$(this).parent().find('p.error.no-label').length; if (!$(this).val()) { found_errors = true; if (not_contains_errors) { $(this).after('This field is required.'); } } found_errors = not_contains_errors ? found_errors : true; }); if (found_errors) { $('form#pardot-form').prepend('

Please correct the errors below:

'); } return !found_errors; } function calculate() { // Client Input var clientBondValue = Number(saveTool.find('#save_bond_value').val().replace(/[^0-9-\.]+/g,"")); var clientBondCost = Number(saveTool.find('#save_bond_cost').val().replace(/[^0-9-\.]+/g,"")); // Find selected job class var selectedJobType = saveTool.find("#save_job option:selected").val(); var jobClass = 'X'; for (var jobKey in jobTypes) { if (jobTypes.hasOwnProperty(jobKey)) { var jobNames = jobTypes[jobKey]; jobNames.forEach(function(name, i, arr) { if (selectedJobType == name) { jobClass = jobKey; } }); } } // Find correct tier var qualifierCount = 0; $('.option input').each(function(i) { var checked = $(this).prop('checked'); if (checked) { qualifierCount += 1; } inquiryForm.find(qualifyingFactorFields[i] + " input").first().prop('checked', checked); }); var tier = (qualifierCount < 8) ? "standard" : ((qualifierCount < 10) ? "select" : ((qualifierCount < 12) ? "preferred" : "merit" )); var correctRates = rateTable[jobClass][tier]; // Calculate total premium var tierRemainder = clientBondValue; var totalPremium = 0; tierLevels.forEach(function(tierAmount, i, arr) { var qualifyingAmount = Math.min(tierAmount, tierRemainder); tierRemainder = tierRemainder - qualifyingAmount; var premium = qualifyingAmount * correctRates[i] / 1000; totalPremium += premium; }); // Calculate premium difference var bondCostDifference = clientBondCost - totalPremium; var bondSavings = (bondCostDifference > 0) ? bondCostDifference : 0; // Update necessary fields saveResults.find('.savings').autoNumeric('set', bondSavings); // Inquiry form update inquiryForm.find('.Last_Bonded_Contract_Work_Type input').val(selectedJobType); inquiryForm.find('.Last_Bonded_Contract_Value input').autoNumeric('set', clientBondValue); inquiryForm.find('.Last_Bond_Cost input').autoNumeric('set', clientBondCost); inquiryForm.find('.Last_Bond_Direct_Surety_Savings input').autoNumeric('set', bondSavings); } }));