//<!-- This script and many more are available free online at -->
//<!-- The JavaScript Source!! http://javascript.internet.com -->

function maxDays(mm, yyyy){
	var mDay;
	if((mm == 3) || (mm == 5) || (mm == 8) || (mm == 10)){
		mDay = 30;
  	}
  	else{
  		mDay = 31
  		if(mm == 1){
   			if (yyyy/4 - parseInt(yyyy/4) != 0){
   				mDay = 28
   			}
		   	else{
   				mDay = 29
  			}
		}
	}
	return mDay;
}

function changeText(value1){
	var currM = parseInt(document.calForm.selMonth.value)+1;
	var currY = parseInt(document.calForm.selYear.value);
	//alert(currY + '-' + currM + '-' + value1);
	document.getElementById(textId).value=currY + '-' + currM + '-' + value1;
	document.getElementById(selectDate2).style.display="none";
	/*
	if (eval(id).style.backgroundColor != "yellow"){
		eval(id).style.backgroundColor = "yellow"
	}
	else{
		eval(id).style.backgroundColor = "#ffffff"
	}
	*/
}
var textId = ""
var selectDate = "selectDate";
var selectDate2 = "selectDate2";
function writeCalendar(textId1, selectDate1, selectDate21){
	
	textId = textId1;
	selectDate = selectDate1;
	selectDate2 = selectDate21;
	document.getElementById(selectDate).innerHTML=""
	var now = new Date
	var dd = now.getDate()
	var mm = now.getMonth()
	var dow = now.getDay()
	var yyyy = now.getFullYear()
	var arrM = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
	var arrY = new Array()
	for (ii=0;ii<=4;ii++){
		arrY[ii] = yyyy - 2 + ii
	}
	var arrD = new Array("S","M","T","W","T","F","S")

	var text = "Click on a day to select<br><br class=shortBr><form name=calForm>"
	text += "<table border=0 cellspacing=0>"
	text += "<tr><td>"
	text += '<table width=100% cellspacing=0 border=0><tr>'
	text += "<td align=left>"
	text += "<select name=selMonth onChange='changeCal()'  style='width:100;height:19px'>"
	for (ii=0;ii<=11;ii++){
		if (ii==mm){
			text += "<option value= " + ii + " Selected>" + arrM[ii] + "</option>"
		}
		else{
			text += "<option value= " + ii + ">" + arrM[ii] + "</option>"
		}
	}
	text += "</select>"
	text += "</td>"
	text += "<td  style='text-align:right'>"
	text += "<select name=selYear onChange='changeCal()'  style='width:70;height:19px'>"
	
	for (ii=0;ii<=4;ii++){
		if (ii==2){
			text += "<option value= " + arrY[ii] + " Selected>" + arrY[ii] + "</option>"
		}
		else{
			text += "<option value= " + arrY[ii] + ">" + arrY[ii] + "</option>"
		}
	}
	text += "</select>"
	text += "</td>"
	text += "</tr></table><br class='shortBr'>"
	text += "</td></tr>"
	text += "<tr><td>"
	text += '<span class="' + selectDate + '">' + "<table border=0 cellspacing=0 width=100%>"
	text += "<tr bgcolor=darkblue>"
	for (ii=0;ii<=6;ii++){
		text += "<td style='text-align:center;color:white;'><b>" + arrD[ii] + "</b></td>"
	}
	text += "</tr>"
	aa = 0
	for (kk=0;kk<=5;kk++){
		text += "<tr>"
		for (ii=0;ii<=6;ii++){
			text += "<td align=center><span id=sp" + aa + " onClick='changeText(" + 1 + ")'>1</span></td>"
			aa += 1
		}
		text += "</tr>"
	}
	text += "</table></span>"
	text += "</td></tr>"
	text += "</table>"
	text += "</form>";
	
	document.getElementById(selectDate2).style.display="";
	document.getElementById(selectDate).innerHTML=text;
	//document.write(text)
	changeCal()
}

function changeCal(){
	var now = new Date
	var dd = now.getDate()
	var mm = now.getMonth()
	var dow = now.getDay()
	var yyyy = now.getFullYear()
	var currM = parseInt(document.calForm.selMonth.value)
	var prevM
	if (currM!=0){
		prevM = currM - 1
	}
	else{
		prevM = 11
	}
	var currY = parseInt(document.calForm.selYear.value)
	var mmyyyy = new Date()
	mmyyyy.setFullYear(currY)
	mmyyyy.setMonth(currM)
	mmyyyy.setDate(1)
	var day1 = mmyyyy.getDay()
	if (day1 == 0){
		day1 = 7
	}
	var arrN = new Array(41)
	var arrN2 = new Array(41)
	var aa
	for (ii=0;ii<day1;ii++){
		arrN[ii] = "<font color=#E6E6FA>.</font>"
	}
	aa = 1
	//alert(day1+maxDays(currM,currY)-1);
	//alert("ii = " + ii)
	for (ii=day1;ii<=day1+maxDays(currM,currY)-1;ii++){
		arrN[ii] = "<a style='' href='javascript:changeText(" + aa + ")'>" + aa + "</a>"
		arrN2[ii] =  aa
		aa += 1
	}
	//alert("ssss = " + arrN[11])
	aa = 1
	for (ii=day1+maxDays(currM,currY);ii<=41;ii++){
		arrN[ii] = "<font color=#E6E6FA>.</font>"
		aa += 1
	}
	for (ii=0;ii<=41;ii++){
		//eval("sp"+ii).style.backgroundColor = ""
	}
	var dCount = 0
	ii=0
	for (ii=0;ii<=41;ii++){
		document.getElementById("sp"+ii).innerHTML = arrN[ii]
		if (((ii<7)&&(arrN[ii]>20))||((ii>27)&&(arrN[ii]<20))){
			//eval("sp"+ii).innerHTML = arrN[ii]
			document.getElementById("sp"+ii).className = "c3"
		}
		else{
			//eval("sp"+ii).innerHTML = arrN[ii]
			if ((dCount==0)||(dCount==6)){
				document.getElementById("sp"+ii).className = "c2"
				document.getElementById("sp"+ii).innerHTML = '<span class="weekend">' + arrN[ii] + '</span>'
			}
			else{
				document.getElementById("sp"+ii).innerHTML = '<span class="weekday">' + arrN[ii] + '</span>'
			}
			if ((arrN2[ii]==dd)&&(mm==currM)&&(yyyy==currY)){
				//document.getElementById("sp"+ii).style.backgroundColor="#cccccc"
				document.getElementById("sp"+ii).innerHTML='<b><font color=blue size=2>' + dd + '</font></b>'
			}
		}
		dCount += 1
		if (dCount>6){
			dCount=0
		}
	}
}

