// JavaScript Document
function GregorianCalendar()
{
  this.dayMillis=(1000*60*60*24);
  this.monthMillis30=this.dayMillis*30;
  this.monthMillis31=this.dayMillis*31;
  this.monthMillis28=this.dayMillis*28;
  this.date=new Date();  
}

GregorianCalendar.prototype.setDate=function(aDate)
{
  this.date=aDate;
}

GregorianCalendar.prototype.shiftDate=function(aNumberOfDays)
{
  this.date= this.shift(aNumberOfDays, this.date);
  return this.date;
}

GregorianCalendar.prototype.shiftMonth=function(aNumberOfMonth, aDate)
{
  if(aNumberOfMonth==0)
  { 
    return aDate;
  }
  
  
  var theTimeMillis = aDate.getTime();
  
  var theShift = 0;
  var theMonth=aDate.getMonth();
  if (theMonth==1)
  {
    theShift=this.monthMillis28;
  }
  else
  {
    theShift=this.monthMillis30;
  }
  var theNewTime = theTimeMillis+(aNumberOfMonth*theShift);
  var theNewDate=new Date();
  theNewDate.setTime(theNewTime);
  return theNewDate;
}


GregorianCalendar.prototype.shiftMonthDays=function(aNumberOfMonth, aDate)
{
  var theNewMonth = aDate.getMonth()+aNumberOfMonth;
  var theNewYear = aDate.getFullYear();
  var theDate =aDate.getDate();
  
  if(theNewMonth>11)
  {
    var theIMonth=theNewMonth%12;
    var theNoOfYears= Math.round(theNewMonth/12);
    theNewMonth=theIMonth;
    theNewYear=theNoOfYears+theNewYear;
  }
  else (theNewMonth<0)
  {
    var theIMonth=theNewMonth%12;
    var theNoOfYears= Math.round(theNewMonth/12);
    theNewMonth=11-theIMonth;
    theNewYear=theNoOfYears-theNewYear;
  } 
  var theNewDate=new Date(theNewYear, theNewMonth, 1);
  
  return theNewDate;
}


GregorianCalendar.prototype.shift=function(aNumberOfDays, aDate)
{
  if(aNumberOfDays==0)
    return aDate;
	  
  // mha		
  var theNewTime=aNumberOfDays*this.dayMillis;
  var theDate = new Date(aDate.getTime()+theNewTime);  
  theDate.setHours(aDate.getHours());
  theDate.setMinutes(aDate.getMinutes());
  return theDate;
}

/** Evaluates whether aDate1 is before aDate2. Then return true otherwise false*/
GregorianCalendar.prototype.before=function(aDate1,aDate2 )
{
  var theTime1 = aDate1.getTime();
  var theTime2=aDate2.getTime();
  return theTime1<theTime2;
}
/** Evaluates whether aDate1 is after aDate2. Then return true otherwise false*/
GregorianCalendar.prototype.after=function(aDate1,aDate2)
{
  var theTime1 = aDate1.getTime();
  var theTime2 = aDate2.getTime();
  return theTime1>theTime2;
}

GregorianCalendar.prototype.compare=function(aDate, aDate2)
{
  var theDay=aDate.getDate();
  var theMonth=aDate.getMonth();
  var theYear=aDate.getFullYear();
  
  var theDate1=new Date(theYear, theMonth, theDay, 12,1,1);

  var theTime1=theDate1.getTime();
    
  theDay=aDate2.getDate();
  theMonth=aDate2.getMonth();
  theYear=aDate2.getFullYear();
  
  theDate1=new Date(theYear, theMonth, theDay, 12,1,1);
  
  var theTime2=theDate1.getTime();
  
  var theDiff = theTime2-theTime1;
  
  var theDiffDays=theDiff/this.dayMillis;
  
  //alert("Diff="+theDiffDays+" rounded "+Math.round(theDiffDays));
  return  Math.round(theDiffDays);
  
}

GregorianCalendar.prototype.print=function(aDate)
{
  var theMonth = aDate.getMonth()+1;
  var theMonthStr=(theMonth < 10) ? ("0" + theMonth) : theMonth;
  var theDay = aDate.getDate();
  var theDayStr= (theDay < 10) ? ("0" + theDay) : theDay;
  return (theDayStr)+'.'+(theMonthStr)+'.'+aDate.getFullYear();
}


GregorianCalendar.prototype.parse=function(aDateString)
{
  var theString = aDateString;
  var theArray=new Array();
  var theDate = null;
  var theTemp = null;
  var theIndex=theString.indexOf(".");
  for (var i=0; i<3; i++)
  {
	  if (theIndex > 0)
	  {
	  	 
	  	theTemp=theString.substring(0,theIndex);
		theString=theString.substring(theIndex+1);
		//document.formular.keys.value += "Index "+theIndex+" temp "+i+"="+theTemp+" Rest="+theString;
		if (theString && theString.length>0)
			theArray[i]=theTemp;
	  }
	  else 
	  {
	  	theArray[i]=theString;
	  }
	  theIndex=theString.indexOf(".");
  }
  if (i>=2)
  {
  	//document.formular.keys.value +=("Y="+theArray[2]+" M="+ (Number(theArray[1])-1)+" D="+ theArray[0]);
  	theDate=new Date(theArray[2], (Number(theArray[1])-1), Number(theArray[0]), 12,1,1);
  }
  return theDate;
}
