// JavaScript Document

xday = new Date();
xday = xday.getDate();


days = new Array("./image/ssun.gif", "./image/smon.gif", "./image/stue.gif", "./image/swed.gif", "./image/sthr.gif", "./image/sfri.gif", "./image/ssat.gif");
weekdays = new Array("Sun","Mon","Tue", "Wed", "Thr", "Fri","Sat");		
etstartdays1 = new Array(11,11,10,10,9,8,10,9,9,8,8,7);
etstartdays2 = new Array(9,8,10,9,9,8,8,7,11,11,10,10);
euenddays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
eumonths = new Array("Sep", "Oct", "Nov", "Dce","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep" );
etmonths = new Array("./image/sm1.gif","./image/sm2.gif","./image/sm3.gif","./image/sm4.gif","./image/sm5.gif",
		"./image/sm6.gif","./image/sm7.gif","./image/sm8.gif","./image/sm9.gif","./image/sm10.gif",
		"./image/sm11.gif","./image/sm12.gif");

//function generate() {
	
	current = true;
	today = getInputDate(window.location.href);
	month = today.getMonth();

	year =today.getYear();

	day =today.getDate();
	
//alert(day+" "+month+" "+year);

	euleapyear=false;
	euleapyear = checkleapyear(year);

//alert(euleapyear +" "+month+" "+year);

	etyear = getEthiopianyear(year,month, day);
	etleapyear = false;
	etleapyear = checkEtleapyear(etyear);

	isNextYearLeapYr = false;

	if (!etleapyear )  isNextYearLeapYr = checkleapyear(etyear+1);

	etmonth = getEthiopianmonth(month, day);

	if (year < 2000 && current ) year+=1900;

	//********************************************************************************************

	a = parseInt((14 - (month + 1)) / 12);
	y = year - a;
	m = (month + 1) + 12*a - 2;

	//********************************************************************************************
	zemen = etyear%4;
	document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src=\"./image/sz"+zemen+".gif\"  border=0 ><br>");
	document.write ("<a href=\"javascript:submitnewdate('B');\"><img src=\"./image/sprevicon.gif\"  border=0></a>");
	document.write("<img src=\""+etmonths[etmonth-1]+"\"><font face=\"arial,helvetica,sans serif\" size=1 ><font color=#0000FF>&nbsp;&nbsp;<strong>"+etyear+"</strong>&nbsp;&nbsp;");
	document.write ("<a href=\"javascript:submitnewdate('N');\"><img src=\"./image/snexticon.gif\"  border=0 ></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
	document.write("<font face=\"arial,helvetica,sans serif\" size=1><font color=#0000FF><strong>"+eumonths[etmonth-1]+"-"+eumonths[etmonth]+", "+year+"</strong></font><br>");	

	document.write("<table border=1  cellspacing=\"0\" cellpadding=\"0\"><tr >");

	for (i=0;i<7;i++) {
		
		document.write("<td ><img src=\""+days[i]+"\">&nbsp;</td>");
		
	}
	document.write("</tr>");
	columncount = 0;
	etcount = 1;

	eucount = etstartdays1[etmonth-1];

	if ((etleapyear && etmonth < 7) || isUniqueYear(year)) eucount++;
//alert((etleapyear && etmonth < 7) || isUniqueYear());
	//********************************************************************************************

	d = (eucount + y + parseInt(y/4) - parseInt(y/100) + parseInt(y/400) + parseInt((31*m)/12)) % 7

	//********************************************************************************************

	loop = 35;
	if (d == 6 || etmonth == 12) loop = 42

	if (month== -1) month= 11;
	enddate = euenddays[month];
	if (month  == 1 && euleapyear) enddate = 29;
	//alert(enddate+"  "+  month  +" d==="+d+" et month==="+etmonth);

	for (i=0; i<loop; i++) {

		if (columncount == 0 || columncount == 6 || eucount == xday)  {
			prefix = "sr";
			fcolor = "#FF0000";
		}else{
			 prefix="sb";
			fcolor = "#0000FF";
		}

		document.write("<td><table border=0 width=\"100%\" cellspacing=\"0\" cellpadding=\"0\" ><tr><td>");

		if (i >= d  && etcount < 31) {
			document.write("<img src=\"./image/"+prefix+etcount+".gif\">" +"</td ></tr><tr><td align='right'>");
			document.write("<font face=\"arial,helvetica,sans serif\" size=1><font color="+fcolor+">"+eucount+"</font></td></tr>");
			etcount++;
			eucount++;
			if (eucount > enddate) eucount = 1;	

		}else if ((etmonth == 12 && etcount > 30) && ((isNextYearLeapYr && etcount < 37) || (etcount < 36)) ) {
			x = etcount-30;
			document.write("<img src=\"./image/"+prefix+x+".gif\">" +"</td></tr><tr><td align='right'>");
			document.write("<font face=\"arial,helvetica,sans serif\" size=1><font color=color="+fcolor+">"+eucount+"</font></td></tr>");
			etcount++;
			eucount++;
			if (eucount > enddate) eucount = 1;	

		}else{
			document.write("<img src=\"./image/sspacer.gif\"></td></tr><tr><td align='right'></td></tr>");
		}
		
		document.write("</table></td>");  
		columncount++;

		if (columncount > 6) {
			columncount = 0;
			document.write("</tr>");
			if (i < 28) document.write("<tr>");
		}

	}
	document.write("</table>");
//}
function getEthiopianyear(y, m){

	if (m < 8)  ret = y - 8;
	else ret = y - 7;
	return ret;

}
function getEthiopianmonth(mon, d){
	mm = 0;
	if (etstartdays2[mon] <= d) mm = mon + 5;
	else mm = mon + 4;

	if (mm>12) mm = mm - 12;
//alert(mm);
	return mm;
}
function checkleapyear(y){

	if (y%4==0 && y%100>0) return true
	else return false;
}

function isUniqueYear(y){

	if (y%4==0 && y%100==0) return true
	else return false;
}

function checkEtleapyear(y){

	if (y%4==0 ) return true
	else return false;
}


function submitnewdate(ind){


	if (ind == "N") month++;
	else month--;

	if (month == -1) {
		month = 11;
		year--;
	}else if (month == 12) {
		month = 0;
		year++;
	}

	ur = "calender.html?DATE="+year+"-"+month+"-"+etstartdays2[month];
	
	 
	window.location = ur;
	
}

function getInputDate(url){

	if (url.indexOf("DATE") == -1) return getTodaysDate();

	current = false;
	dt = url.substring(url.indexOf("=")+1);
	dd = dt.split("-");
	return (new Date(dd[0], dd[1], dd[2]));
	
}
function getTodaysDate(){
	d = new Date();
	if (d.getDate() < etstartdays2[d.getMonth()]) xm = d.getMonth()-1;
	else xm=d.getMonth();
	
	return (new Date(d.getYear(), xm,  etstartdays2[xm]));
}

