Created
May 8, 2010 05:27
-
-
Save davidvanvickle/394374 to your computer and use it in GitHub Desktop.
Javascript utility funcs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Javascript utility funcs */ | |
function valueInArray (val,arr) { | |
for (var i = 0; i < arr.length; i++) { | |
if (val==arr[i]) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function strip (mainStr, stripStartChar){ | |
foundOffset = mainStr.indexOf(stripStartChar); | |
if (foundOffset == -1) { | |
return mainStr; | |
} | |
return mainStr.substring(0,foundOffset); | |
} | |
function radio_value(elem) { | |
for (var i=0; i < elem.length; i++) { | |
if (elem[i].checked) { | |
return elem[i].value; | |
} | |
} | |
return ""; | |
} | |
// takes elem and value to make "checked", returns true/false | |
function radio_check(elem,val) { | |
if (!elem) { | |
return false; | |
} | |
if (!val) { | |
return false; | |
} | |
for (var i = 0; i < elem.length; i++) { | |
if (strip_jeff(elem[i].value)==val) { | |
elem[i].checked = true; | |
return true; | |
} | |
} | |
return false; | |
} | |
function parse_searchstr(str) { | |
str = str ? str : location.search; | |
var query = str.charAt(0) == '?' ? str.substring(1) : str; | |
var args = new Object(); | |
if (query) { | |
var fields = query.split('&'); | |
for (var f = 0; f < fields.length; f++) { | |
var field = fields[f].split('='); | |
args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' ')); | |
} | |
} | |
return args; | |
} | |
function clearWhitespace(str) { | |
return (str.replace(/^\s+/,'')).replace(/\s+$/,''); | |
} | |
// same as clearWhitespace() except it takes a form elem and assign the new val to elem and returns val | |
function clearWhitespaceElem(elem) { | |
var str = elem.value; | |
str = (str.replace(/^\s+/,'')).replace(/\s+$/,''); | |
elem.value = str; | |
return str; | |
} | |
// strip white space and beginned and end of string | |
function trim_string(InString) | |
{ | |
var ichar, icount; | |
var strValue = InString; | |
ichar = strValue.length - 1; | |
icount = -1; | |
while (ichar > icount && strValue.charAt(ichar)==' ') | |
--ichar; | |
if (ichar!=(strValue.length-1)) | |
strValue = strValue.slice(0,ichar+1); | |
ichar = 0; | |
icount = strValue.length - 1; | |
while (ichar < icount && strValue.charAt(ichar)==' ') | |
++ichar; | |
if (ichar!=0) | |
strValue = strValue.slice(ichar,strValue.length); | |
return strValue; | |
} | |
function getWordArray (str) { | |
var st = -1; // word start | |
var thisword = ""; | |
var words_to_check = new Array(); | |
for (var i = 0; i < str.length; i++) { // through each char | |
// find a word | |
var charcode = str.toLowerCase().charCodeAt(i); | |
// if word exists from last round, wipe it | |
if (thisword != "") { | |
thisword = ""; | |
} | |
if (charcode >= 97 && charcode <= 122) { // if char | |
if (thisword == "" && st == -1) { // still look for word start (and no start found before now) | |
st = i; // save this pos as start | |
} // else I have a word so ignore char | |
} else { // not a char; define word if haven't already | |
if (thisword == "" && st != -1) { // need to define/end a word (and start found) | |
thisword = str.substring(st,i); | |
st = -1; // reset start | |
} | |
} | |
if (thisword != "") { // if have word, check spelling | |
words_to_check[words_to_check.length] = thisword; | |
} | |
} | |
return words_to_check; | |
} | |
function bubbleSort(inputArray) { | |
var start = 0; | |
var rest = inputArray.length-1; | |
for (var i = rest - 1; i >= start; i--) { | |
for (var j = start; j <= i; j++) { | |
if (inputArray[j+1] < inputArray[j]) { | |
var tempValue = inputArray[j]; | |
inputArray[j] = inputArray[j+1]; | |
inputArray[j+1] = tempValue; | |
} | |
} | |
} | |
return inputArray; | |
} | |
function bubbleSortUnique(inputArray) { // bubble sort and make unique | |
var start = 0; | |
var rest = inputArray.length-1; | |
for (var i = rest - 1; i >= start; i--) { | |
for (var j = start; j <= i; j++) { | |
if (inputArray[j+1] < inputArray[j]) { | |
var tempValue = inputArray[j]; | |
inputArray[j] = inputArray[j+1]; | |
inputArray[j+1] = tempValue; | |
} | |
} | |
} | |
var arr_unique = new Array(); | |
var curr_word = ""; | |
for (var i = 0; i < inputArray.length; i++) { | |
if (inputArray[i] != curr_word) { | |
arr_unique[arr_unique.length] = inputArray[i]; | |
curr_word = inputArray[i]; | |
} | |
} | |
//return inputArray; | |
return arr_unique; | |
} | |
function bubbleSortN(inputArray) { // bubblesort that uses parseint to sort | |
var start = 0; | |
var rest = inputArray.length-1; | |
for (var i = rest - 1; i >= start; i--) { | |
for (var j = start; j <= i; j++) { | |
if (parseInt(inputArray[j+1],10) < parseInt(inputArray[j],10)) { | |
var tempValue = inputArray[j]; | |
inputArray[j] = inputArray[j+1]; | |
inputArray[j+1] = tempValue; | |
} | |
} | |
} | |
return inputArray; | |
} | |
function removeIndex (arr,i) { | |
var tarr = new Array(); | |
for (var j =0; j < arr.length; j++) { | |
if (j != i) { | |
tarr[tarr.length] = arr[j]; | |
} | |
} | |
arr = tarr; | |
return arr; | |
} | |
function replaceAll (str, token, repstr) { | |
var newstr = "", ts; | |
if (str.indexOf(token) == -1) { return str; } | |
ts = str.split(token); | |
for (var i = 0; i < ts.length; i++) { | |
if (i != (ts.length - 1)) { | |
newstr += ts[i] + repstr; | |
} else { | |
newstr += ts[i]; | |
} | |
} | |
return newstr; | |
} | |
// preload_imgs( '01.gif', '02.gif' ); | |
function preload_imgs() | |
{ | |
var args = preload_imgs.arguments; | |
document.imageArray = new Array(args.length); | |
for(var i=0; i<args.length; i++) | |
{ | |
document.imageArray[i] = new Image; | |
document.imageArray[i].src = args[i]; | |
} | |
} | |
function round_money (float_amt) { | |
float_amt *= 100; | |
float_amt = Math.round(float_amt); | |
float_amt /= 100; | |
return float_amt; | |
} | |
function valid_phone(phone) { | |
var phone_re = /^((\((\d{3})\)|(\d{3}))[- .]?)?(\d{3})[- .]?(\d{4})$/; | |
return phone_re.test(phone); | |
} | |
function valid_creditcard1(cc_number) { | |
var cc_re = /^\d{4}[- .]?\d{4}[- .]?\d{4}[- .]?\d{4}[- .]?$/; | |
return cc_re.test(cc_number); | |
} | |
function valid_creditcard(cc_number) { | |
var cc_re = /^\d{3,4}[- .]?\d{3,4}[- .]?\d{3,4}[- .]?\d{3,4}$/; | |
return cc_re.test(cc_number); | |
} | |
function valid_email(email) { | |
var email_re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/; | |
return email_re.test(email); | |
} | |
function valid_website(website) { | |
var website_re = /^(http:\/\/|https:\/\/)?\w*[.]?\w+[.]\w+$/; | |
return website_re.test(website); | |
} | |
function valid_date (Y, M, D, checkForSatSun) { | |
/* | |
RANGES: | |
Y = 2003, 2004 | |
M = 0-11 | |
D = 1-31 | |
checkForSatSun = true, false; | |
*/ | |
var leap = 0; | |
var err = 0; | |
var today = new Date(); | |
var openhouse = new Date(Y, M, D); | |
var D_ofweek = openhouse.getDay(); // day number 0-6 (0=sunday) | |
var checkWeekend = checkForSatSun ? true : false; | |
// is date_input before now? | |
if (openhouse.getTime() < today.getTime()) { | |
alert("Please select a future date."); | |
return false; | |
} | |
// validate leap-year, february, day | |
if ((Y % 4 == 0) || (Y % 100 == 0) || (Y % 400 == 0)) { | |
leap = 1; | |
} | |
if ((M == 1) && (leap == 1) && (D > 29)) { | |
err = 10; | |
} | |
if ((M == 1) && (leap != 1) && (D > 28)) { | |
err = 20; | |
} | |
// 30 days has sep (8), apr (3), june (5), nov (10) | |
if ((D > 30) && ((M == "3") || (M == "5") || (M == "8") || (M == "10"))) { | |
err = 30; | |
} | |
if (err != 0) { | |
alert("Date does not exist. ("+err+")"); | |
return false; | |
} | |
if (checkWeekend && D_ofweek != 0 && D_ofweek != 6) { | |
if (!confirm("You did not choose a Saturday or a Sunday.")) { return false; } | |
} | |
return true; | |
} | |
function date_makeDayNum (Y,M,D) { | |
var d; | |
if (arguments.length != 3) { | |
d = new Date(); | |
} else { | |
d = new Date(Y,M,D); | |
} | |
return Math.ceil(((((d.getTime())/1000)/60)/60)/24); | |
} | |
function date_daysFromToday (Y,M,D) { | |
return (date_makeDayNum(Y,(M-1),D) - date_makeDayNum()); | |
} | |
function isNumberFloat(inputString) | |
{ | |
return (!isNaN(parseFloat(inputString))) ? true : false; | |
} | |
function isNumberFloat(inputString) | |
{ | |
return (!isNaN(parseInt(inputString))) ? true : false; | |
} | |
function onlyNumbers(inputString) | |
{ | |
var searchForNumbers = /\D+\_+\W+\s+\S+/ | |
(searchForNumbers.test(inputString)) ? return false : return true; | |
} | |
function isVowel(c) { | |
c = c.toUpperCase(); | |
var vowels = new Array("A","E","I","O","U"); | |
for (var i = 0; i < vowels.length; i++) { | |
if (vowels[i]==c) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function popup_window(html_file, w, h) | |
{ | |
var width=w, height=h; | |
var left = (screen.width/2) - width/2; | |
var top = (screen.height/2) - height/2; | |
var styleStr = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top; | |
var msgWindow = window.open(html_file,"popwin", styleStr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment