// JavaScript Document
function doTalk(feedKey,html)
{
 if ($('#divcom'+feedKey).length>0)
 {
  return;
 }
 var fd=$('#fd'+feedKey)[0];
 if (fd!=null)
 {
  var ohtml=$(decodeUrl(html));
  ohtml.appendTo(fd);
 }
}
function printTalk(feedKey,html)
{
 var fd=$('#fd'+feedKey)[0];
 if (fd!=null)
 {
  var ohtml=$(decodeUrl(html));
  ohtml.appendTo(fd);
 }
}

function decodeUrl(ss) {
  var encoded =ss;
  return decodeURIComponent(encoded.replace(/\+/g,  " "));
}
function cancelTalk(feedKey)
{
 if ($('#divcom'+feedKey).length>0)
 {
  $('#divcom'+feedKey).remove();
 }
}
function postTalk(feedKey)
{
 if ($('#divcom'+feedKey).length>0)
 {
  var ss=$('#txtcom'+feedKey)[0].value;
  if (ss!='')
  {
    xajax_saveTalk(feedKey,ss);
    $('#divcom'+feedKey).remove();
  }
 }
}
function hideFeed(feedKey)
{
 var id='divhide'+feedKey;
 if ($('#'+id).length>0)
 {
  var chkIdent=$('#chkident'+id)[0].checked;
  var chkAddress=$('#chkaddress'+id)[0].checked;
  var chkType=$('#chktype'+id)[0].checked;
  xajax_saveHiding(feedKey,chkIdent,chkAddress,chkType);
 }
}

function removeTalk(feedKey,talkId)
{
 if ($('#divtalked'+talkId+feedKey).length>0)
 {
  $('#divtalked'+talkId+feedKey).remove();
 }
}
function addContent(contentId,addToId,html,singleInstance)
{
 if ($('#'+contentId).length>0)
 {
  if (singleInstance)
  {
   return;
  }
 }
 var addTo=$('#'+addToId)[0];
 if (addTo!=null)
 {
  var ohtml=$(decodeUrl(html));
  ohtml.appendTo(addTo);
 }
}
function removeContent(id)
{
 if ($('#'+id).length>0)
 {
  $('#'+id).remove();
 }
}
/**
 Pager functions
*/
function onPagerClick(pageNo,urlParam)
{
 var xx=document.getElementById(urlParam);
 xx.value=pageNo;
 $('form').submit();
}

/**
 SimpleList Class
*/
function simpleList(id,container)
{
 this.id=id;
 this.container=container;
 if (typeof(container)=='object')
 {
  this.cont=$(container)[0];
 }
 else{this.cont=$('#'+container)[0];}
 // create ul inside container
 var xx=$('#'+id)[0];
 if (xx==undefined)
 {
  this.ulist=$('<ul/>').attr({"id":id,"class":"simpleList"});
  this.ulist.appendTo(this.cont);
 }
 else
 {this.ulist=xx;}
 /**
  function add to add new item
 */
 this.add=function(value,title)
 {
  var item=$('#'+value)[0];
  if (item!=undefined)
  {
   return false;
  }
  //var li=$('<li>'+title+'</li>').attr({"id":value,"value":value,"class":"simpleList-item","onclick":"sl_removeItem('"+id+"','"+value+"');"});
  var li=$('<li>'+title+'</li>').attr({"id":value,"value":value,"class":"simpleList-item"});
  li.bind('click',function(){sl_removeItem(id,value);});
  li.appendTo(this.ulist);
  sl_buildIds(id);
  return true;
 }
 /**
  function to load from json
 */
 this.loadFromJson=function(jsonData)
 {
  for(var i=0;i<jsonData.length;i++)
  {
    var item=$('#'+jsonData[i].value)[0];
    if (item!=undefined)
    {
     return false;
    }
    var li=$('<li>'+jsonData[i].title+'</li>').attr({"id":jsonData[i].value,"value":jsonData[i].value,"class":"simpleList-item","onclick":"sl_removeItem('"+this.id+"','"+jsonData[i].value+"');"});
    li.appendTo(this.ulist);
  }
  sl_buildIds(this.id);
  return true;
 }
}
function sl_removeItem(ulId,itemId)
{
 $('#'+itemId).remove();
 sl_buildIds(ulId);
}
function sl_buildIds(id)
{
 var users=[];
 $('#'+id+' li').each(function(index) {
 var u={"value":$(this).attr('value'),"title":$(this).text()};
 users.push(u)
 });
 if (users.length<=0)
 {
  var usersJson='';
 }
 else
 {
  var usersJson=encodeUrl(JSON.stringify(users));
 }

 var hf=$('#'+id+'_hf')[0];
 if (hf==undefined)
 {
  var hfName=id+'_hf';
  var html='<input id="'+hfName+'" type="hidden" name="'+id+'" value="'+usersJson+'"/>';
  $('form').append(html);
 }
 else
 {
  $('#'+id+'_hf').attr("value",usersJson);
 }
}
/* end SimpleList Class and functions*/

