

function doValidationForm(formElementId)
{
    var url = '/async/validateform'
    var data = {};
   

    $("#webForm input:text").each(function()
    {
        
        if ($(this).attr('name')=="captcha[input]")
        {
            data['captcha-input'] = $(this).val();
        }
        else if ($(this).attr('type')!="file")
        {
            data[$(this).attr('name')] = $(this).val();
        }
    });

    $("#webForm textarea").each(function()
    {
        data[$(this).attr('name')] = $(this).val();
    });

    $("#webForm input:checkbox").each(function()
    {
        var id = $(this).attr('id');
        data[$(this).attr('name')] = $(this).val();
        $("#"+id).parent().find('.errors').remove();
    });
    
    $("#webForm input:hidden").each(function()
    {
        data[$(this).attr('name')] = $(this).val();
    });

    
    

           
    $.post(url,data,function(resp)
    {
        var errors=0;
        
        if (formElementId == undefined)
        {
            if (($('input[name=condiciones]').attr('id')=='condiciones') && (!$('input[name=condiciones]').is(':checked')))
            {
                $("#condiciones").parent().append(getErrorHtml(["Debes aceptar las condiciones"], "condiciones"));
                errors+=1;
            }

            for(respKey in resp)
            {
                
                $("#webForm #"+respKey).parent().find('.errors').remove();
                $('#webForm #'+respKey).before(getErrorHtml(resp[respKey], respKey));
                errors+=1;
            }
        }
        else
        {
            $("#webForm #"+formElementId).parent().find('.errors').remove();
            $('#webForm #'+formElementId).before(getErrorHtml(resp[formElementId], formElementId));
             errors+=1;
        }

        if (errors==0) $("#webForm").submit();

    },'json');

}



function getErrorHtml(formErrors,id)
{
    var errors=0;
    var o = '<ul id="errors-'+id+'" class="errors">';
    
    for(errorKey in formErrors)
    {
        if (formErrors[errorKey]!="")
            errors+=1;
        
        o += '<li>' + formErrors[errorKey] + '</li>';
        
    }
    o += '</ul>';

    if (errors==0)
        return "";
    else
        return o;
}
