function desplegar(categoria){
  var menu = document.getElementById(categoria);
    if(menu.className == "ver_menu"){
      menu.className = "ocultar_menu";
    }
    else{
      menu.className = "ver_menu";
    }
}


/**************************************************************************/
/*
*
*
* BBCODE
*
*
****************************************************************************/

function instag(tag){
    var input = document.form_nov.cos;
    if(typeof document.selection != 'undefined' && document.selection) {
        var str = document.selection.createRange().text;
        input.focus();
        var sel = document.selection.createRange();
        sel.text = "[" + tag + "]" + str + "[/" +tag+ "]";
        sel.select();
        return;
    }
    else if(typeof input.selectionStart != 'undefined'){
            var start = input.selectionStart;
            var end = input.selectionEnd;
            var insText = input.value.substring(start, end);
            input.value = input.value.substr(0, start) + '['+tag+']' + insText + '[/'+tag+']'+ input.value.substr(end);
            input.focus();
            input.setSelectionRange(start+2+tag.length+insText.length+3+tag.length,start+2+tag.length+insText.length+3+tag.length);
            return;
        }
        else{
            input.value+=' ['+tag+']Reemplace este texto[/'+tag+']';
            return;
            }
}
function inslink(){
    var input = document.form_nov.cos;
    if(typeof document.selection != 'undefined' && document.selection) {
        var str = document.selection.createRange().text;
        input.focus();
        var my_link = prompt("Enter URL:","http://");
        if (my_link != null) {
            if(str.length==0){
                str=my_link;
            }
            var sel = document.selection.createRange();
            sel.text = "[link]" + my_link + "[link/]" + str + "[/link]";
            sel.select();
        }
        return;
    }
    else if(typeof input.selectionStart != 'undefined'){
        var start = input.selectionStart;
        var end = input.selectionEnd;
        var insText = input.value.substring(start, end);
        var my_link = prompt("Enter URL:","http://");
        if (my_link != null) {
            if(insText.length==0){
                insText=my_link;
            }
            input.value = input.value.substr(0, start) +"[link]" + my_link +"[link/]" + insText + "[/link]"+ input.value.substr(end);
            input.focus();
            input.setSelectionRange(start+11+my_link.length+insText.length+4,start+11+my_link.length+insText.length+4);
        }
        return;
    }
    else{
        var my_link = prompt("Ingresar URL:","http://");
        var my_text = prompt("Ingresar el texto del link:","");
        input.value+="[link]" + my_link + "[link/]" + my_text + "[/link]";
        return;
    }
}
/****************************************************************************
*
*
*  REGISTRAR
*
*
*****************************************************************************/
function validar() {
var reg_ok=new Boolean(true);
var error = "";
    var fr= document.reg;
    if (fr.nom.value.length < 3) {
    error = error + "Escriu com a minim 3 caracteres en el camp \"Nom\".\n";
    fr.nom.focus();
    reg_ok=false;
    }
    
    var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú ";
    var checkStr = fr.nom.value;
    var allValid = true;
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i);
        for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break;
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
    }
    if (!allValid) {
        error = error + "Escriu lletres  al camp \"Nom\".\n";
        fr.nom.focus();
        reg_ok=false;
    }
    
    
    if (fr.cognom1.value.length < 3) {
        error = error + "Escriu com a minim 3 caracteres en el camp \"1er Cognom\".\n";
        fr.cognom1.focus();
        reg_ok=false;
    }
      var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú ";
    var checkStr = fr.cognom1.value;
    var allValid = true;
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i);
        for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break;
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
    }
    if (!allValid) {
        error = error + "Escriu lletres  al camp \"1er Cognom\".\n";
        fr.cognom1.focus();
        reg_ok=false;
    }
    
    if (fr.cognom2.value.length < 3) {
        error = error + "Escriu com a minim 3 caracteres en el camp \"2n Cognom\".\n";
        fr.cognom2.focus();
        reg_ok=false;
    }
      var checkOK = "ABCDEFGHIJKLMNÑOPQRSTUVWXYZÁÉÍÓÚ" + "abcdefghijklmnñopqrstuvwxyzáéíóú ";
    var checkStr = fr.cognom2.value;
    var allValid = true;
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i);
        for (j = 0; j < checkOK.length; j++)
            if (ch == checkOK.charAt(j))
                break;
            if (j == checkOK.length) {
                allValid = false;
                break;
            }
    }
    if (!allValid) {
        error = error + "Escriu lletres  al camp \"2n Cognom\".\n";
        fr.cognom2.focus();
        reg_ok=false;
    }
    
    if (fr.pwd.value.length < 6) {
        error = error + "Escriu com a minim 6 caracteres en el camp \"Contrasenya\".\n";
        fr.cognom2.focus();
        reg_ok=false;
    }
    var strOK = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890";
    var checkStr = fr.pwd.value;
    var Valid = true;
    for (i = 0; i < checkStr.length; i++) {
        ch = checkStr.charAt(i);
        for (j = 0; j < strOK.length; j++)
            if (ch == strOK.charAt(j))
                break;
            if (j == strOK.length) {
                Valid = false;
                break;
            }
    }
     if (!Valid) {
        error = error + "Escriu només caracters alfa-numèrics al camp \"Contrasenya\".\n";
        fr.pwd.focus();
        reg_ok=false;
        }
    if((fr.pwd.value) != (fr.pwd2.value)){
        error = error + "Escriu la mateixa contrasenya que l'anterior al camp \"Repetir Contrassenya\".\n";
        fr.pwd2.focus();
        reg_ok=false;
        }
    if (!(mail(fr.correu.value))) {
        error = error + "Escriu una direccio de correu valida.\n"+fr.correu.value;
        reg_ok=false;
    }
    
    if (reg_ok==false){
        alert(error);
        return false;
        }
    else
        return true;
} 

function mail(texto){

    var mailres = true;            
    var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
    
    var arroba = texto.indexOf("@",0);
    if ((texto.lastIndexOf("@")) != arroba) arroba = -1;
    
    var punto = texto.lastIndexOf(".");
                
     for (var contador = 0 ; contador < texto.length ; contador++){
        if (cadena.indexOf(texto.substr(contador, 1),0) == -1){
            mailres = false;
            break;
     }
    }

    if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
     mailres = true;
    else
     mailres = false;
                
    return mailres; 
}
/***************************************************************************************/
//
//        MULTIFILE IMATGES
//
/***************************************************************************************/
var numero = 0; //Esta es una variable de control para mantener nombres
//diferentes de cada campo creado dinamicamente.
evento = function (evt) { //esta funcion nos devuelve el tipo de evento disparado
return (!evt) ? event : evt;
}
// esta funcion crea dinamicamente los nuevos campos file
addCampo = function () { 
//Creamos un nuevo div para que contenga el nuevo campo
   nDiv = document.createElement('div');
//con esto se establece la clase de la div
   nDiv.className = 'imatgesdiv';
//este es el id de la div, aqui la utilidad de la variable numero
//nos permite darle un id unico
   nDiv.id = 'file' + (++numero);
//creamos el input para el formulario:
   nCampo = document.createElement('input');
//le damos un nombre, es importante que lo nombren como vector, pues todos los campos//compartiran el nombre en un arreglo, asi es mas facil procesar posteriormente con php
   nCampo.name = 'imatges[]';
//Establecemos el tipo de campo
   nCampo.type = 'file';
   //Ahora creamos un link para poder eliminar un campo que ya no deseemos
   a = document.createElement('a');
//El link debe tener el mismo nombre de la div padre, para efectos de localizarla y eliminarla
   a.name = nDiv.id;
//Este link no debe ir a ningun lado
   a.href = '#';
//Establecemos que dispare esta funcion en click
   a.onclick = elimCamp;
//Con esto ponemos el texto del link
   a.innerHTML = 'Eliminar';
//Bien es el momento de integrar lo que hemos creado al documento,//primero usamos la función appendChild para adicionar el campo file nuevo
   nDiv.appendChild(nCampo);
//Adicionamos el Link
   nDiv.appendChild(a);
//Ahora si recuerdan, en el html hay una div cuyo id es 'adjuntos', bien//con esta función obtenemos una referencia a ella para usar de nuevo appendChild//y adicionar la div que hemos creado, la cual contiene el campo file con su link de eliminación:
   container = document.getElementById('adjimatges');
   container.appendChild(nDiv);
}
//con esta función eliminamos el campo cuyo link de eliminación sea presionado
elimCamp = function (evt){
evt = evento(evt);
nCampo = rObj(evt);
div = document.getElementById(nCampo.name);
div.parentNode.removeChild(div);
}
//con esta función recuperamos una instancia del objeto que disparo el evento
rObj = function (evt) { 
return evt.srcElement ?  evt.srcElement : evt.target;
}


// esta funcion crea dinamicamente los nuevos campos file
addCampoArx = function () { 
//Creamos un nuevo div para que contenga el nuevo campo
   nDiv = document.createElement('div');
//con esto se establece la clase de la div
   nDiv.className = 'arxiusdiv';
//este es el id de la div, aqui la utilidad de la variable numero
//nos permite darle un id unico
   nDiv.id = 'file' + (++numero);
//creamos el input para el formulario:
   nCampo = document.createElement('input');
//le damos un nombre, es importante que lo nombren como vector, pues todos los campos//compartiran el nombre en un arreglo, asi es mas facil procesar posteriormente con php
   nCampo.name = 'arxius[]';
//Establecemos el tipo de campo
   nCampo.type = 'file';
   //Ahora creamos un link para poder eliminar un campo que ya no deseemos
   a = document.createElement('a');
//El link debe tener el mismo nombre de la div padre, para efectos de localizarla y eliminarla
   a.name = nDiv.id;
//Este link no debe ir a ningun lado
   a.href = '#';
//Establecemos que dispare esta funcion en click
   a.onclick = elimCamp;
//Con esto ponemos el texto del link
   a.innerHTML = 'Eliminar';
//Bien es el momento de integrar lo que hemos creado al documento,//primero usamos la función appendChild para adicionar el campo file nuevo
   nDiv.appendChild(nCampo);
//Adicionamos el Link
   nDiv.appendChild(a);
//Ahora si recuerdan, en el html hay una div cuyo id es 'adjuntos', bien//con esta función obtenemos una referencia a ella para usar de nuevo appendChild//y adicionar la div que hemos creado, la cual contiene el campo file con su link de eliminación:
   container = document.getElementById('adjarxius');
   container.appendChild(nDiv);
}