var g_ID = '';
var g_DestID = '';
var g_Title = "";
var g_Year = 0;
var g_Month = 0;
var g_Today = new Array(2000, 1, 1)
var g_RunAtSelect = null

function MounthsCombo(chs)
{
	var m = new Array("ינואר", "פברואר", "מרץ", "אפריל", "מאי", "יוני", "יולי", "אוגוסט", "ספטמבר", "אוקטובר", "נובמבר", "דצמבר")
	buf = "<select dir='rtl' size='1' id='id_mnt' onchange='ChangeMounth()'>";
	for(x in m)
	{
		if (x != chs)
			buf += "<option value="+x+">"+m[x]+"</option>";
		else
			buf += "<option value="+x+" selected>"+m[x]+"</option>";
	}
	buf += "</SELECT>";
	return buf;
}

function YearsCombo(chs)
{
	buf = "<select dir='rtl' size='1' id='id_year' onchange='ChangeYear()'>";
	for(i = 2010; i > 1929; i--)
	{
		if (i != chs)
			buf += "<option value="+i+">"+i+"</option>";
		else
			buf += "<option value="+i+" selected>"+i+"</option>";
	}
	buf += "</SELECT>";
	return buf;
}

function DrawHeader(year, month)
{
	var days = new Array('א', 'ב', 'ג', 'ד', 'ה', 'ו', 'ש')
	var buf = "<table border=1 cellSpacing=0 cellPadding=0><tr align=center bgcolor='#90909f'><td>";
	buf += "<table width=100%  border=0 style='cursor:pointer' bgcolor='#ffffff'>"
	
	buf += "<tr align=center style='cursor:default' bgcolor='#60606f'><td><font color='white'>?</font></td><td colspan=5 ><font color='white'>"+g_Title+"</font></td><td valign=center align=center width=15px onclick='Close()' onmouseover='ChangeClose(true)' onmouseout='ChangeClose()' bgcolor='#f02020' id='closeme'><font color='white'>x</font></td></font></tr>"
	
	if ((year == 1930) && (month == 0))
		before = " style='cursor:default' ";
	else
		before = " onclick='ChangeMounth(-1)' ";
		
	if ((year == 2010) && (month == 11))
		after = " style='cursor:default' ";
	else
		after = " onclick='ChangeMounth(1)' ";
	
	buf += "<tr align=center bgcolor='#90909f'><td width=15px"+before+"><</td><td style='cursor:default' colspan=5>"+YearsCombo(year)+MounthsCombo(month)+"<td width=15px"+after+">></td></tr>"
	buf += "<tr align=center bgcolor='#f0f0ff' style='cursor:default'>";

	for (i in days)
		buf += "<td><b>" +days[i]+ "</b></td>"
	
	buf += "</tr>";
	return buf;
}

function ChangeMounth(mon)
{
	y = 0;
	c = document.getElementById('id_mnt').value
	if (mon == 1)
	{
		if (c < 11) 
			c++
		else
		{
			c = 0;
			y = 1;
		}
	}	
	else if (mon == -1)
	{
		if (c > 0 ) 
			c--
		else
		{
			c = 11;
			y = -1;
		}
	}
	
	g_Year = g_Year + y;
	var date = new Date();
	date.setFullYear(g_Year, c, 1);
	DrawCalenderInside(date)
}

function ChangeYear(year)
{
	c = document.getElementById('id_year').value
	
	var date = new Date();
	date.setFullYear(c, g_Month - 1, 1);
	DrawCalenderInside(date)
}

function daysInMonth(iMonth, iYear)
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function CalenderTable(date)
{
	var year = date.getFullYear()
	var month = date.getMonth()

	var buf = DrawHeader(year, month)

	
	var myDate=new Date();
	myDate.setFullYear(year,month,1);
	var start = myDate.getDay();
	var end = daysInMonth(month, year)
	
	buf += "<tr align=center bgcolor='#f0f0ff'>"
	for (i=0; i < start; i++)
		buf += "<td></td>"

	cnt = start;
	for (i=0; i < end; i++)
	{
		if (cnt > 6)
		{
			buf += "</tr><tr align=center bgcolor='#f0f0ff'>"
			cnt = 0;
		}
		
		if ((month != g_Today[1])||((i+1) != g_Today[2])||(year != g_Today[0]))
			buf += "<td onclick='Select("+i+")' onmouseout='ChangeStatus("+i+", false)' onmouseover='ChangeStatus("+i+", true)'><div id='tbl"+(i)+"'>" + (i+1) + "</div></td>";
		else
			buf += "<td onclick='Select("+i+")' onmouseout='ChangeStatus("+i+", false)' onmouseover='ChangeStatus("+i+", true)' bgcolor='#c0c0cf'><div id='tbl"+(i)+"'>" + (i+1) + "</div></td>";
		cnt++; 		
	}
	for (;cnt < 7; cnt++)
		buf += "<td></td>"
	
	buf += "</tr>";
	return buf;
}

function DrawCalender(id, dest, title, daysAgo, runme)
{
	g_ID = id;
	g_DestID = dest;
	if (title)
		g_Title = title
	else
		g_Title = "תאריכון"
	
	g_RunAtSelect = runme

	var today = new Date()
	g_Today[0] = today.getFullYear()
	g_Today[1] = today.getMonth()
	g_Today[2] = today.getDate()
	
	today.setFullYear(today.getFullYear(), today.getMonth(), today.getDate() - daysAgo)
	DrawCalenderInside(today);
}

function DrawCalenderInside(date)
{
	g_Year = date.getFullYear();
	g_Month = WriteFullNum(date.getMonth() + 1);
	
	
	buf = CalenderTable(date);
	
	buf += "<tr dir=ltr><td id='status' align=center bgcolor='#90909f' style='cursor:default;height:20px' colspan=5></td>";
	buf += "<td onclick='ClearAndExit()' align=center bgcolor='#90909f' style='cursor:hand;height:20px;color=white' colspan=2>Clear</td></tr>";
	
	buf += "</table></td></tr></table>";

	document.getElementById(g_ID).innerHTML = buf;	
	document.getElementById(g_ID).style.position = "absolute";
}

function WriteFullNum(num)
{
	if (num < 10)
		return "0" + num;
	return num;
}

function ChangeStatus(index, isOn)
{
	id = "tbl"+(index)
	if (isOn)
	{
		document.getElementById(id).style.fontWeight  = "700"
		document.getElementById(id).style.color = "blue";
		txtDate = WriteFullNum(index + 1) + "/" + g_Month + "/" + g_Year;
		document.getElementById('status').innerHTML = txtDate;
	}
	else
	{
		document.getElementById(id).style.fontWeight  = "400"
		document.getElementById(id).style.color = "black";
	}
}

function ChangeClose(isOn)
{
	if (isOn)
	{
		document.getElementById('closeme').fontWeight  = "700"
	}
	else
	{
		document.getElementById('closeme').fontWeight  = "400"
	}
}

function Select(index)
{
	var txtDate = WriteFullNum(index + 1) + "/" + g_Month + "/" + g_Year;
	document.getElementById(g_DestID).value = txtDate;
	Close()
	if (g_RunAtSelect !=null)
		RunAfterCalender(g_RunAtSelect);
}

function ClearAndExit()
{
	document.getElementById(g_DestID).value = "";
	Close()
	if (g_RunAtSelect !=null)
		RunAfterCalender(g_RunAtSelect);
}

function Close()
{
	document.getElementById(g_ID).innerHTML = "";	
}

	//var draw = "DrawCalender('div_"+id +"', '"+id +"',"+name+","+days+","+isReturn+")";
	//var cal = "<img valign=bottom style=\"cursor:hand\" src=\"images/cal.gif\" onclick=\""+draw+
