/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var currentDoc = null;
var filter = ['1','2','4','5','6','7','8','9'];
var calendarMonthViewXSL = null;
var calendarListViewXSL = null;
var currentView = 'month';
var selectedView = 'month';
var selectedEvent = null;
var eventArray = new Array();
var cViewID = 0;

Array.prototype.contains = function(obj) {
  var i = this.length;
  while (i--) {
    if (this[i] === obj) {
      return true;
    }
  }
  return false;
}
Array.prototype.remove=function(s){
  for(i=0;i<this.length;i++) {
    if(s==this[i]) this.splice(i, 1);
  }
}


Array.prototype.unique = function () {
  var r = new Array();
    o:for(var i = 0, n = this.length; i < n; i++)
    {
      for(var x = 0, y = r.length; x < y; x++)
      {
        if(r[x]==this[i])
        {
          continue o;
        }
      }
      r[r.length] = this[i];
    }
  return r;
}

function nextCalendarView(dir)
{
  //alert("current view:"+currentView)
  if (currentView=='month')
  {
    var idx = cViewID;
    var idxYr = cViewYrID;
    idx = parseInt(idx)+dir;
    if (idx > 12)
    {
      idx = 1;
      idxYr = parseInt(idxYr)+1;
    }
    else if (idx < 1)
    {
      idx = 12;
      idxYr = parseInt(idxYr)-1;
    }
    new net.ContentLoader('calendar/functions/getMonthEvents.php?month='+idx+'&year='+idxYr+'&date ='+new Date(),parseMonthEvents);
    updateButtons();
  }
  else if (currentView=='week')
  {
    var idx = parseInt(cViewID,10)+dir;
    new net.ContentLoader('calendar/functions/getWeekEvents.php?weeknr='+idx+'&year='+cViewYrID+'&date ='+new Date(),parseWeekEvents);
  }
}

function filterCal(di,type)
{
  
  if (di.getAttribute("checked")=='checked')
  {
    filter[filter.length] = ""+type;
    filter.unique();
  }
  else
  {
    filter.remove(""+type);
  }
  filterEvents();
}

function parseMonthViewXSL()
{
  var res = this.req.responseXML;
  // alert(this.req.responseText);
  calendarMonthViewXSL = res;
  
}

function parseListViewXSL()
{
  var res = this.req.responseXML;
  // alert(this.req.responseText);
  calendarListViewXSL = res;
  
}

function parseMainCalendarXSL()
{
  var res = this.req.responseXML;
  //alert(this.req.responseText);
  calendarMainXSL = res;
  currentView = 'month';
  new net.ContentLoader('calendar/mainCalendar.php?&date ='+new Date(),parseMain);
}
function parseMain()
{
  var res = this.req.responseText;
  //alert(res);
  var el = document.getElementById("LayerCalendar");
  if (el != null)
  {
    el.innerHTML = res;
  }
  startUpCalendarXSL();
    
}

function startUpCalendarXSL()
{
  //new net.ContentLoader('calendar/functions/getListEvents.php?&date ='+new Date(),parseListEvents);
  //new net.ContentLoader('calendar/functions/getWeekEvents.php?&date ='+new Date(),weekEventSelected);
  new net.ContentLoader('calendar/functions/getMonthEvents.php?&date ='+new Date(),parseMonthEvents);

}

function weekEventSelected(week,year)
{
  //alert(week+"_"+year);
  currentView = 'week';
  if (week != null && year != null)
  {
    new net.ContentLoader('calendar/functions/getWeekEvents.php?weeknr='+week+'&year='+year+'&date ='+new Date(),parseWeekEvents);
  }
  else
  {
    new net.ContentLoader('calendar/functions/getWeekEvents.php?&date ='+new Date(),parseWeekEvents);
  }
  updateButtons();
}

function monthEventSelected()
{
  currentView = 'month';
  new net.ContentLoader('calendar/functions/getMonthEvents.php?&date ='+new Date(),parseMonthEvents);
  updateButtons()
}

function listEventSelected()
{
  currentView = 'list';
  new net.ContentLoader('calendar/functions/getListEvents.php?&date ='+new Date(),parseListEvents);
  updateButtons()
}
function latestEventSelected()
{
  currentView = 'latest';
  new net.ContentLoader('calendar/functions/getLatestEvents.php?&date ='+new Date(),parseListEvents);
  updateButtons()
}

function updateButtons()
{
  if (this.selectedView != null && this.selectedView != this.currentView)
  {
    var selimg = document.getElementById(selectedView+'img');
    if (selimg != null)
    {
      selimg.src = "images/knap.png";
    }
    selimg = document.getElementById(selectedView+'text');
    if (selimg != null)
    {
      selimg.className = "eightpoint mainselectedcolor";
    }
  }
  var newimg = document.getElementById(currentView+'img');
  if (newimg != null)
  {
    newimg.src = "images/knap_markeret.png"
  }
  newimg = document.getElementById(currentView+'text');
  if (newimg != null)
  {
    newimg.className = "eightpoint colormain";
  }
  selectedView = currentView;
}

function filterEvents()
{
  //alert('filterEvents:'+currentView);
  if (currentView == 'week')
  {
    //alert("calling filterWeekEvents");
    filterWeekEvents();
  }
  else if (currentView == 'month')
  {
    this.filterMonthEvents();
  }
  else if (currentView == 'list')
  {
    this.filterListEvents();
  }
  else if (currentView == 'latest')
  {
    this.filterListEvents();
  }
}
function getActiveXObject()
{
  var obj = null;
  if (window.ActiveXObject)
  {
    obj=new ActiveXObject("Microsoft.XMLDOM");
  }
  else if (document.implementation && document.implementation.createDocument)
  {
    obj = document.implementation.createDocument("", "", null);

  // obj=new XMLHttpRequest();
  }
  return obj;
}

function filterListEvents()
{
  //var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
  var xmlDocument = this.getActiveXObject();
  var root = xmlDocument.createElement('aov');
  var mt = document.getElementById("calLabel");
  if (mt != null)
  {
    mt.innerHTML="";
  }
  showPreviousNextButtons(false);
  //alert("filterWeekEvents");
  if (currentDoc != null)
  {
    var event1 = currentDoc.getElementsByTagName("event1");
    var ecount1 = 0;
    for(j=0;j<event1.length;j++)
    {
      var type = event1[j].getAttribute("type");
      //if (filter.length == 0 || filter.contains(type) || (event1[j].getAttribute("branch")=="1" && filter.contains("9")))
      {
        var e1 = xmlDocument.createElement('event1');
        e1.setAttribute("id",event1[j].getAttribute("id"));
        e1.setAttribute("event_id",event1[j].getAttribute("event_id"));
        e1.setAttribute("comp",event1[j].getAttribute("comp"));
        e1.setAttribute("comppost",event1[j].getAttribute("comppost"));
        e1.setAttribute("datetext",event1[j].getAttribute("datetext"));
        e1.setAttribute("title",event1[j].getAttribute("title"));
        e1.setAttribute("description",event1[j].getAttribute("description"));
        e1.setAttribute("jpg",event1[j].getAttribute("jpg"));
        e1.setAttribute("type",event1[j].getAttribute("type"));
        e1.setAttribute("town",event1[j].getAttribute("town"));
        e1.setAttribute("time_format",event1[j].getAttribute("time_format"));
        e1.setAttribute("branch",event1[j].getAttribute("branch"));
        ecount1++;
        root.appendChild(e1);
      }
    }
    var allevents = currentDoc.getElementsByTagName("events");
    for(k=0;k<allevents.length;k++)
    {
      // alert("allevents");
      var es1 = xmlDocument.createElement('events');
      es1.setAttribute("tlcount",allevents[k].getAttribute("tlcount"));
      es1.setAttribute("event_count",allevents[k].getAttribute("event_count"));
      es1.setAttribute("max_events",allevents[k].getAttribute("max_events"));
      es1.setAttribute("start_events",allevents[k].getAttribute("start_events"));
      es1.setAttribute("limit",allevents[k].getAttribute("limit"));
      var events = allevents[k].getElementsByTagName("event");
      var ecount = 0;
      for(j=0;j<events.length;j++)
      {
        //  alert("event");
        var type = events[j].getAttribute("type");
        if (filter.length == 0 || filter.contains(type) || (events[j].getAttribute("branch")=="1" && filter.contains("9")))
        {
          var e1 = xmlDocument.createElement('event');
          e1.setAttribute("id",events[j].getAttribute("id"));
          e1.setAttribute("event_id",events[j].getAttribute("event_id"));
          e1.setAttribute("comp",events[j].getAttribute("comp"));
          e1.setAttribute("comppost",events[j].getAttribute("comppost"));
          e1.setAttribute("datetext",events[j].getAttribute("datetext"));
          e1.setAttribute("title",events[j].getAttribute("title"));
          e1.setAttribute("description",events[j].getAttribute("description"));
          e1.setAttribute("jpg",events[j].getAttribute("jpg"));
          e1.setAttribute("type",events[j].getAttribute("type"));
          e1.setAttribute("town",events[j].getAttribute("town"));
          e1.setAttribute("time_format",events[j].getAttribute("time_format"));
          e1.setAttribute("branch",events[j].getAttribute("branch"));
          ecount++;
          es1.appendChild(e1);
        }
      }
      root.appendChild(es1);
    }
      

  }
  xmlDocument.appendChild(root);
  // alert(xmlDocument.xml);
  loadEvents(xmlDocument,calendarListViewXSL);
}

function showCalListNext(startoffset,maxevents)
{
  //alert(startoffset);
  if (startoffset >= 0 && startoffset <= maxevents)
  {
    //new net.ContentLoader('barreltasting/functions/getTastingOrder.php?startoffset='+startoffset+'&date ='+new Date(),parseTastingAllEvent);
    new net.ContentLoader('calendar/functions/getListEvents.php?startoffset='+startoffset+'&date ='+new Date(),parseListEvents);
  }
}

function unSelectEvent(el)
{
  if (el != null)
  {
    var top = el.style.top;
    top = top.substring(0,top.indexOf("px"))
    var left = el.style.left;
    left = left.substring(0,left.indexOf("px"))
    var height = el.style.height;
    height = height.substring(0,height.indexOf("px"))
    //alert(height);
    var width = el.style.width;
    width = width.substring(0,width.indexOf("px"))
    el.style.top = (parseInt(top)+4)+"px";
    el.style.left = (parseInt(left)+4)+"px";
    el.style.height = (parseInt(height)-8)+"px";
    //alert(el.style.height);
    el.style.width = (parseInt(width)-8)+"px";
    selectedEvent = null;
  }
}

function selectNextCalevent(dir)
{
  if (selectedEvent != null)
  {
    var id = selectedEvent.id;
    var idKey = id.substring(0,id.indexOf("_")+1);
    var idValue = parseInt(id.substring(id.indexOf("_")+1));
    //alert(id+"_"+newKey);
    var newKey = idKey+(idValue+dir);
    var el = document.getElementById(newKey);

    if (el != null)
    {
      //alert(''+el.getAttribute("event_id"));
      showEvent(''+el.getAttribute("event_id"),'LIST');
      eventSelected(''+newKey);
    }
  //alert(id+"_"+newKey);
  }
}

function eventUnSelected()
{
  if (selectedEvent != null)
  {
    var id = selectedEvent.id;
    unSelectEvent(selectedEvent);
    var el = document.getElementById(id+'b');
    unSelectEvent(el);
    el = document.getElementById(id+'br');
    // alert("try to hide:"+el+":cal"+id+"br");
    if (el != null)
    {
      //  alert("hiding");
      el.style.visibility = "hidden";
      unSelectEvent(el);
    }
    
  }
}

function eventSelected(eid)
{
  //if (selectedEvent != null )
  //alert(eid);
  var el = document.getElementById(eid);
  if (el != selectedEvent)
  {
    //alert(el);
    this.eventUnSelected();
  
    selectEvent(el);
    selectedEvent = el;
    el = document.getElementById(eid+'b');
    selectEvent(el);
    el = document.getElementById(eid+'br');
    if (el != null)
    {
      //alert("showing:"+'cal'+eid+'br');
      el.style.visibility = "visible";
      selectEvent(el);
    }
  }
  else
  {
    closeEventOverlay();
    eventUnSelected();
  }
}
function selectEvent(el)
{
  if (el != null)
  {
    //alert('TOP:'+el.style.top+' LEFT:'+el.style.left+' HEIGHT:'+el.style.height);
    var top = el.style.top;
    top = top.substring(0,top.indexOf("px"))
    var left = el.style.left;
    left = left.substring(0,left.indexOf("px"))
    var height = el.style.height;
    height = height.substring(0,height.indexOf("px"))
    //alert(height);
    var width = el.style.width;
    width = width.substring(0,width.indexOf("px"))
    el.style.top = (parseInt(top)-4)+"px";
    el.style.left = (parseInt(left)-4)+"px";
    el.style.height = (parseInt(height)+8)+"px";
    //alert(el.style.height);
    el.style.width = (parseInt(width)+8)+"px";
    

  }
  
}

function showPreviousNextButtons(show)
{
  var vi = "visible";
  if (!show)
  {
    vi = "hidden";
  }
  var pb = document.getElementById("previousCalendarButton");
  if (pb != null)
  {
    pb.style.visibility=vi;
  }
  var nb = document.getElementById("nextCalendarButton");
  if (nb != null)
  {
    nb.style.visibility=vi;
  }
  
}

function filterWeekEvents()
{
  //var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
  this.eventArray.length = 0;
  var xmlDocument = this.getActiveXObject();
  var root = xmlDocument.createElement('aov');
  showPreviousNextButtons(true);
  //alert("filterWeekEvents");
  if (currentDoc != null)
  {
    var dateEl = currentDoc.getElementsByTagName('date');
    for(i=0;i<dateEl.length;i++)
    {
      var wNR = dateEl[i].getAttribute("weeknr");
      var year = dateEl[i].getAttribute("year");
      var mt = document.getElementById("calLabel");
      if (mt!= null)
      {
        mt.innerHTML ='uge '+ wNR+' '+year;
        this.cViewID = parseInt(wNR,10);
        this.cViewYrID = parseInt(year,10);
      }
    }
    //  if ()
    var days = currentDoc.getElementsByTagName("day");
    for(i=0;i<days.length;i++)
    {
      var day = xmlDocument.createElement('day');
      day.setAttribute("datestr",days[i].getAttribute("datestr"));
      //day.setAttribute("week",days[i].getAttribute("week"));
      //day.setAttribute("year",days[i].getAttribute("year"));
      var events = days[i].getElementsByTagName("event");
      var ecount = 0;
      for(j=0;j<events.length;j++)
      {
        var type = events[j].getAttribute("type");
        if (filter.length == 0 || filter.contains(type) || (events[j].getAttribute("branch")=="1" && filter.contains("9")))
        {
          var e1 = xmlDocument.createElement('event');
          e1.setAttribute("id",events[j].getAttribute("id"));
          e1.setAttribute("comp",events[j].getAttribute("comp"));
          e1.setAttribute("comppost",events[j].getAttribute("comppost"));
          e1.setAttribute("short",events[j].getAttribute("short"));
          e1.setAttribute("longinfo",events[j].getAttribute("longinfo"));
          e1.setAttribute("jpg",events[j].getAttribute("jpg"));
          e1.setAttribute("type",events[j].getAttribute("type"));
          e1.setAttribute("town",events[j].getAttribute("town"));
          e1.setAttribute("time_format",events[j].getAttribute("time_format"));
          e1.setAttribute("branch",events[j].getAttribute("branch"));
          // this.eventArray
          this.eventArray[eventArray.length] = e1;
          ecount++;
          day.appendChild(e1);
        }
      }
      day.setAttribute("nr_events",""+ecount);
      root.appendChild(day);
    }

  }
  xmlDocument.appendChild(root);
  //alert(xmlDocument.xml);
  loadEvents(xmlDocument,calendarMainXSL);
}

function filterMonthEvents()
{
  var xmlDocument = this.getActiveXObject();
  var root = xmlDocument.createElement('month');
  showPreviousNextButtons(true);
  if (currentDoc != null)
  {
    
    var selMonth = currentDoc.getElementsByTagName("month");
    for (ix = 0 ; ix < selMonth.length;ix++)
    {
      this.cViewID = parseInt(selMonth[ix].getAttribute("m"));
      this.cViewYrID = parseInt(selMonth[ix].getAttribute("y"));
      var mt = document.getElementById("calLabel");
      if (mt!= null)
      {
        mt.innerHTML = selMonth[ix].getAttribute("mtext");
      }
    }
    
    var days = currentDoc.getElementsByTagName("day");
    for(i=0;i<days.length;i++)
    {
      var row = xmlDocument.createElement("row");
      for(k=0;k<7;k++)
      {
        var day = xmlDocument.createElement('day');
        day.setAttribute("week",days[i].getAttribute("week"));
        day.setAttribute("year",days[i].getAttribute("year"));
        if (days[i] != null)
        {
          if (i < 7)
          {
            day.setAttribute("datetext",days[i].getAttribute("dayname")+" "+days[i].getAttribute("datestr"));
          }
          else{
            day.setAttribute("datetext",days[i].getAttribute("datestr"));
          }
          day.setAttribute("date",days[i].getAttribute("date"));

          var mPos =  days[i].getAttribute("mpos");
          if (mPos == "-1" || mPos =="11")
          {
            day.setAttribute("month","previous");
          }
          else if (mPos == "0")
          {
            day.setAttribute("month","current");
          }
          else if (mPos == "1" || mPos =="-11")
          {
            day.setAttribute("month","next");
          }

          var events = days[i].getElementsByTagName("event");
          var ecount = 0;
          for(j=0;j<events.length;j++)
          {
            var type = events[j].getAttribute("type");
            if (filter.length == 0 || filter.contains(type) || (events[j].getAttribute("branch")=="1" && filter.contains("9")))
            {
              var e1 = xmlDocument.createElement('event');
              e1.setAttribute("id",events[j].getAttribute("id"));
              e1.setAttribute("comp",events[j].getAttribute("comp"));
              e1.setAttribute("comppost",events[j].getAttribute("comppost"));
              e1.setAttribute("title",events[j].getAttribute("short"));
              e1.setAttribute("longinfo",events[j].getAttribute("longinfo"));
              e1.setAttribute("jpg",events[j].getAttribute("jpg"));
              e1.setAttribute("type",events[j].getAttribute("type"));
              e1.setAttribute("town",events[j].getAttribute("town"));
              e1.setAttribute("time_format",events[j].getAttribute("time_format"));
              e1.setAttribute("branch",events[j].getAttribute("branch"));
              ecount++;
              day.appendChild(e1);
            }
          }
          day.setAttribute("nr_events",""+ecount);
          row.appendChild(day);
          
        }
        i++;
      }
      i--;
      root.appendChild(row);
    }

  }
  // alert(root.xml);
  xmlDocument.appendChild(root);
  //alert(xmlDocument.xml);
  loadEvents(xmlDocument,calendarMonthViewXSL);
}

function parseCalendarMouseOver()
{
  var xmlDoc = this.req.responseText;
  var el = document.getElementById('comp_card');
  if (el != null)
  {
    el.innerHTML = xmlDoc;
  }
  var tt = document.getElementById("ToolTip3");
  tt.style.visibility="visible";
}

function daysInMonth(iMonth, iYear)
{
  return 32 - new Date(iYear, iMonth, 32).getDate();
}




function parseWeekEvents()
{
  getPromotionText();
  var xmlDoc = this.req.responseXML;
  currentDoc = xmlDoc;
  
  filterEvents();
  
}
function parseMonthEvents()
{
  getPromotionText();
  var xmlDoc = this.req.responseXML;
  currentDoc = xmlDoc;
  filterMonthEvents();
  
}

function parseListEvents()
{
  getPromotionText();
  var xmlDoc = this.req.responseXML;
  currentDoc = xmlDoc;
  filterListEvents();
  
}

function getPromotionText()
{
  new net.ContentLoader('calendar/functions/getCalendarPromotion.php?date ='+new Date(),parseCalendarPromotion);
}

function parseCalendarPromotion()
{
  var xmlDoc = this.req.responseText;
  var el = document.getElementById("calPromotionText");
  if (el != null)
  {
    el.innerHTML = xmlDoc;
  }
}

function loadEvents(xDoc,xsl)
{
  var el = document.getElementById('cal_window');
  //alert(tastingXSL);
  if (xsl != null)
  {
    if (window.ActiveXObject)
    {
      el.innerHTML = xDoc.transformNode(xsl);
    //alert(el.innerHTML);
    }
    else
    {
      var xsltProcessor = new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);
      var fragment = xsltProcessor.transformToFragment(xDoc,document);

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

    }
  }

}

function showEvent(idx,view)
{
  //alert('calendar/functions/getCalendarEvent2.php?eventid='+idx+'&vType='+view+'&date ='+new Date());
  closeFunction =closeEventOverlay;
  // selectedEvent = idx;
  new net.ContentLoader('calendar/functions/getCalendarEvent2.php?eventid='+idx+'&vType='+view+'&date ='+new Date(),parseCalendarEvent);

  var el = document.getElementById("LayerCalendarEventOverlay");
  
  if (el != null)
  {
    var vis = el.style.visibility;
    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 = document.getElementById("LayerCalendar");
  //alert(el);
  if (el != null)
  {
    el.style.zIndex="10";

  }
  this.setBlackShade();

  el = document.getElementById("LayerCalendartranspWhite");
  //alert(el);
  if (el != null)
  {
    el.style.visibility="visible";

  }
  
}

function closeEventOverlay()
{
  eventUnSelected();
  var el = document.getElementById("LayerCalendarEventOverlay");
  if (el != null)
  {
    var vis = el.style.visibility;
    el.style.visibility="hidden";
    el.innerHTML="";

  }
  this.hideBlackShade();

  el = document.getElementById("LayerCalendartranspWhite");
  if (el != null)
  {
    el.style.visibility="hidden";

  }
  el = document.getElementById("LayerCalendar");
  if (el != null)
  {
    el.style.zIndex="1";

  }
  
}
function parseCalendarEvent()
{
  var xmlDoc = this.req.responseText;
  //alert(xmlDoc);
  var el = document.getElementById('LayerCalendarEventOverlay');
  if (el != null)
  {
    el.innerHTML = xmlDoc;
  }
}

function parseCalendarEvent2()
{

  //alert(this.req.responseText);
  var xmlDoc = this.req.responseXML;
  var el = document.getElementById('LayerCalendarEventOverlay');
  //alert(tastingXSL);
  if (calendarEventOverlayXSL != null)
  {
    if (window.ActiveXObject)
    {
      el.innerHTML = xmlDoc.transformNode(calendarEventOverlayXSL);
    // alert(el.innerHTML);
    }
    else
    {
      var xsltProcessor = new XSLTProcessor();
      xsltProcessor.importStylesheet(calendarEventOverlayXSL);
      var fragment = xsltProcessor.transformToFragment(xmlDoc,document);

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

    }
  }

      
}


function parseCalendarEventOverlayXSL()
{
  var res = this.req.responseXML;
  // alert(this.req.responseText);
  calendarEventOverlayXSL=  res;
}

function validateNumberInput(e,iField,chbox)
{
  var isNum=false;
  var key = window.event ? e.keyCode : e.which;
  var keychar = String.fromCharCode(key);
  reg = /\d/;
  if (reg.test(keychar))
  {
    isNum=true;
  }
  if (isNum)
  {
    var el = $(chbox);
    if (el != null)
    {
      el.checked='checked';
    }
  }
  return isNum;
}
