
function getAbsoluteLeft(o) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	oLeft = o.offsetLeft;           // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent ;   // Get parent object reference
		oLeft += oParent.offsetLeft; // Add parent left position
		o = oParent;
	}
	// Return left postion
	return oLeft;
}
			
function getAbsoluteTop(o) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	oTop = o.offsetTop;            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent;  // Get parent object reference
		oTop += oParent.offsetTop; // Add parent top position
		o = oParent;
	}
	// Return top position
	return oTop;
}

var rgMN=new Array(12);
rgMN[0]="JAN";
rgMN[1]="FEB";
rgMN[2]="MAR";
rgMN[3]="APR";
rgMN[4]="MAY";
rgMN[5]="JUN";
rgMN[6]="JUL";
rgMN[7]="AUG";
rgMN[8]="SEP";
rgMN[9]="OCT";
rgMN[10]="NOV";
rgMN[11]="DEC";

var rgMC=new Array(12);
rgMC[0]=31;
rgMC[1]=28;
rgMC[2]=31;
rgMC[3]=30;
rgMC[4]=31;
rgMC[5]=30;
rgMC[6]=31;
rgMC[7]=31;
rgMC[8]=30;
rgMC[9]=31;
rgMC[10]=30;
rgMC[11]=31;


function cbCalendar(tag, img, objectName, startDateTag, endDateTag, inIframe, startDate, bolNoBackBound){
if(inIframe){
	this.inIframe = inIframe;	
}else{
this.inIframe = false;
}
if(startDateTag){
	this.startTag = startDateTag;
	//if(isNaN(Date.parse(startDateTag.value).getTime())){
	//	this.startDate = new Date();
	//}else{
	this.startDate = new Date(Date.parse(startDateTag.value));
	//}
}else{
	this.startTag = null;
	this.startDate = new Date();
}
    this.noBackBound = (arguments[7])?bolNoBackBound:false;

    
this.curDate = null;

if(startDate){
	
this.startDate = startDate;
this.curDate = new Date();
}

this.endTag = endDateTag;
this.returnFunction = 'selectDate';

/*
if(tag.value.length){
this.curDate = new Date(Date.parse(tag.value));	
}else{
this.curDate = null;//new Date();
}

if(isNaN(this.curDate.getTime())){
this.curDate = null;
}
*/

this.boundDate = null;//new Date(this.curDate.getTime());

this.today = new Date();

this.boundImg = img;
this.boundTag = tag;
this.objectName = objectName;

this.moveTimeout = null;

document.body.insertAdjacentHTML('beforeEnd', '<div ID="' + this.objectName + '.MainDiv" style="position:absolute;width:148;z-index=100;display:none;">testing</div>');
    
if(document.all)
    document.body.insertAdjacentHTML('beforeEnd', '<iframe style="display:none;position:absolute;" zindex=1 ID="' + this.objectName + '.MainiFrame"></iframe>');

this.dTag = document.getElementById(this.objectName + '.MainDiv');

if(document.all)
    this.Ifrm = document.getElementById(this.objectName + '.MainiFrame');

}

cbCalendar.prototype.selectDate = function(dte){
	this.curDate = dte;
	this.boundTag.value = (dte.getMonth() + 1) + '/' + dte.getDate() + '/' + dte.getUTCFullYear();
	if(this.endTag && (this.endTag.value.length == 0)){
		var nd = new Date(dte);
		nd.setDate(nd.getDate() + 7);
		this.endTag.value = (nd.getMonth() + 1) + '/' + nd.getDate() + '/' + nd.getUTCFullYear();
	}
	this.boundDate = new Date(this.curDate);
	this.dTag.style.display = 'none';
	if(this.Ifrm)
	this.Ifrm.style.display = 'none';

}

cbCalendar.prototype.stopGo = function(){
	if(this.moveTimeout){
	
		clearTimeout(this.moveTimeout);	
		this.moveTimeout = null;
	}
}

cbCalendar.prototype.DayBox = function(dte){

if( (dte && !this.startDate) || (dte && (this.startDate.getTime() <= dte.getTime()) ) || (dte && this.noBackBound)){
	
	var out = '<td align="center" class="' + ((dte.toDateString() == this.boundDate.toDateString())?'dts':'dt') + '" style="' + ((dte.toDateString() == new Date().toDateString())?'border:1px solid red;':'border:1 px solid black;') + '" onClick="' + this.objectName + '.' + this.returnFunction + '(new Date(\'' + dte.toUTCString() + '\'));">';
	out += dte.getDate();

}else{
var out = '<td align="center" class="dt" style="cursor:default;border:1 px solid black;color:#AAAAAA">';

if(dte)
out += dte.getDate();

}
	

	
	
out += '</td>';
return out;

}

cbCalendar.prototype.drawDays = function(arrOut){
	
		var dte = new Date(this.curDate);
		dte.setDate(1);
		var month = dte.getMonth();
		var daysToFill = dte.getDay();
		arrOut[arrOut.length] = '<tr>';
		
	    for(var i = 0; i < daysToFill;i++){
            arrOut[arrOut.length] = this.DayBox();
	    }
		
		while(dte.getMonth() == month){
			
			if(dte.getDay() == 0)
				arrOut[arrOut.length] = '<tr>';
			
			arrOut[arrOut.length] = this.DayBox(dte);
			
			if(dte.getDay() == 6)
				arrOut[arrOut.length] = '</tr>';
				
			dte.setDate(dte.getDate() + 1);
		}

	    dte.setDate(dte.getDate() - 1);
	
	    var daysToFill = 6 - dte.getDay();
	
	    for(var i = 0; i < daysToFill;i++){
		    arrOut[arrOut.length] = this.DayBox();
	    }
	
	
}

cbCalendar.prototype.goNext = function(){
	this.curDate.setMonth(this.curDate.getMonth() + 1);
	this.Display();
	//this.moveTimeout = setTimeout(this.objectName + '.goNext()', 300);
	
}

cbCalendar.prototype.goPrev = function(){
	this.curDate.setMonth(this.curDate.getMonth() - 1);
	setTimeout(this.objectName + '.Display()', 1);
	//this.moveTimeout = setTimeout(this.objectName + '.goPrev()', 300);
	
}

cbCalendar.prototype.Close = function(){

	this.dTag.style.display = 'none';
	if(this.Ifrm)
	this.Ifrm.style.display = 'none';

}

Date.prototype.toMY = function(){
	return (this.getYear()*100) + this.getMonth();	
}

cbCalendar.prototype.Display = function(){
	if(this.startTag){
			this.startDate = new Date(Date.parse(this.startTag.value));
			
		if((this.startDate.toString() == 'Invalid Date')|| isNaN(this.startDate)){
			this.startDate = new Date();
		}
	}else{
		if (this.startDate == null){
		this.startDate = new Date();
		}
	}
	
	
	if(this.boundTag.value.length){
		
		this.boundDate = new Date(Date.parse(this.boundTag.value));

		if(this.boundDate.toString() == 'Invalid Date'|| isNaN(this.boundDate))
			this.boundDate = new Date(this.startDate.getTime());
			
		//if(this.curDate.getTime() <  this.boundDate.getTime()){
		
		//}
	}else{
		
		this.boundDate = new Date(this.startDate.getTime());
		
	}
		
	if(this.curDate == null)
		this.curDate = new Date(this.boundDate.getTime());
		
	var out = [];
	out[out.length] = '	<BASEFONT FACE="Arial,Helvetica,Geneva,Swiss,Sans Serif">';
	//out[out.length] = '	<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=1 style="border-width:2px;border-color:black;border-style:solid;font:8pt arial;background:white;" onmouseover="Display(\'' + this.objectName + '.MainDiv\', \'' + this.objectName + '.MainiFrame\');" onmouseout="NoDisplay(\'' + this.objectName + '.MainDiv\', \'' + this.objectName + '.MainiFrame\');">';
	out[out.length] = '	<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=1 style="border-width:2px;border-color:black;border-style:solid;font:8pt arial;background:white;" onmouseover="Display(\'' + this.objectName + '.MainDiv\');" onmouseout="NoDisplay(\'' + this.objectName + '.MainDiv\');">';
	out[out.length] = '	<TR><TD HEIGHT=20>';
	out[out.length] = '	<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=1 HEIGHT=20 style="font: 8pt arial;"><TR>';
	
	
	if(this.startDate)
	{
	    if((this.curDate.toMY() > this.startDate.toMY()) || this.noBackBound)
		{
			out[out.length] = '	<TD WIDTH=16 ID=PrevDiv CLASS="hdrA"><IMG ID=Prev SRC="/vlinclude/popUpCalendar/images/prev.gif" onMouseUp="' + this.objectName + '.stopGo();" onMouseOut="' + this.objectName + '.stopGo();" onMouseDown="' + this.objectName + '.goPrev();" ALT="Show Previous Month"></TD>';
		}
		else
		{
			this.stopGo();
			out[out.length] = '	<TD WIDTH=16 ID=PrevDiv></TD>';
		}
	}
	else
	{
	    out[out.length] = '	<TD WIDTH=16 ID=PrevDiv CLASS="hdrA"><IMG ID=Prev SRC="/vlinclude/popUpCalendar/images/prev.gif" onMouseUp="' + this.objectName + '.stopGo();" onMouseOut="' + this.objectName + '.stopGo();" onMouseDown="' + this.objectName + '.goPrev();" ALT="Show Previous Month"></TD>';
	}
	out[out.length] = '	<TD WIDTH=101 ALIGN="center" CLASS="hdr"><SPAN ID=' + this.objectName + '.MonthTitle>' + rgMN[this.curDate.getMonth()] + '</SPAN>&nbsp;&nbsp;<SPAN ID=' + this.objectName + '.YearTitle>' + this.curDate.getUTCFullYear() + '</SPAN></TD>';
	out[out.length] = '	<TD WIDTH=16 ID=NextDiv CLASS="hdrA"><IMG ID=Next SRC="/vlinclude/popUpCalendar/images/next.gif" onMouseUp="' + this.objectName + '.stopGo();" onMouseOut="' + this.objectName + '.stopGo();" onMouseDown="' + this.objectName + '.goNext();" ALT="Show Next Month"></TD>';
	out[out.length] = '	</TR></TABLE>';
	out[out.length] = '	</TD></TR>';
	out[out.length] = '	<TR><TD>';
	out[out.length] = '	<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>';
	out[out.length] = '	<TR><TD><IMG ID=WeekImg SRC="/vlinclude/popUpCalendar/images/week0.gif"></TD></TR>';
	out[out.length] = '	<TR><TD HEIGHT=1 BGCOLOR=#000000></TD></TR>';
	out[out.length] = '	<TR><TD ALIGN="center" STYLE="position:relative;">';
	
	out[out.length] = '	<table width=100% border=0 cellpadding=0 cellspacing=1>';
	
	this.drawDays(out);
	
	out[out.length] = '	</table>';

	out[out.length] = '	</TD></TR>';
	out[out.length] = '	<TR><TD HEIGHT=1 BGCOLOR=#000000></TD></TR>';
	out[out.length] = '	</TABLE>';
	out[out.length] = '	</TD></TR>';
	out[out.length] = '	<TR><TD HEIGHT=20 ALIGN="center"><A STYLE="color:gray" HREF="javascript:void(' + this.objectName + '.Close())">Cancel</A></TD></TR>';
	out[out.length] = '	</TABLE>';
		
	if(this.dTag)
	{
		this.dTag.innerHTML = out.join('');
		this.dTag.style.zIndex = 101;
		
		if(this.inIframe)
		{
		    this.dTag.style.top = (this.boundImg.offsetTop + this.boundImg.offsetHeight)+ 'px';
		    this.dTag.style.left = this.boundImg.offsetLeft + 'px';
        }
        else
        {
	        this.dTag.style.top = (getAbsoluteTop(this.boundImg) + this.boundImg.offsetHeight)+ 'px';
	        this.dTag.style.left = getAbsoluteLeft(this.boundImg) + 'px';
        }
        
	    this.dTag.style.display = 'block';
    	
	    if(this.Ifrm && false)
	    {
		    this.Ifrm.style.zIndex = 100;
		    this.Ifrm.style.top = this.dTag.style.top;
		    this.Ifrm.style.left = this.dTag.style.left;
		    this.Ifrm.style.width = this.dTag.style.width;
		    this.Ifrm.style.height = this.dTag.offsetHeight + 'px';
		    this.Ifrm.style.display = 'block';
	    }
	}
	else
	{
		alert('dTag missing!');
	}
	return true;
}

//does the clean up, removes the div (and iFrame if used)
//from the page and deletes the object instance.
cbCalendar.prototype.Dispose = function()
{
    var tmpNode = GetTag(this.objectName+'.MainDiv');
    tmpNode.parentNode.removeChild(tmpNode);
    
    tmpNode = GetTag(this.objectName+'.MainiFrame');
    
    if(tmpNode != null)
        tmpNode.parentNode.removeChild(tmpNode);

    eval('delete ' + this.objectName);
}

document.write('<style><!-- .hdr  {background:gray;color:#FFFFFF;border-width:1px;border-color:black;border-style:solid} .hdrA {background:gray;color:#FFFFFF;border-width:1px;border-color:black;border-style:solid;cursor:hand;cursor:pointer;} .ndt  {position:absolute;width:19;height:19;} .bdt  {position:absolute;width:19;height:19;} .dt   {align:center;width:19;height:19;cursor:hand;cursor:pointer;} .dts  {background:gray;color:#FFFFFF;align:center;width:19;height:19;cursor:hand;cursor:pointer;} .sdt  {position:absolute;width:19;height:19;}  //--> </STYLE>');
