var busy = false;

function saveItem(oForm)
{
    //busy = getBusyOverlay(oForm);

    $(".error").removeClass('error');
//
//    $('#ajaxErrorWrapper').hide();

    $.post(oForm.action, $(oForm).serialize(), function(data)
    {
        if (data)
        {
            if (data.status == 'false')
            {
                ajaxFormErrorHandling(data);
            }
            else
            {
                formHandling(data);
            }
        }
    }, "json");

    return false;
}

function ajaxFormErrorHandling(json)
{
    if (busy)
    {
        busy.remove();
    }

    if (json.errors.length > 0 && json.status == 'false')
    {
        $('#ajaxErrorWrapper').html('');

        for (i=0; i<json.errors.length;i++)
        {
            if ($(json.errors[i].fieldname))
            {
                $('#' + json.errors[i].fieldname).addClass('error');
            }

            var html = $('#ajaxErrorWrapper').html();
            $('#ajaxErrorWrapper').html(html + '<p>' + json.errors[i].message + '</p>');// = $('#ajaxErrorContent').html() + $('#ajaxErrorContent').html('<p>' + json.errors[i].message + '</p>');
        }

        $('#ajaxErrorWrapper').show();
    }
}

function formHandling(json)
{
    if (busy)
    {
        busy.remove();
    }

    if (json.status == 'true')
    {
        if (json.location != '')
        {
            window.location = json.location;
        }
    }
}

function changeLanguage(obj)
{
    document.location = '/changelanguage/' + obj.value + '.html';
}
