function createHTMLElement(type, text, as, ss, es)
{
	var a, i, j;
	var ce = document.createElement(type);
	if (text != "") ce.appendChild(document.createTextNode(text));
	if (as != null)
	{
  	a = as;
  	for (i=0; i<a.length; i++)
  	{
  		j = a[i].indexOf(":");
  		if (j != -1) eval("ce." + a[i].substr(0, j) + " = a[i].substr(j + 1);");
  	}
	}
	if (ss != null)
	{
  	a = ss;
  	for (i=0; i<a.length; i++)
  	{
  		j = a[i].indexOf(":");
  		if (j != -1) eval("ce.style." + a[i].substr(0, j) + " = a[i].substr(j + 1);");
  	}
	}
	if (es != null)
	{
  	a = es;
  	for (i=0; i<a.length; i++)
  	{
  		j = a[i].indexOf(":");
  		if (j != -1) eval("addEvent(ce, a[i].substr(0, j), " + a[i].substr(j + 1) + ");");
  	}
	}
	return(ce);
}

var datetime_window = "";
addEvent(window, "load", datetime_initialize);
addEvent(window, "focus", datetime_focus);

function datetime_initialize()
{
	var args, i;
	var spans = document.getElementsByTagName("span");
	for (i=0; i<spans.length; i++)
	{
		switch (spans[i].className)
		{
		case "datetime_date":
			args = spans[i].firstChild.nodeValue.split(";");
			if (args.length == 2)
			{
      	while (spans[i].childNodes.length > 0)
      	{
      		spans[i].removeChild(spans[i].firstChild);
      	}
				datetime_date_initialize(spans[i], args[0], args[1]);
			}
			break;
		case "datetime_time":
			args = spans[i].firstChild.nodeValue.split(";");
			if (args.length == 2)
			{
      	while (spans[i].childNodes.length > 0)
      	{
      		spans[i].removeChild(spans[i].firstChild);
      	}
				datetime_time_initialize(spans[i], args[0], args[1]);
			}
			break;
		}
	}
}
function datetime_focus()
{
	if (!datetime_window.closed && datetime_window.location && window.focus) datetime_window.focus();
}

function datetime_date_initialize(p, n, v)
{
	var container, d;
	if (v == "") d = new Date();
	else d = new Date(v);
	v = (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear();
	container = createHTMLElement("span", "", null, Array("backgroundColor:white", "border:1px dashed black", "font:8pt courier", "padding:1px", "whiteSpace:nowrap"), null);
	container.appendChild(createHTMLElement("span", d.toDateString(), null, Array("width:125px", "textAlign:center"), null));
	container.appendChild(createHTMLElement("img", "", Array("src:/_global/datetime_calendar.bmp"), null, Array("click:datetime_date_calendar")));
	container.appendChild(createHTMLElement("input", "", Array("id:" + n, "name:" + n, "type:hidden", "value:" + v), null, null));
	p.appendChild(container);
}
function datetime_date_calendar(e)
{
	var target;
	if (!e) e = window.event;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) target = target.parentNode;
	datetime_window = document.open("/_global/datetime_calendar.asp?cn=" + target.nextSibling.id, "_blank", "top=" + e.screenY + ", left=" + e.screenX + ", height=160, width=200, location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no");
}
function datetime_time_initialize(p, n, v)
{
	var container, d;
	if (v == "") d = new Date();
	else d = new Date("1/1/2000 " + v);
	d.setMinutes(parseInt(d.getMinutes() / 15) * 15)
	d = datetime_time_output(d);
	// container
	container = createHTMLElement("span", "", null, Array("backgroundColor:white", "border:1px dashed black", "font:8pt courier", "whiteSpace:nowrap"), null);
	// hours
	container.appendChild(createHTMLElement("span", d[0], null, Array("textAlign:center", "width:20px"), null));
	container.appendChild(createHTMLElement("span", "+", null, Array("backgroundColor:lightgreen", "border:1px solid black", "color:black", "cursor:hand", "textAlign:center"), Array("click:datetime_time_hi")));
	container.appendChild(createHTMLElement("span", "-", null, Array("backgroundColor:pink", "border:1px solid black", "color:black", "cursor:hand", "textAlign:center"), Array("click:datetime_time_hd")));
	// minutes
	container.appendChild(createHTMLElement("span", d[1], null, Array("textAlign:center", "width:20px"), null));
	container.appendChild(createHTMLElement("span", "+", null, Array("backgroundColor:lightgreen", "border:1px solid black", "color:black", "cursor:hand", "textAlign:center"), Array("click:datetime_time_mi")));
	container.appendChild(createHTMLElement("span", "-", null, Array("backgroundColor:pink", "border:1px solid black", "color:black", "cursor:hand", "textAlign:center"), Array("click:datetime_time_md")));
	// ampm
	container.appendChild(createHTMLElement("span", d[2], null, Array("textAlign:center", "width:25px"), null));
	container.appendChild(createHTMLElement("span", "+", null, Array("backgroundColor:lightgreen", "border:1px solid black", "color:black", "cursor:hand", "textAlign:center"), Array("click:datetime_time_api")));
	container.appendChild(createHTMLElement("span", "-", null, Array("backgroundColor:pink", "border:1px solid black", "color:black", "cursor:hand", "textAlign:center"), Array("click:datetime_time_apd")));
	// value
	container.appendChild(createHTMLElement("input", "", Array("name:" + n, "type:hidden", "value:" + d[0] + ":" + d[1] + " " + d[2]), null, null));
	// finally
	p.appendChild(container);
}
function datetime_time_output(d)
{
	var t = new Array(3);
	t[0] = d.getHours() % 12;
	if (t[0] == 0) t[0] = 12;
	t[1] = d.getMinutes()
	if (t[1] < 10) t[1] = "0" + t[1];
	if (d.getHours() < 12) t[2] = "AM";
	else t[2] = "PM";
	return(t);
}
function datetime_time_update(e, i, v)
{
	var d, target;
	if (e.target) target = e.target;
	else if (e.srcElement) target = e.srcElement;
	if (target.nodeType == 3) target = target.parentNode;
	d = new Date("1/1/2000 " + target.parentNode.childNodes(9).value);
	eval("d.set" + i + "(d.get" + i + "() + v);");
	d = datetime_time_output(d);
	target.parentNode.childNodes(0).firstChild.nodeValue = d[0];
	target.parentNode.childNodes(3).firstChild.nodeValue = d[1];
	target.parentNode.childNodes(6).firstChild.nodeValue = d[2];
	target.parentNode.childNodes(9).value = d[0] + ":" + d[1] + " " + d[2];
}
function datetime_time_hi(e)
{
	if (!e) e = window.event;
	datetime_time_update(e, "Hours", 1);
}
function datetime_time_hd(e)
{
	if (!e) e = window.event;
	datetime_time_update(e, "Hours", -1);
}
function datetime_time_mi(e)
{
	if (!e) e = window.event;
	datetime_time_update(e, "Minutes", 15);
}
function datetime_time_md(e)
{
	if (!e) e = window.event;
	datetime_time_update(e, "Minutes", -15);
}
function datetime_time_api(e)
{
	if (!e) e = window.event;
	datetime_time_update(e, "Hours", 12);
}
function datetime_time_apd(e)
{
	if (!e) e = window.event;
	datetime_time_update(e, "Hours", -12);
}
