/*
 * Copyright 2005 Tridium, Inc. All Rights Reserved.
 */

/**
 * forum
 *
 * @author    JJ Frankovich
 * @creation  11 Oct 05
 * @version   $Revision$ $Date$
 * @since     Baja 1.0
 */

////////////////////////////////////////////////////////////////
// forum
////////////////////////////////////////////////////////////////

var forum = new Forum();
function Forum()
{

////////////////////////////////////////////////////////////////
// Events
////////////////////////////////////////////////////////////////
  
  this.sort = function(id, def)
  {
    var old=document.forms[0].order;
    if(old != null && old.value.indexOf(id) == -1)
    {
      this.unsort(old.value);
    }
    var element = document.getElementById(id);
    if(element.getAttribute("class").indexOf("Up") > -1 )
    {
      element.setAttribute("class", "forum-sortDown");
      //document.form.orderDir.value="Down";
    }
    else if(element.getAttribute("class").indexOf("Down") > -1)
    {
      element.setAttribute("class", "forum-sortUp");
      //document.form.orderDir.value="Up";
    }
    else
    {
      if(def.indexOf("true") > -1)
      {
        element.setAttribute("class", "forum-sortUp");
        //document.form.orderDir.value="Up";
      }
      else                                            
      {
        element.setAttribute("class", "forum-sortDown");
        //document.form.orderDir.value="Down";
      } 
    }
    hx.setFormValue("order", id);
  }
  this.replace = function(oldRow, newRow)
  {
    var d1 = document.getElementById(oldRow); 
    var d2 = document.getElementById(newRow); 
    var d2Copy =d2.cloneNode(true);
    d2Copy.setAttribute("id", "copy");
    var table = document.getElementById("myTable");
    //alert(d1.getAttribute("id") + " replaced by " + d2Copy.getAttribute("id") + " for " +table.getAttribute("id"));
    table.tBodies[0].replaceChild(d2Copy, d1);
  }
  
  this.fixAlt = function(element, k)  
  {
    var alt=element.getAttribute("class");
    var indexAlt=alt.toString().indexOf("-alt");
    if( indexAlt > -1 && k%2 == 0)
    { 
      alt=alt.substring(0,indexAlt);
      element.setAttribute("class", alt);
    }
    else if( indexAlt == -1 && k%2 == 1)
    {
      alt=alt + "-alt";
      element.setAttribute("class", alt);
    }
    return element;   
  }
  
  this.replaceAll = function(array, tag, up)
  {
    //alert(array.length);                         
    var table = document.getElementById("myTable");
    var tBodyCopy = document.getElementById("myTable").tBodies[0];
    var dir=document.getElementById(tag);
    //var tBodyCopy = tBody.cloneNode(false);
    //tBodyCopy.appendChild(tBody.firstChild);
    var dirFinal;           
    
    if(dir.getAttribute("class").indexOf("Down") > -1)
      dirFinal=false;
    else if(dir.getAttribute("class").indexOf("Up") > -1)
      dirFinal=true;
    else
    {
      dirFinal=up;
    }
    if(dirFinal==true)
    {
      for(var k=0 ; k < array.length ; ++k)
      {
        tBodyCopy.appendChild( this.fixAlt(document.getElementById(array[k]), k));
      }
    }
    else
    {
      for(var k=array.length-1 ; k >= 0 ; --k)
      {
        var offsetK=k;
        if(array.length%2 == 0)                                                   
          offsetK++;
        tBodyCopy.appendChild( this.fixAlt(document.getElementById(array[k]), offsetK++));
      }
    }                                
  }
     
  this.unsort = function(order)
  {
    document.getElementById(order).setAttribute("class", "forum-sort");
  }
  this.selectTitle = function(id)
  {
    var element = document.getElementById(id);
    if(element.getAttribute("class").indexOf("alt") == -1)
    {
      if(element.getAttribute("class").indexOf("good") != -1)
      {
       element.setAttribute("class", "font-medium forum-title forum-cursorOn forum-bad");
       hx.setFormValue(id, "deny");
      }
      else if(element.getAttribute("class").indexOf("bad") != -1)
      {
        element.setAttribute("class", "font-medium forum-title");
        hx.setFormValue(id, "ignore");
      }
      else
      {
        element.setAttribute("class", "font-medium forum-title forum-cursorOn forum-good");
        hx.setFormValue(id, "accept");
      }
    }                                                                    
    else
    {
      if(element.getAttribute("class").indexOf("good") != -1)
      {
        element.setAttribute("class", "forum-row forum-bad-alt");
        hx.setFormValue(id, "deny");
      }
      else if(element.getAttribute("class").indexOf("bad") != -1)
      {
        element.setAttribute("class", "forum-row-alt");
        hx.setFormValue(id, "ignore");
      }
      else                            
      {
        element.setAttribute("class", "forum-row forum-good-alt");
        hx.setFormValue(id, "accept");
      }
    }
  }
  this.select = function(id)
  {
    var element = document.getElementById(id);
    if(element.getAttribute("class").indexOf("alt") == -1)
    {
      if(element.getAttribute("class").indexOf("good") != -1)
      {
       element.setAttribute("class", "forum-row forum-bad");
       hx.setFormValue(id, "deny");
      }
      else if(element.getAttribute("class").indexOf("bad") != -1)
      {
        element.setAttribute("class", "forum-row");
        hx.setFormValue(id, "ignore");
      }
      else
      {
        element.setAttribute("class", "forum-row forum-good");
        hx.setFormValue(id, "accept");
      }
    }                                                                    
    else
    {
      if(element.getAttribute("class").indexOf("good") != -1)
      {
        element.setAttribute("class", "forum-row forum-bad-alt");
        hx.setFormValue(id, "deny");
      }
      else if(element.getAttribute("class").indexOf("bad") != -1)
      {
        element.setAttribute("class", "forum-row-alt");
        hx.setFormValue(id, "ignore");
      }
      else                            
      {
        element.setAttribute("class", "forum-row forum-good-alt");
        hx.setFormValue(id, "accept");
      }
    }
  }
  
  this.select2 = function(id, id2)
  {
    var element = document.getElementById(id);
    var element2 = document.getElementById(id2);
    if(element2.getAttribute("class").indexOf("alt") == -1)
    {
      if(element.getAttribute("class").indexOf("good") != -1)
      {
        element2.setAttribute("class", "forum-row forum-bad");
        hx.setFormValue(id2, "deny");
      }
      else if(element.getAttribute("class").indexOf("bad") != -1)
      {
        element2.setAttribute("class", "forum-row");
        hx.setFormValue(id2, "ignore");
      }
      else                         
      {
        element2.setAttribute("class", "forum-row forum-good");
        hx.setFormValue(id2, "accept");
      }
    }                                                                    
    else
    {
      if(element.getAttribute("class").indexOf("good") != -1)
      {
        element2.setAttribute("class", "forum-row forum-bad-alt");
        hx.setFormValue(id2, "deny");
      }
      else if(element.getAttribute("class").indexOf("bad") != -1)
      {
        element2.setAttribute("class", "forum-row-alt");
        hx.setFormValue(id2, "ignore");
      }
      else
      {
        element2.setAttribute("class", "forum-row forum-good-alt");
        hx.setFormValue(id2, "accept");
      }
    }
  }
}


