/**
 * kludge push function because IE on Mac
 * does not support the push method on Array objects
 */
function arrayPush (theArray, theItem) {
	var nextIdx = theArray.length;
	theArray[nextIdx] = theItem;
	return theArray;
}

/**
 * Performs a linear search of theArray for theItem
 * returns the index for the element where theArray[index] == theItem 
 * returns -1 if theItem is not found in the array
 */
function arraySearch (theArray, theItem) {
	var theIndex = -1;
	for (var i = 0; i < theArray.length; i++) {
		if (theArray[i] == theItem) {
			theIndex = i;
			break;
		}
	}
	return theIndex;
}

/**
 * looks through haystack and returns the string that comes after needlePrefix and before
 * needlePostfix.  Returned needle does not include the prefix or the postfix.
 *
 * returns null if nothing is found.
 *
 * haystackStart is an offset into the haystack to begin the search
 *
 * if caseSensitiveP is false a case insensative search is performed, but the result
 * is case retentive. Defaults to true (case sensative)
 *
 */
function getInString (haystack, haystackStart, needlePrefix, needlePostfix, caseSensativeP) {
	var needle = null;
	var tmpHaystack;
	if (caseSensativeP != null && !caseSensativeP) {
		tmpHaystack = haystack.toUpperCase();
		needlePrefix = needlePrefix.toUpperCase();
		needlePostfix = needlePostfix.toUpperCase();
	} else {
		tmpHaystack = haystack;
	}
	var pos = tmpHaystack.indexOf(needlePrefix, haystackStart);
	var start = 0;
	var stop = 0;
		
	if (pos != -1) {
		start = pos + needlePrefix.length;
		stop = tmpHaystack.indexOf(needlePostfix,start);
		if (stop == -1) {
			stop = tmpHaystack.length;
		}
		needle = haystack.substring(start,stop);
	}
	return needle;
}

function formatDate () {
	var yearNum;
	var monthNum;
	var dayNum;
	var curArg = 0;
	if (typeof arguments[curArg] == "object") {
		//assume its a date object
		var theDate = arguments[curArg];
		yearNum = theDate.getFullYear();
		monthNum = theDate.getMonth();
		dayNume = theDate.getDate();
	} else {
		// assume its a string in the format yyyy-mm-dd
		var dateArray = (arguments[curArg]).split('-');
		yearNum = dateArray[0];
		monthNum = dateArray[1];
		if (monthNum.length == 1) {
			monthNum = '0' + '' + monthNum;
		}
		dayNum = dateArray[2];
	}
	curArg++;
	var out = "";
	var theItem;
	for (; curArg < arguments.length; curArg++) {
		theItem = arguments[curArg];
		switch (theItem) {

		case 'YYYY':
			out += yearNum;
			break;

		case 'YY':
			out += yearNum.substr(2,2);
			break;

		case 'MM':
			out += monthNum;
			break;
			
		case 'MON':
			if (monthNum == '01') {
				out += 'Jan';
			} else if (monthNum == '02') {
				out += 'Feb';
			} else if (monthNum == '03') {
				out += 'Mar';
			} else if (monthNum == '04') {
				out += 'Apr';
			} else if (monthNum == '05') {
				out += 'May';
			} else if (monthNum == '06') {
				out += 'Jun';
			} else if (monthNum == '07') {
				out += 'Jul';
			} else if (monthNum == '08') {
				out += 'Aug';
			} else if (monthNum == '09') {
				out += 'Sep';
			} else if (monthNum == '10') {
				out += 'Oct';
			} else if (monthNum == '11') {
				out += 'Nov';
			} else if (monthNum == '12') {
				out += 'Dec';
			}
			break;

		case 'MONTH':
			if (monthNum == '01') {
				out += 'January';
			} else if (monthNum == '02') {
				out += 'February';
			} else if (monthNum == '03') {
				out += 'March';
			} else if (monthNum == '04') {
				out += 'April';
			} else if (monthNum == '05') {
				out += 'May';
			} else if (monthNum == '06') {
				out += 'June';
			} else if (monthNum == '07') {
				out += 'July';
			} else if (monthNum == '08') {
				out += 'August';
			} else if (monthNum == '09') {
				out += 'September';
			} else if (monthNum == '10') {
				out += 'October';
			} else if (monthNum == '11') {
				out += 'November';
			} else if (monthNum == '12') {
				out += 'December';
			}
			break;
			
		case 'DD':
			if (dayNum.length == 1) {
				out += '0';
			}
			out += dayNum;
			break;
		
		case 'D':
			if (dayNum.charAt(0) == '0') {
				out += dayNum.charAt(1);
			} else {
				out += dayNum;
			}
			break;
			
		default:
			out += theItem;
			break;
		}
	}
	return out;
}

function formatBytes (bytes) {
	if (bytes < 1048576) {
		return '< 1';
	}
	mb = bytes / 1048576;
	roundMb = (Math.round(mb * 10)/10);
	return roundMb;
}
