<?php

require_once '../incfiles/core.php';
require_once '../incfiles/func.php';
require_once '../incfiles/auth.php';
require_once '../incfiles/user.php';

if (!empty($_GET['id']) && is_numeric($_GET['id']))
{
	$id = abs(intval($_GET['id']));
	$result = mysql_query("SELECT * FROM `users` WHERE `id` = '".$id."' LIMIT 1");
	if (mysql_num_rows($result) == true) $arr_user = mysql_fetch_array($result);
		else go(URL);
}
else go(URL);

if (mysql_result(mysql_query("SELECT COUNT(`id`) FROM `ask` WHERE `user_id` = '".$arr_user['id']."' LIMIT 1"),0) == false) mysql_query("INSERT INTO `ask` SET `user_id` = '".$id."', `time` = '".time()."'");
$arr_ask = mysql_fetch_array(mysql_query("SELECT * FROM `ask` WHERE `user_id` = '".$id."' LIMIT 1"));

$title = $arr_user['name'].' '.$arr_user['surname'].' :: '.lang('Спроси меня','Запитай мене');
require_once '../incfiles/head.php';
require_once 'handler/del_ask.php';

echo user_access_page($arr_user);

	echo '<div class="title">
	<a href="'.URL.'/?id='.$id.'" onclick="Page.Go(this.href); return false">'.$arr_user['name'].' '.$arr_user['surname'].'</a>
	<a class="title_a_hover" href="'.URL.'/ask/?id='.$id.'" onclick="Page.Go(this.href); return false">'.lang('Спроси меня','Запитай мене').'</a>
	'.($user_id == $id?'<a href="'.URL.'/ask/ask.php" onclick="Page.Go(this.href); return false">'.lang('Новые вопросы','Нові запитання').' '.$arr_ask['count_new'].'</a>':'').'
	</div>';

	if (!empty($user_id) && $user_id != $arr_user['id'] && $arr_ask['access'] != 3)
	{
		require_once 'handler/new_ask.php';
	
		?>
		<script type="text/javascript">
		$(function(){
			$('#form').on('submit', function(e){
			$('#form_content').html('');
			Page.Loading('start');
 		    e.preventDefault();
		    var $that = $(this),
		        formData = new FormData($that.get(0));
		    $.ajax({
		      url: $that.attr('action'),
   		     type: $that.attr('method'),
		      contentType: false,
		      processData: false,
		      data: formData,
		      success: function(response){
 		       if(response){
		 			$('.content').html(response);
					Page.Loading('stop');
		      }
		    }
		    });
		  });
		});
		</script>
		<?
	
		echo '<div class="block">
		<form id="form" action="'.URL.'/ask/?id='.$id.'" method="post">
		<input id="textarea" name="ask" class="input" maxlength="500" value=""/>
		<input class="button2" type="submit" value="'.lang('Спросить','Запитати').'" /> ';
		if ($arr_ask['access'] == 2) echo '<img style="margin:3px;" src="'.URL.'/design/img/checkbox_off.gif"/> '.lang('Анонимно','Анонімно');
		else echo '<a id="checkbox_on" href="" onclick="Form.Checkbox_off(); return false"><img style="margin:3px;" src="'.URL.'/design/img/checkbox_on.gif"/> '.lang('Анонимно','Анонімно').'</a>
		<a id="checkbox_off" href="" style="display:none;" onclick="Form.Checkbox_on(\'anonim\'); return false"><img style="margin:3px;" src="'.URL.'/design/img/checkbox_off.gif"/> '.lang('Анонимно','Анонімно').'</a>
		<div id="checkbox"><input type="hidden" name="anonim" value="1"/></div>';
		echo '</form></div>';
	}
	
	echo '<div id="content_comm"><span>';
	
	$all = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `ask_msg` WHERE `ask_id` = '".$arr_user['id']."' AND `new` = 0 LIMIT 1"),0);
	if ($all > 0)
	{
		$cp = ceil($all/10);
		$p = (isset($_GET['p']) and is_numeric($_GET['p']) and $_GET['p']>1 and $_GET['p']<=$cp) ? $_GET['p'] : 1;
		$start = $p*10-10;
		$res = mysql_query("SELECT * FROM `ask_msg` WHERE `ask_id` = '".$arr_user['id']."' AND `new` = 0 ORDER by `time` DESC LIMIT $start, 10");
		while ($arr = mysql_fetch_array($res))
		{
			echo '<div class="block">';
			if (!empty($user_id) && $user_id == $id) echo '<div class="right"><a href="'.URL.'/ask/?id='.$user_id.'&del='.$arr['id'].'" onclick="Form.delPost(this.href); return false"><img src="'.URL.'/design/img/closed.png"/></a></div>';
			if ($arr['anonim'] == 1) echo '<b>'.lang('Аноним','Анонім').'</b>'; else echo user_name($arr['user_id']);
			echo '<br/>'.vtime($arr['time']).'<ul><div class="quote">'.smiles($arr['text']).'</div></ul>
			<ul>'.smiles($arr['re_text']).'</ul>
			</div>';
		}
		navi($p,$cp,'/ask/?id='.$arr_user['id'].'&');
	}
	
	echo '</span></div>';
	
require_once '../incfiles/foot.php';

?>