var C_WinCal = null, CActiveElem;

function Date2Str(d)
{
	var m = d.getMonth() + 1;
	return FormatStr(d.getDate(), 2) + "." + FormatStr(m, 2) + "." + d.getFullYear();
}
function FormatStr(s, d)
{
	var t, a, i;
	t = new String (s);
	a = t.length;
	if (a < d)
	{
		for (i=0; i<d-a; i++)
		{
			t = "0" + t;
		}
	}
	return t;
}
function Calendar(e, path)
{
	var t, l, d;
	if (C_WinCal != null && !C_WinCal.closed)
	{
		if (C_ActiveElem == e)
		{
			C_WinCal.focus();
			return;
		}
		else
		{
			C_WinCal.close();
		}
	}
	d=VerifyDate(e.value);
	if (d == 0)
	{
		d = Date2Str(new Date());
	}
	e.value = d;
	C_ActiveElem = e;
	t = (screen.height - 260) / 2;
	l = (screen.width - 220) / 2;
	C_WinCal = window.open(path + "scripts/calendar/calendar_popup.html", "win_cal", 
				"menubar=no,locationbar=0,status=no,height=240,width=210,resizeable=no,dependent=yes"
				+ ",top=" + t + ",left=" + l);
}
function VerifyDate(checkdate)
{
	var allowed="0123456789.";
	var sTest;
	for (var i=0; i<checkdate.length; i++)
	{
		sTest = checkdate.substring(i, i+1);
		if (allowed.indexOf(sTest) < 0)
		{
			return 0;
		}
	}
	var d, m, y;
	var splitdate = checkdate.split('.');
	if (splitdate.length == 3)
	{
		d = splitdate[0];
		m = splitdate[1];
		y = splitdate[2];
		if (d.length > 2 || m.length > 2 || y.length == 3)
		{
			return 0;
		}
	}
	else if (splitdate.length == 1)
	{
		if (checkdate.length == 6 || checkdate.length == 8)
		{
			d = checkdate.substring(0, 2);
			m = checkdate.substring(2, 4);
			y = checkdate.substring(4);
		}
		else
		{
			return 0;
		}
	}
	else
	{
		return 0;
	}
	var intd, intm, inty;
	intd = parseInt(d, 10);
	intm = parseInt(m, 10);
	inty = parseInt(y, 10);
	if (inty <= 98)
	{
		inty += 2000;
	}
	else if (inty == 99)
	{
		inty += 1900;
	}
	var dobj = new Date(Date.parse(intm + "/" + intd + "/" + inty));
	if (intd + "." + intm + "." + inty != dobj.getDate() + "." + (dobj.getMonth() + 1) + "." + dobj.getFullYear())
	{
		return 0;
	}
	if (d.length < 2) d = "0" + d;
	if (m.length < 2) m = "0" + m;
	checkdate = d + "." + m + "." + inty.toString(10);
	return checkdate;
}
function NewDate(d)
{
	C_ActiveElem.value = d;
}
function GetDate()
{
	return C_ActiveElem.value;
}

