//here we allow for skipping questions based on url parameters
function prepopulateValues() {
//first method which decides whether to use cookie or query string to prepopulate
function decideOperations(nextOperation) {
//detect if it's IE and return if necessary
if (detectMSIE() == true) {
return;
}
//create the schema object based on which opsType we are running
return nextOperation(assignValuesToDom);
}
//have to create a schema of the questions order, ids, values, and identify which were prepopped
function createSchemaObjects(callback) {
var params = queryStringToJSON(window.location.href.split('#')[0]);
if (params.zip_code) {
params.zipcode = params.zip_code;
}
//for when someone just runs this onload in site.js or such, make sure it doesn't do anything to fuck up the whole form if there are no params on the qs
if (params != window.location.href) {
var pathQuestions = [],
questionId,
questionInput,
questionValue,
questionCount = 1;
//grab each of the questions on the current form
$(document).find('fieldset').each(function () {
//grab current element we are iterating overs basic data that we will want
questionId = $(this).prop('id');
//empty the array each fieldset
questionInput = [],
nameObj = {};
//find the relevant inputs
$(this).find('input, select').each(function () {
let value = '';
if ($(this).prop('name') in questionInput) {
return;
}
//if it's the first one in the fieldset, no need to check if the array already contains it
let name = $(this).prop('name');
if (params.hasOwnProperty(name)) {
// don't replace plus signs on emails
if (name === "email") {
value = params[name];
} else if (name === "est_purchase_price") {
if (params[name] === "" || params[name] == 0) {
value = "155000";
} else {
value = params[name];
}
} else {
value = params[name].replace(/\+/g, ' ');
}
//put object into the array if it is contained in prepop data
nameObj[name] = value;
questionInput.push(nameObj);
}
})
//push it to the schema
pathQuestions.push({
questionId,
questionCount,
questionInput
})
questionCount++;
})
}
//keeping our functions local and instantiating them from each other keeps it easy to control scope and execution order
return callback(params, pathQuestions, null);
}
//attempt to prepop the values into the document, and then update the schema based on what values were successfully updated
function assignValuesToDom(params, pathQuestions, callback) {
var questionParentId;
//for each of the values in prepop questions schema, we identify the corresponding DOM element, then attempt to populate it with the value from params objec
var frames = pathQuestions.length;
for (var key = 0; key < frames; key++) {
if (pathQuestions[key].questionInput.length != 0) {
$.each(pathQuestions[key].questionInput, function (name, value) {
question_name = Object.keys(value)[0];
question_value = Object.values(value)[0];
//the corresponding document object to the item in the prepop questions array which we are currently iterating over
var inputTarget,
selectTarget;
if ($(document).find('input[name="' + question_name + '"]').length != 0) {
inputTarget = $(document).find('input[name="' + question_name + '"]')
} else if ($(document).find('select[name="' + question_name + '"]').length != 0) {
selectTarget = $(document).find('select[name="' + question_name + '"]');
}
if (inputTarget) {
//get its id
questionParentId = inputTarget.parents('.card').prop('id') || selectTarget.parents('.card').prop('id');
//there could be more than one domTarget element in our current scope, so have to iterate over them individually
inputTarget.each(function () {
let success = false;
if (this.type == "radio") {
//if it's a radio, we need to find which ones value matchs the schema, and give it checked attribute
if (this.value.toLowerCase() == question_value.toLowerCase()) {
this.checked = true;
//if we did assign it, success for this element is true
success = true;
}
} else if (this.type == 'hidden' || this.type == 'text' || this.type == 'number' || this.type == 'tel' || this.type == 'email') {
//if it's text or hidden, go ahead and just place the value in, hope they didn't typo
this.value = decodeURIComponent(question_value);
if (this.getAttribute('id') == 'propValuePost') {
curValue = this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","),
arrayPosition = estPropValues.indexOf(curValue);
$('#propValues').slider({
value: arrayPosition
})
setPriceDisplay(estPropValues, arrayPosition, 'prop');
}
if (this.getAttribute('id') == 'mortgageValuePost') {
curValue = this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","),
arrayPosition = estMortgageValues.indexOf(curValue);
$('#mortgageValues').slider({
value: arrayPosition
})
setPriceDisplay(estMortgageValues, arrayPosition, 'mortgage');
}
if (this.getAttribute('id') == 'purchaseValuePost') {
curValue = this.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","),
arrayPosition = estPurchaseValues.indexOf(curValue);
$('#purchaseValues').slider({
value: arrayPosition
})
setPriceDisplay(estPurchaseValues, arrayPosition, 'mortgage');
}
}
//again if we did place, we consider a success
success = true;
pathQuestions[key].questionInput[name][question_name].prepopped = 1;
})
}
if (selectTarget) {
questionParentId = selectTarget.parents('.card').prop('id') || selectTarget.parents('.card').prop('id');
//support for selects / dropdowns
selectTarget.each(function () {
let success = false;
for (var i = 0; i < $(this)[0].options.length; i++) {
//cant do vals on these must target by ordinal position which matches the prepop value in the select options
let option = $(this)[0].options[i];
if (option.value.toLowerCase() == pathQuestions[key].questionInput[name][question_name].toLowerCase()) {
$(this)[0].selectedIndex = i;
success = true;
pathQuestions[key].questionInput[name][question_name].prepopped = 1;
} else {
success = false;
}
}
})
}
})
}
}
var prepopEvent = new Event('prepop done');
if (params.zipcode) {
doZipCodeOperations(params.zipcode, false, true, false);
}
if (params.property_zip) {
doZipCodeOperations(params.property_zip, true, false, false);
}
return window.dispatchEvent(prepopEvent);
}
//make this function chain able to be execut by prepopulateValues()
decideOperations(createSchemaObjects)
}