function GetAvailModels(device, brands, featureMatch)
{
  var models = Array();

  for (i = 0; i < boxes.length - 1; i++)
  {
    var box = boxes[i];

    if (box["device"] == device && (!featureMatch || featureMatch(box)) &&
       (brands == 'Any' || brands.indexOf(box['brand']) >= 0))
    {
    
       models[box['brand'] + ' ' + box['model']] = box;
    }
  }

  return models;
}

function GetBestDeals(models, monthlyt, upfrontt, textt, voicet)
{
  var arr = Array();

  var monthlyp = parseFloat(monthlyt);
  var upfrontp = parseFloat(upfrontt);
  var textp = parseInt(textt);
  var voicep = parseInt(voicet);
  var total = 0;
  if (textt != 'Any') total += textp;
  if (voicet != 'Any') total += voicep;

  for (i in models)
  {
    var model = models[i];

    var variants = model.variants;

    for (j in variants)
    {
      var variant = variants[j];

      var deals = variant.deals;

      var best = 99999;

      for (k in deals)
      {
        var deal = deals[k];

        if (deal.cost > best) break;

        if (monthlyt != 'Any' && deal.monthly > monthlyp) continue;
        if (upfrontt != 'Any' && deal.upfront > upfrontp) continue;

        if (deal.tariff.name.substring(0, 3) == 'Mix')
        {
          if (deal.tariff.texts < total) continue;
        }
        else
        {
          if (textt != 'Any' && deal.tariff.texts < textp) continue;
          if (voicet != 'Any' && deal.tariff.mins < voicep) continue;
        }

        arr[arr.length] = deal;

        best = deal.cost;
      }
    }
  }

  return (arr);
}

function RenderDeal(deal)
{
    var variant = deal.variant;
    var box = variant.box;
    var specs = box.specs;

    var title = box.brand + ' ' + box.model + ' ' + variant.color;
    var loc = 'http://track.three.co.uk/ctv2/ar.aspx?aff_id=1417&link_id=fc&url=http%3a%2f%2fstore.three.co.uk%2fview%2fproduct%2fql_catalog%2fthreecatdevice%2f' + deal.zcode;

    var img = 'http://three-stores.co.uk/thumb.jpg.php?img=' + variant.image + '&mw=60&mh=80';

    var b = document.createElement('div');

    b.className = 'child';  

    s = '<div style="height:44px">' +
        '<img width="16" height="16" style="float:left" alt="3 Store" title="3 Store" ' +
        'src="http://three-stores.co.uk/wp-content/themes/iMobile/images/quickpick/3.jpg" border="0" />&nbsp;' +
        '<b><span style="font-family:Arial;color:navy;font-size:15px">' +
        title + '</span></b></div>' +
        '<a style="text-decoration:none" target="_blank" href="' + loc + '" rel="nofollow"><img border="0" style="float:right"  src="' +
        img + '" ' +
        'border="0" title="3 Store ' + title + '. ' + specs.replace('<br>',' ').replace('"','').replace('"™', '') + '" ' +
        ' /></a><div style="height:86px"><br />';

    if (deal.upfront == 0 && ('' + deal.tariff.mins) != 'undefined')
      s += '- FREE ' + box.device + '<br />';
    else
      s += '- &#163;' + deal.upfront + ' up front<br />';

    if (deal.tariff.texts == 0 || ('' + deal.tariff.texts) == 'undefined')
    {
      if (('' + deal.tariff.mins) != 'undefined')
        s += ' - ' + deal.tariff.mins + ' Mixed Texts/Minutes<br />';
    }
    else
    {
      if (deal.tariff.mins != 0)
        s += ' - ' + deal.tariff.mins + ' Minutes<br />';

      if (deal.tariff.texts != 0)
        if (deal.tariff.texts == 2999)
          s += ' - Unlimited Texts<br />';
        else
          s += ' - ' + deal.tariff.texts + ' Texts<br />';
    }

    var getData = /([\d\.]+)\s*gb/i;

    if (getData.test(deal.tariff.name))
      s += ' - ' + RegExp.$1 + ' GB Data<br />';
     
     var oldprice = '';

     if (deal.promoamt != 0)
       oldprice = '<span style="color:#FF5f00"><strike>&#163;' + (deal.monthly + deal.promoamt) + '</strike></span> ';

    s += '</div><div style="height:38px"><span style="color:#00ADEF"><b>' +
        'Only <span style="background:yellow">&#163;' + deal.monthly + '</span> ' +
        'a month (x' + deal.months + ')</b></span><br />' +
        '<center><a style="text-decoration:none" target="_blank" href="' + loc + '" rel="nofollow"><div style="background:#F7FFF7;color:#FF7F00;margin:6px"><u>More Deals &gt;&gt;</u></div></a></center></div>';


    b.innerHTML = s;

   return (b);
}

