var forumXSL = null;
var selectedSOVmagning = null;


function loadForum()
{
  new net.ContentLoader('forum/functions/getForum.php?&date ='+new Date(),parseForum);
}
function parseForum()
{
  var xmlDoc = this.req.responseXML;
  //alert(xmlDoc.xml);
  var el = document.getElementById('LayerForum');
  //alert(forumXSL);
  if (forumXSL != null)
  {
    if (window.ActiveXObject)
    {
      el.innerHTML = xmlDoc.transformNode(forumXSL);
    // alert(el.innerHTML);
    }
    else
    {
      var xsltProcessor = new XSLTProcessor();
      xsltProcessor.importStylesheet(forumXSL);
      var fragment = xsltProcessor.transformToFragment(xmlDoc,document);

      el.innerHTML='';
      el.appendChild(fragment);
    //  alert(el.innerHTML);

    }
  }
}

function showForum(idx)
{
  closeFunction =  closeShowAll;

  if (idx)
  {
    currentIDX = idx;
    // url,onload,onerror,method,params,contentType,notify
    //  alert("calling notify");
    new net.ContentLoader('forum/functions/getForumOrder.php?eid='+idx+'&date ='+new Date(),parseForumAllEvent);
  }
  else
  {
    new net.ContentLoader('forum/functions/getForumOrder.php?&date ='+new Date(),parseForumAllEvent);
  }

  var el = document.getElementById("LayerTastingShowAll");
  //alert(el);
  if (el != null)
  {
    var vis = el.style.visibility;
    //if (vis == "hidden")
    {
      var elheight=el.offsetHeight;
      el.innerHTML="<center><img class=\"va\" src=\"images/wait26trans.gif\" style=\"position:relative;top:"+((elheight/2)-30)+"px;\" /></center>";
      el.style.visibility="visible";
      el.style.zIndex = "10";
    }
  //else
  // {
  //  el.style.visibility="hidden";
  //}
  }

  setBlackShade();

}

function parseForumAllEvent()
{
  var xmlDoc = this.req.responseText;
  var el = document.getElementById('LayerTastingShowAll');
  //alert(tastingXSL);
  if (el != null)
  {
    el.innerHTML = xmlDoc;
  }
  //alert(currentIDX);
  if (currentIDX != -1)
  {
    var eid = currentIDX;
    currentIDX = -1;
    //  alert("eid:"+eid);
    var ttable = document.getElementById('SOVScroller');
    if (ttable != null)
    {
      var currentIdx = -1;

      var trs = ttable.childNodes;
      //    alert(currentIdx);
      for(i=0 ; i < trs.length ; i++)
      {
        var row = trs[i];
        //      alert("nodeType:"+row.nodeType);
        if (row.nodeType == 1)
        {
          //       alert(row.getAttribute("event_id"));
          if (row.getAttribute("event_id") == eid)
          {
            currentIdx = i;
          }
        }
      }
      // alert(currentIdx);
      if (currentIdx == -1)
      {
        showForumEvent(eid);

      }
      else
      {
        var selEl = trs[currentIdx];
        //alert(selEl);
        if (selEl != null)
        {
          // alert("event_id:"+selEl.getAttribute("id"));
          showForumEvent(selEl.getAttribute("id"));
        }
      }
    }
  }
}

function showForumEvent(idx)
{
  var oThis = this;
  if(oThis.readtimer)
  {
    clearTimeout(oThis.readtimer);
  }
  var el = document.getElementById("LayerTastingShowAllEvent");
  if (el != null)
  {
    // var vis = el.style.visibility;
    el.style.left="434px";
    var elheight=el.offsetHeight;
    el.innerHTML="<center><img class=\"va\" src=\"images/wait26trans.gif\" style=\"position:relative;top:"+((elheight/2)-30)+"px;\" /></center>";
    el.style.visibility="visible";
    el.style.zIndex = "10";
  }
  //alert(idx.startsWith("event_")+"_"+idx);
  if (selectedSOVmagning != null && selectedSOVmagning.id.startsWith("eventSOV_"))
  {
    selectedSOVmagning.style.background="white";
  }
  var tdEl= document.getElementById(idx);
  if (tdEl != null)
  {
    
    var el = $("ToolTip2");
    if (el)
    {
      el.innerHTML="";
    }


    if (idx.startsWith("eventSOV_"))
    {
      tdEl.style.background="#CCCCCC";
    }
    new net.ContentLoader('forum/functions/getForumEvent2.php?eventid='+tdEl.getAttribute("event_id")+'&date ='+new Date(),parseForumEventShow);

  }

  selectedSOVmagning = tdEl;

}

function parseForumEventShow()
{
  var xmlDoc = this.req.responseText;
  // alert(xmlDoc);
  var el = document.getElementById('LayerTastingShowAllEvent');
  el.innerHTML = xmlDoc;
  startForumReadTimer();
}




function checkForumResponse(thisform)
{
  with (thisform)
  {
    if (validate_required(forumtextarea,"A response needs to be filled in")==false)
    {
      forumtextarea.focus();
      return false;
    }
    if (forumname.type=="text")
    {
      if (validate_required(forumname,"Name must be filled out!")==false)
      {
        forumname.focus();
        return false;
      }
    }
    if (forumemail.type=="text")
    {
      if (echeck(forumemail.value,"Email must be filled out!")==false)
      {
        forumemail.focus();
        return false;
      }
    }
    }
  return true;
}

function checkForum(thisform)
{
  //alert("checkForum");
  with (thisform)
  {
    //return true;
    var et = $("pdffile");
    //alert(et.value);
    {
      if (validate_required_no_alert(forumtextarea)==false && validate_required_no_alert(forumtitle)==false)
      {
        alert("Either the title or description needs to be filled out");
        forumtextarea.focus();
        return false;
      }
      if (forumname.type=="text")
      {
        if (validate_required(forumname,"Name must be filled out!")==false)
        {
          forumname.focus();
          return false;
        }
      }
      if (forumemail.type=="text")
      {
        if (echeck(forumemail.value,"Email must be filled out!")==false)
        {
          forumemail.focus();
          return false;
        }
      }
      if (forumpdfchbox.checked == true)
      {
        if (!pdffile.value.toUpperCase().endsWith(".PDF"))
        {
          alert("Only pdf files");
          pdffile.focus();
          return false;
        }
      }
      if (forumjpgchbox.checked == true)
      {
        if (!imagefile.value.toUpperCase().endsWith(".JPG") && !imagefile.value.toUpperCase().endsWith(".JPEG"))
        {
          alert("Only jpg files");
          imagefile.focus();
          return false;
        }
      }
      if (usertype.value == '0')
      {
        if (validate_required_no_alert(pubforumaovstartdate) ||
          validate_required_no_alert(pubforumaovenddate) ||
          validate_required_no_alert(promowhole) ||
          validate_required_no_alert(promodag) ||
          validate_required_no_alert(promoshow))
          {
          if (!validate_required(pubforumaovstartdate,"From date needs to be filled in"))
          {
            pubforumaovstartdate.focus();
            return false;
          }
          if (!validate_required(pubforumaovenddate,"To date needs to be filled in"))
          {
            pubforumaovenddate.focus();
            return false;
          }
          if (!validate_number(promowhole,"Period amount needs to be filled in"))
          {
            promowhole.focus();
            return false;
          }
          if (!validate_number(promodag,"Day amount needs to be filled in"))
          {
            promodag.focus();
            return false;
          }
          if (!validate_number(promoshow,"Per show amount needs to be filled in"))
          {
            promoshow.focus();
            return false;
          }
        }
      }
    }
    }
  return true;
}

function loadForumXSL(stylesheet)
{
  var xsl = null;
  if(window.ActiveXObject)
  {
    

    // Load XSL
    xsl = new ActiveXObject("MSXML2.DOMDocument");
    xsl.async = false
    xsl.load(stylesheet)
    
  }
  if(document.implementation && document.implementation.createDocument)
  {

    // load the xslt file
    var myXMLHTTPRequest = new XMLHttpRequest();
    myXMLHTTPRequest.open("GET", stylesheet, false);
    myXMLHTTPRequest.send(null);

    xsl = myXMLHTTPRequest.responseXML;
  // alert(myXMLHTTPRequest.responseText);
  }
  
  this.forumXSL = xsl;
  loadForum();

}

function validate_forum_form(thisform)
{
  with (thisform)
  {
    //  return true;
    if (validate_required(subject,"subject must be filled out!")==false)
    {
      subject.focus();
      return false;
    }
    return true;
    }
  return false;
}

function submitSubject(thisform)
{
  if (validate_forum_form(thisform))
  {
    return Post.Send(thisform,'forum/functions/createForum.php','Creating...',Post.OnResponseForum);
  }
  else{
    return false
  }
}

function reloadForum()
{
  loadForum();
}


function showForumNext(startoffset,maxevents)
{
  //alert(startoffset);
  if (startoffset >= 0 && startoffset <= maxevents)
  {
    new net.ContentLoader('forum/functions/getForumOrder.php?startoffset='+startoffset+'&date ='+new Date(),parseForumAllEvent);
  }
}

function editForumvent(idx,alone)
{
  var el = document.getElementById("LayerTastingShowAll");
  if (el != null)
  {
    //var vis = el.style.visibility;
    //el.style.visibility="hidden";
    el.style.zIndex="2";

  }
  //hideBlackShade();

  el = document.getElementById("LayerTastingShowAllEvent");
  if (el != null)
  {
    //var vis = el.style.visibility;
    //el.style.visibility="hidden";
    el.style.zIndex="2";

  }
  el = document.getElementById("LayerForumAddEvent");
  if (el != null)
  {
    el.style.left="8px";
    el.innerHTML="";
    el.style.visibility="visible";
  }
  if (selectedTSmagning != null)
  {
    selectedTSmagning.style.background="white";
  }
  closeFunction = closeShowAll

  if (false)
  {
    new net.ContentLoader('forum/functions/getForumEdit.php?eventid='+idx+'&date ='+new Date(),parseForumEdit);
  }
  else
  {
    new net.ContentLoader('forum/functions/getForumEdit.php?cf=closeShowAll&eventid='+idx+'&date ='+new Date(),parseForumEdit);
  }


  setBlackShade();

  selectedTSmagning = null;


}


function editForumResponse(idx,alone,eid)
{
  var el = document.getElementById("LayerTastingShowAll");
  if (el != null)
  {
    //var vis = el.style.visibility;
    //el.style.visibility="hidden";
    el.style.zIndex="2";

  }
  //hideBlackShade();

  el = document.getElementById("LayerTastingShowAllEvent");
  if (el != null)
  {
    //var vis = el.style.visibility;
    //el.style.visibility="hidden";
    el.style.zIndex="2";

  }
  el = document.getElementById("LayerForumAddEvent");
  if (el != null)
  {
    el.style.left="8px";
    el.innerHTML="";
    el.style.visibility="visible";
  }
  if (selectedTSmagning != null)
  {
    selectedTSmagning.style.background="white";
  }
  closeFunction = closeShowAll

  if (false)
  {
    new net.ContentLoader('forum/functions/getForumResponse.php?forum_id='+idx+'&eventid='+eid+'&date ='+new Date(),parseForumEdit);
  }
  else
  {
    new net.ContentLoader('forum/functions/getForumResponse.php?cf=closeShowAll&forum_id='+idx+'&eventid='+eid+'&date ='+new Date(),parseForumEdit);
  }


  setBlackShade();

  selectedTSmagning = null;


}

function showForumPromo()
{
  var el = $("forum3");

  if (el != null)
  {
    if (el.style.visibility=="hidden")
    {
      el.style.visibility="visible";
      el.style.display="block";
    }
    else
    {
      el.style.visibility="hidden";
      el.style.display="none";
    }
  }
}

function showForumPDFPicture()
{
  var el = $("forum1");

  if (el != null)
  {
    if (el.style.visibility=="hidden")
    {
      el.style.visibility="visible";
      el.style.display="block";
    }
    else
    {
      el.style.visibility="hidden";
      el.style.display="none";
    }
  }
  el = $("forum2");
  if (el != null)
  {
    if (el.style.visibility=="hidden")
    {
      el.style.visibility="visible";
      el.style.display="block";
    }
    else
    {
      el.style.visibility="hidden";
      el.style.display="none";
    }
  }
}

function searchForum()
{
  
  var el = $("forumsearchtext")
  if (el != null)
  {
    closeFunction =  closeShowAll;
    var stext = el.value;
    if (stext == 'Skriv søgeord')
    {
      stext = '';
    }

    new net.ContentLoader('forum/functions/getForumSearch.php?s='+stext+'&date ='+new Date(),parseForumAllEvent);
 

    var el = document.getElementById("LayerTastingShowAll");
    //alert(el);
    if (el != null)
    {
      var vis = el.style.visibility;
      //if (vis == "hidden")
      {
        var elheight=el.offsetHeight;
        el.innerHTML="<center><img class=\"va\" src=\"images/wait26trans.gif\" style=\"position:relative;top:"+((elheight/2)-30)+"px;\" /></center>";
        el.style.visibility="visible";
        el.style.zIndex = "10";
      }
    //else
    // {
    //  el.style.visibility="hidden";
    //}
    }

    setBlackShade();
  }
}

function parseForumEdit()
{
  var xmlDoc = this.req.responseText;
  var el = document.getElementById('LayerForumAddEvent');
  if (el != null)
  {
    el.innerHTML=xmlDoc;
  }
  var ell = $("forumtextfield");
  if (ell != null)
  {
    var ee = $("forumtextarea");
    //alert(ell.value);
    if (ell.value != 'Skriv her. Upload foto mm. Klik >>')
    {
      ee.value = ell.value;
    }
  }
}


function createForumAnswer()
{
  alert("Not yet Batman");
}



function startForumReadTimer()
{
  if (selectedSOVmagning != null)
  {
    var idx = selectedSOVmagning.getAttribute("event_id");
    if (idx)
    {
      var el = $("ToolTip2");
      if (el)
      {
        el.innerHTML="Start reading "+idx;
      }
    }
  }
  var oThis = this;
  if(oThis.readtimer)
  {
    clearTimeout(oThis.readtimer);
  }
  oThis.readtimer = setTimeout(function() {
    itemForumRead();
  }, 100);
}

function itemForumRead()
{
  if (selectedSOVmagning != null)
  {
    var idx = selectedSOVmagning.getAttribute("event_id");
    if (idx)
    {
      var el = $("ToolTip2");
      if (el)
      {
        el.innerHTML="Read "+idx;
      }
      new net.ContentLoader('forum/functions/getForumItemRead.php?eventid='+idx+'&date ='+new Date(),dummy);

    }
  }

}
function dummy()
{
    
}

function createForum()
{
  alert("here");
  //if (validate_mail_form(document.emailform))
  {
    Post.Send(document.forumform, 'forum/functions/createForum.php','Sending...',Post.OnResponse2);
  }
  return false;
}

function printSpecialForum(id,eType,etid)
{
  //alert(id+"_"+eType+"_"+etid);
  if (etid == null)
  {
    etid = -1;
  }
  //  new net.ContentLoader('print/print.php?valid_to='+valid_to+'&event_type='+encodeURIComponent(eType)+'&eid='+id+'&desc='+encodeURIComponent(desc)+'&logo='+logo+'&comp='+encodeURIComponent(comp)+'&town='+encodeURIComponent(address)+'&web='+encodeURIComponent(web)+'&jpg='+encodeURIComponent(jpg)+'&extra_text='+encodeURIComponent(etext)+'&date ='+new Date(),parsePrintSpecial);
  new net.ContentLoader('print/functions/printSOV.php?eid='+id+'&etid='+etid+'&event_type='+encodeURIComponent(eType)+'&date ='+new Date(),parsePrintSpecial);

}

function showTop5(idx)
{
  closeFunction =  closeShowAll;

  if (idx)
  {
    currentIDX = idx;
    // url,onload,onerror,method,params,contentType,notify
    //  alert("calling notify");
    new net.ContentLoader('forum/functions/getForumTop5.php?eid='+idx+'&date ='+new Date(),parseForumAllEvent);
  }
  else
  {
    new net.ContentLoader('forum/functions/getForumTop5.php?&date ='+new Date(),parseForumAllEvent);
  }

  var el = document.getElementById("LayerTastingShowAll");
  //alert(el);
  if (el != null)
  {
    var vis = el.style.visibility;
    //if (vis == "hidden")
    {
      var elheight=el.offsetHeight;
      el.innerHTML="<center><img class=\"va\" src=\"images/wait26trans.gif\" style=\"position:relative;top:"+((elheight/2)-30)+"px;\" /></center>";
      el.style.visibility="visible";
      el.style.zIndex = "10";
    }
  //else
  // {
  //  el.style.visibility="hidden";
  //}
  }

  setBlackShade();

}

function selectNextForumAll(direction)
{
  if (selectedSOVmagning != null)
  {

    //var children = document.getElementById('TastingScroller-Container').childNodes;
    //alert(children.length);

    var ttable = document.getElementById('SOVScroller');
    if (ttable != null)
    {
      var currentIdx = -1;

      var trs = ttable.childNodes;
      for(i=0 ; i < trs.length ; i++)
      {
        var row = trs[i];
        if (row.nodeType == 1)
        {
          if (row.id == selectedSOVmagning.id)
          {
            currentIdx = i;
          }
        }
      }
      if (currentIdx >= 0)
      {
        // alert("currentID:"+currentIdx)
        if (currentIdx ==0 && direction < 0)
        {
          this.showForumEvent("eventSOV_1");
        }
        //else if (currentIdx ==1 && direction <0)
        //  {
        //  this.showTastingAllEvent("event_1");
        // }
        else
        {
          var selEl = trs[currentIdx+direction];
          while (selEl != null && selEl.nodeType != 1)
          {
            currentIdx+=direction;
            selEl = trs[currentIdx+direction];
          }
          if (selEl != null)
          {
            //alert(selEl.id+"_"+selEl.getAttribute("event_id"));
            if (selEl.getAttribute("event_id") != null)
            {
              this.showForumEvent(selEl.id);
            }
            else
            {
              this.showForumEvent("eventSOV_1");
            }
          }
        }
      }
      else if (currentIdx == -1 && direction>0)
      {
        var selID = selectedSOVmagning.id;
        var iCount = selID.substring(selID.indexOf("_")+1);
        //alert("event_"+(parseInt(iCount)+1));
        if (parseInt(iCount) > ttable.childNodes.length)
        {
          this.showForumEvent("eventSOV_1");
        }
        else{
          this.showForumEvent("eventSOV_"+(parseInt(iCount)+1));
        }
      }

      else if (currentIdx == -1 && direction < 0)
      {
        this.showForumEvent("eventSOV_1");
      }

    }
  }
}




