/*
# AddType text/html .shtml
AddHandler server-parsed .js
Options +Includes
*/



var meses = new Array('enero','febrero','marzo','abril','mayo','junio',
                      'julio','agosto','septiembre','octubre','noviembre','diciembre');
var dias = new Array('Domingo','Lunes','Martes','Miércoles','Jueves','Viernes','Sábado');

var s_inicial = 'Thu Jul 29 18:04:20 2010';
// var s_inicial = 'Thu Nov  6 21:43:03 2003'; //1068164910; // Thu Jul 29 18:04:20 2010;


// var t_inicial = new Date(s_inicial);
var t_inicial = new Date(2010,07 - 1,129 * 1 - 100,118 * 1 - 100,104 * 1 - 100,120 * 1 - 100);
var offsetDate = new Date();

// alert(s_inicial);



function strip0(num) {
  var n = num + '';
  if (n.charAt(0) == '0') {
    return n.charAt(1);
  }else{
    return n;
  };
};


function offset_now() {
  var ahora = new Date();
  return ahora.getTime() - offsetDate.getTime();
};

function get_fecha() {
  var fecha = new Date;
  // fecha.setTime(1000 * s_inicial + offset_now());
  fecha.setTime(t_inicial.getTime() + offset_now());
  var ano = fecha.getYear();
  if (ano < 200) { // onda 103, NS70
    ano = ano + 1900;
  };
  var mes = fecha.getMonth();
  var dia = fecha.getDate();
  var diasem = dias[fecha.getDay()];
  var mes = meses[mes];
  return diasem + ', ' + dia + ' de ' + mes + ' de ' + ano;
  // return dia + ' de ' + mes + ' de ' + ano;
};

function get_hora() {
  var fecha = new Date;
  var fechaGMT = new Date;
  // fecha.setTime(1000 * s_inicial + offset_now());
  fecha.setTime(t_inicial.getTime() + offset_now());
  fechaGMT.setTime(t_inicial.getTime() + offset_now() + 4 * 3600000); // Esto hay que actualizarlo 2 veces al año. Verano: 3, Invierno:4
  var hora = fecha.getHours();
  var min = fecha.getMinutes();
  var seg = fecha.getSeconds();
  var horaGMT = fechaGMT.getHours();
  var minGMT = fechaGMT.getMinutes();
  var segGMT = fechaGMT.getSeconds();
  // return w2(hora,2) + ':' + w2(min,2) + ':' + w2(seg,2) + ' (GMT ' + w2(horaGMT,2) + ':' + w2(minGMT,2) + ':' + w2(segGMT,2) + ')';
  return w2(hora,2) + ':' + w2(min,2) + ':' + w2(seg,2);
};

function w2(num,n) {
  var a = num + '';
  while (a.length < n) {
    a = '0' + a;
  };
  return a;
};

function mod_hace(s1,s2,s3) {

  if (s3 > s2) { s2 = s3; };
  var minutos = Math.abs(Math.floor((s1 - s2)/60));
  if (minutos == 0) { minutos = 1; };
  if ((minutos > 0) && (minutos < 60)) {
     return minutos + ' min';
  }else{
    var horas = Math.floor(minutos / 60);
    return horas + ' hr';
  };
};



function fecha_iso() {
  // Retorna la fecha del servidor en formato iso
  var fecha = new Date;

  // fecha.setTime(1000 * s_inicial + offset_now());
  fecha.setTime(t_inicial.getTime() + offset_now());

  var ano = fecha.getYear();
  if (ano < 200) { // onda 103, NS70
    ano = ano + 1900;
  };
  // alert('year:' + ano);
  var mes = fecha.getMonth();
  var dia = fecha.getDate();
  var diasem = dias[fecha.getDay()];
  mes = mes + 1;
  // return diasem + ', ' + dia + ' de ' + mes + ' de ' + ano;
  return ano + w2(mes,2) + w2(dia,2);
};

function anno_iso() {
  // Retorna anno en formato iso
  var fecha = new Date;

  // fecha.setTime(1000 * s_inicial + offset_now());
  fecha.setTime(t_inicial.getTime() + offset_now());

  var ano = fecha.getYear();
  if (ano < 200) { // onda 103, NS70
    ano = ano + 1900;
  };
  // alert('year:' + ano);
  return ano;
};


function print_date_or_time(aaaammdd_art, hhmm_art, tipo, fechaplong) {
  // Escribe la fecha u hora, dependiendo si la fecha del art. es de hoy o no.
  var isofec = fecha_iso();
  // alert(aaaammdd_art + ' y ' + isofec);
  if (aaaammdd_art == isofec) { // es de hoy
    // Escribe hora (puede venir formateada o no.)

    document.write('<span class="GralHora">');
    if (hhmm_art.substring(2,3) == ':') {
      document.write(hhmm_art);
    }
    else {
      document.write(hhmm_art.substring(0,2) + ':' + hhmm_art.substring(2,4));
    };
    document.write('</span>');
  }
  else {
    // Escribe fecha
    if (aaaammdd_art != '99999999') {
      document.write('<span class="GralFech">');
      if (fechaplong == '') {

        if (tipo == 's') { // sin ano
          document.write(aaaammdd_art.substring(6,8) + '/' + aaaammdd_art.substring(4,6));
        }
        else { // conano o ''
          document.write(aaaammdd_art.substring(6,8) + '/' + aaaammdd_art.substring(4,6) + '/' + aaaammdd_art.substring(0,4));
        };
      }
      else {
        document.write(fechaplong);
      };
      document.write('</span>');
    };
  };
};



