//	Селектор даты
//	Функция date_selector формирует в теле документа hidden-поле с указанным именем и 3 селектора (день,месяц,год)
var month_names=['','январь','февраль','март','апрель','май','июнь','июль','август','сентябрь','октябрь','ноябрь','декабрь']
var month_namesr=['','января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря']

function CurrentDate () {
	var d=new Date()
	return d.getDate()+'-'+(d.getMonth()+1)+'-'+d.getFullYear()
}
function CurrentYear () {
	return new Date().getFullYear()
}
function set_date_selector(name) {
	if (document.all[name+'_y'].value) {
		if (this.document.all[name].date_format == 'r') {
			this.document.all[name].value=document.all[name+'_d'].value+'.'+document.all[name+'_m'].value+'.'+document.all[name+'_y'].value
		} else {
			this.document.all[name].value=document.all[name+'_y'].value+'-'+document.all[name+'_m'].value+'-'+document.all[name+'_d'].value
		}
	} else {
		this.document.all[name].value=''
	}
}
function fill_day_selector(name) {
	d=document.all[name+'_d']
	dv=d.value
	d.options.length=0
	y=document.all[name+'_y']
	m=document.all[name+'_m']
	if (y.value == 0) {
		m.value=''
	} else {
		 if (m.value == 0) {
			m.value=1
		 }
		
	}
	if (m.value) {
		dim=new Date(new Date(y.value,m.value,1) - 86400000).getDate()
		for (i=1;i<=dim;i++) {
			d.options.add(new Option(i,i))
		}
		if (dv) {d.value=dv>dim ? dim : dv}
	}
	else {
		d.value=0;
	}
	set_date_selector(name)
}

function date_selector(name,dy,dm,dd) {
	var p1=name.replace(/\s/g,'').split(';')
	name=p1[0]
	var s=p1[1]
	var f=p1[2]
	var r=p1[3]
 	var cd=new Date()
	var res='<INPUT type="hidden"'+(f?(' date_format="'+f+'"'):'')+' name="'+name+'" id="'+name+'">'+
		"<SELECT name='"+name+"_d' id='"+name+"_d' onchange='set_date_selector(\""+name+"\")'>"+
		"</SELECT>"+
		'<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector(\''+name+'\')">'+
		'<OPTION value="0">-';

	document.writeln('<INPUT type="hidden"'+(f?(' date_format="'+f+'"'):'')+' name="'+name+'" id="'+name+'">')
	document.writeln("<SELECT name='"+name+"_d' id='"+name+"_d' onchange='set_date_selector(\""+name+"\")'>")
	document.writeln("</SELECT>");
	document.writeln('<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector(\''+name+'\')">')
	document.writeln('<OPTION value="0">-')
	for(x=1;x<13;x++) {
		res+="<OPTION value='"+x+"'>"+(s?month_namesr[x]:x)
		document.writeln("<OPTION value='"+x+"'>"+(s?month_namesr[x]:x));
	}
	res+="</SELECT>"
	res+="<SELECT name='"+name+"_y' id='"+name+"_y' onchange='fill_day_selector(\""+name+"\")'>"
	res+='<OPTION value="0">-'

	document.writeln("</SELECT>");
	document.writeln("<SELECT name='"+name+"_y' id='"+name+"_y' onchange='fill_day_selector(\""+name+"\")'>")
	document.writeln('<OPTION value="0">-')
	var cy=CurrentYear();
	for(x=1990;x<=(cy+5);x++) {
		res+="<OPTION value='"+x+"'>"+x
		document.writeln("<OPTION value='"+x+"'>"+x);
	}
	res+="</SELECT>"
	document.writeln("</SELECT>");
	if (dy<0) {
		cd.setDate(cd.getDate()+dy)
		dy = cd.getFullYear()
	}
	document.all[name+"_m"].value = (dm || (cd.getMonth()+1))
	document.all[name+"_y"].value = (dy || cd.getFullYear())
	fill_day_selector(name)
	document.all[name+"_d"].value = (dd || cd.getDate())
	set_date_selector(name)
}

function month_selector(name,dy,dm) {
	var p1=name.replace(/\s/g,'').split(';')
	name=p1[0]
	var s=p1[1]
 	var cd=new Date()
	var res="<SELECT name='"+name+"_m' id='"+name+"_m'>"
	res+="<OPTION value='0'>-"
	document.writeln("<SELECT name='"+name+"_m' id='"+name+"_m'>")
	document.writeln('<OPTION value="0">-')
	for(x=1;x<13;x++) {
		res+="<OPTION value='"+x+"'>"+(s?month_names[x]:x)
		document.writeln("<OPTION value='"+x+"'>"+(s?month_names[x]:x));
	}
	res+="</SELECT>"
	res+="<SELECT name='"+name+"_y' id='"+name+"_y'>"
	res+='<OPTION value="0">-'
	document.writeln("</SELECT>");
	document.writeln("<SELECT name='"+name+"_y' id='"+name+"_y'>")
	document.writeln('<OPTION value="0">-')
	var cy=CurrentYear();
	for(x=1990;x<=(cy+5);x++) {
		res+="<OPTION value='"+x+"'>"+x
		document.writeln("<OPTION value='"+x+"'>"+x);
	}
	res+="</SELECT>"
	document.writeln("</SELECT>");
	document.all[name+"_m"].value = (dm || (cd.getMonth()+1))
	document.all[name+"_y"].value = (dy || cd.getFullYear())
}

function year_selector(name,dy) {
	var p1=name.replace(/\s/g,'').split(';')
	name=p1[0]
	var s=p1[1]
 	var cd=new Date()

	var res="<SELECT name='"+name+"' id='"+name+"'>"
	res+='<OPTION value="0">-'
	document.writeln("</SELECT>");
	document.writeln("<SELECT name='"+name+"' id='"+name+"'>")
	document.writeln('<OPTION value="0">-')
	var cy=CurrentYear();
	for(x=1990;x<=(cy+5);x++) {
		res+="<OPTION value='"+x+"'>"+x
		document.writeln("<OPTION value='"+x+"'>"+x);
	}
	res+="</SELECT>"
	document.writeln("</SELECT>");
	document.all[name].value = (dy || cd.getFullYear())
}

function day_month_selector(name,dd,dm) {
	var p1=name.replace(/\s/g,'').split(';')
	name=p1[0]
	var s=p1[1]
 	var cd=new Date()
	var res='';
	res+="<SELECT name='"+name+"_d' id='"+name+"_d'>"
	document.writeln("<SELECT name='"+name+"_d' id='"+name+"_d'>")
	res+="</SELECT>"
	document.writeln("</SELECT>");
	res+='<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector_1(\''+name+'\')">'
	res+="<OPTION value='0'>-"
	document.writeln('<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector_1(\''+name+'\')">')
	document.writeln('<OPTION value="0">-')
	for(x=1;x<13;x++) {
		res+="<OPTION value='"+x+"'>"+(s?month_names[x]:x)
		document.writeln("<OPTION value='"+x+"'>"+(s?month_names[x]:x));
	}
	res+="</SELECT>"
	document.writeln("</SELECT>");

	document.all[name+"_m"].value = (dm || (cd.getMonth()+1))
	document.all[name+"_d"].value = (dd || cd.getDate())
}

function fill_day_selector_1(name) {
	d=document.all[name+'_d']
	dv=d.value
	d.options.length=0
	m=document.all[name+'_m']
	if (m.value) {
		dim=new Date(new Date(2000,m.value,1) - 86400000).getDate()
		d.options.add(new Option('-',0))                
		for (i=1;i<=dim;i++) {
			d.options.add(new Option(i,i))
		}

		if (dv) {d.value=dv>dim ? dim : dv}
	}
	else {
		d.value=0;
	}
}

function date_selector_1(name,dy,dm,dd) {
	var p1=name.replace(/\s/g,'').split(';')
	name=p1[0]
	var s=p1[1]
	var f=p1[2]
	var r=p1[3]
 	var cd=new Date()
	var res='<INPUT type="hidden"'+(f?(' date_format="'+f+'"'):'')+' name="'+name+'" id="'+name+'">'+
		"<SELECT name='"+name+"_d' id='"+name+"_d' onchange='set_date_selector(\""+name+"\")'>"+
		"</SELECT>"+
		'<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector(\''+name+'\')">'+
		'<OPTION value="0">-';

	document.writeln('<INPUT type="hidden"'+(f?(' date_format="'+f+'"'):'')+' name="'+name+'" id="'+name+'">')
	document.writeln("<SELECT name='"+name+"_d' id='"+name+"_d' onchange='set_date_selector(\""+name+"\")'>")
	document.writeln("</SELECT>");
	document.writeln('<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector(\''+name+'\')">')
	document.writeln('<OPTION value="0">-')
	for(x=1;x<13;x++) {
		res+="<OPTION value='"+x+"'>"+(s?month_namesr[x]:x)
		document.writeln("<OPTION value='"+x+"'>"+(s?month_namesr[x]:x));
	}
	res+="</SELECT>"
	res+="<SELECT name='"+name+"_y' id='"+name+"_y' onchange='fill_day_selector(\""+name+"\")'>"
	res+='<OPTION value="0">-'

	document.writeln("</SELECT>");
	document.writeln("<SELECT name='"+name+"_y' id='"+name+"_y' onchange='fill_day_selector(\""+name+"\")'>")
	document.writeln('<OPTION value="0">-')
	var cy=CurrentYear();
	for(x=1850;x<=(cy+5);x++) {
		res+="<OPTION value='"+x+"'>"+x
		document.writeln("<OPTION value='"+x+"'>"+x);
	}
	res+="</SELECT>"
	document.writeln("</SELECT>");
	if (dy<0) {
		cd.setDate(cd.getDate()+dy)
		dy = cd.getFullYear()
	}
	document.all[name+"_m"].value = (dm || (cd.getMonth()+1))
	document.all[name+"_y"].value = (dy || cd.getFullYear())
	fill_day_selector(name)
	document.all[name+"_d"].value = (dd || cd.getDate())
	set_date_selector(name)
}

function year_selector_1(name,dy) {
	var p1=name.replace(/\s/g,'').split(';')
	name=p1[0]
	var s=p1[1]
 	var cd=new Date()

	var res="<SELECT name='"+name+"' id='"+name+"'>"
	res+='<OPTION value="0">-'
	document.writeln("</SELECT>");
	document.writeln("<SELECT name='"+name+"' id='"+name+"'>")
	document.writeln('<OPTION value="0">-')
	var cy=CurrentYear();
	for(x=cy;x>=1850;x--) {
		res+="<OPTION value='"+x+"'>"+x
		document.writeln("<OPTION value='"+x+"'>"+x);
	}
	res+="</SELECT>"
	document.writeln("</SELECT>");
	document.all[name].value = (dy || cd.getFullYear())
}

function date_selector_2(name,dy,dm,dd) {
	var p1=name.replace(/\s/g,'').split(';')
	name=p1[0]
	var s=p1[1]
	var f=p1[2]
	var r=p1[3]
 	var cd=new Date()
	var res='<INPUT type="hidden"'+(f?(' date_format="'+f+'"'):'')+' name="'+name+'" id="'+name+'">'+
		"<SELECT name='"+name+"_d' id='"+name+"_d' onchange='set_date_selector(\""+name+"\")'>"+
		"</SELECT>"+
		'<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector(\''+name+'\')">'+
		'<OPTION value="0">-';

	document.writeln('<INPUT type="hidden"'+(f?(' date_format="'+f+'"'):'')+' name="'+name+'" id="'+name+'">')
	document.writeln("<SELECT name='"+name+"_d' id='"+name+"_d' onchange='set_date_selector(\""+name+"\")'>")
	document.writeln("</SELECT>");
	document.writeln('<SELECT name="'+name+'_m" id="'+name+'_m" onchange="fill_day_selector(\''+name+'\')">')
	document.writeln('<OPTION value="0">-')
	for(x=1;x<13;x++) {
		res+="<OPTION value='"+x+"'>"+(s?month_namesr[x]:x)
		document.writeln("<OPTION value='"+x+"'>"+(s?month_namesr[x]:x));
	}
	res+="</SELECT>"
	res+="<SELECT name='"+name+"_y' id='"+name+"_y' onchange='fill_day_selector(\""+name+"\")'>"
	res+='<OPTION value="0">-'

	document.writeln("</SELECT>");
	document.writeln("<SELECT name='"+name+"_y' id='"+name+"_y' onchange='fill_day_selector(\""+name+"\")'>")
	document.writeln('<OPTION value="0">-')
	var cy=CurrentYear();
	for(x=1920;x<=(cy);x++) {
		res+="<OPTION value='"+x+"'>"+x
		document.writeln("<OPTION value='"+x+"'>"+x);
	}
	res+="</SELECT>"
	document.writeln("</SELECT>");
	if (dy<0) {
		cd.setDate(cd.getDate()+dy)
		dy = cd.getFullYear()
	}
	document.all[name+"_m"].value = (dm || (cd.getMonth()+1))
	document.all[name+"_y"].value = (dy || cd.getFullYear())
	fill_day_selector(name)
	document.all[name+"_d"].value = (dd || cd.getDate())
	set_date_selector(name)
}
