﻿var vExtraAction=0;      
var g_CalendarDiv;
var g_vlstMonth;
var g_vLstYear;
var g_bExplorer=(window.navigator.appName.toLowerCase().indexOf('explorer') > -1);
			
function Calendar(txtResult,strExitIcon)
{
	this.txtResult=txtResult;
	var strID=txtResult.id + "_calendardiv";
	this.exitIcon=strExitIcon;
	this.calendarDiv=document.getElementById(strID);
	if(this.calendarDiv!=null)
	this.calendarDiv.parentNode.removeChild(this.calendarDiv);
	
	this.calendarDiv=document.createElement("DIV");
	this.calendarDiv.id= strID;
	this.getCalendarDiv=CalendarGetCalendarDiv;	
	this.extraAction=0;
	this.tblMain=null;
	this.lstYear=document.createElement("SELECT");
	this.lstMonth=document.createElement("SELECT");
//	this.lstYear.style.className = 'calSelect';
	this.lstYear.style.fontSize = '11px';
  this.lstMonth.style.fontSize = '11px';
  this.lstYear.style.width = '60px';
	this.lstMonth.style.width = '110px';
	
	this.tableBody=document.createElement("TBODY");
	
	this.lstMonth.parentCalendar=this;
	this.lstYear.parentCalendar=this;
	this.retrieveDate=CalendarRetrieveDate;
	this.buildStructure=CalendarBuildStructure;
	this.populateTable=CalendarPopulateTable;
	this.fillYears=CalendarFillYears;
	this.fillMonths=CalendarFillMonths;
	this.calendarDiv.parentCalendar=this;
	this.setCurrMonth=CalendarSetCurrMonth;
	this.buildStructure();
	this.fillMonths();
	this.fillYears();
	this.bNoYear=false;
	this.populateTable();
	this.calendarDiv.appendChild(this.tblMain);
	//<IFRAME name='divbehindcal' style='DISPLAY: none; LEFT: 0px; POSITION: absolute;zIndex:0; TOP: 0px' ></IFRAME>";
	this.iframe=document.createElement('iframe');
	this.iframe.style.display='none';
	this.iframe.style.left='0px';
	this.iframe.style.position='absolute';
	this.iframe.style.top='0px';
	this.iframe.src='javascript:false;';
	this.iframe.frameBorder='0';
	this.iframe.scrolling='no';
	this.onFinish=null;//event for when window is closed.
//	this.calendarDiv.parentElement.insertBefore(this.iframe,this.calendarDiv);
}


Calendar.prototype.adjustIFrame = function()
{
	this.iframe.style.display='';
	this.iframe.style.position='absolute';
	this.iframe.style.zIndex=102;
	this.calendarDiv.style.zIndex=103;
	this.iframe.style.left=this.calendarDiv.offsetLeft;
	this.iframe.style.top=this.calendarDiv.offsetTop;
	this.iframe.style.width=this.calendarDiv.offsetWidth;
	this.iframe.style.height=this.calendarDiv.offsetHeight;
	
	this.calendarDiv.parentElement.insertBefore(this.iframe,this.calendarDiv);
}

Calendar.prototype.setNoYear = function()
{
	this.bNoYear=true;
}
function CalendarGetCalendarDiv()
{
	return this.calendarDiv;
}
// set month choice to current month
	function AddWeekDaysToRow(tr)
	{
		var strDays="Zo,Ma,Di,Wo,Do,Vr,Za";
		var arDays=strDays.split(",");
		var td;
		for(var i=0;i < 7 ; i ++)
		{
			td=tr.insertCell(-1);
			td.align='center';
			td.style.backgroundColor='#B81319';
			td.style.color='white';
			td.innerHTML= '<div style="width: 20px;">' + arDays[i] + '</div>';
		}
	}
	
	function CalendarBuildStructure()
	{
		this.tblMain=document.createElement("table");
		this.tblMain.name="MainTable";
		this.tblMain.width=200;
		this.tblMain.border=1;
		this.tblMain.borderColor='#B81319';
		this.tblMain.bgColor="#FFFFFF";
		this.tblMain.borderColor="#A71318";
		this.tblMain.cellPadding=0;
		this.tblMain.cellSpacing=0;
		var tr=this.tblMain.insertRow(-1);
		var td=tr.insertCell(-1);
		var tblCalendar=document.createElement("table");
		tblCalendar.border=0;
		tblCalendar.cellPadding=0;
		tblCalendar.cellSpacing=0;
		tblCalendar.bgColor="#FFFFFF";
		td.appendChild(tblCalendar);
		tr=tblCalendar.insertRow(-1);
		td=tr.insertCell(-1);
		tr.bgColor='#B81319';
		td.appendChild(this.lstYear);	
		td=tr.insertCell(-1);
		td.appendChild(this.lstMonth);
		if(this.exitIcon!=null)
		{
			var tdExit=tr.insertCell(-1);
			var a=document.createElement("a");
			var img=document.createElement("IMG");
			img.style.width='15px';
			img.style.height='15px';
			img.src=this.exitIcon;
			a.calendar=this;
			if(g_bExplorer)
			a.onclick=new Function('this.calendar.ClearElements();');
			else
			a.setAttribute('onclick','this.calendar.ClearElements();');
			a.appendChild(img);
			tdExit.align='right';
			tdExit.appendChild(a);
		}
		tr=tblCalendar.insertRow(-1);
		td=tr.insertCell(-1);
		td.colSpan=3;
		var tblWeekDays=document.createElement("table");
		tblWeekDays.border=0;
		//tblWeekDays.borderColor="#0000ff";
		tblWeekDays.cellPadding=0;
		tblWeekDays.cellSpacing=0;
		td.appendChild(tblWeekDays);
		tr=tblWeekDays.insertRow(-1);
		tr.style.backgroundColor='#003A75';
		AddWeekDaysToRow(tr);
		tblWeekDays.appendChild(this.tableBody);
	}
	Calendar.prototype.ClearElements = function()
	{
		try{
		this.iframe.style.display='none';
		this.iframe.parentNode.removeChild(this.iframe);
		}catch(e){}
		try{
		this.calendarDiv.parentNode.removeChild(this.calendarDiv);
		}catch(e){}
		if(this.onFinish)this.onFinish();

	}
      function CalendarSetCurrMonth(today) 
      {
         this.lstMonth.selectedIndex = today.getMonth();
      }
            
	function CalendarFillMonths()
	{
		if(g_bExplorer)
			this.lstMonth.onchange=new Function('this.parentCalendar.populateTable();');
		else
			this.lstMonth.setAttribute('onchange','this.parentCalendar.populateTable();');
		var strMonths="Januari,Februari,Maart,April,Mei,Juni,Juli,Augustus,September,October,November,December";
		var arMonths=strMonths.split(",");
		var opt;
		var doc=document;
		for(var i=0; i < 12;i++)
		{
//			opt=doc.createElement("OPTION");
			this.lstMonth.options[this.lstMonth.options.length]=new Option(arMonths[i],arMonths[i]);
			
//			opt.value=arMonths[i];
//			opt.innerText=arMonths[i];
		}
	}
	

function CalendarFillYears() 
{
   var today = new Date();
   var thisYear = today.getFullYear();
   var opt;
   var doc=document;
   
   var lstYear=this.lstYear;
  if(g_bExplorer)
  {
    this.lstYear.onchange=new Function('this.parentCalendar.populateTable();');
  }
  else
  {
    this.lstYear.setAttribute('onchange','this.parentCalendar.populateTable();');
  } 
  for (i = 1890; i <= thisYear + 10 ; i++) 
   {
		lstYear.options[lstYear.length]=new Option(i,i);
   }
   this.setCurrMonth(today);
   //g_vLstYear.selected=75;
   lstYear.selectedIndex = (thisYear - 1890);
}
      
   function CalendarPopulateTable() 
      {
		var doc = document;
         var theMonth = this.lstMonth.selectedIndex;
         var lstyear=this.lstYear;
         var theYear = parseInt(lstyear.options[lstyear.selectedIndex].value);
         // initialize date-dependent variables
         var firstDay = getFirstDay(theYear, theMonth);
         var howMany = getMonthLen(theYear, theMonth);
         // fill in month/year in table header
         //this.tableHeader.innerHTML = "<B>" + theMonths[theMonth] + " " + theYear + "</B>";
         // initialize vars for table creation
         var dayCounter = 1;
         var TBody = this.tableBody;
         // clear any existing rows
         while (TBody.rows.length > 0)
            TBody.deleteRow(0);
         var newR, newC;
         var done=false;
         var strOnClick='document.getElementById("';
         strOnClick += this.calendarDiv.id;
         strOnClick += '").parentCalendar.retrieveDate("';
         strOnClickEnd='");'
         while (!done) 
         {
            // create new row at end
            newR = TBody.insertRow(TBody.rows.length);
            for (var i = 0; i < 7; i++) 
            {
               // create new cell at end of row
               newC = newR.insertCell(newR.cells.length);
               if (TBody.rows.length == 1 && i < firstDay) {
                  // no content for boxes before first day
                  newC.innerHTML = "";
                  continue;
               }
               if (dayCounter == howMany) {
                  // no more rows after this one
                  done = true;
               }
               // plug in date (or empty for boxes after last day)
				var strHtml="";
				if(dayCounter <= howMany)
				{				
  					strHtml='<div align=center onmouseover="this.style.cursor= \'pointer\'"';
  					strHtml += " onclick='document.getElementById(\"" + this.calendarDiv.id + "\").parentCalendar.retrieveDate(" + dayCounter + ")'>";
					strHtml += dayCounter++;
					strHtml += '</div>';
				}
               newC.innerHTML =strHtml;
               newC.width=100;
            }
         }
      }
    function CalendarRetrieveDate(day)
   {
		var strDate;
		var lst= this.lstYear;
		if(lst.selectedIndex == -1)return;
		var month = this.lstMonth.selectedIndex + 1;
		day = '0' + day
		month = '0' + month;
		strDate = String(day).substring(String(day).length - 2);
		strDate += '-';
   		strDate += String(month).substring(String(month).length - 2);
		if(!this.bNoYear)
		{
			strDate += '-';
			strDate += lst[lst.selectedIndex].value;
		}

		this.calendarDiv.innerHTML='';
		this.txtResult.value=strDate;
		this.ClearElements();		
  }
      // number of days in the month
	
    function getFirstDay(theYear, theMonth)
    {
        var firstDate = new Date(theYear,theMonth,1);
        return firstDate.getDay();
    }
	
	function InitCalendar(g_CalendarDivControl)
	{
		g_CalendarDiv=g_CalendarDivControl;
	}
	
    function RetrieveDate(day)
   {
		var doc=document;
		var strDate;
		var lst= g_vLstYear;
		var month = g_vlstMonth.selectedIndex + 1;
		day = '0' + day
		month = '0' + month;
		strDate = lst[lst.selectedIndex].value;
   			strDate += String(month).substring(String(month).length - 2);
			strDate += String(day).substring(String(day).length - 2);
			g_CalendarDiv.innerHTML='';
			if(vExtraAction == 1)
			{
				//window.opener.document.all.SelectedDate=strDate;				
				window.returnValue = strDate
				window.close();
			}
	 }
      // number of days in the month
      function getMonthLen(theYear, theMonth) 
      {
         var oneHour = 1000 * 60 * 60;
         var oneDay = oneHour * 24;
         var thisMonth = new Date(theYear, theMonth, 1);
         var nextMonth = new Date(theYear, theMonth + 1, 1);
         var len = Math.ceil((nextMonth.getTime() - 
            thisMonth.getTime() - oneHour)/oneDay);
         return len;
      }
      // create array of English month names
      var theMonths = ["January","February","March","April","May","June",
         "July","August","September","October","November","December"];
      // return IE4+ or W3C DOM reference for an ID
      


