﻿function CompanySearch_SortResult() {
    var items = $(".DealerSearchItem");
    items.sort(CompanySearch_SortItems);

    $("#ResultItems").empty();

    for (var i = 0; i < items.length; i++) {
        $("#ResultItems").append(items[i]);
    }

    $("#ResultItems").fadeIn();
}

function CompanySearch_SortItems(a, b) {
    var a_name = $(a).children(".PartnerName").html().toLowerCase();
    var b_name = $(b).children(".PartnerName").html().toLowerCase();

    var a_type = $(a).children(".PartnerType").html();
    if (a_type === null || a_type === "" || isNaN(a_type)) a_type = 100;

    var b_type = $(b).children(".PartnerType").html();
    if (b_type === null || b_type === "" || isNaN(b_type)) b_type = 100;

    var a_priority = $(a).children(".Prioritet").html();
    if (a_priority === null || a_priority === "" || isNaN(a_priority)) a_priority = 100;

    var b_priority = $(b).children(".Prioritet").html();
    if (b_priority === null || b_priority === "" || isNaN(b_priority)) b_priority = 100;

    var result = 0;

    if (parseInt(a_type) < parseInt(b_type)) {
        result = -1;
    }
    else if (parseInt(a_type) > parseInt(b_type)) {
        result = 1;
    }
    else if (parseInt(a_priority) < parseInt(b_priority)) {
        result = -1;
    }
    else if (parseInt(a_priority) > parseInt(b_priority)) {
        result = 1;
    }
    else if (a_name < b_name) {
        result = -1;
    }
    else if (a_name > b_name) {
        result = 1;
    }

    //alert(result + " ** A:" + a_typePriority + "/" + a_priority + "/" + a_name + "  B:" + b_typePriority + "/" + b_priority + "/" + b_name);

    return result;
}

function CompanySearch_GetTypePriority(type) {

    if ("" == type || 0 == type.length)
        return 0;

    type = type.toLowerCase();

    if ("premium distributor" == type) {
        return 100;
    }
    else if ("rep" == type) {
        return 90;
    }
    else if ("premium dealer" == type) {
        return 80;
    }
    else if ("authorised distributor" == type) {
        return 70;
    }
    else if ("authorised dealer" == type) {
        return 60;
    }
    else {
        return 0;
    }
}
