// Formatting functions

/**** Converts A String To Title Case Like This *****/
String.prototype.toTitleCase = function () {
	var checkwords = Array('and', 'the', 'to', 'for', 'in', 'at', 'by', 'an', 'or', 'if',
		'of', 'up', 'from');
	var inputStr=this;
	inputStr += ' ';
	var outputStr = '';
	var i = 0; 

	while (i < this.length) {
		var thischar = this.substring(i, ++i);
		if (i == 1) {
			outputStr += thischar.toUpperCase(); 
			continue;
		}
		if (thischar==' ') {
			//Check stopwords
			var checkword=false;
			for (var j=0; j<checkwords.length;j++) {
				if(checkwords[j]+' '==this.substring(i, i+checkwords[j].length+1)) {
					checkword=true; break;
				}
			}
			outputStr += !checkword ? this.substring(i-1, ++i).toUpperCase() : thischar;
		} else {
			outputStr += thischar;
		}
	}
	return (outputStr);
}
/**** End Title Case *****/

/***** Formats a currency value *****/
function formatPrice(value) {
	var currency = arguments.length>=2?arguments[1]:'&#163;';
	var showpence = arguments.length>=3?arguments[2]: true;
	if (!isNaN(value)) {
		value=new String(value);
		var point = value.lastIndexOf('.');
		if (showpence) {
			if (point==-1) { 
				value=value+'.00';
			} else {
				if (point<(value.length-3)) { value=value.slice(0, point+3);}
				if (point>(value.length-3)) { value=value+'0';}
			}
		} else {
			if (point!=-1) { value = value.slice(0, point-1); }
		}
		return (currency+value);
	} else {
		return (value);
	}
}
/* End format price */

/*** Displays a file size in Mb, Kb etc based on number of bytes ***/
function formattedSize(filesize) {
	var formattedSize;
	if (!isNaN(filesize)) {
		if(filesize > Math.pow(10,9)) {
			formattedSize = Number(filesize/Math.pow(10,9)).toFixed(1) + 'Gb';
		} else if (filesize > Math.pow(10,6)) {
			formattedSize = Math.ceil(filesize/Math.pow(10,6)) + 'Mb';
		} else if (filesize > Math.pow(10,3)) {
			formattedSize = Math.ceil(filesize/Math.pow(10,3)) + 'kb';
		} else {
			formattedSize = filesize + 'b';
		}
	}
	return (formattedSize);
}
/*** End format file size */