
function FormatMoney(c) {
	var	fmtStr = new String(c+"");
	if (fmtStr == "null") {
		fmtStr = "0.00"
	} else {
		var fmtPlace = fmtStr.search("\[.]");
		if (fmtPlace == -1) {
			fmtStr= fmtStr + ".00";
		} else {
			if (fmtStr.substr(fmtPlace+1).length == 1) {
				fmtStr= fmtStr + "0";
			} else {
				fmtStr = fmtStr.substring(0,fmtPlace+3);
			}
		}
	}
	return fmtStr;
}

//Set year as four-digits
function FormatYear(ctrl) {
	var s = new String(ctrl.value);
	if (s =="") {
		y="";
	} else {
		d1 = new Date(ctrl.value);
		y = (d1.getMonth() +1) + "/" + d1.getDate() + "/"
		if (d1.getFullYear() >= 2000) {
			y+= d1.getFullYear();
		} else {
			if (d1.getYear() < 10) {
				y+="200" + d1.getYear();
			} else {
				y+="20" + d1.getYear();
			}
		}
		if (s.length > 10) {
			var h = new Number(d1.getHours());
			var n= "PM"
			if (h < 12) {
				n = "AM";
			}
			if (h == 0) h= 12;
			else {
				if (h >12 ) {
					h= h-12;
				}
			}
			var m = d1.getMinutes();
			if (m < 10) {
				m = "0" + m;
			}
			y += " " + h + ":" + m + " " + n;

		}
		//s = new Date(y);
		//var d2 = new Date();
		//if (s.valueOf() < d2.valueOf()) {
		//	alert('A date in this itinerary is past.');
		//	d2 = new Date(d2.setDate(d2.getDate()+1));
		//	y = (d2.getMonth()+1) + "/" + d2.getDate() + "/" + d2.getFullYear()
		//}
	}
	ctrl.value= y;
}

//checks Date Format
function chckDtFrmt(strDate, src) {
  	var str1 = new String(strDate);
	var source = new String(src);
	var errStr = new String("");
	if(Number(source.length)!=0){errStr = " Error at " + source + ":";}
	if(Number(str1.length) != 10){alert(errStr + " Incorrect Date Format");} else {
		var idx = new Number(str1.indexOf("-"));
		if((idx != 4)||(isNaN(idx))) {alert(errStr + " Please add "-" after Year(first four numbers).");} else {
			var idx2 = new Number(str1.lastIndexOf("-"));
			if(idx2 != 7) {alert(errStr + " Please add "-" after Month(Next two numbers after Year).");} else {
				var numVar = new Number(str1.substr(5,2));
				if((numVar>12)||(numVar<1)||(isNaN(numVar))){alert(errStr + " Wrong Month, please put 01 for January, 12 for December, and so on... as the two digits between hyphen.");} else {
					var numVar2 = new String(str1.substr(8,2));
					if((numVar2>31)||(numVar2<1)||(isNaN(numVar))){alert(errStr + " Wrong Date, please put Date as 01,02,12... as last two digits.");} else {
						var numVar3 = new Number(str1.substr(0,4));
						if((numVar3>2050)||(numVar3<1900)||(isNaN(numVar))){alert(errStr + " Wrong Year, please put year as 1999, 2001, etc. as first four digits.");}
					}
				}
			}
		}
	}
}

//Ensure that a time field is displayed as a string in hh:mm am/pm format
//or empty string if there was no time
function FormatTime(t) {
	if (t+"" == "null") rt ="";
	else {
		rt = new Date(t);
		if (isNaN(rt.getMonth())) rt = new Date("1/1/2000 " + t);
		ah = rt.getHours();
		if (isNaN(ah)) rt="";
		else {
			am = rt.getMinutes();
			if (ah < 12) {
				if (ah == 0) ah = 12;
				an = "am";
			} else {
				an ="pm";
				if (ah > 12) ah -=12;
			}
			if (am < 10) am = "0" + am;
			rt = ah + ":" + am + " " + an;
		}
	}
	return rt;
}

//Ensure that a date field is displayed as a string in MM/DD/YYYY format
//or empty string if there was no date
function FormatDate(d) {
	if (d+""=="null" || d+""=="null") {
		rd="";
	} else {
		rd=new Date(d);
		if (isNaN(rd.getMonth())) rd = "";
		else rd = (rd.getMonth()+1) + "/" + rd.getDate() + "/" + rd.getFullYear();
	}
	return rd;
}

//this is a modification of the function above. The javascrip function getFullYear()
//always return "19" as a prefix if 2 digit years are entered.
//the following function returns "20" as a prefix if the two digits entered are lesss than 50
function fnFormatDate(d) {
	if (d+""=="null" || d+""=="null") {
		rd="";
	} else {
		rd=new Date(d);
		if (isNaN(rd.getMonth())){
			rd = "";
		}else{
			var year = new Number(rd.getFullYear())
			if(year<1950){
				year = year + 100
			}
			rd.getFullYear();
			rd = (rd.getMonth()+1) + "/" + rd.getDate() + "/" + year
		}
	}
	return rd;
}

//this function will return a date in mm/dd/yyyy format, ie will return 1/1/2005 as 01/01/2005
//this format is needed for the calandar popup to highlight the correct date if either the month or day ar single digits.
function Format10DigitDate(d) {
	if (d+""=="null" || d+""=="null") {
		rd="";
	} else {
		rd=new Date(d);
		if (isNaN(rd.getMonth())) rd = "";
		else {
			mo = new String(rd.getMonth()+1);
			if (mo.length == 1) mo = "0"+mo;
			da = new String(rd.getDate());
			if (da.length == 1) da = "0"+da;
			yr = new String(rd.getFullYear());
			
			rd = mo + "/" + da + "/" + yr;
			//rd = (rd.getMonth()+1) + "/" + rd.getDate() + "/" + rd.getFullYear();
		}
	}
	return rd;
}

//function validates clientside date strings and returns false if they fail
//accepted formats dd/mm/yy or dd/mm/yyyy
//dtStr = document.formname.datefield.value
//MaxYear = the last valid year the function will pass
//TdMaxYear = the last Two digit year the funciton will pass
function ValidateDate(dtStr){
	var MaxYear = 2100;
	var TdMaxYear = 99;
	var DaysArray = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
	var m;
	var d;
	var y;
	var pos1=dtStr.indexOf("/");
	var pos2=dtStr.indexOf("/",pos1+1);
	if(pos1 == -1 || pos2 == -1){
			alert("Invalid Date");
			return false;
		}else{
			y = dtStr.substring(pos2+1);
			m = dtStr.substring(0,pos1);
			d = dtStr.substring(pos1+1,pos2);
		}
		//NON-NUMERIC
		if(isNaN(m) || isNaN(d) || isNaN(y)){
			alert("Invalid Date");
			return false;
		}
		//INCORRECT FORMAT
		if(m.length > 2 || d.lengh > 2 || (y.length != 2 && y.length != 4)){
			alert("Invalid Date (incorrect format mm/dd/yy)");
			return false;
		}
		//TO LARGE MONTH OR DAY
		if(m > 12){
			alert("Invalid Date");
			return false;
		}
		if(d > DaysArray[m-1]){
			alert("Invalid Date (There arn't that many days in this month)");
			return false;
		}
		if(y.length == 2){
			if(y > TdMaxYear){
				alert("Invalid Date (Year out of range)");
				return false;
			}
		}else{
			if(y > MaxYear){
				alert("Invalid Date (Year out of range)");
				return false;
			}
		}
		return true;
}

//validates clientside time string
function ValidateTime(tmStr){
	tmStr = "1/1/2004 " + tmStr
	var time = new Date(tmStr);
	if(!isNaN(time.getHours()) && time.getDate()==1){
		return true;
	}
	return false
}
