// Devuelve el "value" de un "input" en funcion de su tipo
// NOTA:    faltan tipos por definir
function obtenerValor(formulario, campo) {
    var s
    if (input = document.forms[formulario].elements[campo]) {
        if (input.type == undefined)
            tipo = document.forms[formulario].elements[campo][0].type
        else
            tipo = input.type
        switch (tipo) {
            case "text":
            case "password":
            case "textarea":
            case "hidden":            return input.value
                                        break
            case "checkbox":        if (input.checked)
                                            return input.value
                                        break
            case "radio":            if (input.length) {
                                            for (s = 0; s < input.length; s++) {
                                                if (input[s].checked) {
                                                    return input[s].value
                                                }
                                            }
                                        } else {
                                            return input.value
                                        }
                                        break
            case "select-one":    return input.options[input.selectedIndex].value
                                        break

            case "file":         return input.value
                                        break

            default:             return false
        }
    } else {
        return false
    }
}

// Establece (o selecciona) el valor de un "input" en funcion de su tipo
// NOTA:    faltan tipos por definir
function cambiarValor(formul, elem, valor) {
    var i
    if (input = document.forms[formul].elements[elem]) {
        if (input.type == undefined)
            tipo = document.forms[formulario].elements[campo][0].type
        else
            tipo = input.type
        switch (tipo) {
            case "text":
            case "hidden":
            case "textarea":    input.value = valor
                                break
            case "checkbox":    input.checked = (input.value == valor)
                                break
            case "radio":
            case "select-one":    for (i = 0; i < input.length; i++) {
                                    if (input.options[i].value == valor) {
                                        input.selectedIndex = i
                                    }
                                }
                                break
            default:            return false
        }
    } else {
        return false
    }
}

// Asigna el foco a un "input" teniendo en cuenta su tipo
// NOTA:    faltan casos por definir
function foco(formulario, campo) {
    if (input = document.forms[formulario].elements[campo]) {
        if (input.type == undefined)
            tipo = document.forms[formulario].elements[campo][0].type
        else
            tipo = input.type
        switch (tipo) {
            case "radio":   if (input[0] && input[0].focus)
                                input[0].focus()
                                break
            default:        if (input.focus)
                                input.focus()
                                break
        }
    } else {
        return false
    }
}

// Devuelve un vector con los nombre de todos los campos del formulario
function nombres_elementos(formulario) {
    var e
    var res = new Array()
    for (e = 0; e < document.forms[formulario].elements.length; e++) {
        res[res.length] = document.forms[formulario].elements[e].name
    }
    return res
}

var cont_documentos = 0;
var cont_imagenes   = 0;
var cont_contenidos = 0;
var cont_categorias = 0;

function seleccionarDocumento(url, idioma, hash) {
    if (cont_documentos < max_documentos) {
        xmlhttp = createXmlHttpequestObject();
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = function() { insertarDivDocumento(xmlhttp, url, idioma, hash); };
        xmlhttp.send(null);
        cambiarSeccion('Documentos', 'C', idioma);
    } else {
        alert(err_maximo_documentos + ' ' + max_documentos);
    }
}

function seleccionarImagen(url, idioma, hash) {
    if (cont_imagenes < max_imagenes) {
        xmlhttp = createXmlHttpequestObject();
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = function() { insertarDivImagen(xmlhttp, url, idioma, hash); };
        xmlhttp.send(null);
        cambiarSeccion('Imagenes', 'C', idioma);
    } else {
        alert(err_maximo_imagenes + ' ' + max_imagenes);
    }
}

function seleccionarContenido(url, hash) {
    if (cont_contenidos < max_contenidos) {
        xmlhttp = createXmlHttpequestObject();
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = function() { insertarDivContenido(xmlhttp, url, hash); };
        xmlhttp.send(null);
        cambiarSeccion('Relacionados', 'B');
    } else {
        alert(err_maximo_contenidos + ' ' + max_contenidos);
    }
}

function seleccionarCategorias(url, hash) {
    if (cont_categorias < max_categorias) {
        reg = url.match(/^.*id\.(\d+).*$/);
        if (!hash) {
            hash = 'cats_' + reg[1];
        }
        xmlhttp = createXmlHttpequestObject();
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = function() { insertarDivCategoria(xmlhttp, url, hash); };
        xmlhttp.send(null);
        cambiarSeccion('Categorias', 'B');
    } else {
        alert(err_maximo_categorias + ' ' + max_categorias);
    }
}

function borrarCategoria(objId) {
    obj = document.getElementById(objId);
    if (obj) {
        obj.outerHTML = '';
        cont_categorias = cont_categorias - 1;
    }
}

function borrarDocumento(objId) {
    obj = document.getElementById(objId);
    if (obj) {
        obj.outerHTML = '';
        cont_documentos = cont_documentos - 1;
    }
}

function borrarImagen(objId, idioma) {
    obj = document.getElementById(objId);
    if (obj) {
        obj.outerHTML = '';
        cont_imagenes = cont_imagenes - 1;
    }
    numerarImagenes(idioma);
}

function borrarContenido(objId) {
    obj = document.getElementById(objId);
    if (obj) {
        obj.outerHTML = '';
        cont_contenidos = cont_contenidos - 1;
    }
}

function insertarDivDocumento(xmlhttp, url, idioma, hash) {
    if (xmlhttp.readyState == 4) { // Complete
        if (xmlhttp.status == 200) { // OK response
            borrarDocumento(hash);
            objGestor = document.getElementById('gestor_Documentos_' + idioma);
            var bloque = document.createElement('div');
            bloque.id = hash;
            bloque.innerHTML = xmlhttp.responseText;
            objGestor.appendChild(bloque);
            load_next();
            cont_documentos = cont_documentos + 1;
        }
    }
}

function insertarDivImagen(xmlhttp, url, idioma, hash) {
    if (xmlhttp.readyState == 4) { // Complete
        if (xmlhttp.status == 200) { // OK response
            borrarImagen(hash);
            objGestor = document.getElementById('gestor_Imagenes_' + idioma);
            var bloque = document.createElement('div');
            bloque.id = hash;
            bloque.innerHTML = xmlhttp.responseText;
            objGestor.appendChild(bloque);
            numerarImagenes(idioma);
            load_next();
            cont_imagenes = cont_imagenes + 1;
        }
    }
}

function insertarDivContenido(xmlhttp, url, hash) {
    if (xmlhttp.readyState == 4) { // Complete
        if (xmlhttp.status == 200) { // OK response
            borrarContenido(hash);
            objGestor = document.getElementById('gestor_Relacionados');
            var bloque = document.createElement('div');
            bloque.id = hash;
            bloque.innerHTML = xmlhttp.responseText;
            objGestor.appendChild(bloque);
            load_next();
            cont_contenidos = cont_contenidos + 1;
        }
    }
}

function insertarDivCategoria(xmlhttp, url, hash) {
    if (xmlhttp.readyState == 4) { // Complete
        if (xmlhttp.status == 200) { // OK response
            borrarCategoria(hash);
            objGestor = document.getElementById('gestor_Categorias');
            var bloque = document.createElement('div');
            bloque.id = hash;
            bloque.innerHTML = xmlhttp.responseText;
            objGestor.appendChild(bloque);
            load_next();
            cont_categorias = cont_categorias + 1;
        }
    }
}

function createXmlHttpequestObject() {
    var xmlHttp;
    if(window.ActiveXObject) {
        try {
            xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            xmlHttp = false;
        }
    } else {
        try {
            xmlHttp = new XMLHttpRequest();
        } catch (e) {
            xmlHttp = false;
        }
    }
    return xmlHttp;
}

function mostrarBloque(id) {
    obj = document.getElementById(id);
    if (obj) {
        if (obj.style.display) {
            obj.style.display = '';
        } else {
            obj.style.display = 'none';
        }
    }
}

function mostrar_botones() {
    mostrarBloque('cajaBotones');
}


// Funciones a ejecutar en el submit
var funciones_someter_form = new Array();
function someter_form(form) {
    var res = true;
    var len = funciones_someter_form.length;
    for (someter_form_i=0; someter_form_i<len; someter_form_i++) {
        eval("res = " + funciones_someter_form[someter_form_i] + "('" + form + "')");
        if (res == false) {
            return false;
        }
    }
    return true;
}

// Control de validación de campos por JS
var obligatorios = new Array();
funciones_someter_form[funciones_someter_form.length] = "submitForm";
function submitForm(form){
    if (!validar_obligatorios(form, obligatorios)){
        alert(ERR_OBLIGATORIOS);
        return false;
    }
    return true;
}

function bajarImagen(hash, idioma) {
    bajar(hash);
    numerarImagenes(idioma);
}

function subirImagen(hash, idioma) {
    subir(hash);
    numerarImagenes(idioma);
}

function numerarImagenes(idioma) {
    var objDiv = document.getElementById('gestor_Imagenes_' + idioma);
    if (objDiv && objDiv.childNodes) {
        var i=0;
        var hijos = objDiv.childNodes.length;
        for (i=0; i < hijos; i++) {
            var id = objDiv.childNodes[i].id;
            var orden = document.getElementById('orden_' + id);
            orden.innerHTML = i+1;
            var obj = document.getElementById('orden_' + id + '_ant');
            obj.style.display = 'inline';
            if (i == 0) {
                obj.style.display = 'none';
            }
            var obj = document.getElementById('orden_' + id + '_sig');
            obj.style.display = 'inline';
            if (i == (hijos - 1)) {
                obj.style.display = 'none';
            }
        }
    }
}

function bajar(id) {
    var objDiv = document.getElementById(id);
    var cambio = objDiv.nextSibling;
    if (cambio) {
        objDiv.parentNode.insertBefore(cambio, objDiv);
    }
}

function subir(id) {
    var objDiv = document.getElementById(id);
    var cambio = objDiv.previousSibling;
    if (cambio) {
        objDiv.parentNode.insertBefore(cambio, objDiv.nextSibling);
    }
}

function continuar(objForm) {
    if (someter_form(objForm.name)) {
        objForm.action = accion_formulario2;
        objForm.submit();
        return true;
    }
    return false;
}

function finalizar(objForm) {
    if (someter_form(objForm.name)) {
        objForm.action = accion_formulario;
        return true;
    }
    return false;
}
