	
/**
 * Capitalise first letter of each word, lower case the rest of the word
 **/ 
function toTitleCase(str) {
    if (str !== undefined)
	    return str.replace(/\b\w+/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}

/**
 * Get parameters from URL
 **/
function urlParam(name) {
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results == null) {
       return null;
    }
    else {
       return decodeURI(results[1]) || 0;
    }
}

/**
 * Check string is a valid timestamp
 **/
 function isValidTimestamp(timestamp) {
	 
	 if ($.isNumeric(timestamp)) {
		 return (new Date(parseInt(timestamp)).getTime() > 0);
	 } else {
		 return false;
	 }
 }

 /**
  * Sort li tags within a ul
  */
 function sortList($ul) {
    if ( $ul.is('ul') ) {
        var $listItems = $ul.children('li');
        $listItems.sort(function(a, b) {
            return ( $(a).text() > $(b).text() ? 1 : -1 );
        });
        $ul.empty().append($listItems);
    }
 }
 
 /**
  * Sort select options
  */
 function sortSelect($select) {
	if ( $select.is('select') ) {
		var $selectOptions = $select.children('option');
		$selectOptions.sort(function(a, b) {
			return ( $(a).val() > $(b).val() ? 1 : -1 );
		});
		$select.empty().append($selectOptions);
	}
 }

 /**
  * Get the server's UT milliseconds since epoch
  */
 function getServerMillis() {
    return $.ajax({
         type: 'POST',
         url: '/helpers.php',
         data: {action: 'getServerMillis'},
     });
 }