Skip to content

Instantly share code, notes, and snippets.

@rickhernandezio
Last active February 9, 2018 02:11
Show Gist options
  • Save rickhernandezio/d3bedb0b96125796f674fd25017a5ca0 to your computer and use it in GitHub Desktop.
Save rickhernandezio/d3bedb0b96125796f674fd25017a5ca0 to your computer and use it in GitHub Desktop.
Custom JavaScript
var ValidateElements;
$(document).ready(function () {
var occurrence = $('.BBListingHeading h1').text().replace(/\s/g, '').split('|');
occurrence = (occurrence.length - 1);
if (occurrence == 3) { // Passes 3 bar test
/* Remove divMemberOnlyInfo h3 element if empty */
var memberOnlyInfo = $('.divMemberOnlyInfo h3').text().replace(/\s/g, '');
if (memberOnlyInfo.length == 0) {
$('.divMemberOnlyInfo h3').hide();
}
/* Hide date and time */
$('.TicketDateTimeInnerContainer').hide();
/* Append a value to the “This camp is sold out” section of “click here to join the waiting list”*/
var availability = $('.Programming_Event_Availability span').text();
if (availability === 'This camp is sold out.') {
availability += ' <a href="https://nhmu.utah.edu/waitlist">click here</a> to join the waiting list';
$('.Programming_Event_Availability span').html(availability);
}
/* Select and deselect section “Parent/Guardian address is same as camper's address” GLOBAL*/
var rows = []; // Save the row
var ids = []; // save the ids of input field
var monitorInput = []; // Monitors fields for changes
var campers = []; // Saves campers
var monitorLists = []; // Monitors list for changes
/* On selection of “Is your child on an IEP in school?”*/
var EIP = []; // Hold id's
var EIPRows = [];
/* On selection of “Add another Emergency Contact”*/
var EmergencyContacts = []; // Monitor checkbox fields
var EmergencyContactsFields = [];
/* On selection of “Add another Pick-up Authorization”*/
var Authorization = []; // Monitor checkbox fields
var AuthorizationFields = [];
/* On selection of “Same as Parent/Guardian”*/
var PickUpRows = []; // Save the row
var PickUpIds = []; // save the ids of input field
var PickUpInput = []; // Monitors fields for changes
/* Runs when ajax call finishes */
ValidateElements = function () {
/* Remove Registration information */
$('.Programming_Event_RegistrantRegistrationInformationHeaderContainer').hide();
/* Focus on first name */
if ($('#divRegistrantFirstName input:text').val() && $('#divRegistrantFirstName input:text').val().length == 0) {
$('#divRegistrantFirstName input:text:visible:first').focus();
}
/* Hide the “Use this as my billing information” checkbox */
$('.checkbox input').each((i, element) => {
if ($(element).prop('checked')) { // Checked
$(element).trigger('click');
}
});
$('.checkbox').each((i, element) => { // Hide all checkbox fields
$(element).hide();
});
/* Prepend the word “Camper” to first name for each registrant */
$('#divRegistrantFirstName label').each((i, element) => {
var firstName = $(element).text().split(' ');
var name = "Camper ";
firstName.forEach((value, index) => {
if (firstName.length != (index - 1)) {
name += value[0].toUpperCase() + value.substring(1) + ' ';
} else {
name += value[0].toUpperCase() + value.substring(1);
}
});
$(element).html(name);
$(element).removeClass('col-sm-3').addClass('col-sm-4');
});
$('#divRegistrantFirstName .col-sm-9').each((i, element) => { // fix width
$(element).removeClass('col-sm-9').addClass('col-sm-8');
});
/* Prepend the word “Camper” to last name */
$('#divRegistrantLastName label').each((i, element) => {
var lastName = $(element).text().split(' ');
var name = "Camper ";
lastName.forEach((value, index) => {
if (lastName.length != (index - 1)) {
name += value[0].toUpperCase() + value.substring(1) + ' ';
} else {
name += value[0].toUpperCase() + value.substring(1);
}
});
$(element).html(name);
$(element).removeClass('col-sm-3').addClass('col-sm-4');
});
$('#divRegistrantLastName .col-sm-9').each((i, element) => { // fix width
$(element).removeClass('col-sm-9').addClass('col-sm-8');
});
rows = []; // Save the row
ids = []; // save the ids of input field
monitorInput = []; // Monitors fields for changes
campers = []; // Saves campers
monitorLists = []; // Monitors list for changes
/* Select and deselect section “Parent/Guardian address is same as camper's address” */
$('.form-group').has('input:checkbox').each((i, item) => {
var textValue = $(item).find('label').text();
if (textValue === 'Parent/Guardian address is same as camper\'s address') {
var id = $(item).find('input').attr('id');
ids.push(id); // save it
var valueOfId = $(item).find('label').attr('id');
var numberOfRows = 4;
var start = false;
$('.form-group').each((j, row) => {
if (start && numberOfRows > 0) {
rows.push(row);
numberOfRows--;
}
if ($(row).find('label').attr('id') === valueOfId) {
start = true;
}
});
}
});
$('.Programming_Registrant_HostAddress input:text').each((i, item) => {
var monitor = $(item).attr('id');
monitorInput.push(monitor);
});
$('.Programming_Registrant_HostAddress textarea').each((i, item) => {
var monitor = $(item).attr('id');
monitorInput.push(monitor);
});
$('.Programming_Registrant_HostAddress select').each((i, item) => { // Monitor Country and State
var monitor = $(item).attr('id');
monitorLists.push(monitor);
});
$('.Programming_Registrant_GuestAddress input:text').each((i, item) => { // city and zip
var monitor = $(item).attr('id');
monitorInput.push(monitor);
if (i % 1 == 0 && i != 0) { // 1, 3, 5...
$('.Programming_Registrant_GuestAddress textarea').each((j, item) => { // address
if ((i - (j + 1)) == j) {
var monitor = $(item).attr('id');
monitorInput.push(monitor);
}
});
}
});
$('.Programming_Registrant_GuestAddress select').each((i, item) => { // Country and State
var monitor = $(item).attr('id');
monitorLists.push(monitor);
});
ids.forEach((id, index) => {
$('#' + id).change(function () { // Attache listener
var camper = {}; // local camper
if (index == 0) {
$('.Programming_Registrant_HostAddress input').each((i, item) => {
if (i == 0) {
camper['City'] = $(item).val();
}
if (i == 1) {
camper['ZIP Code'] = $(item).val();
}
});
$('.Programming_Registrant_HostAddress textarea').each((i, item) => {
camper['Address'] = $(item).val();
});
$('.Programming_Registrant_HostAddress select option:selected').each((i, item) => {
if (i == 0) {
camper['Country'] = $(item).text();
}
if (i == 1) {
camper['State'] = $(item).text();
}
});
if (this.checked) { // hide
rows.forEach((element, i) => {
if (i < 4) {
var property = $(element).find('.label').text();
$(element).find('input').val(camper[property]);
}
});
} else { // show
rows.forEach((element, i) => {
if (i < 4) {
$(element).find('input').val(''); // Empty out values
}
});
}
} else { // Not primary 2,3,4..
var max = (2 * index); // Get items in increments of 2
$('.Programming_Registrant_GuestAddress input').each((i, item) => {
if (i == (max - 1)) {
camper['ZIP Code'] = $(item).val();
}
if (i == (max - 2)) {
camper['City'] = $(item).val();
}
});
$('.Programming_Registrant_GuestAddress textarea').each((i, item) => {
if (i == (index - 1)) { // 0,1,2...
camper['Address'] = $(item).val();
}
});
$('.Programming_Registrant_GuestAddress select option:selected').each((i, item) => {
if (i == (max - 1)) {
camper['State'] = $(item).text();
}
if (i == (max - 2)) {
camper['Country'] = $(item).text();
}
});
var min = (4 * index);
max = (4 * index) + 4; // Get items in increments of 4
if (this.checked) { // hide
rows.forEach((element, i) => {
if (i >= min && i < max) { // in the range for this local div
var property = $(element).find('.label').text();
$(element).find('input').val(camper[property]);
}
});
} else { // show
rows.forEach((element, i) => {
if (i >= min && i < max) { // in the range for this local div
$(element).find('input').val(''); // Empty out values
}
});
}
}
campers[index] = camper;
});
$('#' + id).prop('checked', false).change(); // Force detection on loop
});
ids.forEach((id, index) => {
var max = (2 * index) + 2; // Get items in increments of 2
var min = max - 2;
monitorLists.forEach((item, j) => {
if (j >= min && j < max) {
// 0, 2 => 0
// 2, 4 => 1
// 4, 6 => 2
// 6, 8 => 3
$('#' + item).bind('input', function () { // Monitors the inputs and text areas for selected camper
$('#' + id).change(); // Force detection event change
});
}
});
max = (3 * index) + 3; // Get items in increments of 3
min = max - 3;
monitorInput.forEach((item, j) => {
if (j >= min && j < max) {
// 0, 3 => 0
// 3, 6 => 1
// 6, 9 => 2
// 9, 12 => 3
$('#' + item).change(function () {
$('#' + id).change(); // Force detection event change
});
}
});
});
/* On selection of “Is your child on an IEP in school?”*/
EIP = []; // Hold id's
EIPRows = [];
$('.form-group').has('select').each((i, item) => {
var textValue = $(item).find('.label').text();
if (textValue === 'Is your child on an IEP in school?') {
var id = $(item).find('select').attr('id');
EIP.push(id);
}
});
EIP.forEach((id, index) => {
var start = false;
var skip = index;
$('.form-group').each((j, row) => {
if (start) {
if (skip == 0) {
EIPRows.push(row);
skip = undefined;
} else if (skip != undefined) {
skip--;
start = false;
}
}
if ($(row).find('.label').text() === 'Is your child on an IEP in school?') {
start = true;
}
});
});
EIP.forEach((id, index) => {
$('#' + id).change(function () {
var value = $(this).find('option:selected').text();
if (value === 'Yes') {
EIPRows.forEach((element, j) => {
if (index == j) {
$(element).show();
}
});
} else {
EIPRows.forEach((element, j) => {
if (index == j) {
$(element).hide();
}
});
}
});
$('#' + id).change(); // Force detection onload
});
/* On selection of “Add another Emergency Contact”*/
EmergencyContacts = []; // Monitor checkbox fields
EmergencyContactsFields = [];
$('.form-group').has('input').each((i, item) => {
var textValue = $(item).find('label').text();
if (textValue === 'Add another Emergency Contact') {
id = $(item).find('input').attr('id');
EmergencyContacts.push(id);
}
});
EmergencyContacts.forEach((id, index) => {
var numberOfRows = 4;
var start = false;
var skip = index;
$('.form-group').each((j, row) => {
if (start && numberOfRows > 0) {
if (skip == 0) {
EmergencyContactsFields.push(row);
numberOfRows--;
if (numberOfRows == 0) {
skip = undefined;
}
} else if (skip != undefined) {
skip--;
start = false;
}
}
if ($(row).find('label').text() === 'Add another Emergency Contact') {
start = true;
}
});
});
EmergencyContacts.forEach((id, index) => {
$('#' + id).change(function () {
var max = (4 * index) + 4;
var min = max - 4;
if (this.checked) { // show
EmergencyContactsFields.forEach((element, j) => {
if (j >= min && j < max) {
$(element).show();
}
});
} else { // hide
EmergencyContactsFields.forEach((element, j) => {
if (j >= min && j < max) {
$(element).find('input').val(''); // Empty out values
$(element).hide();
}
});
}
});
$('#' + id).prop('checked', false).change(); // Force detection onload
});
/* On selection of “Add another Pick-up Authorization”*/
Authorization = []; // Monitor checkbox fields
AuthorizationFields = [];
$('.form-group').has('input').each((i, item) => {
var textValue = $(item).find('label').text();
if (textValue === 'Add another Pick-up Authorization') {
var id = $(item).find('input').attr('id');
Authorization.push(id);
}
});
Authorization.forEach((id, index) => {
var numberOfRows = 3;
var start = false;
var skip = index;
$('.form-group').each((j, row) => {
if (start && numberOfRows > 0) {
if (skip == 0) {
AuthorizationFields.push(row);
numberOfRows--;
if (numberOfRows == 0) {
skip = undefined;
}
} else if (skip != undefined) {
skip--;
start = false;
}
}
if ($(row).find('label').text() === 'Add another Pick-up Authorization') {
start = true;
}
});
});
Authorization.forEach((id, index) => {
$('#' + id).change(function () {
var max = (3 * index) + 3;
var min = max - 3;
if (this.checked) { // show
AuthorizationFields.forEach((element, j) => {
if (j >= min && j < max) {
$(element).show();
}
});
} else { // hide
AuthorizationFields.forEach((element, j) => {
if (j >= min && j < max) {
$(element).find('input').val(''); // Empty out values
$(element).hide();
}
});
}
});
$('#' + id).prop('checked', false).change(); // Force detection onload
});
/* Same as Parent/Guardian Checkbox to auto fill based on parent */
PickUpRows = []; // Save the row
PickUpIds = []; // save the ids of input field
PickUpInput = []; // Monitors fields for changes
$('.form-group').has('input:checkbox').each((i, item) => {
var textValue = $(item).find('label').text();
if (textValue === 'Same as Parent/Guardian') {
var id = $(item).find('input').attr('id');
PickUpIds.push(id); // save it
var valueOfId = $(item).find('label').attr('id');
var numberOfRows = 3;
var start = false;
$('.form-group').each((j, row) => {
if (start && numberOfRows > 0) {
PickUpRows.push(row);
numberOfRows--;
}
if ($(row).find('label').attr('id') === valueOfId) {
start = true;
}
});
}
});
// Finds the parents input fields to monitor
PickUpIds.forEach((id, index) => {
var skip = index;
var numberOfRows = 3;
var start = false;
$('.form-group').each((j, row) => {
if ($(row).find('.label').text() === 'Parent/Guardian First Name') {
start = true;
}
if (start && numberOfRows > 0) {
if (skip == 0) {
var field = $(row).find('input').attr('id');
PickUpInput.push(field);
numberOfRows--;
if (numberOfRows == 0) {
skip = undefined;
}
} else if (skip != undefined) {
skip--;
start = false;
}
}
});
});
// One way data binding
PickUpIds.forEach((id, index) => {
$('#' + id).change(function () { // Attache listener
var max = (3 * index) + 3; // Get items in increments of 3
var min = max - 3;
if (this.checked) { // checked
PickUpInput.forEach((input, j) => {
if (j >= min && j < max) {
var value = $('#' + input).val();
$(PickUpRows[j]).find('input').val(value);
}
});
} else { // unchecked
PickUpInput.forEach((input, j) => {
if (j >= min && j < max) {
$(PickUpRows[j]).find('input').val('');
}
});
}
});
$('#' + id).prop('checked', false).change(); // Force detection on loop
});
// Monitors input for parent
PickUpIds.forEach((id, index) => {
var max = (3 * index) + 3; // Get items in increments of 3
var min = max - 3;
PickUpInput.forEach((item, j) => {
if (j >= min && j < max) {
// 0, 2 => 0
// 2, 4 => 1
// 4, 6 => 2
// 6, 8 => 3
$('#' + item).bind('input', function () { // Monitors the inputs and text areas for selected camper
$('#' + id).change(); // Force detection event change
});
}
});
});
}
}
});
(function (open) {
XMLHttpRequest.prototype.open = function (method, url, async, user, pass) {
this.addEventListener("readystatechange", function () {
if (this.readyState == 4) {
if (this.status === 200) { // Successful ajax request
if (typeof ValidateElements !== "undefined") {
ValidateElements(); // Process elements
}
}
}
}, false);
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment