function Calander(el,div,table,name)
{
	this.name=name;
	this.el=el;
	this.div=div;
	this.table=table;
	this.dow = new Array('S','M','T','W','T','F','S');
	this.mnths = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');

	this.today = new Date();
	this.selected = new Date();
	this.date = new Date();

	this.yearOffset=0;
	this.month = this.date.getMonth();
	this.year = this.date.getYear();
	if(this.year<1900)
	{
		this.yearOffset=1900;
		this.year+=this.yearOffset;
	}
	this.showMonth = showMonth;
	this.setDate = setDate;
	this.display = display;
	this.displayNewDate = displayNewDate;
	this.showCalander = showCalander;
	this.addDays = addDays;
	this.addMonths = addMonths;
	this.parseDate = parseDate;
	this.setToToday = setToToday;
}
function setSelected( dIn )
{
	//dIn = Date In ( from input )
	
}
function setToToday()
{
	ty=this.today.getYear()+this.yearOffset;
	tm=this.today.getMonth();
	td=this.today.getDate();
	this.setDate(ty,tm,td);
}
function addDays( days )
{
	this.date.setDate(this.date.getDate()+days);
	this.year=this.date.getYear();
	if(this.year<1900)
	{
		this.yearOffset=1900;
		this.year+=this.yearOffset;
	}
}
function addMonths( numMonths) {
	var addYears = Math.floor(numMonths/12);
    var addMonths = numMonths - (addYears * 12);
    var newMonth = this.date.getMonth() + addMonths;

    if (newMonth > 11) {
      ++addYears;
      newMonth = this.date.getMonth() + addMonths - 12;
    }
	this.setDate(this.year+addYears,newMonth,this.date.getDate());
}
function showMonth(y,m)
{
	this.setDate(y,m,1);
	this.showCalander();
}
function displayNewDate(y,m,d)
{
	this.setDate(y,m,d);
	this.display();
}
function display()
{
	//display date in my element
	this.el.value=(this.month+1)+'-'+this.date.getDate()+'-'+this.year;
	this.cDiv.style.visibility = 'hidden';
}
function parseDate(rawDate)
{
	//var dateRegEx = /(\d{4})|(\d{1,2)/g;
	parsedDate = rawDate.split(/[^\d]/);
	var y,m,d;
	
	for(var c=0;c<parsedDate.length;c++)
	{
		var newString = parsedDate[c].replace(/^0/, '');

		var num = parseInt(newString);
		if(num>0)
		{
			if((y==null)&&(parsedDate[c].match(/\d\d\d\d/)))
			{
				y=num;
			}
			else if(m==null)
			{
				m=num;
			}
			else if(d==null)
			{
				d=num;
			}
		}
	}
	if(y==null)
	{
		return null;
	}
	return new Array(y,m,d);
}
function setDate(y,m,d)
{
	if(m<0)
	{
		m=11;
		y--;
	}
	else if(m>11)
	{
		m=0;
		y++;
	}
	this.month=m;
	this.year=y;
	this.date.setFullYear(y,m,d);
}
function showCalander()
{
	this.date.setDate(0);//go to beggining of month (for display)
	if(this.date.getDay()!=0)
	{
		this.date.setDate(this.date.getDate()-this.date.getDay());//start on sunday
	}
	
	
	if(this.cDiv=this.div)
	{
		if(this.cTable=this.table)
		{
			var row = this.cTable.rows[0];
			var cell = row.cells[0];
			cell.innerHTML='<a href="javascript:'+this.name+'.showMonth('+(this.year-1)+','+this.month+');"><font size="2">&lt;&lt;</font></a>';
			cell = row.cells[1];
			cell.innerHTML='<a href="javascript:'+this.name+'.showMonth('+this.year+','+(this.month-1)+');"><font size="2">&lt;</font></a>';
			cell = row.cells[2];
			cell.innerHTML='<div align="center"><font size="2"><b>'+this.mnths[this.month]+' '+this.year+'</b></font></div>';
			cell = row.cells[3];
			cell.innerHTML='<a href="javascript:'+this.name+'.showMonth('+this.year+','+(this.month+1)+');"><font size="2">&gt;</font></a>';
			cell = row.cells[4];
			cell.innerHTML='<a href="javascript:'+this.name+'.showMonth('+(this.year+1)+','+this.month+');"><font size="2">&gt;&gt;</font></a>';
			for(i=2; i<this.cTable.rows.length; i++)
			{
				var row = this.cTable.rows[i];
				for(k=0;k<row.cells.length; k++)
				{
					if(this.date.getMonth()==this.month)
					{
						row.cells[k].innerHTML=('<div align="center"><font size="2"><a href="javascript:'+this.name+'.displayNewDate('+(this.year)+','+this.month+','+this.date.getDate()+');">'+this.date.getDate()+'</a></font></div>');
						if((this.date.getDate()==this.selected.getDate())
															&&(this.date.getMonth()==this.selected.getMonth())&&(this.date.getYear()==this.selected.getYear()))
						{
							row.cells[k].bgColor='#00FFFF';
						}
						else if((this.date.getDate()==this.today.getDate())
															&&(this.date.getMonth()==this.today.getMonth())&&(this.date.getYear()==this.today.getYear()))
						{
							row.cells[k].bgColor='#0099FF';
						}
						else
						{
							row.cells[k].bgColor='#EEEEEE';
						}
					}
					else
					{
						row.cells[k].innerHTML='';
						row.cells[k].bgColor='#FFFFFF';
					}
					this.date.setDate(this.date.getDate()+1);
				}
			}
			
		}
	}
}
