function ShowSettingsTab(a_id) {
	var id = a_id.replace('link_','');
	$('a[id^=link_]').each(function(){this.style.fontWeight = 'normal'});
	$('div[id^=tab_]').each(function(){this.style.display = 'none'});

	document.getElementById(a_id).style.fontWeight = 'bold';
	document.getElementById('tab_'+id).style.display = 'block';
}


function Validate(frm) {

	var ok = true;
	frm.find('.validate').each(function() {
		var params = new Object;
		eval ('params = '+$(this).attr('rel'));
		if (params.min_length==undefined) params.min_length = 0;
		if (params.max_length==undefined) params.max_length = Infinity;

//		var value = this.tagName=='INPUT' ? $(this).val() : $(this).val();
		var value = this.value;

		var err_msg = params.error;
		if (params.req || value!='') {
			switch (params.type) {
				case 'text': case 'list_select':
						ok = true;
				break;
				case 'list_select_parent': case 'list_select_children':
						var o_val = $(this).val();
						if (o_val == ""){
							ok = false;
						}
				break;
				case 'list_radio':
					if (!$(this).attr('checked') && !$(this).siblings('input[name='+$(this).attr('name')+']').attr('checked'))
						ok = false;
				break;
				case 'email':
					if (!value.match(/^[\w\-\.]+\@[\w\-\.]+\.[a-z]{2,5}$/i)) {
						ok = false;
					}
				break;
				case 'text_eng':
					if (!value.match(/^([a-z_0-9]+)$/i)) {
						ok = false;
					}
				break;
				case 'datetime':
					//this regexp can be advanced with control 0-31 0-12 etc
					if (!value.match(/^[\d]{1,4}[\-\.\/][\d]{1,2}[\-\.\/][\d]{1,4}(([\s\-]+[\d]{1,2}:[\d]{2})?)$/)) {
						ok = false;
					}
				break;
				case 'date':
					//this regexp can be advanced with control 0-31 0-12 etc
					if (!value.match(/^[\d]{2,4}-[\d]{1,2}-[\d]{1,2}$/g)) {
						ok = false;
					}
				break;
				case 'decimal':
					if (!value.match(/^([0-9\.]+)$/g)) {
						ok = false;
					}
				break;
				case 'int':
					if (!value.match(/^([0-9]+)$/g)) {
						ok = false;
					}
				break;
			}
			if (value.length < params.min_length || value.length > params.max_length)
				ok = false;
		}


		$(this).siblings('span.err_val').remove();
		$(this).removeClass('inp_text_err');

		if (!ok) {
			$(this).parents(':first').append('<span class="err_val"><i></i>'+err_msg+'</span>');
			$(this).addClass('inp_text_err');
			//$(this).parents('form:first').find('[id^=save_]').hide();
			$(this).select();
			return false;
		} else {
			//$(this).parents('form:first').find('[id^=save_]').show();
		}
	});
	return ok;
}

$(document).ready(function(){
	//actions with bottom select
	$('.mainCheck').click(function(){
		$('.tr_check').attr('checked','checked');
		$('input#mainCheck').attr('checked','checked');
	});

	$('.mainUnCheck').click(function(){
		$('.tr_check').attr('checked','');
		$('input#mainCheck').attr('checked','');
	});

	//!!!!
	$('#mainCheck').click(function(){
		if ($(this).attr('checked'))
			$('.mainCheck').trigger('click');
		else
			$('.mainUnCheck').trigger('click');
	});

	$('.tr_check').click(function(){
		if (!$(this).attr('checked'))
			$('input#mainCheck').attr('checked','');
	});

	$('select[name=do_select]').change(function(){
		if (this.value!='0' && $('.tr_check').attr('checked')) {
			var str = 'module='+$('#module_prefix').val()+'&ID_Block='+$('#ID_Block').val()+'&act='+this.value+'&ids=';
			$('.tr_check').each(function(){
				if (this.checked) str += this.value+',';
			});
			str = str.slice(0,-1);
			show_message('Обработка данных');
			jQuery.ajax({
				type: "POST",
				url: "/_admincp/_core/Modules/_ajax/check_action.php",
				data: str,
				success: function(msg){
					window.location.reload();
				}
			});
		}
	});

	//-----------------------


	//insert error msg into the end of current td
	/*
	$('.validate').change(function() {
		Validate(this);
	});
	*/

	$('a[id^=link_]').click(function() {
		$('a[id^=link_]').css('font-weight','normal');
		$('div[id^=tab_]').hide();

		var id = this.id.replace('link_','');

		$(this).css('font-weight','bold');
		$('div#tab_'+id).show();
	});

	$('#save_module').click(function(){
		var frm = $(this).parents('form:first');
		if (Validate(frm)) {
			if (frm.attr('method')=='post') {
				frm.submit();
			} else {
				var a = $(this).parents('.NodeParent:first').attr('id').match(/(.*?)_([0-9]+)$/);
				var module = a[1];
				var id = a[2];

				try { tinyMCE.triggerSave(); } catch(err) {};

				var str = frm.find(':input').serialize();

				message_box(language.mb_processing,'/_admincp/_design/img/bar/loading.gif');

				$.ajax({
					type: "POST",
					url: '/_admincp/_core/_ajax/save.php',
					data: "id="+id+"&module="+module+"&"+str,
					success: function(msg){
						if (msg)
							eval(msg)
						else
							window.parent.parent.document.location.reload();
					}
				});
			}
		}
	});

//	$('a#back_on_close').click(function(){history.back(1)});

	$('#save_class').click(function(){
		var form = $(this).parents('form:first');
		var module = form.attr('name');

		try { tinyMCE.triggerSave(); } catch(err) {};

		if (Validate(form)) {
			//works for AdminTable!
			form.find('td.pos').each(function(i){
				var tr = $(this).parents('tr:first');
				if (tr.attr('id').match(/\d$/)) {
					var tbody_id = $(this).parents('tbody:first').attr('id').replace(/\d+$/,'');
					var id = tr.attr('id').replace(tbody_id,'');
					$(this).append('<input type="hidden" name="var['+id+'][Pos]" value="'+tr.attr('pos')+'">');
				}
			});

			if (form.attr('method')=='post') {
				form.submit();
			} else {
				var str = form.find(':input').serialize();
				var dir = module.match(/^[A-Z]/) ? 'Libraries' : 'Modules';
				var script = form.attr('id');
				$.ajax({
					type: "POST",
					url: '/_admincp/_core/'+dir+'/'+module+'/ajax/'+script+'.php',
					data: str,
					success: function(msg){
						if (msg) eval(msg);
					}
				});
			}
			form.find('input[type=hidden][name$=][Pos]]').remove();
		} else return false;
	});
});
