window.cooID = window.coo.get('cooid','/');

function $id(id)
{
	return document.getElementById(id);
}

function auth(f)
{
    (new CAjax(true)).req( {'method':f.method,'url':f.action,'t':f.type.value,'l':f.login.value,'p':f.pwd.value}, function(q){
       if (q.selectSingleNode('/user/name'))
       {
           $id('user_info').style.display = 'inline';
           $id('user_name').innerHTML = xText(q.selectSingleNode('/user/name'));
           $id('user_name').href = xText(q.selectSingleNode('/user/url'));
           $id('user_image').src = xText(q.selectSingleNode('/user/image'));
           $id('user_tp').src = '/img/'+xText(q.selectSingleNode('/user/type'))+'.gif';
		   $id('0_comment_form').style.display = 'block';
		   $id('login_comment_form').style.display = 'none';
		   window.cooID = xText(q.selectSingleNode('/user/coo'));
		   window.coo.set('cooid',window.cooID,'/');
       }
       else
       {
           alert('Неверный логин/пароль');
           $id('comm_login').focus();
           $id('comm_login').select();
       }
    });
    return false;
}

function regUser(tp,n,u,i)
{
    if (window.cooID) return;
    (new CAjax(true)).req( {'method':'post','url':'/comm/auth/'+tp,'n':n,'u':u,'i':i}, function(q){
       tp = xText(q.selectSingleNode('/user/type'));
       if (tp)
       {
           $id('user_info').style.display = 'inline';
           $id('user_name').innerHTML = xText(q.selectSingleNode('/user/name'));
           $id('user_name').href = xText(q.selectSingleNode('/user/url'));
           $id('user_image').src = xText(q.selectSingleNode('/user/image'));
           $id('user_tp').src = '/img/'+tp+'.gif';
		   $id('0_comment_form').style.display = 'block';
		   $id('login_comment_form').style.display = 'none';
		   window.cooID = xText(q.selectSingleNode('/user/coo'));
		   window.coo.set('cooid',window.cooID,'/');
       }
    });
}

function commExit()
{
   if (confirm('Выйти?'))
   {
       $id('user_info').style.display = 'none';
       $id('user_image').src = '/img/nouser.gif';
       window.coo.set('cooid','','/');
       window.cooID = false;
	   $id('login_comment_form').style.display = 'block';
	   $id('0_comment_form').style.display = 'none';
	   var el = $id('page_comments').getElementsByTagName('DIV');
	   var len = el.length;
	   for(var i=0;i<len;i++)
	   {
           if (!el[i].id) continue;
	   	   var ss = el[i].id.split('_');
	   	   if (ss.length==2 && ss[1]=='form') el[i].style.display = 'none';
	   }
   }
}

function postComment(f)
{
	if (!window.cooID)
	{
		$id('0_comment_form').style.display = 'none';
		$id('login_comment_form').style.display = 'block';
		$id('comm_login').focus();
		return false;
	}
	else
	{
  	    if (!f) return;
  	    var val = trim(f.txt.value);
  	    if (val.length==0 || val=='Добавить комментарий')
  	    {
  	    	f.txt.focus();
  	    	return false;
  	    }


  	    var id = f.action.split('/');
  	    var len = id.length;
  	    for(var i=0;i<len;i++)
  	    {
  	    	if(id[i]=='comm')
  	    	{
  	            var vid = id[i+1];
        	    id = id[i+3];
  	    		break;
  	    	}
  	    }

  	    if(id>0) $id(id+'_form').innerHTML = '<img src="/img/load.gif" alt="Загрузка..." title="Загрузка..."/>';
  	    else
  	    {
  	        $id('0_comment_form').style.display = 'none';
            $id('comments').innerHTML = '<img src="/img/load.gif" alt="Загрузка..." title="Загрузка..."/>';
  	    }
  	    (new CAjax()).req( {'method':f.method,'url':f.action,'t':val}, function(q){
            if (q)
            {
                $id(id+'_form').style.display = 'none';
                $id(q+'_comments').style.display = 'none'
                loadComments(vid,q);
            }
            else
            {
                $id('0_comment_form').getElementsByTagName('TEXTAREA')[0].value = '';
                $id('0_comment_form').style.display = 'block';
                loadMess(f.id.value);
            }
  	    });
	}
	return false;
}

function commentForm(id)
{
	if (!window.cooID)
	{
		$id('0_comment_form').style.display = 'none';
		$id('login_comment_form').style.display = 'block';
		$id('comm_login').focus();
    }
    else if($id(id+'_form').style.display == 'none')
    {
    	var h = $id('0_comment_form').innerHTML;
    	h = h.replace('post/0/','post/'+id+'/');
    	$id(id+'_form').innerHTML = h;
    	$id(id+'_form').style.display = 'block';
    	$id(id+'_form').getElementsByTagName('TEXTAREA')[0].focus();
    }
    else $id(id+'_form').style.display = 'none';
	return false;
}

function loadMess(id)
{
    (new CAjax()).req('/comm/'+id+'/', function(q){
        $id('comments').innerHTML = q;
    });
}

function loadComments(vid,id)
{
    if($id(id+'_comments').style.display=='none')
    {
    	$id(id+'_comments').innerHTML = '<img src="/img/load.gif" alt="Загрузка..." title="Загрузка..."/>';
    	$id(id+'_comments').style.display = 'block';
        (new CAjax()).req('/comm/'+vid+'/comments/'+id+'/', function(q){
            $id(id+'_comments').innerHTML = q;
            $id(id+'_comments').style.display = 'block';
            var cnt = 0;
            var el = $id(id+'_comments').getElementsByTagName('DIV');
            var len = el.length;
            for(var i=0;i<len;i++) if(el[i].id.indexOf('_form')>0) cnt++;
            $id(id+'_comments_count').innerHTML = cnt;
    });
    }
    else $id(id+'_comments').style.display='none';
    return false;
}

function commEmailForm(s)
{
    if (s===false) $id('email_comment_form').style.display = 'none';
	else $id('email_comment_form').style.display = 'block';
}

function commSaveEmail(s)
{

    if (!s) $id('comm_email').value = '';
    (new CAjax()).req('/comm/1/email/'+$('td.notify:first input[name="vid"]').val()+'/'+$('td.notify:first input[name="n_type"]:checked').val()+'/'+$id('comm_email').value+'/', function(q){
        if((trim(q)).length==0)
        {
            alert('Email адрес указан неверно');
            $id('comm_email').focus();
            $id('comm_email').select();
        }
        else
        {
            alert(q);
            commEmailForm(false);
        }
    });
}


