MOB.calendar = function(input,btn)
{
	this.BTN = btn;
	this.INPUT = input;
	//this.INPUT.setAttribute('disabled','disabled');
	this.DIV = null;
	this.IMG_DIRECTORY = '/assets/scripts/mob/calendar/images/';
	this.drawCalendar();
}
MOB.calendar.prototype.getMonthLen = function(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;
}

MOB.calendar.prototype.clearCalendars = function()
{
	for(var i=0;i<calendarCount;i++)
	{
		var div = document.getElementById('mobCalendar_'+i);
		try{		
			div.parentNode.removeChild(div);
		}catch(e){
		}
	}
}
MOB.calendar.prototype.setDate = function(d,m,y){
	
	try{
		//this.INPUT.setAttribute('disabled','');
		this.INPUT.value = this.getDateString(d,m,y);
		//this.INPUT.setAttribute('disabled','disabled');
		this.toggle();
	}catch(e){
		alert(e);
	}
}
MOB.calendar.prototype.toggle = function()
{
	if(this.DIV.style.display=='none')
	{
		this.DIV.style.left = getAbsoluteLeft(this.BTN);
		this.DIV.style.top = getAbsoluteTop(this.BTN);
		this.DIV.style.display = 'block';
		if(!document.all)
		{
			this.DIV.style.marginLeft = this.INPUT.offsetWidth+'px';
		}
		else
		{
			this.DIV.style.marginLeft = '30px';
		}
	}
	else
	{
		this.DIV.style.display = 'none';
	}
}
MOB.calendar.prototype.drawCalendar = function()
{
	if(this.DIV)
	{
		this.DIV.innerHTML = '';
	}
	
	var fDate;
	var days = new Array('Su','Mo','Tu','We','Th','Fr','Sa');
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var date = new Date();
	
	if(this.INPUT.value=='')
	{
		var day = date.getDate();
		var month = date.getMonth();
		var year = date.getYear();
		if(year<1000) year+=1900;
		this.INPUT.value = this.getDateString(day,month+1,year);		
	}
	else
	{
		try{
			var dateExploded = this.INPUT.value.split('-');
			var year = parseNumber(dateExploded[0]);
			var month = parseNumber(dateExploded[1])-1;
			var day = parseNumber(dateExploded[2]);
			if(year<1000) year+=1900;
		}catch(e){}
	}
	var dayCounter = 0;
	
	var nMonth = new Date();
	nMonth.setDate(1);
	nMonth.setMonth(month);
	nMonth.setYear(year);	
	nMonth.setHours(12);
	var firstDay = nMonth.getDay();
	
	//alert(getDateString(1,month+1,year));
	//alert(firstDay);
	
	var lastDay = this.getMonthLen(year,month);
	
	if(!this.DIV)
	{
		this.DIV = document.createElement('DIV');
		this.DIV.style.display = 'none';
		this.DIV.className = 'calendar';
	}
	
	
	var table = document.createElement('TABLE');
	table.className = 'header';
	var tbody = document.createElement('TBODY');
	var tr = document.createElement('TR');
	
	//Draw a prev month button
	var td = document.createElement('TD');
	td.style.width = '12px';
	td.style.textAlign = 'center';
	var a = document.createElement('A');
	var img = document.createElement('IMG');
	img.src=this.IMG_DIRECTORY+'down.gif';
	img.CALENDAR = this;
	img.onmouseout = function(){this.style.borderStyle='solid';}
	img.onmouseover = function(){this.style.borderStyle='outset';}
	img.onmousedown = function(){this.style.borderStyle='inset';}
	img.onclick = function(){
		var m = month;
		var y = year;
		if(m>11){m = 11;}
		if(m<=0){m = 11;y--;}else{m = parseNumber(month)-1;}
		this.CALENDAR.INPUT.value = this.CALENDAR.getDateString(day,m+1,y);
		this.CALENDAR.drawCalendar();
	}
	td.appendChild(img);
	tr.appendChild(td);
	
	//Current month and year
	var td = document.createElement('TD');
	td.style.textAlign = 'center';
	var txt = document.createTextNode(months[month]+' '+year);
	td.appendChild(txt);
	tr.appendChild(td);	
	
	//Draw a next month button
	var td = document.createElement('TD');
	td.style.width = '12px';
	td.style.textAlign = 'center';
	var a = document.createElement('A');
	var img = document.createElement('IMG');
	
	img.onmouseout = function(){this.style.borderStyle='solid';}
	img.onmouseover = function(){this.style.borderStyle='outset';}
	img.onmousedown = function(){this.style.borderStyle='inset';}
	img.src=this.IMG_DIRECTORY+'up.gif';
	img.CALENDAR = this;
	img.onclick = function(){
		var m = month;
		var y = year;
		if(m>11){m = 11;}
		if(m>=11){m = 0;y++}else{m =  parseNumber(month)+1;}
		this.CALENDAR.INPUT.value = this.CALENDAR.getDateString(day,m+1,y);
		this.CALENDAR.drawCalendar();
	}
	td.appendChild(img);
	tr.appendChild(td);
	
	tbody.appendChild(tr);
	table.appendChild(tbody);
	this.DIV.appendChild(table);
	
	
	
	var table = document.createElement('TABLE');
	table.className = 'days';
	var tbody = document.createElement('TBODY');
	//Days Headers
	var tr = document.createElement('TR');
	for(var col=0;col<7;col++)
	{
		var th = document.createElement('TH');
		var txt = document.createTextNode(days[col]);
		th.appendChild(txt);
		tr.appendChild(th);
	}
	tbody.appendChild(tr);
	//Days Values
	for(var r=0;r<6;r++)
	{
		if(dayCounter<=lastDay)
		{
			var tr = document.createElement('TR');
			for(var col=0;col<7;col++)
			{
				var td = document.createElement('TD');
				td.style.textAlign = 'right';
				if(firstDay>-1 && col==firstDay)
				{
					firstDay = -1;
					dayCounter++;
				}
				
				if(dayCounter>0 && dayCounter<=lastDay)
				{
					var a = document.createElement('A');
					a.CALENDAR = this;
					a.href = 'javascript:void(0);';
										
					fDate = this.getDateString(dayCounter,month+1,year);
					if(fDate==this.INPUT.value)
					{
						a.className = 'currentValue';
					}
					var txt = document.createTextNode(dayCounter);
					a.appendChild(txt);
					a = td.appendChild(a);
					a.onclick = function()
					{
						this.CALENDAR.setDate(this.firstChild.nodeValue,month+1,year);
					}
					dayCounter++;
				}
				else
				{
					var txt = document.createTextNode(' ');
					td.appendChild(txt);
				}
				tr.appendChild(td);
			}
			tbody.appendChild(tr);
		}
	}
	
	var tr = document.createElement('TR');
	var td = document.createElement('TD');
	td.align = 'center';
	td.colSpan=7;
	var a = document.createElement('A');
	a.CALENDAR = this;
	a.href = 'javascript:void(0);';	
	var txt = document.createTextNode('Today');
	a.appendChild(txt);
	a = td.appendChild(a);
	a.onclick = function()
	{
		var d = new Date();		
		this.CALENDAR.setDate(d.getDate(),d.getMonth()+1,d.getFullYear());
	}
	tr.appendChild(td);
	tbody.appendChild(tr);
	
	
	table.appendChild(tbody);
	this.DIV.appendChild(table);
	
	
	this.INPUT.parentNode.appendChild(this.DIV);		
}

MOB.calendar.prototype.getDateString = function(y,m,d)
{
	var date = new Array(d,m,y);
	for(var i=0;i<date.length;i++)
	{
		var s = new String(date[i]);
		while(s.length<2)
		{
			s = '0'+s;
		}
		date[i] = s;
	}
	return date.join('-');
}


function parseNumber(s) {
	return parseInt(parseFloat(s));
}

