 var Months = new Array();
 Months[0]  = 'January';  
 Months[1]  = 'February'; 
 Months[2]  = 'March';    
 Months[3]  = 'April';    
 Months[4]  = 'May';      
 Months[5]  = 'June';     
 Months[6]  = 'July';     
 Months[7]  = 'August';   
 Months[8]  = 'September';
 Months[9]  = 'October';  
 Months[10] = 'November'; 
 Months[11] = 'December'; 
 
 var calField;
 var calFormat;

function calOn(thisId, Field, Format) {
 calField=Field;
 calFormat = Format;
 Cal = document.getElementById('calendar');   
 Top  = getTop(thisId);
 Left = getLeft(thisId);
 Position = Top + Cal.offsetHeight - document.body.scrollTop;
 Client = document.body.clientHeight;
 if(Position > Client)
  document.body.scrollTop += Position-Client;
 Cal.style.top = Top;
 Cal.style.left = Left + thisId.offsetWidth;
 drawWindowOn(Cal);
} 

function drawCal(Year, Month) {
 nowDate = new Date();
 if(!Year)
  Year =  nowDate.getFullYear();
 if(Month == null)
  Month = nowDate.getMonth();
 permMonth= Month;
 calTable = document.getElementById('calTab');
 thisDate = new Date(Year, Month, 1);
 thisDay   = thisDate.getDate();
 thisMonth = thisDate.getMonth();
 thisYear =  thisDate.getFullYear();
 document.getElementById('calMonth').innerHTML=Months[thisMonth];
 document.getElementById('calYear').innerHTML=thisYear;
 
  
 if(calTable.rows.length> 2) {
  Length=calTable.rows.length;
 for(var i =1; i < Length ; i++)
  calTable.childNodes[0].removeChild(calTable.rows[1]);
 }

 i=1;
 do
 {
   thisWeek  = thisDate.getDay();
   if(i==1 ||  !thisWeek)
    var row=document.createElement('tr');
   if(i==1 && thisWeek)
   {
    for(var j =0; j < thisWeek; j++)
    {
     var td=document.createElement('td');
     td.innerHTML='&nbsp;'; 
     row.appendChild(td);
    }
   }
   var td=document.createElement('td');
   row.appendChild(td);
   td.innerHTML=i;
   
    td['onclick'] = closeCalendar;
    if(thisYear != nowDate.getFullYear() || thisMonth != nowDate.getMonth() || i !=nowDate.getDate())
    {
      td['onmouseover']=calBgOn;
      td['onmouseout']=calBgOff;
    }
    else
      td.style.backgroundColor='#ff7f00';
   if(browser[0]=='ie')  
    td.style.cursor='hand';
   else
    td.style.cursor='pointer';
   if(thisWeek==6)
    calTable.childNodes[0].appendChild(row);
   i++;
   thisDate.setDate(i);
  }
  while(thisDate.getMonth()==thisMonth);
  if(thisWeek!=6)
  {
   for(var i=0; i < 6-thisWeek; i++)
   {
    var td=document.createElement('td');
    td.innerHTML='&nbsp;';
    row.appendChild(td);
   }
   calTable.childNodes[0].appendChild(row);
  }
 }

function  closeCalendar() {
 if(browser[0]=='ie')
  thisId = event.srcElement;
 else
  thisId = this;
 undrawWindow();

 var now = new Date();
 var Year =  now.getFullYear();

 Dat = new Date(Year, permMonth, 1);

 Day=thisId.innerHTML;
 if(Day.length ==1)
  Day ="0"+Day;
 Month =''+ (parseInt(Dat.getMonth())+1);
    
 if(Month.length == 1)
  Month="0"+Month;
 if(calFormat ==2)
  calField.value= Day+'/'+Month+'/'+ Dat.getFullYear();
 else
  calField.value= Dat.getFullYear()+'-'+Month+'-'+Day;
}


  
function calBgOn() {
 if(browser[0]=='ie')
  thisId = event.srcElement;
 else
  thisId = this;
   
 thisId.style.backgroundColor='#ff7f00';
}

function calBgOff() {
 if(browser[0]=='ie')
  thisId = event.srcElement;
 else
  thisId = this;
   
 thisId.style.backgroundColor='white';
}

