// показать объект в определенной позиции
function cmShow(name,l,t){
	name=name.toString();
	if(document.getElementById(name)&&document.getElementById(name).style){
		if(!document.getElementById(name).style.left)
			document.getElementById(name).style.left=l;
		if(!document.getElementById(name).style.top)
			document.getElementById(name).style.top=t;
		document.getElementById(name).style.display="block";
	}
}
// спрятать объект
function cmHide(name){
	if(document.getElementById(name)&&document.getElementById(name).style)
		document.getElementById(name).style.display="none";
}
// окно выбора даты/времени
function frmOpenDateTimePopup(code,mode,y,m,d,hour,minute,second){
	var _value,_date,_time,curdate;
	if((y%4==3)&&m==2&&d==29) d=28; // 29 февраля заменяем на 28 если надо
	if(!y&&!m&&!d&&document.getElementById(code).value&&(mode==0||mode==1)){
		if(mode==0){
			_value=document.getElementById(code).value.split(" ");
			_date=_value[0].split(".");
		}else if(mode==1){
			_date=document.getElementById(code).value.split(".");
		}
		y=_date[0],m=_date[1],d=_date[2];
	}
	if(!hour&&!minute&&!second&&document.getElementById(code).value&&(mode==0||mode==2)){
		if(mode==0){
			_value=document.getElementById(code).value.split(" ");
			_time=_value[1].split(":");
		}else if(mode==2){
			_time=document.getElementById(code).value.split(":");
		}
		hour=_time[0],minute=_time[1],second=_time[2];
	}
	if((y||m||d||hour||minute||second)&&mode==0) curdate=new Date(y,m-1,d,hour,minute,second);
	else if((y||m||d)&&mode==1) curdate=new Date(y,m-1,d);
	else if((hour||minute||second)&&mode==2) curdate=new Date(0,0,0,hour,minute,second);
	else curdate=new Date();

	y=curdate.getFullYear();
	m=curdate.getMonth();
	d=curdate.getDate();
	hour=curdate.getHours();
	minute=curdate.getMinutes();
	second=curdate.getSeconds();
	var curmonth=new Date(y,m,1);
	var wd=curmonth.getDay(); if(!wd) wd=7;
	var dayCount;
	if(m==0||m==2||m==4||m==6||m==7||m==9||m==11) dayCount=31;
	else if(m==1&&(y%4==3)) dayCount=28;
	else if(m==1) dayCount=29;
	else dayCount=30;
	var lastY=y,lastM=m-1;
	var nextY=y,nextM=m+1;
	if(m==0){ lastY=y-1; lastM=11; }
	if(m==11){ nextY=y+1; nextM=0; }
	var content="";
	var i=0,k=1;

	content+="<div style='text-align: right; padding-right: 5px;'><b onclick=\"frmCloseDateTimePopup('"+code+"');\" class='hand'>x</b></div>";
	content+="<table border='0' onselectstart=\"return false;\" class='calendar_table'><tr>";

	if(mode==0||mode==1){
		content+="<td><table border='0' style='text-align: center;'>";
		// заголовок
		content+="<tr>";
		content+="<th width='20px' onclick=\"dtDateChange('"+code+"','"+mode+"','"+(y-1)+"','"+(m+1)+"','"+d+"');\" class='hand'>&laquo;</th>";
		content+="<th width='20px' onclick=\"dtDateChange('"+code+"','"+mode+"','"+lastY+"','"+(lastM+1)+"','"+d+"');\" class='hand'>&larr;</th>";
		content+="<th width='60px' colspan='3'>"+dtMonths[m]+" "+y+"</th>";
		content+="<th width='20px' onclick=\"dtDateChange('"+code+"','"+mode+"','"+nextY+"','"+(nextM+1)+"','"+d+"');\" class='hand'>&rarr;</th>";
		content+="<th width='20px' onclick=\"dtDateChange('"+code+"','"+mode+"','"+(y+1)+"','"+(m+1)+"','"+d+"');\" class='hand'>&raquo;</th>";
		content+="</tr>";
		// дни недели &larr; mes
		content+="<tr><th width='20px'>Пн</th><th width='20px'>Вт</th><th width='20px'>Ср</th><th width='20px'>Чт</th><th width='20px'>Пт</th><th width='20px'>Сб</th><th width='20px'>Вс</th></tr>";
		// контент
		content+="<tr>";
		while(k<=dayCount||(i%7)!=0){
			if(i&&(i%7)==0&&k<=dayCount)
				content+="</tr><tr>";
			if((i+1)>=wd&&k<=dayCount){
				if(k==d) content+="<td onclick=\"frmSetDateTime('"+y+"."+(m+1)+"."+k+"','"+mode+"','"+code+"'); frmCloseDateTimePopup('"+code+"');\" class='current'>"+k+"</td>";
				else content+="<td onclick=\"frmSetDateTime('"+y+"."+(m+1)+"."+k+"','"+mode+"','"+code+"'); frmCloseDateTimePopup('"+code+"');\" class='hand'>"+k+"</td>";
				k++;
			}else{
				content+="<td>&nbsp;</td>";
			}
			i++;
		}
		// закрыть
		content+="</tr></table></td>";
	}
	if(mode==0||mode==2){
		content+="<td valign='top'><table border='0' style='text-align: align;'>";
		// заголовок
		content+="<tr><th colspan='4'>Время</th></tr>";
		content+="<tr><th width='90px' align='right'>Часы (<span id='hour_value_"+code+"'>"+hour+"</span>):</th><th width='5px' onclick=\"dtTimeDown('"+code+"','hour_',23);\" class='hand'>&laquo;</th><td width='60px' style='padding: 0; position: relative;' onmousedown=\"dtTimeChange('"+code+"','hour_',23);\" onmousemove=\"dtTimeChanging('"+code+"','hour_',23);\" onmouseup=\"dtTimeChanged();\" onmouseout=\"dtTimeChanged();\" class='hand'><div id='hour_"+code+"' style='background-color: #8e1b17; width: "+Math.ceil(hour*60/24)+"px;'></div></td><th width='5px' onclick=\"dtTimeUp('"+code+"','hour_',23);\" class='hand'>&raquo;</th></tr>";
		content+="<tr><th width='90px' align='right'>Минуты (<span id='minute_value_"+code+"'>"+minute+"</span>):</th><th width='5px' onclick=\"dtTimeDown('"+code+"','minute_',59);\" class='hand'>&laquo;</th><td width='60px' style='padding: 0; position: relative;' onmousedown=\"dtTimeChange('"+code+"','minute_',59);\" onmousemove=\"dtTimeChanging('"+code+"','minute_',59);\" onmouseup=\"dtTimeChanged();\" onmouseout=\"dtTimeChanged();\" class='hand'><div id='minute_"+code+"' style='background-color: #8e1b17; width: "+minute+"px;'></div></td><th width='5px' onclick=\"dtTimeUp('"+code+"','minute_',59);\" class='hand'>&raquo;</th></tr>";
		content+="<tr><th width='90px' align='right'>Секунды (<span id='second_value_"+code+"'>"+second+"</span>):</th><th width='5px' onclick=\"dtTimeDown('"+code+"','second_',59);\" class='hand'>&laquo;</th><td width='60px' style='padding: 0; position: relative;' onmousedown=\"dtTimeChange('"+code+"','second_',59);\" onmousemove=\"dtTimeChanging('"+code+"','second_',59);\" onmouseup=\"dtTimeChanged();\" onmouseout=\"dtTimeChanged();\" class='hand'><div id='second_"+code+"' style='background-color: #8e1b17; width: "+second+"px;'></div></td><th width='5px' onclick=\"dtTimeUp('"+code+"','second_',59);\" class='hand'>&raquo;</th></tr>";
		content+="</table></td>";
	}
	//content+="</tr><tr><th colspan='2'><span onclick=\"frmSetDateTime('"+y+"."+(m+1)+"."+d+"','"+mode+"','"+code+"'); frmCloseDateTimePopup('"+code+"');\" class='hand'>Установить</span> | <span onclick=\"frmCloseDateTimePopup('"+code+"');\" class='hand'>Закрыть</span></th></tr></table>";
	content+="</tr></table>";
	// вывод
	//var dateName="date_"+code;
	var dateName="date_search_event_date";
	cmShow(dateName,0,0);
	document.getElementById(dateName).innerHTML=content;
	event.returnValue=false;
}
// закрытие окна выбора даты
function frmCloseDateTimePopup(code){
	//var dateName="date_"+code;
	var dateName="date_search_event_date";
	cmHide(dateName);
}

function frmSetDateTime(d,mode,code){
	if(mode==0||mode==2){
		hour=document.getElementById("hour_value_"+code).innerHTML;
		minute=document.getElementById("minute_value_"+code).innerHTML;
		second=document.getElementById("second_value_"+code).innerHTML;
	}
	if(mode==1) document.getElementById(code).value=d;
	else if(mode==2) document.getElementById(code).value=hour+":"+minute+":"+second;
	else document.getElementById(code).value=d+" "+hour+":"+minute+":"+second;
}

// месяцы
var dtMonths=new Array("Январь","Февраль","Март","Апрель","Май","Июнь","Июль","Август","Сентябрь","Октябрь","Ноябрь","Декабрь");
// изменение времени (мышка нажата)
var dtTimeCanChange=false;

function dtDateChange(code,mode,y,m,d){
	if(mode==0)
		frmOpenDateTimePopup(code,mode,y,m,d,document.getElementById("hour_value_"+code).innerHTML,document.getElementById("minute_value_"+code).innerHTML,document.getElementById("second_value_"+code).innerHTML);
	else if(mode==1)
		frmOpenDateTimePopup(code,mode,y,m,d);
}
function dtTimeChange(code,prefix,maxValue){
	if(event.x) document.getElementById(prefix+code).style.width=event.x+1;
	else document.getElementById(prefix+code).style.width=event.x;
	document.getElementById(prefix+"value_"+code).innerHTML=parseInt(event.x*(maxValue+1)/60);
	dtTimeCanChange=true;
}
function dtTimeChanging(code,prefix,maxValue){
	if(dtTimeCanChange){
		dtTimeChange(code,prefix,maxValue);
	}
}
function dtTimeChanged(){
	dtTimeCanChange=false;
}
function dtTimeDown(code,prefix,maxValue){
	if(parseInt(document.getElementById(prefix+"value_"+code).innerHTML)>0){
		document.getElementById(prefix+"value_"+code).innerHTML=parseInt(document.getElementById(prefix+"value_"+code).innerHTML)-1;
		document.getElementById(prefix+code).style.width=parseInt(parseInt(document.getElementById(prefix+"value_"+code).innerHTML)*60/(maxValue+1));
	}
}
function dtTimeUp(code,prefix,maxValue){
	if(parseInt(document.getElementById(prefix+"value_"+code).innerHTML)<maxValue){
		document.getElementById(prefix+"value_"+code).innerHTML=parseInt(document.getElementById(prefix+"value_"+code).innerHTML)+1;
		document.getElementById(prefix+code).style.width=parseInt(parseInt(document.getElementById(prefix+"value_"+code).innerHTML)*60/(maxValue+1));
	}
}

