function updateBox(box,updt)
{
    var options = { 
                     method : 'post'
                    ,parameters : 'box='+box+'&update='+updt
                    ,onSuccess: function(t)
                    {
                      jsonRaw = t.responseText;      
                      jsonContent = eval("(" + jsonRaw + ")");        
                      
                      if(jsonContent.output == '')
                      {
                        document.getElementById(box + '_content').innerHTML = '';
                        document.getElementById(box + '_icon').src = smf_images_url + '/expand.gif';
                      }
                      else
                      {
                        document.getElementById(box + '_content').innerHTML = jsonContent.output;
                        document.getElementById(box + '_icon').src = smf_images_url + '/collapse.gif';
                      }              
                    }
                  };
 
    new Ajax.Request(smf_scripturl+'?action=ajax&do=updateBox', options);  	    
    
    if( document.getElementById(box + '_icon').src == smf_images_url + '/expand.gif' || updt == false)
    {
      document.getElementById(box + '_content').innerHTML = ajax_loading();
      
    }    
}

function ajax_loading()
{
  return "<div style='width: 100%; padding-top: 5px; padding-bottom: 5px; text-align:center'><img src='" + smf_images_url + "/ajax_loading.gif' /><br /></div>";
}

function sortBy( column)
{
  var addtourl = '';
    if(column == dkporder)
    {
      if(ascdesc == 'DESC') { ascdesc = 'ASC';  }
      else { ascdesc = 'DESC'; }
    }
    else
    {
      if(column == "name") { ascdesc = 'ASC';  }
      else { ascdesc = 'DESC'; }
      dkporder = column;
    }
    if(ascdesc == 'DESC') { addtourl = '&DESC=1'; }
    else { addtourl = '&ASC=1'; }

    var options = { 
                     method : 'post'
                    ,parameters : 'order='+column+addtourl+'&tier='+tier
                    ,onSuccess: populateView
                  };
 
    new Ajax.Request(smf_scripturl+'?action=ajax&do=switchView', options);  	 

}

function switchTier(t,page)
{
  document.getElementById('dkp_list').innerHTML = ajax_loading();  
  
    var options = { 
                     method : 'post'
                    ,parameters : 'tier='+t+'&page='+page+'&current='+ current
                    ,onSuccess: function(t){
                      
              jsonRaw = t.responseText;      
                jsonContent = eval("(" + jsonRaw + ")");

  document.getElementById('dkp_list').innerHTML = jsonContent.output;
  current =  jsonContent.current;                        
                    }
                  };
 
    new Ajax.Request(smf_scripturl+'?action=ajax&do=switchTier', options);  	    
    
    if(t == "T1")
    {
      document.getElementById('T1').src = smf_images_url + '/dkp/ssc_tkon.gif';
      document.getElementById('T2').src = smf_images_url + '/dkp/mh_bt.gif';         
      document.getElementById('T3').src = smf_images_url + '/dkp/swp.gif';  
      tier = "T1";
    }
    else if(t == "T2")
    { 
      document.getElementById('T1').src = smf_images_url + '/dkp/ssc_tk.gif';
      document.getElementById('T2').src = smf_images_url + '/dkp/mh_bton.gif';         
      document.getElementById('T3').src = smf_images_url + '/dkp/swp.gif';        
      tier = "T2";
    }
    else if(t == "T3")
    { 
      document.getElementById('T1').src = smf_images_url + '/dkp/ssc_tk.gif';
      document.getElementById('T2').src = smf_images_url + '/dkp/mh_bt.gif';         
      document.getElementById('T3').src = smf_images_url + '/dkp/swpon.gif';     
      tier = "T3";
    }    
    else
    {
      document.getElementById('T1').src = smf_images_url + '/dkp/ssc_tk.gif';
      document.getElementById('T2').src = smf_images_url + '/dkp/mh_bt.gif';         
      document.getElementById('T3').src = smf_images_url + '/dkp/swp.gif';  
    }
    
    updateIcons();
}

function removeEqDKP(el)
{

   var name = Element.previous(el).innerHTML;

    var options = { 
                     method : 'post'
                    ,parameters : 'name='+name
                    ,onSuccess: function(t){
                      jsonRaw = t.responseText;
                      jsonContent = eval("(" + jsonRaw + ")");
                      document.getElementById('raid_options_'+jsonContent.name).innerHTML = jsonContent.output;                            
                    }
                  };
 
    new Ajax.Request(smf_scripturl+'?action=ajax&do=removeEqDKP', options);  	  
}

function addToTracker(el)
{
  var name = Element.next(el).innerHTML;
  if(el.src == smf_images_url + '/icons/delete.gif')
  {
    var options = { 
                     method : 'post'
                    ,parameters : 'name='+name
                    ,onSuccess: function(t){

                      el.src = smf_images_url + '/icons/last_post.gif';          
                      el.title = 'Add to tracker';                 
                      jsonRaw = t.responseText;
                      jsonContent = eval("(" + jsonRaw + ")");
                      document.getElementById('dkp_tracker').innerHTML = jsonContent.output;                      
                    }
                  };

    new Ajax.Request(smf_scripturl+'?action=ajax&do=deleteFromTracker', options);  	    	      
  }
  else
  {
    var options = { 
                     method : 'post'
                    ,parameters : 'name='+name
                    ,onSuccess: function(t){
                      el.src = smf_images_url + '/icons/delete.gif';          
                      el.title = 'Remove from tracker';   
                      jsonRaw = t.responseText;
                      jsonContent = eval("(" + jsonRaw + ")");
                      document.getElementById('dkp_tracker').innerHTML = jsonContent.output;                                       
                    }
                  };

    new Ajax.Request(smf_scripturl+'?action=ajax&do=addToTracker', options);  	  
  }
}

function switchView()
{
  document.getElementById('dkp_list').innerHTML = ajax_loading();   
    var options = { 
                     method : 'post'
                    ,parameters : 'current='+current+'&tier='+tier
                    ,onSuccess: populateView
                  };
 
    new Ajax.Request(smf_scripturl+'?action=ajax&do=switchView', options);  	  

    
}
function updateView()
{
    var options = { 
                     method : 'post'
                    ,parameters : 'current='+current+'&tier='+tier
                    ,onSuccess: function(t){
                      document.getElementById('setView').src = smf_images_url + '/icons/quick_sticky.gif';
                      document.getElementById('setView').title = 'Default view';                            
                    }
                  };
    
    new Ajax.Request(smf_scripturl+'?action=ajax&do=setView', options);  	  
    
    userset = current;
    usertier = tier;
}

function populateView(t)
{
  jsonRaw = t.responseText;
  jsonContent = eval("(" + jsonRaw + ")");
  
  document.getElementById('dkp_list').innerHTML = jsonContent.output;
  current =  jsonContent.current;
  
  updateIcons();

  //recreate sortable
  Sortable.create('dkp_list',{tag:'div', handle:'catbg', constraint:false, onUpdate : updateClassOrder });
}

function updateIcons()
{
  if((current != userset || tier != usertier) && document.getElementById('setView'))
  {
    document.getElementById('setView').src = smf_images_url + '/icons/quick_sticky_bw.gif';
    document.getElementById('setView').title = 'Set as default view';
  }
  else if(document.getElementById('setView'))
  {
    document.getElementById('setView').src = smf_images_url + '/icons/quick_sticky.gif';
    document.getElementById('setView').title = 'Default view';
  }
    
}

function updateClassOrder()
{
  if(document.getElementById('dkp_list'))
  {
    var options = { 
                     method : 'post'
                    ,parameters : Sortable.serialize('dkp_list')
                  };
  }
  else if(document.getElementById('raid_list'))
  {
    var options = { 
                     method : 'post'
                    ,parameters : Sortable.serialize('raid_list')
                  };    
  } 
    new Ajax.Request(smf_scripturl+'?action=ajax&do=updateClassOrder', options);
}

function switchRaid(rid,raid)
{
  document.getElementById('raidinfo').innerHTML =ajax_loading();   
    var options = { 
                     method : 'post'
                    ,parameters : 'rid='+rid+'&raid='+raid
                    ,onSuccess: function(t){
                      jsonRaw = t.responseText;
                      jsonContent = eval("(" + jsonRaw + ")");
                      document.getElementById('raidinfo').innerHTML = jsonContent.output;                                       
                    }
                  };

    new Ajax.Request(smf_scripturl+'?action=ajax&do=switchRaid', options);  	  
}

function getAttendancy(cid)
{
  document.getElementById('attendancyinfo').innerHTML =  '' + ajax_loading();   
    var options = { 
                     method : 'post'
                    ,parameters : 'cid='+cid
                    ,onSuccess: function(t){
                      jsonRaw = t.responseText;
                      jsonContent = eval("(" + jsonRaw + ")");
                      document.getElementById('attendancyinfo').innerHTML = jsonContent.output;                                       
                    }
                  };

    new Ajax.Request(smf_scripturl+'?action=ajax&do=getAttendancy', options);    
}