/*
 * Copyright (C) Jonathan Turkanis and Lincoln Law Center LLC 2006
 * File: pub/controls/datetime.js
 * Date: 2006-10-01
 * Description: Contains date and time formatting functions
 */

DATE_weekdays = 
	Array( 'Sunday', 'Monday', 'Tuesday', 'Wednesday',
		   'Thursday', 'Friday', 'Saturday' );
DATE_months = 
	Array( 'January', 'February', 'March', 'April', 'May', 'June', 'July', 
		   'August', 'September', 'October', 'November', 'December' );

function DATE_specifier(type)
{
	this.type = type;
	this.convert = 
		function(year, mon, day, hour, min, sec, wday)
		{
			switch (type) {
				
    		case "a": return this.weekday(wday, 1);
    		case "A": return this.weekday(wday, 0);
		    case "b": return this.month(mon, 1);
		    case "B": return this.month(mon, 0);
		    case "c": 
		    	return this.weekday(wday, 1) + " " +
		    		   this.month(mon, 1) + " " +
		    		   this.pad(day, " ") + " " +
		    		   this.pad(hour, " ") + ":" +
		    		   this.pad(min, " ") + ":" +
		    		   this.pad(sec, " ") + " " +
		    		   (year + 1900);
		    case "d": return this.pad(day, "0");
		    case "D": return this.pad(mon, "0") + "/" +
							 this.pad(day, "0") + "/" +
							 year % 100;
		    case "e": return this.pad(day, "");
		    case "F": return year + 1900 + "-" + 
		    				 this.pad(mon, "0") + "-" +
							 this.pad(day, "0");
		    case "h": return this.month(mon, 1);
		    case "H": return this.pad(hour, "0");
		    case "I": return this.pad(hour % 12, "0");
		    case "m": return this.pad(mon + 1, "0");
		    case "M": return this.pad(min, "0");
		    case "n": return "\n";
		    case "p": return hour > 11 ? "PM" : "AM";
		    case "r": return this.pad(hour % 12, "0") + ":" +
							 this.pad(min, "0") + ":" +
							 this.pad(sec, "0") + " " +
		    				 (hour > 11 ? "PM" : "AM");
		    case "R": return this.pad(hour, "0") + ":" +
							 this.pad(min, "0");
		    case "S": return this.pad(sec, "0");
		    case "t": return "\t";
		    case "T": return this.pad(hour, "0") + ":" +
							 this.pad(min, "0") + ":" +
							 this.pad(sec, "0");
		    case "u": return (wday - 1) % 7 + 1;
		    case "w": return wday;
		    case "x": return this.pad(mon, "0") + "/" +
							 this.pad(day, "0") + "/" +
							 year % 100;
		    case "X": return this.pad(hour, "0") + ":" +
							 this.pad(min, "0") + ":" +
							 this.pad(sec, "0");
		    case "y": return this.pad(year % 100, "0");
		    case "Y": return year + 1900;
		    case "%": return "%";
		    case "Ec": case "EC": case "j": case "r": case "R":
		    case "Er": case "U": case "V": case "W": case "Ex":
		    case "EX": case "Ey": case "EY": case "z": case "Z": 
		    default:
		    	throw new Error("Specifier %" + type + " not supported");
			}
		}
	this.weekday = 
		function(wday, abbr)
		{
			var w = DATE_weekdays[wday];
			return abbr ?  w.substr(0, 3) : w;
		}
	this.month = 
		function(mon, abbr)
		{
			var m =  DATE_months[mon - 1];
			return abbr ?  m.substr(0, 3) : m;
		}
	this.pad = 
		function(val, pad)
		{
			v = val + "";
			return v.length == 1 ? pad + v : v;
		}
}

function DATE_format(fmt)
{
	this.rep = Array();
	var spec = false;
	var cur = "";
	for (var z = 0, n = fmt.length; z < n; ++z) {
		var c = fmt.charAt(z);
		if (spec) {
			spec = false;
			this.rep[this.rep.length] = new DATE_specifier(c);
		} else {
			if (c == '%') {
				spec = true;
				if (cur.length)
					this.rep[this.rep.length] = cur;
				cur = "";
			} else {
				cur += c;
			}
		}
	}
	if (cur.length)
		this.rep[this.rep.length] = cur;
	this.print = 
		function(year, mon, day, hour, min, sec, wday)
		{
			var result = "";
			for (var z = 0, n = this.rep.length; z < n; ++z) {
				var fmt = this.rep[z];
				result += typeof(fmt) == "string" ? 
					fmt :
					fmt.convert(year, mon, day, hour, min, sec, wday);
			}
			return result;
		};
}

function DATE_print(fmt, year, mon, day, hour, min, sec, wday)
{
	//alert('fmt = ' + fmt + '; year = ' + year + '; mon = ' + mon + '; day = ' + day)
	if (typeof(fmt) == "string")
		fmt = new DATE_format(fmt);
	return fmt.print(year, mon, day, hour, min, sec, wday);
}
