// ----------------------------------------------------------------
// Project:  Wireless Data Lookup Chart
// Module:   Sort
// ----------------------------------------------------------------
// Filename: sort.js
// URL:      http://www.classaxe.com/wireless/data/sort.js
// Version:  1.0
// Date:     17th December 2000
// Author:   Martin Francis
// ----------------------------------------------------------------
// ****************
// * Sorting Sets *
// ****************
function sortingSets(what,how) 
{ if (top.ItemsMatched!=null){
  top.window.status=('Sorting records by '+what+' - please wait...')
  var width=200 ; var height=20
  Wait2=window.open('','Wait2', 'width='+width+',height='+height+',resizable=1,menubar=0,location=0,toolbar=0')
//  centerPopup(Wait2,width,height)
  var out= "<html>\n<head>\n<title>Sorting by "+what+"... </title>\n</head>\n\n"
      out+="<body BGCOLOR='#e0ffe0' background='wallpaper.gif' topmargin='5'>\n<table height='100%' width='100%' border='0'>"
      out+="\n<tr>\n  <td valign='middle' align='center'><img src='sorting.gif' width='122' height='22'><br>\n"
      out+="<font face='arial' color='Red'><b>"+what+" Please wait.</td>\n</tr>\n"
      out+="<tr><td height='10'><img src='progress.gif' height='8' width='180' border='1'></table></b>\n</body>\n</html>"
  Wait2.focus();Wait2.document.write(out);Wait2.document.close()
  top.ItemsMatched.sort(how);
  return true }
}

// *******************
// * Sorting Devices *
// *******************
function sortingDevices(what,how) 
{ if (top.DevicesMatched!=null){
  top.window.status=('Sorting devices by '+what+' - please wait...')
  Wait3=window.open('','Wait3', 'width=200,height=20,resizable=0,menubar=0,location=0,toolbar=0');
  var out= "<html>\n<head>\n<title>Sorting by "+what+"... </title>\n</head>\n\n"
      out+="<body BGCOLOR='#e0ffe0' background='wallpaper.gif' topmargin='5'>\n<table height='100%' width='100%' border='0'>"
      out+="\n<tr>\n  <td valign='middle' align='center'><img src='sorting.gif' width='122' height='22'><br>\n"
      out+="<font face='arial' color='Red'><b>by "+what+".<br>Please wait.</td>\n</tr>\n"
      out+="<tr><td height='10'><img src='progress.gif' height='8' width='180' border='1'></table></b>\n</body>\n</html>"

  Wait3.focus();Wait3.document.write(out);Wait3.document.close()
  top.DevicesMatched.sort(how);
  return true }
}


// *********************
// * Set Sort Routines *
// *********************
function setsByMake(a,b)
{ var anew=a.make.toLowerCase();var bnew=b.make.toLowerCase()
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByModel(a,b)
{ var anew=a.model.toLowerCase();var bnew=b.model.toLowerCase()
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsBySerials(a,b)
{ var anew=a.serials.toLowerCase();var bnew=b.serials.toLowerCase()
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByRefNo(a,b)
{ var anew=a.referencesNo ; var bnew=b.referencesNo
  if (anew < bnew) return 1 ; if (anew > bnew) return -1  ; return 0 }

function setsByNotes(a,b)
{ var anew=a.notes.toLowerCase();var bnew=b.notes.toLowerCase()
  if (anew=="") anew=String.fromCharCode(255);  if (bnew=="") bnew=String.fromCharCode(255)
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByPic(a,b)
{ var anew=a.pic !=""?1:0; var bnew=b.pic !=""?1:0;
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByDate(a,b)
{ var anew=a.date;var bnew=b.date
  if (anew=="") anew="2000" ; if (bnew=="") bnew="2000"
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByPrice(a,b)
{ var anew=a.price ; var bnew=b.price
  if (anew==0) anew=999999 ; if (bnew==0) bnew=999999
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByBands(a,b)
{ var anew=a.bands.length ; var bnew=b.bands.length
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByIF(a,b)
{ var anew=parseFloat(a.IF);var bnew=parseFloat(b.IF)
  if (isNaN(anew)) anew=999999 ; if (isNaN(bnew)) bnew=999999
  if (a.IF=="TRF") anew=0 ; if (b.IF=="TRF") bnew=0
  if ((/MHz/).test(a.IF)) anew*=1000 ; if ((/MHz/).test(b.IF)) bnew*=1000
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByDeviceCount(a,b)
{ var anew=a.deviceCount ; var bnew=b.deviceCount
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByAC(a,b)  // show AC sets first
{ var anew=a.isAC ; var bnew=b.isAC
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByDC(a,b)  // show DC sets first
{ var anew=a.isDC ; var bnew=b.isDC
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByBA(a,b)  // show Battery operated sets first
{ var anew=a.isBA ; var bnew=b.isBA
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByCA(a,b)  // show car radios first
{ var anew=a.isCA ; var bnew=b.isCA
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByFM(a,b)  // show FM sets first
{ var anew=a.hasFM ; var bnew=b.hasFM
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByCL(a,b)  // show clock radios first
{ var anew=a.hasCL ; var bnew=b.hasCL
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByTI(a,b)  // show sets with tuning indicators first
{ var anew=a.hasTI ; var bnew=b.hasTI
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function setsByGR(a,b)  // show sets with gram decks first
{ var anew=""+a.gramType+a.gramSpeed ; var bnew=""+b.gramType+b.gramSpeed
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByDeck(a,b)
{ var anew=a.gramDeck.toLowerCase();var bnew=b.gramDeck.toLowerCase()
  if (anew=="") anew=String.fromCharCode(255);  if (bnew=="") bnew=String.fromCharCode(255)
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function setsByPic(a,b)
{ var anew=a.pic !=""?1:0; var bnew=b.pic !=""?1:0;
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

// *************************
// * Devices Sort Routines *
// *************************
function devicesByCode(a,b)
{ var anew=a.code;var bnew=b.code
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function devicesByType(a,b)
{ var anew=a.type;var bnew=b.type
  if (anew=="") anew="zzz" ; if (bnew=="") bnew="zzz"
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function devicesByBase(a,b)
{ var anew=a.base;var bnew=b.base
  if (anew=="") anew="zzz" ; if (bnew=="") bnew="zzz"
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function devicesByFv(a,b)
{ var anew = a.fv !=0 ? a.fv : 999; var bnew = b.fv !=0 ? b.fv : 999;
  if (anew < bnew) return -1  ; if (anew > bnew) return 1 ; return 0 }

function devicesByFc(a,b)
{ var anew = a.fc !=0 ? a.fc : 999; var bnew = b.fc !=0 ? b.fc : 999;
  if (anew < bnew) return -1  ; if (anew > bnew) return 1 ; return 0 }

function devicesByNotes(a,b)
{ var anew=a.notes;var bnew=b.notes
  if (anew=="") anew="zzz" ; if (bnew=="") bnew="zzz"
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }

function devicesByDiag(a,b)
{ var anew=a.diag !=""?1:0; var bnew=b.diag !=""?1:0;
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function devicesByPic(a,b)
{ var anew=a.pic !=""?1:0; var bnew=b.pic !=""?1:0;
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }

function devicesByCount(a,b)
{ var anew=a.count+a.countSubs;var bnew=b.count+b.countSubs
  if (anew < bnew) return 1  ; if (anew > bnew) return -1 ; return 0 }


// *************************
// * Popup device set sort *
// *************************
function setsWithDevicesByDate(a,b)
{ var anew=""+Item[a].date;var bnew=""+Item[b].date
  if (anew=="") anew="2000" ; if (bnew=="") bnew="2000"
  if (anew < bnew) return -1 ; if (anew > bnew) return 1  ; return 0 }
  
// ***********************
// * Initialise Database *
// ***********************
// At this point all the device details extracted live in an associative  array - Device["deviceName"]
// This structure was needed to enable all the device data from the  SET  array
// to be parsed efficiently into the Device["deviceName"] array.
//
// In order to reference this data, we created a second array to hold pointers  - DeviceNames[n]
//
// In order to be able to sort this data by any column we need to focus all the
// data held in the first array into a non-associative array which we can sort  - DeviceList[n]
//
// We leave the Device["DeviceName"] array because we need this for the
// hyperlinks from the sets results page

DeviceList=new Array                   // Create the new non-associative array
for(i=0;i<DeviceNames.length;i++)      // Go through the entire list
  DeviceList[i]=Device[DeviceNames[i]] // Copy the old to the new
//DeviceList.sort(devicesByCount)        // Numerically sorts by device count (not needed since presorted anyway)
delete DeviceNames                     // Don't need this any more
//delete Device                        // Or this


// Now close "Loading" window:
Loading.window.close()

