var timeoutDelay=2000; 
var g_startDay=0;
var imgUp=new Image(8,12);
imgUp.src='images/up.gif';
var imgDown=new Image(8,12);
imgDown.src='images/down.gif';
var timeoutId=false;

function Browser(){
var d=document;
this.dom=d.getElementById?1:0;
this.ie4=(d.all&&!this.dom)?1:0;
this.ns4=(d.layers&&!this.dom)?1:0;
this.ns6=(this.dom&&!d.all)?1:0;
this.ie5=(this.dom&&d.all)?1:0;
this.ok=this.dom||this.ie4||this.ns4;
this.platform=navigator.platform;
};
var b=new Browser();

if(b.dom||b.ie4){
w('<style>');
w('#container{');
w('position : absolute;');
w('left : 100px;');
w('top : 100px;');
w('width : 124px;');
b.platform=='Win32'?height=140:height=145;
w('height : '+height+'px;');
w('clip:rect(0px 124px '+height+'px 0px);');

w('visibility : hidden;');
w('background-color : #ffffff');
w('}');
w('</style>');
document.write('<div id="container"');
if(timeoutDelay)document.write(' onmouseout="calendarTimeout();" onmouseover="if(timeoutId)clearTimeout(timeoutId);"');
document.write('></div>');
};

var g_C;

function calendarTimeout(){
if(b.ie4||b.ie5){
if(window.event.srcElement&&window.event.srcElement.name!='month')timeoutId=setTimeout('g_C.hide();',timeoutDelay);
};
if(b.ns6||b.ns4){
timeoutId=setTimeout('g_C.hide();',timeoutDelay);
};
};

function Calendar(){
g_C=this;

this.daysOfWeek=new Array("Su","Mo","Tu","We","Th","Fr","Sa");
this.months=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
this.daysInMonth=new Array(31,28,31,30,31,30,31,31,30,31,30,31);

if(b.ns4){
var tL=new Layer(127);
if(timeoutDelay){
tL.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
tL.onmouseover=function(event){if(timeoutId)clearTimeout(timeoutId);};
tL.onmouseout=function(event){timeoutId=setTimeout('g_C.hide()',timeoutDelay);};
}
tL.x=100;
tL.y=100;
tL.bgColor="#ffffff";
}
if(b.dom||b.ie4){
var tL=b.dom?document.getElementById('container'):document.all.container;
}
this.cL=tL;
if(b.ns4&&b.platform=='Win32'){
this.cL.clip.height=134;
this.cL.clip.width=127;
}
}

 Calendar.prototype.getFirstDOM=function(){
var thedate=new Date();
thedate.setDate(1);
thedate.setMonth(this.month);
thedate.setFullYear(this.year);
return thedate.getDay();
}

Calendar.prototype.getDaysInMonth=function(){
if(this.month!=1){
return this.daysInMonth[this.month];
}
else{
if(Date.isLeapYear(this.year)){
return 29;
}
else{
return 28;
}
}
}
 
Calendar.prototype.bS=function(){
var tS='<form onSubmit="this.year.blur();return false;"><table width="100%" border="0" cellspacing="0" cellpadding="2" class="calBorderColor"><tr><td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
tS+='<tr>';
tS+='<td width="60%" class="cal" align="left">';
if(this.hasDropDown){
tS+='<select class="month" name="month" onchange="g_C.selectChange();">';
for(var i=0;i<this.months.length;i++){
tS+='<option value="'+i+'"' 
if(i==this.month)tS+=' selected';
tS+='>'+this.months[i]+'</option>';
}
tS+='</select>';
}else{
tS+='<table border="0" cellspacing="0" cellpadding="0"><tr><td><a href="javascript: g_C.changeMonth(-1);"><img name="calendar" src="images/down.gif" width="8" height="12" border="0" alt=""></a></td><td class="cal" width="100%" align="center">'+this.months[this.month]+'</td><td class="cal"><a href="javascript: g_C.changeMonth(+1);"><img name="calendar" src="images/up.gif" width="8" height="12" border="0" alt=""></a></td></tr></table>';
}
tS+='</td>';

tS+='<td width="40%" align="right" class="cal">';

if(this.hasDropDown){
tS+='<input class="year" type="text" size="';

(b.ns4&&b.platform=='Win32')?tS+=1:tS+=4;
tS+='" name="year" maxlength="4" onBlur="g_C.inputChange();" value="'+this.year+'">';
}else{
tS+='<table border="0" cellspacing="0" cellpadding="0"><tr><td class="cal"><a href="javascript: g_C.changeYear(-1);"><img name="calendar" src="images/down.gif" width="8" height="12" border="0" alt=""></a></td><td class="cal" width="100%" align="center">'+this.year+'</td><td class="cal"><a href="javascript: g_C.changeYear(+1);"><img name="calendar" src="images/up.gif" width="8" height="12" border="0" alt=""></a></td></tr></table>'
}
tS+='</td>';
tS+='</tr>';
tS+='</table>';
var iCount=1;

var iFirstDOM=(7+this.getFirstDOM()-g_startDay)%7; 

var iDaysInMonth=this.getDaysInMonth(); 

tS+='<table width="100%" border="0" cellspacing="0" cellpadding="1" class="calBgColor">';
tS+='<tr>';
for(var i=0;i<7;i++){
tS+='<td align="center" class="calDaysColor">'+this.daysOfWeek[(g_startDay+i)%7]+'</td>';
}
tS+='</tr>';
var tmpFrom=ps(''+this.dateFromYear+this.dateFromMonth+this.dateFromDay);
var tmpTo=ps(''+this.dateToYear+this.dateToMonth+this.dateToDay);
var tmpCompare;
for(var j=1;j<=6;j++){
tS+='<tr>';
for(var i=1;i<=7;i++){
tS+='<td width="16" align="center" '
if((7*(j-1)+i)>=iFirstDOM+1&& iCount <=iDaysInMonth){
if(iCount==this.day&&this.year==this.oYear&&this.month==this.oMonth)tS+='class="calHighlightColor"';
else{
if(i==7-g_startDay||i==((7-g_startDay)%7)+1)tS+='class="calWeekend"';
else tS+='class="cal"';
}
tS+='>';

tmpCompare=ps(''+this.year+pZ(this.month)+pZ(iCount));
if(tmpCompare >=tmpFrom&&tmpCompare <=tmpTo){
tS+='<a class="cal" href="javascript: g_C.clickDay('+iCount+');">'+iCount+'</a>';
}else{
tS+='<span class="disabled">'+iCount+'</span>';
}
 iCount++;
}else{
if(i==7-g_startDay||i==((7-g_startDay)%7)+1)tS+='class="calWeekend"'; else tS+='class="cal"';
 tS+='>&nbsp;';
}
tS+='</td>'
}
 tS+='</tr>'
}
tS+='</table></td></tr></table></form>'
return tS;
}

Calendar.prototype.selectChange=function(){
this.month=b.ns6?this.cL.ownerDocument.forms[0].month.selectedIndex:this.cL.document.forms[0].month.selectedIndex;
this.wS(this.bS());
}

Calendar.prototype.inputChange=function(){
var tmp=b.ns6?this.cL.ownerDocument.forms[0].year:this.cL.document.forms[0].year;
if(tmp.value >=1900||tmp.value <=2100){
this.year=tmp.value;
this.wS(this.bS());
}else{
tmp.value=this.year;
}
}
Calendar.prototype.changeYear=function(incr){
(incr==1)?this.year++:this.year--;
this.wS(this.bS());
}
Calendar.prototype.changeMonth=function(incr){
if(this.month==11&&incr==1){
this.month=0;
this.year++;
}else{
if(this.month==0&&incr==-1){
this.month=11;
this.year--;
}else{
(incr==1)?this.month++:this.month--;
}
}
this.wS(this.bS());
}

Calendar.prototype.clickDay=function(day){
var tmp=eval('document.'+this.target);
tmp.value=this.fDaS(day,this.month,this.year);
if(b.ns4)this.cL.hidden=true;
if(b.dom||b.ie4){
this.cL.style.visibility='hidden';
}
}
Calendar.prototype.fDaS=function(day,month,year){
return pZ(day)+this.dD+pZ(month+1)+this.dD+year;
}
Calendar.prototype.wS=function(str){
if(b.ns4){
this.cL.document.open();
this.cL.document.write(str);
this.cL.document.close();
}
if(b.dom||b.ie4){
this.cL.innerHTML=str;
}
}

Calendar.prototype.show=function(event,target,bHasDropDown,dateFormat,dateFrom,dateTo){

this.dateFrom=dateFrom||new Date(1900,0,1);
this.dateFromDay=pZ(this.dateFrom.getDate());
this.dateFromMonth=pZ(this.dateFrom.getMonth());
this.dateFromYear=this.dateFrom.getFullYear();
this.dateTo=dateTo||new Date(3000,0,1);
this.dateToDay=pZ(this.dateTo.getDate());
this.dateToMonth=pZ(this.dateTo.getMonth());
this.dateToYear=this.dateTo.getFullYear();
this.hasDropDown=bHasDropDown;
this.dateFormat=dateFormat||'dd-mmm-yyyy';
this.dD='/';

if(b.ns4){
if(!this.cL.hidden){
this.cL.hidden=true;
return;
}
}
if(b.dom||b.ie4){
if(this.cL.style.visibility=='visible'){
this.cL.style.visibility='hidden';
return;
}
}

if(b.ie5||b.ie4){
var event=window.event;
}
if(b.ns4){
this.cL.x=event.x+10;
this.cL.y=event.y-5;
}
if(b.ie5||b.ie4){
var obj=event.srcElement;
x=0;
while(obj.offsetParent !=null){
x+=obj.offsetLeft;
obj=obj.offsetParent;
}
x+=obj.offsetLeft;
y=0;
var obj=event.srcElement;
while(obj.offsetParent !=null){
y+=obj.offsetTop;
obj=obj.offsetParent;
}
y+=obj.offsetTop;

this.cL.style.left=x+35;
if(event.y>0)this.cL.style.top=y;
}
if(b.ns6){
this.cL.style.left=event.pageX+10;
this.cL.style.top=event.pageY-5;
}
this.target=target;
var tmp=eval('document.'+this.target);
if(tmp&&tmp.value&&tmp.value.split(this.dD).length==3&&tmp.value.indexOf('d')==-1){
var atmp=tmp.value.split(this.dD);
this.month=this.oMonth=ps(atmp[1]-1); 
this.day=ps(atmp[0]);
this.year=this.oYear=ps(atmp[2]);
}else{
var theDate=new Date();
this.year=this.oYear=theDate.getFullYear();
this.month=this.oMonth=theDate.getMonth();
this.day=this.oDay=theDate.getDate();
}
this.wS(this.bS());

if(b.ns4){
this.cL.hidden=false;
}
if(b.dom||b.ie4){
this.cL.style.visibility='visible';
}
}

Calendar.prototype.hide=function(){
if(b.ns4)this.cL.hidden=true;
if(b.dom||b.ie4){
this.cL.style.visibility='hidden';
}
}

function handleDocumentClick(e){
var s=g_C.cL.style;
if(b.ie4||b.ie5)e=window.event;

if(b.ns6){
var n=e.target.name;
var bTest=(e.pageX > ps(s.left)&&e.pageX <(ps(s.left)+125)&&e.pageY <(ps(s.top)+125)&&e.pageY > ps(s.top));
if(n!='imgCalendar'&&n!='month'&& n!='year'&&n!='calendar'&&!bTest){
g_C.hide(); 
}
}
if(b.ie4||b.ie5){
var n=e.srcElement.name;
var bTest=(e.x > ps(s.left)&&e.x <(ps(s.left)+125)&&e.y <(ps(s.top)+125)&&e.y > ps(s.top));
if(n!='imgCalendar'&&n!='month'&&n!='year'&&!bTest & typeof(e.srcElement)!='object'){
g_C.hide(); 
}
}
if(b.ns4)g_C.hide();
}

function pZ(num){
return((num <=9)?("0"+num): num);
}
Date.isLeapYear=function(year){if(year%4==0 &&((year%100!=0)||(year%400==0)))return true; else return false;}
Date.daysInYear=function(year){if(Date.isLeapYear(year))return 366; else return 365;}
var DAY=1000*60*60*24;
Date.prototype.addDays=function(num){
return new Date((num*DAY)+this.valueOf());
}

window.onload=function(){
new Calendar(new Date());
if(b.ns4){
if(typeof document.NSfix=='undefined'){
document.NSfix=new Object();
document.NSfix.initWidth=window.innerWidth;
document.NSfix.initHeight=window.innerHeight;
}
}
}
if(b.ns4)window.onresize=function(){
if(document.NSfix.initWidth!=window.innerWidth||document.NSfix.initHeight!=window.innerHeight)window.location.reload(false);
}
window.document.onclick=handleDocumentClick;
window.onerror=function(msg,url,line){
alert('Please wait for page to load completely.\nPress F5 to reload page if this error persists');
}

function w(s){document.writeln(s)};
function ps(i){return parseInt(i,10)};
