var errores = new Array();

function validar(formId)
{
    var campos = document.getElementById(formId).elements;
    var validationResult = true;

    errores = new Array();
    
    for (var i = 0; i < campos.length; i++)
    {		
        if (campos[i].validar)
        {
            if (!campos[i].validar())
            {
                validationResult = false;
            }
        }
    }

    if (validationResult)
    {
        document.getElementById(formId).submit();
    }
    else
    {
        alert('Se han encontrado los siguientes errores al validar los datos:\n\n' + errores.join('\n'));
    }

    return validationResult;
}

function inicializar(idCampo, error)
{
    document.getElementById(idCampo).mostrarError = function()
    {
        this.className = 'formulariono';
        this.tipAntiguo = this.title;
        this.title = error;
        errores[errores.length] = '- ' + error;
    };

    document.getElementById(idCampo).limpiarError = function()
    {
        this.className = 'formulario';
        this.title = this.tipAntiguo;
    };
    
    document.getElementById(idCampo).validar = function()
    {
        var returnValue = (this.value != '');
        if (!returnValue)
        {
            this.mostrarError();
        }
        else
        {
            this.limpiarError();
        }
        return returnValue;
    };
}

