function daysInMonth(month,year) {
	var dd = new Date(year, month, 0);
	return dd.getDate();
} 

 function makeCalendar(objId, theMonth, theYear){

	theObj = document.getElementById(objId);

	if(typeof theMonth == "undefined" || typeof theYear == "undefined"){


		today = new Date();

		theMonth = today.getMonth()+1;
		theYear = today.getUTCFullYear();

	}

	while(theMonth > 12){
		theMonth -= 12;
		theYear++;
	}
	while(theMonth < 1){
		theMonth += 12;
		theYear--;
	}


	d=new Date(theYear, theMonth - 1, 1, 1, 1, 1, 1);
	days = daysInMonth(theMonth, theYear);
	startday = d.getDay() + 1;




	var months=Array('January','February','March','April','May','June','July','August','September','October','November','December');
 
 	day = " ";
	start = false;
	table = "<table border=0 width='200' class='devicecalendar'>";
	table += "<tr>";
	table += "<td> <a href=\"javascript:makeCalendar('"+objId+"', "+(theMonth-1)+", "+theYear+")\">&lt;&lt;</a> </td>";
	table += "<td colspan='5'>"+months[theMonth-1]+" "+theYear+"</td>";
	table += "<td> <a href=\"javascript:makeCalendar('"+objId+"', "+(theMonth+1)+", "+theYear+")\"> &gt;&gt; </td>";
	table += "</tr>";

	table += "<tr>";
	table += "<td>s</td>";
	table += "<td>m</td>";
	table += "<td>d</td>";
	table += "<td>w</td>";
	table += "<td>t</td>";
	table += "<td>f</td>";
	table += "<td>s</td>";

	table += "</tr>";
	table += "<tr>";
	for(i = 1; i <= days + startday - 1; i++){

		if(i == startday){
			start = true;
			day = 0;
		}
		if(start){
			day++;
		}


		table += "<td><a href=\"#\" o"+"nc"+"lick=\"document.getElementById('"+objId+"').value='"+theMonth+"-"+day+"-"+theYear+"'; DeleteCalendar('"+objId+"')\">"+day+"</a></td>";

		if(i % 7 == 0){

			table += "</tr><tr>";
	
		}
	}
	table += "</tr>";
	table += "<tr>";
	table += "<td colspan=7 align='center'> <a href=\"#\" o"+"n"+"c"+"lick=\"DeleteCalendar('"+objId+"')\">Close</a> </td>";
	table += "</tr>";
	table += "</table>";




	l = theObj.offsetLeft; 
	t = theObj.offsetTop + theObj.offsetHeight; 


	if(document.getElementById("deviceCalendar_"+objId+"")){
		document.getElementById("deviceCalendar_"+objId+"").innerHTML = ""+table+"";
	} else {

		theObj.parentNode.style.position="relative";

		theObj.parentNode.innerHTML += "<div id=\"deviceCalendar_"+objId+"\" style=\"position: absolute; left: "+l+"px; top: "+t+"px; background-color: #ffffff;\">"+table+"</div>";
	}

}




function DeleteCalendar(objId){
		obj = document.getElementById("deviceCalendar_"+objId+"");
		obj.parentNode.removeChild(obj);
}
