function setup_sample_links(is_full, ids)
{
  if (is_full){
    $('.sample_link').addClass('disabled');
  }
  else
  {
    $('.sample_link').each(function(){
      if (is_in($(this).attr('id').split('_')[1], ids)){
        $(this).addClass('disabled');
      }
      else
      {
        $(this).removeClass('disabled');
      }
    });
  }
}

function is_in(value, array)
{
  answer = false;
  for (i in array){
    if (value == array[i]) answer = true;
  }
  return answer;
}

$(function(){
  $('#sample_form').live('submit', function(){
    errors = [];
    if ($('#sample_form #id_name').val() == ''){
      errors[errors.length] = 'Name cannot be blank';
    }
    address = $('#sample_form #id_address').val();
    email   = $('#sample_form #id_email').val().replace(/^\s+|\s+$/g,"");
    telephone = $('#sample_form #id_tel').val().replace(/^\s+|\s+$/g,"");
    if (email == '' || telephone == '') {
      errors[errors.length] = 'You must fill in both "email" and "telephone"';
    }

    if (errors.length > 0)
    {
      alert(errors[0]);

      return false;
    }
    else
    {
      return true;
    }
  });
  $('#sample_basket_content .close-link').live('click', function(){
    $('#sample_basket_content').remove();
    return false;
  });

  $('.sample_link').click(function(){
    $('#samplebasket img').show();
    $('#sample_basket_content').fadeOut();
    $('#samplebasket').load('/sample_basket/add_sample', {sample: $(this).attr('id').split('_')[1]});
  });

  $('#samplebasket_link').live('click', function(){
    if ($('#products-wrap').length == 0) {
      $('body').append('<div id="products-wrap"></div>');
    }
    $('#products-wrap').append('<span id="samplebasket_holder"></span>');
    $('#samplebasket_holder').load('/sample_basket/content');
    return false;
  });

  $('#samplebasket_holder .sample_remove_link').live('click', function(){
    $(this).parent().fadeOut(200, function(){
      $(this).remove();
    });
    $('#samplebasket img').show();
    $('#samplebasket').load('/sample_basket/remove_sample', {sample: $(this).attr('id').split('_')[1]});
  });
});

