/*
<input type="submit" value="Add" name="add" class="addButton" 
onclick="return choosevolume('[% product.uid %]','49999','Product_view',volume);">
this function takes an array which is created on the page and loops thr it to see if 
a) the item is within the specified range
b) what variant uid is a best fit 
c) assigns that uid to the valuefield
d) gives some nice messages

*/

function choosevolume(m,max,fmname,volume,type){
  if(volume){/*did they pass an array, if not do not pass go.*/
    var fm;
    /* all forms should have an id */
    /* but in case they dont */
    if(document.getElementById(fmname)){
      fm = document.getElementById(fmname);
    }
    else{
      fm = document[fmname];
    }
    
    /*is this add or update?*/
    if(type=='update'){
      /*lets see what variables we have on the page*/
      var regex = /^SETBASKET(\d*)_(\d*)$/;
      
      for (var i=0; i<fm.elements.length; i++) {
        var el = fm.elements[i];
        var name = el.name;
        var quantity = el.value;
        var price_per_qty = el.value;
        if(quantity){
          var results = name.match(regex);
          if(results){
            var productid = results[1];
            var variantid = results[2];
            
            var datad= do_check(productid,quantity,volume,max,variantid);
            if(datad[1]){/*did we error?*/
              alert(datad[1]);
              el.focus();
              return false;
            }
            else{
              /*do we need to change the variant*/
              if(datad[0]!=variantid){
                el.value=0;
                var afield = "ADDBASKET"+productid;
                var vfield = "VALUEBASKET"+productid;
                fm[vfield].value = datad[0];
                fm[afield].value = quantity;
              }
            }
          }
        }
      }      
      return true;
    }
    else{/*this is an add*/
      var field = "ADDBASKET"+m;
      var vfield = "VALUEBASKET"+m;
      var quantity = fm[field].value;
      var price_per_qty = fm[field].value;
      var datad= do_check(m,quantity,volume,max,0);
      
      if(datad[1]){/*did we error?*/
        alert(datad[1]);
        fm[field].focus();
        return false;
      }
      else{
  	    fm[vfield].value = datad[0];
	      return true;
  	  }    
    }     
  }
	return true;
}

function do_check(productid,quantity,volume,max,variantid){
  var lastuid = variantid;
  var error = "";
  var price_per_qty = 0;
  var data = new Array();
  data[0] = lastuid;
  data[1] = error;
  if(quantity > max){
    data[1] = 'Please contact us direct for quantities of this size';
    return data;  
  }
  else{  
    if(volume[productid]){
     for (var i=volume[productid].length; i>0; i--) {
        if(volume[productid][i]){
          price_per_qty = volume[productid][i]['price_per_qty'];
          
          if((quantity >= i && !(quantity % price_per_qty)) || (quantity > 1000 && !((quantity - 500) % price_per_qty) && ((quantity - 500) >= i ))){
            lastuid = volume[productid][i]['uid'];
            break;
          }
          price_per_qty = volume[productid][i]['price_per_qty'];
        }
      }
      if(!lastuid){
        data[1] = 'Please make sure your quantity specified is a multiple of the Unit quantity ';
        return data;
      }
    }
  
    data[0] = lastuid;
    return data;  
  }
}
