
function get_count(url, elem_id, nofill) {
  var httpRequest;
  var t = "?t=" + new Date().getTime(); // IE's caching hacked, #4967
  url += t;

  $.ajax({
  type: "GET",
  url: url,
  async: true,
  dataType: "json",
  success: function(data){if(!nofill){fill_count(data,elem_id)}}
  });
}

function fill_count(data, elem_id) {
      var count_dict = data;
      if (count_dict.error)
      {
        if (document.getElementById(elem_id))
          document.getElementById(elem_id).innerHTML = '<span style="color:red;font-size:xx-small">counter error:'+count_dict.error+'</span>'
        else if (document.getElementById(elem_id+'0'))
          document.getElementById(elem_id+'0').innerHTML = '<span style="color:red;font-size:xx-small">counter error:'+count_dict.error+'</span>'
      }
      else if (count_dict.count.length == 1 && document.getElementById(elem_id))
      {
        document.getElementById(elem_id).innerHTML = count_dict.count[0]
      }
      else
      {
          for (var i=0; i < count_dict.count.length; ++i)
          {
              counter = document.getElementById(elem_id+i);
              if ( counter )
              {
                  counter.innerHTML = count_dict.count[i]
              }
          }
      }
}

function make_count(elem_id, site, name, oid, action, nofill) {
    var newelem = get_count('/counter/'+action+'/'+name+'/'+site+'/'+oid+'/', 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.
     * Optionaly you can add fifth argumet 'true' if you wont href open new window.
     * */
    var url = '/counter/write/'+name+'/'+site+'/'+oid+'/';
	if(arguments[4] && arguments[4]=="true"){
		var newWindow = true;
	}
    $.ajax({
        type: "GET",
        url: url,
        data: "t=" + new Date().getTime(), // IE's caching hacked, #4967
        success: function( response ) {
			if(newWindow){
				window.open(href);
			}else{
				document.location = href;
			}
        },
        error: function() {
			if(newWindow){
				window.open(href);
			}else{
				document.location = href;
			}
        }
    } );
}

function make_count_with_submit(site, name, oid, form, blockSubmit) {
    /* Similar to make_count_with_redirect but with form submit, 
     * used in e.g. ad details email forms
     * */
    if (blockSubmit == undefined) {
        var blockSubmit = true;
    }
    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 ) {
            if (!blockSubmit) {
                $( form ).submit();
            }
        },
        error: function() {
            if (!blockSubmit) {
                $( form ).submit();
            }
        }
    });
    return !(blockSubmit);
}
