//Bangla calender class
//Developed by Khaled Mahmud, 2003
//Email: kmahmud@ieee.org
var gMonthMap0=new Array(31,31,31,31,31,30,30,30,30,30,30,30);
var gMonthMap1=new Array(14,13,15,14,15,15,16,16,16,16,15,15);
var gMonthMap2=new Array(18,19,17,18,18,18,17,17,17,16,17,17);

//Constructor
function BanglaCalendar (aEngDate)
{
	//properties
	this.mEngDate=new Date();
	//alert (this.mEngDate);
	this.mYear=0;
	this.mMonth=0;
	this.mDate=0;
	this.mDay=0;
	this.mIsLeapYear=false;
	//methods
	this.getDay=getDay;
	this.getDate=getDate;
	this.getMonth=getMonth;
	this.getYear=getYear;
	this.setDate=setDate;
	this.getEngDate=getEngDate;
	this.Bangla2Eng=Bangla2Eng;
	this.MonthLength=MonthLength;
	
	this.setDate (aEngDate);
}
////////////////////////////////////////////////////////////
function getEngDate()
{
	return this.mEngDate;
}
function setDate(aEngDate)
{
	var tempedate=aEngDate;
	this.mEngDate=aEngDate;
	this.mDay=tempedate.getDay();
	var lyear = tempedate.getYear();
	var ldate = tempedate.getDate();
	var lmonth = tempedate.getMonth()+1;//1-12

	this.mIsLeapYear = IsLeapYear(lyear);
	if (lmonth < 4 || (lmonth == 4 && ldate < 14))
	{	this.mYear = lyear - 594;}
	else{this.mYear = lyear - 593;}
	this.mMonth = ((lmonth + 7) % 12) + 1;
	if (ldate < gMonthMap1[lmonth -1])
	{
		this.mDate = ldate + gMonthMap2[lmonth - 1] - 1;
		if (this.mIsLeapYear && lmonth == 3)  this.mDate = this.mDate + 1;
	}
	else
	{
		this.mDate = ldate - gMonthMap1[lmonth - 1] + 1;
		this.mMonth = (this.mMonth % 12) + 1;
	}
}
//////////////////////////////////////////////////////////////
function Bangla2Eng(newBYear, newBMonth, newBDay)
{
	var tempEngdate = new Date();
    //check the validity of the supplied day
    var isvalidday=true;
    var isleap=false;
    var lyear,lmonth,ldate;
    isleap = IsLeapYear(newBYear + 594);
    if (newBDay > 31 || newBDay < 1) isvalidday=false;
    else if (newBMonth > 12 || newBMonth < 1) isvalidday=false;
    
    else if (newBMonth > 5)
    {
       if (newBMonth != 11 && newBDay == 31) isvalidday=false;
       if (!isleap && newBDay == 31) isvalidday=false;
    }
    if (isvalidday)
    {
		//get year
		if (newBMonth > 9 || (newBMonth == 9 && newBDay >= 18))
			lyear = newBYear + 594;
		else
			lyear = newBYear + 593;
	    
		//get month
		lmonth = ((newBMonth + 2) % 12) + 1;
	    
		if (newBDay < gMonthMap2[lmonth % 12]) //not into the next Eng month
			ldate = gMonthMap1[lmonth - 1] + newBDay - 1;
		else
		{
			ldate = newBDay - gMonthMap2[lmonth % 12] + 1;
			lmonth = (lmonth % 12) + 1;
		}

		//Adjust for leapyear
		if (isleap && newBMonth == 11 && newBDay > 16)
		{
			if (newBDay == 17)
			{
				ldate = 29;
				lmonth=2;
			}
			else
			{
				ldate = newBDay - 17;
				lmonth = 3;
			}
		}        
		this.mIsLeapYear = isleap;
		this.mYear = newBYear;
		this.mMonth = newBMonth;
		this.mDate = newBDay;
	    
		tempEngdate = new Date(lyear, lmonth-1,ldate);
		this.mEngDate = tempEngdate;
		this.mDay =tempEngdate.getDay();
		return tempEngdate;
    }
    else
//error:
	{
		return tempEngdate;
	}
}

///////////////////
function getDate()
{
	return this.mDate;
}
///////////////////
function getMonth()
{
	return this.mMonth;
}
///////////////////
function getYear()
{
	return this.mYear;
}
///////////////////
function getDay()
{
	return this.mDay;
}
//////////////////////////
function MonthLength()
{
	var temp=gMonthMap0[this.mMonth-1];
	if(this.mIsLeapYear && this.mMonth==11) temp=31;
	return temp;
}
//////////////////////////////////////////////////////
function IsLeapYear(aYear)
{
    var IsLeapYr = false;
    if ((aYear % 4) == 0)
    {
		if ((aYear % 100) == 0)
		{
			if ((Math.floor(aYear / 100) % 4) == 0) 
			{
				IsLeapYr = true;
			}
		}
		else
		{
			IsLeapYr = true;
		}        
    }   
    return IsLeapYr;
}

