/* 
 * Mensajes de error
 */

var months = new Array( 'Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre' );
var drawNotificon;

function showError( message ) {
    if (!$('#mderror').length) {
        container = document.createElement('div');
        container.id = 'mderror';
        container.className = 'modal';
        elem = document.createElement('h3');
        elem.innerHTML = 'Error';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mderrormsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        button = document.createElement('input');
        button.type = 'button';
        button.className = 'close btns';
        button.value = 'Aceptar';
        elem.appendChild(button);
        container.appendChild(elem);
        document.body.appendChild(container);
    }
    $('#mderror #mderrormsg').html(message);
    $('#mderror').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

function showWrkError( message ) {
    if (!$('#mdwrkerror').length) {
        container = document.createElement('div');
        container.id = 'mdwrkerror';
        container.className = 'modal';
        elem = document.createElement('h3');
        elem.innerHTML = 'Error';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mderrormsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mderrorbtns';
        button = document.createElement('input');
        button.id = 'mderrorbtnone';
        button.type = 'button';
        button.className = 'close';
        button.value = 'No hacer nada';
        elem.appendChild(button);
        if (isdebug > '') {
            button = document.createElement('input');
            button.id = 'mderrorbtignore';
            button.type = 'button';
            button.className = 'close';
            button.value = 'Procesar ignorando los errores';
            elem.appendChild(button);
        }
        button = document.createElement('input');
        button.id = 'mderrorbtnormal';
        button.type = 'button';
        button.className = 'close';
        button.value = 'Procesar las entradas correctas';
        elem.appendChild(button);
        container.appendChild(elem);
        document.body.appendChild(container);
        $('#mderrorbtnone').bind('click',function() {
            $('#passvalidations').val('-1');
            document.forms['f'].submit();
        });
        if (isdebug > '') {
            $('#mderrorbtignore').bind('click',function() {
                $('#passvalidations').val('1');
                document.forms['f'].submit();
            });
        }
        $('#mderrorbtnormal').bind('click',function() {
            $('#passvalidations').val('0');
            document.forms['f'].submit();
        });
    }
    $('#mdwrkerror #mderrormsg').html(message);
    $('#mdwrkerror').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

function showFormWarning( message, form, functionok, functioncancel ) {
    if (!$('#mdwarning').length) {
        container = document.createElement('div');
        container.id = 'mdwarning';
        container.className = 'modal';
        elem = document.createElement('h3');
        elem.innerHTML = 'Aviso';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mdwarningmsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mdwarningbtns';
        button = document.createElement('input');
        button.id = 'mdwarningbtcontinue';
        button.type = 'button';
        button.className = 'close';
        button.value = 'Continuar';
        elem.appendChild(button);
        button = document.createElement('input');
        button.id = 'mdwarningbtcancel';
        button.type = 'button';
        button.className = 'close';
        button.value = 'Cancelar';
        elem.appendChild(button);
        container.appendChild(elem);
        document.body.appendChild(container);
        $('#mdwarningbtcontinue').bind('click',functionok);
        $('#mdwarningbtcancel').bind('click',functioncancel);
    }
    $('#mdwarning #mdwarningmsg').html(message);
    omdwarning = $('#mdwarning').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

function showFormMessage( message, form, functionok, functioncancel ) {
    if (!$('#mdmessage').length) {
        container = document.createElement('div');
        container.id = 'mdmessage';
        container.className = 'modal';
        elem = document.createElement('h3');
        elem.innerHTML = 'Mensaje';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mdmessagemsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mdmessagebtns';
        button = document.createElement('input');
        button.id = 'mdmessagebtcontinue';
        button.type = 'button';
        button.className = 'btns close';
        button.value = 'Continuar';
        elem.appendChild(button);
        button2 = document.createElement('input');
        button2.id = 'mdmessagebtcancel';
        button2.type = 'button';
        button2.className = 'btns close';
        button2.value = 'Cancelar';
        elem.appendChild(button2);
        container.appendChild(elem);
        document.body.appendChild(container);
        $('#mdmessagebtcontinue').bind('click',functionok);
        $('#mdmessagebtcancel').bind('click',functioncancel);
    }
    $('#mdmessage #mdmessagemsg').html(message);
    omdmessage = $('#mdmessage').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

function showDialogFromDOM( element ) {
    
}

function showAjaxIndicator( message, show_btns ) {

    if (!$('#mdajaxind').length) {
        container = document.createElement('div');
        container.id = 'mdajaxind';
        container.className = 'modal';
        elem = document.createElement('h3');
        elem.innerHTML = '';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mdajaxindmsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.style.textAlign = 'center';
        img = document.createElement('img');
        img.src='/images/indicator-big.gif';
        elem.appendChild(img);
        container.appendChild(elem);
        if (show_btns == true) {
            elem = document.createElement('p');
            elem.className = 'alcenter';
            button = document.createElement('input');
            button.type = 'button';
            button.className = 'btns close';
            button.value = 'Cerrar';
            elem.appendChild(button);
            container.appendChild(elem);
        }
        document.body.appendChild(container);
    }
    $('#mdajaxind #mdajaxindmsg').html(message);
    mdajaxind = $('#mdajaxind').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

/**
 * Mensajes no intrusivos
 */

var msgs_timer;
var msgs_showing = false;

function close_msgs() {
    if (msgs_showing) {
        clearTimeout(msgs_timer);
        $('#cmsgs').fadeOut(300);
        msgs_showing = false;
    }
}

function show_msgs( message, autoclose ) {
    if (msgs_showing) {
        close_msgs();
    }
    $('#cmsgs #cmsg_content').html(message);
    $('#cmsgs').fadeIn(300);
    msgs_showing = true;
    if (autoclose != false) {
        msgs_timer = setTimeout(close_msgs, 5000);
    }
}

function show_dialog( message ) {
    if (!$('#mddialog').length) {
        container = document.createElement('div');
        container.id = 'mddialog';
        container.className = 'modal';
        elem = document.createElement('h3');
        elem.innerHTML = '';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mddialogmsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        button = document.createElement('input');
        button.type = 'button';
        button.className = 'btns close';
        button.value = 'Aceptar';
        elem.appendChild(button);
        container.appendChild(elem);
        document.body.appendChild(container);
    }
    $('#mddialog #mddialogmsg').html(message);
    mddialog = $('#mddialog').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

function show_dialog_action( message, action ) {
    if (!$('#mddialog').length) {
        container = document.createElement('div');
        container.id = 'mddialog';
        container.className = 'modal';
        elem = document.createElement('h3');
        elem.innerHTML = '';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mddialogmsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        button = document.createElement('input');
        button.type = 'button';
        button.id = 'mddlgact_btok';
        button.className = 'btns close';
        button.value = 'Aceptar';
        elem.appendChild(button);
        container.appendChild(elem);
        document.body.appendChild(container);
        $("#mddlgact_btok").bind('click',action);
    }
    $('#mddialog #mddialogmsg').html(message);
    mddialog = $('#mddialog').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

function show_dialog_graph( title, message, graph, year, graphtype ) {
    if (!$('#mddialoggr').length) {
        container = document.createElement('div');
        container.id = 'mddialoggr';
        container.className = 'modal';
        elem = document.createElement('h2');
        elem.id = 'mddialoggrtit';
        elem.innerHTML = title;
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mddialogmsg';
        container.appendChild(elem);
        elem = document.createElement('p');
        elem.id = 'mddialoggraph';
        container.appendChild(elem);
        elem = document.createElement('p');
        lbl = document.createElement('label');
        lbl.innerHTML = 'Año: ';
        lbl.htmlFor = 'mddialoggraphyear';
        elem.appendChild(lbl);
        select = document.createElement('select');
        select.id = 'mddialoggraphyear';
        d = new Date();
        firstyear = d.getFullYear() - 10;
        for (ct=0;ct<11;ct++) {
            opt = new Option(firstyear + ct);
            if (firstyear + ct == year) {
                opt.selected = true;
            }
            select.appendChild(opt);
        }
        elem.appendChild(select);
        lbl = document.createElement('label');
        lbl.innerHTML = ' Tipo de gráfica: ';
        lbl.htmlFor = 'mddialoggraphtype';
        elem.appendChild(lbl);
        select = document.createElement('select');
        select.id = 'mddialoggraphtype';
        opt = new Option('Barras','bar');
        if (graphtype == 'bar') {
            opt.selected = true;
        }
        select.appendChild(opt);
        opt = new Option('Barras apiladas','stackedbar');
        if (graphtype == 'stackedbar') {
            opt.selected = true;
        }
        select.appendChild(opt);
        opt = new Option('Líneas','lines');
        if (graphtype == 'lines') {
            opt.selected = true;
        }
        select.appendChild(opt);
        elem.appendChild(select);
        button = document.createElement('input');
        button.type = 'button';
        button.className = 'btns close';
        button.value = 'Cerrar';
        elem.appendChild(button);
        container.appendChild(elem);
        document.body.appendChild(container);
    }
    $('#mddialoggr #mddialoggrtit').html(title);
    $('#mddialoggr #mddialogmsg').html(message);
    $('#mddialoggr #mddialoggraph').html(graph);
    $('#mddialoggr').overlay({
        expose: {

            // you might also consider a "transparent" color for the mask
            color: '#fff',

            // load mask a little faster
            loadSpeed: 200,

            // highly transparent
            opacity: 0.8
        },

        // disable this for modal dialog-type of overlays
        closeOnClick: false,

        // we want to use the programming API
        api: true
    }).load();
}

/**
 * Menú de la aplicación
 */

function mnuEnter() {
    $(this).addClass( 'hovering' );
}

function mnuLeave() {
    $(this).removeClass( 'hovering' );
}

function makeClearable( target ) {

    $(target).each( function( elem ) {
        left = $(this).width() - 18;
        top = '-23';
        $(this).parent().html($(this).parent().html()+"<img id='btd"+$(this).attr('id')+"' class='clearfieldimg' src='/images/btnsdltd.gif' onclick='$(this).prev().val(\"\");$(this).fadeOut(\"slow\");' style='display: none; position: relative; left: " + (left + 6) + 'px' + "; top: " + top + "px; cursor: pointer;' />");
    });
    $(target).keyup( function() {
        if ($(this).val() == '') {
            $("#btd"+$(this).attr('id')).fadeOut('slow');
        } else {
            $("#btd"+$(this).attr('id')).fadeIn('slow');
        }
    })

}

$(document).ready(function () {
    var mnuConfig = {
        interval: 200,
        sensivity: 4,
        timeout: 350,
        over: mnuEnter,
        out: mnuLeave
    }
    if ($('#full_menu').length) {
        $('#full_menu li.mnu').hoverIntent(mnuConfig);
        $('#full_menu li.mnu li.submenu').hoverIntent(mnuConfig);
    }
    if ($('form#fc').length) {
        $('#admcea, .srchcea').autocomplete(
            '/code/getac_ceas.ajax.php',
            {
                minChars: 0,
                max: 100,
                autoFill: true
            }
        );
    }
    
    setNotificon();
    
})

function setNotificon() {
    
    /**
     * Notificon
     */
    if (typeof drawNotificon !== 'undefined' && drawNotificon == '1') {
        if (typeof Notificon == 'undefined') {
            fileref=document.createElement('script');
            fileref.setAttribute("type","text/javascript");
            fileref.setAttribute("id","jsnotificon");
            fileref.setAttribute("src", '/javascript/notificon.min.js');
            document.getElementsByTagName("head")[0].appendChild(fileref);
            $("#jsnotificon").ready(function () {
                setTimeout( 'Notificon( "' + drawNotificonText + '" )', 1500 );
            })
        } else {
            Notificon( drawNotificonText, {color: '#000000', stroke: 'rgba(255,255,255,0.85)' } );
        }
    }
    
}

/**
 * Guiders
 */
function startGuiders( context ) {
    
    if (typeof guider == 'undefined' || !guider) {
        fileref=document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", '/javascript/jquery/plugins/Guiders-JS/guider.css');
        document.getElementsByTagName("head")[0].appendChild(fileref);
        fileref=document.createElement('script');
        fileref.setAttribute("type","text/javascript");
        fileref.setAttribute("src", '/javascript/jquery/plugins/Guiders-JS/guider.js');
        document.getElementsByTagName("head")[0].appendChild(fileref);
        setTimeout('startGuiders("' + context + '")', 1000);
    }

    if (context.substr(0,6) == 'tables') {
        
        guider.createGuider({
          buttons: [{name: "Siguiente", onclick: guider.next}],
          description: "En esta página disponemos de una tabla avanzada, que permite ordenar los datos en modo ascendente o descendente pulsando sobre las cabeceras de las columnas.",
          id: "first",
          next: "second",
          overlay: true,
          title: "Ayuda de la tabla de Bajas"
        }).show();

        guider.createGuider({
          attachTo: "#srchganadero",
          buttons: [{name: "Siguiente", onclick: guider.next}],
          description: 'Cada columna de la tabla dispone de un campo al final que permitirá filtrar los datos según los caracteres que introduzcamos.',
          id: "second",
          next: "third",
          position: 12,
          title: "Filtros de búsqueda",
          width: 300
        });

        guider.createGuider({
          attachTo: "#guider_3",
          buttons: [{name: "Siguiente", onclick: guider.next}],
          description: 'Podemos ordenar la tabla pulsando sobre las cabeceras de las columnas.',
          id: "third",
          next: "fourth",
          position: 12,
          title: "Orden de los campos",
          width: 300
        });

        if (context.search('_extra')>0) {
            
            guider.createGuider({
              attachTo: "#guider_4",
              buttons: [{name: "Cerrar", onclick: guider.hideAll}],
              description: 'Podemos obtener información extra pulsando sobre los botones "+" y "-".',
              id: "fourth",
              next: "fifth",
              position: 3,
              title: "Informaci&oacute;n extra",
              width: 300
            });
            
        }
    
    }

}


