// show_hide function
function sh(field , is_block)
	{
		var show = (is_block) ? "block" : "none";
		$("#"+field).css("display", show);
	}

function onCancel()
	{
		$("#add_messages").text('Файл не был загружен. проверьте размер(<250кб) и формат');
	}
	
function onComplete()
	{	
		$("#add_messages").text('Файл был загружен');
	}
	
function redirect()
	{
		document.location = loc;
	}
	
$(document).ready(function(){ // инициализация после загрузки страницы
	
	$("#login").click(function(){
		sh("opacity" , 1);
		sh("login_layer" , 1);
	});
	
	$("#login_cancel").click(function(){
		sh("opacity" , 0);
		sh("login_layer" ,0);
	});
	
	$("#add").click(function(){
		sh("opacity" , 1);
		sh("add_layer" , 1);
	});
	
	$("#add_cancel").click(function(){
		sh("opacity" , 0);
		sh("add_layer" ,0);
	});
	
	$("#exit").click(function(){
		document.location = loc+'users/logout';
	});
	
	$("#login_submit").click(function(){
		$("#error").load(loc+'users/login' , {
											login: $("#l_login").val(),
											password: $('input[@name=l_password]').val()
												} , 
												function(){
													if($("#error").text() == '')
														{
															redirect();
														}
												});
	});
	
	
				
	if(	typeof(AjaxUpload) != 'undefined')
		new AjaxUpload('#upload', {

			  action: loc+'users/upload_foto/',
			  name: 'userfile',
			  autoSubmit: true,
			  responseType: false,			  
			  onChange: function(file, extension){
			  },			 
			  onSubmit: function(file, extension) {},	
			 onComplete: function(file, response) {
				if(response == 200)
					{
						onComplete();
					}
					else
						{
							onCancel();
						}
			  }, 
			  onCancel: function(file, response) {
				onCancel();
			  },
			});
	
	
	
	$("#add_submit").click(function(){
	
		$("#add_messages").load(loc+'users/add' , {
											login: $("#login_field").val(),
											password: $('input[@name=password]').val(),
											F: $("#F").val(),
											I: $("#I").val(),
											O: $("#O").val(),
											group: $("#group").val()
												} , 
												function(){
													if($("#add_messages").text() == '')
														{
															redirect();
														}
												});
									});
			
});

	
		
	
