var newsXSL = null;
var selectedSOVmagning = null;

function loadNews()
{
  new net.ContentLoader('news/functions/getNews.php?&date ='+new Date(),parseNews);
}
function parseNews()
{
  var xmlDoc = this.req.responseXML;
  //alert(xmlDoc.xml);
  var el = document.getElementById('LayerNews');
  //alert(newsXSL);
  if (newsXSL != null)
  {
    if (window.ActiveXObject)
    {
      el.innerHTML = xmlDoc.transformNode(newsXSL);
    // alert(el.innerHTML);
    }
    else
    {
      var xsltProcessor = new XSLTProcessor();
      xsltProcessor.importStylesheet(newsXSL);
      var fragment = xsltProcessor.transformToFragment(xmlDoc,document);

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

    }
  }
}

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

function checkNews(thisform)
{
  //alert("checkNews");
  with (thisform)
  {
    //return true;
    var et = $("pdffile");
    //alert(et.value);
    {
      if (validate_required_no_alert(newstextarea)==false && validate_required_no_alert(newstitle)==false)
      {
        alert("Either the title or description needs to be filled out");
        newstextarea.focus();return false;
      }
      if (newsname.type=="text")
      {
        if (validate_required(newsname,"Name must be filled out!")==false)
        {
          newsname.focus();return false;
        }
      }
      if (newsemail.type=="text")
      {
        if (echeck(newsemail.value,"Email must be filled out!")==false)
        {
          newsemail.focus();return false;
        }
      }
      if (newspdfchbox.checked == true)
      {
        if (!pdffile.value.toUpperCase().endsWith(".PDF"))
        {
          alert("Only pdf files");
          pdffile.focus();return false;
        }
      }
      if (newsjpgchbox.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(pubnewsaovstartdate) ||
          validate_required_no_alert(pubnewsaovenddate) ||
          validate_required_no_alert(promowhole) ||
          validate_required_no_alert(promodag) ||
          validate_required_no_alert(promoshow))
          {
          if (!validate_required(pubnewsaovstartdate,"From date needs to be filled in"))
          {
            pubnewsaovstartdate.focus();return false;
          }
          if (!validate_required(pubnewsaovenddate,"To date needs to be filled in"))
          {
            pubnewsaovenddate.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 loadNewsXSL(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.newsXSL = xsl;
  loadNews();

}

function validate_news_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_news_form(thisform))
  {
    return Post.Send(thisform,'news/functions/createNews.php','Creating...',Post.OnResponseNews);
  }
  else{
    return false
  }
}

function reloadNews()
{
  loadNews();
}

function showNews(idx)
{
  closeFunction =  closeShowAll;

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

  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 showNewsNext(startoffset,maxevents)
{
  //alert(startoffset);
  if (startoffset >= 0 && startoffset <= maxevents)
  {
    new net.ContentLoader('news/functions/getNewsOrder.php?startoffset='+startoffset+'&date ='+new Date(),parseNewsAllEvent);
  }
}

function editNewsvent(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('news/functions/getNewsEdit.php?eventid='+idx+'&date ='+new Date(),parseNewsEdit);
  }
  else
  {
    new net.ContentLoader('news/functions/getNewsEdit.php?cf=closeShowAll&eventid='+idx+'&date ='+new Date(),parseNewsEdit);
  }


  setBlackShade();

  selectedTSmagning = null;


}


function editNewsResponse(idx,alone,eid)
{
 // alert(idx+"_"+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('news/functions/getNewsResponse.php?news_id='+idx+'&eventid='+eid+'&date ='+new Date(),parseNewsEdit);
  }
  else
  {
    new net.ContentLoader('news/functions/getNewsResponse.php?cf=closeShowAll&news_id='+idx+'&eventid='+eid+'&date ='+new Date(),parseNewsEdit);
  }


  setBlackShade();

  selectedTSmagning = null;


}

function showNewsPromo()
{
  var el = $("news3");

  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 showPDFPicture()
{
  var el = $("news1");

  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 = $("news2");
  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 searchNews()
{
  
  var el = $("newssearchtext")
  if (el != null)
  {
    closeFunction =  closeShowAll;
    var stext = el.value;
    if (stext == 'Skriv søgeord')
    {
      stext = '';
    }

    new net.ContentLoader('news/functions/getNewsSearch.php?s='+stext+'&date ='+new Date(),parseNewsAllEvent);
 

    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 parseNewsEdit()
{
  var xmlDoc = this.req.responseText;
  var el = document.getElementById('LayerForumAddEvent');
  if (el != null)
  {
    el.innerHTML=xmlDoc;
  }
  var ell = $("newstextfield");
  if (ell != null)
  {
    var ee = $("newstextarea");
    //alert(ell.value);
    if (ell.value != 'Skriv her. Upload foto mm. Klik >>')
    {
      ee.value = ell.value;
    }
  }
}

function showNewsEvent(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('news/functions/getNewsEvent2.php?eventid='+tdEl.getAttribute("event_id")+'&date ='+new Date(),parseNewsEventShow);

  }

  selectedSOVmagning = tdEl;

}

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

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

function startReadTimer()
{
  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() {
    itemRead();
  }, 100);
}

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

    }
  }

}
function dummy()
{
    
}

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

function printSpecialNews(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/printPRO.php?eid='+id+'&etid='+etid+'&event_type='+encodeURIComponent(eType)+'&date ='+new Date(),parsePrintSpecial);

}

function showNewsTop5(idx)
{
  closeFunction =  closeShowAll;

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

  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 selectNextNewsAll(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.showNewsEvent("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.showNewsEvent(selEl.id);
            }
            else
            {
              this.showNewsEvent("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.showNewsEvent("eventSOV_1");
        }
        else{
          this.showNewsEvent("eventSOV_"+(parseInt(iCount)+1));
        }
      }

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

    }
  }
}

function parseNewsAllEvent()
{
  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)
      {
        showNewsEvent(eid);

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



