//Steve Fosdal wrote the following functions...

function FormatAsDate(theObject, desc) {
	theObject = MakeObject(theObject);
        if(!theObject.value){return true;}
	theOriginalValue = theObject.value;
        if(desc==null){desc='';}else{desc=desc+': Must be a date. ';}

	
	var in_date;
	var in_time = "";
	if(theObject.value.indexOf(" ") == -1){
		in_date = theObject.value;
	}else{
		in_date = theObject.value.substring(0,theObject.value.indexOf(" "));
		in_time = theObject.value.substring(theObject.value.indexOf(" ") + 1,theObject.value.length).toUpperCase();
	}
	
	
	in_date = in_date.replace(/\s/gi,'');
	in_date = in_date.replace(/\\/gi,'/');
	in_date = in_date.replace(/\./gi,'/');
	in_date = in_date.toUpperCase();
	var date_is_bad = 0;  
	if(!allowInString(in_date,"/0123456789T+-")){
		date_is_bad = 1; // invalid characters in date
	}
	if(!date_is_bad){ 
		var has_rdi = 0;
		if(in_date.indexOf("T")>=0){ 
			has_rdi = 1;
		}else{
			in_date = in_date.replace(/\-/gi,'/');
		}
		if (!date_is_bad && has_rdi && (in_date.indexOf("T") != 0)) { 
			date_is_bad = 2; // relative date index character is not in first position
		}
		if (!date_is_bad && has_rdi && (in_date.length == 1)) { 
			var d = new Date();
			var return_month = parseInt(d.getMonth() + 1).toString();
			return_month = (return_month.length==1 ? "0" : "") + return_month; 
			var return_date =  parseInt(d.getDate()).toString();
			return_date = (return_date.length==1 ? "0" : "") + return_date; 
			in_date = return_month + "/" + return_date + "/" + getFullYear(d);		
			has_rdi = 0; // date doesn't have rdi char anymore (will also cause failure of add'l rdi checks, which is a good thing)
		}
		if (!date_is_bad && has_rdi && (in_date.length > 1) && !(in_date.charAt(1) == "+" || in_date.charAt(1) == "-")) {
			date_is_bad = 3; // length of rdi string is greater than 1 but second char is not "+" or "-"
		}
		if (!date_is_bad && has_rdi && isNaN(parseInt(in_date.substring(2,in_date.length),10))) {
			date_is_bad = 4; // rdi value is not a number
		}
		if (!date_is_bad && has_rdi && (parseInt(in_date.substring(2,in_date.length),10) < 0)) {
			date_is_bad = 5; // rdi value is not a positive integer
		}
		if (!date_is_bad && has_rdi) {
			var d = new Date();
			ms = d.getTime();
			offset = parseInt(in_date.substring(2,in_date.length),10);
			if(in_date.charAt(1) == "+") {
				ms += (86400000 * offset);
			}else{
				ms -= (86400000 * offset);
			}
			d.setTime(ms);
			var return_month = parseInt(d.getMonth() + 1).toString();
			return_month = (return_month.length==1 ? "0" : "") + return_month; 
			var return_date =  parseInt(d.getDate()).toString();
			return_date = (return_date.length==1 ? "0" : "") + return_date; 
			in_date = return_month + "/" + return_date + "/" + getFullYear(d);	
			has_rdi = 0;
		}
	} 
	if(!date_is_bad){
		var date_pieces = new Array();
		date_pieces = in_date.split("/");
		if(date_pieces.length == 2) {
			var d = new Date();
			in_date = in_date + "/" + getFullYear(d);
			date_pieces = in_date.split("/");
		}
		if (date_pieces.length != 3 || parseInt(date_pieces[0],10) < 1 || parseInt(date_pieces[0],10) > 12 
			|| parseInt(date_pieces[1],10) < 1 || parseInt(date_pieces[1],10) > 31 
			|| (date_pieces[2].length != 2 && date_pieces[2].length != 4)) {
			date_is_bad = 6;  // date is not in format of m[m]/d[d]/yy[yy]
		}
	}
	
	
	if(date_is_bad){
		alert(desc+"'"+theOriginalValue+"' is not a valid date (m[m]/d[d]/yy[yy], t, t+n or t-n).");
		theObject.value='';
		theObject.focus();
		return (false);
	}
	
	
	//---Time formatting.
	var time_is_bad = 0;
	if(!allowInString(in_time,":0123456789 APM")){
		time_is_bad = 1; // invalid characters in time
	}
	if(!time_is_bad && in_time.substring(0,in_time.indexOf(":") == -1)){
		time_is_bad = 2; // no colon in the time.
	}
	if(!time_is_bad){ 
		var theHour = in_time.substring(0,in_time.indexOf(":"));
		var theMinute = in_time.substring(in_time.indexOf(":")+1,in_time.indexOf(":")+3);
		var theAMPM = in_time.substring(in_time.indexOf(":")+4,in_time.indexOf(":")+6);	
		
		if(isNaN(theHour) || isNaN(theMinute)){
			time_is_bad = 3; // hour or minutes not a number
		}
		else if((Number(theHour) > 12 || Number(theHour) < 0) || (Number(theMinute) > 59 || Number(theMinute) < 0)){
			time_is_bad = 4; // hour or minutes not in correct numeric format
		}
		else if(theAMPM != "AM" && theAMPM != "PM"){
			theAMPM = in_time.substring(in_time.indexOf(":")+3,in_time.indexOf(":")+5);
			if(theAMPM != "AM" && theAMPM != "PM"){ // Double check to see if the time was formatted "12:00PM"
				time_is_bad = 5; // AM PM not formatted correctly
			}	
		}
	}
	
		var ms = Date.parse(in_date);
        var d = new Date();
        d.setTime(ms);
        var return_date = d.toLocaleString();
        var return_month = parseInt(d.getMonth() + 1).toString();
        return_month = (return_month.length==1 ? "0" : "") + return_month; 
        var return_date =  parseInt(d.getDate()).toString();
        return_date = (return_date.length==1 ? "0" : "") + return_date; 
        return_date = return_month + "/" + return_date + "/" + getFullYear(d);
        theObject.value = return_date;
        var return_time = ""
        
        if(!time_is_bad && in_time != ""){
        	return_time = " " + theHour + ":" + theMinute + " " + theAMPM
			theObject.value = theObject.value + return_time
        }
        else if(time_is_bad && in_time != ""){
			if(desc!=''){desc=desc.replace(': Must be a date. ',' : Must be a date with a properly formatted time. ');}
			alert(desc+"'"+in_time+"' is not a valid time (h[h]:mm PM).");	
			return false;
		}	
        
        
        return true;
}

function getFullYear(d) {
	var y = ""
	if (d.getFullYear() != null){
		y = d.getFullYear();
		if (y < 1970) y+= 100;		
	}else{	
		y = d.getYear();
		if(y > 69  && y < 100){
			y += 1900;
		}
		if(y < 1000){
			y += 2000;
		}
	}
	return y;
}

function FormatAsTime(theObject){

    theObject = MakeObject(theObject);
    theOriginalValue = theObject.value;

    in_time = theObject.value.toUpperCase();
    
	var time_is_bad = 0;
	if(!allowInString(in_time,":0123456789 APM")){
		time_is_bad = 1; // invalid characters in time
	}
	if(!time_is_bad && in_time.substring(0,in_time.indexOf(":") == -1)){
		time_is_bad = 2; // no colon in the time.
	}
	if(!time_is_bad){ 
		var theHour = in_time.substring(0,in_time.indexOf(":"));
		var theMinute = in_time.substring(in_time.indexOf(":")+1,in_time.indexOf(":")+3);
		var theAMPM = in_time.substring(in_time.indexOf(":")+4,in_time.indexOf(":")+6);	
		
		if(isNaN(theHour) || isNaN(theMinute)){
			time_is_bad = 3; // hour or minutes not a number
		}
		else if((Number(theHour) > 12 || Number(theHour) < 0) || (Number(theMinute) > 59 || Number(theMinute) < 0)){
			time_is_bad = 4; // hour or minutes not in correct numeric format
		}
		else if(theAMPM != "AM" && theAMPM != "PM"){
			theAMPM = in_time.substring(in_time.indexOf(":")+3,in_time.indexOf(":")+5);
			if(theAMPM != "AM" && theAMPM != "PM"){ // Double check to see if the time was formatted "12:00PM"
				time_is_bad = 5; // AM PM not formatted correctly
			}	
		}
	}
	
	if(time_is_bad && theOriginalValue != ""){
	    theObject.value = ""
	    alert("\"" + theOriginalValue + "\" is not a valid time (h[h]:mm PM). ");
	    return false; 
	}
	else{
	    return true;
	}
}


function allowInString (InString, RefString)  {
	if(InString.length==0){return (false);}
	for(var Count=0; Count < InString.length; Count++){
		var TempChar= InString.substring (Count, Count+1);
		if(RefString.indexOf (TempChar, 0)==-1){
			return (false);
		}
	}
	return (true);
}
