$(document).ready(function(){

$('#mesage').focus(function() {test_logon();});
$('#mesage').blur(function() {test_mesage();});
$('#reload').click(function() {reload_code();});
$('#gfx_check').keyup(function() {check_code();});
$('#send').click(function() {validate();}); 
//$('#send').click(function() {ok_on();}); 
});


//----------------------------------------------------- obrazki ------------------------------------------------------------
function get_big_foto(foto){
$('#big_photo').attr("src",foto);
}
//------------------------------------------------------ ilość znaków --------------------------------------------------------------
function count_har(ile_znak){
	if(ile_znak<1001){
	c=1000-ile_znak;
	return c;
	}else{
	return 1000;
	}
}
//---------------------------------------------------------- test email--------------------------------------------------------
function test_logon(){
	$.post("odpowiedz.php?ajax&valid=logon",{"fpn":$('#user_id').val()},
		function(obj){
			if(obj == 1){
			$('#r_mesage').html('');
			}else{
			//alert(obj);
			$('#r_mesage').html(obj);
			}	
		},'text/plain'
	);
}
//------------------------------------------------------- test opisu -------------------------------------------------------
function test_mesage(){
	$.post("odpowiedz.php?ajax&valid=mesage",{"fpn":$('#mesage').val()},
		function(obj){
			if(obj == 1){
			$('#r_mesage').html('');
			}else{
			$('#r_mesage').html(obj);
			}		
		},'text/plain'
	);
}
//--------------------------------------------- sprawdzenie poprawności kodu ----------------------------
function code_validate(){
	$.post("odpowiedz.php?ajax&valid=heck_code",{"rand" : $('#rand_num').val(),"hcode" : $('#gfx_check').val()},
		function(obj){
			if(obj==1){
				$('#img_rcode').css('display','none');
				$('#img_rcode_true').css('display','');
				$('#img_rcode_false').css('display','none');			
			}else{
			test_code=false;
				$('#img_rcode').css('display','none');
				$('#img_rcode_true').css('display','none');
				$('#img_rcode_false').css('display','');			
			}
		},'text/plain'
	);			
}
//-------------------------------------- zliczanie znaków kodu ----------------------------------------------
function check_code(){
	if($('#gfx_check').val().length>=6){
	code_validate();
	}
}
//--------------------------------------------- przeładowanie obrazka ------------------------------------
function reload_code(){
	$.post("odpowiedz.php?ajax&valid=random_num",{"fpn" : ''},
		function(obj){
			$('#rand_num').val(obj);
			rys = new Image();
			rys.src = "fotoimg/code.php?random_num="+obj;
			$('#image_gfx_check').attr("src",rys.src);
			$('#gfx_check').val('');
			$('#img_rcode').css('display','');
			$('#img_rcode_true').css('display','none');
			$('#img_rcode_false').css('display','none');			
		},'text/plain');
}
//------------------------------------------- pole eror -----------------------------------------
function eror_on(mesage)
{
$('#div_eror').html(mesage);
$('#div_eror').css('display','');
setTimeout("$('#div_eror').css('display','none')",2000);
}
function ok_on()
{
$('#div_ok').css('display','');
setTimeout("$('#div_ok').css('display','none')",2000);
}
function reset_form()
{
$('#email').val('');
$('#mesage').val('');
reload_code();
$('#gfx_check').val('');
}
//---------------------------------------------------- walidacja ------------------------------------------------------------
function validate(){
	if( ($('#mesage').val()!="") || ($('#gfx_check').val()!="") )
	{
		$.post("odpowiedz.php?ajax&write", $("#frm_mesage").serialize(),
		function(obj)
		{
			if(obj=='ok')
			{
			reset_form();
			ok_on();
			}
			else
			{
			eror_on('WYPEŁNIJ PRAWIDŁOWO WSZYSTKIE POLA<br /><br />'+obj);			
			}
		},'text/plain');	
	}
	else
	{
	eror_on('WYPEŁNIJ PRAWIDŁOWO WSZYSTKIE POLA');
	}
}
