function sendStdForm(form) {
	$(form).find('.errorHeading').remove();
	$(form).find('.errorText').remove();
	$(form).find('.errorList').remove();
	$(form).find('.resultText').remove();
	$(form).find('.resultList').remove();
	var Fv=$(form).find('input:file').val();
	if(Fv && Fv!=''){
		$(form).submit();
	}else{
	$.post ( form.action, $(form).serialize(), function ( data ) {
		//$(form).html(data);
		$(form).hide();
		$(form).html('');
		$(form).replaceWith(data);
		$(form).show();
		readyDoc();
	});
	}
}

function ajaxActionClick() {
	if ( this.href.match(/delete/) )
		if ( !confirm('Удалить эту запись?') )
			return false;
	var action = null;
	var parent = this;
	do {
		parent = parent.parentNode;
		if ( parent.className == 'ajaxReloadContainer' )
			action = $(parent).attr('rel');
		else
			action = parent.action;
	} while ( action == undefined );
	var form = parent;
	
	action = parent.className == 'ajaxReloadContainer' ? $(form).attr('rel') : form.action;
	action1 = action.replace ( /index.php$/gi, '' );
	//alert(action);
	
	if( action == undefined ) return true;	
	var path = window.location.pathname + window.location.search;
	var sendToRequest = '';
	if ( action != this.href && action != path && action1 != this.href && action1 != path )
		sendToRequest = action;
	//alert(sendToRequest);
	$.post ( this.href, {'_ajaxSendToRequest':sendToRequest}, function(data){
		//alert(data);
		//$(form).html(data);
		$(form).replaceWith(data);
		readyDoc();
	});
	return false;
}

function decorateSortForm() {
	var list = $('select[rel=sel_list]');
	var dir = $('select[rel=sel_dir]');
	
	if ( !list.attr('value') || list.attr('value') == 'По умолчанию' )
		dir.attr('disabled', true);
	else
		dir.attr('disabled', false);
}

function createWysiwyg(o) {
	var sBasePath = '/bitrix/php_interface/ExBitrix/public/fck/';

	var oFCKeditor = new FCKeditor( o.id ) ;
	oFCKeditor.BasePath	= sBasePath;
	// Set the custom configurations file path (in this way the original file is mantained).
	oFCKeditor.Config['CustomConfigurationsPath'] = sBasePath + 'myconfig.js' ;
	oFCKeditor.Height = 400;
	oFCKeditor.Width = '100%';
	//oFCKeditor.Value = 'This is some [b]sample text[/b]. You are using [url=http://www.fckeditor.net/]FCKeditor[/url].' ;
	oFCKeditor.ReplaceTextarea();
}



function readyDoc() {
	$('a[rel=ajaxAction]').bind('click', ajaxActionClick);
	decorateSortForm();
	$('select[rel=sel_list]').bind('change', decorateSortForm);
	//Clinicssort_list
	
	//$('#messages_to').bind ( 'keyup', function() {
	//	registerUserAction('userUsersSelect');
	//});
	
	$('textarea[rel=wysiwyg]').each(function() {
		createWysiwyg(this);
	})
	
	if (typeof initUpload != 'undefined' && initUpload != undefined && initUpload != null )
		initUpload();
	
	$('#ufClinicSelect').bind ( 'keyup', function() {
		registerUserAction('userClinicSelect');
	});
	
	$('input[rel=date]').each ( function() {
		$(this).attachDatepicker({
			clearText: '<span>Очистить поле</span>', // Display text for clear link
			closeText: '<img src="/images/calendar/close.gif" alt="закрыть">', // Display text for close link
			currentText: '<span>сегодня</span>', // Display text for current month link
			prevText: '&#x3c;<span>пред</span>', // Display text for previous month link
			nextText: '<span>след</span>&#x3e;', // Display text for next month link
			yearRange: "2009:2015"
		});
	});
	
	
		/* Events calendar */
	$(".clnd_days li.active_event").hover(
		function(){
			$(this).find(".calendar_popup_event").show();
			$(this).css("z-index",150);
		},
		function(){
			$(this).find(".calendar_popup_event").hide();
			$(this).css("z-index",10);
		}
	);
	
	$(".month .click").click(
		function(){
			$(".months_popup").toggle();
			$(".year_popup").hide();
		}
	);

	$(".months_popup .close").click(
		function(){
			$(".months_popup").hide();
		}
	);
	$(".year .click").click(
		function(){
			$(".year_popup").show();
			$(".months_popup").hide();
		}
	);
	$(".year_popup .close").click(
		function(){
			$(".year_popup").hide();
		}
	);
}


$(document).ready ( function() {
	readyDoc();
	
	$('#ajaxLoader').ajaxStart(function() {
		$('#ajaxLoader').show();
	});
	$('#ajaxLoader').ajaxStop(function(){
		$('#ajaxLoader').hide();
	});

});


var sectionCheckboxes = new Array;
function SectionGroupSelect ( id, name ) {
	var rel = 'section_'+id;
	var curentBlock = new Array;
	var tmpSectionCheckboxes = new Array;
	
		/// сохраняем состояние всех чекбосов
	tmpSectionCheckboxes = sectionCheckboxes;
	$('input[type=checkbox]').each(function() {
		if ( this.name == name ) {
			if ( this.checked )
				tmpSectionCheckboxes[this.value] = true;
		}
	});
	
	var index = 0;
	$('input[type=checkbox]').each(function() {
		if ( this.name == name ) {
				/// get curent block checkboxes
			if ( $(this).attr('rel') == rel ) {
				this.disabled = false;
				this.checked = index == 0 || (sectionCheckboxes[this.value] != undefined && sectionCheckboxes[this.value]) ? true : false;
				tmpSectionCheckboxes[this.value] = this.checked;
				index++;
			} else {
				this.checked = false;
				this.disabled = true;
			}
		}
	});
	
	$("#tabs_content1").show();
	$('#tabs').find('li').removeClass('active');
	$('#clicker'+id).parents('li').addClass('active');
	
	var curentTab = 'tab'+id;
	$('.SelectionTabClass').each(function(){
		if ( this.id == curentTab ) {
			$(this).show();
		} else {
			$(this).hide();
		}
	});
	
	sectionCheckboxes = tmpSectionCheckboxes;
}


function reloadCaptcha() {
	$.get('/bitrix/php_interface/ExBitrix/public/captcha.php', {}, function(data) {
		$('img[alt=CAPTCHA]').attr('src', '/bitrix/tools/captcha.php?captcha_sid='+data);
		$('input[name=captcha_sid]').attr('value', data)
	});
}
