
CalendarCounter = 0;

function Calendar( Layer , Year , Month , Day, formObject, setInitValue ){
	this.CalendarLayer = Layer;
	this.monthNames = new Array('Januari', 'Februari', 'Mars', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	this.weekNames = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
	this.Date = new Date( Year , Month , Day );
	this.CheckDate = new Date( Year , Month , Day );
	this.Day = this.Date.getDay();
	this.Month =  this.Date.getMonth();
	
	this.formObject = formObject;
	this.Year =  this.Date.getFullYear();
	this.monthDays = new Array(31, this.Year % 4 == 0 ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	this.Render = Render;
	this.nextMonth = nextMonth;
	this.nextYear = nextYear;
	this.previousMonth = previousMonth;
	this.previousYear = previousYear;
	this.pick = pick;
	this.hideDays = hideDays;
	this.close	= close;
	this.font = "verdana";
	this.fontSize = "10px";
	this.weekDayBackground = "#CECECE";
	this.weekDayFontColor = "#000000";
	this.BackgroundColor = "#EFEFEF";
	this.headerLinkColor = "#993333";
	this.border = "#000000";
	this.isHidden = false;
	CalendarCounter++;
	this.refObj = "Calendar" + CalendarCounter + "ref";
	eval( this.refObj + " = this;" );
	this.isAfterCurrentDate = isAfterCurrentDate;
	this.isBeforeStartDate = isBeforeStartDate;
	this.setSelectedDate = setSelectedDate;
	
	
	// Added 2006-08-03
	this.setInitValue = setInitValue;
	if( setInitValue ){
			
		this.selectedDay = Day;
		this.setSelectedDate( true );
	}else{
		this.selectedDay = 0;
	}
}

function close(){
	this.CalendarLayer.hide();
}

function setSelectedDate( setValue ){
	this.selectedDate = this.Year + "-";
	
	if( new String( this.Month + 1 ).length == 1 ){
		this.selectedDate += "0" + ( this.Month + 1 );
	}else{
		this.selectedDate += this.Month + 1;
	}
	
	if( new String( this.selectedDay ).length == 1 ){
		this.selectedDate += "-0" + this.selectedDay;
	}else{
		this.selectedDate += "-" + this.selectedDay;
	}
	
	if( setValue ){
	
		this.formObject.value = this.selectedDate;
	}
	
	if( ! setValue ){
		
		if( this.formObject.value.length ){
			this.formObject.value = this.selectedDate;
		}
		
	}
	
}

function isBeforeStartDate(){
	
	if( this.Year < this.CheckDate.getFullYear()  ){
		return true;
	}else{
		if( this.Month < this.CheckDate.getMonth()   && (  this.Year <= this.CheckDate.getFullYear() ) ){
			return true;
		}else{
			if( ( this.selectedDay  < this.CheckDate.getDate()  ) && ( this.Month <= this.CheckDate.getMonth() ) && (  this.Year <= this.CheckDate.getFullYear() ) ){
				return true;
			}
		}
	}
	return false;
	

}

function isAfterCurrentDate(){
	
	d = new Date();

	if( d.getFullYear() < this.Year ){
		return true;
	}else{
		if( d.getMonth() < this.Month ){
			return true;
		}else{
		
			if( ( d.getDate() < this.selectedDay ) && ( d.getMonth() == this.Month )){
				return true;
			}
		}
	}
	return false;
	

}

function Render(){
	
	var Code = "";
	var counter = 0;
	ccounter = 1;
	var dday = new Date( this.Year , this.Month , 1 ).getDay() + 1;

	Code +="<table cellpadding=\"0\" cellspacing=\"1\" border=\"0\" bgcolor=\""+ this.border +"\" width=\"280\"><tr><td>";
	Code += "<table cellpadding=\"2\" cellspacing=\"0\" border=\"0\" bgcolor=\""+ this.BackgroundColor +"\" width=\"100%\">";
	
	
	
	Code += "<tr><td colspan=\"7\"><table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" ><tr><td width=\"80\"><span style=\"font-family:"+ this.font +";font-size:"+ this.fontSize +";color:"+ this.headerLinkColor +"\">&laquo; <a href=\"javascript:" + this.refObj + ".previousYear();\" style=\"color:"+ this.headerLinkColor +";\">Year</a> / <a href=\"javascript:" + this.refObj + ".previousMonth();\" style=\"color:"+ this.headerLinkColor +";\">Month</a></span></td><td width=\"130\" align=\"center\"><span style=\"font-family:"+ this.font  +";font-size:"+ this.fontSize +";color:#000000\">"  + this.monthNames[ this.Month ] + " " + this.Year + " </span></td><td width=\"80\"><span style=\"font-family:"+ this.font +";font-size:"+ this.fontSize +";color:"+ this.headerLinkColor +"\"><a href=\"javascript:" + this.refObj + ".nextMonth();\" style=\"color:"+ this.headerLinkColor +";\">Month</a> / <a href=\"javascript:" + this.refObj + ".nextYear();\" style=\"color:"+ this.headerLinkColor +";\">Year</a> &raquo;</span></td></tr></table>";
	
	if( ! this.isHidden ){
	
		Code += "<tr bgcolor=\""+ this.weekDayBackground +"\">";
		for( i = 0; i < this.weekNames.length ; i++ ){
			Code +=  "<td width=\"20\"><span style=\"font-family:"+ this.font  +";font-size:"+ this.fontSize +";color:"+ this.weekDayFontColor +";font-weight:bold;\">"+ this.weekNames[ i ] +"</span></td>";
		}
		Code += "</tr>";
	
	
	// Generate Days
		for( i = 1 ;  ; i++ ){
			
			if( counter == 0 ){
				Code += "<tr>";
			}
			
			if( i >= dday ){
				if( ccounter == this.selectedDay ){
					Code += "<td><span style=\"font-family:"+ this.font  +";font-size:" + this.fontSize + ";color:#000000;font-weight:bold;\">" + ccounter + "</span></td>";	
				}else{
					Code += "<td><a href=\"javascript:void( null );\" onclick=\"" + this.refObj + ".pick( "+ ccounter +" );\" style=\"font-family:"+ this.font  +";font-size:"+ this.fontSize +";color:#000000;\">" + ccounter + "</a></td>";	
				}
				
				ccounter++;
			}else{
				Code += "<td>&nbsp;</td>";
			}
			
	
			counter ++;
			
			
			if( counter == 7 ){
				Code += "<tr>";
				counter = 0;
			}
			
			if( ccounter > this.monthDays[ this.Month ] ){
				Code += "<td colspan=\"+ 7 - counter  +\">&nbsp;</td></tr>"
				break;
			}
		}
		
		Code += "<tr bgcolor=\""+ this.BackgroundColor +"\">";
		Code +=  "<td colspan=\"7\"><span style=\"font-family:"+ this.font  +";font-size:"+ this.fontSize +";color:#993333;font-weight:normal;\"><a href=\"javascript:" + this.refObj + ".close();\" style=\"color:#993333;cursor:hand;\">Close</a></span></td>";
		Code += "</tr>";
		
	}
	

	
	Code += "</td></tr></table>";
	Code +="</td></tr></table>";
	
	this.CalendarLayer.write( Code );
	
}

function pick( pickedDate ){
	this.selectedDay = pickedDate;
	this.setSelectedDate( true );
	this.Render();
	this.CalendarLayer.hide();
}

function nextMonth(){
	this.Month++;
	if(  ( this.Month ) > 11 ){
		this.Month = 0;
		this.Year++;
	}
	this.Date.setYear = this.Year;
	this.Date.setMonth = this.Month;
	this.monthDays[ 1 ]  = this.Year % 4 == 0 ? 29 : 28;
	this.selectedDay = 0;
	this.selectedDate = null;
	this.Render();
}

function previousMonth(){
	this.Month--;
	if(  ( this.Month ) < 0 ){
		this.Month = 11;
		this.Year--;
	}
	this.Date.setYear = this.Year;
	this.Date.setMonth = this.Month;
	this.monthDays[ 1 ]  = this.Year % 4 == 0 ? 29 : 28;
	this.selectedDay = 0;
	this.selectedDate = null;
	this.Render();
}


function previousYear(){
	this.Year--;
	this.Date.setYear = this.Year;
	this.monthDays[ 1 ]  = this.Year % 4 == 0 ? 29 : 28;
	this.selectedDay = 0;
	this.selectedDate = null;
	this.Render();
}

function nextYear(){
	this.Year++;
	this.Date.setYear = this.Year;
	this.monthDays[ 1 ]  = this.Year % 4 == 0 ? 29 : 28;
	this.selectedDay = 0;
	this.selectedDate = null;
	this.Render();
}

function hideDays(){
	this.isHidden = !this.isHidden 
	this.Render();
}

