//[
//'x|x|x|x|x',// Shop score | GOOD Total | NEUTRAL Total | BAD Total | Total Reviews
//'',//1 = Div Key
//'',//2 = Shop Name
//'',//3 = Site URL
//'',//4 = Address
//'',//5 = Phone number(s)
//'',//6 = Ebay store URL
//'',//7 = Extra Information
//'x|x|x', //Format="**0,1 or 2|Review Text|Reviewer & Date"   **0 = Thumbs Down, 1 = Neutral Vote, 2 = Thumbs up
//],


function Sort_DB_Data(ShopArray){
  // Calculate the shop scores and add them to each record
  // Then build the single line record for easier sorting
  for (i=0;i<ShopArray.length;i++)
    {
    if (ShopArray[i][1])
      {
      // Calculate the shop scores and add them to each record
      var down=0;
      var neutral=0;
      var good=0;
	  var total=0;
	  var score=0;
	  
      var ii=ShopArray[i].length;
      while (ii>7)
        {
        if (ShopArray[i][ii])
	      {
		  total=++total;
	      myReview = ShopArray[i][ii].split("|");
          if (myReview[0]=="0"){	//BAD Vote
			  down=++down;
			  score=--score;		//-1 point for a bad vote
			  }
          if (myReview[0]=="1"){	//NEUTRAL Vote
			  neutral=++neutral;
			  score=++score;		//+1 point for a neutral vote
			  }
          if (myReview[0]=="2"){	//GOOD Vote
			  good=++good;
			  score=++score+1;		//+2 points for a good vote
			  }	
          }
        ii=ii-1
      }
      ShopArray[i][0]=score + '|' + down + '|' + neutral + '|' + good + '|' + total;
      //'x|x|x|x|x',// Shop score | BAD Total | NEUTRAL Total | GOOD Total | Total Reviews
    }
  } 

  //Sort the shops into ascending score order (bubble sort method)
  var x, y, holder;
  for(x = 0; x < ShopArray.length; x++) {
    for(y = 0; y < (ShopArray.length-1); y++) {
      if(parseFloat(ShopArray[y][0]) < parseFloat(ShopArray[y+1][0])) {
        holder = ShopArray[y+1];
        ShopArray[y+1] = ShopArray[y];
        ShopArray[y] = holder;
	  }
    }
  }
  
  return ShopArray;

}
