﻿// String formatting function for javascript similar to C#'s string.format
// usage: format('My name is {0} and I am {1}', 'Mikey', '10');
function format(str)
{
  for(i = 1; i < arguments.length; i++)
  {
    str = str.replace('{' + (i - 1) + '}', arguments[i]);
  }
  return str;
}

// scott andrew (www.scottandrew.com) wrote this function. thanks, scott!
// adds an eventListener for browsers which support it.
function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, true);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        return false;
    }
}

// check that a value is strictly numeric
function isNumber(number) {
    if (number == '') { return false; }
    return (number.match(/-*[0-9]+/) == number);
}

function padStr(num, totalChars, padWith) {

    num = num + "";
    
    padWith = (padWith) ? padWith : "0";
    if (num.length < totalChars) {
        while (num.length < totalChars) {	
            num = padWith + num;		
        }
    }
    if (num.length > totalChars) {
 	    //if padWith was a multiple character string and num was overpadded
        num = num.substring((num.length - totalChars), (totalChars+1));
 	}
 	return num;
}

function getElementsByClass(searchClass, node, tag) {
    var classElements = new Array();
    if (node == null)
        node = document;
    if (tag == null)
        tag = '*';
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\s)" + searchClass + "(\\s|$)");
    for (i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}

