/*
usage the script automatically 
creates the cal object

to show calendar put cal.show(this) to 
onclick event on an input field

<input name="textfield" type="text" onlcick="cal.show(this)">

or 

<input name="textfield" type="text" onlcick="showCalendar(this)">

required calendar.css style sheet
*/

function Calendar() {
    this.YEAR;  // current year
    this.MONTH; // current month
    this.WEEKSTART = 0; // 0 - week start with Sunday
    this.OBJ;
    this.CLASSN;

    this.month; 
    this.year;
    
    this.eventobj = null;
    this.DIV;
    this.divover = false;
    this.postaction = null;
    
    THIS = this; // store calendar instance

    if (typeof(this.DIV) == "undefined"){
        this.DIV = document.createElement("div");
        document.body.appendChild(this.DIV);

        document.body.onclick = hideme;
        this.DIV.onmouseover  = overme;
            
    }
    
    this.DIV.style.position   = "absolute";

    var d = new Date();

    this.YEAR  = d.getFullYear();
    this.year  = this.YEAR;

    this.MONTH = d.getMonth();
    this.month = this.MONTH;
}

Calendar.prototype.verifydate = verifydate;
Calendar.prototype.display    = display;
Calendar.prototype.show       = show;

function goto_month(m){

    if (m == 0) {
        THIS.year  = THIS.YEAR;
        THIS.month = THIS.MONTH;
    }

    THIS.month+= m;
    THIS.display();
}

function show(eventobj){ // event obj

    this.eventobj = eventobj;

    if (arguments.length > 1) {
        this.month = arguments[1];
    }

    if (arguments.length > 2) {
        this.year  = arguments[2];
    }
    
    else{

        var d = new Date(eventobj.value);

        if (isNaN(d)){
            this.year  = this.YEAR;
            this.month = this.MONTH;
        }
        else{
            this.year  = d.getFullYear();
            this.month = d.getMonth();
        }

        if (this.year < 1920) this.year+= 100;
    }

    if (typeof(event) != "undefined") {
        var x = event.clientX - 80;
        var y = event.clientY + 10;
    }
    else {
        var x = 0;
        var y = 0;
    }

    this.DIV.style.pixelLeft  = x;
    this.DIV.style.pixelTop   = y;
    this.DIV.style.visibility = "visible";
    this.display();
}

function hideme() {
    if (THIS.divover){
        THIS.DIV.style.visibility = "hidden";
        THIS.divover = false;
    }
}

function overme(){
    THIS.divover = true;
}

function mover(obj){
   THIS.OBJ = obj;
   THIS.CLASSN = obj.className;
   obj.className="dayh";
}

function mout(obj){
   THIS.OBJ.className = THIS.CLASSN;
}

function display(){
    
    var d, day, days, week, premdays, lastoffs, cal ="";

    if (this.month > 11){
        this.year++;
        this.month = 0;
    }
    if (this.month < 0){
        this.year--;
        this.month = 11;
    }

    var dayone = new Date(this.year, this.month, 1);    
    var offset = dayone.getDay() - this.WEEKSTART;// * 86400000;
    var wdays  = new Array("S","M","T","W","T","F","S");
    var mnames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

    days     = monthdays(this.month, this.year);
    premdays = monthdays(this.month - 1, this.year);

    lastoffs = 7 - ((days + offset) % 7);
    if (lastoffs > 6) lastoffs = 0;

    day = days + offset + lastoffs;

    week = "";

    for (i = 0; i < day; i++) {

        d = i - offset;
        classn = "day";
        mm = this.month;
        yy = this.year;

        if (i < offset) {
            d = premdays - offset + i;
            classn = "days";
            mm = this.month - 1;
            if (mm < 0){
                mm = 11;
                yy = this.year - 1;
            }
        }
        if (d >= days && i > offset) {
            d = d - days;
            classn = "days";
            mm = this.month + 1;
            if (mm > 11){
                mm = 0;
                yy = this.year + 1;
            }
        }
        if (i % 7 == 0 && i != 0) {
            cal+= '<tr align="center">' + week + "</tr>\n";
            week = "";
        }
        week+= '<td class="'+classn+'" onmouseover="mover(this)" onmouseout="mout(this)" onclick="selectdate('+yy+','+mm+','+(d+1)+')">'+ (d + 1) +'</td>\n';
    }

    cal+= '<tr align="center">' + week + "</tr>\n";
    week = "";

    for (i = 0; i < 7; i++) {
        d = (i + this.WEEKSTART) % 7;
        week+= '<td class="wday">'+wdays[d]+'</td>\n';
    }
    week = '<tr align="center">'+week+'</tr>\n';
    yy = '<tr align="center"><td class="year" onclick="goto_month(-1)">&lt;</td>';
    yy+= '<td colspan="4" class="year" onclick="goto_month(0)">'+mnames[this.month]+' '+this.year+'</td>';
    yy+= '<td class="year" onclick="goto_month(1)">&gt;</td>';
    yy+= '<td class="close" onclick="hideme()">x</td></tr>';
    this.DIV.innerHTML='<table width="160" border="1" cellspacing="0" cellpadding="0" bordercolor="#FF9933" bgcolor="#EEEEEE"><tr><td><table width="100%" border="0" cellspacing="1" cellpadding="0">'+ yy + week + cal +'</table></td></tr></table>';

}


function monthdays(month, year){
    var days;
    var mdays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    if (month < 0) month = 11;
    if (month > 11) month = 0;

    days = mdays[month];
    if (month == 1) days = (year % 400) == 0 || (year % 4 == 0 && year % 100 != 0) ? 29 : 28;

    return(days);
}

function selectdate(y, m, d){
   THIS.eventobj.value = (m+1) + "/" + d + "/" + y;
   THIS.DIV.style.visibility = "hidden";

   if (THIS.postaction != null) {
       eval(THIS.postaction);
       THIS.postaction = null;
   }
}

function verifydate(eventobj){
    var d = new Date(eventobj.value);

    this.eventobj = eventobj;

    if (isNaN(d)){
        eventobj.value = '';
        hideme();
        return;
    }

    var ye = d.getFullYear();

    //if (ye < 100) ye+= 2000;
    if (ye < 1920) ye+= 100;

    var s = (d.getMonth() + 1) + "/";
        s += d.getDate() + "/";
        s += ye;

    eventobj.value = s;
    hideme();
}

cal = new Calendar();

function showCalendar(obj){
    if (typeof(cal) == "undefined") {
        cal = new Calendar();
    }

    cal.show(obj);
}


