Use the following sample code to build JavaScript functions:
Function: Event.observe (element, event handler)
Purpose: Adds event to element. In this case, when window loads, generic function executes.
/*Example 1: load function when pages loads:*/
Event.observe(window, 'load', translate() );
/*Example 2: load "listener" when page loads. listener listen for user to click preview button*/
Event.observe(window, 'load', translate() ) {
Event.observe( forms['ContactFormID'].elements['Preview'], 'click', translate);
});
|
Function: translate ()
Purpose: Find/Replace text - can be used for many things, on being localization of languages.
function translate()
{
if (document.getElementById("centered"))
{
document.getElementById("centered").innerHTML = document.getElementById("centered").innerHTML.replace(new RegExp(/agree/g), "suis d'accord.");
}
}
/*Ecommerce Example: Fix hardcoded spelling errors in e-commerce module*/ function fixEcommerce() {
if (document.getElementById("TableInner"))
{
document.getElementById("TableInner").innerHTML = document.getElementById("TableInner").innerHTML.replace(new RegExp("No record were"), "No records were");
document.getElementById("TableInner").innerHTML = document.getElementById("TableInner").innerHTML.replace(new RegExp("Or Try different price."), "Try a different price.");
document.getElementById("TableInner").innerHTML = document.getElementById("TableInner").innerHTML.replace(new RegExp("Try more general keywords.Try more general keywords."), "Try more general keywords.");
}
} |
Function: hideWrapper()
Purpose: Hide wrapper content in CMS when using content listings or contact lookup tool.
function hideWrapper()
{
var site = location.href;
var page1 = site.indexOf("contactdisplay.asp");
if (page1 > 0)
{
document.getElementById('WrapperContentHdr').style.display = 'none'; document.getElementById('WrapperContentFtr').style.display = 'none'; document.getElementById('Form1').style.display = 'none';
}
} |
Function: getElementsByClassName()
Purpose: Loop through page to grab all class names.
function getElementsByClassName(oElm, strTagName, strClassName){
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\-/g, "\\-"); var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
var oElement;
for(var i=0; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
} |
Purpose: Find and replace specific header text in CMS Contact lookup tool
if(getElementsByClassName(document, "table", "listren-listtbl") != ""){
var headings = getElementsByClassName(document, "tr", "listren-header");
var headingCells = headings[0].getElementsByTagName('td');
for (i=1;i<headingCells.length;i++) {
var headingLink = headingCells[i].getElementsByTagName('a')[0]:
if(headingLink.innerHTML == "CompanyName") {
headingLink.innerHTM = "Grant Title";
}
}
}
|
Function: updateStateValue(str)
Purpose: Take in CRM merge field and replace 2 letter code with complete name
function updateStateValue(str){
var nValue;
var x = str.substring(0,2);
switch(x){
case ("AL"): nValue = "Alabama" ; break;
case ("AK"): nValue = "Alaska" ; break;
case ("AS"): nValue = "American Samoa" ; break;
case ("AZ"): nValue = "Arizona" ; break;
.
.
.
default: nValue = "";
}
document.write(nValue);
} |
Function: updateCountryValue(str)
Purpose: Take in CRM merge field and replace 2 letter code with complete name
function updateCountryValue(str){
var nValue;
var x = str.substring(0,2);
switch(x){
case ("AL"): nValue = "United States" ; break;
case ("AK"): nValue = "Afghanistan" ; break;
case ("AS"): nValue = "Albania" ; break;
case ("AZ"): nValue = "Algeria" ; break;
.
.
.
default: nValue = "";
}
document.write(nValue);
} |
Function: populatMulti()
Purpose: prepopulate multi-select with merge tags
function populateMulti()
{
var value = "";
var valueArray = value.split(",");
for (i=0; i < valueArray.length; i++)
{
for (x=0; x < document.SUPPORTER174472.multiselect.length; x++)
{
if (valueArray[i] == document.SUPPORTER174472.multiselect.options[x].value)
{
document.SUPPORTER174472.multiselect.options[x].selected = true;
}
}
}
} |
Function: getQueryStringValues()
Purpose: get values from querystring and use them to prepopulate form
function getQueryStringValues()
{
if (window.location.search) {
if (window.location.search.indexOf("keyword")!=-1) {
var qs_raw = self.window.location.search;
var qs = qs_raw.slice(1,qs_raw.length);
var qs_ar = qs.split('&');
var tidy_ar = new Array();
for (i=0;i
tidy_ar.push(qs_ar[i].slice(qs_ar[i].indexOf('=')+1))
}
document.getElementById("sender_name").value = tidy_ar[2].replace(/\+/g," ");
document.getElementById("sender_email").value = tidy_ar[3];
var friends_array = (unescape(tidy_ar[4])).split(",");
for (i=0;i<6;i++) {
if (friends_array[i]) {
document.forms.spreadWord.email_recipients_email[i].value = friends_array[i];
}
document.getElementById("email_body").value = (unescape(tidy_ar[5])).replace(/\+/g," ");
document.getElementById("email_subject").focus();
}
}
} |
Function: replaceButtons()
Purpose: Replace any submit button with an image, based on the value of its 'value' attribute
function replaceButtons()
{
if (document.getElementsByClassName("HtmlButton"))
{
x = document.getElementsByClassName("HtmlButton");
for (i=0;i
{
if (x[i].value == "Submit") // find 'Submit' buttons
{
x[i].style.height = '30px';
x[i].style.padding = '20px 0 0 0;';
x[i].style.background ='url(/atf/cf/%7B74BA1956-0C57-4B8E-9D15-D6AB8CE64CF1%7D/SUBMIT.gif) no-repeat center top';
}
if (x[i].value == "Clear Selection") // find 'Clear Selection' buttons
{
x[i].style.height = '30px';
x[i].style.padding = '20px 0 0 0;';
x[i].style.background ='url(/atf/cf/%7B74BA1956-0C57-4B8E-9D15-D6AB8CE64CF1%7D/ClearSelection.gif) no-repeat center top';
}
if (x[i].value == "Preview") // find 'Preview' buttons
{
x[i].style.height = '30px';
x[i].style.padding = '20px 0 0 0;';
x[i].style.background ='url(/atf/cf/%7B74BA1956-0C57-4B8E-9D15-D6AB8CE64CF1%7D/ClearSelection.gif) no-repeat center top';
}
}
}
} |
Function: insertPreview()
Purpose: Insert HTML (Preview button in advocacy module) before or after any elements. See http://www.prototypesjs.org/api/insertion for details.
function insertPreview()
{
if(!$('ad_Stw_wpr'))
{
if(document.getElementsByClassName('ad_Ltr')[0])
{
if(document.getElementsByClassName('ad_preview')[0])
{
document.getElementsByClassName('ad_preview')[0].style.visibility = 'hidden';
var x = document.getElementsByClassName('ad_Ltr')[0];
new Insertion.Bottom(x, '<div style="margin:20px 0; padding: 10px 0;"><input type = "image" name="ct100$ct100$cmdPreviewLetter" id="ct100_ct100_cmdPreviewLetter" class="ad_preview" scr="/siteapps/advocacy/images/spacer.gif" alt="Preview" style="border-width:0px;"/></div>');
}
}
}
} |
Function: sfHover
Purpose: simulate a:hover for non-A elements in IE. Used in CSS dropdowns.
sfHover = function() {
var sfEls = document.getElementById("nav").getElementsByTagName("LI");
for (var i=0; i
sfEls[i].onmouseover=function() {
this.className+=" sfhover";
}
sfEls[i].onmouseout=function() {
this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}
}
}
if (window.attachEvent) window.attachEvent("onload", sfHover); // only recognized and loaded by IE
|
Function: setIDs
Purpose: set unique ids to header nav li's, & body tag, in order to set section-based color scheme for pages.
function setIDs()
{
if ($("headerNav"))
{
var count = 0;
var navItems = $("headerNav").getElementsByTagName("LI");
for (i=0;i
{
if (navItems[i].className == "header-nav-item")
{
if (navItems[i].getElementsByTagName('IMG')[0].src.indexOf("_on")>-1)
{
navItems[i].setAttribute("id","headerNavItem" + [count]);
document.getElementsByTagName("BODY")[0].setAttribute("id","section" + [count]);
} else {
navItems[i].setAttribute("id","headerNavItem" + [count]);
}
count++;
}
}
}
} |
Function: resetChar(obj)
Purpose: Replace Double Quotes with Single Quotes on all fee form text fields
function resetChar(obj){
var string = obj.value;
var replace = string.replace(/"/g,"'");
obj.value = replace;
}
function addOnBlur(obj){
var origEvt = obj.onblur;
obj.onblur = "";
var newFunc = function(){
resetChar(obj);
if (origEvt) {
origEvt();
}
}
obj.onblur = newFunc;
}
function captureFields(){
try {
for (var i=0; i
var match = /^(SUPPORTER)/.test(document.forms[i].name);
if (match){
var input_field = document.forms[i].getElementsByTagName('input');
var textarea = document.forms[i].getElementsByTagName('textarea');
for (var o=0; o
var match_input = /^(text)/.test(input_field[o].type);
if (match_input) {
addOnBlur(input_field[o]);
}
}
for (var p=0; p
addOnBlur(textarea[p]);
}
}
}
} catch(err){}
}
captureFields(); |
Function: del_cookie(name)
Purpose: Delete cookie
function del_cookie(name) {
document.cookie = name + "=; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=/";
}
cookies = document.cookie.split(";");
for (i=0;i
indCookie = cookies[i].indexOf("=");
cName=cookies[i].substring(0,indCookie);
del_cookie(cName);
}
document.cookie = "";
|
Function: choosePic()
Purpose: Random Image rotator
myPix = new Array("homeimg_diversity.jpg"
,"HOMEIMG_PRIDE.JPG"
,"HOMEIMG_RELATIONSHIPS.JPG"
,"HOMEIMG_TRADITION.JPG")
imgCt = myPix.length
function choosePic() {
if (document.images) {
randomNum = Math.floor((Math.random()*imgCt));
document.myPicture.src=myPix[randomNum];
}
} |
Function: hideLastName ()
Purpose: Hide form field (example below hiding last name field)
function hideLastName(){
try {
for (var i=0; i
var match = /^(SUPPORTER)/.test(document.forms[i].name);
if (match){
var input_fields = document.forms[i].getElementsByTagName('input');
for (var o=0; o
if (input_fields[o].name == "last_name"){
var obj = input_fields[o];
do {
var objName = obj.parentNode.nodeName;
obj = obj.parentNode;
if (objName == "TR"){
obj.style.display = "none";
}
} while (objName != "TR");
}
}
}
}
} catch(err){}
}
hideLastName();
|
Function: updateHomePhoneValidation(); updateBusinessPhoneValuidation(); updateCellPhoneValidation();
Purpose: Custom form field validation (Examples below for Phone validation)
function addOnBlurPhone(obj){
try {
var origEvt = obj.onblur;
obj.onblur = "";
var newFunc = function(){
if (origEvt) {
origEvt.apply(this);
}
checkPhoneNumber(obj);
}
obj.onblur = newFunc;
}catch(err){}
}
function checkPhoneNumber(obj){
try {
var string = obj.value;
var objRegExp = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
var match_input = objRegExp.test(string);
if (match_input == false){
if (obj.value != "" ){
alert("Please enter your phone number in the following format: (999) 999-9999");
obj.focus();
obj.select();
}
}
}catch(err){}
}
function updateHomePhoneValidation(){
try {
for (var i=0; i
var match = /^(SUPPORTER)/.test(document.forms[i].name);
if (match){
var input_field = document.forms[i].getElementsByTagName('input');
for (var o=0; o
var match_input = /^(phone_number)/.test(input_field[o].name);
if (match_input) {
addOnBlurPhone(input_field[o]);
}
}
}
}
} catch(err){}
}
function updateBusinessPhoneValidation(){
try {
for (var i=0; i
var match = /^(SUPPORTER)/.test(document.forms[i].name);
if (match){
var input_field = document.forms[i].getElementsByTagName('input');
for (var o=0; o
var match_input = /^(business_phone_number)/.test(input_field[o].name);
if (match_input) {
addOnBlurPhone(input_field[o]);
}
}
}
}
} catch(err){}
}
function updateCellPhoneValidation(){
try {
for (var i=0; i
var match = /^(SUPPORTER)/.test(document.forms[i].name);
if (match){
var input_field = document.forms[i].getElementsByTagName('input');
for (var o=0; o
var match_input = /^(cell_phone_number)/.test(input_field[o].name);
if (match_input) {
addOnBlurPhone(input_field[o]);
}
}
}
}
} catch(err){}
} |
Function: fixBreadCrumb()
Purpose: Modify breadcrumb
function fixBreadCrumb(){
try {
var obj = document.getElementById('breadcrumb').getElementsByTagName('a');
for (var i=0; i
if (obj[i].firstChild.nodeValue == "Home"){
obj[i].href = "https://secure.csualum.com/site/lookup.asp?c=efIPK1OAJoG&b=2763889";
} else {
var str = obj[i].href;
var newlinkREF = str.replace(/^(http:\/\/www\.csualum\.com\/)/g,'https://secure.csualum.com/');
obj[i].href = newlinkREF;
}
}
}catch(err){}
}
fixBreadCrumb(); |
|