dragging = false;

function dateNode(name) {
    this.name = name;
    this.obj = function() {
        return document.getElementById(this.name);
    }
    this.updateInput = "",
    this.date = new Date();
    this.date.setMonth(this.date.getMonth());
    this.day = 0;
    this.month = function() {
        return this.date.getMonth()
    };
    this.year = function() {
        return (this.date.getFullYear().toString()).substring(2,4)
    };
    this.months = new Array("January","February","March","April","May","June","July","August","Septemtber","October","November","December");
    this.sMonth = function() {
        return this.months[this.month()]
    };
    this.retDate = function() {
        cMonth = parseInt(this.month(),10) +1;
        cMonth = (cMonth < 10)?"0"+cMonth:cMonth;
        cDay = (this.day < 10)?"0"+this.day:this.day;
        return cDay+"/"+cMonth+"/"+this.year();
    }

    this.sD = new Date();
    this.eD = new Date();

    this.aM = function(dir) {
        dir=(dir=="prev")?-1:1;
        this.date.setMonth(this.date.getMonth() + dir);
        this.reset();
    }

    this.reset = function() {
        this.obj().getElementsByTagName("li")[1].innerHTML = this.sMonth()+" &nbsp; "+this.year();
        nDateMod = (this.date.getYear().toString().length == 4)?0:1900;
        this.sD.setMonth(this.date.getMonth());
        this.sD.setDate(this.date.getDate() - (this.date.getDate()-1));
        this.sD.setYear(this.date.getYear()+nDateMod);
	
        this.eD.setYear(this.sD.getYear()+nDateMod);
        this.eD.setMonth(this.sD.getMonth() + 1);
        this.eD.setDate(this.sD.getDate());
        this.eD.setDate(this.eD.getDate() - 1);

        this.rebuild();
    }
    this.rebuild = function() {
        obj = document.getElementById("calDays");
        if (obj.childNodes.length > 0)
            clearList(obj);
        var cDay = 0;
        for(i=0; i<this.sD.getDay(); i++) {
            obj.appendChild(createLi("","mt"));
            cDay++;
        }
        for (i=this.sD.getDate(); i<=this.eD.getDate(); i++) {
            sC=((cDay+i)%7==0||(cDay+i-1)%7==0)?"we":"wd";
            obj.appendChild(createLi(i,sC));
        }
        for (i=this.eD.getDate()+cDay; i<Math.ceil((this.eD.getDate()+cDay)/7)*7; i++)
            obj.appendChild(createLi("","mt"));
    }
}

function clearList(oList) {
    if (typeof oList == "string")
        oList = document.getElementById(oList);
    while (oList.childNodes.length > 0)
        oList.removeChild(oList.childNodes[0]);
}

function createLi(sContent,sClass) {
    var newLi = document.createElement("li");
    var textNode = document.createTextNode("");
    newLi.appendChild(textNode);
    newLi.innerHTML = sContent;
    newLi.className = sClass;
    newLi.onmouseover = function () {
        if (this.className.indexOf("_ov") == -1)
            this.className = this.className+"_ov";
    };
		
    newLi.onmouseout = function () {
        if (this.className.indexOf("_ov") != -1)
            this.className = this.className.replace("_ov","");
    };
    newLi.onclick = function () {
        if (this.innerHTML != "") {
            oDate.day = this.innerHTML;
            updateInput();
        }
    };
    return newLi;
}

function toggleOver(obj) {
    if (obj.className.indexOf("_ov") == -1)
        obj.className = obj.className+"_ov";
    else
        obj.className = obj.className.replace("_ov","");
}

oDate = new dateNode("calendar");

function drag(mX,mY) {
    if (dragging) {
        oDate.obj().style.left = mX-50;
        oDate.obj().style.top = mY-10;
    }
}


function buildDate() {
    sDate = '<div id="calendar">'
    +	'<ul>'
    +		'<li class="arrows" onmouseover="toggleOver(this)" onmouseout="toggleOver(this)" onclick="oDate.aM(\'prev\')">&lt;</li>'
    +		'<li class="month" onmouseover="toggleOver(this)" onmouseout="toggleOver(this)" onmousedown="dragging=true" onmouseup="dragging=false">'+oDate.sMonth()+'</li>'
    +		'<li class="arrows" onmouseover="toggleOver(this)" onmouseout="toggleOver(this)" onclick="oDate.aM(\'next\')">&gt;</li>'
    +		'<li class="day">s</li>'
    +		'<li class="day">m</li>'
    +		'<li class="day">t</li>'
    +		'<li class="day">w</li>'
    +		'<li class="day">t</li>'
    +		'<li class="day">f</li>'
    +		'<li class="day">s</li>'
    +	'</ul>'
    +	'<ul id="calDays">'
    +		'<li></li>'
    +		'<li>r</li>'
    +		'<li>e</li>'
    +		'<li>s</li>'
    +		'<li>e</li>'
    +		'<li>t</li>'
    +		'<li></li>'
    +	'</ul>'
    +'</div>';
    document.write(sDate);
}

function displayDate(e,fInput) {
    e=(e==null)?event:e;
    oDate.obj().style.left = e.x;
    oDate.obj().style.top = e.y;
    oDate.reset();
    oDate.obj().style.display = "block";
    oDate.updateField = fInput;
}
		
function updateInput() {
    document.form1[oDate.updateField].value = oDate.retDate();
    oDate.obj().style.display = "none";
}

