var CTTrack = {}; CTTrack.replaceNumber = function(numberToReplace) { CTTrack.replaceNumber(numberToReplace, null); } CTTrack.replaceNumber = function(numberToReplace, criteriaValue) { CTTrack.replaceNumberFormatted(numberToReplace, CTTrack.defaultPhoneFormat, criteriaValue); } CTTrack.replaceNumberFormatted = function(numberToReplace, formatter) { CTTrack.replaceNumberFormatted(numberToReplace, CTTrack.defaultPhoneFormat, null); } CTTrack.replaceNumberFormatted = function(numberToReplace, formatter, criteriaValue) { var keysList = new Array() var valuesList = new Array() var replacementNumber = CTTrack.findReplacementNumber( keysList, valuesList, numberToReplace, criteriaValue ); if (formatter!=null) document.write(formatter(replacementNumber)); else document.write(replacementNumber); } CTTrack.replaceNumberUnformatted = function(numberToReplace) { CTTrack.replaceNumberUnformatted(numberToReplace, null); } CTTrack.replaceNumberUnformatted = function(numberToReplace, criteriaValue) { CTTrack.replaceNumberFormatted(numberToReplace, null, criteriaValue); } CTTrack.defaultPhoneFormat = function(numberToFormat) { if(numberToFormat.length!=10)return numberToFormat; return "(" + numberToFormat.substring(0,3) + ") " + numberToFormat.substring(3,6) + "-" + numberToFormat.substr(6); } /** Attempts to find a replacement number by matching the criteriaValue to an item in the keysList array. */ CTTrack.findReplacementNumber = function( keysList, valuesList, numberToReplace, criteriaValue ) { // if no criteria value is supplied, return the first number if there is one. // otherwise return the passed number if ( criteriaValue == null ) { if ( keysList.length > 0 ) { return valuesList[ 0 ]; } else { return numberToReplace; } } // find the criteria value in the keysList var indexValue = -1; for (element in keysList ) { var key = keysList[element]; // do we have a match? if ( criteriaValue == key ) { indexValue = element; break; } } // return the passed number if no match is found if ( indexValue == -1 ) { return numberToReplace; } else { return valuesList[ indexValue ]; } }