function handle_error(elem_id, data) {
    if (data.error) {
        var target = $('#'+elem_id);
        if (!target.length) target = $('#'+elem_id+'0');
        if (target.length) target.html('<span style="color:red;font-size:xx-small">counter error:'+data.error+'</span>');
        return true;
    }
    return false;
}

function get_count(url, elem_id, nofill) {
    $.getJSON(url + '?t=' + new Date().getTime(),
        function(data){
            if(nofill)return;
            if (handle_error(elem_id, data)) {
                // error
            } else if (data.count.length == 1) {
                var value = data.count[0]
                $('#'+elem_id).text(value);
                $('#'+elem_id+'0').text(value);
            } else {
                $.each( data.count, function (i, count) {
                    $('#'+elem_id+i).html(count);
                }); 
            }
        });
}

function get_ctr(elem_id, site, oid) {
    $.getJSON('/counter/read/advert_all_impressions/'+site+'/'+oid+'/' + '?t=' + new Date().getTime(), function(data) {
        var impressions_cnt = data.count[0];
        if(impressions_cnt != 0 && !handle_error(elem_id, data)) {
            $.getJSON('/counter/read/advert_all_views/'+site+'/'+oid+'/' + '?t=' + new Date().getTime(), function(data) {
                if(!handle_error(elem_id, data)) {
                    var views_cnt = data.count[0];
                    ctr = parseInt(100*views_cnt/impressions_cnt);
                    $('#'+elem_id).text(''+ctr+'%');
                }
            });
        } else {
            $('#'+elem_id).html('N/A');
        }
    });
}

function make_count(elem_id, site, name, oid, action, nofill) {
    var newelem = get_count('/counter/'+action+'/'+name+'/'+site+'/'+oid+'/', elem_id, nofill)
}

function make_count_and_read(elem_id, site, name, oid, sum_name, nofill) {
    var newelem = get_count('/counter/write_and_read/'+name+'/'+site+'/'+oid+'/'+sum_name+'/', elem_id, nofill)
}

function make_counts(elem_id_prefix, site, oname, oids, action, nofill) {
    var newelem = get_count('/counter/'+action+'/'+oname+'/'+site+'/'+oids.join(',')+'/', elem_id_prefix, nofill)
}

function make_count_with_redirect( site, name, oid, href ) {
    /* Makes count request using jQuery and after it is proccessed,
     * either with error or not, it redirects to new page.
     * Basically it holds down synchronous redirect before asynchronous
     * request is finished.
     * It's used in e.g. counting 'go to dealer's website' link click on 
     * ad details page. 
     * */
    var url = '/counter/write/'+name+'/'+site+'/'+oid+'/';
    $.ajax({
        type: "GET",
        url: url,
        data: "t=" + new Date().getTime(), // IE's caching hacked, #4967
        success: function( response ) {
            document.location = href;
        },
        error: function() {
            document.location = href;
        }
    } );
}

function make_count_with_submit( site, name, oid, form ) {
    /* Similar to make_count_with_redirect but with form submit, 
     * used in e.g. ad details email forms
     * */
    var url = '/counter/write/'+name+'/'+site+'/'+oid+'/';
    $.ajax( {
        type: "GET",
        url: url,
        data: "t=" + new Date().getTime(), // IE's caching hacked, #4967
        success: function( response ) {
            $( form ).submit();
        },
        error: function() {
            $( form ).submit();
        }
    } );
}
