//////////////// dependencies  ///////////////////////////////


function CMITimespan() {
	this.Validate = Validate;
	this.Parse = Parse;
	this.ToMilliseconds = ToMilliseconds;
	this.ToFormattedString = ToFormattedString;
	this._UnitTestError = _UnitTestError;
	
	function Validate(sValue) {
		var aValue = sValue.split(":");
		if (aValue.length == 3) {
			if (isNaN(aValue[0]) || isNaN(aValue[1]) || isNaN(aValue[2])) {
				return false;
			} else {
				if (aValue[0].length >= 2 && aValue[0].length <= 4 && aValue[1].length == 2) {
					var aSecs = aValue[2].split(".");
					if (aSecs.length >= 1 && aSecs.length <= 2) {
						if (aSecs[0].length == 2) {
							if (aSecs.length == 1) {
								return true;
							} else {
								return (aSecs[1].length >= 1 && aSecs[1].length <= 2) ? true : false;
							}
						} else {
							return false;
						}
					} else {
						return false;
					}
				} else {
					return false;
				}
			}
		} else {
			return false;
		}
	}

	// Convert duration from milliseconds to CMITimespan format: 0000:00:00.00 (hhhh:mm:ss.ss).
	// Note: uses the Date type to simplify the process - from the minutes field
	//       down the Date value is correct
	function Parse(iTime) {
		var dtm = new Date(iTime);	
		var hrs = "000" + Math.floor(iTime / (60 * 60 * 1000));
		var mins = "0" + dtm.getMinutes();
		var secs = "0" + dtm.getSeconds();
		var secFraction = "0" + Math.floor(dtm.getMilliseconds() / 10);
	
		var sHMS = "";
		// remove unnecessary leading 0's
		sHMS += hrs.substr(hrs.length-4) + ":";
		sHMS += mins.substr(mins.length-2) + ":";
		sHMS += secs.substr(secs.length-2) + ".";
		sHMS += secFraction.substr(secFraction.length-2);
		//alert(sHMS);
		return sHMS;
	}
	
	function ToMilliseconds(sTime) {
		var aTime = sTime.split(":");
		if (aTime.length == 3) {
			var iMilliseconds = aTime[0] * 60 * 60 * 1000;
			iMilliseconds += aTime[1] * 60 * 1000;
			var aSecs = aTime[2].split(".");
			if (aSecs.length > 1) {
				iMilliseconds += aSecs[0] * 1000;
				// 1 or 2 digits
				aSecs[1] = aSecs[1] + "000";
				aSecs[1] = aSecs[1].substr(0, 3);
				iMilliseconds += parseInt(aSecs[1], 10);
			} else {
				iMilliseconds += aTime[2] * 1000;
			}
			return iMilliseconds;
		} else {
			return 0;
		}
	}
	
	// Convert duration from milliseconds to a specific format: 1h35m34s.
	function ToFormattedString(iMillisecs) {
		var sTimeFormatted = "";
		if (iMillisecs > 0) {
			var arrCMIDuration = this.Parse(iMillisecs).split(":");
			if (parseInt(arrCMIDuration[0], 10) > 0) {
				sTimeFormatted = parseInt(arrCMIDuration[0], 10) + "h";
			}
			sTimeFormatted += parseInt(arrCMIDuration[1], 10) + "m" + Math.round(parseFloat(arrCMIDuration[2])) + "s";
		}
		return sTimeFormatted;
	}
	
	function _UnitTestError() {
		var obj = new CMITimespan();

		if (obj.Validate("1121:03:40") != true) return "error 1.1";
		if (obj.Validate("1121:03:40.1") != true) return "error 1.2";
		if (obj.Validate("1121:03:40.12") != true) return "error 1.3";
		if (obj.Validate("21:03:40.1") != true) return "error 1.4";
		if (obj.Validate("121:03:40.1") != true) return "error 1.5";
		if (obj.Validate("A121:03:40") != false) return "error 1.6";
		if (obj.Validate("3 hours") != false) return "error 1.7";
		if (obj.Validate("") != false) return "error 1.8";
		if (obj.Validate("33330:30:40") != false) return "error 1.9";
		if (obj.Validate("3330:330:40") != false) return "error 1.10";
		if (obj.Validate("3330:30:1.34") != false) return "error 1.11";
		if (obj.Validate("3330:30:12.344") != false) return "error 1.12";
		if (obj.Parse(3630250) != "0001:00:30.25") return "error 2.1";
		if (obj.Parse(2667610) != "0000:44:27.61") return "error 2.2";
		if (obj.ToMilliseconds("0001:00:30.25") != 3630250) return "error 3.1";
		if (obj.ToMilliseconds("0000:44:27.61") != 2667610) return "error 3.2";
		if (obj.ToFormattedString(3630250) != "1h0m30s") return "error 4.1";
		if (obj.ToFormattedString(2667610) != "44m28s") return "error 4.2";
		
		return false;
	}
	
}
;
