var daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var daynames = new Array("Sun","Mon", "Tue", "Wed","Thu", "Fri", "Sat");
var mm = new Array("Jan","Feb", "Mar", "Apr","May", "Jun", "Jul","Aug","Sep","Oct","Nov","Dec");
function getDays(year, month) {
if (month != 1) return daysInMonth[month];
return ((0==year%4)&&(year%100))||(0==year%400)?29:28;
}
function getTitle(year, month) {
month++;
//	return year + "Äê" + month + "ÔÂ <font style='font-family: arial;'>( "+mm[month-1]+" )</font>";
//	return year + "&#24180;" + month + "&#26376; <font style='font-family: arial;'>( "+mm[month-1]+" )</font>"; // CHINESE FORMAT
return "<font style='font-family: arial;'>"+mm[month-1]+" "+year+" </font>"; // ENGLISH FORMAT
}
function getObject(e)
{
if (!e || !e.target)
obj = event.srcElement;
else if (e.target.nodeType == 3)
obj = e.target.parentNode;
else
obj = e.target;
return obj;
}
var activecell = null;
function mouseover2(e) {
obj = getObject(e);
if (activecell == obj)
obj.className = obj.className.replace(/btnUp/, 'btnDown');
}
function addEvent(el, name, func) {
if (el.attachEvent)
el.attachEvent("on" + name, func);
else if (el.addEventListener)
el.addEventListener(name, func, false);
else
el["on" + name] = func;
}
function removeEvent(el, name, func) {
if (el.detachEvent)
el.detachEvent("on" + name, func);
else if (el.removeEventListener)
el.removeEventListener(name, func, true);
else
el["on" + name] = null;
}
function getPosition(el) {
var SL = 0, ST = 0;
var is_div = /^div$/i.test(el.tagName);
if (is_div && el.scrollLeft)
SL = el.scrollLeft;
if (is_div && el.scrollTop)
ST = el.scrollTop;
var r = {x: el.offsetLeft - SL, y: el.offsetTop - ST};
if (el.offsetParent) {
var tmp = getPosition(el.offsetParent);
r.x += tmp.x;
r.y += tmp.y;
}
return r;
};
function changeClass(cell, olds, news) {
if (!cell.tagName) cell = event.srcElement;
if ((pos = cell.className.indexOf(olds))>=0)
cell.className = cell.className.substr(0, pos) + news;
}
function mouseUp(e) {
obj = getObject(e);
activecell = null;
changeClass(obj, 'btnDown', 'btnUp');
}
var mouseX=mouseY=0;
function mouseDown(e) {
obj = getObject(e);
activecell = obj;
changeClass(obj, 'btnUp', 'btnDown');
if (obj.dragevent) {
if (!e) e = window.event;
if (e.target) {
mouseX = e.pageX;
mouseY = e.pageY;
}
else {
mouseX = e.clientX;
mouseY = e.clientY;
}
return false;
}
}
function mouseMove(e) {
obj = getObject(e);
if (obj.dragevent && (activecell==obj)) {
if (!e) e = window.event;
if (e.target) {
obj.dragevent(e.pageX - mouseX, e.pageY - mouseY);
mouseX = e.pageX;
mouseY = e.pageY;
}
else {
obj.dragevent(e.clientX - mouseX, e.clientY - mouseY);
mouseX = e.clientX;
mouseY = e.clientY;
}
return false;
}
}
function mouseOut(e) {
obj = getObject(e);
changeClass(obj, 'btnDown', 'btnUp');
}
function mouseOver(e) {
obj = getObject(e);
if (activecell == obj)
changeClass(obj, 'btnUp', 'btnDown');
}
function makeButton(el, dragevent) {
el.className = "btnUp";
el.setAttribute('UNSELECTABLE','on');
if (dragevent) {
el.dragevent = dragevent;
addEvent(el, 'mousemove', mouseMove);
}
addEvent(el, 'mouseup', mouseUp);
addEvent(el, 'mouseout', mouseOut);
addEvent(el, 'mousedown', mouseDown);
addEvent(el, 'mouseover', mouseOver);
}
function Calendar(id, callback, autohide) {
if (document.layers) return;
if (document.getElementById) {
this.table = document.getElementById(id);
this.rows = this.table.getElementsByTagName("tr");
}
else {
this.table = document.all[id];
this.rows = this.table.rows;
}
this.table.cellSpacing = 0;
this.callback = callback;
var i=0, cell = this.rows[0].firstChild;
var name = id + '_cal';
eval(name + "=this");
addEvent(this.table, 'mouseup', new Function("activecell=null"));
with (this.rows[0]) {
addEvent(cells[0], 'click', new Function(name + ".nextMonth(-12)"));
addEvent(cells[1], 'click', new Function(name + ".nextMonth(-1)"));
addEvent(cells[3], 'click', new Function(name + ".nextMonth(1)"));
addEvent(cells[4], 'click', new Function(name + ".nextMonth(12)"));
makeButton(cells[0]); makeButton(cells[1]);
makeButton(cells[3]); makeButton(cells[4]);
if (autohide) makeButton(cells[2], new Function("x", "y", name + ".moveBy(x,y)"));
else makeButton(cells[2]);
}
this.rows[1].firstChild.className = "sunday";
this.rows[1].lastChild.className = "satday";
cell = this.rows[1].firstChild;
while (cell) {
cell.setAttribute('UNSELECTABLE','on');
cell.innerHTML = "&nbsp;"+daynames[i++]+"&nbsp;&nbsp;";
cell = cell.nextSibling;
}
var clicked = new Function(name + ".dateClicked(this)");
var mouseover = new Function("changeClass(this, 'btnOut', 'btnOver')");
var mouseout = new Function("changeClass(this, 'btnOver', 'btnOut')");
for (i=2; i<this.rows.length; i++) {
cell = this.rows[i].firstChild;
while (cell) {
addEvent(cell, 'mousedown', clicked);
addEvent(cell, 'mouseover', mouseover);
addEvent(cell, 'mouseout', mouseout);
cell = cell.nextSibling;
}
}
if (!autohide&&(pos = location.href.indexOf("&date="))>0)
day = make_date(location.href.substring(pos+6));
else day = new Date();
year = day.getFullYear();
month = day.getMonth();
if (!autohide) {
this.table.style.position = 'relative';
this.populate(year, month, day.getDate());
}
}
Calendar.prototype.moveTo = function(x, y) {
if (document.all) {
this.table.style.pixelLeft = x;
this.table.style.pixelTop = y;
}
else {
this.table.style.left = x;
this.table.style.top = y;
}
}
Calendar.prototype.moveBy = function(x, y) {
if (document.all) {
this.table.style.pixelLeft += x;
this.table.style.pixelTop += y;
}
else {
x += parseInt(this.table.style.left+0);
y += parseInt(this.table.style.top+0);
this.table.style.left = x;
this.table.style.top = y;
}
}
Calendar.prototype.getObject = function() {
return this.table;
}
Calendar.prototype.nextMonth = function(delta) {
var year, month = this.month + delta;
if (month > 11) {
month -= 12;
year = this.year + 1;
}
else if (month < 0) {
month += 12;
year = this.year - 1;
}
else year = this.year;
var day = getDays(year, month);
if (this.day < day) day = this.day;
this.populate(year, month, day);
}
Calendar.prototype.populate = function(year, month, day) {
if (document.layers) return;
this.table.style.display = "block";
// this.table.style.zIndex = "1"; // positive values move it up, negative down, 0 is default
this.year = year = parseInt(year);
this.month = month = parseInt(month); // user typed in month, feb = 2
this.day = day = parseInt(day);
this.rows[0].childNodes[2].innerHTML = getTitle(year, month);
first = new Date(year, month, 1);
count = getDays(year, month);
if (i = week = first.getDay()) {
if (month == 0) // user typed in bad month
last = 31;
else
last = getDays(year, month - 1); // convert month to array idx, feb = 1
while (i--) {
this.rows[2].childNodes[i].className = "notday btnOut"; // controls previous month days
this.rows[2].childNodes[i].innerHTML = last--;
}
}
last = 1; i = 2;
cell = this.rows[2].childNodes[week];
while (cell) {
if (day == last) {
day = 0;
//			cell.className = "today btnOut";
cell.className = "notday btnOut";
}
else if (i>5&&last<21)
cell.className = "notday btnOut";
else if (week == 0)
cell.className = "sunday btnOut";
else if (week == 6)
cell.className = "satday btnOut";
else
cell.className = "theday btnOut";
cell.innerHTML = last++;
if (week == 6) week = 0;
else week++;
if (last > count) last = 1;
cell = cell.nextSibling;
if (!cell && ++i < this.rows.length)
cell = this.rows[i].firstChild;
}
if (this.table.focus) this.table.focus();
}
Calendar.prototype.dateClicked = function(cell) {
if (!this.callback) return;
if (!cell.tagName) cell = event.srcElement;
var year, month, day = cell.innerHTML;
year = this.year;
if (cell.className.indexOf('notday') < 0)
month = this.month;
else if (day > 15) {
if (month == 0) {
month = 11;
year--;
}
else month = this.month - 1;
}
else {
if (month == 11) {
month = 0;
year++;
}
else month = this.month + 1;
}
if (typeof(this.callback)=='function') {
var show = this.callback(year, month, day);
if (!show) this.table.style.display = "none";
}
else if (typeof(this.callback)!='object') ;
else if (typeof(this.callback.value)=='string')
this.callback.value = year + "-" + (month+1) + "-" + day;
else
this.callback.innerHTML = year + "-" + (month+1) + "-" + day;
}
function hide_Calendar(e){
if (!cal||cal.table.style.display!='block')return;
var node = e.target;
while (node) {
if (cal.table==node) return true;
node = node.parentNode;
}
cal.table.style.display='none';
}
function make_Calendar(autohide){
if (autohide) {
document.write("<TABLE CELLPADDING=0 ID=cal1 CLASS=calendar ONBLUR=\"this.style.display='none'\">");
if (!document.all) document.onmousedown = hide_Calendar;
} else document.write("<TABLE CELLPADDING=0 ID=cal2 CLASS=calendar>");
document.write("<TR><TH>&#171;</TH><TH>&#8249;</TH><TH COLSPAN=3></TH><TH>&#8250;</TH><TH>&#187;</TH></TR>");
document.write("<TR><TH></TH><TH></TH><TH></TH><TH></TH><TH></TH><TH></TH><TH></TH></TR>");
for (i=0; i<6; i++)
document.write("<TR><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD><TD></TD></TR>");
document.write("</TABLE>");
if (autohide) cal = new Calendar('cal1', getdate, autohide);
else new Calendar('cal2', gotodate, autohide);
}
var theobj = null;
day = new Date();
function autodate(form_name, name, xy_pos)
{
/*
You can specify xy_pos as "top" or "right"
This means the calendar will display on top of the input text field or to the right.
If the variable is not inputted, or is given a different value,
it will default display below the input text field.
The purpose of this is to avoid 'select' fields, which are displayed
on a top HTML layer so look funny when the calendar is below them.
*/
theobj = document[form_name].elements[name];
if (!theobj) theobj = document.forms[1].elements[name];
//var tmp_d = theobj.value;
//alert("ct_calendar.js --- Date="+tmp_d);
var theday = theobj.value.split('-'); // e.g. 12-6-2006 => array
var rect = getPosition(theobj);
if (xy_pos) {
if (xy_pos == "top") {
cal.moveTo(rect.x, rect.y - 130);
} else if (xy_pos == "right") {
cal.moveTo(rect.x + 105, rect.y);
} else {
cal.moveTo(rect.x, rect.y + 22);
}
} else {
cal.moveTo(rect.x, rect.y + 22);
}
if (theday.length == 3)
// cal.populate(theday[0], theday[1]-1, theday[2]) // CHINESE DATE
cal.populate(theday[2], theday[0]-1, theday[2]) // year, month, day
else
cal.populate(day.getFullYear(), day.getMonth(), day.getDate())
}
function gotodate(year,month,day)
{
month++;
// day = "&date=" + year + "-" + month + "-" + day; // CHINESE DATE
day = "&date=" + month + "-" + day + "-" + year; // ENGLISH DATE
if ((pos = location.href.indexOf("&date="))>0)
location.href = location.href.substring(0, pos) + day;
else location.href = location.href + day;
return true;
}
function gotodate2(edit)
{
if (!edit.value) return;
day = "&date=" + edit.value;
if ((pos = location.href.indexOf("&date="))>0)
location.href = location.href.substring(0, pos) + day;
else location.href = location.href + day;
return true;
}
function getdate(year,month,day)
{
if (!theobj) return;
month++;
// theobj.value = year + "-" + month + "-" + day; // CHINESE DATE
theobj.value = month + "-" + day + "-" + year; // ENGLISH DATE
return false;
}
function is_date(str)
{
return str.match(/^(\d{2,4})[-.\/](\d{1,2})[-.\/](\d{1,2})$/);
}
function is_num(str)
{
return str.match(/\d/);
}
function is_mail(str)
{
return str.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
}
function make_date(str)
{
var str = str.split('-');
return new Date(str[0],str[1]-1,str[2],23,59,59);
}

