$(document).ready(function() {

  
  $("#shop_topcat").change(function () {
    
    if($(this).val()<1) {
      reset_subs('disable');
    
    } else {
      reset_subs('enable');
      //TODO: json this -> get the subcats of this topcat - it returns a full html options list
      $.post("../shop/manage/index.php?q=ajax/shop_ajax_get_subcats", { vid: $(this).val() },
        function(data){
        $('#shop_subcat').find('option:gt(0)').remove();
        $("#shop_subcat").append(data) ;
      });

    }
    
  })
  
  $("#shop_subcat").change(function () {

    if($(this).val()<1) {
      $("#shop_status").attr('disabled','disabled');

    } else {
      $("#shop_status").removeAttr('disabled');
    }

  })
  
  
  // check if the radio val is greater than zero, otherwise use the user_price val 
  $("#uc-donate-form").submit(function() {
    var price = 0 ;
     if($("input[name='set_price']:checked").val() <1 ) {
       price = $("input[name='user_price']").val() ;
     
     } else {
       price = $("input[name='set_price']:checked").val() ;
     }

     price = price.length<1 || isNaN(price) ? 0 : parseFloat(price) ;
     if(price < 5 ) {
       alert('The minimum donation amount is £5.00') ; 
       return false ;
     }
     
     if(!$("input[name='regularity']:checked").val()) {
       alert('Please select the donation frequency') ;
       return false ;     
     }
     
     // good to go
     return true ;
  })
  
  
   

});
 
 
 /**
  * disable or enable the form elements 
  */
 function reset_subs(action) {

   switch(action) {
     case 'disable':
      $("#shop_subcat").attr('disabled','disabled');
      $("#shop_status").attr('disabled','disabled');

      break ;

     case 'enable' :
      $('#shop_subcat').removeAttr('disabled') ;
      $("#shop_status").attr('disabled','disabled');
      break ;

   }
 
 }
