function massageRankingsResult(result) {
    var newResult = result;
    if (newResult.numberOfRankings > 0) {
        for (var i in newResult.rankings) {
            var ranking = newResult.rankings[i];
            ranking.subject = { id: ranking.subjectId, name: ranking.subjectName };
        }
    }
    return newResult;
}

HyltonRankingsLoader = function(sid) {

  this.onSuccess = new YAHOO.util.CustomEvent('HyltonRankingsLoader_Success', this);

  this.handleSuccess = function(o) {
    var result;
    var roleId;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
      roleId = o.argument;
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }

    if (result.redirectTo) {
      window.location = result.redirectTo;
    }
    else {
      this.onSuccess.fire({result: massageRankingsResult(result), roleId: roleId});
    }
  };

  this.handleFailure = function(o) {
    var result;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }
  };

  this.loadRankings = function(roleId) {
    var callback = {
      success : this.handleSuccess,
      failure : this.handleFailure,
      scope : this,
      argument : roleId
    };

    YAHOO.util.Connect.asyncRequest('POST', GLOBALS.contextPath + '/ajax/loadHyltonRankings?subjectId=' + sid, callback, 'roleId=' + roleId);
  };
}

PerfectAttorneyRegulatoryRecordRankingsLoader = function(sid) {

  this.onSuccess = new YAHOO.util.CustomEvent('PARRRankingsLoader_Success', this);

  this.handleSuccess = function(o) {
    var result;
    var roleId;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
      roleId = o.argument;
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }

    if (result.redirectTo) {
      window.location = result.redirectTo;
    }
    else {
      this.onSuccess.fire({result: massageRankingsResult(result), roleId: roleId});
    }
  };

  this.handleFailure = function(o) {
    var result;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }
  };

  this.loadRankings = function(roleId) {
    var callback = {
      success : this.handleSuccess,
      failure : this.handleFailure,
      scope : this,
      argument: roleId
    };

    YAHOO.util.Connect.asyncRequest('POST', GLOBALS.contextPath + '/ajax/loadPARRRankings?subjectId=' + sid, callback, 'roleId=' + roleId);
  };
}

AttorneyRegulatorySightingsRankingsLoader = function(sid) {

  this.onSuccess = new YAHOO.util.CustomEvent('ARSRankingsLoader_Success', this);

  this.handleSuccess = function(o) {
    var result;
    var roleId;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
      roleId = o.argument;
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }

    if (result.redirectTo) {
      window.location = result.redirectTo;
    }
    else {
      this.onSuccess.fire({result: massageRankingsResult(result), roleId: roleId});
    }
  };

  this.handleFailure = function(o) {
    var result;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }
  };

  this.loadRankings = function(roleId) {
    var callback = {
      success : this.handleSuccess,
      failure : this.handleFailure,
      scope : this,
      argument: roleId
    };

    YAHOO.util.Connect.asyncRequest('POST', GLOBALS.contextPath + '/ajax/loadARSRankings?subjectId=' + sid, callback, 'roleId=' + roleId);
  };
}

IndustryReputationRankingsLoader = function(sid) {

  this.onSuccess = new YAHOO.util.CustomEvent('IndustryRepRankingsLoader_Success', this);

  this.handleSuccess = function(o) {
    var result;
    var roleId;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
      roleId = o.argument;
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }

    if (result.redirectTo) {
      window.location = result.redirectTo;
    }
    else {
      this.onSuccess.fire({result: massageRankingsResult(result), roleId: roleId});
    }
  };

  this.handleFailure = function(o) {
    var result;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }
  };

  this.loadRankings = function(roleId) {
    var callback = {
      success : this.handleSuccess,
      failure : this.handleFailure,
      scope : this,
      argument: roleId
    };

    YAHOO.util.Connect.asyncRequest('POST', GLOBALS.contextPath + '/ajax/loadIndustryRepRankings?subjectId=' + sid, callback, 'roleId=' + roleId);
  };
}

PerfectContractorRegulatoryRecordRankingsLoader = function(sid) {

  this.onSuccess = new YAHOO.util.CustomEvent('PCRRRankingsLoader_Success', this);

  this.handleSuccess = function(o) {
    var result;
    var roleId;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
      roleId = o.argument;
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }

    if (result.redirectTo) {
      window.location = result.redirectTo;
    }
    else {
      this.onSuccess.fire({result: massageRankingsResult(result), roleId: roleId});
    }
  };

  this.handleFailure = function(o) {
    var result;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }
  };

  this.loadRankings = function(roleId) {
    var callback = {
      success : this.handleSuccess,
      failure : this.handleFailure,
      scope : this,
      argument: roleId
    };

    YAHOO.util.Connect.asyncRequest('POST', GLOBALS.contextPath + '/ajax/loadPCRRRankings?subjectId=' + sid, callback, 'roleId=' + roleId);
  };
}

ContractorRegulatorySightingsRankingsLoader = function(sid) {

  this.onSuccess = new YAHOO.util.CustomEvent('CRSRankingsLoader_Success', this);

  this.handleSuccess = function(o) {
    var result;
    var roleId;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
      roleId = o.argument;
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }

    if (result.redirectTo) {
      window.location = result.redirectTo;
    }
    else {
      this.onSuccess.fire({result: massageRankingsResult(result), roleId: roleId});
    }
  };

  this.handleFailure = function(o) {
    var result;
    try {
      result = YAHOO.lang.JSON.parse(o.responseText);
    }
    catch (e) {
      result = new Object();
      result.error = true;
      result.errors = {
        '_GLOBAL_' : [ 'an internal error occurred. please try again later' ]
      };
    }
  };

  this.loadRankings = function(roleId) {
    var callback = {
      success : this.handleSuccess,
      failure : this.handleFailure,
      scope : this,
      argument: roleId
    };

    YAHOO.util.Connect.asyncRequest('POST', GLOBALS.contextPath + '/ajax/loadCRSRankings?subjectId=' + sid, callback, 'roleId=' + roleId);
  };
}
