/******************************************************************
* Forum
*******************************************************************/

var rowId = '1';
var topicId;
var postPage = 1;
/** *****************************
 *  Init
 ***************************** **/
function initForum(){
  getForumList();

}

function initSubForum(id){
  getSubForumList(id);
}

function initForumID(id){
  topicId = id;
  getTopicPostList();
  if(isLogin == false) $("coment_form_form").disable();
}

/** *****************************
 *  Do Functions
 ***************************** **/
// Hide un Show limited topics under forum
function showTopicRecs(){
//      alert(ar[1]);
//      rowId = ar[1];
//      block = 'fblock_id_'+rowId;
//      $(block).style.display = 'none';

}

// Send Coment to forum
function doSendComment(){
  //if($('commentAgreeFlag').checked){ 
    addComent();
   $('newComent').value = '';
   //$('commentAgreeFlag').checked = false;
  //}
}

function doSaveAddTopic(){
  saveAddTopic();
}
 
/** *****************************
 *  AJAX Reaquests
 ***************************** **/       

 // Get Forum List
function getForumList()
{
  $("forumLoad_Content").innerHTML = loadingImg();
  var url = webLink +'forum/loadindex';
  var req = new Ajax.Request(url,{
      method: 'post',
      onSuccess: function(response){
        responseData = response.responseText;
        $("forumLoad_Content").innerHTML = responseData;
      }
    }
   );
}

 // Load Add Topic Form
function loadAddTopic(fId)
{
  var url = webLink +'forum/loadAddTopic/?id='+fId;
  var req = new Ajax.Request(url,{
      method: 'post',
      onSuccess: function(response){
        responseData = response.responseText;
        params = {width:600, id: 'add_topic', title:'Izveidot jaunu foruma tēmu'};
        new Dialog(responseData, params);
        
      }
    }
   );
}

 // Load Add Topic Form
function saveAddTopic()
{
  var url = webLink +'forum/addTopic';
  var req = new Ajax.Request(url,{
      parameters: $H({title : $('newTopicTitle').value
                      ,desc : $('newTopicDesc').value
                      ,forum_id : $('forumId').value}
                      ).toQueryString(),
      method: 'post',
      onSuccess: function(response){
        json = response.responseText.evalJSON();
        if(json.status == "OK"){
          getSubForumList($('forumId').value);
          closeDialog('add_topic_dialog');
        }
      }
    }
   );
}
/* ### */   
function getSubForumList(id, p)
{
  var page = (p)?p:1;
  var url = webLink +'forum/loadsub/?id='+id+'&page='+page;
  var req = new Ajax.Request(url,{
      method: 'post',
      onSuccess: function(response){
        responseData = response.responseText;
        $("forumLoad_Content").innerHTML = responseData;
      }
    }
   );
}

/* ### */   
function getTopicPostList(page)
{
  postPage = (page) ? page : postPage;
  $("forumLoad_Content").innerHTML = loadingImg();
  var url = webLink +'forum/loadtopicid/?id='+topicId;
  var req = new Ajax.Request(url,{
      method: 'post',
      parameters : {page : postPage, forum_id : topicId}, 
      onSuccess: function(response){
        responseData = response.responseText;
        $("forumLoad_Content").innerHTML = responseData;
      }
    }
   );
}

/* Add Coment to Topic */   
function addComent()
{
    var url = webLink +'forum/addComent';
    var req = new Ajax.Request(url,{
        parameters: $H({topic_id : $('topicId').value
                        ,text : $('newComent').value
                        ,agreeflag : /*$('commentAgreeFlag').checked*/ true}
                        ).toQueryString(),
        method: 'post',
        onSuccess: function(response){
          json = response.responseText.evalJSON(); 
          if(json.status == 'OK'){
            getTopicPostList(postPage);
          }
        }
      }
     );
}
