var day_name = Array('Ïí','Âò','Ñð','×ò','Ïò','Ñá','Âñ');
var month_name = Array('ßíâ','Ôåâ','Ìàð','Àïð','Ìàé','Èþí','Èþë','Àâã','Ñåí','Îêò','Íîÿ','Äåê');
var holiday_month = Array(1, 1, 1, 2, 3, 5, 5, 5, 6,11,12);
var holiday_day   = Array(1, 2, 7,23, 8, 1, 2, 9,12, 7,12);

var isNullDate = false;

var calendarselects;

function calendar_create(cid,min_date,selected_date) {
	var current_date = new Date();
	//if(min_date < current_date)
	//	min_date = current_date;
	
	if (!selected_date) 
	{
	 isNullDate = true;
	 selected_date = new Date();
	}
	
	if(selected_date < min_date)
		selected_date = min_date;

	with(document) {
		writeln('<DIV ID="calendar_facediv_'+cid+'" STYLE="z-index:-1">');
		writeln('<TABLE CELLPADDING=0 CELLSPACING=2 BORDER=0 MARIGN=0><TR>');
		writeln('<TD><INPUT ID="calendar_day_'+cid+'" TYPE=TEXT VALUE="'+selected_date.getDate()+'" SIZE=2  onClick="calendar_show(\''+cid+'\','+min_date.valueOf()+');" STYLE="BACKGROUND-COLOR: #FFFFFF; BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; PADDING-BOTTOM: 1px; PADDING-LEFT: 2px; PADDING-RIGHT: 0px; PADDING-TOP: 1px; border-color: C0C0C0; font-family: Tahoma; font-size: 11px; color: #000000; font-weight:bold; width:20px;" onPropertyChange="calendar_redraw(\''+cid+'\','+min_date.valueOf()+','+selected_date.valueOf()+');" READONLY=true onkeydown="key_down(this,\''+cid+'\','+min_date.valueOf()+'); calendar_day_keydown(this,\''+cid+'\','+min_date.valueOf()+');"></TD>');
		writeln('<TD>&nbsp;</TD>');
		writeln('<TD><SELECT ID="calendar_month_'+cid+'" STYLE="BACKGROUND-COLOR: #FFFFFF; BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; PADDING-BOTTOM: 0px; PADDING-LEFT: 2px; PADDING-RIGHT: 0px; PADDING-TOP: 0px; border-color: #C0C0C0; font-family: Tahoma; font-size: 11px; font-weight: bold; color: #000000;" onChange="check_day(\''+cid+'\','+min_date.valueOf()+'); calendar_redraw(\''+cid+'\','+min_date.valueOf()+');" onkeydown="key_down(this,\''+cid+'\');">');
		
		for(i=( (min_date.getFullYear()==selected_date.getFullYear()) ? min_date.getMonth() : 0);i<12;i++) {
			if( selected_date.getMonth() == i ){
		
				writeln('<OPTION VALUE="'+i+'" SELECTED>'+month_name[i]+'</OPTION>');}
			else
				writeln('<OPTION VALUE="'+i+'">'+month_name[i]+'</OPTION>');
		}
		writeln('</SELECT></TD>');
		writeln('<TD>&nbsp;</TD>');
		writeln('<TD><SELECT ID="calendar_year_'+cid+'" STYLE="BACKGROUND-COLOR: #FFFFFF; BORDER-BOTTOM: #000000 1px solid; BORDER-LEFT: #000000 1px solid; BORDER-RIGHT: #000000 1px solid; BORDER-TOP: #000000 1px solid; PADDING-BOTTOM: 0px; PADDING-LEFT: 2px; PADDING-RIGHT: 0px; PADDING-TOP: 0px; border-color: #C0C0C0; font-family: Tahoma; font-size: 11px; font-weight: bold; color: #000000;" onChange="check_day(\''+cid+'\'); calendar_redraw_month_check(\''+cid+'\','+min_date.valueOf()+');" onkeydown="key_down(this,\''+cid+'\');">');
		
		var today_date = new Date();
						
		for(i=today_date.getFullYear()+2; i>=min_date.getFullYear(); i--) {
			if(i == selected_date.getFullYear())
				writeln('<OPTION VALUE="' + i + '" SELECTED>' + i + '</OPTION>');
			else
				writeln('<OPTION VALUE="' + i + '">' + i + '</OPTION>');
		}
		writeln('</SELECT></TD><TD><IMG ID="calendar_clear_'+cid+'" src="/icon/delete.gif" onClick="calendar_clear(\''+cid+'\')" STYLE="display:none;"><IMG ID="calendar_new_'+cid+'" src="/icon/new.gif" onClick="calendar_new(\''+cid+'\')" STYLE="display:none;"></TD>');
		writeln('</TR></TABLE>');
		writeln('</DIV>');

		writeln('<DIV ID="calendar_div_'+cid+'" STYLE="position:absolute; display:none; z-index:1; background-color:white">');
		writeln('</DIV>');
	}
	
	if (isNullDate) 
	{
	document.all('calendar_clear_'+cid).style.display = 'none';
	document.all('calendar_day_'+cid).style.display   = 'none';
	document.all('calendar_month_'+cid).style.display = 'none';
	document.all('calendar_year_'+cid).style.display  = 'none';
	document.all('calendar_new_'+cid).style.display = 'block';
	
	if(document.all["val"+cid]) document.all["val"+cid].value = '';
	if(document.all["lnk"+cid]) document.all["lnk"+cid].value = 0;
	}
}


function calendar_show(cid,min_date_val) {
	var showed_calendars_div = document.all.tags("div");
	for(i=0;i<showed_calendars_div.length;i++) {
		re = /calendar_div_/i;
		if(showed_calendars_div[i].id.match(re)!=null)
			if(showed_calendars_div[i].style.display == 'block') {
				return;
			}
	}
	
	var current_date = new Date();
	var min_date = new Date(min_date_val);
	min_date.setHours(23,59,00,000);  // END_OF_DAY_TIME
	var selected_date = new Date(document.all('calendar_year_'+cid).value, document.all('calendar_month_'+cid).value, document.all('calendar_day_'+cid).value);
	selected_date.setHours(23,59,00,000);  // END_OF_DAY_TIME
	var firstday_date = new Date(selected_date.getYear(),selected_date.getMonth(),1);
	var firstday_weekday = ((firstday_date.getDay()+6)%7);
	var tmp_date;

	res_table='';
	res_table+='<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=1 BORDERCOLOR=#C0C0C0 MARIGN=0><TR><TD BORDERCOLOR=white>';

	res_table+='<TABLE ID="calendar_table_'+cid+'" CELLPADDING=1 CELLSPACING=0 BORDER=1 BORDERCOLOR=white STYLE="font-family:Tahoma; font-size:10px;font-weight:bold; text-align:right;">';

	res_table+='<TR STYLE="font-weight:normal;">';
  for(i=0;i<7;i++) {
	  res_table+='<TD>'+day_name[i]+'</TD>';
	}
	res_table+='</TR>';


	for(str_num=0;str_num<6;str_num++) {
		res_table+='<TR>';
		
	  for(i=0;i<7;i++) { 
	  	tmp_date = new Date(selected_date);
	  	tmp_date.setHours(23,59,00,000);  // END_OF_DAY_TIME
	  	tmp_date.setDate(i-firstday_weekday+1+str_num*7);
			
		  if(tmp_date.getMonth() == selected_date.getMonth()) {
		    
				if(tmp_date >= min_date) {
				
				 	if(tmp_date.toString() == selected_date.toString()) {
				   	res_table+='<TD ID="selected_day_td_'+cid+'" BORDERCOLOR=red onMouseOver="mouse_in(this);" onMouseOut="mouse_out(this,true);"  onClick="mouse_click(this,\''+cid+'\');" STYLE="cursor:hand;border-width:2px;">';
				  }
				  else {
			    	res_table+='<TD onMouseOver="mouse_in(this);" onMouseOut="mouse_out(this,false);"  	onClick="mouse_click(this,\''+cid+'\'); calendar_redraw(\''+cid+'\','+min_date.valueOf()+','+selected_date.valueOf()+');" STYLE="cursor:hand;border-width:2px;">';
				  }

			    is_holiday = false; 
			    for(holiday_i=0;holiday_i<holiday_month.length;holiday_i++) {
				    if((tmp_date.getMonth()==(holiday_month[holiday_i]-1) && 			tmp_date.getDate()==holiday_day[holiday_i]) || ((i)%7==6)) {
				    	res_table+='<FONT COLOR="red">'+(i-firstday_weekday+1+str_num*7)+'</FONT>';
			    		is_holiday = true;
				    	break;
				    }
		  		  else {
		  		  	if((i)%7==5) {
				    	res_table+='<FONT COLOR="#808080">'+(i-firstday_weekday+1+str_num*7)+'</FONT>';
				    	is_holiday = true;
				    	break;
			  	  	}
				    }
				  } 
		    	if(is_holiday==false) {
			    	res_table+=(i-firstday_weekday+1+str_num*7);
			  	}
			  	
			    res_table+='</TD>';
		  	}
		  	else {
			  	res_table+='<TD STYLE="border-width:2px;"><FONT COLOR="#E0E0E0">' +(i-firstday_weekday+1+str_num*7)+ '</FONT></TD>';
		  	}
		  }
		  else {
		  	res_table+='<TD></TD>';
		  }
			
		  delete tmp_date;
		}

		res_table+='</TR>';
	}
	res_table+='</TD></TR></TABLE></TABLE>';

  document.all("calendar_div_"+cid).innerHTML = res_table;
  if(document.all('calendar_div_'+cid).style.display=='none') {
//    OLD VERSION
//		document.all("calendar_div_"+cid).style.top  = (event.clientY-event.offsetY-5+20);
//		document.all("calendar_div_"+cid).style.left = (event.clientX-event.offsetX-5);
		document.all("calendar_div_"+cid).style.top  = document.all('calendar_facediv_'+cid).style.top;
		document.all("calendar_div_"+cid).style.left = document.all('calendar_facediv_'+cid).style.left;
		document.all("calendar_div_"+cid).style.display = 'block';
		document.all("calendar_month_"+cid).focus();

//	hide all selects
		var allselects = document.all.tags("select");
		if(allselects != null) {
			calendarselects = Array();
			for(i=0;i<allselects.length;i++) {
				select_month = 'calendar_month_'+ cid;
				select_year  = 'calendar_year_' + cid;
				
				re = /calendar_/i;
				//alert(getOffset(allselects[i])+";"+document.all('calendar_div_'+cid).offsetTop);
				if((allselects[i].id!=select_month)&&(allselects[i].id!=select_year)&&(getOffset(allselects[i])>=document.all('calendar_div_'+cid).offsetTop)&&(getOffset(allselects[i])<=document.all('calendar_div_'+cid).offsetTop+document.all('calendar_div_'+cid).offsetHeight)) {
   					if(allselects[i].id.match(re)==null) {
						allselects[i].style.display = 'none';
					}
					else {
						if(allselects[i].style.display != 'none') {
							calendarselects[calendarselects.length+1] = allselects[i].id;
							allselects[i].style.display = 'none';
						}
					}
   				}
			}
		}
		
	}
}


function calendar_redraw_month_check(cid, min_date_val) {
	var min_date = new Date(min_date_val);
	var month_option;
	
	if(document.all('calendar_year_'+cid).value > min_date.getFullYear()) {
		with(document.all('calendar_month_'+cid)) {
			if(options.length<12) {
				var old_value = value;
				for(i=options.length; i>0; i--) {
					options.remove(i-1);
				}
				for(i=0;i<12;i++) {
					month_option = document.createElement("OPTION");
					add(month_option);
					month_option.text = month_name[i];
					month_option.value = i;
				}
				value = old_value;
			}
		}
	}
	else {
		with(document.all('calendar_month_'+cid).options) {
			if (options.length==12){
				var old_value = value;
				for(i=12; i>0; i--) {
					options.remove(i-1);
				}
				for(i=min_date.getMonth();i<12;i++) {
					month_option = document.createElement("OPTION");
					add(month_option);
					month_option.text = month_name[i];
					month_option.value = i;
				}
				if(old_value > min_date.getMonth())
					value = old_value;
				else
					value = min_date.getMonth();
			}
		}
	}

	if(document.all('calendar_div_'+cid).style.display=='block') {
		calendar_show(cid, min_date_val);
	}
	// INFRACAT INTEGRATION
	export_date(cid);
}


function calendar_redraw(cid, min_date_val) {
	if(document.all('calendar_div_'+cid).style.display=='block') {
		calendar_show(cid, min_date_val);
	}
	// INFRACAT INTEGRATION
	export_date(cid);
}


function check_day(cid, min_date_val) { 
	var min_date = new Date(min_date_val);
	min_date.setHours(23,59,00,000);  // END_OF_DAY_TIME

	var selected_date = new Date(document.all('calendar_year_'+cid).value, document.all('calendar_month_'+cid).value, document.all('calendar_day_'+cid).value);
	selected_date.setHours(23,59,00,000);  // END_OF_DAY_TIME

	if(selected_date < min_date) {
		document.all('calendar_day_'+cid).value = min_date.getDate()
	}

	if(selected_date.getMonth()!=document.all('calendar_month_'+cid).value) {
		document.all('calendar_day_'+cid).value -= selected_date.getDate();
	}
}


function mouse_in(src_element) {
	src_element.borderColor='pink';
}


function mouse_out(src_element,selected) {
	if(selected)
		src_element.borderColor='red';
	else
		src_element.borderColor='white';
}


function mouse_click(src_element,cid) {
	document.all("calendar_day_"+cid).value = src_element.innerText;
	document.all("calendar_div_"+cid).style.display = 'none';
//	show all selects
		var allselects = document.all.tags("select");
		if(allselects != null) {
			for(i=0;i<allselects.length;i++) {
				select_month = 'calendar_month_'+ cid;
				select_year  = 'calendar_year_' + cid;
				
				re = /calendar_/i;
   				if((allselects[i].id!=select_month)&&(allselects[i].id!=select_year)) {
   					if(allselects[i].id.match(re)==null) {
						allselects[i].style.display = 'block';
					}
					else {
						for(j=0;j<calendarselects.length;j++) {
							if(allselects[i].id == calendarselects[j]) {
								allselects[i].style.display = 'block';
							}
						}
					}
   				}
			}
		}
	
}


function calendar_day_keydown(src_element,cid,min_date_val) {
	var new_day = document.all("calendar_day_"+cid).value;

	switch(event.keyCode) {
		case(40) : 	new_day++; break;
		case(38) : 	new_day--; break;
		case(13) : 	calendar_day_keyenter(cid, min_date_val); break;
		default	 : break;//alert('Unknown keyCode:'+event.keyCode);
	}

	var min_date = new Date(min_date_val);
	min_date.setHours(23,59,00,000);  // END_OF_DAY_TIME

	var selected_date = new Date(document.all('calendar_year_'+cid).value, document.all('calendar_month_'+cid).value, document.all('calendar_day_'+cid).value);
	selected_date.setHours(23,59,00,000);  // END_OF_DAY_TIME

	var new_date = new Date(document.all('calendar_year_'+cid).value, document.all('calendar_month_'+cid).value, new_day);
	new_date.setHours(23,59,00,000);  // END_OF_DAY_TIME

	if((new_date >= min_date) && (new_date.getFullYear() < (min_date.getFullYear()+5))) {
		document.all("calendar_day_"+cid).value = new_day;

		if(new_date.getMonth() != selected_date.getMonth()) {
			document.all('calendar_year_'+cid).value = new_date.getFullYear();
			document.all('calendar_month_'+cid).value = new_date.getMonth();
			document.all('calendar_day_'+cid).value = new_date.getDate();
		}
             
		calendar_redraw_month_check(cid, min_date);
	}
}


function calendar_day_keyenter(cid, min_date_val) {
	if(document.all('calendar_div_'+cid).style.display=='block') {
		document.all("calendar_day_"+cid).value = document.all('selected_day_td_'+cid).innerText;
		document.all("calendar_div_"+cid).style.display = 'none';
	}
	else {
			calendar_show(cid, min_date_val);
	}
}


function key_down(src_element,cid) {
	switch(src_element.id) {
		case('calendar_day_'+cid): 
			if(event.keyCode==39) document.all('calendar_month_'+cid).focus();
			break;
		case('calendar_month_'+cid):
			if(event.keyCode==39) document.all('calendar_year_'+cid).focus();
			if(event.keyCode==37) document.all('calendar_day_'+cid).focus();
			break;
		case('calendar_year_'+cid):
			if(event.keyCode==37) document.all('calendar_month_'+cid).focus();
			break;
		default: break;
	}
}


function export_date(cid) {


	if(document.all["val"+cid]) {
		document.all["val"+cid].value = document.all['calendar_year_'+cid].value  + '-' +	eval(eval(document.all['calendar_month_'+cid].value) + 1) + '-' + document.all["calendar_day_"+cid].value;

	if(document.all["lnk"+cid]) document.all["lnk"+cid].value = 1;
			
	//alert("Äàòà óñòàíîâëåíà:"+ document.all["val"+cid].value);
	}

}


function export_empty_date(cid) {
	if(document.all["val"+cid]) {
		document.all["val"+cid].value = '';
	}
	if(document.all["lnk"+cid]) document.all["lnk"+cid].value = 1;	
}


function calendar_clear(cid) {
	document.all('calendar_clear_'+cid).style.display = 'none';
	document.all('calendar_day_'+cid).style.display   = 'none';
	document.all('calendar_month_'+cid).style.display = 'none';
	document.all('calendar_year_'+cid).style.display  = 'none';
	document.all('calendar_new_'+cid).style.display = 'block';
	export_empty_date(cid);
}


function calendar_new(cid) {
	document.all('calendar_new_'+cid).style.display = 'none';
	document.all('calendar_day_'+cid).style.display   = 'block';
	document.all('calendar_month_'+cid).style.display = 'block';
	document.all('calendar_year_'+cid).style.display  = 'block';
	document.all('calendar_clear_'+cid).style.display = 'block';
	export_date(cid);
}

function getOffset(obj) {
	var offset = 0;
	while(obj!=document.body) {
		offset += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return offset;
}

//if (window.event) {
//	document.captureEvents(Event.MOUSEDOWN);
//	document.onmousedown = hide_onclink;
//}

function hide_onclink() {
//	alert("111");
//	var showed_calendars_div = document.all.tags("div");
//	for(i=0;i<showed_calendars_div.length;i++) {
//		re = /calendar_div_/i;
//		if(showed_calendars_div[i].id.match(re)!=null)
//			if(showed_calendars_div[i].style.display == 'block') 
//				if((showed_calendars_div[i].offsetTop >= event.clientY)||((showed_calendars_div[i].offsetTop+showed_calendars_div[i].offsetHeight) <= event.clientY))
//				if((showed_calendars_div[i].offsetLeft >= event.clientX)||((showed_calendars_div[i].offsetLeft+showed_calendars_div[i].offsetWidth) <= event.clientX))
//					showed_calendars_div[i].style.display = 'none';
//	}


//	if(event.srcElement.id.match(re))
// 		if(showed_calendars_div[i].id.match(re)!=null)
// 			alert(event.srcElement.id);
 	
// 		event.srcElement.style.display = 'none';

}

