function loadPage()
{
    DWRUtil.useLoadingMessage();
    roundedPage();
}

function roundedPage()
{
    Nifty("div#principal,div.topbottom,div.color,div.labels","normal");
    Nifty("div.topopenclose","top normal");
    Nifty("ul#nav a","normal transparent top");
}

function checkAlfaNumPunGui(data)
{
    //significa la primera letra miniscula y despues o letra o numero o . o -
    var ex_alfanum = /^[a-z]+[a-z0-9._]+$/ 
    if(!ex_alfanum.test(data)) 
        return false;
    else
        return true;
}

function checkAlfaNum(data)
{
    //significa la primera letra miniscula y despues o letra o numero o . o -
    var ex_alfanum = /^[a-z0-9]+$/ 
    if(!ex_alfanum.test(data))
        return false;
    else
        return true;
}

function checkAlfaMayuEspecialesNum(data)
{
    //significa la primera letra miniscula y despues o letra o numero o . o -
    var ex_alfanum = /^[A-Za-z0-9ñçÑÇ]+$/ 
    if(!ex_alfanum.test(data))            
        return false;
    else
        return true;
}

function checkLength(data, min, max)
{
    if(data !=null && data !="") 
    {
        if((data.length >= min) && (data.length <=max))
            return true;
    }
    return false;
}

function checkEmail(data)
{
    var ex_email = /^(.+\@.+\..+)$/
    if(!ex_email.test(data))
        return false;
    else
        return true;
}

function objectEval(text)
{
    text = text.replace(/\n/g, ' ');
    text = text.replace(/\r/g, ' ');
    if (text.match(/^\s*\{.*\}\s*$/))
    {
      text = '[' + text + '][0]';
    }
    return eval(text);
}

function checkLogin()
{
    var checkLogin = DWRUtil.getValue("checkLogin");
    if(checkLogin == "1")
    {
       return true;
    }
    else
    {
        return false;
    }
}

function marcarMyDocuments()
{
    for (i=0;i<document.form.elements.length;i++)
    {
        if(document.form.elements[i].type == "checkbox")
        {
             document.form.elements[i].checked=true;
        }
    }
}

function desmarcarMyDocuments()
{
    for (i=0;i<document.form.elements.length;i++)
    {
        if(document.form.elements[i].type == "checkbox")
        {
             document.form.elements[i].checked=false;
        }
    }
}

function enviarInvitacion()
{
    DWRUtil.setValue('respEnviarInvitacion', "");
    var email = DWRUtil.getValue("email");
    if((email != null) && (checkEmail(email))) 
    {
        document.getElementById("BSend").disabled = true; 
        DwrFunctions.EnviarInvitacion(email, replyEnviarInvitacion);
    }
    else
    {
        DWRUtil.setValue('email', "");
    }
}

var replyEnviarInvitacion = function(data)
{
   if (data != null)
   {
        DWRUtil.setValue('respEnviarInvitacion', data);
        DWRUtil.setValue('email', "");
        window.setTimeout("DWRUtil.setValue('respEnviarInvitacion', '');",10000); 
   }
   document.getElementById("BSend").disabled = false;
}

function printOpen(key)
{
    if(checkLogin())
    {
        if((key != null) && (key !="")) 
        {
            DwrFunctions.printOpen(key, replyOpenClose);
        }
    }
    else
    {
        alert("Debes estar Registrado");
    }
}

function printClose(key)
{
    if((key != null) && (key !="")) 
    {
        DwrFunctions.printClose(key, replyOpenClose);
    }
}

var replyOpenClose = function(data)
{   
    if (data != null && typeof data == 'object')
    {
        DWRUtil.setValue(data[0], data[1]); 
        roundedPage();
        //La posicion 0 es el key del span donde deberemos poner el mensaje
    }
}

function reenviarPassword()
{
    var email = DWRUtil.getValue('email');
    DWRUtil.setValue('respReenviarPassword', "");
    if((email != null) && (checkEmail(email))) 
    {
        document.getElementById("BSend").disabled = true; 
        DwrFunctions.ReenviarPassword(email, replyReenviarPassword);
    }
    else
    {
        DWRUtil.setValue('email', "");
    }
}

var replyReenviarPassword = function(data)
{
   if (data != null) 
   {
        DWRUtil.setValue('respReenviarPassword', data);
        DWRUtil.setValue('email',"");
        document.getElementById("BSend").disabled = false; 
        //Transcurridos 3 desactivamos el lightbox
        deactivateLightboxTime(3000);
   }
}


//
//FUNCIONES MYDOCUMENTS.HTML
//

function addToMyDocument(id_documento)
{
    if(checkLogin())
    {
        if((id_documento != null) && (id_documento !="")) 
        {
            DwrFunctions.addToMyDocuments(id_documento, replyAddToMyDocument);
        }
    }
    else
    {
        alert("Debes estar registrado");
    }
}

var replyAddToMyDocument = function(data)
{
    if (data != null && typeof data == 'object')
    {
        DWRUtil.setValue(data[0], data[1]); 
        //La posicion 0 es el key del span donde deberemos poner el mensaje
    }
}

function printMoreDetails(id_documento)
{
    if((id_documento != null) && (id_documento !="")) 
    {
        DwrFunctions.printMoreDetails(id_documento, replyPrintMoreDetails);
    }
}

var replyPrintMoreDetails = function(data)
{
    if (data != null && typeof data == 'object')
    {
        DWRUtil.setValue(data[0], data[1]); 
        DWRUtil.setValue(data[2], data[3]); 
        //La posicion 0 es el key del span donde deberemos poner el mensaje
    }
}
function printHideDetails(id_documento)
{
    if((id_documento != null) && (id_documento !="")) 
    {
        DwrFunctions.printHideDetails(id_documento, replyPrintHideDetails);
    }
}

var replyPrintHideDetails = function(data)
{
    if (data != null && typeof data == 'object')
    {
        DWRUtil.setValue(data[0], data[1]); 
        DWRUtil.setValue(data[2], data[3]); 
        //La posicion 0 es el key del span donde deberemos poner el mensaje
    }
}

function crearCarpeta()
{
    DWRUtil.setValue('respCrearCarpeta', "");
    var folder = DWRUtil.getValue("folder");
    if((folder != null) && (checkLength(folder, 1, 15)) && (checkAlfaMayuEspecialesNum(folder))) 
    {
        document.getElementById("BCreate").disabled = true;
        DwrFunctions.crearCarpeta(folder, replyCrearCarpeta);
    }
}

var replyCrearCarpeta = function(data)
{
   if (data != null)
   {
        DWRUtil.setValue('respCrearCarpeta', data);
        //Actualizamos menu carpeta
        DwrFunctions.actualizarCarpetas(replyActualizarCarpeta);
        //Actualizamos select carpeta move
        DwrFunctions.actualizarCarpetasSelectMove(replyActualizarCarpetaSelectMove);
        //Actualizamos select carpeta Rename
        DwrFunctions.actualizarCarpetasSelectRename(replyActualizarCarpetaSelectRename);
        //Actualizamos select carpeta Delete
        DwrFunctions.actualizarCarpetasSelectDelete(replyActualizarCarpetaSelectDelete);
        window.setTimeout("DWRUtil.setValue('respCrearCarpeta', '');",10000); 
        DWRUtil.setValue('folder', "");
   }
   document.getElementById("BCreate").disabled = false;
}

function renameFolder()
{
    var idCarpetaToRename = DWRUtil.getValue('folderToRename');
    var nombreCarpetaNuevo = DWRUtil.getValue('folderrename');
    if((idCarpetaToRename != null)&&(idCarpetaToRename != "") && (nombreCarpetaNuevo != null) && (checkLength(nombreCarpetaNuevo, 1, 15)) && (checkAlfaMayuEspecialesNum(nombreCarpetaNuevo)))
    {
        var confirmacion = DWRUtil.getValue('confirmacion');
        if(confirm(confirmacion)==true)
        {
            document.getElementById("BRename").disabled = true;
            DwrFunctions.changeNameFolder(idCarpetaToRename,nombreCarpetaNuevo,replyRenameFolder);
        }
    }
}

var replyRenameFolder= function(data)
{
   if (data != null)
   {
        DWRUtil.setValue('respRenameFolder', data);
        //Actualizamos menu carpeta
        DwrFunctions.actualizarCarpetas(replyActualizarCarpeta);
        //Actualizamos select carpeta move
        DwrFunctions.actualizarCarpetasSelectMove(replyActualizarCarpetaSelectMove);
        //Actualizamos select carpeta Rename
        DwrFunctions.actualizarCarpetasSelectRename(replyActualizarCarpetaSelectRename);
        //Actualizamos select carpeta Delete
        DwrFunctions.actualizarCarpetasSelectDelete(replyActualizarCarpetaSelectDelete);
    //    window.setTimeout("DWRUtil.setValue('respRenameFolder', '');",10000); PROBLEMAS PARA INTERNET EXPLORER 7
        DWRUtil.setValue('folderrename','');
   }
   document.getElementById("BRename").disabled = false;
}

function deleteFolder()
{
    var id_carpeta = DWRUtil.getValue('folderToDelete');
    if((id_carpeta != null)&&(id_carpeta !=""))
    {
        var confirmacion = DWRUtil.getValue('confirmacion');
        if(confirm(confirmacion)==true)
        {
            document.getElementById("BDelete").disabled = true;
            DwrFunctions.deleteFolder(id_carpeta,replyDeleteFolder);
        }   
    }
}

var replyDeleteFolder= function(data)
{
     if (data != null)
     {
        DWRUtil.setValue('respDeletefolder', data);
        //Actualizamos menu carpeta
        DwrFunctions.actualizarCarpetas(replyActualizarCarpeta);
        //Actualizamos select carpeta move
        DwrFunctions.actualizarCarpetasSelectMove(replyActualizarCarpetaSelectMove);
        //Actualizamos select carpeta Rename
        DwrFunctions.actualizarCarpetasSelectRename(replyActualizarCarpetaSelectRename);
        //Actualizamos select carpeta Delete
        DwrFunctions.actualizarCarpetasSelectDelete(replyActualizarCarpetaSelectDelete);
        window.setTimeout("DWRUtil.setValue('respDeletefolder', '');",10000); 
     }
     document.getElementById("BDelete").disabled = false;
}

var replyActualizarCarpeta = function(data)
{
   if (data != null)
   {
        DWRUtil.setValue('respActualizarCarpetas', data); 
   }
}

var replyActualizarCarpetaSelectMove = function(data)
{
   if (data != null)
   {
        DWRUtil.setValue('respActualizarCarpetaSelectMove', data); 
   }
}
var replyActualizarCarpetaSelectDelete = function(data)
{
   if (data != null)
   {
        DWRUtil.setValue('respActualizarCarpetaSelectDelete', data); 
   }
}
var replyActualizarCarpetaSelectRename = function(data)
{
   if (data != null)
   {
        DWRUtil.setValue('respActualizarCarpetaSelectRename', data); 
   }
}

function printDocumentosCarpetas(id_carpeta)
{
    if((id_carpeta != null) && (id_carpeta !="")) 
    {
        DwrFunctions.printDocumentosCarpetas(id_carpeta, replyPrintDocumentosCarpetas);
    }
}

var replyPrintDocumentosCarpetas= function(data)
{
    if (data != null)
    {
        DWRUtil.setValue('respPrintDocumentosCarpetas', data); 
    }
}


function deleteDocuments()
{
    var id_carpeta_origen = DWRUtil.getValue('foldernow');
    var confirmacion = DWRUtil.getValue('confirmacion');
    if(confirm(confirmacion)==true)
    {
        for (i=0;i<document.form.elements.length;i++)
        {
            if(document.form.elements[i].type == "checkbox")
            {
                 if(document.form.elements[i].checked == true)
                 {
                     DwrFunctions.deleteDocumets(document.form.elements[i].value,replyDeleteDocuments);
                 }
            }
        }
    }
}

var replyDeleteDocuments = function(data)
{
    var id_carpeta_origen = DWRUtil.getValue('foldernow');
    //Imprimimos de nuevo los documentos de esa carpeta
    printDocumentosCarpetas(id_carpeta_origen);
    //Actualizamos todas las carpetas
    DwrFunctions.actualizarCarpetas(replyActualizarCarpeta);
}

function changeFolder()
{
    var carpetaDestino = DWRUtil.getValue('folderDestino');
    if((carpetaDestino != null)&&(carpetaDestino != ""))
    {
        var confirmacion = DWRUtil.getValue('confirmacion');
        if(confirm(confirmacion)==true)
        {
            for (i=0;i<document.form.elements.length;i++)
            {
                if(document.form.elements[i].type == "checkbox")
                {
                     if(document.form.elements[i].checked == true)
                     {
                         DwrFunctions.changeDocumentFolder(document.form.elements[i].value,carpetaDestino,replyChangeFolder);
                     }
                }
            }
        }
    }
}

var replyChangeFolder= function(data)
{
    var id_carpeta_origen = DWRUtil.getValue('foldernow');
    //Imprimimos de nuevo los documentos de esa carpeta
    printDocumentosCarpetas(id_carpeta_origen);
    //Actualizamos todas las carpetas
    DwrFunctions.actualizarCarpetas(replyActualizarCarpeta);
}

////////////////////////////////////////////////////////////////////

function changePassword()
{
    var passwordold = DWRUtil.getValue('passwordold');
    var passwordnew1 = DWRUtil.getValue('passwordnew1');
    var passwordnew2 = DWRUtil.getValue('passwordnew2');
        
    if((passwordnew1!= null)&&(passwordnew1!= "")&&(passwordnew2!= null)&&(passwordnew2!="")&&(passwordold!=null)&&(passwordold!=""))
    {
        document.getElementById("BChangeP").disabled = true;
        DwrFunctions.changePassword(passwordold,passwordnew1,passwordnew2,replyChangePassword);
    }
}

var replyChangePassword= function(data)
{
    if (data != null)
     {
        DWRUtil.setValue('respChangePassword', data);
        DWRUtil.setValue('passwordold','');
        DWRUtil.setValue('passwordnew1','');
        DWRUtil.setValue('passwordnew2','');
        window.setTimeout("DWRUtil.setValue('respChangePassword', '');",10000);
     }
     document.getElementById("BChangeP").disabled = false;
}

function insertComentarioDocumento()
{
    var votos = DWRUtil.getValue('votos');
    var comentario = DWRUtil.getValue('comentario');
    var id_documento = DWRUtil.getValue('id_documento');        
    if((votos!= null)&&(votos!= "")&&(comentario!= null)&&(comentario!="")&&(id_documento!= null)&&(id_documento!=""))
    {
        document.getElementById("BComentario").disabled = true;
        DwrFunctions.insertComentario(id_documento,comentario,votos,replyInsertComentarioDocumento);
    }
}

var replyInsertComentarioDocumento= function(data)
{
    if (data != null)
     {
        DWRUtil.setValue('respInsertComentarioDocumento', data);
        DWRUtil.setValue('votos','');
        DWRUtil.setValue('comentario',''); 
        printActualizarComentariosDocumento();
     }
     document.getElementById("BComentario").disabled = false;
}

function printActualizarComentariosDocumento()
{
    var id_documento = DWRUtil.getValue('id_documento');        
    if((id_documento!= null)&&(id_documento!=""))
    {
        DwrFunctions.printActualizarComentariosDocumento(id_documento,replyPrintActualizarComentariosDocumento);
    }
}

var replyPrintActualizarComentariosDocumento = function(data)
{
    if (data != null)
    {
        DWRUtil.setValue('respPrintActualizarComentariosDocumento', data);
    }
    window.setTimeout("DWRUtil.setValue('respInsertComentarioDocumento', '');",10000);
}

//FUNCION PARA VALIDAR EL FORMULARIO DE SUBIR FICHERO

function verifyUploadFile()
{
    var error = false;
    //document.getElementById("spanloadupload").style.visibility = "visible";
    var file=document.FUpload.file.value;
    var categoria=document.FUpload.c.value;
    var idioma=document.FUpload.l.value;
    var titulo=document.FUpload.t.value;
    var codigo=document.FUpload.code.value;
    
    if((checkLength(file, 3, 500) == false)) 
    {
        error = true;
    }
    if(error==false)
    {
        var guardandoDocu = DWRUtil.getValue('guardandoDocu');
        document.getElementById("BUpload").disabled = true;
        document.getElementById("BUpload").value = guardandoDocu;
        return true;
    }
    else
    {
        //document.getElementById("spanloadupload").style.visibility = "hidden";
        document.getElementById("BUpload").disabled = false;
        return false;
    }
}

//FUNCION PARA HACER EL LOGIN

function login()
{
    document.getElementById("BLogin").disabled = true;
    return true;
}

//FUNCION PARA HACER EL LOGINDWR

function loginKeyEnter(e)
{
    var tecla = (document.all) ? e.keyCode : e.which; // obtenemos la tecla pulsada
    if(tecla==13)
    {
        loginDwr();
    }      
}

function loginDwr()
{
    var user = DWRUtil.getValue('userDwr');
    var password = DWRUtil.getValue('passwordDwr');
    DWRUtil.setValue('respValidarUsuario', '');
    
    if((checkLength(user, 3, 15))&&(checkLength(password, 3, 15))) 
    {
        DwrFunctions.loginDwr(user, password, function(data) { 
            if(data == true)
            {
                var form =  document.getElementById("FDwrLogin");
                form.submit();
            }
            else
            {
                DWRUtil.setValue('userDwr', '');
                DWRUtil.setValue('passwordDwr', '');
                var error = DWRUtil.getValue('error');
                DWRUtil.setValue('respValidarUsuario', error);
            }
        });
    }
    else
    {
        DWRUtil.setValue('userDwr', '');
        DWRUtil.setValue('passwordDwr', '');
        var error = DWRUtil.getValue('error');
        DWRUtil.setValue('respValidarUsuario', error);
    }
    
}

//FUNCION PARA EL AUTOCOMPLETE

function updateListEtiquetas(autocompleter, token) {
   DwrFunctions.getListEtiquetas(token, function(data) {   
      autocompleter.setChoices(data) 
   });
}

function updateListUsuarios(autocompleter, token) {
   DwrFunctions.getListUsuarios(token, function(data) {   
      autocompleter.setChoices(data) 
   });
}

function nameValueSelector(tag){
   return tag;
}
