var dDate = new Date();
var dCurMonth = dDate.getMonth();
var dCurDayOfMonth = dDate.getDate();
var dCurYear = dDate.getFullYear();
var objPrevElement = new Object();

function toggle_color(myElement) {
var toggleColor = "#ff0000";

if (myElement.color == toggleColor) {
myElement.color = "";
} else {
myElement.color = toggleColor;
   }

}

function get_now_time() {
	var get_date = new Date();
	var get_hour = get_date.getHours();
	if (get_hour<10)
		get_hour = "0" + get_hour.toString();
		
	var get_minute = get_date.getMinutes();
	if (get_minute<10)
		get_minute = "0" + get_minute.toString();
	
	return get_hour+":"+get_minute;
}
// Выбор элемента
function set_selected_day(myElement, fieldname)
{
		if (!isNaN(parseInt(myElement.innerHTML)) ) {
		
			myElement.className = "select";
			objPrevElement.className = "";
			
			day = myElement.innerHTML;
			if (day<10)
				day = "0" + day.toString();
			
			month = document.forms['main'].elements['tbSelMonth_'+fieldname].value;
			if (month<10)
				month = "0" + month.toString();
				
			year = document.forms['main'].elements['tbSelYear_'+fieldname].value;
			
			val = day + "-" + month + "-" + year + " "+get_now_time();
			document.forms['main'].elements[fieldname].value = val;
			objPrevElement = myElement;
		}
	
}
function get_days_in_month(iMonth, iYear) {
	var dPrevDate = new Date(iYear, iMonth, 0);
	return dPrevDate.getDate();
}
function build_cal(iYear, iMonth) {
	var aMonth = new Array();
	aMonth[0] = new Array(7);
	aMonth[1] = new Array(7);
	aMonth[2] = new Array(7);
	aMonth[3] = new Array(7);
	aMonth[4] = new Array(7);
	aMonth[5] = new Array(7);
	aMonth[6] = new Array(7);
	var dCalDate = new Date(iYear, iMonth-1, 1);
	var iDayOfFirst = dCalDate.getDay();
	var iDaysInMonth = get_days_in_month(iMonth, iYear);
	var iVarDate = 1;
	var i, d, w;

	aMonth[0][0] = "Вс";
	aMonth[0][1] = "Пн";
	aMonth[0][2] = "Вт";
	aMonth[0][3] = "Ср";
	aMonth[0][4] = "Чт";
	aMonth[0][5] = "Пт";
	aMonth[0][6] = "Сб";
	
	for (d = iDayOfFirst; d < 7; d++) {
		aMonth[1][d] = iVarDate;
		iVarDate++;
	}
	for (w = 2; w < 7; w++) {
		for (d = 0; d < 7; d++) {
			if (iVarDate <= iDaysInMonth) {
				aMonth[w][d] = iVarDate;
					iVarDate++;
	    }
	 	}
	}
	return aMonth;
}
function draw_cal(iYear, iMonth, fieldname) {
	var myMonth;
	myMonth = build_cal(iYear, iMonth);
	document.write('<table cellspacing="0" cellpadding="0" border="0" class="calendar">');
	document.write("<tr>");
	for (d = 0; d < 7; d++) {
		document.write("<th>" + myMonth[0][d] + "</tH>");
	}
	document.write("</tr>");
	for (w = 1; w < 7; w++) {
		document.write("<tr>")
		for (d = 0; d < 7; d++) {
			document.write("<td id='calCell'>");
			document.write("<div id='calDateText_"+(((7*w)+d)-7)+"' style='CURSOR:Hand;' onclick=\"set_selected_day(this, '" + fieldname + "')\">");
			if (!isNaN(myMonth[w][d])) {
				document.write( myMonth[w][d] );
			} else {
				document.write("&nbsp");
			}
			document.write("</div>");
			document.write("</td>")
		}
		document.write("</tr>");
	}
	document.write("</table>");
}

function update_cal(iYear, iMonth) {
	myMonth = build_cal(iYear, iMonth);
	objPrevElement.bgColor = "";
	for (w = 1; w < 7; w++) {
		for (d = 0; d < 7; d++) {
			if (!isNaN(myMonth[w][d])) {
				document.getElementById('calDateText_'+(((7*w)+d)-7) ).innerHTML = myMonth[w][d];
			} 
			else {
				document.getElementById('calDateText_'+(((7*w)+d)-7) ).innerHTML = " ";
			}
		}
	}
}


function select_current_tab (fieldname)
{
	document.forms['main'].elements['tbSelMonth_'+fieldname].options[dCurMonth].selected = true;
	select_elem = document.forms['main'].elements['tbSelYear_'+fieldname].options;
	for(t=0; t < select_elem.length; t++)
		if (select_elem[t].value==dCurYear)	
			select_elem[t].selected = true;

}

// Создать цифровой календарь Для формы main
function create_calendar(fieldname)
{
	draw_cal(dCurYear, dCurMonth+1, fieldname);
}

// Вкладки календаря
function create_calender_tabs (fieldname)
{
document.write('<div class="calender_tab">');
                document.write('<select name="tbSelMonth_'+fieldname+'" onchange="update_cal(main.tbSelYear_'+fieldname+'.value, main.tbSelMonth_'+fieldname+'.value)" style="width: 80px">');
                document.write('<option value="1">Январь</option>');
                document.write('<option value="2">Февраль</option>');
                document.write('<option value="3">Март</option>');
                document.write('<option value="4">Апрель</option>');
                document.write('<option value="5">Май</option>');
                document.write('<option value="6">Июнь</option>');
                document.write('<option value="7">Июль</option>');
                document.write('<option value="8">Август</option>');
                document.write('<option value="9">Сентябрь</option>');
                document.write('<option value="10">Октябрь</option>');
                document.write('<option value="11">Ноябрь</option>');
                document.write('<option value="12">Декабрь</option>');
                document.write('</select>');
  
                document.write('<select name="tbSelYear_'+fieldname+'" onchange="update_cal(main.tbSelYear_'+fieldname+'.value, main.tbSelMonth_'+fieldname+'.value)" style="width: 70px">');
                year_count=dCurYear-5;
                for(i=0; i<11; i++) {
                	current_year=year_count+i;
                	document.write('<option value="'+current_year+'">'+current_year+'</option>');
              	}
            
                document.write('</select>');
document.write('</div>');


}

function calender_main(fieldname) 
{
	document.write('<div class="calender_outer">');
  
  create_calender_tabs (fieldname);
	
  document.write('<div>');
	
	create_calendar(fieldname);
	
	document.write('</div>');
	
  select_current_tab(fieldname);
  
  document.write('</div>');
}




// Календарь с месяцами

function mth_select_current_tab (fieldname,this_date)
{
	month_year = this_date.split( '-' );
	month_year[0]=parseInt(month_year[0]);
	document.forms['main'].elements['tbSelMonth_'+fieldname].options[month_year[0]-1].selected = true;
	select_elem = document.forms['main'].elements['tbSelYear_'+fieldname].options;
	for(t=0; t < select_elem.length; t++)
		if (select_elem[t].value==month_year[1])	
			select_elem[t].selected = true;

}

// Создать цифровой календарь Для формы main
function mth_update_cal(fieldname, year_select, month_select)
{
	document.forms['main'].elements[fieldname].value = month_select + "-" + year_select;
}

// Вкладки календаря
function mth_create_calender_tabs (fieldname)
{
document.write('<div class="calender_tab">');
                document.write('<select name="tbSelMonth_'+fieldname+'" onchange="mth_update_cal(\''+fieldname+'\', main.tbSelYear_'+fieldname+'.value, main.tbSelMonth_'+fieldname+'.value)" style="width: 80px">');
                document.write('<option value="1">Январь</option>');
                document.write('<option value="2">Февраль</option>');
                document.write('<option value="3">Март</option>');
                document.write('<option value="4">Апрель</option>');
                document.write('<option value="5">Май</option>');
                document.write('<option value="6">Июнь</option>');
                document.write('<option value="7">Июль</option>');
                document.write('<option value="8">Август</option>');
                document.write('<option value="9">Сентябрь</option>');
                document.write('<option value="10">Октябрь</option>');
                document.write('<option value="11">Ноябрь</option>');
                document.write('<option value="12">Декабрь</option>');
                document.write('</select>');
  
                document.write('<select name="tbSelYear_'+fieldname+'" onchange="mth_update_cal(main.tbSelYear_'+fieldname+'.value, main.tbSelMonth_'+fieldname+'.value)" style="width: 70px">');
                year_count=dCurYear-5;
                for(i=0; i<11; i++) {
                	current_year=year_count+i;
                	document.write('<option value="'+current_year+'">'+current_year+'</option>');
              	}
            
                document.write('</select>');
document.write('</div>');


}

function mth_calender_create(fieldname,this_date) 
{
	document.write('<div class="calender_outer">');
  
  mth_create_calender_tabs (fieldname);
	
  mth_select_current_tab(fieldname,this_date);
  
  document.write('</div>');
}
