var DateInput;
function ChangeBox(y,m)
{
	
	if (isNaN(parseInt(y))||isNaN(parseInt(m)))
	{
	   y=new Date().getFullYear();
	   YearBox.value=y;
	   m=new Date().getMonth()+1;
	}
	for(i=0;i<DayArray.length;i++)
		eval(DayArray[i]).innerHTML="га";
	for(i=0;i<new Date(y,m,0).getDate();i++)
		eval(DayArray[i+new Date(y,m,1).getDay()]).innerHTML=i+1;
	for(i=0;i<DayArray.length;i++)
	{
		DayArray[i].slice(5,6)==0?eval(DayArray[i]).innerHTML="<nobr style=\"color:red\">"+eval(DayArray[i]).innerText+"</nobr>":null;
		eval(DayArray[i]).innerText==new Date().getDate()&& y==new Date().getYear()&& m==(new Date().getMonth()+1)?eval(DayArray[i]).innerHTML="<nobr style=\"color:blue\">"+eval(DayArray[i]).innerText+"</nobr>":null;
	}
	
	ChangeInput();
}
function ChangHer(TheDate)
{
	
	var dateobject=new Date();
	
	if (isSpace(TheDate)||(!IsDate(TheDate)))
		TheDate=dateobject.getFullYear()+"-"+(dateobject.getMonth()+1)+"-"+dateobject.getDate() +" "+dateobject.getHours()+":"+dateobject.getMinutes();
	
	YearBox.value=GetDate(TheDate,"year");
	MonthBox.value=GetDate(TheDate,"month");
	DayBox.value=GetDate(TheDate,"day");
	HourBox.value=GetDate(TheDate,"hour");
	MinuteBox.value=GetDate(TheDate,"minute");
	ChangeBox(GetDate(TheDate,"year"),GetDate(TheDate,"month"));
	for(i=0;i<DayArray.length;i++)
	{
		eval(DayArray[i]).className ="";
		eval(DayArray[i]).innerText==DayBox.value?eval(DayArray[i]).className ="EFCalendar":"";
	}
}
function DateChange(step)
{
	var thenum;
	if(YearBox.className=='NoBorderB')
	{
		thenum=parseInt(YearBox.value);
		if(isNaN(thenum))
			thenum=new Date().getFullYear();
		
		thenum = thenum + step ;
		YearBox.value = thenum;
		ChangeBox(thenum,MonthBox.value);
	}
	if(MonthBox.className=='NoBorderB')
	{
		thenum=parseInt(MonthBox.value);
		if(isNaN(thenum))
			thenum=new Date().getMonth();
		
		thenum = thenum + step ;
		if(thenum>12)
			thenum=1;
		if(thenum<1)
			thenum=12;
		MonthBox.value = thenum;
		ChangeBox(YearBox.value,thenum);
	}
	if(DayBox.className=='NoBorderB')
	{
		thenum=parseInt(DayBox.value);
		if(isNaN(thenum))
			thenum=new Date().getDay();
		
		thenum = thenum + step ;
		if(thenum>31)
			thenum=0;
		if(thenum<0)
			thenum=31;
		DayBox.value = thenum;
		ChangeDate(thenum);
	}
	if(HourBox.className=='NoBorderB')
	{
		thenum=parseInt(HourBox.value);
		if(isNaN(thenum))
			thenum=new Date().getHours();
		
		thenum = thenum + step ;
		if(thenum>23)
			thenum=0;
		if(thenum<0)
			thenum=23;
		HourBox.value = thenum;
	}
	if(MinuteBox.className=='NoBorderB')
	{
		thenum=parseInt(MinuteBox.value);
		if(isNaN(thenum))
			thenum=new Date().getMinutes();
		
		thenum = thenum + step ;
		if(thenum>59)
			thenum=0;
		if(thenum<0)
			thenum=59;
		MinuteBox.value = thenum;
	}
	ChangeInput()
}


function SelectDate(TheObject)
{
	var thedate;
	DayBox.value=TheObject.innerText;
	if(TheObject.innerText!="га" && TheObject.innerText!=" " && DateInput!="")
	{
		for(i=0;i<new Date(YearBox.value,MonthBox.value-1,0).getDate();i++)
			    eval(DayArray[i+new Date(YearBox.value,MonthBox.value-1,1).getDay()]).className = "";
		TheObject.className = "EFCalendar"
	}
	ChangeInput();
	return true;
}
function ChangeInput()
{	var thedate;
	thedate = YearBox.value + "-" + MonthBox.value + "-" +  DayBox.value + " " 
	if(HourBox.value!="")
		thedate = thedate + HourBox.value + ":" +  MinuteBox.value ;
	DateInput.value=thedate;		
}
function ChangeDate(TheDay)
{
	var thedate;
	for(i=0;i<new Date(YearBox.value,MonthBox.value-1,0).getDate();i++)
	{
		eval(DayArray[i]).className ="";
		eval(DayArray[i]).innerText==TheDay?eval(DayArray[i]).className ="EFCalendar":"";

	}
		
	if(TheObject.innerText!="га" && TheObject.innerText!=" " && DateInput!="")
	{
		thedate = YearBox.value + "-" + MonthBox.value + "-" +  TheDay + " " +  HourBox.value + ":" +  MinuteBox.value ;
		DateInput.value=thedate;
	}
	return true;
}
function SelectClose(TheObject)
{
        
	SelectDate(TheObject);
	SwitchCalendar();
	DateInput.focus();
	return true;
}
function SwitchCalendar()
{	
	if(_Calendar.style.visibility!="hidden")
	{	
		_Calendar.style.visibility = "hidden";
		_Calendar.parentElement.style.visibility = "hidden";
	}	
	else
	{	
		_Calendar.style.visibility = "visible";
		_Calendar.parentElement.style.visibility = "visible";
		
	}
}

function Hide()
{	event.srcElement.id
	if(event.srcElement.id=="_mark")
	  {	
	  	_Calendar.style.visibility = "hidden";
	  	_Calendar.parentElement.style.visibility = "hidden";
	  	DateInput.focus();
	  }	
	 
}

function ShowCal()
{
	var thedate;
	DateInput=document.getElementById(event.srcElement.linkobj);
	thedate = DateInput.value;
	ChangHer(thedate);
	_Calendar.style.width=_CalendarTable.clientWidth + 2
	_Calendar.style.height=_CalendarTable.clientHeight + 2
	_Calendar.parentElement.style.width = _Calendar.document.body.clientWidth + _Calendar.document.body.scrollLeft;
	_Calendar.parentElement.style.height  =_Calendar.document.body.clientHeight +  _Calendar.document.body.scrollTop;
	
	var offSrc = DateInput.offsetParent;;
	var offX =  GetoffX(DateInput)
	var offY =  GetoffY(DateInput)
	
	if (offX > _Calendar.parentElement.clientWidth - _Calendar.clientWidth)
		_Calendar.style.left =_Calendar.parentElement.clientWidth - _Calendar.clientWidth-2;
	else
		_Calendar.style.left =offX;
		
	if (offY > _Calendar.parentElement.clientHeight - _Calendar.clientHeight)
		_Calendar.style.top =offY - _Calendar.clientHeight+4;
	else
		_Calendar.style.top =offY  + DateInput.clientHeight+4;
	
	SwitchCalendar();
}
