jaymascarenas
7/9/2018 - 9:45 PM

gf-prepop-v2

function prepopulateValues() {
    var params = {};

    if (location.search) {
        var parts = location.search.substring(1).split('&');

        for (var i = 0; i < parts.length; i++) {
            var nv = parts[i].split('=');
            if (!nv[0]) continue;

            var inputKey = $('input[name="' + nv[0] + '"]');

            // if param has an email don't replace the plus sign, if not replace all plus signs
            if (nv[0] === 'email') {
                params[nv[0]] = decodeURIComponent(nv[1]) || '';
            } else {
                params[nv[0]] = decodeURIComponent(nv[1]).replace(/\+/g, ' ') || '';
            }

            // handle radio and checkboxes or just plain inputs
            if (inputKey.is(':radio') || inputKey.is(":checkbox")) {
                $('input[name="' + nv[0] + '"][value="' + nv[1] + '"]').prop("checked", true);
            } else {
                inputKey.val(decodeURIComponent(nv[1]).replace(/\+/g, ' '));
            }

            // handle select dropdowns
            $('select[name="' + nv[0] + '"]').val(decodeURIComponent(nv[1]));
        }

        // mc zip or gf zip param
        if (params.zip_code) {
            params.zipcode = params.zip_code;
        }

        // if est pur price is null or zero set it's default
        if (params.est_purchase_price === '' || params.est_purchase_price === '0') {
            $('select[name="est_purchase_price"]').val('155000');
        }

        // run zip and prop zip operations if they exist
        if (params.zipcode || params.zip_code) {
            doZipCodeOperations(params.zipcode, false, true, false);
        }

        if (params.property_zip) {
            doZipCodeOperations(params.property_zip, true, false, false);
        }

        // run either dropdown LTV or initiate sliders
        // TODO: determine if this site contains a slider or dropdown then run either ltv functions
        if (params.mortgage_amount || params.est_property_value) {

            if ($('input[name="mortgage_amount"]').attr('type') == 'hidden' || $('input[name="est_property_value"]').attr('type') == 'hidden' || $('input[name="est_purchase_price"]').attr('type') == 'hidden') {
                // this is a slider

                var ma = params.mortgage_amount, pv = params.est_property_value, pp = params.est_purchase_price;

                console.log("MA = " + ma + ", PV = " + pv + ", PP = " + pp);
                console.log("MA = " + ma.toString() + ", PV = " + pv.toString() + ", PP = " + pp.toString());

                if ($('input[name="mortgage_amount"]').attr('type') == 'hidden') {
                    var mortArrayPosition = estMortgageValues.indexOf(ma);

                    $('#mortgageValues').slider({
                        value: mortArrayPosition
                    })
                    setPriceDisplay(estMortgageValues, mortArrayPosition, 'mortgage');
                }

                if ($('input[name="est_property_value"]').attr('type') == 'hidden') {
                    var propValueArrayPosition = estPropValues.indexOf(pv);

                    $('#propValues').slider({
                        value: propValueArrayPosition
                    })
                    setPriceDisplay(estPropValues, propValueArrayPosition, 'prop');
                }

                if ($('input[name="est_purchase_price"]').attr('type') == 'hidden') {

                    var estPurArrayPosition = estPurchaseValues.indexOf(pp);

                    console.log(estPurArrayPosition);

                    $('#purchaseValues').slider({
                        value: estPurArrayPosition
                    })
                    setPriceDisplay(estPurchaseValues, estPurArrayPosition, 'mortgage');
                }

            } else {
                // this is a dropdown
                dropDownLTV();
            }


        }

    }

}