// $Id: tools.js,v 1.84 2011/02/12 01:00:00 rbravo Exp $
//
var hname = location.hostname;
var path = location.pathname;
var target_url = hname + path;
var base_dir = get_basedir(target_url);
var highlighted_origclass = null;
var highlight_blocked = false;

function get_basedir(url) {
    var url_parts = url.split('/');
    var parts_qty = url_parts.length;
    var parts_count = parts_qty;
    var output = '';
    while ( parts_count ) {
        parts_count--;
        if ( parts_count < parts_qty - 1 || url_parts[parts_count].indexOf('.') == -1 ) {
            output = url_parts[parts_count] + '/' + output;
        }
    }
    output = 'http://' + output;
    return output;
}
function openwin(url) {
    popupwin ( url, 1000, 700, 40, 40 );
}
function popupwin(url, width, height, left, top) {
    var nue_vent = window.open (url, 'pop_win', config='height=' + height + ', width=' + width + ', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
    if ( ! nue_vent.opener ) nue_vent.opener = self;
    nue_vent.moveTo (left, top);
}
/*
 * checkall.js
 *    uso:
 *    <form name="myform" id="myform">
 *    <input type="hidden" name="cant_seleccionados">
 *    <input type="checkbox" name="checkall" class="checkall" onClick="checkAll(myform, 'nombre_campo');">
 *    <input type="checkbox" ... class="nombre_campo" onClick="aToggle(myform);">
 * setea la variable "cant_seleccionados" para poder mostrar en un alert su cantidad
*/
function checkAll(formId, cName) {
    if ( formId.checkall.checked ) {
        var checked_counter = 0;
        for (i=0,n=formId.elements.length;i<n;i++) {
            if (formId.elements[i].className.indexOf(cName) !=-1) {
                formId.elements[i].checked = true;
                checked_counter++;
            }
        }
        // formId.cant_seleccionados.value=0; // para que el cartel diga "todos los elementos"
        formId.cant_seleccionados.value = checked_counter;
    } else {
        for (i=0,n=formId.elements.length;i<n;i++)
            if (formId.elements[i].className.indexOf(cName) !=-1)
                formId.elements[i].checked = false;
    }
}
function aToggle(formId) {
    formId.checkall.checked = false;
    formId.cant_seleccionados.value = 0;
    for (i=0,n=formId.elements.length;i<n;i++)
        if ( formId.elements[i].checked )
                formId.cant_seleccionados.value++;
}
function sdi_checkall(this_checkbox, class_name) {
    var myform = this_checkbox.form;
    var new_checked = this_checkbox.checked ? true : false;
    var elems_list = myform.elements;
    var elems_qty = elems_list.length;
    for ( var i=0; i<elems_qty; i++ ) {
        if ( elems_list[i].type == 'checkbox' && elems_list[i].className == class_name ) {
            var my_checkbox = elems_list[i];
            my_checkbox.checked = new_checked;
            if ( my_checkbox.fireEvent ) {
                my_checkbox.fireEvent('onchange');
            } else if ( document.createEvent ) {
                var evt = document.createEvent('HTMLEvents');
                if ( evt.initEvent ) evt.initEvent('change', true, true);
                if ( my_checkbox.dispatchEvent ) my_checkbox.dispatchEvent(evt);
            }
        }
    }
    return;
}
function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}
function add(input_field, delta, limit) {
    // adds to input numeric value delta (positive or negative) up to limit (to use with up/down arrows)
    if ( delta == undefined || isNaN(delta) ) delta = 1;
    if ( input_field != undefined ) {
        if ( typeof(input_field) != 'object' ) {
            var input_field = document.getElementById(input_field);
        }
        var myvalue = input_field.value;
        var new_value = myvalue
        var dec_separator = myvalue.indexOf(',') == -1 ? '.' : ',';
        if ( dec_separator == ',' ) myvalue = myvalue.replace(',', '.');
        var limit_no = typeof limit == 'undefined' ? 0 : parseFloat(limit, 10);
        limit_no = limit_no * 1;
        delta = parseInt(delta, 10);
        myvalue = parseFloat(myvalue, 10);
        if ( (delta > 0 && myvalue + delta <= limit_no) || (delta < 0 && myvalue + delta >= limit_no) ) {
            new_value = myvalue + delta;
            if ( new_value.toString().indexOf('.') != -1 ) {
                new_value = new_value.toFixed(2);
                var value_parts = new_value.toString().split('.');
                new_value = value_parts[0];
                if ( typeof value_parts[1] != 'undefined' ) {
                    new_value += dec_separator + value_parts[1];
                }
            }
        } else {
            alert('limit: ' + limit_no);
            new_value = limit_no;
        }
        input_field.value = new_value;
    }
    return;
}
function num_ar2us(num) {
    num = num + '';
    output = num.replace(/\,/g,'.');
    return output;
}
function num_us2ar(num) {
    num = num + '';
    output = num.replace(/\./g,',');
    return output;
}
function is_valid(field, type, msg, maybe_empty) {
    if ( maybe_empty == undefined ) maybe_empty = false;
    var value = field.value;
    if ( maybe_empty ) {
        if ( trim(value) == '' ) {
            field.value = '';
            return true;
        }
    }
    var bodies = document.getElementsByTagName('body');
    var body_lang = bodies[0].lang;
    var myregex = /^\d+$/;
    if ( msg == undefined ) msg = 'error';
    if ( type == 'alfa' ) myregex = /^[a-zA-Z]+$/;
    if ( type == 'alfanum' ) myregex = /^[\w]*$/;
    if ( type == 'alfanumspace' ) myregex = /^[\w\s]*$/;
    if ( type == 'alfanumhyphen' ) myregex = /^[-\w]*$/;
    if ( type == 'alfanumunder' ) myregex = /^[_\w]*$/;
    if ( type == 'alfanumunderhyphen' ) myregex = /^[-_\w]*$/;
    if ( type == 'bool' ) myregex = /^(on|1)$/; // checkbox's value is always on, the true value is 'checked' bool property
    if ( type == 'csvfile' ) myregex = /^.+\.csv$/i;
    if ( type == 'date' ) myregex = /^\d\d\d\d-\d\d-\d\d$/;
    if ( type == 'datetime' ) myregex = /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/;
    if ( type == 'email' ) myregex = /^[\w]([.]?([.\w-]+)*)?@([\w-]+\.)+[a-zA-Z]{2,6}$/;
    if ( type == 'file' ) myregex = /^.+$/;
    if ( type == 'html' ) myregex = /^.*$/m;
    if ( type == 'htmlcolor' ) myregex = /^#[0-9A-F][0-9A-F][0-9A-F]([0-9A-F][0-9A-F][0-9A-F])?$/i;
    if ( type == 'id' ) myregex = /^\d+$/;
    if ( type == 'imgfile' ) myregex = /^.+\.(jpg|gif|png)$/i;
    if ( type == 'imgswffile' ) myregex = /^.+\.(jpg|gif|swf|png)$/i;
    if ( type == 'integer' ) myregex = /^[+-]?[\d]+$/;
    if ( type == 'jpgfile' ) myregex = /^.+\.jpe?g$/i;
    if ( type == 'ln' ) myregex = /^[a-z][a-z]$/;
    if ( type == 'mp3file' ) myregex = /^.+\.mp3$/i;
    if ( type == 'natural' ) myregex = /^[1-9][\d]*$/;
    if ( type == 'natural0' ) myregex = /^[\d]+$/;
    if ( type == 'number' ) myregex = /^[+-]?\d*[.,]?\d*$/;
    if ( type == 'password' ) myregex = /^[\S]{4,32}$/; // no spaces
    if ( type == 'percent' ) myregex = /^\d+\d?\d?([.,]\d\d?)?$/;
    if ( type == 'phone' ) myregex = /^\+?(\(?\d+\))?[\d\s-]{5,}(\s*(x|int)\s*\d+)?$/;
    if ( type == 'price' ) myregex = /^\d+[.,]?\d*$/;
    if ( type == 'swffile' ) myregex = /^.+\.swf$/i;
    if ( type == 'text' ) myregex = /^.*$/;
    if ( type == 'text_multiline' ) myregex = /^.*$/m;
    if ( type == 'textile' ) myregex = /^.*$/m;
    if ( type == 'url_http' ) myregex = /^.*$/;
    if ( type == 'videofile' ) myregex = /^.+\.(avi|mov|mpg|wmv)$/i
    if ( type == 'wavfile' ) myregex = /^.+\.wav$/i;
    var output = false;
    if ( myregex != undefined && value != '' ) {
        if ( myregex.test(value) ) {
            output = true;
        }
    }
    field.style.backgroundColor = 'white';
    if ( ! output ) bad_field(field, msg + ' "' + value + '"');
    return output;
}
function bad_field(field, msg) {
    if ( ! (field && msg) ) return;
    field.style.backgroundColor = 'red';
    alert(msg);
    field.focus();
    return;
}
function isMSIE() {
  return '\v' == 'v';
}
function money_format(number, type, decimals) { // type: ar, us; decimals: 0, 1, 2
    number = number + '';
    var output = '';
    if ( type == undefined ) type = 'ar';
    if ( decimals == undefined ) decimals = 2;
    if ( type == 'ar' ) {
        var separator = ',';
    } else if ( type == 'us' ) {
        var separator = '.';
    } else {
        var separator = ',';
    }
    var parts = new Array(2);
    if ( number.indexOf(',' ) != -1 ) {
        parts = number.split(',');
    } else if ( number.indexOf('.' ) != -1 ) {
        parts = number.split('.');
    } else {
        parts[0] = number;
        parts[1] = '00';
    }
    parts[1] = parts[1].substr(0,decimals);
    while ( parts[1].length < decimals ) {
        parts[1] += '0';
    }
    output = parts.join(separator);
    return output;
}
function strip_units(cadena) { // strips out px/pt/cm/% units
    if ( cadena == '' || cadena == undefined ) return;
    if (typeof cadena == 'number' ) {
        output = cadena;
    } else {
        var ultimos_uno = cadena.substring(cadena.length-1,cadena.length);
        var ultimos_dos = cadena.substring(cadena.length-2,cadena.length);
        var output = cadena;
        if ( ultimos_dos == 'px' || ultimos_dos == 'pt' || ultimos_dos == 'cm' ) {
            output = cadena.substring(0,cadena.length-2);
        } else if ( ultimos_uno == '%' ) {
            output = cadena.substring(0,cadena.length-1);
        }
    }
    output = output * 1;
    return output;
}
function enadisa_inputs(action) { // enable/disable input changes (in order to block form while Ajax is working)
    var new_disa = ( action == undefined || action == 'ena' ) ? false : true;
    var new_cursor = new_disa ? 'wait' : 'default';
    var imgs_list = document.getElementsByTagName('img');
    document.body.style.cursor = new_cursor;
    for( i=0; i<imgs_list.length; i++) {
        var my_img = imgs_list[i];
        if ( my_img.useMap != undefined && my_img.useMap != '' ) {
            my_img.disabled = new_disa;
        }
    }
    var inputs_list = document.getElementsByTagName('input');
    for( i=0; i<inputs_list.length;i++) {
        var my_input = inputs_list[i];
        my_input.disabled = new_disa;
    }
    var select_list = document.getElementsByTagName('select');
    for( i=0; i<select_list.length;i++) {
        var my_select = select_list[i];
        my_select.disabled = new_disa;
    }
}
function return_false () {
    return false;
}
function say_hello () {
    alert('hi/hola!');
    return;
}
function findPosition(oElement) {
    if ( typeof( oElement.offsetParent ) != 'undefined' ) {
        for ( var posX = oElement.scrollLeft, posY = oElement.scrollTop; oElement; oElement = oElement.offsetParent ) {
            posX += oElement.offsetLeft - oElement.scrollLeft;
            posY += oElement.offsetTop - oElement.scrollTop;
        }
    } else {
        posX = oElement.x;
        posY = oElement.y;
    }
    return [ posX, posY ];
}
function toggle_display(button, panel_id, deltaX, deltaY) { // necesita que el boton tenga la propiedad style:height definida
    if ( button == undefined || panel_id == undefined ) return;
    if ( deltaX == undefined || isNaN(deltaX) ) deltaX = 0;
    if ( deltaY == undefined || isNaN(deltaY) ) deltaY = 0;
    var panel = document.getElementById(panel_id);
    if ( panel.style.display == 'none' ) {
        var button_pos = findPosition(button);
        var button_height = strip_units(button.style.height);
        var panel_left = button_pos[0] + deltaX;
        panel.style.left = panel_left + 'px';
        var panel_top = button_pos[1] + button_height + deltaY;
        panel.style.top = panel_top + 'px';
        panel.style.display = 'inline';
    } else {
        panel.style.display = 'none';
    }
}
function check_thisonly(form_name, inputid_prefix, item_no) {
    if ( form_name ==  undefined || document.forms[form_name] == undefined || item_no == undefined ) return;
    var miform = document.forms[form_name];
    var pref_len = inputid_prefix.length;
    for ( i=1; i<miform.elements.length; i++ ) {
        var mielem = miform.elements[i];
        var my_id = mielem.id;
        if ( my_id.substr(0, pref_len) == inputid_prefix ) {
            if ( mielem.type == 'checkbox' ) {
                mielem.checked = ( mielem.id == inputid_prefix + item_no ) ? true : false;
            } else if ( mielem.type == 'hidden' ) {
                mielem.value = ( mielem.id == inputid_prefix + item_no ) ? 1 : 0;
            }
        }
    }
    return;
}
function check_allnone(button, id_prefix) {
    // sets all checkboxes (with the stated id prefix) checked or not according to button checked or not
    if ( button == undefined ) return;
    var miform = button.form;
    var pref_len = id_prefix.length;
    var new_checked = button.checked;
    var elems_len = miform.elements.length;
    for ( i=1; i<elems_len; i++ ) {
        var miobj = miform.elements[i];
        if ( miobj.type == 'checkbox' ) {
            if ( pref_len == 0 || id_prefix == miobj.id.substr(0,pref_len) ) {
                miobj.checked = new_checked;
            }
        }
    }
    return;
}
function imgbox_fit(img, box, minimaxi, align, tilt, rotated) {
    // align = 0100: 0/100%, 3070: 30/70%, 5050: 50/50%, 1000: 100/0%
    // tilt = degrees of image tilt; rotated: image is displayed rotated by 90 or 270 degrees (IE only)
    if ( minimaxi == undefined || minimaxi != 1 ) minimaxi = 0;
    var digits_regex = /^\d\d\d\d$/;
    if ( align == undefined || ! digits_regex.test(align) ) align = '5050';
    var align_prop;
    if ( align == '0100' ) {
        align_prop = 0;
    } else if ( align == '1000' ) {
        align_prop = 1;
    } else {
        align = align.toString();
        var in_first = align.substr(0,2);
        var in_sec = align.substr(2,2);
        if ( ! in_first + in_sec == 100 ) {
            in_first = 50;
            in_sec = 50;
        }
        align_prop = in_first / 100;
    }
    if ( tilt == null ) tilt = 0;
    var tilt_rad = tilt / 57.2957;
    if ( rotated == null ) rotated = 0;
    var img_width = strip_units(img.style.width); // img
    var img_height = strip_units(img.style.height);
    var img_prop = img_width / img_height;
    var box_width = strip_units(box.style.width); // box
    var box_height = strip_units(box.style.height);
    var box_prop = box_width / box_height;
    var true_boxh = box_height;
    var true_boxw = box_width;
    if ( rotated ) {
        var aux = box_width;
        box_width = box_height;
        box_height = aux;
    }
    var new_width = Math.round(box_width * Math.cos(tilt_rad) + box_height * Math.sin(tilt_rad));
    var new_height = Math.round(new_width / img_prop);
    if ( (minimaxi == 0 && new_height > Math.round(box_height * Math.cos(tilt_rad) + box_width * Math.sin(tilt_rad)) )
            || (minimaxi == 1 && new_height < Math.round(box_height * Math.cos(tilt_rad) + box_width * Math.sin(tilt_rad))) )
    {
        // new_height = box_height;
        new_height = Math.round(box_height * Math.cos(tilt_rad) + box_width * Math.sin(tilt_rad));
        new_width = Math.round(new_height * img_prop);
    }
    if ( rotated ) {
        var new_left = Math.round( ((true_boxw * Math.cos(tilt_rad) + true_boxh * Math.sin(tilt_rad)) - new_height - true_boxh * Math.sin(tilt_rad)) * align_prop );
        var new_top = Math.round( (true_boxh - new_width - true_boxw * Math.cos(tilt_rad) * 0) * align_prop );
    } else {
        var new_left = Math.round( ((box_width * Math.cos(tilt_rad) + box_height * Math.sin(tilt_rad)) - new_width - box_height * Math.sin(tilt_rad)) * align_prop );
        var new_top = Math.round( (box_height - new_height - box_width * Math.cos(tilt_rad) * 0) * align_prop );
    }
    img.style.width = new_width + 'px';
    img.style.height = new_height + 'px';
    img.style.left = new_left + 'px';
    img.style.top =  new_top + 'px';
}
function accents2entities(my_str) {
    if ( my_str == undefined || my_str == '' ) return '';
    my_str = my_str.replace(/á/g,"%26aacute;");
    my_str = my_str.replace(/é/g,"%26eacute;");
    my_str = my_str.replace(/í/g,"%26iacute;");
    my_str = my_str.replace(/ó/g,"%26oacute;");
    my_str = my_str.replace(/ú/g,"%26uacute;");
    my_str = my_str.replace(/Á/g,"%26Aacute;");
    my_str = my_str.replace(/É/g,"%26Eacute;");
    my_str = my_str.replace(/Í/g,"%26Iacute;");
    my_str = my_str.replace(/Ó/g,"%26Oacute;");
    my_str = my_str.replace(/Ú/g,"%26Uacute;");
    my_str = my_str.replace(/ñ/g,"%26ntilde;");
    my_str = my_str.replace(/Ñ/g,"%26Ntilde;");
    var output = my_str;
    return output;
}
function inspect(elm){
  var str = '';
  for (var i in elm){
    str += i + ": " + elm.getAttribute(i) + "\n";
  }
  alert(str);
}
function wininner_size() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return [ myWidth, myHeight ];
}
function GetXmlHttpObject(handler) {
    var objXMLHttp=null;
    if (window.XMLHttpRequest) {
        objXMLHttp=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}
function ac_selectIt(sel, text_field) { // ac (autocomplete) function
    var selobj = document.getElementById(sel);
    if ( ! selobj ) return;
    var in_text = text_field.value;
    if ( in_text ) {
        // alert('entro ' + in_text);
        var reg = new RegExp('^'+in_text,"i");
        var options_list = selobj.options;
        var options_len = options_list.length;
        var found = false;
        var i = 0;
        while ( ! found && i < options_len ) {
            if( reg.test(options_list[i].text) ) {
                found = true;
                selobj.selectedIndex = i;
            }
            i++;
        }
    } else {
        selobj.selectedIndex = 0;
    }
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function setCookie(c_name,value,expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    var cookie_content = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    document.cookie = cookie_content;
}
function highlight(target_obj, sense, blocking, high_class) {
    if ( sense == undefined ) sense = true;
    if ( blocking == undefined ) blocking = false;
    if ( high_class == undefined ) high_class = 'highlighted-elem';
    var blocked = ( target_obj.getAttribute('highlight_blocked') && target_obj.getAttribute('highlight_blocked') == 1 );
    if ( ! blocked || blocking ) {
        if ( sense ) {
            var new_blocked = 1;
            if ( target_obj.getAttribute('orig_className') == undefined ) {
                var orig_class = target_obj.className;
                target_obj.setAttribute('orig_className', orig_class);
            }
            target_obj.className = high_class;
            highlight_blocked = true;
        } else {
            var new_blocked = 0;
            var orig_class = target_obj.getAttribute('orig_className');
            if ( orig_class ) target_obj.className = orig_class;
            highlight_blocked = false;
        }
        if ( blocking ) target_obj.setAttribute('highlight_blocked', new_blocked);
    }
    return;
}
function sdidump(this_obj) {
    var msg = 'obj name: ' + this_obj.nodeName + ' class: ' + this_obj.className + ' id: ' + this_obj.id + ' children: ' + this_obj.childNodes.length;
    if ( this_obj.nodeName == 'IMG' ) {msg = msg + ' src: ' + this_obj.getAttribute('src'); }
    alert(msg);
    return;
}
document.sdi_getElementsByClassName = function(class_name) {
    var retnode = [];
    class_name = class_name.replace(/\[/, '\\[');
    class_name = class_name.replace(/]/, '\\]');
    class_name = '^' + class_name + '$';
    // var myclass = new RegExp('\\b'+class_name+'\\b'); // original
    var myclass = new RegExp(class_name);
    var elem = this.getElementsByTagName('*');
    for (var i=0; i<elem.length; i++) {
        var classes = elem[i].className;
        if (myclass.test(classes)) retnode.push(elem[i]);
    }
    return retnode;
};
function adjacentbox_show ( target_obj, src_obj ) {
    // ADVICE: it works better if target_obj has a defined height
    // displays target_obj to the right of src_obj, vertically-aligned to the middle of target_obj (taking care of browser borders)
    var margin_left = 30;
    var scroll_margin = 10;
    var scroll_spare = 15;
    if ( typeof src_obj != undefined ) {
        var src_pos = findPosition(src_obj);
        var src_width = src_obj.offsetWidth;
        if ( target_obj.style.height ) {
            var target_height = strip_units(target_obj.style.height);
            var vert_shift = - Math.round(target_height / 2);
        } else {
            var target_height = 0;
            var vert_shift = -20;
        }
        var new_left= src_pos[0] + src_width + margin_left;
        var new_top = src_pos[1] + vert_shift;
        var win_dims = wininner_size();
        var win_height = win_dims[1] - scroll_spare;
        var win_scroll_top = 0;
        if (document.documentElement && document.documentElement.scrollTop) { // IE?
            win_scroll_top = document.documentElement.scrollTop;
        } else if (window.pageYOffset) { // Safari
            win_scroll_top = window.pageYOffset;
            new_top = new_top + win_scroll_top;// Safari trick
        } else if (document.body) { // FF, IE
            win_scroll_top = document.body.scrollTop;
        }
        if ( new_top + target_height + scroll_margin - win_scroll_top > win_height ) {
            new_top = win_height - scroll_margin - target_height + win_scroll_top;
        }
        if ( new_top < win_scroll_top ) {
            new_top = win_scroll_top + scroll_margin;
        }
    } else {
        var new_left = 100;
        var new_top = 100;
    }
    target_obj.style.position = 'absolute';
    target_obj.style.left = new_left + 'px';
    target_obj.style.top = new_top + 'px';
    target_obj.style.zIndex = 1;
    target_obj.style.display = 'inline';
    if ( target_height == 0 ) {
        target_height = target_obj.offsetHeight;
        target_obj.style.height = target_height + 'px'; // target height for next times
        vert_shift = - Math.round(target_height / 2);
        new_top = src_pos[1] + vert_shift;
        if (window.pageYOffset) new_top = new_top + win_scroll_top;// Safari trick
        if ( new_top + target_height + scroll_margin - win_scroll_top > win_height ) {
            new_top = win_height - scroll_margin - target_height + win_scroll_top;
        }
        if ( new_top < win_scroll_top ) {
            new_top = win_scroll_top + scroll_margin;
        }
        target_obj.style.top = new_top + 'px';
    }
    return;
}
function adjacentbox_hide ( target_obj ) {
    target_obj.style.display = 'none';
    return;
}
function ontop() {
    // HTML classes used: ontop-velo, ontop-contenido, ontop-cerrarcaja, ontop-cerrarsigno;
    // slideshow: elements to be shown must have style="width:XXpx", no borders, padding or margin allowed
    this.veil_opacity = 65;
    this.veil_bgcolor = 'black';
    this.top_min = 0;
    this.top_margin = 30;
    this.omit_close = false;
    this.omit_slide_buttons = false;
    this.veil_padding_bottom = 10;
    this.fade_step = 15; // 0 = no fade
    this.popup_blocked = true; // click within ontop may be disabled
    this.transition = 'sliding'; // [none|sliding]
    this.margin_width = 2; // px, to take into account that table cells have a margin among them
    this.cur_slideid = null;
    this.btn_close = 'sys_files/btn-close.gif';
    this.btn_left = 'sys_files/btn-left.gif';
    this.btn_right = 'sys_files/btn-right.gif';
    this.btn_width = '32px';
    this.btn_height = '32px';
    this.img_null = 'sys_files/1x1.gif';

    this.veil = document.createElement('div');
    this.veil.style.display = 'none';
    this.veil.style.position = 'absolute';
    this.veil.style.backgroundColor = this.veil_bgcolor;
    this.veil.style.zIndex = 10;
    this.veil.style.left = '0px';
    this.veil.style.top = '0px';
    this.veil.className = 'ontop-velo';
    this.veil_element = document.body.appendChild(this.veil); // IE6: new ontop must be called after </body>
    this.content_element = null;
    this.content_tr = null;
    this.content_td = null;
    this.slides_table = null;
    this.slides_qty = 0;
    this.slides_pos = new Array();
    this.use_fade = this.fade_step != 0;
    this.prev_img = null;
    this.next_img = null;

    this.display = function ( display_content ) { // display_content: object or relative url
        this.veil.style.opacity = this.use_fade ? 0 : this.veil_opacity / 100;
        this.veil.style.filter = this.use_fade ? 'alpha(opacity=0)' : 'alpha(opacity=' + this.veil_opacity + ')';
        var content_box = document.createElement('div');
        content_box.style.opacity = this.use_fade ? 0 : 1;
        content_box.style.filter = this.use_fade ? 'alpha(opacity=0)' : 'alpha(opacity=100)';
        content_box.style.position = 'absolute';
        if ( typeof window.pageYOffset == 'undefined' ) { // FF, Safari
            var scroll_top = document.documentElement.scrollTop;
        } else { // IE6
            var scroll_top = window.pageYOffset;
        }
        if ( this.top_min > scroll_top ) {
            var top_value = this.top_min;
        } else {
            var top_value = scroll_top + this.top_margin;
        }
        content_box.style.top = top_value + 'px';
        // kk content_box.style.left = '0px';
        content_box.style.width = '100%';
        content_box.style.textAlign = 'center';
        content_box.align = 'center';
        content_box.style.zIndex = 30;
        var content_table = document.createElement('table');
        content_table.align = 'center';
        content_table.style.margin = 'auto';
        content_table.className = 'ontop-contenido';
        var content_tbody = document.createElement('tbody');
        if ( ! this.omit_close ) {
            var close_tr = document.createElement('tr');
            var close_td = document.createElement('td');
            close_td.align = 'right';
            if ( this.slideshow != 'none' ) close_td.colSpan = 3;
            close_td.className = 'ontop-cerrarcaja';
            var close_sign = document.createElement('span');
            close_sign.className = 'ontop-cerrarsigno';
            close_sign.style.cursor = 'pointer';
            var close_img = document.createElement('img');
            close_img.setAttribute('src', this.btn_close);
            close_img.alt = 'X';
            close_img.title = close_img.alt;
            close_sign.appendChild(close_img);
            close_sign.onclick = ontop_close;
            close_td.appendChild(close_sign);
            close_tr.appendChild(close_td);
            content_tbody.appendChild(close_tr);
        }
        var content_tr = document.createElement('tr');
        var content_td = document.createElement('td');
        if ( typeof display_content == 'object' ) {
            if ( this.slideshow != 'none' ) {
                var curr_id = null;
                var class_name = display_content.className;
                if ( class_name ) {
                    var class_list = document.sdi_getElementsByClassName(class_name);
                    this.slides_qty = class_list.length;
                }
            }
            if ( this.slideshow != 'none' && this.slides_qty > 1 ) {
                var content = document.createElement('div');
                content.style.overflow = 'hidden';
                content.style.position = 'relative';
                this.slides_table = document.createElement('table');
                var slides_tbody = document.createElement('tbody');
                var slides_tr = document.createElement('tr');
                var prevs_width = this.margin_width;
                var max_width = 0;
                this.cur_slideid = null;
                for ( var i=0; i<this.slides_qty; i++ ) {
                    if ( class_list[i] == display_content ) this.cur_slideid = i;
                    this.slides_pos[i] = prevs_width;
                    var slide_width = strip_units(class_list[i].style.width);
                    prevs_width += slide_width + this.margin_width;
                    if ( max_width < slide_width ) max_width = slide_width;
                    var slide_td = document.createElement('td');
                    var slide_content = class_list[i].cloneNode(true);
                    if ( slide_content.style.height ) {
                        slide_td.style.height = slide_content.style.height;
                        slide_content.style.height = null; // should not interfere with vertical align
                    }
                    if ( slide_content.style.backgroundColor ) slide_td.style.backgroundColor = slide_content.style.backgroundColor;
                    slide_content.style.display = 'block';
                    // kk why? slide_content.style.textAlign = 'center';
                    slide_td.style.padding = '0px';
                    slide_td.appendChild(slide_content);
                    slides_tr.appendChild(slide_td);
                }
                if ( max_width ) content.style.width = max_width + 'px';
                slides_tbody.appendChild(slides_tr);
                this.slides_table.appendChild(slides_tbody);
                this.slides_table.style.position = 'relative';
                var new_left = '-' + this.slides_pos[this.cur_slideid] + 'px';
                this.slides_table.style.left = new_left;
                content.appendChild(this.slides_table);
            } else {
                var content = display_content.cloneNode(true);
                content.style.display = 'block';
                content.style.margin = 'auto';
            }
        } else if ( typeof display_content == 'string' ) {
            if ( display_content.substring(0,7).toLowerCase() == 'http://' ) {
                var url = display_content;
            } else if ( display_content.substring(0,1) == '/' ) {
                var url = 'http://' + hname + display_content;
            } else if ( display_content.substring(0,1) == '?' ) {
                var url = 'http://' + target_url + display_content
            } else {
                var url = base_dir + display_content;
            }
            url += url.indexOf('?') >= 0 ? '&' : '?';
            url += "x=" + (Math.random() * 1000).toFixed(0);
            xmlHttp=GetXmlHttpObject();
            if (xmlHttp == null) alert (signs['browser_no_httpreq']);
            // window.open(url); // debug, just in case
            try {
                xmlHttp.open('GET', url, false);
                xmlHttp.send(null);
                if ( xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete' ) {
                    var content = document.createElement('div');
                    content.style.display = 'block';
                    content.style.margin = 'auto';
                    var response_text = xmlHttp.responseText;
                    content.innerHTML = response_text;
                }
            }
            catch (err) {
                alert('error "' + url + '": ' + err.message);
            }
        }
        if ( typeof content != 'undefined' ) { // success
            this.veil.style.display = 'inline';
            this.content_td = content_td.appendChild(content);
            this.content_tr = content_tr.appendChild(content_td);
            if ( this.slideshow != 'none' && this.slides_qty > 1 ) {
                var prev_td = document.createElement('td');
                var prev_img = document.createElement('img');
                prev_img.alt = ' <- ';
                prev_img.title = prev_img.alt;
                if ( this.cur_slideid == 0 ) {
                    var new_src = this.img_null;
                    var new_height = '1px';
                } else {
                    var new_src = this.btn_left;
                    var new_height = this.btn_height;
                }
                prev_img.setAttribute('src', new_src);
                prev_img.style.width = this.btn_width;
                prev_img.style.height = new_height;
                prev_img.style.cursor = 'pointer';
                prev_img.onclick = ontop_prevslide;
                this.prev_img = prev_td.appendChild(prev_img);
                // kk is it done in next line? content_tr.appendChild(prev_td);
                if ( ! this.omit_slide_buttons ) content_tr.insertBefore(prev_td, content_tr.firstChild);
                var next_td = document.createElement('td');
                var next_img = document.createElement('img');
                next_img.alt = ' -> ';
                next_img.title = next_img.alt;
                if ( this.cur_slideid == this.slides_qty - 1 ) {
                    var new_src = this.img_null;
                    var new_height = '1px';
                } else {
                    var new_src = this.btn_right;
                    var new_height = this.btn_height;
                }
                next_img.setAttribute('src', new_src);
                next_img.style.width = this.btn_width;
                next_img.style.height = new_height;
                next_img.style.cursor = 'pointer';
                next_img.onclick = ontop_nextslide;
                this.next_img = next_td.appendChild(next_img);
                if ( ! this.omit_slide_buttons ) content_tr.appendChild(next_td);
            }
            if ( this.popup_blocked ) {
                content_tr.onclick = return_false;
            } else {
                content_tr.onclick = null;
            }
            content_tbody.appendChild(content_tr);
            content_table.appendChild(content_tbody);
            content_box.appendChild(content_table);
            this.content_element = document.body.appendChild(content_box);
            var content_height = this.content_element.offsetHeight;
            this.veil_width = document.documentElement.clientWidth;
            this.veil_height = document.documentElement.offsetHeight;
            if ( this.veil_height < window.innerHeight ) this.veil_height = window.innerHeight;
            this.veil.style.width = this.veil_width + 'px';
            var new_veil_height = this.veil_height;
            // popup may be longer than original body
            if ( content_height + this.top_margin + scroll_top + this.veil_padding_bottom > this.veil_height ) {
                new_veil_height = content_height + this.top_margin + scroll_top + this.veil_padding_bottom;
            }
            this.veil.style.height = new_veil_height + 'px';
            if ( this.use_fade ) ontop_fade(this.fade_step);
            if ( typeof response_text != 'undefined' ) {
                var scripts_list = content.getElementsByTagName("script");
                for (var i=0; i<scripts_list.length; i++) {
                    eval(scripts_list[i].text);
                }
            }
        } else { // failure
            this.veil.parentNode.removeChild(this.veil);
        }
        return true;
    }
    this.close = function () {
        if ( this.use_fade ) { // fade() takes care of erasing content and hiding veil-close() cannot find out when fade() is done
            this.fade(-this.fade_step);
        } else {
            this.content_element.parentNode.removeChild(this.content_element);
            this.content_element = null;
            this.veil_element.style.display = 'none';
            this.veil_element.style.height = this.veil_height + 'px';
        }
        this.slides_qty = 0;
        return true;
    }
    this.fade = function (fade_step) {
        var veil_opac = this.veil_element.style.opacity;
        veil_opac = Math.round(veil_opac * 100 );
        if ( fade_step > 0 && veil_opac < this.veil_opacity || fade_step < 0 && veil_opac > 0 ) {
            var new_veil_opac = Math.round(veil_opac + fade_step);
            if ( fade_step > 0 && new_veil_opac + fade_step > this.veil_opacity ) { // overshooting
                new_veil_opac = this.veil_opacity;
                new_content_opac = 100;
            } else if ( fade_step < 0 && new_veil_opac + fade_step < 0 ) { // overshooting
                new_veil_opac = 0;
                new_content_opac = 0;
            }
            var new_veil_value = new_veil_opac / 100;
            this.veil_element.style.opacity = new_veil_value;
            this.veil_element.style.filter = 'alpha(opacity=' + new_veil_opac + ')';
            var content_step = Math.round(100 / (this.veil_opacity / fade_step));
            if ( typeof new_content_opac == 'undefined' ) {
                var content_opac = this.content_element.style.opacity;
                content_opac = Math.round(content_opac * 100 );
                var new_content_opac = Math.round(content_opac + content_step);
            }
            var new_content_value = new_content_opac / 100;
            this.content_element.style.opacity = new_content_value;
            this.content_element.style.filter = 'alpha(opacity=' + new_content_opac + ')';
            var fc_str = 'ontop_fade(' + fade_step + ')';
            if ( new_veil_opac == 0 ) {
                this.content_element.parentNode.removeChild(this.content_element);
                this.content_element = null;
                this.veil_element.style.display = 'none';
                this.veil_element.style.height = this.veil_height + 'px';
            } else {
                setTimeout(fc_str, 1);
            }
        }
        return true;
    }
    this.slide_change = function (new_id) {
        if ( parseInt(new_id) < 0 ) {
            new_id = 0;
        } else if ( this.slides_qty < parseInt(new_id) ) {
            new_id = this.slides_qty;
        }
        if ( new_id == 0 ) {
            this.prev_img.setAttribute('src', this.img_null);
            this.prev_img.style.height = '1px';
        } else if ( new_id == 1 ) {
            this.prev_img.setAttribute('src', this.btn_left);
            this.prev_img.style.height = this.btn_height;
        }
        if ( new_id == this.slides_qty - 2 ) {
            this.next_img.setAttribute('src', this.btn_right);
            this.next_img.style.height = this.btn_height;
        } else if ( new_id == this.slides_qty - 1 ) {
            this.next_img.setAttribute('src', this.img_null);
            this.next_img.style.height = '1px';
        }
        var target_pos = - parseInt(this.slides_pos[new_id]);
        var moving_obj = this.slides_table;
        var dimm = 'horizontal';
        if ( dimm != 'vertical' ) dimm = 'horizontal';
        var cur_pos = dimm == 'horizontal' ? strip_units(moving_obj.style.left) : strip_units(moving_obj.style.top);
        var delta = (target_pos - cur_pos) / 4;
        delta = delta.toFixed(0);
        var new_pos = (delta > -1 && delta < 1) ? target_pos : cur_pos * 1 + delta * 1;
        if ( dimm == 'horizontal' ) {
            moving_obj.style.left = new_pos + 'px';
        } else {
            moving_obj.style.top = new_pos + 'px';
        }
        if ( target_pos == new_pos ) {
            this.cur_slideid = new_id;
        } else {
            var fc_str = 'ontop_slidechange(' + new_id + ')';
            setTimeout(fc_str, 1);
        }
        return true;
    }
    return true;
}
function ontop_display ( elem, top_margin, omit_close, popup_blocked, use_fade ) {
    if ( typeof ontop_screen == 'undefined' ) {
        ontop_screen = new ontop();
    }
    if ( typeof top_margin != 'undefined' && top_margin != null ) ontop_screen.top_margin = top_margin;
    if ( typeof omit_close != 'undefined' && omit_close != null ) ontop_screen.omit_close = omit_close;
    if ( typeof popup_blocked != 'undefined' && popup_blocked != null ) ontop_screen.popup_blocked = use_fade;
    if ( typeof use_fade != 'undefined' && use_fade != null ) ontop_screen.use_fade = use_fade;
    var output = ontop_screen.display(elem);
    return output;
}
function ontop_close (e) {
    var output = false;
    if ( typeof ontop_screen != 'undefined' ) {
        output = ontop_screen.close();
    }
    return output;
}
function ontop_fade (fade_step) {
    var output = false;
    if ( typeof ontop_screen != 'undefined' ) {
        output = ontop_screen.fade(fade_step);
    }
    return output;
}
function ontop_slidechange (new_id) {
    var output = false;
    if ( typeof ontop_screen != 'undefined' ) {
        output = ontop_screen.slide_change(new_id);
    }
    return output;
}
function ontop_prevslide () {
    var output = false;
    if ( typeof ontop_screen != 'undefined' ) {
        ontop_screen.slide_change(ontop_screen.cur_slideid - 1);
    }
}
function ontop_nextslide () {
    var output = false;
    if ( typeof ontop_screen != 'undefined' ) {
        var new_id = ontop_screen.cur_slideid + 1;
        ontop_screen.slide_change(ontop_screen.cur_slideid + 1);
    }
}
function box_rotate (container) { // only one per page (_self is global, trick for setTimeout)
    this.boxes_list = new Array();
    this.transition_delay = 4000;
    this.transition_effect = 'fade'; // 'fade', 'turn'
    this.fade_step = 0.05;
    this.fade_delay = 1;

    this.index_in = null;
    this.index_out = null;
    if ( this.fade_delay * (1/this.fade_step) > this.transition_delay / 2 ) this.fade_delay = this.transition_delay / ( 2 * (1/this.fade_step) );

    var childs_list = container.childNodes;
    for ( var i=0, child_item; child_item=childs_list[i]; i++ ) {
        if ( child_item.nodeName != '#text' && child_item.nodeName != '#comment' ) {
            this.boxes_list.push(child_item);
        }
    }
    _self = this;

    this.rotate = function () {
        var out_index = null;
        for ( var i=0, box; box=_self.boxes_list[i]; i++ ) {
            if ( _self.transition_effect == 'fade' || _self.transition_effect == 'turn' ) {
                if ( ! out_index && (box.style.opacity == 1 || box.style.filter == 'alpha(opacity=100)') ) out_index = i;
            }
        }
        if ( out_index == null ) {
            _self.boxes_list[0].style.opacity = 1;
            _self.boxes_list[0].style.filter = 'alpha(opacity=100)';
        } else {
            var in_index = ( out_index == _self.boxes_list.length - 1 ) ? 0 : out_index + 1;
            if ( _self.transition_effect == 'fade' ) {
                this.index_out = out_index;
                this.index_in = in_index;
                _self.switch_fade();
            } else if ( _self.transition_effect == 'turn' ) {
                _self.boxes_list[in_index].style.opacity = 1;
                _self.boxes_list[in_index].style.filter = 'alpha(opacity=100)';
                _self.boxes_list[out_index].style.opacity = 0;
                _self.boxes_list[out_index].style.filter = 'alpha(opacity=0)';
            }
        }
        setTimeout('_self.rotate()', _self.transition_delay);
        return;
    }
    this.switch_fade = function() {
        var step = _self.fade_step;

        var box_out = _self.boxes_list[_self.index_out];
        var opacout_ini = ( box_out.style.opacity ) * 1;
        var opacout_new = opacout_ini - step;
        if ( opacout_new < 0 ) opacout_new = 0;
        var opacout_ie_new = parseInt(opacout_new * 100);

        var box_in = _self.boxes_list[_self.index_in];
        var opacin_ini = ( box_in.style.opacity ) ? box_in.style.opacity * 1 : 0; // IE ignores CSS opacity
        var opacin_new = opacin_ini + step;
        if ( opacin_new > 1 ) opacin_new = 1;
        var opacin_ie_new = parseInt(opacin_new * 100);

        _self.boxes_list[_self.index_out].style.opacity = opacout_new;
        _self.boxes_list[_self.index_out].style.filter = 'alpha(opacity=' + opacout_ie_new + ')';
        _self.boxes_list[_self.index_in].style.opacity = opacin_new;
        _self.boxes_list[_self.index_in].style.filter = 'alpha(opacity=' + opacin_ie_new + ')';

        if ( opacout_new > 0 || opacin_new < 1 ) {
            setTimeout('_self.switch_fade()', _self.fade_delay);
        }
        return;
    }
    this.rotate();
    return;
}

