	var html=function(id){ return document.getElementById(id); }; 	
	
	function print_r(arr, level) 
	{
		var print_red_text = "";
		if(!level) level = 0;
		var level_padding = "";
		for(var j=0; j<level+1; j++) level_padding += "    ";
		if(typeof(arr) == 'object') {
			for(var item in arr) {
				var value = arr[item];
				if(typeof(value) == 'object') {
					print_red_text += level_padding + "'" + item + "' :\n";
					print_red_text += print_r(value,level+1);
			} 
				else 
					print_red_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		} 

		else  print_red_text = "===>"+arr+"<===("+typeof(arr)+")";
		return print_red_text;
	}
	
	
	/**/	
	function sortEvents(back)
	{		
		if(document.forms.search.elements['personal'].checked)
			document.forms.search.elements['personal'].value = '1';			
			
		if(document.forms.search.elements['history'])
			if(document.forms.search.elements['history'].checked)
				document.forms.search.elements['history'].value = '1';			
				
		if(back != 0)
		{
			var parArr = parseGetParams();
			var element = document.createElement("input"); 
			element.setAttribute("type", 'hidden');
			element.setAttribute("name", back);
			document.forms.search.appendChild(element);
			var element = document.createElement("input"); 
			element.setAttribute("type", 'hidden');
			element.setAttribute("name", "date");
			element.setAttribute("value", parArr['date']);
			document.forms.search.appendChild(element);
		}
		document.forms.search.submit();
	}
	
	/**/	
	function getSelectedTab()
	{		
		if(document.getElementById('week_tab').className == 'dhx_cal_tab active')
			return 'week';
		if(document.getElementById('month_tab').className == 'dhx_cal_tab active')
			return 'month';
		return 0;
	}
		
	/*
	*
	* Проверяет форму создания события
	*
	*
	*/
	function validateForm()
	{	
		date_start = new Date(html('start_year').value,html('start_month').value,html('start_day').value,html('start_time').value);
		date_end = new Date(html('end_year').value,html('end_month').value,html('end_day').value,html('end_time').value);
		
		if(eval_date_period(date_start, date_end) <= 0)
		{
			alert('Некорректный временной промежуток');
			return false;
		}	
		
		html('start_year').value = date_start.getFullYear();
		html('start_month').value = date_start.getMonth();
		html('start_day').value = date_start.getDate();
		html('end_year').value = date_end.getFullYear();
		html('end_month').value = date_end.getMonth();
		html('end_day').value = date_end.getDate();
		
		
		if(document.getElementById('history').checked)
			document.getElementById('history').value=1;
		else
			document.getElementById('history').value=0;
			
		if(document.getElementById('personal').checked)
			document.getElementById('personal').value=1;
		else
			document.getElementById('personal').value=0;
			
		if(document.getElementById('only_for_community').checked)
			document.getElementById('only_for_community').value=1;
		else
			document.getElementById('only_for_community').value=0;
		
		//alert(document.getElementById('link').value.search(/^http(s?)\:\/\/risk\.r[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1);
		//http://risk.ru/
		//http://test.risk.ru/
		//http://wiki.risk.ru/
		//if(document.getElementById('radio_link').checked && document.getElementById('link').value.search(/^http(s?)\:\/\/w*\.*risk\.r[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1 && document.getElementById('link').value.search(/^http(s?)\:\/\/w*\.*test\.risk\.r[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1 && document.getElementById('link').value.search(/^http(s?)\:\/\/w*\.*wiki\.risk\.r[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1)
		if(document.getElementById('radio_link').checked && document.getElementById('link').value.search(/^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1 )
		{
			alert('Некорректная ссылка');
			return false;
		}
		
		if(document.getElementById('category').value == document.getElementById('category2').value && document.getElementById('category').value != 0)
		{
			alert('Вы выбрали одинаковые категории');
			return false;
		}
		
		if(document.getElementById('radio_place_choose').checked)
			document.getElementById('place').value=document.getElementById('select_place').value;
		else
			if(document.getElementById('radio_place_write').checked)
				document.getElementById('place').value=document.getElementById('text_place').value;		
		
		if(document.getElementById('place').value == 0)
		{
			alert('Некорректное место ');
			return false;
		}
		
		return true;
	}
	
	/*
	*
	* Проверяет форму изменения события
	*
	*
	*/
	function validateFormEdit()
	{	
	
		date_start = new Date(html('start_year').value,html('start_month').value,html('start_day').value,html('start_time').value);
		date_end = new Date(html('end_year').value,html('end_month').value,html('end_day').value,html('end_time').value);
		
		if(eval_date_period(date_start, date_end) <= 0)
		{
			alert('Некорректный временной промежуток');
			return false;
		}
		
		html('start_year').value = date_start.getFullYear();
		html('start_month').value = date_start.getMonth();
		html('start_day').value = date_start.getDate();
		html('end_year').value = date_end.getFullYear();
		html('end_month').value = date_end.getMonth();
		html('end_day').value = date_end.getDate();
		
		if(document.getElementById('history').checked)
			document.getElementById('history').value=1;
		else
			document.getElementById('history').value=0;
			
		if(document.getElementById('personal').checked)
			document.getElementById('personal').value=1;
		else
			document.getElementById('personal').value=0;
			
		if(document.getElementById('only_for_community').checked)
			document.getElementById('only_for_community').value=1;
		else
			document.getElementById('only_for_community').value=0;
		
		/* if(document.getElementById('link').value.search(/^http(s?)\:\/\/w*\.*risk\.r[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1 && document.getElementById('link').value.search(/^http(s?)\:\/\/w*\.*test\.risk\.r[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1 && document.getElementById('link').value.search(/^http(s?)\:\/\/w*\.*wiki\.risk\.r[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1)
		{
			alert('Некорректная ссылка\nМожно добавлять только ссылки на Риск.ру');
			return false;
		} */
		if(document.getElementById('link').value.search(/^http(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/) == -1 )
		{
			alert('Некорректная ссылка');
			return false;
		}
		
		if(document.getElementById('category').value == document.getElementById('category2').value && document.getElementById('category').value != 0)
		{
			alert('Вы выбрали одинаковые категории');
			return false;
		}
		
		if(document.getElementById('radio_place_choose').checked)
			document.getElementById('place').value=document.getElementById('select_place').value;
		else
			if(document.getElementById('radio_place_write').checked)
				document.getElementById('place').value=document.getElementById('text_place').value;		
		
		if(document.getElementById('place').value == 0)
		{
			alert('Некорректное место ');
			return false;
		}
		
		return true;
	}
	
	/*
	*
	* Проверяет форму создания или изменения события, кроме ссылки
	*
	*
	*/
	function validateFormPost()
	{	
		date_start = new Date(html('start_year').value,html('start_month').value,html('start_day').value,html('start_time').value);
		date_end = new Date(html('end_year').value,html('end_month').value,html('end_day').value,html('end_time').value);
		
		if(eval_date_period(date_start, date_end) <= 0)
		{
			alert('Некорректный временной промежуток');
			return false;
		}
		
		html('start_year').value = date_start.getFullYear();
		html('start_month').value = date_start.getMonth();
		html('start_day').value = date_start.getDate();
		html('end_year').value = date_end.getFullYear();
		html('end_month').value = date_end.getMonth();
		html('end_day').value = date_end.getDate();
		
		
		
		if(document.getElementById('history').checked)
			document.getElementById('history').value=1;
		else
			document.getElementById('history').value=0;
			
		if(document.getElementById('personal').checked)
			document.getElementById('personal').value=1;
		else
			document.getElementById('personal').value=0;
			
		if(document.getElementById('only_for_community').checked)
			document.getElementById('only_for_community').value=1;
		else
			document.getElementById('only_for_community').value=0;		
		
		if(document.getElementById('category').value == document.getElementById('category2').value && document.getElementById('category').value != 0)
		{
			alert('Вы выбрали одинаковые категории');
			return false;
		}
		
		if(document.getElementById('radio_place_choose').checked)
			document.getElementById('place').value=document.getElementById('select_place').value;
		else
			if(document.getElementById('radio_place_write').checked)
				document.getElementById('place').value=document.getElementById('text_place').value;		
		
		if(document.getElementById('place').value == 0)
		{
			alert('Некорректное место ');
			return false;
		}			
		return true;
	}
		
	/**/
	function postAjaxEditEvent()
	{
		var aData={
			event_id:html('event_id').value,
			history:html('history').value,
			start_time:html('start_time').value,
			start_day:html('start_day').value,
			start_month:html('start_month').value,
			start_year:html('start_year').value,
			end_time:html('end_time').value,
			end_day:html('end_day').value,
			end_month:html('end_month').value,
			end_year:html('end_year').value,
			personal:html('personal').value,
			only_for_community:html('only_for_community').value,
			community:html('community').value,
			title:html('title').value,
			link:html('link').value,
			category:html('category').value,
			category2:html('category2').value,
			place:html('place').value,
			};
		var aButton = html('radio_place_write');
		postAjax('/inc/site/ajax/edit_event.php',aData,aButton);		
		return true;
	}
	
	/**/
	function postAjaxAddEvent()
	{
		var aData={
			history:html('history').value,
			start_time:html('start_time').value,
			start_day:html('start_day').value,
			start_month:html('start_month').value,
			start_year:html('start_year').value,
			end_time:html('end_time').value,
			end_day:html('end_day').value,
			end_month:html('end_month').value,
			end_year:html('end_year').value,
			personal:html('personal').value,
			only_for_community:html('only_for_community').value,
			community:html('community').value,
			title:html('title').value,
			link:html('link').value,
			category:html('category').value,
			category2:html('category2').value,
			place:html('place').value,
			};
		var aInput = html('event_id');
		postAjax('/inc/site/ajax/add_event.php',aData,aInput);
		return true;
	}
	
	/**/
	function addEvent(NoRedirect)
	{		
		if(html('action') != null)		
		{
			if(!validateFormEdit())
				return false;
			postAjaxEditEvent();
		}
		else
		{
			if(!validateForm())
				return false;
			postAjaxAddEvent();
		}
		
	 	if(NoRedirect == 0)
			redirect(); 
		return false;			
	}
		
	/**/
	function redirect()
	{
		date_period = 1;
		
		if(date_period < 2)
			window.location='http://test.risk.ru/calendar/day.php';
		else if(date_period < 8)
			window.location='http://test.risk.ru/calendar/calendar.php?week';
		else if(date_period < 30)
			window.location='http://test.risk.ru/calendar/calendar.php?mounth';
		else	
			window.location='http://test.risk.ru/calendar/calendar.php';		
	}
	
	/**/
 	function addEventExt()
	{
		// day_url = "http://test.risk.ru/calendar/day.php";
		// calendar_url = "http://test.risk.ru/calendar/calendar.php";
		day_url = "http://risk.ru/events/day/";
		calendar_url = "http://risk.ru/events/calendar/";
		
		if(html('action') != null)		
		{
			if(!validateFormEdit())
				return false;
			postAjaxEditEvent();
		}
		else
		{
			if(!validateForm())
				return false;
			postAjaxAddEvent();
		}

		date_start = new Date(html('start_year').value,html('start_month').value,html('start_day').value);
		date_period = eval_date_period(date_start, new Date(html('end_year').value,html('end_month').value,html('end_day').value));
				
		if(date_period < 2)
			setTimeout(function() { window.location=day_url+"?date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);
		else if(date_period < 8)
			setTimeout(function() { window.location=calendar_url+"?week&date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);
		else if(date_period < 32)
			setTimeout(function() { window.location=calendar_url+"?month&date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);
		else	
			setTimeout(function() { window.location=calendar_url+"?date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);		
		
		return false;		
	} 
	
	/**/
	function eval_date_period(date_start, date_end)
	{
		return (date_end-date_start)/(1000*60*60*24);
	}
	
	/**/
	function fullfillPostPreviewForm()
	{
		document.getElementById('title').innerHTML = document.getElementById('post_title').value;
		document.getElementById('category_id_1').value = document.getElementById('category').value;
		document.getElementById('category_id_2').value = document.getElementById('category2').value;
		document.getElementById('comm_id_1').value = document.getElementById('community').value;
		document.forms.fm.action = "http://test.risk.ru/message_write_for_test.php";			
	} 
	
	/**/
	function addEventExtAndPost()
	{		
		if(!validateFormPost())
			return false;
		postAjaxAddEvent();
		setTimeout(function() { window.location="http://risk.ru/write/?title="+html('title').value+"&event="+html('event_id').value }, 1000);
		return false;			
	}
	
	/**/
	function showPostBody()
	{		
		document.getElementById('post_body').style.display = 'table-row';
	}
	
	/**/
	function hidePostBody()
	{
		document.getElementById('post_body').style.display = 'none';
	}
	
	/**/
	function showPostButton()
	{		
		document.getElementById('SaveAndPostButton').style.display = 'inline';
	}
	
	/**/
	function hidePostButton()
	{
		document.getElementById('SaveAndPostButton').style.display = 'none';
	}
	
	/**/
	function showSaveButton()
	{		
		document.getElementById('SaveButton').style.display = 'inline';
	}
	
	/**/
	function hideSaveButton()
	{
		document.getElementById('SaveButton').style.display = 'none';
	}
	
	/**/
	function radioLink(mode)
	{
		switch(mode)
		{
			case 'link':
				hidePostButton();
				hidePostBody();
				showSaveButton();
			break;
			
			case 'no_link':
				hidePostButton();
				showSaveButton();
				showPostBody();
			break;
			
			case 'post':
				hidePostBody();
				hideSaveButton();
				showPostButton();
			break;
			
			default:
			break;			
		}
	}	
	
	/**/
 	function cancel_event(event_id)
	{	
		// day_url = "http://test.risk.ru/calendar/day.php";
		// calendar_url = "http://test.risk.ru/calendar/calendar.php";
		day_url = "http://risk.ru/events/day/";
		calendar_url = "http://risk.ru/events/calendar/";
		
		
		if(confirm('Вы действительно хотите отменить событие?'))
		{
			var aData={
					iEventId:event_id,
					iActionId:'7'
					};
			
			var aInput = html('event_id');		
			postAjax("/inc/site/ajax/write_event_action.php",aData,aInput);
			
			date_start = new Date(html('start_year').value,html('start_month').value,html('start_day').value);
			date_period = eval_date_period(date_start, new Date(html('end_year').value,html('end_month').value,html('end_day').value));
					
			if(date_period < 2)
				setTimeout(function() { window.location=day_url+"?date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);
			else if(date_period < 8)
				setTimeout(function() { window.location=calendar_url+"?week&date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);
			else if(date_period < 32)
				setTimeout(function() { window.location=calendar_url+"?month&date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);
			else	
				setTimeout(function() { window.location=calendar_url+"?date="+date_start.getFullYear()+'-'+(date_start.getMonth()+1)+'-'+date_start.getDate() }, 1000);		
			
			return false;		
		}
	} 
