function CVDate_checkValidity()
{
	CVDate_checkDate(groups[this.group]);
}

function CVDate_checkDate(gp)
{
	var nDay = new Number(gp.CVDate_day.value);
	if (gp.CVDate_day.value == "")
	{
		nDay = NaN;
	}
	var nMonth = new Number(gp.CVDate_month.value);
	if (gp.CVDate_month.value == "")
	{
		nMonth = NaN;
	}
	var nYear = new Number(gp.CVDate_year.value);
	if (gp.CVDate_year.value == "")
	{
		nYear = NaN;
	}
	
	if ( nMonth.toString()=="NaN")
	{
		CValidateBase_invalid(gp.CVDate_month, "Month is empty");	
	}
	else if (nMonth < 1 || nMonth > 12)
	{
		CValidateBase_invalid(gp.CVDate_month, "Month is\nout of range");
	}
	else
	{
		CValidateBase_valid(gp.CVDate_month);
	}
	
	var nMaxDay = 31;

	if (nMonth==4 || nMonth==6 || nMonth==9 ||nMonth==11)
	{
			nMaxDay = 30;
	}
	else if (nMonth==2)
	{
/*
		if (nYear % 4)
		{	
			nMaxDay = 28;
		}
		else
		{
			nMaxDay = 29;
		}
*/
		if (nYear % 100 == 0)
		{
			if (nYear % 400 == 0)
				nMaxDay = 29;
			else
				nMaxDay = 28;
		}
		else if (nYear % 4 == 0)
			nMaxDay = 29;
		else
			nMaxDay = 28;
	}
		
	if (nDay.toString()=="NaN")	
	{
		CValidateBase_invalid(gp.CVDate_day, "Day is empty");
	}
	else if (nDay < 1 || nDay > nMaxDay)
	{
		CValidateBase_invalid(gp.CVDate_day, "Day is out of range");
	}
	else
	{
		CValidateBase_valid(gp.CVDate_day);
	}
	
	if (nYear.toString()=="NaN")
	{
		CValidateBase_invalid(gp.CVDate_year, "Year is empty");
	}
	
	else if (event.type != "keyup")
	{
		if (nYear >= 0 && nYear <= 9)
		{
			gp.CVDate_year.value="200" + nYear;
		}
		if (nYear >= 10 && nYear <= 69)
		{
			gp.CVDate_year.value="20" + nYear;
		}
		if (nYear >=70 && nYear <= 99)
		{
			gp.CVDate_year.value="19" + nYear;
		}		

		CValidateBase_valid(gp.CVDate_year);
	}
}

function CVDate_onBindingComplete()
{
	var g;
		
	for (g in groups)
	{
		var gp = groups[g];
		if (gp.CVDate)
		{
			CVDate_SetUpGroup(gp);
		}
	}
}

function CVDate_SetUpGroup(gp)
{		
	if (!gp.CVDate_day)
	{
		alert("No day object in date!");
		return;
	}
	if (!gp.CVDate_month)
	{
		alert("No month object in date!");
		return;
	}
	if (!gp.CVDate_year)
	{
		alert("No year object in date!");
		return;
	}

	var autoDate = gp.autoDate
	if (autoDate == true || autoDate == "true")
	{
		d = new Date();
	
		gp.CVDate_day.value = d.getDate();
		gp.CVDate_month.value = d.getMonth();
		gp.CVDate_year.value = d.getYear();
	}
	else 
	{
		var slash1 = autoDate.indexOf("/", 0);
		if (slash1 > 0)
		{
			var slash2 = autoDate.indexOf("/", slash1 + 1);
			if (slash2 > 0)
			{
				gp.CVDate_day.value = autoDate.slice(0, slash1);
				gp.CVDate_month.value = autoDate.slice(slash1 + 1, slash2);
				gp.CVDate_year.value = autoDate.slice(slash2 + 1);
			}
		}
	}
	
	CVDate_checkDate(gp);
}

function CVDate(el)
{
	CValidateBase(el);

	if (!el.group)
	{
		alert("Date element does not belong to a group!");
		return;
	}
	
	var gp=groups[el.group];
	gp.CVDate=true;
	
	switch (el.date)
	{
		case "day":
			gp.CVDate_day=el;
			break;
		case "month":
			gp.CVDate_month=el;
			break;
		case "year":
			gp.CVDate_year=el;
			break;
		default:
			alert("incorrect date property value" + el.date);
			return;
			break;
	}
	
	if (el.autoDate)
	{
		gp.autoDate = el.autoDate;	
	}
	
	el.checkValidity = CVDate_checkValidity;
	el.numeric=true;
	el.noenter=false;
}

bindings.vdate=new CBinding(CVDate);
bindings.vdate.onBindingComplete=CVDate_onBindingComplete;
