﻿var _currdate_control_id;
var _perioddiv_control_id; 
var _eventsdiv_control_id;
var _listeventsdiv_control_id;
var _control_id; 
var _month_span_id; 
var _direction;
var _month_of_year;
var _day_of_week;
var _actualview;
var _year;
var _iniyear;
var _positionx;
var _positiony;
var _properties_id;
var _intval=""
var _calendar_table_id;
    

function callback_RenderPeriodMonths(res){
  eval(res);
  var m_names = _month_of_year;
  currdate_control = document.getElementById(_currdate_control_id);
	var thisdate = new Date(res.FromDate);
	//Title
	if (_actualview == "month-year"){
	    altercontent(_month_span_id, m_names[thisdate.getMonth()] + " " + thisdate.getFullYear()); 
	    
	    var s = "";
    	if(res.ClickableDates != null) {
	        for(var i=0; i<res.ClickableDates.length; i++)
		        s += "<br/>-<a href='#' onclick=\"selectperiod('" + _control_id + "','" + res.ClickableDates[i] + "')\">" + res.ClickableDates[i] + "</a>"; 
		}
    		
	    currdate_control.value = res.FromDate;
    		
	    altercontent(_perioddiv_control_id, GetCalendarDaysHTML(res));
	}
	
	if (_actualview == "month"){
	    altercontent(_month_span_id, m_names[thisdate.getMonth()] + " " + thisdate.getFullYear()); 
	}
}

function IsClickable(res, month, day, year)
{
    if(res.ClickableDates != null) {
      for(var i=0; i<res.ClickableDates.length; i++)
      {
        var PeriodDay = new Date(res.ClickableDates[i]);
        if (PeriodDay.getMonth() == month && PeriodDay.getDate() == day && PeriodDay.getFullYear() == year)
        {
          return true; 
        }
      }
  }
  return false;
}

function IsHoliday(res, month, day, year){
  for(var j=0; j<res.Holidays.length; j++)
  {
    var Holiday = new Date(res.Holidays[j]);
    if (Holiday.getMonth() == month && Holiday.getDate() == day && Holiday.getFullYear() == year)
    {
      return true; 
    }
  }
  return false;
}

function GetCalendarDaysHTML(res){
    //  Sets Arrays
    var day_of_week = _day_of_week;
    var month_of_year = _month_of_year;

    //  Declare and initialize variables
    var Calendar = new Date(res.FromDate);

    var year = Calendar.getFullYear(); // Returns year

    var month = Calendar.getMonth();    // Returns month (0-11)
    var today = Calendar.getDate();    // Returns day (1-31)
    var weekday = Calendar.getDay();    // Returns day (1-31)

    var DAYS_OF_WEEK = 7;    // "constant" for number of days in a week
    var DAYS_OF_MONTH = 31;    // "constant" for number of days in a month
    var cal;    // Used for printing

    Calendar.setDate(1);    // Start the calendar day at '1'
    Calendar.setMonth(month);    // Start the calendar month at now

    var HolidayCssClass = "";


    cal = '<table class="calendar_drop_down_calendar">';
    cal += '<tr class="calendar_days_of_week">';


    // Loops for each day of week
    for(index=0; index < DAYS_OF_WEEK; index++){
        // Bold today´s day of week
        if(weekday == index)
        cal += '<td>' + '' + day_of_week[index] + '' + '</td>';

        // Prints day
        else
        cal += '<td>' + day_of_week[index] + '</td>';
        }

        cal += '</td></tr>';
        cal += '<tr>';

        // Fill in blank gaps until today´s day
        for(index=0; index < Calendar.getDay(); index++)
        cal += '<td>' + '  ' + '</td>';

        // Loops for each day in calendar
        for(index=0; index < DAYS_OF_MONTH; index++){
            if( Calendar.getDate() > index ){
              // Returns the next day to print
              week_day =Calendar.getDay();

              // Start new row for first day of week
              if(week_day == 0)
              cal += '<tr>';

              if(week_day != DAYS_OF_WEEK){

              // Sets variable inside loop for incrementing purposes
              var day  = Calendar.getDate();
              var now = new Date();
              
              var day_str = (month+1) + "/" + day + "/" + year;
              
              if (IsHoliday(res, month, day, year)) { HolidayCssClass = "calendar_holiday_date"; }
              else { HolidayCssClass = ""; }

              // Highlisht today´s date
              if(month == now.getMonth()&& day == now.getDate() && year == now.getFullYear() ){
                if (IsClickable(res,month,day,year)){
                  cal += "<td class=\"calendar_regular_date calendar_todays_date " + HolidayCssClass + "\"><a class=\"calendar_clickable_date\" onclick=\"selectperiod('" + _control_id + "','" + day_str + "')\">" + day + "</a></td>"; 
                }    
                else{
                  cal += "<td class=\"calendar_regular_date calendar_todays_date " + HolidayCssClass + "\">" + day + "</td>"; 
                } 
              }
              // Prints day
              else{
                if (IsClickable(res,month,day,year)){
                  cal += "<td class=\"calendar_regular_date " + HolidayCssClass + "\"><a class=\"calendar_clickable_date\" onclick=\"selectperiod('" + _control_id + "','" + day_str + "')\">" + day + "</a>" + "</td>"; 
                } 
                else{  
                  cal += "<td class=\"calendar_regular_date " + HolidayCssClass + "\">" + day + "</td>"; 
                }
              }
          }

          // End row for last day of week
          if(week_day == DAYS_OF_WEEK)
          cal += '</tr>';
          }

          // Increments until end of the month
          Calendar.setDate(Calendar.getDate()+1);

    }// end for loop

    cal += '</table>';

    //  Print calendar
    return cal;
}

function GetCalendarMonthsHTML(){
    var month_of_year = _month_of_year;

    var MONTHS_OF_YEAR = 12;    // "constant" for number of months in a year
    var cal;    // Used for printing

    cal = '<table class="calendar_drop_down_calendar">';
    cal += '<tr>';

    // Loops for each day of week
    for(index=0; index < MONTHS_OF_YEAR; index++){
        
        cal += "<td class=\"calendar_regular_date\"><a class=\"calendar_clickable_date\" onclick=\"SelectView(" + (index + 1) + ")\">" + month_of_year[index].substring(0,3) + "</a></td>"; 
        
        if (index == 2 || index == 5 || index == 8)
            cal += '</tr><tr>';

    }// end for loop
    cal += '</tr>';
    cal += '</table>';

    //  Print calendar
    return cal;
}

function GetCalendarYearsHTML(direction){

    var YEARS = 12;    // "constant" for number of years to display
    var cal;    // Used for printing

    cal = '<table class="calendar_drop_down_calendar">';
    cal += '<tr>';
    

    if (direction == "prev"){
       _year = _year - (YEARS * 2);
    }
    
    _iniyear = _year;
    
     // Loops for each day of week
    for(index=0; index < YEARS; index++){
       cal += "<td class=\"calendar_regular_date\"><a class=\"calendar_clickable_date\" onclick=\"SelectView(" + _year + ")\">" + _year + "</a></td>"; 
        _year += 1;
        if (index == 2 || index == 5 || index == 8)
            cal += '</tr><tr>';
            
    }// end for loop
    
    cal += '</tr>';
    cal += '</table>';

    //  Print calendar
    return cal;
}

function RenderPeriodMonths(Pcurrdate_control_id, Pperioddiv_control_id, Pcontrol_id, Pmonth_span_id, Peventsdiv_control_id, PListeventsdiv_control_id, Pdirection, PDays, PMonths, PCalendarTable){   
  //Sets variables
  _currdate_control_id = Pcurrdate_control_id;
  _perioddiv_control_id = Pperioddiv_control_id;
  _control_id = Pcontrol_id;
  _month_span_id = Pmonth_span_id; 
  _direction = Pdirection;
  _eventsdiv_control_id = Peventsdiv_control_id;
  _listeventsdiv_control_id  = PListeventsdiv_control_id;
  _month_of_year = PMonths;
  _day_of_week = PDays;
  _calendar_table_id = PCalendarTable;
  
  ShowDivEvent(false, _eventsdiv_control_id);
  
  if (_actualview == null){
    _actualview = "month-year";
  }
  if (_actualview == "month-year"){  
        //Loading 
       altercontent(_perioddiv_control_id, "<div class='calendar_loading_icon'><img src='_layouts/eMarketingLAResources/Images/spinner.gif' /></div>");
      
      currdate_control = document.getElementById(_currdate_control_id);
	  properties = document.getElementById(GetEventsCalendarProperties());
	  
      if (_direction == 'next') { CallGetMonthPeriods("GetNextMonthPeriods," + currdate_control.value + "," + properties.value); }
	    else if (_direction == 'prev') { CallGetMonthPeriods("GetPrevMonthPeriods," + currdate_control.value + "," + properties.value); }
	    else { CallGetMonthPeriods("GetThisMonthPeriod," + currdate_control.value + "," + properties.value); }
  }
  
  if (_actualview == "month"){
        if (_direction == 'next'){
            _year += 1;
        }
        else{
            _year = _year - 1;
        }
        altercontent(_month_span_id, _year); 
  }
  
  if (_actualview == "year"){
    var vyear = _year;
    altercontent(_perioddiv_control_id, GetCalendarYearsHTML(_direction));
    altercontent(_month_span_id, _iniyear + "-" + (_year - 1));
  }
}

function altercontent(elementid, content)
{
  //if IE 4+
  if (document.all) document.getElementById(elementid).innerHTML=content;
  //else if NS 6 (supports new DOM)
  else if (document.getElementById)
  {
    rng = document.createRange();
    el = document.getElementById(elementid);
    rng.setStartBefore(el);
    htmlFrag = rng.createContextualFragment(content);
    while (el.hasChildNodes()) el.removeChild(el.lastChild);
    el.appendChild(htmlFrag);
  }
}

function ChangeView(){
     
	 ShowDivEvent(false, _eventsdiv_control_id);
	 
     if (_actualview == "month"){
        _actualview = "year";
        altercontent(_perioddiv_control_id, GetCalendarYearsHTML("next"));
        altercontent(_month_span_id, _iniyear + "-" + (_year - 1));        
    }
    
    if (_actualview == "month-year"){
        _actualview = "month";
        
        currdate_control = document.getElementById(_currdate_control_id);
        var thisdate = new Date(currdate_control.value); 
        
        altercontent(_perioddiv_control_id, GetCalendarMonthsHTML());
        altercontent(_month_span_id, thisdate.getFullYear());  
        _year = thisdate.getFullYear(); 
    }
}

function SelectView(data){
     if (_actualview == "month"){
         _actualview = "month-year";
         altercontent(_perioddiv_control_id, "<div class='calendar_loading_icon'><img src='_layouts/eMarketingLAResources/Images/spinner.gif' /></div>");
		 properties = document.getElementById(GetEventsCalendarProperties());
         CallGetMonthPeriods("GetThisMonthPeriod," + (data + "/1/" + _year) + "," + properties.value);
     }
        
     if (_actualview == "year"){
        _actualview = "month";
        _year = data;
        
        altercontent(_perioddiv_control_id, GetCalendarMonthsHTML());
        altercontent(_month_span_id, _year); 
    }
   
}

function selectperiod(control_id, txtValue){
	findposition(document.getElementById(_calendar_table_id));
	
	properties = document.getElementById(GetEventsCalendarProperties());
	CallGetDisplayEvents("GetDisplayEvents," + txtValue + "," + properties.value);
}
function callback_DisplayEvents(arg) {
    var events = arg;
	altercontent(_listeventsdiv_control_id, events);
	ShowDivEvent(true, _eventsdiv_control_id, _positionx - document.getElementById(_eventsdiv_control_id).offsetWidth, _positiony );
	
	if(_intval!=""){
       clearInterval(_intval)
	}
		
	_intval = setInterval("ShowDivEvent(false, _eventsdiv_control_id)", 3000);
}
var _gRValue;
function ReceiveGetMonthPeriods(arg) {
    _gRValue = arg;
    window.setTimeout('__ReceiveGetMonthPeriods(_gRValue)',0);  
}
function __ReceiveGetMonthPeriods(arg) { 
    callback_RenderPeriodMonths(arg);
}

function ReceiveIsClickableDate(arg) {
    _gRValue = arg;
    window.setTimeout('__ReceiveIsClickableDate(_gRValue)',0);  
}
function __ReceiveIsClickableDate(arg) { 
    callback_RenderPeriodMonths(arg);
}
function ReceiveDisplayEvents(arg) {
    _gRValue = arg;
    window.setTimeout('__ReceiveDisplayEvents(_gRValue)',0); 
}
function __ReceiveDisplayEvents(arg) {
    callback_DisplayEvents(arg);
}
function TimeDivEvents(value){
	if (value == "false"){
		clearInterval(_intval);
	}
	else{
		_intval = setInterval("ShowDivEvent(false, _eventsdiv_control_id)", 3000);
	}
}

function findposition(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft + document.body.scrollLeft;
			curtop += obj.offsetTop + document.body.scrollTop;
		}
	}
	_positionx = curleft;
	_positiony= curtop;
  
}



