/*
	define a new class to deal with dates:
	Revision:		4.0.0
							function getDecimalPartOfWholeYear(firstDate, secondDate) changed

	Author:			Miles Glisovic
	Modtime:		03/01/09
*/
function myGetYear()
{
	return this.myYear
}
function myGetMonth()
{
	return this.myMonth
}
function myGetDay()
{
	return this.myDay
}
function mySetYear(yearNumber)
{
	this.myYear = yearNumber
	this.adjustLeap()
}
function mySetMonth(monthNumber)
{
	this.myMonth = monthNumber
	this.adjustLeap()
}
function mySetDay(dayNumber)
{
	this.myDay = dayNumber
	this.adjustLeap()
}
function getNoOfDaysSince01011900()
{
	var y, y1900, x, x1900
	y1900 = parseFloat(1900 + (1 - 2.85) / 12)
	x1900 = Math.floor(Math.floor(Math.floor(367*y1900)-1.75*Math.floor(y1900) + 1)-0.75*Math.floor(0.01*y1900))+1721119
	y = parseFloat(this.myYear + (this.myMonth - 2.85) / 12)
	x = Math.floor(Math.floor(Math.floor(367*y)-1.75*Math.floor(y)+this.myDay)-0.75*Math.floor(0.01*y))+1721119
	return x - x1900
}
function getNoOfDays(mydate)
{
	// return + number if 'this' date is before passed reference to 'mydate'; or
	// call object's method as: var diff = date1.getNoOfDaysAfterThisDate(date2)
	// diff is + if date1 < date2
	return (mydate.getNoOfDaysSince01011900() - this.getNoOfDaysSince01011900())
}
function date1BeforeDate2(mydate)
{
	if((mydate.getNoOfDaysSince01011900() - this.getNoOfDaysSince01011900()) > 0)
		return true
	else
		return false
}
function date1AfterDate2(mydate)
{
	if((mydate.getNoOfDaysSince01011900() - this.getNoOfDaysSince01011900()) < 0)
		return true
	else
		return false
}
function date1NOTAfterDate2(mydate)
{
	if((mydate.getNoOfDaysSince01011900() - this.getNoOfDaysSince01011900()) >= 0)
		return true
	else
		return false
}
function equalDates(mydate)
{
	if((mydate.getNoOfDaysSince01011900() - this.getNoOfDaysSince01011900()) == 0)
		return true
	else
		return false
}
function alertDate(s)
{
alert((s=="")?"alertDate: ":(s+": ") + zerroPrefix1(this.myDay) + "/" + zerroPrefix1(this.myMonth) + "/" + zerroPrefix1(this.myYear))
}
function get_sDD_MM_YYYY()
{
return (zerroPrefix1(this.myDay) + "/" + zerroPrefix1(this.myMonth) + "/" + this.myYear)
}

// define the class itself
function MyDate4(myYear, myMonth, myDay)
{
	/*
	MyDate4 object can be construced as 'var date1 = new MyDate4(2000,11,04)'
	OR
	'var date1 = new MyDate4()', which will default to today's date
	*/
   var today=new Date()
   this.myYear = ((myYear==null)?(today.getFullYear()):parseInt(myYear,10))
   this.myMonth = ((myMonth==null)?(today.getMonth() + 1):parseInt(myMonth,10))
   this.myDay = ((myDay==null)?(today.getDate()):parseInt(myDay,10))
   // class methods
   this.myGetYear = myGetYear
   this.myGetMonth = myGetMonth
   this.myGetDay = myGetDay
   this.mySetYear = mySetYear
   this.mySetMonth = mySetMonth
   this.mySetDay = mySetDay
   this.getNoOfDaysSince01011900 = getNoOfDaysSince01011900
   this.getNoOfDaysAfterThisDate = getNoOfDays
   this.isThisDateBefore = date1BeforeDate2
   this.isThisDateAfter = date1AfterDate2
   this.isThisDateNOTAfter = date1NOTAfterDate2
   this.areDatesEqual = equalDates
   this.alertDate = alertDate
   this.get_sDD_MM_YYYY = get_sDD_MM_YYYY

   this.getRefBroj = getRefBroj
   this.set_DD_MM_YYYY_FromRefBroj = set_DD_MM_YYYY_FromRefBroj
   this.advanceDateFor_X_Days = advanceDateFor_X_Days
   
   this.isLeapYear = isLeapYear
   this.daysBetweenThisDateAndPreviousDate=daysBetweenThisDateAndPreviousDate
   this.adjustLeap = adjustLeap
   // 030308: from now on, myGetNumberOfYearsBetween2Dates is obsolete; instead, use dGetNumYears()
   //this.myGetNumberOfYearsBetween2Dates = myGetNumberOfYearsBetween2Dates
   
   //261008 this.iGetNumYears = iGetNumYears
   //261008: from now on, dGetNumYears is obsolete; instead, use dGetNoOfYears()
   //this.dGetNumYears = dGetNumYears
   this.thisYearOneYearOrMoreEarlier = thisYearOneYearOrMoreEarlier
   // 261008:
   // new function to return a decimal number representing the number of years between this date and
   // any other date; do not use dGetNumYears() any more
   this.dGetNoOfYears = dGetNoOfYears
}
function set_DD_MM_YYYY_FromRefBroj(refBroj1)
{
// class member method; get refBroj1 and sets object's myYear, myMonth, myDay member variables
// I start with refBroj1 (number of days since 1st January 4713BC) and sets object's myYear, myMonth, myDay member variables
// used in advanceDateFor_X_Days class member method
// oposite to getRefBroj()
var n = refBroj1 - 1721119;
var c = myTrunc((n-0.2)/36524.25);
var np = n + c - myTrunc(c/4)

var yp = myTrunc(( np - 0.2 )/365.25);
var npp = np - myTrunc(365.25*yp);
var mp = myTrunc((npp - 0.5)/30.6);
var dd = myTrunc(npp - 30.6*mp + 0.5);
if(mp <= 9)
{
	mm = mp+3;
	yy = yp
}
else
{
	yy = yp+1;
	mm = mp-9;
}
this.myYear=yy
this.myMonth=mm
this.myDay=dd
}
function getRefBroj()
{
// calculates this object's number of days since 1st January 4713BC
// I start with MyDate4 class-object and calculate this object's number of days since 1st January 4713BC
// oposite to set_DD_MM_YYYY_FromRefBroj()
var yp = this.myYear + (this.myMonth - 2.85)/12.0;
var temp = Math.floor(367*yp)-Math.floor(yp)-0.75*Math.floor(yp) + this.myDay;
temp = Math.floor(temp)-0.75*Math.floor(yp/100)
temp = Math.floor(temp)+1721115;
return myTrunc(temp);
}
function advanceDateFor_X_Days(X)
{
// i start with any MyDate4 object and advance its member variables X days in the future
this.set_DD_MM_YYYY_FromRefBroj(this.getRefBroj() + X)
}
function adjustLeap()
{
if(!this.isLeapYear())
{
	if(this.myGetMonth() == 2 && this.myGetDay() > 28)
	{
		this.myMonth = 3
		this.myDay = 1
	}
}
}
function isLeapYear()
{
	if(this.myYear % 100 == 0)
	{
		if(this.myYear % 400 == 0)
			return true;
	}
	else
	{
		if((this.myYear % 4) == 0)
			return true;
	}
	return false;
}
function daysBetweenThisDateAndPreviousDate(mydate)
{
	return (this.getNoOfDaysSince01011900() - mydate.getNoOfDaysSince01011900())
}
/*
function myGetNumberOfYearsBetween2Dates(nextDate)
{
	var noOfDays_date_m1 = this.getRefBroj()
	var noOfDays_date_M2 = nextDate.getRefBroj()

	var date_m1_1 = new MyDate3()
	date_m1_1.mySetYear(this.myGetYear() + 1)
	date_m1_1.mySetMonth(this.myGetMonth())
	date_m1_1.mySetDay(this.myGetDay())

	var noOfDays_date_m1_1 = date_m1_1.getRefBroj()
	var count = 0

	while(noOfDays_date_m1_1 <= noOfDays_date_M2)
	{
		count++;

		date_m1_1.mySetYear(date_m1_1.myGetYear() + 1)
		date_m1_1.mySetMonth(date_m1_1.myGetMonth())
		date_m1_1.mySetDay(date_m1_1.myGetDay())

		noOfDays_date_m1_1 = date_m1_1.getRefBroj()
	}

	if(noOfDays_date_m1_1 == noOfDays_date_M2)
		count = count++;
	else
	{
		date_m1_1.mySetYear(date_m1_1.myGetYear() - 1)
		date_m1_1.mySetMonth(date_m1_1.myGetMonth())
		date_m1_1.mySetDay(date_m1_1.myGetDay())
		noOfDays_date_m1_1 = date_m1_1.getRefBroj()

		count = count + (noOfDays_date_M2 - noOfDays_date_m1_1)/365.0
	}
	return count;
}
*/
/*261008
function iGetNumYears(secondDate)
{
	var count, ind
	var firstDate1, secondDate1, tmpDate

	if(!this.isThisDateBefore(secondDate))
	{
		firstDate1 = new MyDate3(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
		secondDate1 = new MyDate3(this.myGetYear(), this.myGetMonth(), this.myGetDay());
	}
	else
	{
		firstDate1 = new MyDate3(this.myGetYear(), this.myGetMonth(), this.myGetDay());
		secondDate1 = new MyDate3(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
	}

	if(firstDate1.myGetDay() == secondDate1.myGetDay() && firstDate1.myGetMonth() == secondDate1.myGetMonth() && firstDate1.myGetYear() == secondDate1.myGetYear())
		count = 0;
	else
	{
		if( firstDate1.myGetDay() == secondDate1.myGetDay() && firstDate1.myGetMonth() == secondDate1.myGetMonth() )
			count = secondDate1.myGetYear() - firstDate1.myGetYear();
		else
		{
			tmpDate = new MyDate3();
			ind = 0
			count = 0

			while(true)
			{
				ind++;
				tmpDate.mySetDay(firstDate1.myGetDay())
				tmpDate.mySetMonth(firstDate1.myGetMonth())
				tmpDate.mySetYear(firstDate1.myGetYear() + ind)

				if(tmpDate.isThisDateBefore(secondDate1))
					count++;
				else
				{
					if(tmpDate.areDatesEqual(secondDate1))
						count++;
					break;
				}
			}
		}
	}

	return count
}
*/
function dGetNoOfYears(secondDate)
{
	var iNoYears
	var firstDate1, secondDate1, tmpDate
	var tmpDate = new MyDate4()
	var startDateForFractionCalculation = new MyDate4()

  var noOfWholeYears = 0
  var decimalPartOfWholeYear = 0

	if(!this.isThisDateBefore(secondDate))
	{
		firstDate1 = new MyDate4(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
		secondDate1 = new MyDate4(this.myGetYear(), this.myGetMonth(), this.myGetDay());
	}
	else
	{
		firstDate1 = new MyDate4(this.myGetYear(), this.myGetMonth(), this.myGetDay());
		secondDate1 = new MyDate4(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
	}
	startDateForFractionCalculation = getStartDateForFractionCalculation(firstDate1, secondDate1)
  if(startDateForFractionCalculation.myGetYear() < firstDate1.myGetYear() && startDateForFractionCalculation.myGetYear() < secondDate1.myGetYear())
    noOfWholeYears = -1000
  else
  {
    noOfWholeYears = startDateForFractionCalculation.myGetYear() - firstDate1.myGetYear()
    decimalPartOfWholeYear = getDecimalPartOfWholeYear(startDateForFractionCalculation, secondDate1)
	}

	if(noOfWholeYears == -1000)
		return NaN
	else
		return noOfWholeYears + decimalPartOfWholeYear
}
function getDecimalPartOfWholeYear(firstDate, secondDate)
{
  var day1, day2, month1, month2, year1, year2
  var ret, totalNoOfDays, w
  var secondDate1
  var numberOfDaysInTheYear

  ret = 0

  if(firstDate.areDatesEqual(secondDate ))
    ret = 0
  else
  {
    day1 = firstDate.myGetDay()
    day2 = secondDate.myGetDay()
    month1 = firstDate.myGetMonth()
    month2 = secondDate.myGetMonth()
    year1 = firstDate.myGetYear()
    year2 = secondDate.myGetYear()

		totalNoOfDays = Math.abs(secondDate.daysBetweenThisDateAndPreviousDate(firstDate))

    if(day1 == 29 && month1 == 2)
			secondDate1 = new MyDate4(year1 + 1, 3, 1);
    else
    	secondDate1 = new MyDate4(year1 + 1, month1, day1);

    if(!((new MyDate4(year1, 1, 1)).isLeapYear()) && !((new MyDate4(year1 + 1, 1, 1)).isLeapYear()))
      numberOfDaysInTheYear = 365
    else
    {
      if(!((new MyDate4(year1, 1, 1)).isLeapYear()))
      {
      	if(secondDate1.isThisDateAfter(new MyDate4(year1 + 1, 2, 29)))
          numberOfDaysInTheYear = 366
        else
          numberOfDaysInTheYear = 365
      }
      else
      {
        if(firstDate.isThisDateNOTAfter(new MyDate4(year1, 2, 29)))
        {
          if(secondDate1.isThisDateAfter(new MyDate4(year1, 2, 29)))
            numberOfDaysInTheYear = 366
        	else
          	numberOfDaysInTheYear = 365
        }
        else
        	numberOfDaysInTheYear = 365
      }
    }
    ret = totalNoOfDays/numberOfDaysInTheYear
  }
  return ret
}
function getStartDateForFractionCalculation(firstDate, secondDate)
{
	var firstDate1, secondDate1, tmpDate
	var day1, day2, month1, month2, year1, year2
	var count
	var firstDateIs29Feb
	var dTmp, mTmp, yTmp

	firstDate1 = new MyDate4(firstDate.myGetYear(), firstDate.myGetMonth(), firstDate.myGetDay());
	secondDate1 = new MyDate4(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
	

  day1 = firstDate1.myGetDay()
  day2 = secondDate1.myGetDay()
  month1 = firstDate1.myGetMonth()
  month2 = secondDate1.myGetMonth()
  year1 = firstDate1.myGetYear()
  year2 = secondDate1.myGetYear()
  
  tmpDate = new MyDate4(firstDate1.myGetYear(), firstDate1.myGetMonth(), firstDate1.myGetDay());
  count = 0

	if(day1 == 29 && month1 == 2)
		firstDateIs29Feb = true
	else
		firstDateIs29Feb = false

	if(tmpDate.areDatesEqual(secondDate1) == false)
	{
		while(tmpDate.isThisDateBefore(secondDate1) || tmpDate.areDatesEqual(secondDate1))
		{
			count = count + 1
      dTmp = day1
      mTmp = month1
      yTmp = year1 + count

      if(firstDateIs29Feb == true)
      {
        if(!(new MyDate4(yTmp, mTmp, dTmp)).isLeapYear())
        {
          dTmp = 1
          mTmp = 3
        }
      }
      
      if(secondDate1.isThisDateBefore((new MyDate4(yTmp, mTmp, dTmp))))
      	break;
      else
      {
      	tmpDate = (new MyDate4(yTmp, mTmp, dTmp))
      	if(tmpDate.areDatesEqual(secondDate1))
      		break;
      }
      if(count > 10000)
      {
        // return dummy date whose year is less than the MIN of year1 and year2
        // so i can test it in the calling environment
        tmpDate = (new MyDate4(Math.min(year1, year2) - 1, 1, 1))
        break;
			}
		}
	}
	return tmpDate
}
/*261008
function dGetNumYears(secondDate)
{
	var iNoYears
	var firstDate1, secondDate1, tmpDate
	var tmpDate = new MyDate3()

	if(!this.isThisDateBefore(secondDate))
	{
		firstDate1 = new MyDate3(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
		secondDate1 = new MyDate3(this.myGetYear(), this.myGetMonth(), this.myGetDay());
	}
	else
	{
		firstDate1 = new MyDate3(this.myGetYear(), this.myGetMonth(), this.myGetDay());
		secondDate1 = new MyDate3(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
	}

	iNoYears = firstDate1.iGetNumYears(secondDate1)

	tmpDate.mySetDay(firstDate1.myGetDay())
	tmpDate.mySetMonth(firstDate1.myGetMonth())
	tmpDate.mySetYear(firstDate1.myGetYear() + iNoYears)

	return (iNoYears + getFractionOfAYear(tmpDate, secondDate1));
}
*/
function thisYearOneYearOrMoreEarlier(secondDate)
{
	var secondDate11, firstDate11;

	// re-adjust 'special' dates for the purpose of this function
	if(secondDate.isLeapYear() && secondDate.myGetMonth() == 2 && secondDate.myGetDay() == 29)
		secondDate11 = new MyDate4(secondDate.myGetYear(), 3, 1);
	else
		secondDate11 = new MyDate4(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());

	if(this.isLeapYear() && this.myGetMonth() == 2 && this.myGetDay() == 29)
		firstDate11 = new MyDate4(this.myGetYear(), 3, 1);
	else
		firstDate11 = new MyDate4(this.myGetYear(), this.myGetMonth(), this.myGetDay());

	var diffY = secondDate11.myGetYear() - firstDate11.myGetYear();
	var diffM = secondDate11.myGetMonth() - firstDate11.myGetMonth();
	var diffD = secondDate11.myGetDay() - firstDate11.myGetDay();

	if(diffY > 1)
		return true;
	else if(diffY == 1)
	{
		if(diffM > 0)
			return true;
		else if(diffM == 0)
		{
			if(diffD >= 0)
				return true;
			else
				return false;
		}
		else
			return false;
	}
	else
		return false;
}
/////////////////////////////////
// NON-CLASS MEMBER FUNCTIONS
function myTrunc(n)
{
if(n <= 0)
	return Math.ceil(n)
else
	return Math.floor(n)
}
function zerroPrefix1(x)
{
	var m = x
	if(m < 10)
		m="0"+m.toString()
	return m
}
/*261008
function getFractionOfAYear(firstDate, secondDate)
{
	var ret = 0.0;
	var firstDate1, secondDate1, date2902_first, date2902_second;

	if(!firstDate.isThisDateBefore(secondDate))
	{
		firstDate1 = new MyDate3(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
		secondDate1 = new MyDate3(firstDate.myGetYear(), firstDate.myGetMonth(), firstDate.myGetDay());
	}
	else
	{
		firstDate1 = new MyDate3(firstDate.myGetYear(), firstDate.myGetMonth(), firstDate.myGetDay());
		secondDate1 = new MyDate3(secondDate.myGetYear(), secondDate.myGetMonth(), secondDate.myGetDay());
	}

	if(firstDate1.myGetDay() == secondDate1.myGetDay() && firstDate1.myGetMonth() == secondDate1.myGetMonth() && firstDate1.myGetYear() == secondDate1.myGetYear())
		ret = 0;
	else
	{
		if( firstDate1.myGetDay() == secondDate1.myGetDay() && firstDate1.myGetMonth() == secondDate1.myGetMonth() )
			ret = 1;
		else
		{
			var totalNoOfDays = secondDate1.daysBetweenThisDateAndPreviousDate(firstDate1);

			if(firstDate1.isLeapYear())
				date2902_first = new MyDate3(firstDate1.myGetYear(), 2, 29);

			if(secondDate1.isLeapYear())
				date2902_second = new MyDate3(secondDate1.myGetYear(), 2, 29);
			
			if((firstDate1.isLeapYear() && (firstDate1.isThisDateBefore(date2902_first) || firstDate1.areDatesEqual(date2902_first))))
				return totalNoOfDays/366.0;
			if((secondDate1.isLeapYear() && (!secondDate1.isThisDateBefore(date2902_second) || secondDate1.areDatesEqual(date2902_second))))
				return totalNoOfDays/366.0;

			ret = totalNoOfDays/365.0;
		}
	}
	return ret;
}
*/

