

/*
 +-------------------------------------------------------------------+
 |                 H T M L - C A L E N D A R   (v2.9)                |
 |                                                                   |
 | Copyright Gerd Tentler               www.gerd-tentler.de/tools    |
 | Created: May 27, 2003                Last modified: Jan. 10, 2009 |
 +-------------------------------------------------------------------+
 | This program may be used and hosted free of charge by anyone for  |
 | personal purpose as long as this copyright notice remains intact. |
 |                                                                   |
 | Obtain permission before selling the code for this program or     |
 | hosting this software on a commercial website or redistributing   |
 | this software over the Internet or in any other medium. In all    |
 | cases copyright must remain intact.                               |
 +-------------------------------------------------------------------+

 EXAMPLE #1:  myCal = new CALENDAR();
              document.write(myCal.create());

 EXAMPLE #2:  myCal = new CALENDAR(2004, 12);
              document.write(myCal.create());

 EXAMPLE #3:  myCal = new CALENDAR();
              myCal.year = 2004;
              myCal.month = 12;
              document.write(myCal.create());

 Returns HTML code
==========================================================================================================
*/
var cal_ID = 0;
var Posx = 0;
var Posy = 0;
var DivsMixtos = "";
var idInterval = 0;
function hiddenDiv(div) { 
    clearInterval(idInterval)
    idInterval = setInterval(function() { document.getElementById(div).style.display = 'none' }, 2000)    
}
function CALENDAR(year, month) {
//========================================================================================================
// Configuration
//========================================================================================================
  this.tFontFace = 'Arial, Helvetica'; // title: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.tFontSize = 10;                 // title: font size (pixels)
  this.tFontColor = '#000000';         // title: font color
  this.tBGColor = '#FFFFFF';           // title: background color

  this.hFontFace = 'Arial, Helvetica'; // heading: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.hFontSize = 10;                 // heading: font size (pixels)
  this.hFontColor = '#000000';         // heading: font color
  this.hBGColor = '#FFFFFF';           // heading: background color

  this.dFontFace = 'Arial, Helvetica'; // days: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.dFontSize = 10;                 // days: font size (pixels)
  this.dFontColor = '#000000';         // days: font color
  this.dBGColor = '#FFFFFF';           // days: background color

  this.wFontFace = 'Arial, Helvetica'; // weeks: font family (CSS-spec, e.g. "Arial, Helvetica")
  this.wFontSize = 10;                 // weeks: font size (pixels)
  this.wFontColor = '#000000';         // weeks: font color
  this.wBGColor = '#FFFFFF';           // weeks: background color

  this.saFontColor = '#000000';        // Saturdays: font color
  this.saBGColor = '#FFFFFF';          // Saturdays: background color

  this.suFontColor = '#000000';        // Sundays: font color
  this.suBGColor = '#FFFFFF';          // Sundays: background color

  this.tdBorderColor = '#FF0000';      // today: border color

  //this.borderColor = '#304B90';        // border color
  this.hilightColor = '#FFFF00';       // hilight color (works only in combination with link)
  this.tdCursor = 'pointer';       // 
  
  this.link = 'mainCenter.aspx';                      // page to link to when day is clicked
  this.offset = 2;                     // week start: 0 - 6 (0 = Saturday, 1 = Sunday, 2 = Monday ...)
  this.weekNumbers = false;             // view week numbers: true = yes, false = no

  this.TipoRemate = 0;
  this.EsTipoMixto = false;
  this.estadoActivo = true;
//--------------------------------------------------------------------------------------------------------
// You should change these variables only if you want to translate them into your language:
//--------------------------------------------------------------------------------------------------------
  // weekdays: must start with Saturday because January 1st of year 1 was a Saturday
  this.weekdays = ['S', 'D', 'L', 'M', 'M', 'J', 'V'];

  // months: must start with January
  this.months = ['ENERO', 'FEBRERO', 'MARZO', 'ABRIL', 'MAYO', 'JUNIO', 'JULIO',
                 'AGOSTO', 'SEPTIEMBRE', 'OCTUBRE', 'NOVIEMBRE', 'DICIEMBRE'];
  // error messages
  this.error = ['El aņo debe ser entre 1 - 3999!', 'El mes debe ser entre 1 - 12!'];

//--------------------------------------------------------------------------------------------------------
// Don't change from here:
//--------------------------------------------------------------------------------------------------------
  this.size = 0;
  this.mDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  if(year == null && month == null) {
    var obj = new Date();
    year = obj.getYear();
    if(year < 1900) year += 1900;
    month = obj.getMonth() + 1;
  }
  else if(year != null && month == null) month = 1;
  this.year = year;
  this.month = month;
  this.specDays = {};
 
  this.mouseMove = function(e) {
      Posx = (document.layers) ? e.pageX : event.x + document.body.scrollLeft
      Posy = (document.layers) ? e.pageY : event.y + document.body.scrollTop
      //status = "x:" + Posx + " y:" + Posy
      return true
  }
  
  document.onmousemove = this.mouseMove;
  if (document.layers) document.captureEvents(Event.MOUSEMOVE);


//========================================================================================================
// Functions
//========================================================================================================
  this.set_styles = function() {
    cal_ID++;
    var html = '<style> .cssTitle' + cal_ID + ' { ';
    if(this.tFontFace) html += 'font-family: ' + this.tFontFace + '; ';
    if(this.tFontSize) html += 'font-size: ' + this.tFontSize + 'px; ';
    if(this.tFontColor) html += 'color: ' + this.tFontColor + '; ';
    if(this.tBGColor) html += 'background-color: ' + this.tBGColor + '; ';
    html += '} .cssHeading' + cal_ID + ' { ';
    if(this.hFontFace) html += 'font-family: ' + this.hFontFace + '; ';
    if(this.hFontSize) html += 'font-size: ' + this.hFontSize + 'px; ';
    if(this.hFontColor) html += 'color: ' + this.hFontColor + '; ';
    if(this.hBGColor) html += 'background-color: ' + this.hBGColor + '; ';
    html += '} .cssDays' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
    if(this.dFontColor) html += 'color: ' + this.dFontColor + '; ';
    if(this.dBGColor) html += 'background-color: ' + this.dBGColor + '; ';
    html += '} .cssWeeks' + cal_ID + ' { ';
    if(this.wFontFace) html += 'font-family: ' + this.wFontFace + '; ';
    if(this.wFontSize) html += 'font-size: ' + this.wFontSize + 'px; ';
    if(this.wFontColor) html += 'color: ' + this.wFontColor + '; ';
    if(this.wBGColor) html += 'background-color: ' + this.wBGColor + '; ';
    html += '} .cssSaturdays' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
    if(this.saFontColor) html += 'color: ' + this.saFontColor + '; ';
    if(this.saBGColor) html += 'background-color: ' + this.saBGColor + '; ';
    html += '} .cssSundays' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
    if(this.suFontColor) html += 'color: ' + this.suFontColor + '; ';
    if(this.suBGColor) html += 'background-color: ' + this.suBGColor + '; ';
    html += '} .cssHilight' + cal_ID + ' { ';
    if(this.dFontFace) html += 'font-family: ' + this.dFontFace + '; ';
    if(this.dFontSize) html += 'font-size: ' + this.dFontSize + 'px; ';
    if(this.dFontColor) html += 'color: ' + this.dFontColor + '; ';
    if(this.hilightColor) html += 'background-color: ' + this.hilightColor + '; ';
    if(this.tdCursor) html += 'cursor: ' + this.tdCursor + '; ';
    //html += 'cursor: default; ';
    html += '} </style>';

    return html;
  }

  this.leap_year = function(year) {
    return (!(year % 4) && (year < 1582 || year % 100 || !(year % 400))) ? true : false;
  }

  this.get_weekday = function(year, days) {
    var a = days;
    if(year) a += (year - 1) * 365;
    for(var i = 1; i < year; i++) if(this.leap_year(i)) a++;
    if(year > 1582 || (year == 1582 && days >= 277)) a -= 10;
    if(a) a = (a - this.offset) % 7;
    else if(this.offset) a += 7 - this.offset;

    return a;
  }

  this.get_week = function(year, days) {
    var firstWDay = this.get_weekday(year, 0);
    return Math.floor((days + firstWDay) / 7) + (firstWDay <= 3);
  }

  this.table_cell = function(content, cls, date, style) {
      var size = Math.round(this.size * 1.5);
      var clsName = cls.toLowerCase();
      var html = '<td id="((-IDTD-))" align=center width=' + size + ' class="' + cls + '"';

      //if(content != '&nbsp;' && clsName.indexOf('day') != -1) {
      var nombreDivMixto = "";
      var htmlMixto = "";
      if (!isNaN(content)) {
          var link = this.link;

          if (this.specDays[content]) {
              if (this.specDays[content][0]) {
                  style += 'background-color:' + this.specDays[content][0] + ';';
              }
              if (this.specDays[content][1]) {
                  html += ' title="' + this.specDays[content][1] + '"';
              }
              if (this.specDays[content][2]) link = this.specDays[content][2];
          }
          var arr = date.split('-');
          //alert(date);
          var idTabla = "TDContenedor" + arr[2] + arr[1] + arr[0];
          if (link && existeFecha(arr[2], arr[1], arr[0])) {
              if (estadoActivo == 'true'){
                  if (EsTipoMixto) {
                      nombreDivMixto = "divDiaMes" + arr[2] + arr[1] + arr[0];
                      DivsMixtos += '<div id="' + nombreDivMixto + '" style="position: absolute; z-index: 1000; display: none; top: 0px; left: 0px; border: 2px solid #808080; width:180px; height:10px;" onmouseout="hiddenDiv(\'' + nombreDivMixto + '\');">'
                      DivsMixtos += '    <table width="180" height="10" border="0" cellpadding="0" cellspacing="0" >'
                      DivsMixtos += '        <tr>'
                      DivsMixtos += '            <td nowrap="nowrap" bgcolor="#009D06" class="TextoRematesCategoriasCale" align="center" width="90" style="cursor:hand"'
                      DivsMixtos += '    onClick="document.getElementById(\'' + nombreDivMixto + '\').style.display=\'none\'; top.cargarFrames(\'icenter\',\'centerRematesFecha.aspx?id_origen=' + arr[0] + '-' + arr[1] + '-' + arr[2] + '&id_rematetipo=1&id_clase=2\');">'
                      DivsMixtos += '                <a href="#">Propiedades</a>'
                      DivsMixtos += '            </td>'
                      DivsMixtos += '            <td nowrap="nowrap" bgcolor="#00B3ED" class="TextoRematesCategoriasCale" width="90" style="cursor:hand"'
                      DivsMixtos += '    onClick="document.getElementById(\'' + nombreDivMixto + '\').style.display=\'none\'; top.cargarFrames(\'icenter\',\'centerRematesFecha.aspx?id_origen=' + arr[0] + '-' + arr[1] + '-' + arr[2] + '&id_rematetipo=2&id_clase=2\');">'
                      DivsMixtos += '                <a href="#">Bienes Muebles</a></td>'
                      DivsMixtos += '        </tr>'
                      DivsMixtos += '    </table>'
                      DivsMixtos += '</div> '
                      arr[2] = ((arr[2].length == 1) ? '0' + arr[2] : arr[2]);
                      html += ' onClick="document.getElementById(\'' + nombreDivMixto + '\').style.left=((Posx - 80) < 0) ? 0 : (Posx - 80);document.getElementById(\'' + nombreDivMixto + '\').style.top=(Posy-10);document.getElementById(\'' + nombreDivMixto + '\').style.display=\'\';"';                       
                      
                  } else { 
                      arr[2] = ((arr[2].length == 1) ? '0' + arr[2] : arr[2]);
                      html += ' onMouseOver="this.className=\'' + cls + '\'"';
                      html += ' onMouseOut="this.className=\'' + cls + '\'"';
                      html += ' onClick="top.cargarFrames(\'icenter\',\'centerRematesFecha.aspx?id_origen=' + arr[0] + '-' + arr[1] + '-' + arr[2] + '&id_rematetipo=' + TipoRemate + '&id_clase=2\');"'                          
                  }
              }
          }
      }

      if (style) html += ' style="' + style + '"';

      //if (EsTipoMixto == false) {
      html += '>' + content + '</td>';
      html = html.replace("((-IDTD-))", idTabla)
      //} else {
      //          html += ' onclick="javascript:document.getElementById(\'' + nombreDivMixto + '\').style.display=\'\';" >' + htmlMixto + content + '</td>';
      //}

      return html;
  }
    
  

  this.table_head = function(content) {
    var html, ind, wDay, i;
    var cols = this.weekNumbers ? 8 : 7;

    html = '<tr><td colspan=' + cols + ' class="TextoMesCalendario' + '" align=right><b>' +
           content + '</b></td></tr><tr>';
    for(i = 0; i < this.weekdays.length; i++) {
      ind = (i + this.offset) % 7;
      wDay = this.weekdays[ind];
      //html += this.table_cell(wDay, 'cssHeading' + cal_ID);
      html += this.table_cell(wDay, 'TextoSemanaCalendario');
    }
    if(this.weekNumbers) html += this.table_cell('&nbsp;', 'TextoSemanaCalendario');
    html += '</tr>';

    return html;
  }

  this.viewEvent = function(from, to, color, title, link) {
    if(from > to) return;
    if(from < 1 || from > 31) return;
    if(to < 1 || to > 31) return;

    while(from <= to) {
      this.specDays[from] = [color, title, link];
      from++;
    }
  }

  this.create = function() {
      var obj, html, curYear, curMonth, curDay, start, stop, title, daycount,
        inThisMonth, weekNr, wdays, days, ind, cls, style, content, date, i;

      this.size = (this.hFontSize > this.dFontSize) ? this.hFontSize : this.dFontSize;
      if (this.wFontSize > this.size) this.size = this.wFontSize;

      obj = new Date();
      curYear = obj.getYear();
      if (curYear < 1900) curYear += 1900;
      curMonth = obj.getMonth() + 1;
      curDay = obj.getDate();

      if (this.year < 1 || this.year > 3999) html = '<b>' + this.error[0] + '</b>';
      else if (this.month < 1 || this.month > 12) html = '<b>' + this.error[1] + '</b>';
      else {
          this.mDays[1] = this.leap_year(this.year) ? 29 : 28;
          for (i = days = 0; i < this.month - 1; i++) days += this.mDays[i];

          start = this.get_weekday(this.year, days);
          stop = this.mDays[this.month - 1];

          html = this.set_styles();
          html += '<table border=0 cellspacing=0 cellpadding=0><tr>';
          html += '<td' + (this.borderColor ? ' bgcolor=' + this.borderColor : '') + '>';
          html += '<table border=0 cellspacing=0 cellpadding=3>';
          title = this.months[this.month - 1] + ' ' + this.year;
          html += this.table_head(title);
          daycount = 1;

          if ((this.year == curYear) && (this.month == curMonth)) inThisMonth = true;
          else inThisMonth = false;

          if (this.weekNumbers) weekNr = this.get_week(this.year, days);

          while (daycount <= stop) {
              html += '<tr>';

              for (i = wdays = 0; i <= 6; i++) {
                  ind = (i + this.offset) % 7;
                  if (ind == 0) cls = 'TextoDiaCalendario';
                  else if (ind == 1) cls = 'TextoDiaCalendario';
                  else cls = 'TextoDiaCalendario';

                  style = '';
                  date = this.year + '-' + this.month + '-' + daycount;

                  if ((daycount == 1 && i < start) || daycount > stop) content = '&nbsp;';
                  else {
                      content = daycount;
                      //if(inThisMonth && daycount == curDay) {
                      if (existeFecha(daycount, this.month, this.year)) {
                          if (EsTipoMixto == false) {
                              if (TipoRemate == 1) {
                                  if (estadoActivo == 'true'){
                                    cls = 'TextoDiaCalendarioSel';
                                  }else{
                                    cls = 'TextoDiaCalendarioSel_gris';
                                  }
                              }
                              else {
                                  if (estadoActivo == 'true'){
                                    cls = 'TextoDiaCalendarioSelBM';
                                  }else{
                                    cls = 'TextoDiaCalendarioSelBM_gris';
                                  }                                    
                              }
                          } else {
                              if (estadoActivo == 'true'){
                                cls = 'TextoDiaCalendarioSelMixto';
                              }else{
                                cls = 'TextoDiaCalendarioSelMixto_gris';
                              }                                      
                          }
                          //style = 'padding:0px;border:3px solid ' + this.tdBorderColor + ';';
                      }
                      else if (this.year == 1582 && this.month == 10 && daycount == 4) daycount = 14;
                      daycount++;
                      wdays++;
                  }
                  html += this.table_cell(content, cls, date, style);
              }

              if (this.weekNumbers) {
                  if (!weekNr) {
                      if (this.year == 1) content = '&nbsp;';
                      else if (this.year == 1583) content = 52;
                      else content = this.get_week(this.year - 1, 365);
                  }
                  else if (this.month == 12 && weekNr >= 52 && wdays < 4) content = 1;
                  else content = weekNr;

                  html += this.table_cell(content, 'cssWeeks' + cal_ID);
                  weekNr++;
              }
              html += '</tr>';
          }
          html += '</table></td></tr></table>' + DivsMixtos;
      }
      return html;
  }
}
function existeFecha(dd,mm,aa) {
    EsTipoMixto = false;
    var fec = new Date(aa, parseInt(eval(mm)) - 1, parseInt(eval(dd)));
    var antFecha="";
    var antTipo;
    var encontro = false;
    for(k=0;k<arr_fechas.length;k++) {
      var arrFT = arr_fechas[k];
      var arr_date = arrFT[0];      
      var ff = arr_date.split('-');
      var f = new Date(parseInt(ff[2]), parseInt(eval(ff[1])) - 1, eval(ff[0]));
      if (encontro==false) {
          if (f.valueOf() == fec.valueOf()) {
              antFecha = f.valueOf()
              TipoRemate = arrFT[1]
              estadoActivo = arrFT[2]
              encontro = true;              
          }
      } else {
          if (antFecha == f.valueOf()) {
              if (TipoRemate != arrFT[1]) {
                  EsTipoMixto = true; //Mixto
                  estadoActivo = arrFT[2]
                  encontro = true;
                  break;
              }
          } else {
            encontro = true;
             break;
          }
      }
    }
    return encontro;
}

