function zeropad (n, toplaces, post) {
	var ret = ""+n;
	var foo = toplaces - ret.length;
	for (var i=0; i<foo; i++)
		ret = post ? ret+"0" : "0"+ret;
	return ret;
}

function timecode_fromsecs(rawsecs, fract, alwaysfract) {
	if (fract == undefined) fract = false;
	if (alwaysfract == undefined) alwaysfract = false;
	// returns a string in HH:MM:SS[.xxx] notation
	// if fract is True, uses .xxx if either necessary (non-zero) OR alwaysfract is True
	var hours = Math.floor(rawsecs / 3600);
	rawsecs -= hours*3600;
	var mins = Math.floor(rawsecs / 60);
	rawsecs -= mins*60
	if (fract) {
		secs = Math.floor(rawsecs)
		rawsecs -= secs
		if ((rawsecs > 0) || alwaysfract) {
			fract = zeropad((""+rawsecs).substr(2, 3), 3, true);
			return zeropad(hours, 2)+":"+zeropad(mins, 2)+":"+zeropad(secs, 2)+","+fract;
		} else {
			return zeropad(hours, 2)+":"+zeropad(mins, 2)+":"+zeropad(secs, 2);
		}
	} else {
		var secs = Math.round(rawsecs);
		return zeropad(hours, 2)+":"+zeropad(mins, 2)+":"+zeropad(secs, 2);
	}
}

timecode_tosecs_pat = /^(\d\d):(\d\d):(\d\d)(,(\d\d\d))?$/;

function timecode_tosecs(tcstr) {
	// seeks and returns first timecode pattern and returns it in secs
	// nb: timecode can appear anywhere in string, will only convert first match
	// note that the parseInt(f, 10), the 10 is necessary to avoid "09" parsing as octal (incorrectly returns 0 then since 9 is an invalid octal digit)
	r = tcstr.match(timecode_tosecs_pat);
	if (r) {
		ret = 0;
		ret += 3600 * parseInt(r[1], 10);
		ret += 60 * parseInt(r[2], 10);
		ret += parseInt(r[3], 10);
		if (r[5]) {
			ret = parseFloat(ret+"."+r[5]);
		}
		return ret;
	} else {
		return undefined;
	}
}

