﻿// Sorts the mpans/ca numbers in listbox
function SortAccountItems(mpansListBoxId, sortColumnId, sortOrderId) {
    toggle_visibility('sorting');
    toggle_enabled('sortItems');
    setTimeout(function() { Sort(mpansListBoxId, sortColumnId, sortOrderId) }, 250);
}

function toggle_visibility(id) {
    var e = document.getElementById(id);
    if (e != null) {
        if (e.style.display == 'block')
            e.style.display = 'none';
        else
            e.style.display = 'block';
    }
}

function toggle_enabled(id) 
{
    var e = document.getElementById(id);
    if (e != null) 
    {
        if (e.disabled == true)
            e.disabled = false;
        else
            e.disabled = true;

        if (e.childNodes && e.childNodes.length > 0) 
        {
            for (var x = 0; x < e.childNodes.length; x++) 
            {
                toggle_enabled(e.childNodes[x].id);
            }
        }
    }
}

function Sort(mpansListBoxId, sortColumnId, sortOrderId) {

    var list1 = GetObj(mpansListBoxId);
    var sortCol = GetObj(sortColumnId);
    var sortOrder = GetObj(sortOrderId);

    var sort = sortCol.options[sortCol.selectedIndex].value + sortOrder.options[sortOrder.selectedIndex].value;

    switch(sort) {
        case "mpanOrCaNumberAsc":
          arrAccountItems.sort(SortArrayMpanOrCaNumberAsc);
          break;
        case "mpanOrCaNumberDesc":
          arrAccountItems.sort(SortArrayMpanOrCaNumberDesc);
          break;
        case "customerOrSiteNameAsc":
          arrAccountItems.sort(SortArrayByCustomerOrSiteNameAsc);
          break;
        case "customerOrSiteNameDesc":
          arrAccountItems.sort(SortArrayByCustomerOrSiteNameDesc);
          break;
        case "postcodeAsc":
          arrAccountItems.sort(SortArrayByPostcodeAsc);
          break;
        case "postcodeDesc":
          arrAccountItems.sort(SortArrayByPostcodeDesc);
          break;
        default:
          break;
    }
    
    // clear current values from listbox
    list1.length = 0;

    
   // determine the account type and add sorted items to listbox
   for (i = 0; i < arrAccountItems.length; i++) {
        if (arrAccountItems[i][2] != undefined) {
            list1.options.add(new Option(arrAccountItems[i][0] + " - " + arrAccountItems[i][1] + " " + arrAccountItems[i][2], arrAccountItems[i][0]));
        }
        else {
            list1.options.add(new Option(arrAccountItems[i][0] + " - " + arrAccountItems[i][1], arrAccountItems[i][0]));
        }
    }
   
   //Align the search box selected item with the current drop down list item
   var docId = document.getElementById('typeToSearch');
   if (docId != null) 
   {
         docId.getElementsByTagName('select')[0].selectedIndex = sortCol.selectedIndex;
   }

   toggle_visibility('sorting');
   toggle_enabled('sortItems');
}

// sort by mpan/ca number asc
function SortArrayMpanOrCaNumberAsc(a, b) {
    return (a[0] - b[0]);
}

// sort by mpan/ca number desc
function SortArrayMpanOrCaNumberDesc(a, b) {
    return (b[0] - a[0]);
}

// sort by customer name/site name asc
function SortArrayByCustomerOrSiteNameAsc(a, b) {
    var x = a[1].toLowerCase();
    var y = b[1].toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

// sort by customer name/site name desc
function SortArrayByCustomerOrSiteNameDesc(a, b) {
    var x = a[1].toLowerCase();
    var y = b[1].toLowerCase();
    return ((y < x) ? -1 : ((y > x) ? 1 : 0));
}

// sort by postcode asc
function SortArrayByPostcodeAsc(a, b) {
    var x = a[2].toLowerCase();
    var y = b[2].toLowerCase();
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}

// sort by postcode desc
function SortArrayByPostcodeDesc(a, b) {
    var x = a[2].toLowerCase();
    var y = b[2].toLowerCase();
    return ((y < x) ? -1 : ((y > x) ? 1 : 0));
}


// search listBox
function SearchAccountItems(searchTextboxId, searchFilterId, mpanListboxId) {
    var searchTerms = GetObj(searchTextboxId);
    var searchFilter = GetObj(searchFilterId);
    var list1 = GetObj(mpanListboxId);
    
    var index = 0;
    
    // set the selected search filter
    switch(searchFilter.value) {
        case "mpanOrCaNumber":
          index = 0;
          break;
        case "customerOrSiteName":
          index = 1;
          break;
        case "postcode":
          index = 2;
          break;
        default:
          break;
    }
    
    var regExp = new RegExp("^" + searchTerms.value, ["i"]);
    
    // perform the search
    if (searchTerms.value.length > 0) {
        ClearSelectedListItems(list1);
        for (i = 0; i < arrAccountItems.length; i++) {
            if (regExp.test(arrAccountItems[i][index])) {
                list1.options[i].selected = true;
                break;
            }
            else {
                if (list1.options[i].selected == true) {
                    list1.options[i].selected = false;
                }
            }
        }
    }
    else {
        // no text supplied in search box - clear all
        for (i = 0; i < list1.length; i++) {
            list1.options[i].selected = false;
        }
    }
}

// Clears all selected items from listBox
function ClearSelectedListItems(theList) {
    for (i = 0; i < theList.length; i++) {
        theList.options[i].selected = false;
    }
}

// Clears the search textbox
function ClearSearchText(controlId) {
    var search = GetObj(controlId);
    
    if (search.value == "Search MPANs" 
        || search.value == "Search Customer Acc No."
        || search.value == "Search Tender List"
        || search.value == "Search...") 
    {
        search.value = "";
    }
}
