window.onload = init;

ddaccordion.init({
    headerclass: "submenuheader", //Shared CSS class name of headers group
    contentclass: "submenu", //Shared CSS class name of contents group
    collapseprev: true, //Collapse previous content (so only one open at any time)? true/false
    defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
    animatedefault: false, //Should contents open by default be animated into view?
    persiststate: true, //persist state of opened contents within browser session?
    toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
    togglehtml: ["suffix", "<img src='img/plus.gif' class='statusicon' alt='expandir' />", "<img src='img/minus.gif' class='statusicon' alt='contraer' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
    animatespeed: "normal" //speed of animation: "fast", "normal", or "slow"
})

function init() {
    hide_loading();
    init_familias();

    if (document.getElementById('pedidos')) {
        var iobjs = document.getElementById('pedidos').getElementsByTagName('input');
        if (iobjs) {
            var tam = iobjs.length;
            for (i = 0; i < tam; i++) {
                if (iobjs[i].name.substring(0,4) == 'cant') {
                    iobjs[i].onkeyup = update_cantidades;
                }
            }
        }
    }
    if (document.getElementById('lnk_buscar')) {
        var lnk = document.getElementById('lnk_buscar');
        lnk.onclick = submit_search_form;
    }
    if (document.getElementById('frm_buscar')) {
        var frm = document.getElementById('frm_buscar');
        frm.onsubmit = process_search_form;
    }
    if (document.getElementById('frm_reg_puntos')) {
        var y = document.getElementById('y');
        if (y) {
            y.onchange = update_fecha;
        }
        var m = document.getElementById('m');
        if (m) {
            m.onchange = update_fecha;
        }
    }
}

function update_fecha() {
    var y = document.getElementById('y');
    if (!y) {
        return false;
    }
    var year  = y.options[y.selectedIndex].value;
    var month = '';
    var m = document.getElementById('m');
    if (m) {
        month = m.options[m.selectedIndex].value;

        location = '/reg_puntos.php?y='+year+'&m='+month;
        return;
    }
    location = '/reg_puntos.php?y='+year;
}

function init_familias() {
    var obj = document.getElementById('idfam');
    if (!obj) {
        return false;
    }
    obj.onchange = function() {
        location = '/productos.php?idfam='+this.options[this.selectedIndex].value;
    }
}

function update_cantidades() {
    if (!this.id) {
        return false;
    }
    var tmp = this.id.split('_');
    var id = tmp[1];

    var oprod = document.getElementById('idprod_'+id);
    if (!oprod || !oprod.value) {
        return false;
    }

    var ocant = document.getElementById('cant_'+id);
    if (!ocant || !ocant.value) {
        alert('Debe especificar una cantidad');
        ocant.focus();
        return false;
    }
    var idprod = oprod.value;
    var cant = ocant.value;

    getPrecio('&idprod='+idprod+'&cant='+cant);
}

function process_search_form() {
    var frm = document.getElementById('frm_buscar');
    if (!frm) {
        return false;
    }
    if (!frm.texto.value) {
        alert('Debe escribir el producto a buscar');
        frm.texto.focus();
        return false;
    }
    return true;
}

function submit_search_form() {
    var frm = document.getElementById('frm_buscar');
    if (!frm) {
        return false;
    }
    if (!frm.texto.value) {
        alert('Debe escribir el producto a buscar');
        frm.texto.focus();
        return false;
    }
    frm.submit();
}

function editar(cate, id) {
    if (!id) {
        alert('ERROR (editar): Invalid identifier');
        return false;
    }
    switch (cate) {
        case 'usu':
            location = 'cliente.php?id='+id;
            break;
        default:
            alert('ERROR (editar): Cannot edit');
            return false;
    }
    return true;
}

function borrar(cate, id) {
    if (!id) {
        alert('ERROR (borrar): Invalid Identifier');
        return false;
    }
    var url = '';
    var msg = '';
    switch (cate) {
        case 'usu':
            url = '/cliente_acciones.php?borrar=1&id='+id;
            break;
        default:
            alert('ERROR (borrar): No se puede borrar');
            return false;
    }
    if (confirm('Seguro que desea borrarlo?'+msg)) {
        location = url;
    }
}

function verifica(form) {

    if (!confirm('Quieres continuar con el proceso de borrado?')) {
        return false;
    }
    return true;
}