function rg_unserialize(url) {
  var params = url;
  if (url.match(/\?(.+)$/)) {
      params = RegExp.$1;
  }
  var pArray = params.split("&");
  var pHash = {};
  for(var i = 0; i < pArray.length; i++) {
    var temp = pArray[i].split("=");
    pHash[temp[0]] = unescape(temp[1]).replace(/\+/g, ' ');
  }
  return pHash;
}

function rg_get_param(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null)
    return null;
  else
    return results[1];
}

function get_full_rg_file_path(name) {
  return document.rg_root_path + name;
}

function rg_show_hourglass(hide_element, wait_text) {
  document.rg_ajax_call_pending = true;
  if (hide_element && hide_element.length > 0) {
    document.rg_ajax_call_hide_element = hide_element;
    hide_element.css('display', 'none');
    hide_element.before("<div id='hourglass_element'>"+(wait_text ? wait_text : '')+" <img src='" + get_full_rg_file_path('images/hourglass.gif') + "' align='middle' /></div>");
  }
}

function rg_hide_hourglass() {
  if (document.rg_ajax_call_pending) {
    if (document.rg_ajax_call_hide_element && document.rg_ajax_call_hide_element.length > 0) {
      document.rg_ajax_call_hide_element.css('display', 'inline');
      $('#hourglass_element').remove();
      document.rg_ajax_call_hide_element = null;
    }
    document.rg_ajax_call_pending = false;
  }
}

function rg_on_request_failure() {
  var error_div = $('#error_msg');
  if (error_div && error_div.length > 0) {
    error_div.css('display', 'inline');
    error_div.html("Unknown error occurred.");
  }
}

function rg_send_request(url, data, on_complete_function, hide_item, wait_text) {
  rg_show_hourglass(hide_item, wait_text);
  $.post(url, data,
         function(data, status) {
           rg_hide_hourglass();
           if (status == "success") {
             on_complete_function(data, hide_item);
           }
           else {
             rg_on_request_failure();
           }
	 }, 'html');
}

function rg_report_done(txt, hide_item) {
  $('#generate_report_status').html(txt);
}

function rg_rating_done(txt, hide_item) {
  hide_item.html (txt);
}

function rg_create_report(report_category_id) {
  rg_send_request(get_full_rg_file_path('refresh-report.php'), {cid: report_category_id}, rg_report_done, $('#generate_report_status'), "Please wait while the report is generated...");
}

function rg_switch_report_view(div_id) {
  $('#'+div_id).css('display', $('#'+div_id).css('display') == 'none' ? 'block' : 'none');
}

function rg_switch_all_reports() {
  if (document.all_displayed) {
    $('.report-part').css('display','none');
    document.all_displayed = false;
  }
  else {
    $('.report-part').css('display','block');
    document.all_displayed = true;
  }
}

function rg_rate_item(item_type, item_id, report_category_id, rate_offset, div_id) {
  cookie_name = item_type+"_"+report_category_id+"_"+item_id;
  if ($.cookie(cookie_name) == '1')
    return;

  rg_send_request(get_full_rg_file_path('rate-item.php'), {id: item_id, type: item_type, offset: rate_offset, cid: report_category_id}, rg_rating_done, $('#'+div_id));
  $.cookie(cookie_name, '1', {expires: 365, path: '/'});
}

