var num_photo = 0;
var num_file = 0;

function Check_Register_Form()
{
	id_name = $('#id_name').val();
	id_desc = $('#id_desc').val();
	id_address = $('#id_address').val();
	id_phone = $('#id_phone').val();
	
	if(id_name == '')
	{
		alert('Не заполнено Название организации');
		return false;
	}
	if(id_desc == '')
	{
		alert('Не заполнено Описание деятельности');
		return false;
	}
	if(id_address == '')
	{
		alert('Не заполнен Адрес');
		return false;
	}
	if(id_phone == '')
	{
		alert('Не заполнено Телефоны');
		return false;
	}
	return true;
}

function Register_Add_Photo()
{
	if(num_photo == 20)
	{
		alert('Более 20 фотографий добавить нельзя.');
		return false;
	}
	num_photo++;
	$('#photos').append('<p>'+num_photo+'.&nbsp;<input type="file" name="photo['+num_photo+']" /></p>');
}

function deletePhoto(id_photo)
{
	$.ajax({
	   type: "GET",
	   url: "/my-page-delete-photo?id="+id_photo,
	   data: "ajax=1",
	   success: function(data){
	     if(data == 1)
	     {
	     	$('#photos_'+id_photo).empty();
	     }
	   }
	 });
}

function Register_Add_File()
{
	if(num_file == 10)
	{
		alert('Более 10 файлов добавить нельзя.');
		return false;
	}
	num_file++;
	$('#files').append('<p>'+num_file+'.&nbsp;<input type="file" name="file['+num_file+']" /></p>');
}

function Register_Add_File2()
{
	if(num_file == 10)
	{
		alert('Более 10 файлов добавить нельзя.');
		return false;
	}
	num_file++;
	$('#files').append('<p>'+num_file+'.&nbsp;<input type="file" name="file['+num_file+']" />');
	$('#files').append('Название: <input type="text" name="file_title['+num_file+']" value="" /></p>');
	
}

function deleteFile(id)
{
	$.ajax({
	   type: "GET",
	   url: "/my-page-delete-file?id="+id,
	   data: "ajax=1",
	   success: function(data){
	     if(data == 1)
	     {
	     	$('#files_'+id).empty();
	     }
	   }
	 });
}

function submitOnEnter(event, formElem)
{
	if((event.keyCode == 0xA)||(event.keyCode == 0xD))    
	{
		document.login.submit();
	}
}

function checkAddTender()
{
	id_cat = $('#id_cat').val();
	
	title = $('#title').val();
	
	var oFCKeditor = FCKeditorAPI.GetInstance($('#descr').attr('name')) ;
	descr = oFCKeditor.GetXHTML( true );
	
	tender_start_year = $('#tender_start_year').val();
	tender_start_month = $('#tender_start_month').val();
	tender_start_day = $('#tender_start_day').val();
	
	tender_finish_year = $('#tender_finish_year').val();
	tender_finish_month = $('#tender_finish_month').val();
	tender_finish_day = $('#tender_finish_day').val();
	
	$.post("/check-create-edit-tender", 
	{
		ajax: '1',
		id_cat: id_cat,
		title: title,
		descr: descr,
		tender_start_year: tender_start_year,
		tender_start_month: tender_start_month,
		tender_start_day: tender_start_day,
		tender_finish_year: tender_finish_year,
		tender_finish_month: tender_finish_month,
		tender_finish_day: tender_finish_day
	 },
		function(data){
			data = data.split('|');
     		o_error = 0;
     		for(var i in data)
     		{
     			if(data[i] != '')
     			{
     				o_error = 1;
     				$('#'+data[i]).addClass("red");
     			}
     		}
     		if(o_error == 0)
			{
				document.tender.submit();
			}
			else
			{
				return false;
			}
     	});
}

function checkAddOffer()
{
	id_cat = $('#id_cat').val();
	id_parent_cat = $('#id_parent_cat').val();
	
	title = $('#title').val();
	//descr = $('#descr').val();
	
	var oFCKeditor = FCKeditorAPI.GetInstance($('#descr').attr('name')) ;
	descr = oFCKeditor.GetXHTML( true );
	
	offer_start_year = $('#offer_start_year').val();
	offer_start_month = $('#offer_start_month').val();
	offer_start_day = $('#offer_start_day').val();
	
	offer_finish_year = $('#offer_finish_year').val();
	offer_finish_month = $('#offer_finish_month').val();
	offer_finish_day = $('#offer_finish_day').val();
	
	$.post("/check-create-edit-offer", 
	{
		ajax: '1',
		id_cat: id_cat,
		id_parent_cat: id_parent_cat,
		title: title,
		descr: descr,
		offer_start_year: offer_start_year,
		offer_start_month: offer_start_month,
		offer_start_day: offer_start_day,
		offer_finish_year: offer_finish_year,
		offer_finish_month: offer_finish_month,
		offer_finish_day: offer_finish_day
	 },
		function(data){
			data = data.split('|');
     		o_error = 0;
     		for(var i in data)
     		{
     			if(data[i] != '')
     			{
     				o_error = 1;
     				$('#'+data[i]).addClass("red");
     			}
     		}
     		if(o_error == 0)
			{
				document.offer.submit();
			}
			else
			{
				return false;
			}
     	});
}

function checkAddNews()
{
	title = $('#title').val();
	var oFCKeditor = FCKeditorAPI.GetInstance($('#text').attr('name')) ;
	text = oFCKeditor.GetXHTML( true );
	$.post("/check-create-edit-news", 
	{
		ajax: '1',
		title: title,
		text: text
	 },
		function(data){
			data = data.split('|');
     		o_error = 0;
     		for(var i in data)
     		{
     			if(data[i] != '')
     			{
     				o_error = 1;
     				$('#'+data[i]).addClass("red");
     			}
     		}
     		if(o_error == 0)
			{
				document.news.submit();
			}
			else
			{
				return false;
			}
     	});
}

$(document).ready(
	function(){
		$('dl.noscript').removeClass('noscript');

		$.fn.wait = function(time, type){
			
			time = time || 100;
			type = type || 'fx';
			
			return this.queue(type, function() {
	            var self = this;
	            setTimeout(function() {
	                $(self).dequeue();
	            }, time);
	        });
		};

	}
);

$(function(){
	$('dl.tabs dt').click(function(){
		$(this)
			.siblings().removeClass('selected').end()
			.next('dd').andSelf().addClass('selected');
	});
});

$(function(){
	$('.table-list tr').hover(
			function(){
				$(this).addClass('hover');
			},
			function(){
				$(this).removeClass('hover');
			}
	);
});

function commentArea()
{
	$('#comment-area').toggle();
}

function commentSubmit(id_news)
{
	var oFCKeditor = FCKeditorAPI.GetInstance($('#textarea').attr('name')) ;

	user_text = oFCKeditor.GetXHTML( true );
	
	$.post("/news-post-comment", 
	{
		ajax: '1',
		id_news: id_news,
		text: user_text
	 },
		function(data){
			data = data.split('|');
			status = data[0];
			data = data[1];
			if(status == 0)
			{
				$('#errors_out').html(data);
				$('#errors_out').show();
			}
			else if(status == 1)
			{
				$('#no-comment').hide();
				$('#comments-list').append(data);
				$('#comment-area').toggle();
				oFCKeditor.SetData('');
			}
     	});
}

var num_file = 0;
function Tenders_Add_File()
{
	if(num_file == 10)
	{
		alert('Более 10 файлов добавить нельзя.');
		return false;
	}
	num_file++;
	$('#files').append('<b>Файл '+num_file+':</b><br /><br />Введите название файла:&nbsp;<input type="text" name="file_title['+num_file+']" /><br />');
	$('#files').append('Выберите файл:&nbsp;<input type="file" name="file['+num_file+']" /><br /><br />');
}

function DeleteTender(id)
{
	$.ajax({
	   type: "GET",
	   url: "/delete-tender-file",
	   data: "ajax=1&id="+id,
	   success: function(data){
	     if(data == 1)
	     {
	     	$('#file_have_'+id).empty();
	     }
	   }
	 });
}

$(function(){
	$('#new-users, #leaders').accordion({
		active: false,
		autoHeight: false,
		event: 'mouseover'
	});
});
