﻿/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function RTSearchTextBox(e, what, where, root) {
    var characterCode;
    if (e && e.which) { // NN4 specific code
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;  // IE specific code
    }
        if (characterCode == 13) {
            window.location = root + "busqueda.aspx?que=" + document.getElementById(what).value +
                      "&donde=" + document.getElementById(where).value;
            return false;
        }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function RTSearchButton(what, where, root) {
    window.location = root + "busqueda.aspx?que=" + document.getElementById(what).value +
                  "&donde=" + document.getElementById(where).value;
    return false;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function showBack(id, show, color) {
    //    document.getElementById(id + "_left").style.backgroundImage = show ? "url(" + root + "/images/gradientsShadows/menuizq.png)" : "";
    //    document.getElementById(id + "_right").style.backgroundImage = show ? "url(" + root + "/images/gradientsShadows/menuder.png)" : "";
    //    document.getElementById(id + "_center").style.backgroundImage = show ? "url(" + root + "/images/gradientsShadows/menulinea.png)" : "";
    //    document.getElementById(id).style.color = show ? 'white' : '';

    document.getElementById(id + "_left").className = show ? "MenuSearchLeft" : "";
    document.getElementById(id + "_right").className = show ? "MenuSearchRight" : "";
    document.getElementById(id + "_center").className = show ? "MenuSearchSelected" : "MenuSearch"+color;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function IsValidPositiveNumber(sText) {
    var IsNumber = false;
    if ((sText != null) && (sText.length != 0)) {
        var ValidChars = "0123456789";
        var Char;
        IsNumber = true;

        for (i = 0; i < sText.length && IsNumber == true; i++) {
            Char = sText.charAt(i);
            if (ValidChars.indexOf(Char) == -1) {
                IsNumber = false;
            }
        }
    }
    return IsNumber;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function detectFile(fileUploadID, buttonID)
{
  var filename = document.getElementById(fileUploadID).value;

  if (filename.length > 5) {
      if (filename.indexOf(":") && filename.indexOf(".")) {

          //Setting the extension array for diff. type of text files 
          var extArray = new Array(".gif", ".jpg", ".jpeg", ".bmp", ".png", ".tif");

          //getting the file name
          while (filename.indexOf("\\") != -1)
              filename = filename.slice(filename.indexOf("\\") + 1);

          //Getting the file extension
          var ext = filename.slice(filename.indexOf(".")).toLowerCase();

          //matching extension with our given extensions.
          for (var i = 0; i < extArray.length; i++) {
              if (extArray[i] == ext) {
                  document.getElementById(buttonID).click();
                  return;
              }
          }
          alert("Sólo se aceptan las siguientes extensiones:  "
           + (extArray.join("  ")) );
          return;
      
    }
  }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function TruncateSentence(sentence, maxLength) {
    var words = sentence.split(' ');
    var result = sentence;

    if (sentence.length > maxLength) {
        result = '';
        for (i = 0; i < words.length; i++) {
            if (result.length + words[i].length + 3 < maxLength) {
                result += words[i] + ' ';
            }
            else {
                break;
            }
        }
        result.length--;
        result += '...';
    }
    
    return result;
}
/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function PressEnterKeyInTextBox(e, buttonid) {
    var characterCode;
    if (e && e.which) { // NN4 specific code
        e = e;
        characterCode = e.which;
    }
    else {
        e = event;
        characterCode = e.keyCode;  // IE specific code
    }
    // var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt) {
        if (characterCode == 13) {
            bt.click();
            return false;
        }
    }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function FormatLocalTime(d, longFormat) {
    function TwoDigits(s) {
        var ss = s.toString();
        if (ss.length == 1)
            return ('0' + ss);
        return (ss);
    }

    var monts = new Array("ene", "feb", "mar", "abr", "may", "jun", "jul", "ago", "sep", "oct", "nov", "dic");
    var Year = d.getFullYear().toString();
    var Month = d.getMonth();
    var Day = TwoDigits(d.getDate());
    var Hours = d.getHours();
    var Minutes = TwoDigits(d.getMinutes());
    var AmPm = 'am';
    if (Hours == 12) {
        AmPm = 'pm';
    }
    if (Hours == 0) {
        Hours = 12;
    }
    if (Hours > 12) {
        Hours -= 12;
        AmPm = 'pm';
    }
    var date = Day + '/' + monts[parseInt(Month)] + '/' + Year;
    if (longFormat) {
        date += ' ' + TwoDigits(Hours) + ':' + Minutes + ' ' + AmPm
    }
    return date;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function convertToDate(dateCoded) {
    var parts = dateCoded.split(',');
    var d = new Date(parts[0], parts[1], parts[2]);
    return d;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function getDateCoded(date) {
    return date.getFullYear() + ',' + date.getMonth() + ',' + date.getDate() ;
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function popupImagen(path, imagenes, e, activeImage, imgTitle) {
    if (e) {
        Event.stop(e);
    }
    var bg;
    if ($$(".bg").length == 0) {
        bg = new Element("div", { 'class': 'bg' });
        bg.setStyle({ width: "100%", height: getFullScreenSize().y + "px" });
        $$("body")[0].insert(bg);
    }
    else {
        bg = $$(".bg")[0];
        bg.show();

    }
    var popup;
    var actual = parseInt(activeImage);
    if ($$(".popupImagen").length == 0) {
        popup = new Element("div", { 'class': 'popupImagen' });
        $$("body")[0].insert(popup);
    }
    else {
        popup = $$(".popupImagen")[0];
    }
    var title = new Element("div", { 'class': 'title' });
    //title.update(imgTitle);
    popup.insert(title);
    var tache = new Element("div", { 'class': 'tache' });
    popup.insert(tache);
    popup.setStyle({ left: document.viewport.getWidth() / 2 - popup.getWidth() / 2 + "px" });
    popup.setStyle({ top: document.viewport.getScrollOffsets().top + document.viewport.getHeight() / 2 - popup.getHeight() / 2 + "px" });
    
    var paginador = new Element("div", { 'class': 'pager' });
    var izq = new Element("div", { 'class': 'flechas izq' });
    var der = new Element("div", { 'class': 'flechas der' });
    var numeroFoto = new Element("div", { 'class': 'numeroFoto' }).update("Foto <span class='actual'>" + (actual + 1) + "</span> de " + imagenes.length + "");
    paginador.insert(izq);
    paginador.insert(numeroFoto);
    paginador.insert(der);
    popup.insert(paginador);
    mostrarImagen(path, imagenes[actual], popup, imgTitle)

    izq.observe("click", function() {
        actual = actual - 1 < 0 ? imagenes.length - 1 : actual - 1;
        numeroFoto.down(".actual").update(actual + 1);
        mostrarImagen(path, imagenes[actual], popup, imgTitle);
    });

    der.observe("click", function() {
        actual = actual + 1 >= imagenes.length ? 0 : actual + 1;
        numeroFoto.down(".actual").update(actual + 1);
        mostrarImagen(path, imagenes[actual], popup, imgTitle);
    });

    var cerrar = function() {
        new Effect.Fade(popup, { duration: .5, queue: { position: 'end', scope: 'popin'} })
        new Effect.Fade(bg, { duration: .5, queue: { position: 'end', scope: 'popin' }, afterFinish: function() {
            bg.remove();
            popup.remove();
        } 
        })
    }
    tache.observe("click", function() {
        cerrar();
    });
    Event.observe(document, "keyup", function(e) {
        if (!bg) {
            return;
        }
        var tecla = teclas(e);
        if (tecla == 27) {
            cerrar();
        }
    });
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function mostrarImagen(path, imagen, popup, imgTitle) {
    var imagen = new Element("img", { src: path + imagen });
    var loader = new Element("div", { 'class': 'loader' });
    popup.insert(loader);
    cargandoImagen(imagen, function() {
        imagen.hide();
        loader.remove();
        popup.insert(imagen);
        var w = imagen.getWidth() < 200 ? 200 : imagen.getWidth();
        var h = imagen.getHeight();
        //popup.setStyle({width:w+"px",height:h+"px"});
        var title = popup.getElementsByClassName("title");          // 8 is a good aprox of the width of a char in Arial 12px
        title[0].update(TruncateSentence(imgTitle, (w - 30) / 8)); // 30 is the width for the close button
        imagen.setStyle({ left: 15 + (w - imagen.getWidth()) / 2 });
        if (imagen.previous("img")) {
            imagen.previous("img").remove();
        }
        $(popup).morph('width:' + w + 'px;height:' + h + 'px;', { duration: .3, afterUpdate: function() {
            popup.setStyle({ left: document.viewport.getWidth() / 2 - popup.getWidth() / 2 + "px" });
            popup.setStyle({ top: document.viewport.getScrollOffsets().top + document.viewport.getHeight() / 2 - popup.getHeight() / 2 + "px" });
        },
            afterFinish: function() {
                new Effect.Appear(imagen, { duration: .3, afterFinish: function() {
                }
                });
            }
        });
    });
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function getScreenSize() {
    var sx = document.documentElement.clientWidth > document.body.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth;
    var sy = document.documentElement.clientHeight > document.body.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
    return { x: sx, y: sy };
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function getMaxSize() {
    var sx = 0;
    var sy = 0;
    if (window.scrollMaxX || window.scrollMaxY) {
        var sx = window.scrollMaxX;
        var sy = window.scrollMaxY;
    }
    return { x: sx, y: sy };
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function getFullScreenSize() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = document.body.scrollWidth;
        yScroll = getScreenSize().y + window.scrollMaxY;
    }
    else {
        if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        }
        else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) { // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else {
        if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        }
        else {
            if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
            }
        }
    }

    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    }
    else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = windowWidth;
    }
    else {
        pageWidth = xScroll;
    }
    return { x: pageWidth, y: pageHeight };

}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function cargandoImagen(imagen, callback) {
    imagen = $(imagen);
    if (imagen.complete) {
        callback(imagen);
    }
    else {
        setTimeout(function() { cargandoImagen(imagen, callback) }, 100);
    }
}

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function teclas(e) {
    var key;
    if (document.all) {
        key = e.keyCode;
    }
    else {
        key = e.which;
    }
    return key;
};

/* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
function textCounter(fieldId, maxlimit) {
    var field = document.getElementById(fieldId);
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
}
