function calendar(div_id,parent_div_id,insert_id,control_id,change)
         {
// настройки
	font_size ='110%';
	header_font_size ='9px';
	table_border = 0;
	table_width = 135;
	font = "'Tahoma'";
	
	table_background_color ='#ffffff';
	table_font_color = 'ffffff';
	table_border_color = 'ffffff';
	
	header_background_color = '#edede1';
	header_font_color = '000000';
	header_heigth = 40;
	
	name_day_background_color = '#ffffff';
	name_day_font_color = '000000';
	holyday_background_color = '#d4d4d4';
	holyday_font_color = '000000';
	no_week_day_background_color ='#ffffff';
	no_week_day_font_color = '000000';
	day_heigth = 18;
	day_width = 18;
	no_active_day_background_color = '#ffffff';
	no_active_day_font_color = '#c0c0c0';
	no_active_day_holyday_background_color = '#d4d4d4';
	no_active_day_holyday_font_color = '#c0c0c0';
	
	insert_day_background_color = '#ffffff';
	insert_day_border_color = '000000';
	insert_day_font_color = '#000000';
	no_active_insert_day_font_color = '#c0c0c0';
	insert_border = 0;
	
	now_day_background_color = '#ffffff';
	now_day_border_color = '000000';
	now_day_font_color = '#ff0000';
	no_active_now_day_font_color = '#c0c0c0';
	now_border = 0;
	
	no_select_background_color = '#ffffff';
	no_select_font_color = '#000000';
	no_select_holyday_background_color = '#d4d4d4';
	no_select_holyday_font_color = '#000000';
// настройки
		str_selected_date ='';
		if (insert_id !='' && document.getElementById(insert_id).value !='') {
			document.getElementById('temp_date').value = document.getElementById(insert_id).value;
			input_date = document.getElementById(insert_id).value;
			str_date = new Date(input_date.substr(6,4),(input_date.substr(3,2)-1),input_date.substr(0,2));		
			str_selected_date = str_date.getDate() +'-'+str_date.getMonth()+'-'+str_date.getFullYear();
			if (change == 'open'){
				date = new Date(input_date.substr(6,4),(input_date.substr(3,2)-1),input_date.substr(0,2));					
			}
		}	else {
			if (document.getElementById('temp_date').value !='') {
				input_date = document.getElementById('temp_date').value;
				date = new Date(input_date.substr(6,4),(input_date.substr(3,2)-1),input_date.substr(0,2));		
			} else {
				date = new Date();
			}
		}
		if (parent_div_id !='' && document.getElementById(parent_div_id).value !='') {
			
			document.getElementById(parent_div_id).innerHTML = '';
			document.getElementById('temp_date').value = '';
			document.getElementById(parent_div_id).style.display = "none";
		}
		now_date = new Date();
		now_day = now_date.getDate();
		now_month = now_date.getMonth();
		now_year = now_date.getFullYear();
		str_now_date = now_day+'-'+now_month+'-'+now_year;
		day = date.getDate();
		month = date.getMonth();
		year = date.getFullYear(); 
		month_id = div_id + 'month';
		year_id = div_id + 'year';
		calendar_close = '';
		if (Number(change) !='NaN' && Number(change)>0) {
			day = Number(change);
			calendar_close = 1;
		}
		if (change == 'year') year = document.getElementById(year_id).value;
		if (change == 'month') month = document.getElementById(month_id).value - 1;
		if (change == 'next') { 
			month++;
			day = 1;
		}
		if (change == 'prev') {
			month--;
			day = 1;
			}
		if (month == 0) {
			month = 12;
			year--;
			}
		if (month == 13) {
			month = 1;
			year++;
		}
		date = new Date(year, month, day);
		day = date.getDate();
		month = date.getMonth();
		year = date.getFullYear();
		if (control_id !='' && document.getElementById(control_id).value !='') {
			str_date_control = document.getElementById(control_id).value;
			control_date = new Date(str_date_control.substr(6,4),(str_date_control.substr(3,2)-1),str_date_control.substr(0,2));
			if (control_date > date) {
//				day = control_date.getDate();
				month = control_date.getMonth();
				year = control_date.getFullYear();
			}
		 } else {
		 
			str_date_control ='';
			control_date = new Date(now_year, now_month, now_day);
		 }
		if (str_selected_date =='') str_selected_date = now_day+'-'+(now_month-1)+'-'+now_year;
		months = new Array('Январь',
                            'Февраль',
                            'Март',
                            'Апрель',
                            'Май',
                            'Июнь',
                            'Июль',
                            'Август',
                            'Сентябрь',
                            'Октябрь',
                            'Ноябрь',
                            'Декабрь');
         days = new Array('Пн',
                          'Вт',
                          'Ср',
                          'Чт',
                          'Пт',
                          'Сб',
                          'Вс');
							
         this_month = new Date(year, month, 1);
         next_month = new Date(year, month + 1, 1);
                  
         first_week_day = this_month.getDay();
		 if (first_week_day == 0) first_week_day =7;
 		 if (month != 2) {
         days_in_this_month = Math.floor((next_month.getTime() - this_month.getTime()) / (1000 * 60 * 60 * 24));
         } else {
		 days_in_this_month = 31;
		}
         
         calendar_html = '<table width = "'+table_width+'" cellpadding="0" cellspacing="1" border = "'+table_border+'" style="font-size:'+font_size+';font-family:'+font+'; border-color:'+table_border_color+';background-color:'+table_background_color+'; color:'+table_font_color+';">';
         
        calendar_html += '<tr><td colspan="7" align="center" style="background-color:'+header_background_color+'; color:'+header_font_color+';">';
		calendar_html += '<div style="padding:3px;float:right;"><a style = "text-decoration:none;color:black;" href = "javascript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','next')"+'"><b>></b></a></div>';
		if (control_date !=''){
			current_date = new Date(year,month,1);
			if (current_date >= control_date) {
			 calendar_html += '<div style="padding:3px;float:left;"><a style = "text-decoration:none;color:black;" href = "javascript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','prev')"+'"><b><</b></a></div>';
			} else {
				calendar_html += '<div style="padding:3px;float:left;color:'+no_active_day_font_color+'"><</div>';
			}
		} else {
			calendar_html += '<div style="padding:3px;float:left;"><a style = "text-decoration:none;color:black;" href = "javascript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','prev')"+'"><b><</b></a></div>';
		}
		calendar_html +='<div style="padding:3px;"><b>'+months[month]+' '+year+' </b></div>';
		calendar_html +='</td></tr>';
        calendar_html +='<tr align="center">';
		for(week_day = 0; week_day <7; week_day++){
			calendar_html += '<td width = "'+day_width+'" style="padding-top:2px;padding-bottom:2px;font-size:'+header_font_size+';font-family:'+font+';font-weight:bold;background-color:'+name_day_background_color+'; color:'+name_day_font_color+';">'+days[week_day]+'</td>';   
		}
		calendar_html +='</tr>';
		calendar_html += '</table>';
		calendar_html += '<table width = "'+table_width+'" cellpadding="0" cellspacing="1" border = "'+table_border+'" style="font-size:'+font_size+';font-family:'+font+'; border-color:'+table_border_color+';background-color:'+table_background_color+'; color:'+table_font_color+';">';
        calendar_html += '<tr  align="center">';
          
         for(week_day = 0; week_day < first_week_day-1; week_day++)
            {
			if (week_day == 5 || week_day == 6) {
				calendar_html += '<td style="background-color:'+holyday_background_color+'; color:'+holyday_font_color+';">&nbsp;</td>';   			
			} else {
				calendar_html += '<td style="background-color:'+no_week_day_background_color+'; color:'+no_week_day_font_color+';">&nbsp;</td>';   			
			}
            }
            
         week_day = first_week_day-1;
         for(day_counter = 1; day_counter <= days_in_this_month; day_counter++)
            {
            week_day %= 7;
			if(week_day == 0)
					   calendar_html += '</tr><tr align="center" heigth = "'+day_heigth+'">';
            if (control_date !=''){
				current_date = new Date(year,month,day_counter);
				str_current_date = day_counter+'-'+month+'-'+year;
				if (current_date >= control_date) {
					if(str_selected_date == str_current_date) {
					if(str_selected_date == str_now_date) insert_day_font_color = now_day_font_color;
					calendar_html += '<td heigth = "'+(day_heigth-2)+'" width = "'+(day_width-2)+'" style = "background-color:'+insert_day_background_color+'; color:'+insert_day_font_color+'; border:'+insert_border+'px solid; border-color:'+insert_day_border_color+';"><a style="text-decoration:none; color:'+insert_day_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'">' + day_counter + '</a></td>';
						} else {
							if (str_current_date == str_now_date) {
								calendar_html += '<td width = "'+day_width+'" style = "background-color:'+now_day_background_color+'; color:'+now_day_font_color+'; border:'+now_border+'px solid; border-color:'+now_day_border_color+';"><a style="text-decoration:none;color:'+now_day_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'">' + day_counter + '</a></td>';
							} else { 
								if (week_day == 5 || week_day == 6) 
								{	calendar_html += '<td width = "'+day_width+'" style="background-color:'+no_select_holyday_background_color+';"><a style="text-decoration:none;color:'+no_select_holyday_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'">' + day_counter + '</a></td>';
								} else {
								   calendar_html += '<td width = "'+day_width+'" style="background-color:'+no_select_background_color+'; color:'+no_select_font_color+';"><a style="text-decoration:none;color:'+no_select_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'">' + day_counter + '</a></td>';
								}
							}	
					}
				} else {
				if(str_selected_date == str_current_date) {
					   calendar_html += '<td heigth = "'+(day_heigth-2)+'" width = "'+(day_width-2)+'" style = "background-color:'+insert_day_background_color+'; color:'+no_active_insert_day_font_color+'; border:'+insert_border+'px solid; border-color:'+insert_day_border_color+';">' + day_counter + '</td>';
						} else {
					if (str_current_date == str_now_date) {
					calendar_html += '<td width = "'+day_width+'" style = "background-color:'+now_day_background_color+'; border:'+now_border+'px solid; border-color:'+now_day_border_color+';color:'+no_active_now_day_font_color+';">' + day_counter + '</td>';
					} else {
						if (week_day == 5 || week_day == 6) 
						{	calendar_html += '<td width = "'+day_width+'" style="background-color:'+no_active_day_holyday_background_color+'; color:'+no_active_day_holyday_font_color+';">' + day_counter + '</td>';
						} else {
							calendar_html += '<td width = "'+day_width+'" style="background-color:'+no_active_day_background_color+'; color:'+no_active_day_font_color+';">' + day_counter + '</td>';
						}
					}	
				}
				}
				
			} else { 
					current_date = new Date(year,month,day_counter);
					str_current_date = day_counter+'-'+month+'-'+year;
					if(str_selected_date == str_current_date) {
						if(str_selected_date == str_now_date) insert_day_font_color = now_day_font_color;
					   calendar_html += '<td heigth = "'+(day_heigth-2)+'" width = "'+(day_width-2)+'" style = "background-color:'+insert_day_background_color+'; color:'+insert_day_font_color+'; border:'+insert_border+'px solid; border-color:'+insert_day_border_color+';"><a style="text-decoration:none; color:'+insert_day_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'">' + day_counter + '</a></td>';
					} else {
					if (str_current_date == str_now_date) {
						calendar_html += '<td width = "'+day_width+'" style = "background-color:'+now_day_background_color+'; border:'+now_border+'px solid; border-color:'+now_day_border_color+';"><a style="text-decoration:none;color:'+now_day_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'"><b>' + day_counter + '</b></a></td>';
					} else {
						if (week_day == 5 || week_day == 6) 
						{ calendar_html += '<td width = "'+day_width+'" align="center" style="background-color:'+no_select_holyday_background_color+'; color:'+no_select_holyday_font_color+';"><a style="text-decoration:none;color:'+no_select_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'">' + day_counter + '</a></td>';
						} else {
						  calendar_html += '<td width = "'+day_width+'" align="center" style="background-color:'+no_select_background_color+'; color:'+no_select_font_color+';"><a style="text-decoration:none;color:'+no_select_font_color+';" href="JavaScript:calendar('+"'"+div_id+"','"+parent_div_id+"','"+insert_id+"','"+control_id+"','"+day_counter+"')"+'">' + day_counter + '</a></td>';
						}
						}
					}
			}
            week_day++;
            }
            
         calendar_html += '</tr>';
         calendar_html += '</table>';

		str_month = '';
		str_day = '';
		if (month < 9) {str_month = '0'+ (month + 1);
		} else {
			str_month = month + 1;
		}
		if (day < 10) { str_day = '0' + day;
		} else {
			str_day = day;
		}
		document.getElementById('temp_date').value = str_day + '-'+ str_month + '-' + year;
        if (calendar_close == 1) {
			document.getElementById(insert_id).value = str_day + '-'+ str_month + '-' + year;
			document.getElementById(div_id).innerHTML = '';
			document.getElementById('temp_date').value = ''; 
			document.getElementById(div_id).style.display = "none";
		} else {
			if (document.getElementById(div_id).style.display == 'block' && change =='open') {
			document.getElementById(div_id).innerHTML = '';
			document.getElementById('temp_date').value = '';
			document.getElementById(div_id).style.display = "none";
			} else 	{
			document.getElementById(div_id).innerHTML = calendar_html;
			document.getElementById(div_id).style.display = "block";
			}
		}
		
}

