Skip to content

Instantly share code, notes, and snippets.

@gMagicScott
Created September 17, 2012 00:59
Show Gist options
  • Save gMagicScott/3735034 to your computer and use it in GitHub Desktop.
Save gMagicScott/3735034 to your computer and use it in GitHub Desktop.
Combined AS-Birthday frontpage test
/*jslint browser:true */
/*global $*/
(function () {
"use strict";
var multiAR, genderField, tomorrowField, childNameField, defaultAR, getGender, changeAR, titleCaseHandler, pluralHandler;
//******************************
// Settings
//******************************
multiAR = 'XXXXXX'; //Multi-Child Autoresponder ID Number
tomorrowField = '#field7'; //Field that holds tomorrow's date
genderField = '#field8'; //The select element for Boy/Girl/Multi
childNameField = '#field9'; //Birthday child(ren)'s name field
//******************************
// Party on...
//******************************
String.prototype.toTitleCase = function () {
return this.replace(/([\w&`'‘’"“.@:\/\{\(\[<>_]+-? *)/g, function (match, p1, index, title) {
if (index > 0 && title.charAt(index - 2) !== ":" && match.search(/^(a(nd?|s|t)?|b(ut|y)|en|for|i[fn]|o[fnr]|t(he|o)|vs?\.?|via)[ \-]/i) > -1) {
return match.toLowerCase();
}
if (title.substring(index - 1, index + 1).search(/['"_{(\[]/) > -1) {
return match.charAt(0) + match.charAt(1).toUpperCase() + match.substr(2);
}
if (match.substr(1).search(/[A-Z]+|&|[\w]+[._][\w]+/) > -1 || title.substring(index - 1, index + 1).search(/[\])}]/) > -1) {
return match;
}
return match.charAt(0).toUpperCase() + match.substr(1);
});
};
function toProperCase(s) {
return s.toTitleCase();
}
function checkPlural(s) {
var amp1 = "&";
s = s.replace(amp1, "and");
$(childNameField).val(s);
s = s.toLowerCase();
if (s.indexOf(' and ') > 0 || s.indexOf('&') > 0 || s.indexOf(',') > 0) {
return true;
}
return false;
}
defaultAR = $("#defaultar").val();
getGender = function () {
return $(genderField).val();
};
changeAR = function () {
var option = getGender();
switch (option) {
case "boy":
$("#defaultar").val(defaultAR);
break;
case "girl":
$("#defaultar").val(defaultAR);
break;
case "multi":
$("#defaultar").val(multiAR);
break;
case "0":
$("#defaultar").val(defaultAR);
break;
default:
$("#defaultar").val(defaultAR);
}
};
titleCaseHandler = function () {
$(this).val(toProperCase($(this).val()));
};
pluralHandler = function () {
if (checkPlural($(this).val())) {
$(genderField).val("multi").change();
}
};
function tomorrow() {
var currentDate, day, month, year;
currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
if (currentDate.getDate() < 10) {
day = "0" + currentDate.getDate();
} else {
day = currentDate.getDate();
}
if ((currentDate.getMonth() + 1) < 10) {
month = "0" + (currentDate.getMonth() + 1);
} else {
month = currentDate.getMonth() + 1;
}
year = currentDate.getFullYear();
return month + "/" + day + "/" + year;
}
$(genderField).change(changeAR);
$("#Name").change(titleCaseHandler);
$(childNameField).change(titleCaseHandler);
$(childNameField).change(pluralHandler);
$(tomorrowField).val(tomorrow());
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment